2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2011 NetApp, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/linker_set.h>
46 #include <machine/vmm.h>
47 #include <machine/vmm_snapshot.h>
60 #include "pci_passthru.h"
61 #include "qemu_fwcfg.h"
63 #define CONF1_ADDR_PORT 0x0cf8
64 #define CONF1_DATA_PORT 0x0cfc
66 #define CONF1_ENABLE 0x80000000ul
68 #define MAXBUSES (PCI_BUSMAX + 1)
69 #define MAXSLOTS (PCI_SLOTMAX + 1)
70 #define MAXFUNCS (PCI_FUNCMAX + 1)
72 #define GB (1024 * 1024 * 1024UL)
76 struct pci_devemu *fi_pde;
77 struct pci_devinst *fi_devi;
87 struct intxinfo si_intpins[4];
88 struct funcinfo si_funcs[MAXFUNCS];
92 uint16_t iobase, iolimit; /* I/O window */
93 uint32_t membase32, memlimit32; /* mmio window below 4GB */
94 uint64_t membase64, memlimit64; /* mmio window above 4GB */
95 struct slotinfo slotinfo[MAXSLOTS];
98 static struct businfo *pci_businfo[MAXBUSES];
100 SET_DECLARE(pci_devemu_set, struct pci_devemu);
102 static uint64_t pci_emul_iobase;
103 static uint8_t *pci_emul_rombase;
104 static uint64_t pci_emul_romoffset;
105 static uint8_t *pci_emul_romlim;
106 static uint64_t pci_emul_membase32;
107 static uint64_t pci_emul_membase64;
108 static uint64_t pci_emul_memlim64;
110 struct pci_bar_allocation {
111 TAILQ_ENTRY(pci_bar_allocation) chain;
112 struct pci_devinst *pdi;
114 enum pcibar_type type;
118 static TAILQ_HEAD(pci_bar_list, pci_bar_allocation) pci_bars =
119 TAILQ_HEAD_INITIALIZER(pci_bars);
122 TAILQ_ENTRY(boot_device) boot_device_chain;
123 struct pci_devinst *pdi;
126 static TAILQ_HEAD(boot_list, boot_device) boot_devices = TAILQ_HEAD_INITIALIZER(
129 #define PCI_EMUL_IOBASE 0x2000
130 #define PCI_EMUL_IOLIMIT 0x10000
132 #define PCI_EMUL_ROMSIZE 0x10000000
134 #define PCI_EMUL_ECFG_BASE 0xE0000000 /* 3.5GB */
135 #define PCI_EMUL_ECFG_SIZE (MAXBUSES * 1024 * 1024) /* 1MB per bus */
136 SYSRES_MEM(PCI_EMUL_ECFG_BASE, PCI_EMUL_ECFG_SIZE);
139 * OVMF always uses 0xC0000000 as base address for 32 bit PCI MMIO. Don't
140 * change this address without changing it in OVMF.
142 #define PCI_EMUL_MEMBASE32 0xC0000000
143 #define PCI_EMUL_MEMLIMIT32 PCI_EMUL_ECFG_BASE
144 #define PCI_EMUL_MEMSIZE64 (32*GB)
146 static struct pci_devemu *pci_emul_finddev(const char *name);
147 static void pci_lintr_route(struct pci_devinst *pi);
148 static void pci_lintr_update(struct pci_devinst *pi);
149 static void pci_cfgrw(int in, int bus, int slot, int func, int coff,
150 int bytes, uint32_t *val);
153 CFGWRITE(struct pci_devinst *pi, int coff, uint32_t val, int bytes)
157 pci_set_cfgdata8(pi, coff, val);
159 pci_set_cfgdata16(pi, coff, val);
161 pci_set_cfgdata32(pi, coff, val);
164 static __inline uint32_t
165 CFGREAD(struct pci_devinst *pi, int coff, int bytes)
169 return (pci_get_cfgdata8(pi, coff));
171 return (pci_get_cfgdata16(pi, coff));
173 return (pci_get_cfgdata32(pi, coff));
177 is_pcir_bar(int coff)
179 return (coff >= PCIR_BAR(0) && coff < PCIR_BAR(PCI_BARMAX + 1));
183 is_pcir_bios(int coff)
185 return (coff >= PCIR_BIOS && coff < PCIR_BIOS + 4);
193 * Slot options are in the form:
195 * <bus>:<slot>:<func>,<emul>[,<config>]
196 * <slot>[:<func>],<emul>[,<config>]
200 * emul is a string describing the type of PCI device e.g. virtio-net
201 * config is an optional string, depending on the device, that can be
202 * used for configuration.
208 pci_parse_slot_usage(char *aopt)
211 EPRINTLN("Invalid PCI slot info field \"%s\"", aopt);
215 * Helper function to parse a list of comma-separated options where
216 * each option is formatted as "name[=value]". If no value is
217 * provided, the option is treated as a boolean and is given a value
221 pci_parse_legacy_config(nvlist_t *nvl, const char *opt)
223 char *config, *name, *tofree, *value;
228 config = tofree = strdup(opt);
229 while ((name = strsep(&config, ",")) != NULL) {
230 value = strchr(name, '=');
234 set_config_value_node(nvl, name, value);
236 set_config_bool_node(nvl, name, true);
243 * PCI device configuration is stored in MIBs that encode the device's
246 * pci.<bus>.<slot>.<func>
248 * Where "bus", "slot", and "func" are all decimal values without
249 * leading zeroes. Each valid device must have a "device" node which
250 * identifies the driver model of the device.
252 * Device backends can provide a parser for the "config" string. If
253 * a custom parser is not provided, pci_parse_legacy_config() is used
254 * to parse the string.
257 pci_parse_slot(char *opt)
259 char node_name[sizeof("pci.XXX.XX.X")];
260 struct pci_devemu *pde;
261 char *emul, *config, *str, *cp;
262 int error, bnum, snum, fnum;
268 emul = config = NULL;
269 if ((cp = strchr(str, ',')) != NULL) {
272 if ((cp = strchr(emul, ',')) != NULL) {
277 pci_parse_slot_usage(opt);
281 /* <bus>:<slot>:<func> */
282 if (sscanf(str, "%d:%d:%d", &bnum, &snum, &fnum) != 3) {
285 if (sscanf(str, "%d:%d", &snum, &fnum) != 2) {
288 if (sscanf(str, "%d", &snum) != 1) {
294 if (bnum < 0 || bnum >= MAXBUSES || snum < 0 || snum >= MAXSLOTS ||
295 fnum < 0 || fnum >= MAXFUNCS) {
296 pci_parse_slot_usage(opt);
300 pde = pci_emul_finddev(emul);
302 EPRINTLN("pci slot %d:%d:%d: unknown device \"%s\"", bnum, snum,
307 snprintf(node_name, sizeof(node_name), "pci.%d.%d.%d", bnum, snum,
309 nvl = find_config_node(node_name);
311 EPRINTLN("pci slot %d:%d:%d already occupied!", bnum, snum,
315 nvl = create_config_node(node_name);
316 if (pde->pe_alias != NULL)
317 set_config_value_node(nvl, "device", pde->pe_alias);
319 set_config_value_node(nvl, "device", pde->pe_emu);
321 if (pde->pe_legacy_config != NULL)
322 error = pde->pe_legacy_config(nvl, config);
324 error = pci_parse_legacy_config(nvl, config);
331 pci_print_supported_devices(void)
333 struct pci_devemu **pdpp, *pdp;
335 SET_FOREACH(pdpp, pci_devemu_set) {
337 printf("%s\n", pdp->pe_emu);
342 pci_config_read_reg(const struct pcisel *const host_sel, nvlist_t *nvl,
343 const uint32_t reg, const uint8_t size, const uint32_t def)
346 const nvlist_t *pci_regs;
348 assert(size == 1 || size == 2 || size == 4);
350 pci_regs = find_relative_config_node(nvl, "pcireg");
351 if (pci_regs == NULL) {
357 config = get_config_value_node(pci_regs, "device");
360 config = get_config_value_node(pci_regs, "vendor");
363 config = get_config_value_node(pci_regs, "revid");
366 config = get_config_value_node(pci_regs, "subvendor");
369 config = get_config_value_node(pci_regs, "subdevice");
375 if (config == NULL) {
377 } else if (host_sel != NULL && strcmp(config, "host") == 0) {
378 return read_config(host_sel, reg, size);
380 return strtol(config, NULL, 16);
385 pci_valid_pba_offset(struct pci_devinst *pi, uint64_t offset)
388 if (offset < pi->pi_msix.pba_offset)
391 if (offset >= pi->pi_msix.pba_offset + pi->pi_msix.pba_size) {
399 pci_emul_msix_twrite(struct pci_devinst *pi, uint64_t offset, int size,
402 int msix_entry_offset;
406 /* support only 4 or 8 byte writes */
407 if (size != 4 && size != 8)
411 * Return if table index is beyond what device supports
413 tab_index = offset / MSIX_TABLE_ENTRY_SIZE;
414 if (tab_index >= pi->pi_msix.table_count)
417 msix_entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
419 /* support only aligned writes */
420 if ((msix_entry_offset % size) != 0)
423 dest = (char *)(pi->pi_msix.table + tab_index);
424 dest += msix_entry_offset;
427 *((uint32_t *)dest) = value;
429 *((uint64_t *)dest) = value;
435 pci_emul_msix_tread(struct pci_devinst *pi, uint64_t offset, int size)
438 int msix_entry_offset;
440 uint64_t retval = ~0;
443 * The PCI standard only allows 4 and 8 byte accesses to the MSI-X
444 * table but we also allow 1 byte access to accommodate reads from
447 if (size != 1 && size != 4 && size != 8)
450 msix_entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
452 /* support only aligned reads */
453 if ((msix_entry_offset % size) != 0) {
457 tab_index = offset / MSIX_TABLE_ENTRY_SIZE;
459 if (tab_index < pi->pi_msix.table_count) {
460 /* valid MSI-X Table access */
461 dest = (char *)(pi->pi_msix.table + tab_index);
462 dest += msix_entry_offset;
465 retval = *((uint8_t *)dest);
467 retval = *((uint32_t *)dest);
469 retval = *((uint64_t *)dest);
470 } else if (pci_valid_pba_offset(pi, offset)) {
471 /* return 0 for PBA access */
479 pci_msix_table_bar(struct pci_devinst *pi)
482 if (pi->pi_msix.table != NULL)
483 return (pi->pi_msix.table_bar);
489 pci_msix_pba_bar(struct pci_devinst *pi)
492 if (pi->pi_msix.table != NULL)
493 return (pi->pi_msix.pba_bar);
499 pci_emul_io_handler(struct vmctx *ctx __unused, int in, int port,
500 int bytes, uint32_t *eax, void *arg)
502 struct pci_devinst *pdi = arg;
503 struct pci_devemu *pe = pdi->pi_d;
509 for (i = 0; i <= PCI_BARMAX; i++) {
510 if (pdi->pi_bar[i].type == PCIBAR_IO &&
511 (uint64_t)port >= pdi->pi_bar[i].addr &&
512 (uint64_t)port + bytes <=
513 pdi->pi_bar[i].addr + pdi->pi_bar[i].size) {
514 offset = port - pdi->pi_bar[i].addr;
516 *eax = (*pe->pe_barread)(pdi, i,
519 (*pe->pe_barwrite)(pdi, i, offset,
528 pci_emul_mem_handler(struct vcpu *vcpu __unused, int dir,
529 uint64_t addr, int size, uint64_t *val, void *arg1, long arg2)
531 struct pci_devinst *pdi = arg1;
532 struct pci_devemu *pe = pdi->pi_d;
534 int bidx = (int) arg2;
536 assert(bidx <= PCI_BARMAX);
537 assert(pdi->pi_bar[bidx].type == PCIBAR_MEM32 ||
538 pdi->pi_bar[bidx].type == PCIBAR_MEM64);
539 assert(addr >= pdi->pi_bar[bidx].addr &&
540 addr + size <= pdi->pi_bar[bidx].addr + pdi->pi_bar[bidx].size);
542 offset = addr - pdi->pi_bar[bidx].addr;
544 if (dir == MEM_F_WRITE) {
546 (*pe->pe_barwrite)(pdi, bidx, offset,
547 4, *val & 0xffffffff);
548 (*pe->pe_barwrite)(pdi, bidx, offset + 4,
551 (*pe->pe_barwrite)(pdi, bidx, offset,
556 *val = (*pe->pe_barread)(pdi, bidx,
558 *val |= (*pe->pe_barread)(pdi, bidx,
559 offset + 4, 4) << 32;
561 *val = (*pe->pe_barread)(pdi, bidx,
571 pci_emul_alloc_resource(uint64_t *baseptr, uint64_t limit, uint64_t size,
576 assert((size & (size - 1)) == 0); /* must be a power of 2 */
578 base = roundup2(*baseptr, size);
580 if (base + size <= limit) {
582 *baseptr = base + size;
589 * Register (or unregister) the MMIO or I/O region associated with the BAR
590 * register 'idx' of an emulated pci device.
593 modify_bar_registration(struct pci_devinst *pi, int idx, int registration)
595 struct pci_devemu *pe;
597 struct inout_port iop;
601 switch (pi->pi_bar[idx].type) {
603 bzero(&iop, sizeof(struct inout_port));
604 iop.name = pi->pi_name;
605 iop.port = pi->pi_bar[idx].addr;
606 iop.size = pi->pi_bar[idx].size;
608 iop.flags = IOPORT_F_INOUT;
609 iop.handler = pci_emul_io_handler;
611 error = register_inout(&iop);
613 error = unregister_inout(&iop);
617 bzero(&mr, sizeof(struct mem_range));
618 mr.name = pi->pi_name;
619 mr.base = pi->pi_bar[idx].addr;
620 mr.size = pi->pi_bar[idx].size;
623 mr.handler = pci_emul_mem_handler;
626 error = register_mem(&mr);
628 error = unregister_mem(&mr);
639 if (pe->pe_baraddr != NULL)
640 (*pe->pe_baraddr)(pi, idx, registration, pi->pi_bar[idx].addr);
644 unregister_bar(struct pci_devinst *pi, int idx)
647 modify_bar_registration(pi, idx, 0);
651 register_bar(struct pci_devinst *pi, int idx)
654 modify_bar_registration(pi, idx, 1);
657 /* Is the ROM enabled for the emulated pci device? */
659 romen(struct pci_devinst *pi)
661 return (pi->pi_bar[PCI_ROM_IDX].lobits & PCIM_BIOS_ENABLE) ==
665 /* Are we decoding i/o port accesses for the emulated pci device? */
667 porten(struct pci_devinst *pi)
671 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND);
673 return (cmd & PCIM_CMD_PORTEN);
676 /* Are we decoding memory accesses for the emulated pci device? */
678 memen(struct pci_devinst *pi)
682 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND);
684 return (cmd & PCIM_CMD_MEMEN);
688 * Update the MMIO or I/O address that is decoded by the BAR register.
690 * If the pci device has enabled the address space decoding then intercept
691 * the address range decoded by the BAR register.
694 update_bar_address(struct pci_devinst *pi, uint64_t addr, int idx, int type)
698 if (pi->pi_bar[idx].type == PCIBAR_IO)
704 unregister_bar(pi, idx);
709 pi->pi_bar[idx].addr = addr;
712 pi->pi_bar[idx].addr &= ~0xffffffffUL;
713 pi->pi_bar[idx].addr |= addr;
716 pi->pi_bar[idx].addr &= 0xffffffff;
717 pi->pi_bar[idx].addr |= addr;
724 register_bar(pi, idx);
728 pci_emul_alloc_bar(struct pci_devinst *pdi, int idx, enum pcibar_type type,
731 assert((type == PCIBAR_ROM) || (idx >= 0 && idx <= PCI_BARMAX));
732 assert((type != PCIBAR_ROM) || (idx == PCI_ROM_IDX));
734 if ((size & (size - 1)) != 0)
735 size = 1UL << flsl(size); /* round up to a power of 2 */
737 /* Enforce minimum BAR sizes required by the PCI standard */
738 if (type == PCIBAR_IO) {
741 } else if (type == PCIBAR_ROM) {
742 if (size < ~PCIM_BIOS_ADDR_MASK + 1)
743 size = ~PCIM_BIOS_ADDR_MASK + 1;
750 * To reduce fragmentation of the MMIO space, we allocate the BARs by
751 * size. Therefore, don't allocate the BAR yet. We create a list of all
752 * BAR allocation which is sorted by BAR size. When all PCI devices are
753 * initialized, we will assign an address to the BARs.
756 /* create a new list entry */
757 struct pci_bar_allocation *const new_bar = malloc(sizeof(*new_bar));
758 memset(new_bar, 0, sizeof(*new_bar));
761 new_bar->type = type;
762 new_bar->size = size;
765 * Search for a BAR which size is lower than the size of our newly
768 struct pci_bar_allocation *bar = NULL;
769 TAILQ_FOREACH(bar, &pci_bars, chain) {
770 if (bar->size < size) {
777 * Either the list is empty or new BAR is the smallest BAR of
778 * the list. Append it to the end of our list.
780 TAILQ_INSERT_TAIL(&pci_bars, new_bar, chain);
783 * The found BAR is smaller than our new BAR. For that reason,
784 * insert our new BAR before the found BAR.
786 TAILQ_INSERT_BEFORE(bar, new_bar, chain);
790 * pci_passthru devices synchronize their physical and virtual command
791 * register on init. For that reason, the virtual cmd reg should be
792 * updated as early as possible.
797 enbit = PCIM_CMD_PORTEN;
801 enbit = PCIM_CMD_MEMEN;
808 const uint16_t cmd = pci_get_cfgdata16(pdi, PCIR_COMMAND);
809 pci_set_cfgdata16(pdi, PCIR_COMMAND, cmd | enbit);
815 pci_emul_assign_bar(struct pci_devinst *const pdi, const int idx,
816 const enum pcibar_type type, const uint64_t size)
819 uint64_t *baseptr, limit, addr, mask, lobits, bar;
824 addr = mask = lobits = 0;
827 baseptr = &pci_emul_iobase;
828 limit = PCI_EMUL_IOLIMIT;
829 mask = PCIM_BAR_IO_BASE;
830 lobits = PCIM_BAR_IO_SPACE;
835 * Some drivers do not work well if the 64-bit BAR is allocated
836 * above 4GB. Allow for this by allocating small requests under
837 * 4GB unless then allocation size is larger than some arbitrary
838 * number (128MB currently).
840 if (size > 128 * 1024 * 1024) {
841 baseptr = &pci_emul_membase64;
842 limit = pci_emul_memlim64;
843 mask = PCIM_BAR_MEM_BASE;
844 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64 |
845 PCIM_BAR_MEM_PREFETCH;
847 baseptr = &pci_emul_membase32;
848 limit = PCI_EMUL_MEMLIMIT32;
849 mask = PCIM_BAR_MEM_BASE;
850 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64;
854 baseptr = &pci_emul_membase32;
855 limit = PCI_EMUL_MEMLIMIT32;
856 mask = PCIM_BAR_MEM_BASE;
857 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_32;
860 /* do not claim memory for ROM. OVMF will do it for us. */
863 mask = PCIM_BIOS_ADDR_MASK;
867 printf("pci_emul_alloc_base: invalid bar type %d\n", type);
871 if (baseptr != NULL) {
872 error = pci_emul_alloc_resource(baseptr, limit, size, &addr);
879 pdi->pi_bar[idx].type = type;
880 pdi->pi_bar[idx].addr = addr;
881 pdi->pi_bar[idx].size = size;
883 * passthru devices are using same lobits as physical device they set
886 if (pdi->pi_bar[idx].lobits != 0) {
887 lobits = pdi->pi_bar[idx].lobits;
889 pdi->pi_bar[idx].lobits = lobits;
892 /* Initialize the BAR register in config space */
893 bar = (addr & mask) | lobits;
894 pci_set_cfgdata32(pdi, PCIR_BAR(idx), bar);
896 if (type == PCIBAR_MEM64) {
897 assert(idx + 1 <= PCI_BARMAX);
898 pdi->pi_bar[idx + 1].type = PCIBAR_MEMHI64;
899 pci_set_cfgdata32(pdi, PCIR_BAR(idx + 1), bar >> 32);
902 if (type != PCIBAR_ROM) {
903 register_bar(pdi, idx);
910 pci_emul_alloc_rom(struct pci_devinst *const pdi, const uint64_t size,
913 /* allocate ROM space once on first call */
914 if (pci_emul_rombase == 0) {
915 pci_emul_rombase = vm_create_devmem(pdi->pi_vmctx, VM_PCIROM,
916 "pcirom", PCI_EMUL_ROMSIZE);
917 if (pci_emul_rombase == MAP_FAILED) {
918 warnx("%s: failed to create rom segment", __func__);
921 pci_emul_romlim = pci_emul_rombase + PCI_EMUL_ROMSIZE;
922 pci_emul_romoffset = 0;
925 /* ROM size should be a power of 2 and greater than 2 KB */
926 const uint64_t rom_size = MAX(1UL << flsl(size),
927 ~PCIM_BIOS_ADDR_MASK + 1);
929 /* check if ROM fits into ROM space */
930 if (pci_emul_romoffset + rom_size > PCI_EMUL_ROMSIZE) {
931 warnx("%s: no space left in rom segment:", __func__);
932 warnx("%16lu bytes left",
933 PCI_EMUL_ROMSIZE - pci_emul_romoffset);
934 warnx("%16lu bytes required by %d/%d/%d", rom_size, pdi->pi_bus,
935 pdi->pi_slot, pdi->pi_func);
939 /* allocate ROM BAR */
940 const int error = pci_emul_alloc_bar(pdi, PCI_ROM_IDX, PCIBAR_ROM,
946 *addr = pci_emul_rombase + pci_emul_romoffset;
948 /* save offset into ROM Space */
949 pdi->pi_romoffset = pci_emul_romoffset;
951 /* increase offset for next ROM */
952 pci_emul_romoffset += rom_size;
958 pci_emul_add_boot_device(struct pci_devinst *pi, int bootindex)
960 struct boot_device *new_device, *device;
962 /* don't permit a negative bootindex */
964 errx(4, "Invalid bootindex %d for %s", bootindex, pi->pi_name);
967 /* alloc new boot device */
968 new_device = calloc(1, sizeof(struct boot_device));
969 if (new_device == NULL) {
972 new_device->pdi = pi;
973 new_device->bootindex = bootindex;
975 /* search for boot device with higher boot index */
976 TAILQ_FOREACH(device, &boot_devices, boot_device_chain) {
977 if (device->bootindex == bootindex) {
979 "Could not set bootindex %d for %s. Bootindex already occupied by %s",
980 bootindex, pi->pi_name, device->pdi->pi_name);
981 } else if (device->bootindex > bootindex) {
986 /* add boot device to queue */
987 if (device == NULL) {
988 TAILQ_INSERT_TAIL(&boot_devices, new_device, boot_device_chain);
990 TAILQ_INSERT_BEFORE(device, new_device, boot_device_chain);
996 #define CAP_START_OFFSET 0x40
998 pci_emul_add_capability(struct pci_devinst *pi, u_char *capdata, int caplen)
1000 int i, capoff, reallen;
1005 reallen = roundup2(caplen, 4); /* dword aligned */
1007 sts = pci_get_cfgdata16(pi, PCIR_STATUS);
1008 if ((sts & PCIM_STATUS_CAPPRESENT) == 0)
1009 capoff = CAP_START_OFFSET;
1011 capoff = pi->pi_capend + 1;
1013 /* Check if we have enough space */
1014 if (capoff + reallen > PCI_REGMAX + 1)
1017 /* Set the previous capability pointer */
1018 if ((sts & PCIM_STATUS_CAPPRESENT) == 0) {
1019 pci_set_cfgdata8(pi, PCIR_CAP_PTR, capoff);
1020 pci_set_cfgdata16(pi, PCIR_STATUS, sts|PCIM_STATUS_CAPPRESENT);
1022 pci_set_cfgdata8(pi, pi->pi_prevcap + 1, capoff);
1024 /* Copy the capability */
1025 for (i = 0; i < caplen; i++)
1026 pci_set_cfgdata8(pi, capoff + i, capdata[i]);
1028 /* Set the next capability pointer */
1029 pci_set_cfgdata8(pi, capoff + 1, 0);
1031 pi->pi_prevcap = capoff;
1032 pi->pi_capend = capoff + reallen - 1;
1036 static struct pci_devemu *
1037 pci_emul_finddev(const char *name)
1039 struct pci_devemu **pdpp, *pdp;
1041 SET_FOREACH(pdpp, pci_devemu_set) {
1043 if (!strcmp(pdp->pe_emu, name)) {
1052 pci_emul_init(struct vmctx *ctx, struct pci_devemu *pde, int bus, int slot,
1053 int func, struct funcinfo *fi)
1055 struct pci_devinst *pdi;
1058 pdi = calloc(1, sizeof(struct pci_devinst));
1060 pdi->pi_vmctx = ctx;
1062 pdi->pi_slot = slot;
1063 pdi->pi_func = func;
1064 pthread_mutex_init(&pdi->pi_lintr.lock, NULL);
1065 pdi->pi_lintr.pin = 0;
1066 pdi->pi_lintr.state = IDLE;
1067 pdi->pi_lintr.pirq_pin = 0;
1068 pdi->pi_lintr.ioapic_irq = 0;
1070 snprintf(pdi->pi_name, PI_NAMESZ, "%s@pci.%d.%d.%d", pde->pe_emu, bus,
1073 /* Disable legacy interrupts */
1074 pci_set_cfgdata8(pdi, PCIR_INTLINE, 255);
1075 pci_set_cfgdata8(pdi, PCIR_INTPIN, 0);
1077 pci_set_cfgdata8(pdi, PCIR_COMMAND, PCIM_CMD_BUSMASTEREN);
1079 err = (*pde->pe_init)(pdi, fi->fi_config);
1089 pci_populate_msicap(struct msicap *msicap, int msgnum, int nextptr)
1093 /* Number of msi messages must be a power of 2 between 1 and 32 */
1094 assert((msgnum & (msgnum - 1)) == 0 && msgnum >= 1 && msgnum <= 32);
1095 mmc = ffs(msgnum) - 1;
1097 bzero(msicap, sizeof(struct msicap));
1098 msicap->capid = PCIY_MSI;
1099 msicap->nextptr = nextptr;
1100 msicap->msgctrl = PCIM_MSICTRL_64BIT | (mmc << 1);
1104 pci_emul_add_msicap(struct pci_devinst *pi, int msgnum)
1106 struct msicap msicap;
1108 pci_populate_msicap(&msicap, msgnum, 0);
1110 return (pci_emul_add_capability(pi, (u_char *)&msicap, sizeof(msicap)));
1114 pci_populate_msixcap(struct msixcap *msixcap, int msgnum, int barnum,
1115 uint32_t msix_tab_size)
1118 assert(msix_tab_size % 4096 == 0);
1120 bzero(msixcap, sizeof(struct msixcap));
1121 msixcap->capid = PCIY_MSIX;
1124 * Message Control Register, all fields set to
1125 * zero except for the Table Size.
1126 * Note: Table size N is encoded as N-1
1128 msixcap->msgctrl = msgnum - 1;
1132 * - MSI-X table start at offset 0
1133 * - PBA table starts at a 4K aligned offset after the MSI-X table
1135 msixcap->table_info = barnum & PCIM_MSIX_BIR_MASK;
1136 msixcap->pba_info = msix_tab_size | (barnum & PCIM_MSIX_BIR_MASK);
1140 pci_msix_table_init(struct pci_devinst *pi, int table_entries)
1144 assert(table_entries > 0);
1145 assert(table_entries <= MAX_MSIX_TABLE_ENTRIES);
1147 table_size = table_entries * MSIX_TABLE_ENTRY_SIZE;
1148 pi->pi_msix.table = calloc(1, table_size);
1150 /* set mask bit of vector control register */
1151 for (i = 0; i < table_entries; i++)
1152 pi->pi_msix.table[i].vector_control |= PCIM_MSIX_VCTRL_MASK;
1156 pci_emul_add_msixcap(struct pci_devinst *pi, int msgnum, int barnum)
1159 struct msixcap msixcap;
1161 assert(msgnum >= 1 && msgnum <= MAX_MSIX_TABLE_ENTRIES);
1162 assert(barnum >= 0 && barnum <= PCIR_MAX_BAR_0);
1164 tab_size = msgnum * MSIX_TABLE_ENTRY_SIZE;
1166 /* Align table size to nearest 4K */
1167 tab_size = roundup2(tab_size, 4096);
1169 pi->pi_msix.table_bar = barnum;
1170 pi->pi_msix.pba_bar = barnum;
1171 pi->pi_msix.table_offset = 0;
1172 pi->pi_msix.table_count = msgnum;
1173 pi->pi_msix.pba_offset = tab_size;
1174 pi->pi_msix.pba_size = PBA_SIZE(msgnum);
1176 pci_msix_table_init(pi, msgnum);
1178 pci_populate_msixcap(&msixcap, msgnum, barnum, tab_size);
1180 /* allocate memory for MSI-X Table and PBA */
1181 pci_emul_alloc_bar(pi, barnum, PCIBAR_MEM32,
1182 tab_size + pi->pi_msix.pba_size);
1184 return (pci_emul_add_capability(pi, (u_char *)&msixcap,
1189 msixcap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
1190 int bytes, uint32_t val)
1192 uint16_t msgctrl, rwmask;
1195 off = offset - capoff;
1196 /* Message Control Register */
1197 if (off == 2 && bytes == 2) {
1198 rwmask = PCIM_MSIXCTRL_MSIX_ENABLE | PCIM_MSIXCTRL_FUNCTION_MASK;
1199 msgctrl = pci_get_cfgdata16(pi, offset);
1201 msgctrl |= val & rwmask;
1204 pi->pi_msix.enabled = val & PCIM_MSIXCTRL_MSIX_ENABLE;
1205 pi->pi_msix.function_mask = val & PCIM_MSIXCTRL_FUNCTION_MASK;
1206 pci_lintr_update(pi);
1209 CFGWRITE(pi, offset, val, bytes);
1213 msicap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
1214 int bytes, uint32_t val)
1216 uint16_t msgctrl, rwmask, msgdata, mme;
1220 * If guest is writing to the message control register make sure
1221 * we do not overwrite read-only fields.
1223 if ((offset - capoff) == 2 && bytes == 2) {
1224 rwmask = PCIM_MSICTRL_MME_MASK | PCIM_MSICTRL_MSI_ENABLE;
1225 msgctrl = pci_get_cfgdata16(pi, offset);
1227 msgctrl |= val & rwmask;
1230 CFGWRITE(pi, offset, val, bytes);
1232 msgctrl = pci_get_cfgdata16(pi, capoff + 2);
1233 addrlo = pci_get_cfgdata32(pi, capoff + 4);
1234 if (msgctrl & PCIM_MSICTRL_64BIT)
1235 msgdata = pci_get_cfgdata16(pi, capoff + 12);
1237 msgdata = pci_get_cfgdata16(pi, capoff + 8);
1239 mme = msgctrl & PCIM_MSICTRL_MME_MASK;
1240 pi->pi_msi.enabled = msgctrl & PCIM_MSICTRL_MSI_ENABLE ? 1 : 0;
1241 if (pi->pi_msi.enabled) {
1242 pi->pi_msi.addr = addrlo;
1243 pi->pi_msi.msg_data = msgdata;
1244 pi->pi_msi.maxmsgnum = 1 << (mme >> 4);
1246 pi->pi_msi.maxmsgnum = 0;
1248 pci_lintr_update(pi);
1252 pciecap_cfgwrite(struct pci_devinst *pi, int capoff __unused, int offset,
1253 int bytes, uint32_t val)
1256 /* XXX don't write to the readonly parts */
1257 CFGWRITE(pi, offset, val, bytes);
1260 #define PCIECAP_VERSION 0x2
1262 pci_emul_add_pciecap(struct pci_devinst *pi, int type)
1265 struct pciecap pciecap;
1267 bzero(&pciecap, sizeof(pciecap));
1270 * Use the integrated endpoint type for endpoints on a root complex bus.
1272 * NB: bhyve currently only supports a single PCI bus that is the root
1273 * complex bus, so all endpoints are integrated.
1275 if ((type == PCIEM_TYPE_ENDPOINT) && (pi->pi_bus == 0))
1276 type = PCIEM_TYPE_ROOT_INT_EP;
1278 pciecap.capid = PCIY_EXPRESS;
1279 pciecap.pcie_capabilities = PCIECAP_VERSION | type;
1280 if (type != PCIEM_TYPE_ROOT_INT_EP) {
1281 pciecap.link_capabilities = 0x411; /* gen1, x1 */
1282 pciecap.link_status = 0x11; /* gen1, x1 */
1285 err = pci_emul_add_capability(pi, (u_char *)&pciecap, sizeof(pciecap));
1290 * This function assumes that 'coff' is in the capabilities region of the
1291 * config space. A capoff parameter of zero will force a search for the
1295 pci_emul_capwrite(struct pci_devinst *pi, int offset, int bytes, uint32_t val,
1296 uint8_t capoff, int capid)
1300 /* Do not allow un-aligned writes */
1301 if ((offset & (bytes - 1)) != 0)
1305 /* Find the capability that we want to update */
1306 capoff = CAP_START_OFFSET;
1308 nextoff = pci_get_cfgdata8(pi, capoff + 1);
1311 if (offset >= capoff && offset < nextoff)
1316 assert(offset >= capoff);
1317 capid = pci_get_cfgdata8(pi, capoff);
1321 * Capability ID and Next Capability Pointer are readonly.
1322 * However, some o/s's do 4-byte writes that include these.
1323 * For this case, trim the write back to 2 bytes and adjust
1326 if (offset == capoff || offset == capoff + 1) {
1327 if (offset == capoff && bytes == 4) {
1337 msicap_cfgwrite(pi, capoff, offset, bytes, val);
1340 msixcap_cfgwrite(pi, capoff, offset, bytes, val);
1343 pciecap_cfgwrite(pi, capoff, offset, bytes, val);
1351 pci_emul_iscap(struct pci_devinst *pi, int offset)
1355 sts = pci_get_cfgdata16(pi, PCIR_STATUS);
1356 if ((sts & PCIM_STATUS_CAPPRESENT) != 0) {
1357 if (offset >= CAP_START_OFFSET && offset <= pi->pi_capend)
1364 pci_emul_fallback_handler(struct vcpu *vcpu __unused, int dir,
1365 uint64_t addr __unused, int size __unused, uint64_t *val,
1366 void *arg1 __unused, long arg2 __unused)
1369 * Ignore writes; return 0xff's for reads. The mem read code
1370 * will take care of truncating to the correct size.
1372 if (dir == MEM_F_READ) {
1373 *val = 0xffffffffffffffff;
1380 pci_emul_ecfg_handler(struct vcpu *vcpu __unused, int dir, uint64_t addr,
1381 int bytes, uint64_t *val, void *arg1 __unused, long arg2 __unused)
1383 int bus, slot, func, coff, in;
1385 coff = addr & 0xfff;
1386 func = (addr >> 12) & 0x7;
1387 slot = (addr >> 15) & 0x1f;
1388 bus = (addr >> 20) & 0xff;
1389 in = (dir == MEM_F_READ);
1392 pci_cfgrw(in, bus, slot, func, coff, bytes, (uint32_t *)val);
1400 return (PCI_EMUL_ECFG_BASE);
1404 init_bootorder(void)
1406 struct boot_device *device;
1409 size_t bootorder_len;
1411 if (TAILQ_EMPTY(&boot_devices))
1414 fp = open_memstream(&bootorder, &bootorder_len);
1415 TAILQ_FOREACH(device, &boot_devices, boot_device_chain) {
1416 fprintf(fp, "/pci@i0cf8/pci@%d,%d\n",
1417 device->pdi->pi_slot, device->pdi->pi_func);
1421 return (qemu_fwcfg_add_file("bootorder", bootorder_len, bootorder));
1424 #define BUSIO_ROUNDUP 32
1425 #define BUSMEM32_ROUNDUP (1024 * 1024)
1426 #define BUSMEM64_ROUNDUP (512 * 1024 * 1024)
1429 init_pci(struct vmctx *ctx)
1431 char node_name[sizeof("pci.XXX.XX.X")];
1432 struct mem_range mr;
1433 struct pci_devemu *pde;
1435 struct slotinfo *si;
1436 struct funcinfo *fi;
1440 int bus, slot, func;
1443 if (vm_get_lowmem_limit(ctx) > PCI_EMUL_MEMBASE32)
1444 errx(EX_OSERR, "Invalid lowmem limit");
1446 pci_emul_iobase = PCI_EMUL_IOBASE;
1447 pci_emul_membase32 = PCI_EMUL_MEMBASE32;
1449 pci_emul_membase64 = 4*GB + vm_get_highmem_size(ctx);
1450 pci_emul_membase64 = roundup2(pci_emul_membase64, PCI_EMUL_MEMSIZE64);
1451 pci_emul_memlim64 = pci_emul_membase64 + PCI_EMUL_MEMSIZE64;
1453 TAILQ_INIT(&boot_devices);
1455 for (bus = 0; bus < MAXBUSES; bus++) {
1456 snprintf(node_name, sizeof(node_name), "pci.%d", bus);
1457 nvl = find_config_node(node_name);
1460 pci_businfo[bus] = calloc(1, sizeof(struct businfo));
1461 bi = pci_businfo[bus];
1464 * Keep track of the i/o and memory resources allocated to
1467 bi->iobase = pci_emul_iobase;
1468 bi->membase32 = pci_emul_membase32;
1469 bi->membase64 = pci_emul_membase64;
1471 /* first run: init devices */
1472 for (slot = 0; slot < MAXSLOTS; slot++) {
1473 si = &bi->slotinfo[slot];
1474 for (func = 0; func < MAXFUNCS; func++) {
1475 fi = &si->si_funcs[func];
1476 snprintf(node_name, sizeof(node_name),
1477 "pci.%d.%d.%d", bus, slot, func);
1478 nvl = find_config_node(node_name);
1482 fi->fi_config = nvl;
1483 emul = get_config_value_node(nvl, "device");
1485 EPRINTLN("pci slot %d:%d:%d: missing "
1486 "\"device\" value", bus, slot, func);
1489 pde = pci_emul_finddev(emul);
1491 EPRINTLN("pci slot %d:%d:%d: unknown "
1492 "device \"%s\"", bus, slot, func,
1496 if (pde->pe_alias != NULL) {
1497 EPRINTLN("pci slot %d:%d:%d: legacy "
1498 "device \"%s\", use \"%s\" instead",
1499 bus, slot, func, emul,
1504 error = pci_emul_init(ctx, pde, bus, slot,
1511 /* second run: assign BARs and free list */
1512 struct pci_bar_allocation *bar;
1513 struct pci_bar_allocation *bar_tmp;
1514 TAILQ_FOREACH_SAFE(bar, &pci_bars, chain, bar_tmp) {
1515 pci_emul_assign_bar(bar->pdi, bar->idx, bar->type,
1519 TAILQ_INIT(&pci_bars);
1522 * Add some slop to the I/O and memory resources decoded by
1523 * this bus to give a guest some flexibility if it wants to
1524 * reprogram the BARs.
1526 pci_emul_iobase += BUSIO_ROUNDUP;
1527 pci_emul_iobase = roundup2(pci_emul_iobase, BUSIO_ROUNDUP);
1528 bi->iolimit = pci_emul_iobase;
1530 pci_emul_membase32 += BUSMEM32_ROUNDUP;
1531 pci_emul_membase32 = roundup2(pci_emul_membase32,
1533 bi->memlimit32 = pci_emul_membase32;
1535 pci_emul_membase64 += BUSMEM64_ROUNDUP;
1536 pci_emul_membase64 = roundup2(pci_emul_membase64,
1538 bi->memlimit64 = pci_emul_membase64;
1542 * PCI backends are initialized before routing INTx interrupts
1543 * so that LPC devices are able to reserve ISA IRQs before
1544 * routing PIRQ pins.
1546 for (bus = 0; bus < MAXBUSES; bus++) {
1547 if ((bi = pci_businfo[bus]) == NULL)
1550 for (slot = 0; slot < MAXSLOTS; slot++) {
1551 si = &bi->slotinfo[slot];
1552 for (func = 0; func < MAXFUNCS; func++) {
1553 fi = &si->si_funcs[func];
1554 if (fi->fi_devi == NULL)
1556 pci_lintr_route(fi->fi_devi);
1562 if ((error = init_bootorder()) != 0) {
1563 warnx("%s: Unable to init bootorder", __func__);
1568 * The guest physical memory map looks like the following:
1569 * [0, lowmem) guest system memory
1570 * [lowmem, 0xC0000000) memory hole (may be absent)
1571 * [0xC0000000, 0xE0000000) PCI hole (32-bit BAR allocation)
1572 * [0xE0000000, 0xF0000000) PCI extended config window
1573 * [0xF0000000, 4GB) LAPIC, IOAPIC, HPET, firmware
1574 * [4GB, 4GB + highmem)
1578 * Accesses to memory addresses that are not allocated to system
1579 * memory or PCI devices return 0xff's.
1581 lowmem = vm_get_lowmem_size(ctx);
1582 bzero(&mr, sizeof(struct mem_range));
1583 mr.name = "PCI hole";
1584 mr.flags = MEM_F_RW | MEM_F_IMMUTABLE;
1586 mr.size = (4ULL * 1024 * 1024 * 1024) - lowmem;
1587 mr.handler = pci_emul_fallback_handler;
1588 error = register_mem_fallback(&mr);
1591 /* PCI extended config space */
1592 bzero(&mr, sizeof(struct mem_range));
1593 mr.name = "PCI ECFG";
1594 mr.flags = MEM_F_RW | MEM_F_IMMUTABLE;
1595 mr.base = PCI_EMUL_ECFG_BASE;
1596 mr.size = PCI_EMUL_ECFG_SIZE;
1597 mr.handler = pci_emul_ecfg_handler;
1598 error = register_mem(&mr);
1605 pci_apic_prt_entry(int bus __unused, int slot, int pin, int pirq_pin __unused,
1606 int ioapic_irq, void *arg __unused)
1609 dsdt_line(" Package ()");
1611 dsdt_line(" 0x%X,", slot << 16 | 0xffff);
1612 dsdt_line(" 0x%02X,", pin - 1);
1613 dsdt_line(" Zero,");
1614 dsdt_line(" 0x%X", ioapic_irq);
1619 pci_pirq_prt_entry(int bus __unused, int slot, int pin, int pirq_pin,
1620 int ioapic_irq __unused, void *arg __unused)
1624 name = lpc_pirq_name(pirq_pin);
1627 dsdt_line(" Package ()");
1629 dsdt_line(" 0x%X,", slot << 16 | 0xffff);
1630 dsdt_line(" 0x%02X,", pin - 1);
1631 dsdt_line(" %s,", name);
1638 * A bhyve virtual machine has a flat PCI hierarchy with a root port
1639 * corresponding to each PCI bus.
1642 pci_bus_write_dsdt(int bus)
1645 struct slotinfo *si;
1646 struct pci_devinst *pi;
1647 int count, func, slot;
1650 * If there are no devices on this 'bus' then just return.
1652 if ((bi = pci_businfo[bus]) == NULL) {
1654 * Bus 0 is special because it decodes the I/O ports used
1655 * for PCI config space access even if there are no devices
1662 dsdt_line(" Device (PC%02X)", bus);
1664 dsdt_line(" Name (_HID, EisaId (\"PNP0A03\"))");
1666 dsdt_line(" Method (_BBN, 0, NotSerialized)");
1668 dsdt_line(" Return (0x%08X)", bus);
1670 dsdt_line(" Name (_CRS, ResourceTemplate ()");
1672 dsdt_line(" WordBusNumber (ResourceProducer, MinFixed, "
1673 "MaxFixed, PosDecode,");
1674 dsdt_line(" 0x0000, // Granularity");
1675 dsdt_line(" 0x%04X, // Range Minimum", bus);
1676 dsdt_line(" 0x%04X, // Range Maximum", bus);
1677 dsdt_line(" 0x0000, // Translation Offset");
1678 dsdt_line(" 0x0001, // Length");
1683 dsdt_fixed_ioport(0xCF8, 8);
1686 dsdt_line(" WordIO (ResourceProducer, MinFixed, MaxFixed, "
1687 "PosDecode, EntireRange,");
1688 dsdt_line(" 0x0000, // Granularity");
1689 dsdt_line(" 0x0000, // Range Minimum");
1690 dsdt_line(" 0x0CF7, // Range Maximum");
1691 dsdt_line(" 0x0000, // Translation Offset");
1692 dsdt_line(" 0x0CF8, // Length");
1693 dsdt_line(" ,, , TypeStatic)");
1695 dsdt_line(" WordIO (ResourceProducer, MinFixed, MaxFixed, "
1696 "PosDecode, EntireRange,");
1697 dsdt_line(" 0x0000, // Granularity");
1698 dsdt_line(" 0x0D00, // Range Minimum");
1699 dsdt_line(" 0x%04X, // Range Maximum",
1700 PCI_EMUL_IOBASE - 1);
1701 dsdt_line(" 0x0000, // Translation Offset");
1702 dsdt_line(" 0x%04X, // Length",
1703 PCI_EMUL_IOBASE - 0x0D00);
1704 dsdt_line(" ,, , TypeStatic)");
1714 dsdt_line(" WordIO (ResourceProducer, MinFixed, MaxFixed, "
1715 "PosDecode, EntireRange,");
1716 dsdt_line(" 0x0000, // Granularity");
1717 dsdt_line(" 0x%04X, // Range Minimum", bi->iobase);
1718 dsdt_line(" 0x%04X, // Range Maximum",
1720 dsdt_line(" 0x0000, // Translation Offset");
1721 dsdt_line(" 0x%04X, // Length",
1722 bi->iolimit - bi->iobase);
1723 dsdt_line(" ,, , TypeStatic)");
1725 /* mmio window (32-bit) */
1726 dsdt_line(" DWordMemory (ResourceProducer, PosDecode, "
1727 "MinFixed, MaxFixed, NonCacheable, ReadWrite,");
1728 dsdt_line(" 0x00000000, // Granularity");
1729 dsdt_line(" 0x%08X, // Range Minimum\n", bi->membase32);
1730 dsdt_line(" 0x%08X, // Range Maximum\n",
1731 bi->memlimit32 - 1);
1732 dsdt_line(" 0x00000000, // Translation Offset");
1733 dsdt_line(" 0x%08X, // Length\n",
1734 bi->memlimit32 - bi->membase32);
1735 dsdt_line(" ,, , AddressRangeMemory, TypeStatic)");
1737 /* mmio window (64-bit) */
1738 dsdt_line(" QWordMemory (ResourceProducer, PosDecode, "
1739 "MinFixed, MaxFixed, NonCacheable, ReadWrite,");
1740 dsdt_line(" 0x0000000000000000, // Granularity");
1741 dsdt_line(" 0x%016lX, // Range Minimum\n", bi->membase64);
1742 dsdt_line(" 0x%016lX, // Range Maximum\n",
1743 bi->memlimit64 - 1);
1744 dsdt_line(" 0x0000000000000000, // Translation Offset");
1745 dsdt_line(" 0x%016lX, // Length\n",
1746 bi->memlimit64 - bi->membase64);
1747 dsdt_line(" ,, , AddressRangeMemory, TypeStatic)");
1750 count = pci_count_lintr(bus);
1753 dsdt_line("Name (PPRT, Package ()");
1755 pci_walk_lintr(bus, pci_pirq_prt_entry, NULL);
1757 dsdt_line("Name (APRT, Package ()");
1759 pci_walk_lintr(bus, pci_apic_prt_entry, NULL);
1761 dsdt_line("Method (_PRT, 0, NotSerialized)");
1763 dsdt_line(" If (PICM)");
1765 dsdt_line(" Return (APRT)");
1769 dsdt_line(" Return (PPRT)");
1776 for (slot = 0; slot < MAXSLOTS; slot++) {
1777 si = &bi->slotinfo[slot];
1778 for (func = 0; func < MAXFUNCS; func++) {
1779 pi = si->si_funcs[func].fi_devi;
1780 if (pi != NULL && pi->pi_d->pe_write_dsdt != NULL)
1781 pi->pi_d->pe_write_dsdt(pi);
1790 pci_write_dsdt(void)
1795 dsdt_line("Name (PICM, 0x00)");
1796 dsdt_line("Method (_PIC, 1, NotSerialized)");
1798 dsdt_line(" Store (Arg0, PICM)");
1801 dsdt_line("Scope (_SB)");
1803 for (bus = 0; bus < MAXBUSES; bus++)
1804 pci_bus_write_dsdt(bus);
1810 pci_bus_configured(int bus)
1812 assert(bus >= 0 && bus < MAXBUSES);
1813 return (pci_businfo[bus] != NULL);
1817 pci_msi_enabled(struct pci_devinst *pi)
1819 return (pi->pi_msi.enabled);
1823 pci_msi_maxmsgnum(struct pci_devinst *pi)
1825 if (pi->pi_msi.enabled)
1826 return (pi->pi_msi.maxmsgnum);
1832 pci_msix_enabled(struct pci_devinst *pi)
1835 return (pi->pi_msix.enabled && !pi->pi_msi.enabled);
1839 pci_generate_msix(struct pci_devinst *pi, int index)
1841 struct msix_table_entry *mte;
1843 if (!pci_msix_enabled(pi))
1846 if (pi->pi_msix.function_mask)
1849 if (index >= pi->pi_msix.table_count)
1852 mte = &pi->pi_msix.table[index];
1853 if ((mte->vector_control & PCIM_MSIX_VCTRL_MASK) == 0) {
1854 /* XXX Set PBA bit if interrupt is disabled */
1855 vm_lapic_msi(pi->pi_vmctx, mte->addr, mte->msg_data);
1860 pci_generate_msi(struct pci_devinst *pi, int index)
1863 if (pci_msi_enabled(pi) && index < pci_msi_maxmsgnum(pi)) {
1864 vm_lapic_msi(pi->pi_vmctx, pi->pi_msi.addr,
1865 pi->pi_msi.msg_data + index);
1870 pci_lintr_permitted(struct pci_devinst *pi)
1874 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND);
1875 return (!(pi->pi_msi.enabled || pi->pi_msix.enabled ||
1876 (cmd & PCIM_CMD_INTxDIS)));
1880 pci_lintr_request(struct pci_devinst *pi)
1883 struct slotinfo *si;
1884 int bestpin, bestcount, pin;
1886 bi = pci_businfo[pi->pi_bus];
1890 * Just allocate a pin from our slot. The pin will be
1891 * assigned IRQs later when interrupts are routed.
1893 si = &bi->slotinfo[pi->pi_slot];
1895 bestcount = si->si_intpins[0].ii_count;
1896 for (pin = 1; pin < 4; pin++) {
1897 if (si->si_intpins[pin].ii_count < bestcount) {
1899 bestcount = si->si_intpins[pin].ii_count;
1903 si->si_intpins[bestpin].ii_count++;
1904 pi->pi_lintr.pin = bestpin + 1;
1905 pci_set_cfgdata8(pi, PCIR_INTPIN, bestpin + 1);
1909 pci_lintr_route(struct pci_devinst *pi)
1912 struct intxinfo *ii;
1914 if (pi->pi_lintr.pin == 0)
1917 bi = pci_businfo[pi->pi_bus];
1919 ii = &bi->slotinfo[pi->pi_slot].si_intpins[pi->pi_lintr.pin - 1];
1922 * Attempt to allocate an I/O APIC pin for this intpin if one
1923 * is not yet assigned.
1925 if (ii->ii_ioapic_irq == 0)
1926 ii->ii_ioapic_irq = ioapic_pci_alloc_irq(pi);
1927 assert(ii->ii_ioapic_irq > 0);
1930 * Attempt to allocate a PIRQ pin for this intpin if one is
1933 if (ii->ii_pirq_pin == 0)
1934 ii->ii_pirq_pin = pirq_alloc_pin(pi);
1935 assert(ii->ii_pirq_pin > 0);
1937 pi->pi_lintr.ioapic_irq = ii->ii_ioapic_irq;
1938 pi->pi_lintr.pirq_pin = ii->ii_pirq_pin;
1939 pci_set_cfgdata8(pi, PCIR_INTLINE, pirq_irq(ii->ii_pirq_pin));
1943 pci_lintr_assert(struct pci_devinst *pi)
1946 assert(pi->pi_lintr.pin > 0);
1948 pthread_mutex_lock(&pi->pi_lintr.lock);
1949 if (pi->pi_lintr.state == IDLE) {
1950 if (pci_lintr_permitted(pi)) {
1951 pi->pi_lintr.state = ASSERTED;
1954 pi->pi_lintr.state = PENDING;
1956 pthread_mutex_unlock(&pi->pi_lintr.lock);
1960 pci_lintr_deassert(struct pci_devinst *pi)
1963 assert(pi->pi_lintr.pin > 0);
1965 pthread_mutex_lock(&pi->pi_lintr.lock);
1966 if (pi->pi_lintr.state == ASSERTED) {
1967 pi->pi_lintr.state = IDLE;
1968 pci_irq_deassert(pi);
1969 } else if (pi->pi_lintr.state == PENDING)
1970 pi->pi_lintr.state = IDLE;
1971 pthread_mutex_unlock(&pi->pi_lintr.lock);
1975 pci_lintr_update(struct pci_devinst *pi)
1978 pthread_mutex_lock(&pi->pi_lintr.lock);
1979 if (pi->pi_lintr.state == ASSERTED && !pci_lintr_permitted(pi)) {
1980 pci_irq_deassert(pi);
1981 pi->pi_lintr.state = PENDING;
1982 } else if (pi->pi_lintr.state == PENDING && pci_lintr_permitted(pi)) {
1983 pi->pi_lintr.state = ASSERTED;
1986 pthread_mutex_unlock(&pi->pi_lintr.lock);
1990 pci_count_lintr(int bus)
1992 int count, slot, pin;
1993 struct slotinfo *slotinfo;
1996 if (pci_businfo[bus] != NULL) {
1997 for (slot = 0; slot < MAXSLOTS; slot++) {
1998 slotinfo = &pci_businfo[bus]->slotinfo[slot];
1999 for (pin = 0; pin < 4; pin++) {
2000 if (slotinfo->si_intpins[pin].ii_count != 0)
2009 pci_walk_lintr(int bus, pci_lintr_cb cb, void *arg)
2012 struct slotinfo *si;
2013 struct intxinfo *ii;
2016 if ((bi = pci_businfo[bus]) == NULL)
2019 for (slot = 0; slot < MAXSLOTS; slot++) {
2020 si = &bi->slotinfo[slot];
2021 for (pin = 0; pin < 4; pin++) {
2022 ii = &si->si_intpins[pin];
2023 if (ii->ii_count != 0)
2024 cb(bus, slot, pin + 1, ii->ii_pirq_pin,
2025 ii->ii_ioapic_irq, arg);
2031 * Return 1 if the emulated device in 'slot' is a multi-function device.
2032 * Return 0 otherwise.
2035 pci_emul_is_mfdev(int bus, int slot)
2038 struct slotinfo *si;
2042 if ((bi = pci_businfo[bus]) != NULL) {
2043 si = &bi->slotinfo[slot];
2044 for (f = 0; f < MAXFUNCS; f++) {
2045 if (si->si_funcs[f].fi_devi != NULL) {
2050 return (numfuncs > 1);
2054 * Ensure that the PCIM_MFDEV bit is properly set (or unset) depending on
2055 * whether or not is a multi-function being emulated in the pci 'slot'.
2058 pci_emul_hdrtype_fixup(int bus, int slot, int off, int bytes, uint32_t *rv)
2062 if (off <= PCIR_HDRTYPE && off + bytes > PCIR_HDRTYPE) {
2063 mfdev = pci_emul_is_mfdev(bus, slot);
2073 *rv &= ~(PCIM_MFDEV << 16);
2075 *rv |= (PCIM_MFDEV << 16);
2083 * Update device state in response to changes to the PCI command
2087 pci_emul_cmd_changed(struct pci_devinst *pi, uint16_t old)
2090 uint16_t changed, new;
2092 new = pci_get_cfgdata16(pi, PCIR_COMMAND);
2093 changed = old ^ new;
2096 * If the MMIO or I/O address space decoding has changed then
2097 * register/unregister all BARs that decode that address space.
2099 for (i = 0; i <= PCI_BARMAX_WITH_ROM; i++) {
2100 switch (pi->pi_bar[i].type) {
2102 case PCIBAR_MEMHI64:
2105 /* I/O address space decoding changed? */
2106 if (changed & PCIM_CMD_PORTEN) {
2107 if (new & PCIM_CMD_PORTEN)
2108 register_bar(pi, i);
2110 unregister_bar(pi, i);
2114 /* skip (un-)register of ROM if it disabled */
2120 /* MMIO address space decoding changed? */
2121 if (changed & PCIM_CMD_MEMEN) {
2122 if (new & PCIM_CMD_MEMEN)
2123 register_bar(pi, i);
2125 unregister_bar(pi, i);
2134 * If INTx has been unmasked and is pending, assert the
2137 pci_lintr_update(pi);
2141 pci_emul_cmdsts_write(struct pci_devinst *pi, int coff, uint32_t new, int bytes)
2144 uint32_t cmd, old, readonly;
2146 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND); /* stash old value */
2149 * From PCI Local Bus Specification 3.0 sections 6.2.2 and 6.2.3.
2151 * XXX Bits 8, 11, 12, 13, 14 and 15 in the status register are
2152 * 'write 1 to clear'. However these bits are not set to '1' by
2153 * any device emulation so it is simpler to treat them as readonly.
2155 rshift = (coff & 0x3) * 8;
2156 readonly = 0xFFFFF880 >> rshift;
2158 old = CFGREAD(pi, coff, bytes);
2160 new |= (old & readonly);
2161 CFGWRITE(pi, coff, new, bytes); /* update config */
2163 pci_emul_cmd_changed(pi, cmd);
2167 pci_cfgrw(int in, int bus, int slot, int func, int coff, int bytes,
2171 struct slotinfo *si;
2172 struct pci_devinst *pi;
2173 struct pci_devemu *pe;
2175 uint64_t addr, bar, mask;
2177 if ((bi = pci_businfo[bus]) != NULL) {
2178 si = &bi->slotinfo[slot];
2179 pi = si->si_funcs[func].fi_devi;
2184 * Just return if there is no device at this slot:func or if the
2185 * the guest is doing an un-aligned access.
2187 if (pi == NULL || (bytes != 1 && bytes != 2 && bytes != 4) ||
2188 (coff & (bytes - 1)) != 0) {
2195 * Ignore all writes beyond the standard config space and return all
2198 if (coff >= PCI_REGMAX + 1) {
2202 * Extended capabilities begin at offset 256 in config
2203 * space. Absence of extended capabilities is signaled
2204 * with all 0s in the extended capability header at
2207 if (coff <= PCI_REGMAX + 4)
2219 /* Let the device emulation override the default handler */
2220 if (pe->pe_cfgread != NULL) {
2221 needcfg = pe->pe_cfgread(pi, coff, bytes, valp);
2227 *valp = CFGREAD(pi, coff, bytes);
2229 pci_emul_hdrtype_fixup(bus, slot, coff, bytes, valp);
2231 /* Let the device emulation override the default handler */
2232 if (pe->pe_cfgwrite != NULL &&
2233 (*pe->pe_cfgwrite)(pi, coff, bytes, *valp) == 0)
2237 * Special handling for write to BAR and ROM registers
2239 if (is_pcir_bar(coff) || is_pcir_bios(coff)) {
2241 * Ignore writes to BAR registers that are not
2244 if (bytes != 4 || (coff & 0x3) != 0)
2247 if (is_pcir_bar(coff)) {
2248 idx = (coff - PCIR_BAR(0)) / 4;
2249 } else if (is_pcir_bios(coff)) {
2252 errx(4, "%s: invalid BAR offset %d", __func__,
2256 mask = ~(pi->pi_bar[idx].size - 1);
2257 switch (pi->pi_bar[idx].type) {
2259 pi->pi_bar[idx].addr = bar = 0;
2262 addr = *valp & mask;
2264 bar = addr | pi->pi_bar[idx].lobits;
2266 * Register the new BAR value for interception
2268 if (addr != pi->pi_bar[idx].addr) {
2269 update_bar_address(pi, addr, idx,
2274 addr = bar = *valp & mask;
2275 bar |= pi->pi_bar[idx].lobits;
2276 if (addr != pi->pi_bar[idx].addr) {
2277 update_bar_address(pi, addr, idx,
2282 addr = bar = *valp & mask;
2283 bar |= pi->pi_bar[idx].lobits;
2284 if (addr != (uint32_t)pi->pi_bar[idx].addr) {
2285 update_bar_address(pi, addr, idx,
2289 case PCIBAR_MEMHI64:
2290 mask = ~(pi->pi_bar[idx - 1].size - 1);
2291 addr = ((uint64_t)*valp << 32) & mask;
2293 if (bar != pi->pi_bar[idx - 1].addr >> 32) {
2294 update_bar_address(pi, addr, idx - 1,
2299 addr = bar = *valp & mask;
2300 if (memen(pi) && romen(pi)) {
2301 unregister_bar(pi, idx);
2303 pi->pi_bar[idx].addr = addr;
2304 pi->pi_bar[idx].lobits = *valp &
2306 /* romen could have changed it value */
2307 if (memen(pi) && romen(pi)) {
2308 register_bar(pi, idx);
2310 bar |= pi->pi_bar[idx].lobits;
2315 pci_set_cfgdata32(pi, coff, bar);
2317 } else if (pci_emul_iscap(pi, coff)) {
2318 pci_emul_capwrite(pi, coff, bytes, *valp, 0, 0);
2319 } else if (coff >= PCIR_COMMAND && coff < PCIR_REVID) {
2320 pci_emul_cmdsts_write(pi, coff, *valp, bytes);
2322 CFGWRITE(pi, coff, *valp, bytes);
2327 static int cfgenable, cfgbus, cfgslot, cfgfunc, cfgoff;
2330 pci_emul_cfgaddr(struct vmctx *ctx __unused, int in,
2331 int port __unused, int bytes, uint32_t *eax, void *arg __unused)
2337 *eax = (bytes == 2) ? 0xffff : 0xff;
2342 x = (cfgbus << 16) | (cfgslot << 11) | (cfgfunc << 8) | cfgoff;
2348 cfgenable = (x & CONF1_ENABLE) == CONF1_ENABLE;
2349 cfgoff = (x & PCI_REGMAX) & ~0x03;
2350 cfgfunc = (x >> 8) & PCI_FUNCMAX;
2351 cfgslot = (x >> 11) & PCI_SLOTMAX;
2352 cfgbus = (x >> 16) & PCI_BUSMAX;
2357 INOUT_PORT(pci_cfgaddr, CONF1_ADDR_PORT, IOPORT_F_INOUT, pci_emul_cfgaddr);
2360 pci_emul_cfgdata(struct vmctx *ctx __unused, int in, int port,
2361 int bytes, uint32_t *eax, void *arg __unused)
2365 assert(bytes == 1 || bytes == 2 || bytes == 4);
2367 coff = cfgoff + (port - CONF1_DATA_PORT);
2369 pci_cfgrw(in, cfgbus, cfgslot, cfgfunc, coff, bytes, eax);
2371 /* Ignore accesses to cfgdata if not enabled by cfgaddr */
2378 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+0, IOPORT_F_INOUT, pci_emul_cfgdata);
2379 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+1, IOPORT_F_INOUT, pci_emul_cfgdata);
2380 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+2, IOPORT_F_INOUT, pci_emul_cfgdata);
2381 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+3, IOPORT_F_INOUT, pci_emul_cfgdata);
2383 #ifdef BHYVE_SNAPSHOT
2385 * Saves/restores PCI device emulated state. Returns 0 on success.
2388 pci_snapshot_pci_dev(struct vm_snapshot_meta *meta)
2390 struct pci_devinst *pi;
2394 pi = meta->dev_data;
2396 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msi.enabled, meta, ret, done);
2397 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msi.addr, meta, ret, done);
2398 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msi.msg_data, meta, ret, done);
2399 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msi.maxmsgnum, meta, ret, done);
2401 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.enabled, meta, ret, done);
2402 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.table_bar, meta, ret, done);
2403 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.pba_bar, meta, ret, done);
2404 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.table_offset, meta, ret, done);
2405 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.table_count, meta, ret, done);
2406 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.pba_offset, meta, ret, done);
2407 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.pba_size, meta, ret, done);
2408 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.function_mask, meta, ret, done);
2410 SNAPSHOT_BUF_OR_LEAVE(pi->pi_cfgdata, sizeof(pi->pi_cfgdata),
2413 for (i = 0; i < (int)nitems(pi->pi_bar); i++) {
2414 SNAPSHOT_VAR_OR_LEAVE(pi->pi_bar[i].type, meta, ret, done);
2415 SNAPSHOT_VAR_OR_LEAVE(pi->pi_bar[i].size, meta, ret, done);
2416 SNAPSHOT_VAR_OR_LEAVE(pi->pi_bar[i].addr, meta, ret, done);
2419 /* Restore MSI-X table. */
2420 for (i = 0; i < pi->pi_msix.table_count; i++) {
2421 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.table[i].addr,
2423 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.table[i].msg_data,
2425 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.table[i].vector_control,
2434 pci_snapshot(struct vm_snapshot_meta *meta)
2436 struct pci_devemu *pde;
2437 struct pci_devinst *pdi;
2440 assert(meta->dev_name != NULL);
2442 pdi = meta->dev_data;
2445 if (pde->pe_snapshot == NULL)
2448 ret = pci_snapshot_pci_dev(meta);
2450 ret = (*pde->pe_snapshot)(meta);
2456 pci_pause(struct pci_devinst *pdi)
2458 struct pci_devemu *pde = pdi->pi_d;
2460 if (pde->pe_pause == NULL) {
2461 /* The pause/resume functionality is optional. */
2465 return (*pde->pe_pause)(pdi);
2469 pci_resume(struct pci_devinst *pdi)
2471 struct pci_devemu *pde = pdi->pi_d;
2473 if (pde->pe_resume == NULL) {
2474 /* The pause/resume functionality is optional. */
2478 return (*pde->pe_resume)(pdi);
2482 #define PCI_EMUL_TEST
2483 #ifdef PCI_EMUL_TEST
2485 * Define a dummy test device
2489 struct pci_emul_dsoftc {
2490 uint8_t ioregs[DIOSZ];
2491 uint8_t memregs[2][DMEMSZ];
2494 #define PCI_EMUL_MSI_MSGS 4
2495 #define PCI_EMUL_MSIX_MSGS 16
2498 pci_emul_dinit(struct pci_devinst *pi, nvlist_t *nvl __unused)
2501 struct pci_emul_dsoftc *sc;
2503 sc = calloc(1, sizeof(struct pci_emul_dsoftc));
2507 pci_set_cfgdata16(pi, PCIR_DEVICE, 0x0001);
2508 pci_set_cfgdata16(pi, PCIR_VENDOR, 0x10DD);
2509 pci_set_cfgdata8(pi, PCIR_CLASS, 0x02);
2511 error = pci_emul_add_msicap(pi, PCI_EMUL_MSI_MSGS);
2514 error = pci_emul_alloc_bar(pi, 0, PCIBAR_IO, DIOSZ);
2517 error = pci_emul_alloc_bar(pi, 1, PCIBAR_MEM32, DMEMSZ);
2520 error = pci_emul_alloc_bar(pi, 2, PCIBAR_MEM32, DMEMSZ);
2527 pci_emul_diow(struct pci_devinst *pi, int baridx, uint64_t offset, int size,
2531 struct pci_emul_dsoftc *sc = pi->pi_arg;
2534 if (offset + size > DIOSZ) {
2535 printf("diow: iow too large, offset %ld size %d\n",
2541 sc->ioregs[offset] = value & 0xff;
2542 } else if (size == 2) {
2543 *(uint16_t *)&sc->ioregs[offset] = value & 0xffff;
2544 } else if (size == 4) {
2545 *(uint32_t *)&sc->ioregs[offset] = value;
2547 printf("diow: iow unknown size %d\n", size);
2551 * Special magic value to generate an interrupt
2553 if (offset == 4 && size == 4 && pci_msi_enabled(pi))
2554 pci_generate_msi(pi, value % pci_msi_maxmsgnum(pi));
2556 if (value == 0xabcdef) {
2557 for (i = 0; i < pci_msi_maxmsgnum(pi); i++)
2558 pci_generate_msi(pi, i);
2562 if (baridx == 1 || baridx == 2) {
2563 if (offset + size > DMEMSZ) {
2564 printf("diow: memw too large, offset %ld size %d\n",
2569 i = baridx - 1; /* 'memregs' index */
2572 sc->memregs[i][offset] = value;
2573 } else if (size == 2) {
2574 *(uint16_t *)&sc->memregs[i][offset] = value;
2575 } else if (size == 4) {
2576 *(uint32_t *)&sc->memregs[i][offset] = value;
2577 } else if (size == 8) {
2578 *(uint64_t *)&sc->memregs[i][offset] = value;
2580 printf("diow: memw unknown size %d\n", size);
2584 * magic interrupt ??
2588 if (baridx > 2 || baridx < 0) {
2589 printf("diow: unknown bar idx %d\n", baridx);
2594 pci_emul_dior(struct pci_devinst *pi, int baridx, uint64_t offset, int size)
2596 struct pci_emul_dsoftc *sc = pi->pi_arg;
2601 if (offset + size > DIOSZ) {
2602 printf("dior: ior too large, offset %ld size %d\n",
2609 value = sc->ioregs[offset];
2610 } else if (size == 2) {
2611 value = *(uint16_t *) &sc->ioregs[offset];
2612 } else if (size == 4) {
2613 value = *(uint32_t *) &sc->ioregs[offset];
2615 printf("dior: ior unknown size %d\n", size);
2619 if (baridx == 1 || baridx == 2) {
2620 if (offset + size > DMEMSZ) {
2621 printf("dior: memr too large, offset %ld size %d\n",
2626 i = baridx - 1; /* 'memregs' index */
2629 value = sc->memregs[i][offset];
2630 } else if (size == 2) {
2631 value = *(uint16_t *) &sc->memregs[i][offset];
2632 } else if (size == 4) {
2633 value = *(uint32_t *) &sc->memregs[i][offset];
2634 } else if (size == 8) {
2635 value = *(uint64_t *) &sc->memregs[i][offset];
2637 printf("dior: ior unknown size %d\n", size);
2642 if (baridx > 2 || baridx < 0) {
2643 printf("dior: unknown bar idx %d\n", baridx);
2650 #ifdef BHYVE_SNAPSHOT
2651 struct pci_devinst *
2652 pci_next(const struct pci_devinst *cursor)
2654 unsigned bus = 0, slot = 0, func = 0;
2656 struct slotinfo *si;
2657 struct funcinfo *fi;
2659 bus = cursor ? cursor->pi_bus : 0;
2660 slot = cursor ? cursor->pi_slot : 0;
2661 func = cursor ? (cursor->pi_func + 1) : 0;
2663 for (; bus < MAXBUSES; bus++) {
2664 if ((bi = pci_businfo[bus]) == NULL)
2667 if (slot >= MAXSLOTS)
2670 for (; slot < MAXSLOTS; slot++) {
2671 si = &bi->slotinfo[slot];
2672 if (func >= MAXFUNCS)
2674 for (; func < MAXFUNCS; func++) {
2675 fi = &si->si_funcs[func];
2676 if (fi->fi_devi == NULL)
2679 return (fi->fi_devi);
2688 pci_emul_snapshot(struct vm_snapshot_meta *meta __unused)
2694 static const struct pci_devemu pci_dummy = {
2696 .pe_init = pci_emul_dinit,
2697 .pe_barwrite = pci_emul_diow,
2698 .pe_barread = pci_emul_dior,
2699 #ifdef BHYVE_SNAPSHOT
2700 .pe_snapshot = pci_emul_snapshot,
2703 PCI_EMUL_SET(pci_dummy);
2705 #endif /* PCI_EMUL_TEST */