]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - usr.sbin/bhyve/pci_lpc.c
MFC 263780,264516,265062,265101,265203,265364:
[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         offset = port - sc->iobase;
134
135         switch (bytes) {
136         case 1:
137                 if (in)
138                         *eax = uart_read(sc->uart_softc, offset);
139                 else
140                         uart_write(sc->uart_softc, offset, *eax);
141                 break;
142         case 2:
143                 if (in) {
144                         *eax = uart_read(sc->uart_softc, offset);
145                         *eax |= uart_read(sc->uart_softc, offset + 1) << 8;
146                 } else {
147                         uart_write(sc->uart_softc, offset, *eax);
148                         uart_write(sc->uart_softc, offset + 1, *eax >> 8);
149                 }
150                 break;
151         default:
152                 return (-1);
153         }
154
155         return (0);
156 }
157
158 static int
159 lpc_init(void)
160 {
161         struct lpc_uart_softc *sc;
162         struct inout_port iop;
163         const char *name;
164         int unit, error;
165
166         /* COM1 and COM2 */
167         for (unit = 0; unit < LPC_UART_NUM; unit++) {
168                 sc = &lpc_uart_softc[unit];
169                 name = lpc_uart_names[unit];
170
171                 if (uart_legacy_alloc(unit, &sc->iobase, &sc->irq) != 0) {
172                         fprintf(stderr, "Unable to allocate resources for "
173                             "LPC device %s\n", name);
174                         return (-1);
175                 }
176
177                 sc->uart_softc = uart_init(lpc_uart_intr_assert,
178                                     lpc_uart_intr_deassert, sc);
179
180                 if (uart_set_backend(sc->uart_softc, sc->opts) != 0) {
181                         fprintf(stderr, "Unable to initialize backend '%s' "
182                             "for LPC device %s\n", sc->opts, name);
183                         return (-1);
184                 }
185
186                 bzero(&iop, sizeof(struct inout_port));
187                 iop.name = name;
188                 iop.port = sc->iobase;
189                 iop.size = UART_IO_BAR_SIZE;
190                 iop.flags = IOPORT_F_INOUT;
191                 iop.handler = lpc_uart_io_handler;
192                 iop.arg = sc;
193
194                 error = register_inout(&iop);
195                 assert(error == 0);
196                 sc->enabled = 1;
197         }
198
199         return (0);
200 }
201
202 static void
203 pci_lpc_write_dsdt(struct pci_devinst *pi)
204 {
205         struct lpc_dsdt **ldpp, *ldp;
206
207         dsdt_line("");
208         dsdt_line("Device (ISA)");
209         dsdt_line("{");
210         dsdt_line("  Name (_ADR, 0x%04X%04X)", pi->pi_slot, pi->pi_func);
211         dsdt_line("  OperationRegion (P40C, PCI_Config, 0x60, 0x04)");
212
213         dsdt_indent(1);
214         SET_FOREACH(ldpp, lpc_dsdt_set) {
215                 ldp = *ldpp;
216                 ldp->handler();
217         }
218
219         dsdt_line("");
220         dsdt_line("Device (PIC)");
221         dsdt_line("{");
222         dsdt_line("  Name (_HID, EisaId (\"PNP0000\"))");
223         dsdt_line("  Name (_CRS, ResourceTemplate ()");
224         dsdt_line("  {");
225         dsdt_indent(2);
226         dsdt_fixed_ioport(IO_ICU1, 2);
227         dsdt_fixed_ioport(IO_ICU2, 2);
228         dsdt_fixed_irq(2);
229         dsdt_unindent(2);
230         dsdt_line("  })");
231         dsdt_line("}");
232
233         dsdt_line("");
234         dsdt_line("Device (TIMR)");
235         dsdt_line("{");
236         dsdt_line("  Name (_HID, EisaId (\"PNP0100\"))");
237         dsdt_line("  Name (_CRS, ResourceTemplate ()");
238         dsdt_line("  {");
239         dsdt_indent(2);
240         dsdt_fixed_ioport(IO_TIMER1_PORT, 4);
241         dsdt_fixed_irq(0);
242         dsdt_unindent(2);
243         dsdt_line("  })");
244         dsdt_line("}");
245         dsdt_unindent(1);
246
247         dsdt_line("}");
248 }
249
250 static void
251 pci_lpc_sysres_dsdt(void)
252 {
253         struct lpc_sysres **lspp, *lsp;
254
255         dsdt_line("");
256         dsdt_line("Device (SIO)");
257         dsdt_line("{");
258         dsdt_line("  Name (_HID, EisaId (\"PNP0C02\"))");
259         dsdt_line("  Name (_CRS, ResourceTemplate ()");
260         dsdt_line("  {");
261
262         dsdt_indent(2);
263         SET_FOREACH(lspp, lpc_sysres_set) {
264                 lsp = *lspp;
265                 switch (lsp->type) {
266                 case LPC_SYSRES_IO:
267                         dsdt_fixed_ioport(lsp->base, lsp->length);
268                         break;
269                 case LPC_SYSRES_MEM:
270                         dsdt_fixed_mem32(lsp->base, lsp->length);
271                         break;
272                 }
273         }
274         dsdt_unindent(2);
275
276         dsdt_line("  })");
277         dsdt_line("}");
278 }
279 LPC_DSDT(pci_lpc_sysres_dsdt);
280
281 static void
282 pci_lpc_uart_dsdt(void)
283 {
284         struct lpc_uart_softc *sc;
285         int unit;
286
287         for (unit = 0; unit < LPC_UART_NUM; unit++) {
288                 sc = &lpc_uart_softc[unit];
289                 if (!sc->enabled)
290                         continue;
291                 dsdt_line("");
292                 dsdt_line("Device (%s)", lpc_uart_names[unit]);
293                 dsdt_line("{");
294                 dsdt_line("  Name (_HID, EisaId (\"PNP0501\"))");
295                 dsdt_line("  Name (_UID, %d)", unit + 1);
296                 dsdt_line("  Name (_CRS, ResourceTemplate ()");
297                 dsdt_line("  {");
298                 dsdt_indent(2);
299                 dsdt_fixed_ioport(sc->iobase, UART_IO_BAR_SIZE);
300                 dsdt_fixed_irq(sc->irq);
301                 dsdt_unindent(2);
302                 dsdt_line("  })");
303                 dsdt_line("}");
304         }
305 }
306 LPC_DSDT(pci_lpc_uart_dsdt);
307
308 static void
309 pci_lpc_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi,
310                int baridx, uint64_t offset, int size, uint64_t value)
311 {
312 }
313
314 uint64_t
315 pci_lpc_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi,
316               int baridx, uint64_t offset, int size)
317 {
318         return (0);
319 }
320
321 #define LPC_DEV         0x7000
322 #define LPC_VENDOR      0x8086
323
324 static int
325 pci_lpc_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
326 {
327         /*
328          * Do not allow more than one LPC bridge to be configured.
329          */
330         if (lpc_bridge != NULL) {
331                 fprintf(stderr, "Only one LPC bridge is allowed.\n");
332                 return (-1);
333         }
334
335         /*
336          * Enforce that the LPC can only be configured on bus 0. This
337          * simplifies the ACPI DSDT because it can provide a decode for
338          * all legacy i/o ports behind bus 0.
339          */
340         if (pi->pi_bus != 0) {
341                 fprintf(stderr, "LPC bridge can be present only on bus 0.\n");
342                 return (-1);
343         }
344
345         if (lpc_init() != 0)
346                 return (-1);
347
348         /* initialize config space */
349         pci_set_cfgdata16(pi, PCIR_DEVICE, LPC_DEV);
350         pci_set_cfgdata16(pi, PCIR_VENDOR, LPC_VENDOR);
351         pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_BRIDGE);
352         pci_set_cfgdata8(pi, PCIR_SUBCLASS, PCIS_BRIDGE_ISA);
353
354         lpc_bridge = pi;
355
356         return (0);
357 }
358
359 struct pci_devemu pci_de_lpc = {
360         .pe_emu =       "lpc",
361         .pe_init =      pci_lpc_init,
362         .pe_write_dsdt = pci_lpc_write_dsdt,
363         .pe_barwrite =  pci_lpc_write,
364         .pe_barread =   pci_lpc_read
365 };
366 PCI_EMUL_SET(pci_de_lpc);