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