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