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