]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - usr.sbin/bhyve/pci_lpc.c
MFC 259942,262274,263035,263054,263211,263744,264179,264324,264468,264631,
[FreeBSD/stable/10.git] / usr.sbin / bhyve / pci_lpc.c
1 /*-
2  * Copyright (c) 2013 Neel Natu <neel@freebsd.org>
3  * Copyright (c) 2013 Tycho Nightingale <tycho.nightingale@pluribusnetworks.com>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/types.h>
34 #include <machine/vmm.h>
35 #include <machine/vmm_dev.h>
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40
41 #include <vmmapi.h>
42
43 #include "acpi.h"
44 #include "inout.h"
45 #include "pci_emul.h"
46 #include "pci_lpc.h"
47 #include "uart_emul.h"
48
49 #define IO_ICU1         0x20
50 #define IO_ICU2         0xA0
51
52 SET_DECLARE(lpc_dsdt_set, struct lpc_dsdt);
53 SET_DECLARE(lpc_sysres_set, struct lpc_sysres);
54
55 #define ELCR_PORT       0x4d0
56 SYSRES_IO(ELCR_PORT, 2);
57
58 #define IO_TIMER1_PORT  0x40
59
60 #define NMISC_PORT      0x61
61 SYSRES_IO(NMISC_PORT, 1);
62
63 static struct pci_devinst *lpc_bridge;
64
65 #define LPC_UART_NUM    2
66 static struct lpc_uart_softc {
67         struct uart_softc *uart_softc;
68         const char *opts;
69         int     iobase;
70         int     irq;
71         int     enabled;
72 } lpc_uart_softc[LPC_UART_NUM];
73
74 static const char *lpc_uart_names[LPC_UART_NUM] = { "COM1", "COM2" };
75
76 /*
77  * LPC device configuration is in the following form:
78  * <lpc_device_name>[,<options>]
79  * For e.g. "com1,stdio"
80  */
81 int
82 lpc_device_parse(const char *opts)
83 {
84         int unit, error;
85         char *str, *cpy, *lpcdev;
86
87         error = -1;
88         str = cpy = strdup(opts);
89         lpcdev = strsep(&str, ",");
90         if (lpcdev != NULL) {
91                 for (unit = 0; unit < LPC_UART_NUM; unit++) {
92                         if (strcasecmp(lpcdev, lpc_uart_names[unit]) == 0) {
93                                 lpc_uart_softc[unit].opts = str;
94                                 error = 0;
95                                 goto done;
96                         }
97                 }
98         }
99
100 done:
101         if (error)
102                 free(cpy);
103
104         return (error);
105 }
106
107 static void
108 lpc_uart_intr_assert(void *arg)
109 {
110         struct lpc_uart_softc *sc = arg;
111
112         assert(sc->irq >= 0);
113
114         vm_isa_pulse_irq(lpc_bridge->pi_vmctx, sc->irq, sc->irq);
115 }
116
117 static void
118 lpc_uart_intr_deassert(void *arg)
119 {
120         /* 
121          * The COM devices on the LPC bus generate edge triggered interrupts,
122          * so nothing more to do here.
123          */
124 }
125
126 static int
127 lpc_uart_io_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
128                     uint32_t *eax, void *arg)
129 {
130         int offset;
131         struct lpc_uart_softc *sc = arg;
132
133         if (bytes != 1)
134                 return (-1);
135
136         offset = port - sc->iobase;
137
138         if (in)
139                 *eax = uart_read(sc->uart_softc, offset); 
140         else
141                 uart_write(sc->uart_softc, offset, *eax);
142
143         return (0);
144 }
145
146 static int
147 lpc_init(void)
148 {
149         struct lpc_uart_softc *sc;
150         struct inout_port iop;
151         const char *name;
152         int unit, error;
153
154         /* COM1 and COM2 */
155         for (unit = 0; unit < LPC_UART_NUM; unit++) {
156                 sc = &lpc_uart_softc[unit];
157                 name = lpc_uart_names[unit];
158
159                 if (uart_legacy_alloc(unit, &sc->iobase, &sc->irq) != 0) {
160                         fprintf(stderr, "Unable to allocate resources for "
161                             "LPC device %s\n", name);
162                         return (-1);
163                 }
164
165                 sc->uart_softc = uart_init(lpc_uart_intr_assert,
166                                     lpc_uart_intr_deassert, sc);
167
168                 if (uart_set_backend(sc->uart_softc, sc->opts) != 0) {
169                         fprintf(stderr, "Unable to initialize backend '%s' "
170                             "for LPC device %s\n", sc->opts, name);
171                         return (-1);
172                 }
173
174                 bzero(&iop, sizeof(struct inout_port));
175                 iop.name = name;
176                 iop.port = sc->iobase;
177                 iop.size = UART_IO_BAR_SIZE;
178                 iop.flags = IOPORT_F_INOUT;
179                 iop.handler = lpc_uart_io_handler;
180                 iop.arg = sc;
181
182                 error = register_inout(&iop);
183                 assert(error == 0);
184                 sc->enabled = 1;
185         }
186
187         return (0);
188 }
189
190 static void
191 pci_lpc_write_dsdt(struct pci_devinst *pi)
192 {
193         struct lpc_dsdt **ldpp, *ldp;
194
195         dsdt_line("");
196         dsdt_line("Device (ISA)");
197         dsdt_line("{");
198         dsdt_line("  Name (_ADR, 0x%04X%04X)", pi->pi_slot, pi->pi_func);
199         dsdt_line("  OperationRegion (P40C, PCI_Config, 0x60, 0x04)");
200
201         dsdt_indent(1);
202         SET_FOREACH(ldpp, lpc_dsdt_set) {
203                 ldp = *ldpp;
204                 ldp->handler();
205         }
206
207         dsdt_line("");
208         dsdt_line("Device (PIC)");
209         dsdt_line("{");
210         dsdt_line("  Name (_HID, EisaId (\"PNP0000\"))");
211         dsdt_line("  Name (_CRS, ResourceTemplate ()");
212         dsdt_line("  {");
213         dsdt_indent(2);
214         dsdt_fixed_ioport(IO_ICU1, 2);
215         dsdt_fixed_ioport(IO_ICU2, 2);
216         dsdt_fixed_irq(2);
217         dsdt_unindent(2);
218         dsdt_line("  })");
219         dsdt_line("}");
220
221         dsdt_line("");
222         dsdt_line("Device (TIMR)");
223         dsdt_line("{");
224         dsdt_line("  Name (_HID, EisaId (\"PNP0100\"))");
225         dsdt_line("  Name (_CRS, ResourceTemplate ()");
226         dsdt_line("  {");
227         dsdt_indent(2);
228         dsdt_fixed_ioport(IO_TIMER1_PORT, 4);
229         dsdt_fixed_irq(0);
230         dsdt_unindent(2);
231         dsdt_line("  })");
232         dsdt_line("}");
233         dsdt_unindent(1);
234
235         dsdt_line("}");
236 }
237
238 static void
239 pci_lpc_sysres_dsdt(void)
240 {
241         struct lpc_sysres **lspp, *lsp;
242
243         dsdt_line("");
244         dsdt_line("Device (SIO)");
245         dsdt_line("{");
246         dsdt_line("  Name (_HID, EisaId (\"PNP0C02\"))");
247         dsdt_line("  Name (_CRS, ResourceTemplate ()");
248         dsdt_line("  {");
249
250         dsdt_indent(2);
251         SET_FOREACH(lspp, lpc_sysres_set) {
252                 lsp = *lspp;
253                 switch (lsp->type) {
254                 case LPC_SYSRES_IO:
255                         dsdt_fixed_ioport(lsp->base, lsp->length);
256                         break;
257                 case LPC_SYSRES_MEM:
258                         dsdt_fixed_mem32(lsp->base, lsp->length);
259                         break;
260                 }
261         }
262         dsdt_unindent(2);
263
264         dsdt_line("  })");
265         dsdt_line("}");
266 }
267 LPC_DSDT(pci_lpc_sysres_dsdt);
268
269 static void
270 pci_lpc_uart_dsdt(void)
271 {
272         struct lpc_uart_softc *sc;
273         int unit;
274
275         for (unit = 0; unit < LPC_UART_NUM; unit++) {
276                 sc = &lpc_uart_softc[unit];
277                 if (!sc->enabled)
278                         continue;
279                 dsdt_line("");
280                 dsdt_line("Device (%s)", lpc_uart_names[unit]);
281                 dsdt_line("{");
282                 dsdt_line("  Name (_HID, EisaId (\"PNP0501\"))");
283                 dsdt_line("  Name (_UID, %d)", unit + 1);
284                 dsdt_line("  Name (_CRS, ResourceTemplate ()");
285                 dsdt_line("  {");
286                 dsdt_indent(2);
287                 dsdt_fixed_ioport(sc->iobase, UART_IO_BAR_SIZE);
288                 dsdt_fixed_irq(sc->irq);
289                 dsdt_unindent(2);
290                 dsdt_line("  })");
291                 dsdt_line("}");
292         }
293 }
294 LPC_DSDT(pci_lpc_uart_dsdt);
295
296 static void
297 pci_lpc_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi,
298                int baridx, uint64_t offset, int size, uint64_t value)
299 {
300 }
301
302 uint64_t
303 pci_lpc_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi,
304               int baridx, uint64_t offset, int size)
305 {
306         return (0);
307 }
308
309 #define LPC_DEV         0x7000
310 #define LPC_VENDOR      0x8086
311
312 static int
313 pci_lpc_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
314 {
315         /*
316          * Do not allow more than one LPC bridge to be configured.
317          */
318         if (lpc_bridge != NULL) {
319                 fprintf(stderr, "Only one LPC bridge is allowed.\n");
320                 return (-1);
321         }
322
323         /*
324          * Enforce that the LPC can only be configured on bus 0. This
325          * simplifies the ACPI DSDT because it can provide a decode for
326          * all legacy i/o ports behind bus 0.
327          */
328         if (pi->pi_bus != 0) {
329                 fprintf(stderr, "LPC bridge can be present only on bus 0.\n");
330                 return (-1);
331         }
332
333         if (lpc_init() != 0)
334                 return (-1);
335
336         /* initialize config space */
337         pci_set_cfgdata16(pi, PCIR_DEVICE, LPC_DEV);
338         pci_set_cfgdata16(pi, PCIR_VENDOR, LPC_VENDOR);
339         pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_BRIDGE);
340         pci_set_cfgdata8(pi, PCIR_SUBCLASS, PCIS_BRIDGE_ISA);
341
342         lpc_bridge = pi;
343
344         return (0);
345 }
346
347 struct pci_devemu pci_de_lpc = {
348         .pe_emu =       "lpc",
349         .pe_init =      pci_lpc_init,
350         .pe_write_dsdt = pci_lpc_write_dsdt,
351         .pe_barwrite =  pci_lpc_write,
352         .pe_barread =   pci_lpc_read
353 };
354 PCI_EMUL_SET(pci_de_lpc);