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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/linker_set.h>
50 #include <machine/vmm.h>
51 #include <machine/vmm_snapshot.h>
64 #include "pci_passthru.h"
65 #include "qemu_fwcfg.h"
67 #define CONF1_ADDR_PORT 0x0cf8
68 #define CONF1_DATA_PORT 0x0cfc
70 #define CONF1_ENABLE 0x80000000ul
72 #define MAXBUSES (PCI_BUSMAX + 1)
73 #define MAXSLOTS (PCI_SLOTMAX + 1)
74 #define MAXFUNCS (PCI_FUNCMAX + 1)
76 #define GB (1024 * 1024 * 1024UL)
80 struct pci_devemu *fi_pde;
81 struct pci_devinst *fi_devi;
91 struct intxinfo si_intpins[4];
92 struct funcinfo si_funcs[MAXFUNCS];
96 uint16_t iobase, iolimit; /* I/O window */
97 uint32_t membase32, memlimit32; /* mmio window below 4GB */
98 uint64_t membase64, memlimit64; /* mmio window above 4GB */
99 struct slotinfo slotinfo[MAXSLOTS];
102 static struct businfo *pci_businfo[MAXBUSES];
104 SET_DECLARE(pci_devemu_set, struct pci_devemu);
106 static uint64_t pci_emul_iobase;
107 static uint8_t *pci_emul_rombase;
108 static uint64_t pci_emul_romoffset;
109 static uint8_t *pci_emul_romlim;
110 static uint64_t pci_emul_membase32;
111 static uint64_t pci_emul_membase64;
112 static uint64_t pci_emul_memlim64;
114 struct pci_bar_allocation {
115 TAILQ_ENTRY(pci_bar_allocation) chain;
116 struct pci_devinst *pdi;
118 enum pcibar_type type;
122 static TAILQ_HEAD(pci_bar_list, pci_bar_allocation) pci_bars =
123 TAILQ_HEAD_INITIALIZER(pci_bars);
126 TAILQ_ENTRY(boot_device) boot_device_chain;
127 struct pci_devinst *pdi;
130 static TAILQ_HEAD(boot_list, boot_device) boot_devices = TAILQ_HEAD_INITIALIZER(
133 #define PCI_EMUL_IOBASE 0x2000
134 #define PCI_EMUL_IOLIMIT 0x10000
136 #define PCI_EMUL_ROMSIZE 0x10000000
138 #define PCI_EMUL_ECFG_BASE 0xE0000000 /* 3.5GB */
139 #define PCI_EMUL_ECFG_SIZE (MAXBUSES * 1024 * 1024) /* 1MB per bus */
140 SYSRES_MEM(PCI_EMUL_ECFG_BASE, PCI_EMUL_ECFG_SIZE);
143 * OVMF always uses 0xC0000000 as base address for 32 bit PCI MMIO. Don't
144 * change this address without changing it in OVMF.
146 #define PCI_EMUL_MEMBASE32 0xC0000000
147 #define PCI_EMUL_MEMLIMIT32 PCI_EMUL_ECFG_BASE
148 #define PCI_EMUL_MEMSIZE64 (32*GB)
150 static struct pci_devemu *pci_emul_finddev(const char *name);
151 static void pci_lintr_route(struct pci_devinst *pi);
152 static void pci_lintr_update(struct pci_devinst *pi);
153 static void pci_cfgrw(int in, int bus, int slot, int func, int coff,
154 int bytes, uint32_t *val);
157 CFGWRITE(struct pci_devinst *pi, int coff, uint32_t val, int bytes)
161 pci_set_cfgdata8(pi, coff, val);
163 pci_set_cfgdata16(pi, coff, val);
165 pci_set_cfgdata32(pi, coff, val);
168 static __inline uint32_t
169 CFGREAD(struct pci_devinst *pi, int coff, int bytes)
173 return (pci_get_cfgdata8(pi, coff));
175 return (pci_get_cfgdata16(pi, coff));
177 return (pci_get_cfgdata32(pi, coff));
181 is_pcir_bar(int coff)
183 return (coff >= PCIR_BAR(0) && coff < PCIR_BAR(PCI_BARMAX + 1));
187 is_pcir_bios(int coff)
189 return (coff >= PCIR_BIOS && coff < PCIR_BIOS + 4);
197 * Slot options are in the form:
199 * <bus>:<slot>:<func>,<emul>[,<config>]
200 * <slot>[:<func>],<emul>[,<config>]
204 * emul is a string describing the type of PCI device e.g. virtio-net
205 * config is an optional string, depending on the device, that can be
206 * used for configuration.
212 pci_parse_slot_usage(char *aopt)
215 EPRINTLN("Invalid PCI slot info field \"%s\"", aopt);
219 * Helper function to parse a list of comma-separated options where
220 * each option is formatted as "name[=value]". If no value is
221 * provided, the option is treated as a boolean and is given a value
225 pci_parse_legacy_config(nvlist_t *nvl, const char *opt)
227 char *config, *name, *tofree, *value;
232 config = tofree = strdup(opt);
233 while ((name = strsep(&config, ",")) != NULL) {
234 value = strchr(name, '=');
238 set_config_value_node(nvl, name, value);
240 set_config_bool_node(nvl, name, true);
247 * PCI device configuration is stored in MIBs that encode the device's
250 * pci.<bus>.<slot>.<func>
252 * Where "bus", "slot", and "func" are all decimal values without
253 * leading zeroes. Each valid device must have a "device" node which
254 * identifies the driver model of the device.
256 * Device backends can provide a parser for the "config" string. If
257 * a custom parser is not provided, pci_parse_legacy_config() is used
258 * to parse the string.
261 pci_parse_slot(char *opt)
263 char node_name[sizeof("pci.XXX.XX.X")];
264 struct pci_devemu *pde;
265 char *emul, *config, *str, *cp;
266 int error, bnum, snum, fnum;
272 emul = config = NULL;
273 if ((cp = strchr(str, ',')) != NULL) {
276 if ((cp = strchr(emul, ',')) != NULL) {
281 pci_parse_slot_usage(opt);
285 /* <bus>:<slot>:<func> */
286 if (sscanf(str, "%d:%d:%d", &bnum, &snum, &fnum) != 3) {
289 if (sscanf(str, "%d:%d", &snum, &fnum) != 2) {
292 if (sscanf(str, "%d", &snum) != 1) {
298 if (bnum < 0 || bnum >= MAXBUSES || snum < 0 || snum >= MAXSLOTS ||
299 fnum < 0 || fnum >= MAXFUNCS) {
300 pci_parse_slot_usage(opt);
304 pde = pci_emul_finddev(emul);
306 EPRINTLN("pci slot %d:%d:%d: unknown device \"%s\"", bnum, snum,
311 snprintf(node_name, sizeof(node_name), "pci.%d.%d.%d", bnum, snum,
313 nvl = find_config_node(node_name);
315 EPRINTLN("pci slot %d:%d:%d already occupied!", bnum, snum,
319 nvl = create_config_node(node_name);
320 if (pde->pe_alias != NULL)
321 set_config_value_node(nvl, "device", pde->pe_alias);
323 set_config_value_node(nvl, "device", pde->pe_emu);
325 if (pde->pe_legacy_config != NULL)
326 error = pde->pe_legacy_config(nvl, config);
328 error = pci_parse_legacy_config(nvl, config);
335 pci_print_supported_devices(void)
337 struct pci_devemu **pdpp, *pdp;
339 SET_FOREACH(pdpp, pci_devemu_set) {
341 printf("%s\n", pdp->pe_emu);
346 pci_config_read_reg(const struct pcisel *const host_sel, nvlist_t *nvl,
347 const uint32_t reg, const uint8_t size, const uint32_t def)
350 const nvlist_t *pci_regs;
352 assert(size == 1 || size == 2 || size == 4);
354 pci_regs = find_relative_config_node(nvl, "pcireg");
355 if (pci_regs == NULL) {
361 config = get_config_value_node(pci_regs, "device");
364 config = get_config_value_node(pci_regs, "vendor");
367 config = get_config_value_node(pci_regs, "revid");
370 config = get_config_value_node(pci_regs, "subvendor");
373 config = get_config_value_node(pci_regs, "subdevice");
379 if (config == NULL) {
381 } else if (host_sel != NULL && strcmp(config, "host") == 0) {
382 return read_config(host_sel, reg, size);
384 return strtol(config, NULL, 16);
389 pci_valid_pba_offset(struct pci_devinst *pi, uint64_t offset)
392 if (offset < pi->pi_msix.pba_offset)
395 if (offset >= pi->pi_msix.pba_offset + pi->pi_msix.pba_size) {
403 pci_emul_msix_twrite(struct pci_devinst *pi, uint64_t offset, int size,
406 int msix_entry_offset;
410 /* support only 4 or 8 byte writes */
411 if (size != 4 && size != 8)
415 * Return if table index is beyond what device supports
417 tab_index = offset / MSIX_TABLE_ENTRY_SIZE;
418 if (tab_index >= pi->pi_msix.table_count)
421 msix_entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
423 /* support only aligned writes */
424 if ((msix_entry_offset % size) != 0)
427 dest = (char *)(pi->pi_msix.table + tab_index);
428 dest += msix_entry_offset;
431 *((uint32_t *)dest) = value;
433 *((uint64_t *)dest) = value;
439 pci_emul_msix_tread(struct pci_devinst *pi, uint64_t offset, int size)
442 int msix_entry_offset;
444 uint64_t retval = ~0;
447 * The PCI standard only allows 4 and 8 byte accesses to the MSI-X
448 * table but we also allow 1 byte access to accommodate reads from
451 if (size != 1 && size != 4 && size != 8)
454 msix_entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
456 /* support only aligned reads */
457 if ((msix_entry_offset % size) != 0) {
461 tab_index = offset / MSIX_TABLE_ENTRY_SIZE;
463 if (tab_index < pi->pi_msix.table_count) {
464 /* valid MSI-X Table access */
465 dest = (char *)(pi->pi_msix.table + tab_index);
466 dest += msix_entry_offset;
469 retval = *((uint8_t *)dest);
471 retval = *((uint32_t *)dest);
473 retval = *((uint64_t *)dest);
474 } else if (pci_valid_pba_offset(pi, offset)) {
475 /* return 0 for PBA access */
483 pci_msix_table_bar(struct pci_devinst *pi)
486 if (pi->pi_msix.table != NULL)
487 return (pi->pi_msix.table_bar);
493 pci_msix_pba_bar(struct pci_devinst *pi)
496 if (pi->pi_msix.table != NULL)
497 return (pi->pi_msix.pba_bar);
503 pci_emul_io_handler(struct vmctx *ctx __unused, int in, int port,
504 int bytes, uint32_t *eax, void *arg)
506 struct pci_devinst *pdi = arg;
507 struct pci_devemu *pe = pdi->pi_d;
513 for (i = 0; i <= PCI_BARMAX; i++) {
514 if (pdi->pi_bar[i].type == PCIBAR_IO &&
515 (uint64_t)port >= pdi->pi_bar[i].addr &&
516 (uint64_t)port + bytes <=
517 pdi->pi_bar[i].addr + pdi->pi_bar[i].size) {
518 offset = port - pdi->pi_bar[i].addr;
520 *eax = (*pe->pe_barread)(pdi, i,
523 (*pe->pe_barwrite)(pdi, i, offset,
532 pci_emul_mem_handler(struct vcpu *vcpu __unused, int dir,
533 uint64_t addr, int size, uint64_t *val, void *arg1, long arg2)
535 struct pci_devinst *pdi = arg1;
536 struct pci_devemu *pe = pdi->pi_d;
538 int bidx = (int) arg2;
540 assert(bidx <= PCI_BARMAX);
541 assert(pdi->pi_bar[bidx].type == PCIBAR_MEM32 ||
542 pdi->pi_bar[bidx].type == PCIBAR_MEM64);
543 assert(addr >= pdi->pi_bar[bidx].addr &&
544 addr + size <= pdi->pi_bar[bidx].addr + pdi->pi_bar[bidx].size);
546 offset = addr - pdi->pi_bar[bidx].addr;
548 if (dir == MEM_F_WRITE) {
550 (*pe->pe_barwrite)(pdi, bidx, offset,
551 4, *val & 0xffffffff);
552 (*pe->pe_barwrite)(pdi, bidx, offset + 4,
555 (*pe->pe_barwrite)(pdi, bidx, offset,
560 *val = (*pe->pe_barread)(pdi, bidx,
562 *val |= (*pe->pe_barread)(pdi, bidx,
563 offset + 4, 4) << 32;
565 *val = (*pe->pe_barread)(pdi, bidx,
575 pci_emul_alloc_resource(uint64_t *baseptr, uint64_t limit, uint64_t size,
580 assert((size & (size - 1)) == 0); /* must be a power of 2 */
582 base = roundup2(*baseptr, size);
584 if (base + size <= limit) {
586 *baseptr = base + size;
593 * Register (or unregister) the MMIO or I/O region associated with the BAR
594 * register 'idx' of an emulated pci device.
597 modify_bar_registration(struct pci_devinst *pi, int idx, int registration)
599 struct pci_devemu *pe;
601 struct inout_port iop;
605 switch (pi->pi_bar[idx].type) {
607 bzero(&iop, sizeof(struct inout_port));
608 iop.name = pi->pi_name;
609 iop.port = pi->pi_bar[idx].addr;
610 iop.size = pi->pi_bar[idx].size;
612 iop.flags = IOPORT_F_INOUT;
613 iop.handler = pci_emul_io_handler;
615 error = register_inout(&iop);
617 error = unregister_inout(&iop);
621 bzero(&mr, sizeof(struct mem_range));
622 mr.name = pi->pi_name;
623 mr.base = pi->pi_bar[idx].addr;
624 mr.size = pi->pi_bar[idx].size;
627 mr.handler = pci_emul_mem_handler;
630 error = register_mem(&mr);
632 error = unregister_mem(&mr);
643 if (pe->pe_baraddr != NULL)
644 (*pe->pe_baraddr)(pi, idx, registration, pi->pi_bar[idx].addr);
648 unregister_bar(struct pci_devinst *pi, int idx)
651 modify_bar_registration(pi, idx, 0);
655 register_bar(struct pci_devinst *pi, int idx)
658 modify_bar_registration(pi, idx, 1);
661 /* Is the ROM enabled for the emulated pci device? */
663 romen(struct pci_devinst *pi)
665 return (pi->pi_bar[PCI_ROM_IDX].lobits & PCIM_BIOS_ENABLE) ==
669 /* Are we decoding i/o port accesses for the emulated pci device? */
671 porten(struct pci_devinst *pi)
675 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND);
677 return (cmd & PCIM_CMD_PORTEN);
680 /* Are we decoding memory accesses for the emulated pci device? */
682 memen(struct pci_devinst *pi)
686 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND);
688 return (cmd & PCIM_CMD_MEMEN);
692 * Update the MMIO or I/O address that is decoded by the BAR register.
694 * If the pci device has enabled the address space decoding then intercept
695 * the address range decoded by the BAR register.
698 update_bar_address(struct pci_devinst *pi, uint64_t addr, int idx, int type)
702 if (pi->pi_bar[idx].type == PCIBAR_IO)
708 unregister_bar(pi, idx);
713 pi->pi_bar[idx].addr = addr;
716 pi->pi_bar[idx].addr &= ~0xffffffffUL;
717 pi->pi_bar[idx].addr |= addr;
720 pi->pi_bar[idx].addr &= 0xffffffff;
721 pi->pi_bar[idx].addr |= addr;
728 register_bar(pi, idx);
732 pci_emul_alloc_bar(struct pci_devinst *pdi, int idx, enum pcibar_type type,
735 assert((type == PCIBAR_ROM) || (idx >= 0 && idx <= PCI_BARMAX));
736 assert((type != PCIBAR_ROM) || (idx == PCI_ROM_IDX));
738 if ((size & (size - 1)) != 0)
739 size = 1UL << flsl(size); /* round up to a power of 2 */
741 /* Enforce minimum BAR sizes required by the PCI standard */
742 if (type == PCIBAR_IO) {
745 } else if (type == PCIBAR_ROM) {
746 if (size < ~PCIM_BIOS_ADDR_MASK + 1)
747 size = ~PCIM_BIOS_ADDR_MASK + 1;
754 * To reduce fragmentation of the MMIO space, we allocate the BARs by
755 * size. Therefore, don't allocate the BAR yet. We create a list of all
756 * BAR allocation which is sorted by BAR size. When all PCI devices are
757 * initialized, we will assign an address to the BARs.
760 /* create a new list entry */
761 struct pci_bar_allocation *const new_bar = malloc(sizeof(*new_bar));
762 memset(new_bar, 0, sizeof(*new_bar));
765 new_bar->type = type;
766 new_bar->size = size;
769 * Search for a BAR which size is lower than the size of our newly
772 struct pci_bar_allocation *bar = NULL;
773 TAILQ_FOREACH(bar, &pci_bars, chain) {
774 if (bar->size < size) {
781 * Either the list is empty or new BAR is the smallest BAR of
782 * the list. Append it to the end of our list.
784 TAILQ_INSERT_TAIL(&pci_bars, new_bar, chain);
787 * The found BAR is smaller than our new BAR. For that reason,
788 * insert our new BAR before the found BAR.
790 TAILQ_INSERT_BEFORE(bar, new_bar, chain);
794 * pci_passthru devices synchronize their physical and virtual command
795 * register on init. For that reason, the virtual cmd reg should be
796 * updated as early as possible.
801 enbit = PCIM_CMD_PORTEN;
805 enbit = PCIM_CMD_MEMEN;
812 const uint16_t cmd = pci_get_cfgdata16(pdi, PCIR_COMMAND);
813 pci_set_cfgdata16(pdi, PCIR_COMMAND, cmd | enbit);
819 pci_emul_assign_bar(struct pci_devinst *const pdi, const int idx,
820 const enum pcibar_type type, const uint64_t size)
823 uint64_t *baseptr, limit, addr, mask, lobits, bar;
828 addr = mask = lobits = 0;
831 baseptr = &pci_emul_iobase;
832 limit = PCI_EMUL_IOLIMIT;
833 mask = PCIM_BAR_IO_BASE;
834 lobits = PCIM_BAR_IO_SPACE;
839 * Some drivers do not work well if the 64-bit BAR is allocated
840 * above 4GB. Allow for this by allocating small requests under
841 * 4GB unless then allocation size is larger than some arbitrary
842 * number (128MB currently).
844 if (size > 128 * 1024 * 1024) {
845 baseptr = &pci_emul_membase64;
846 limit = pci_emul_memlim64;
847 mask = PCIM_BAR_MEM_BASE;
848 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64 |
849 PCIM_BAR_MEM_PREFETCH;
851 baseptr = &pci_emul_membase32;
852 limit = PCI_EMUL_MEMLIMIT32;
853 mask = PCIM_BAR_MEM_BASE;
854 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64;
858 baseptr = &pci_emul_membase32;
859 limit = PCI_EMUL_MEMLIMIT32;
860 mask = PCIM_BAR_MEM_BASE;
861 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_32;
864 /* do not claim memory for ROM. OVMF will do it for us. */
867 mask = PCIM_BIOS_ADDR_MASK;
871 printf("pci_emul_alloc_base: invalid bar type %d\n", type);
875 if (baseptr != NULL) {
876 error = pci_emul_alloc_resource(baseptr, limit, size, &addr);
883 pdi->pi_bar[idx].type = type;
884 pdi->pi_bar[idx].addr = addr;
885 pdi->pi_bar[idx].size = size;
887 * passthru devices are using same lobits as physical device they set
890 if (pdi->pi_bar[idx].lobits != 0) {
891 lobits = pdi->pi_bar[idx].lobits;
893 pdi->pi_bar[idx].lobits = lobits;
896 /* Initialize the BAR register in config space */
897 bar = (addr & mask) | lobits;
898 pci_set_cfgdata32(pdi, PCIR_BAR(idx), bar);
900 if (type == PCIBAR_MEM64) {
901 assert(idx + 1 <= PCI_BARMAX);
902 pdi->pi_bar[idx + 1].type = PCIBAR_MEMHI64;
903 pci_set_cfgdata32(pdi, PCIR_BAR(idx + 1), bar >> 32);
906 if (type != PCIBAR_ROM) {
907 register_bar(pdi, idx);
914 pci_emul_alloc_rom(struct pci_devinst *const pdi, const uint64_t size,
917 /* allocate ROM space once on first call */
918 if (pci_emul_rombase == 0) {
919 pci_emul_rombase = vm_create_devmem(pdi->pi_vmctx, VM_PCIROM,
920 "pcirom", PCI_EMUL_ROMSIZE);
921 if (pci_emul_rombase == MAP_FAILED) {
922 warnx("%s: failed to create rom segment", __func__);
925 pci_emul_romlim = pci_emul_rombase + PCI_EMUL_ROMSIZE;
926 pci_emul_romoffset = 0;
929 /* ROM size should be a power of 2 and greater than 2 KB */
930 const uint64_t rom_size = MAX(1UL << flsl(size),
931 ~PCIM_BIOS_ADDR_MASK + 1);
933 /* check if ROM fits into ROM space */
934 if (pci_emul_romoffset + rom_size > PCI_EMUL_ROMSIZE) {
935 warnx("%s: no space left in rom segment:", __func__);
936 warnx("%16lu bytes left",
937 PCI_EMUL_ROMSIZE - pci_emul_romoffset);
938 warnx("%16lu bytes required by %d/%d/%d", rom_size, pdi->pi_bus,
939 pdi->pi_slot, pdi->pi_func);
943 /* allocate ROM BAR */
944 const int error = pci_emul_alloc_bar(pdi, PCI_ROM_IDX, PCIBAR_ROM,
950 *addr = pci_emul_rombase + pci_emul_romoffset;
952 /* save offset into ROM Space */
953 pdi->pi_romoffset = pci_emul_romoffset;
955 /* increase offset for next ROM */
956 pci_emul_romoffset += rom_size;
962 pci_emul_add_boot_device(struct pci_devinst *pi, int bootindex)
964 struct boot_device *new_device, *device;
966 /* don't permit a negative bootindex */
968 errx(4, "Invalid bootindex %d for %s", bootindex, pi->pi_name);
971 /* alloc new boot device */
972 new_device = calloc(1, sizeof(struct boot_device));
973 if (new_device == NULL) {
976 new_device->pdi = pi;
977 new_device->bootindex = bootindex;
979 /* search for boot device with higher boot index */
980 TAILQ_FOREACH(device, &boot_devices, boot_device_chain) {
981 if (device->bootindex == bootindex) {
983 "Could not set bootindex %d for %s. Bootindex already occupied by %s",
984 bootindex, pi->pi_name, device->pdi->pi_name);
985 } else if (device->bootindex > bootindex) {
990 /* add boot device to queue */
991 if (device == NULL) {
992 TAILQ_INSERT_TAIL(&boot_devices, new_device, boot_device_chain);
994 TAILQ_INSERT_BEFORE(device, new_device, boot_device_chain);
1000 #define CAP_START_OFFSET 0x40
1002 pci_emul_add_capability(struct pci_devinst *pi, u_char *capdata, int caplen)
1004 int i, capoff, reallen;
1009 reallen = roundup2(caplen, 4); /* dword aligned */
1011 sts = pci_get_cfgdata16(pi, PCIR_STATUS);
1012 if ((sts & PCIM_STATUS_CAPPRESENT) == 0)
1013 capoff = CAP_START_OFFSET;
1015 capoff = pi->pi_capend + 1;
1017 /* Check if we have enough space */
1018 if (capoff + reallen > PCI_REGMAX + 1)
1021 /* Set the previous capability pointer */
1022 if ((sts & PCIM_STATUS_CAPPRESENT) == 0) {
1023 pci_set_cfgdata8(pi, PCIR_CAP_PTR, capoff);
1024 pci_set_cfgdata16(pi, PCIR_STATUS, sts|PCIM_STATUS_CAPPRESENT);
1026 pci_set_cfgdata8(pi, pi->pi_prevcap + 1, capoff);
1028 /* Copy the capability */
1029 for (i = 0; i < caplen; i++)
1030 pci_set_cfgdata8(pi, capoff + i, capdata[i]);
1032 /* Set the next capability pointer */
1033 pci_set_cfgdata8(pi, capoff + 1, 0);
1035 pi->pi_prevcap = capoff;
1036 pi->pi_capend = capoff + reallen - 1;
1040 static struct pci_devemu *
1041 pci_emul_finddev(const char *name)
1043 struct pci_devemu **pdpp, *pdp;
1045 SET_FOREACH(pdpp, pci_devemu_set) {
1047 if (!strcmp(pdp->pe_emu, name)) {
1056 pci_emul_init(struct vmctx *ctx, struct pci_devemu *pde, int bus, int slot,
1057 int func, struct funcinfo *fi)
1059 struct pci_devinst *pdi;
1062 pdi = calloc(1, sizeof(struct pci_devinst));
1064 pdi->pi_vmctx = ctx;
1066 pdi->pi_slot = slot;
1067 pdi->pi_func = func;
1068 pthread_mutex_init(&pdi->pi_lintr.lock, NULL);
1069 pdi->pi_lintr.pin = 0;
1070 pdi->pi_lintr.state = IDLE;
1071 pdi->pi_lintr.pirq_pin = 0;
1072 pdi->pi_lintr.ioapic_irq = 0;
1074 snprintf(pdi->pi_name, PI_NAMESZ, "%s@pci.%d.%d.%d", pde->pe_emu, bus,
1077 /* Disable legacy interrupts */
1078 pci_set_cfgdata8(pdi, PCIR_INTLINE, 255);
1079 pci_set_cfgdata8(pdi, PCIR_INTPIN, 0);
1081 pci_set_cfgdata8(pdi, PCIR_COMMAND, PCIM_CMD_BUSMASTEREN);
1083 err = (*pde->pe_init)(pdi, fi->fi_config);
1093 pci_populate_msicap(struct msicap *msicap, int msgnum, int nextptr)
1097 /* Number of msi messages must be a power of 2 between 1 and 32 */
1098 assert((msgnum & (msgnum - 1)) == 0 && msgnum >= 1 && msgnum <= 32);
1099 mmc = ffs(msgnum) - 1;
1101 bzero(msicap, sizeof(struct msicap));
1102 msicap->capid = PCIY_MSI;
1103 msicap->nextptr = nextptr;
1104 msicap->msgctrl = PCIM_MSICTRL_64BIT | (mmc << 1);
1108 pci_emul_add_msicap(struct pci_devinst *pi, int msgnum)
1110 struct msicap msicap;
1112 pci_populate_msicap(&msicap, msgnum, 0);
1114 return (pci_emul_add_capability(pi, (u_char *)&msicap, sizeof(msicap)));
1118 pci_populate_msixcap(struct msixcap *msixcap, int msgnum, int barnum,
1119 uint32_t msix_tab_size)
1122 assert(msix_tab_size % 4096 == 0);
1124 bzero(msixcap, sizeof(struct msixcap));
1125 msixcap->capid = PCIY_MSIX;
1128 * Message Control Register, all fields set to
1129 * zero except for the Table Size.
1130 * Note: Table size N is encoded as N-1
1132 msixcap->msgctrl = msgnum - 1;
1136 * - MSI-X table start at offset 0
1137 * - PBA table starts at a 4K aligned offset after the MSI-X table
1139 msixcap->table_info = barnum & PCIM_MSIX_BIR_MASK;
1140 msixcap->pba_info = msix_tab_size | (barnum & PCIM_MSIX_BIR_MASK);
1144 pci_msix_table_init(struct pci_devinst *pi, int table_entries)
1148 assert(table_entries > 0);
1149 assert(table_entries <= MAX_MSIX_TABLE_ENTRIES);
1151 table_size = table_entries * MSIX_TABLE_ENTRY_SIZE;
1152 pi->pi_msix.table = calloc(1, table_size);
1154 /* set mask bit of vector control register */
1155 for (i = 0; i < table_entries; i++)
1156 pi->pi_msix.table[i].vector_control |= PCIM_MSIX_VCTRL_MASK;
1160 pci_emul_add_msixcap(struct pci_devinst *pi, int msgnum, int barnum)
1163 struct msixcap msixcap;
1165 assert(msgnum >= 1 && msgnum <= MAX_MSIX_TABLE_ENTRIES);
1166 assert(barnum >= 0 && barnum <= PCIR_MAX_BAR_0);
1168 tab_size = msgnum * MSIX_TABLE_ENTRY_SIZE;
1170 /* Align table size to nearest 4K */
1171 tab_size = roundup2(tab_size, 4096);
1173 pi->pi_msix.table_bar = barnum;
1174 pi->pi_msix.pba_bar = barnum;
1175 pi->pi_msix.table_offset = 0;
1176 pi->pi_msix.table_count = msgnum;
1177 pi->pi_msix.pba_offset = tab_size;
1178 pi->pi_msix.pba_size = PBA_SIZE(msgnum);
1180 pci_msix_table_init(pi, msgnum);
1182 pci_populate_msixcap(&msixcap, msgnum, barnum, tab_size);
1184 /* allocate memory for MSI-X Table and PBA */
1185 pci_emul_alloc_bar(pi, barnum, PCIBAR_MEM32,
1186 tab_size + pi->pi_msix.pba_size);
1188 return (pci_emul_add_capability(pi, (u_char *)&msixcap,
1193 msixcap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
1194 int bytes, uint32_t val)
1196 uint16_t msgctrl, rwmask;
1199 off = offset - capoff;
1200 /* Message Control Register */
1201 if (off == 2 && bytes == 2) {
1202 rwmask = PCIM_MSIXCTRL_MSIX_ENABLE | PCIM_MSIXCTRL_FUNCTION_MASK;
1203 msgctrl = pci_get_cfgdata16(pi, offset);
1205 msgctrl |= val & rwmask;
1208 pi->pi_msix.enabled = val & PCIM_MSIXCTRL_MSIX_ENABLE;
1209 pi->pi_msix.function_mask = val & PCIM_MSIXCTRL_FUNCTION_MASK;
1210 pci_lintr_update(pi);
1213 CFGWRITE(pi, offset, val, bytes);
1217 msicap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
1218 int bytes, uint32_t val)
1220 uint16_t msgctrl, rwmask, msgdata, mme;
1224 * If guest is writing to the message control register make sure
1225 * we do not overwrite read-only fields.
1227 if ((offset - capoff) == 2 && bytes == 2) {
1228 rwmask = PCIM_MSICTRL_MME_MASK | PCIM_MSICTRL_MSI_ENABLE;
1229 msgctrl = pci_get_cfgdata16(pi, offset);
1231 msgctrl |= val & rwmask;
1234 CFGWRITE(pi, offset, val, bytes);
1236 msgctrl = pci_get_cfgdata16(pi, capoff + 2);
1237 addrlo = pci_get_cfgdata32(pi, capoff + 4);
1238 if (msgctrl & PCIM_MSICTRL_64BIT)
1239 msgdata = pci_get_cfgdata16(pi, capoff + 12);
1241 msgdata = pci_get_cfgdata16(pi, capoff + 8);
1243 mme = msgctrl & PCIM_MSICTRL_MME_MASK;
1244 pi->pi_msi.enabled = msgctrl & PCIM_MSICTRL_MSI_ENABLE ? 1 : 0;
1245 if (pi->pi_msi.enabled) {
1246 pi->pi_msi.addr = addrlo;
1247 pi->pi_msi.msg_data = msgdata;
1248 pi->pi_msi.maxmsgnum = 1 << (mme >> 4);
1250 pi->pi_msi.maxmsgnum = 0;
1252 pci_lintr_update(pi);
1256 pciecap_cfgwrite(struct pci_devinst *pi, int capoff __unused, int offset,
1257 int bytes, uint32_t val)
1260 /* XXX don't write to the readonly parts */
1261 CFGWRITE(pi, offset, val, bytes);
1264 #define PCIECAP_VERSION 0x2
1266 pci_emul_add_pciecap(struct pci_devinst *pi, int type)
1269 struct pciecap pciecap;
1271 bzero(&pciecap, sizeof(pciecap));
1274 * Use the integrated endpoint type for endpoints on a root complex bus.
1276 * NB: bhyve currently only supports a single PCI bus that is the root
1277 * complex bus, so all endpoints are integrated.
1279 if ((type == PCIEM_TYPE_ENDPOINT) && (pi->pi_bus == 0))
1280 type = PCIEM_TYPE_ROOT_INT_EP;
1282 pciecap.capid = PCIY_EXPRESS;
1283 pciecap.pcie_capabilities = PCIECAP_VERSION | type;
1284 if (type != PCIEM_TYPE_ROOT_INT_EP) {
1285 pciecap.link_capabilities = 0x411; /* gen1, x1 */
1286 pciecap.link_status = 0x11; /* gen1, x1 */
1289 err = pci_emul_add_capability(pi, (u_char *)&pciecap, sizeof(pciecap));
1294 * This function assumes that 'coff' is in the capabilities region of the
1295 * config space. A capoff parameter of zero will force a search for the
1299 pci_emul_capwrite(struct pci_devinst *pi, int offset, int bytes, uint32_t val,
1300 uint8_t capoff, int capid)
1304 /* Do not allow un-aligned writes */
1305 if ((offset & (bytes - 1)) != 0)
1309 /* Find the capability that we want to update */
1310 capoff = CAP_START_OFFSET;
1312 nextoff = pci_get_cfgdata8(pi, capoff + 1);
1315 if (offset >= capoff && offset < nextoff)
1320 assert(offset >= capoff);
1321 capid = pci_get_cfgdata8(pi, capoff);
1325 * Capability ID and Next Capability Pointer are readonly.
1326 * However, some o/s's do 4-byte writes that include these.
1327 * For this case, trim the write back to 2 bytes and adjust
1330 if (offset == capoff || offset == capoff + 1) {
1331 if (offset == capoff && bytes == 4) {
1341 msicap_cfgwrite(pi, capoff, offset, bytes, val);
1344 msixcap_cfgwrite(pi, capoff, offset, bytes, val);
1347 pciecap_cfgwrite(pi, capoff, offset, bytes, val);
1355 pci_emul_iscap(struct pci_devinst *pi, int offset)
1359 sts = pci_get_cfgdata16(pi, PCIR_STATUS);
1360 if ((sts & PCIM_STATUS_CAPPRESENT) != 0) {
1361 if (offset >= CAP_START_OFFSET && offset <= pi->pi_capend)
1368 pci_emul_fallback_handler(struct vcpu *vcpu __unused, int dir,
1369 uint64_t addr __unused, int size __unused, uint64_t *val,
1370 void *arg1 __unused, long arg2 __unused)
1373 * Ignore writes; return 0xff's for reads. The mem read code
1374 * will take care of truncating to the correct size.
1376 if (dir == MEM_F_READ) {
1377 *val = 0xffffffffffffffff;
1384 pci_emul_ecfg_handler(struct vcpu *vcpu __unused, int dir, uint64_t addr,
1385 int bytes, uint64_t *val, void *arg1 __unused, long arg2 __unused)
1387 int bus, slot, func, coff, in;
1389 coff = addr & 0xfff;
1390 func = (addr >> 12) & 0x7;
1391 slot = (addr >> 15) & 0x1f;
1392 bus = (addr >> 20) & 0xff;
1393 in = (dir == MEM_F_READ);
1396 pci_cfgrw(in, bus, slot, func, coff, bytes, (uint32_t *)val);
1404 return (PCI_EMUL_ECFG_BASE);
1408 init_bootorder(void)
1410 struct boot_device *device;
1413 size_t bootorder_len;
1415 if (TAILQ_EMPTY(&boot_devices))
1418 fp = open_memstream(&bootorder, &bootorder_len);
1419 TAILQ_FOREACH(device, &boot_devices, boot_device_chain) {
1420 fprintf(fp, "/pci@i0cf8/pci@%d,%d\n",
1421 device->pdi->pi_slot, device->pdi->pi_func);
1425 return (qemu_fwcfg_add_file("bootorder", bootorder_len, bootorder));
1428 #define BUSIO_ROUNDUP 32
1429 #define BUSMEM32_ROUNDUP (1024 * 1024)
1430 #define BUSMEM64_ROUNDUP (512 * 1024 * 1024)
1433 init_pci(struct vmctx *ctx)
1435 char node_name[sizeof("pci.XXX.XX.X")];
1436 struct mem_range mr;
1437 struct pci_devemu *pde;
1439 struct slotinfo *si;
1440 struct funcinfo *fi;
1444 int bus, slot, func;
1447 if (vm_get_lowmem_limit(ctx) > PCI_EMUL_MEMBASE32)
1448 errx(EX_OSERR, "Invalid lowmem limit");
1450 pci_emul_iobase = PCI_EMUL_IOBASE;
1451 pci_emul_membase32 = PCI_EMUL_MEMBASE32;
1453 pci_emul_membase64 = 4*GB + vm_get_highmem_size(ctx);
1454 pci_emul_membase64 = roundup2(pci_emul_membase64, PCI_EMUL_MEMSIZE64);
1455 pci_emul_memlim64 = pci_emul_membase64 + PCI_EMUL_MEMSIZE64;
1457 TAILQ_INIT(&boot_devices);
1459 for (bus = 0; bus < MAXBUSES; bus++) {
1460 snprintf(node_name, sizeof(node_name), "pci.%d", bus);
1461 nvl = find_config_node(node_name);
1464 pci_businfo[bus] = calloc(1, sizeof(struct businfo));
1465 bi = pci_businfo[bus];
1468 * Keep track of the i/o and memory resources allocated to
1471 bi->iobase = pci_emul_iobase;
1472 bi->membase32 = pci_emul_membase32;
1473 bi->membase64 = pci_emul_membase64;
1475 /* first run: init devices */
1476 for (slot = 0; slot < MAXSLOTS; slot++) {
1477 si = &bi->slotinfo[slot];
1478 for (func = 0; func < MAXFUNCS; func++) {
1479 fi = &si->si_funcs[func];
1480 snprintf(node_name, sizeof(node_name),
1481 "pci.%d.%d.%d", bus, slot, func);
1482 nvl = find_config_node(node_name);
1486 fi->fi_config = nvl;
1487 emul = get_config_value_node(nvl, "device");
1489 EPRINTLN("pci slot %d:%d:%d: missing "
1490 "\"device\" value", bus, slot, func);
1493 pde = pci_emul_finddev(emul);
1495 EPRINTLN("pci slot %d:%d:%d: unknown "
1496 "device \"%s\"", bus, slot, func,
1500 if (pde->pe_alias != NULL) {
1501 EPRINTLN("pci slot %d:%d:%d: legacy "
1502 "device \"%s\", use \"%s\" instead",
1503 bus, slot, func, emul,
1508 error = pci_emul_init(ctx, pde, bus, slot,
1515 /* second run: assign BARs and free list */
1516 struct pci_bar_allocation *bar;
1517 struct pci_bar_allocation *bar_tmp;
1518 TAILQ_FOREACH_SAFE(bar, &pci_bars, chain, bar_tmp) {
1519 pci_emul_assign_bar(bar->pdi, bar->idx, bar->type,
1523 TAILQ_INIT(&pci_bars);
1526 * Add some slop to the I/O and memory resources decoded by
1527 * this bus to give a guest some flexibility if it wants to
1528 * reprogram the BARs.
1530 pci_emul_iobase += BUSIO_ROUNDUP;
1531 pci_emul_iobase = roundup2(pci_emul_iobase, BUSIO_ROUNDUP);
1532 bi->iolimit = pci_emul_iobase;
1534 pci_emul_membase32 += BUSMEM32_ROUNDUP;
1535 pci_emul_membase32 = roundup2(pci_emul_membase32,
1537 bi->memlimit32 = pci_emul_membase32;
1539 pci_emul_membase64 += BUSMEM64_ROUNDUP;
1540 pci_emul_membase64 = roundup2(pci_emul_membase64,
1542 bi->memlimit64 = pci_emul_membase64;
1546 * PCI backends are initialized before routing INTx interrupts
1547 * so that LPC devices are able to reserve ISA IRQs before
1548 * routing PIRQ pins.
1550 for (bus = 0; bus < MAXBUSES; bus++) {
1551 if ((bi = pci_businfo[bus]) == NULL)
1554 for (slot = 0; slot < MAXSLOTS; slot++) {
1555 si = &bi->slotinfo[slot];
1556 for (func = 0; func < MAXFUNCS; func++) {
1557 fi = &si->si_funcs[func];
1558 if (fi->fi_devi == NULL)
1560 pci_lintr_route(fi->fi_devi);
1566 if ((error = init_bootorder()) != 0) {
1567 warnx("%s: Unable to init bootorder", __func__);
1572 * The guest physical memory map looks like the following:
1573 * [0, lowmem) guest system memory
1574 * [lowmem, 0xC0000000) memory hole (may be absent)
1575 * [0xC0000000, 0xE0000000) PCI hole (32-bit BAR allocation)
1576 * [0xE0000000, 0xF0000000) PCI extended config window
1577 * [0xF0000000, 4GB) LAPIC, IOAPIC, HPET, firmware
1578 * [4GB, 4GB + highmem)
1582 * Accesses to memory addresses that are not allocated to system
1583 * memory or PCI devices return 0xff's.
1585 lowmem = vm_get_lowmem_size(ctx);
1586 bzero(&mr, sizeof(struct mem_range));
1587 mr.name = "PCI hole";
1588 mr.flags = MEM_F_RW | MEM_F_IMMUTABLE;
1590 mr.size = (4ULL * 1024 * 1024 * 1024) - lowmem;
1591 mr.handler = pci_emul_fallback_handler;
1592 error = register_mem_fallback(&mr);
1595 /* PCI extended config space */
1596 bzero(&mr, sizeof(struct mem_range));
1597 mr.name = "PCI ECFG";
1598 mr.flags = MEM_F_RW | MEM_F_IMMUTABLE;
1599 mr.base = PCI_EMUL_ECFG_BASE;
1600 mr.size = PCI_EMUL_ECFG_SIZE;
1601 mr.handler = pci_emul_ecfg_handler;
1602 error = register_mem(&mr);
1609 pci_apic_prt_entry(int bus __unused, int slot, int pin, int pirq_pin __unused,
1610 int ioapic_irq, void *arg __unused)
1613 dsdt_line(" Package ()");
1615 dsdt_line(" 0x%X,", slot << 16 | 0xffff);
1616 dsdt_line(" 0x%02X,", pin - 1);
1617 dsdt_line(" Zero,");
1618 dsdt_line(" 0x%X", ioapic_irq);
1623 pci_pirq_prt_entry(int bus __unused, int slot, int pin, int pirq_pin,
1624 int ioapic_irq __unused, void *arg __unused)
1628 name = lpc_pirq_name(pirq_pin);
1631 dsdt_line(" Package ()");
1633 dsdt_line(" 0x%X,", slot << 16 | 0xffff);
1634 dsdt_line(" 0x%02X,", pin - 1);
1635 dsdt_line(" %s,", name);
1642 * A bhyve virtual machine has a flat PCI hierarchy with a root port
1643 * corresponding to each PCI bus.
1646 pci_bus_write_dsdt(int bus)
1649 struct slotinfo *si;
1650 struct pci_devinst *pi;
1651 int count, func, slot;
1654 * If there are no devices on this 'bus' then just return.
1656 if ((bi = pci_businfo[bus]) == NULL) {
1658 * Bus 0 is special because it decodes the I/O ports used
1659 * for PCI config space access even if there are no devices
1666 dsdt_line(" Device (PC%02X)", bus);
1668 dsdt_line(" Name (_HID, EisaId (\"PNP0A03\"))");
1670 dsdt_line(" Method (_BBN, 0, NotSerialized)");
1672 dsdt_line(" Return (0x%08X)", bus);
1674 dsdt_line(" Name (_CRS, ResourceTemplate ()");
1676 dsdt_line(" WordBusNumber (ResourceProducer, MinFixed, "
1677 "MaxFixed, PosDecode,");
1678 dsdt_line(" 0x0000, // Granularity");
1679 dsdt_line(" 0x%04X, // Range Minimum", bus);
1680 dsdt_line(" 0x%04X, // Range Maximum", bus);
1681 dsdt_line(" 0x0000, // Translation Offset");
1682 dsdt_line(" 0x0001, // Length");
1687 dsdt_fixed_ioport(0xCF8, 8);
1690 dsdt_line(" WordIO (ResourceProducer, MinFixed, MaxFixed, "
1691 "PosDecode, EntireRange,");
1692 dsdt_line(" 0x0000, // Granularity");
1693 dsdt_line(" 0x0000, // Range Minimum");
1694 dsdt_line(" 0x0CF7, // Range Maximum");
1695 dsdt_line(" 0x0000, // Translation Offset");
1696 dsdt_line(" 0x0CF8, // Length");
1697 dsdt_line(" ,, , TypeStatic)");
1699 dsdt_line(" WordIO (ResourceProducer, MinFixed, MaxFixed, "
1700 "PosDecode, EntireRange,");
1701 dsdt_line(" 0x0000, // Granularity");
1702 dsdt_line(" 0x0D00, // Range Minimum");
1703 dsdt_line(" 0x%04X, // Range Maximum",
1704 PCI_EMUL_IOBASE - 1);
1705 dsdt_line(" 0x0000, // Translation Offset");
1706 dsdt_line(" 0x%04X, // Length",
1707 PCI_EMUL_IOBASE - 0x0D00);
1708 dsdt_line(" ,, , TypeStatic)");
1718 dsdt_line(" WordIO (ResourceProducer, MinFixed, MaxFixed, "
1719 "PosDecode, EntireRange,");
1720 dsdt_line(" 0x0000, // Granularity");
1721 dsdt_line(" 0x%04X, // Range Minimum", bi->iobase);
1722 dsdt_line(" 0x%04X, // Range Maximum",
1724 dsdt_line(" 0x0000, // Translation Offset");
1725 dsdt_line(" 0x%04X, // Length",
1726 bi->iolimit - bi->iobase);
1727 dsdt_line(" ,, , TypeStatic)");
1729 /* mmio window (32-bit) */
1730 dsdt_line(" DWordMemory (ResourceProducer, PosDecode, "
1731 "MinFixed, MaxFixed, NonCacheable, ReadWrite,");
1732 dsdt_line(" 0x00000000, // Granularity");
1733 dsdt_line(" 0x%08X, // Range Minimum\n", bi->membase32);
1734 dsdt_line(" 0x%08X, // Range Maximum\n",
1735 bi->memlimit32 - 1);
1736 dsdt_line(" 0x00000000, // Translation Offset");
1737 dsdt_line(" 0x%08X, // Length\n",
1738 bi->memlimit32 - bi->membase32);
1739 dsdt_line(" ,, , AddressRangeMemory, TypeStatic)");
1741 /* mmio window (64-bit) */
1742 dsdt_line(" QWordMemory (ResourceProducer, PosDecode, "
1743 "MinFixed, MaxFixed, NonCacheable, ReadWrite,");
1744 dsdt_line(" 0x0000000000000000, // Granularity");
1745 dsdt_line(" 0x%016lX, // Range Minimum\n", bi->membase64);
1746 dsdt_line(" 0x%016lX, // Range Maximum\n",
1747 bi->memlimit64 - 1);
1748 dsdt_line(" 0x0000000000000000, // Translation Offset");
1749 dsdt_line(" 0x%016lX, // Length\n",
1750 bi->memlimit64 - bi->membase64);
1751 dsdt_line(" ,, , AddressRangeMemory, TypeStatic)");
1754 count = pci_count_lintr(bus);
1757 dsdt_line("Name (PPRT, Package ()");
1759 pci_walk_lintr(bus, pci_pirq_prt_entry, NULL);
1761 dsdt_line("Name (APRT, Package ()");
1763 pci_walk_lintr(bus, pci_apic_prt_entry, NULL);
1765 dsdt_line("Method (_PRT, 0, NotSerialized)");
1767 dsdt_line(" If (PICM)");
1769 dsdt_line(" Return (APRT)");
1773 dsdt_line(" Return (PPRT)");
1780 for (slot = 0; slot < MAXSLOTS; slot++) {
1781 si = &bi->slotinfo[slot];
1782 for (func = 0; func < MAXFUNCS; func++) {
1783 pi = si->si_funcs[func].fi_devi;
1784 if (pi != NULL && pi->pi_d->pe_write_dsdt != NULL)
1785 pi->pi_d->pe_write_dsdt(pi);
1794 pci_write_dsdt(void)
1799 dsdt_line("Name (PICM, 0x00)");
1800 dsdt_line("Method (_PIC, 1, NotSerialized)");
1802 dsdt_line(" Store (Arg0, PICM)");
1805 dsdt_line("Scope (_SB)");
1807 for (bus = 0; bus < MAXBUSES; bus++)
1808 pci_bus_write_dsdt(bus);
1814 pci_bus_configured(int bus)
1816 assert(bus >= 0 && bus < MAXBUSES);
1817 return (pci_businfo[bus] != NULL);
1821 pci_msi_enabled(struct pci_devinst *pi)
1823 return (pi->pi_msi.enabled);
1827 pci_msi_maxmsgnum(struct pci_devinst *pi)
1829 if (pi->pi_msi.enabled)
1830 return (pi->pi_msi.maxmsgnum);
1836 pci_msix_enabled(struct pci_devinst *pi)
1839 return (pi->pi_msix.enabled && !pi->pi_msi.enabled);
1843 pci_generate_msix(struct pci_devinst *pi, int index)
1845 struct msix_table_entry *mte;
1847 if (!pci_msix_enabled(pi))
1850 if (pi->pi_msix.function_mask)
1853 if (index >= pi->pi_msix.table_count)
1856 mte = &pi->pi_msix.table[index];
1857 if ((mte->vector_control & PCIM_MSIX_VCTRL_MASK) == 0) {
1858 /* XXX Set PBA bit if interrupt is disabled */
1859 vm_lapic_msi(pi->pi_vmctx, mte->addr, mte->msg_data);
1864 pci_generate_msi(struct pci_devinst *pi, int index)
1867 if (pci_msi_enabled(pi) && index < pci_msi_maxmsgnum(pi)) {
1868 vm_lapic_msi(pi->pi_vmctx, pi->pi_msi.addr,
1869 pi->pi_msi.msg_data + index);
1874 pci_lintr_permitted(struct pci_devinst *pi)
1878 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND);
1879 return (!(pi->pi_msi.enabled || pi->pi_msix.enabled ||
1880 (cmd & PCIM_CMD_INTxDIS)));
1884 pci_lintr_request(struct pci_devinst *pi)
1887 struct slotinfo *si;
1888 int bestpin, bestcount, pin;
1890 bi = pci_businfo[pi->pi_bus];
1894 * Just allocate a pin from our slot. The pin will be
1895 * assigned IRQs later when interrupts are routed.
1897 si = &bi->slotinfo[pi->pi_slot];
1899 bestcount = si->si_intpins[0].ii_count;
1900 for (pin = 1; pin < 4; pin++) {
1901 if (si->si_intpins[pin].ii_count < bestcount) {
1903 bestcount = si->si_intpins[pin].ii_count;
1907 si->si_intpins[bestpin].ii_count++;
1908 pi->pi_lintr.pin = bestpin + 1;
1909 pci_set_cfgdata8(pi, PCIR_INTPIN, bestpin + 1);
1913 pci_lintr_route(struct pci_devinst *pi)
1916 struct intxinfo *ii;
1918 if (pi->pi_lintr.pin == 0)
1921 bi = pci_businfo[pi->pi_bus];
1923 ii = &bi->slotinfo[pi->pi_slot].si_intpins[pi->pi_lintr.pin - 1];
1926 * Attempt to allocate an I/O APIC pin for this intpin if one
1927 * is not yet assigned.
1929 if (ii->ii_ioapic_irq == 0)
1930 ii->ii_ioapic_irq = ioapic_pci_alloc_irq(pi);
1931 assert(ii->ii_ioapic_irq > 0);
1934 * Attempt to allocate a PIRQ pin for this intpin if one is
1937 if (ii->ii_pirq_pin == 0)
1938 ii->ii_pirq_pin = pirq_alloc_pin(pi);
1939 assert(ii->ii_pirq_pin > 0);
1941 pi->pi_lintr.ioapic_irq = ii->ii_ioapic_irq;
1942 pi->pi_lintr.pirq_pin = ii->ii_pirq_pin;
1943 pci_set_cfgdata8(pi, PCIR_INTLINE, pirq_irq(ii->ii_pirq_pin));
1947 pci_lintr_assert(struct pci_devinst *pi)
1950 assert(pi->pi_lintr.pin > 0);
1952 pthread_mutex_lock(&pi->pi_lintr.lock);
1953 if (pi->pi_lintr.state == IDLE) {
1954 if (pci_lintr_permitted(pi)) {
1955 pi->pi_lintr.state = ASSERTED;
1958 pi->pi_lintr.state = PENDING;
1960 pthread_mutex_unlock(&pi->pi_lintr.lock);
1964 pci_lintr_deassert(struct pci_devinst *pi)
1967 assert(pi->pi_lintr.pin > 0);
1969 pthread_mutex_lock(&pi->pi_lintr.lock);
1970 if (pi->pi_lintr.state == ASSERTED) {
1971 pi->pi_lintr.state = IDLE;
1972 pci_irq_deassert(pi);
1973 } else if (pi->pi_lintr.state == PENDING)
1974 pi->pi_lintr.state = IDLE;
1975 pthread_mutex_unlock(&pi->pi_lintr.lock);
1979 pci_lintr_update(struct pci_devinst *pi)
1982 pthread_mutex_lock(&pi->pi_lintr.lock);
1983 if (pi->pi_lintr.state == ASSERTED && !pci_lintr_permitted(pi)) {
1984 pci_irq_deassert(pi);
1985 pi->pi_lintr.state = PENDING;
1986 } else if (pi->pi_lintr.state == PENDING && pci_lintr_permitted(pi)) {
1987 pi->pi_lintr.state = ASSERTED;
1990 pthread_mutex_unlock(&pi->pi_lintr.lock);
1994 pci_count_lintr(int bus)
1996 int count, slot, pin;
1997 struct slotinfo *slotinfo;
2000 if (pci_businfo[bus] != NULL) {
2001 for (slot = 0; slot < MAXSLOTS; slot++) {
2002 slotinfo = &pci_businfo[bus]->slotinfo[slot];
2003 for (pin = 0; pin < 4; pin++) {
2004 if (slotinfo->si_intpins[pin].ii_count != 0)
2013 pci_walk_lintr(int bus, pci_lintr_cb cb, void *arg)
2016 struct slotinfo *si;
2017 struct intxinfo *ii;
2020 if ((bi = pci_businfo[bus]) == NULL)
2023 for (slot = 0; slot < MAXSLOTS; slot++) {
2024 si = &bi->slotinfo[slot];
2025 for (pin = 0; pin < 4; pin++) {
2026 ii = &si->si_intpins[pin];
2027 if (ii->ii_count != 0)
2028 cb(bus, slot, pin + 1, ii->ii_pirq_pin,
2029 ii->ii_ioapic_irq, arg);
2035 * Return 1 if the emulated device in 'slot' is a multi-function device.
2036 * Return 0 otherwise.
2039 pci_emul_is_mfdev(int bus, int slot)
2042 struct slotinfo *si;
2046 if ((bi = pci_businfo[bus]) != NULL) {
2047 si = &bi->slotinfo[slot];
2048 for (f = 0; f < MAXFUNCS; f++) {
2049 if (si->si_funcs[f].fi_devi != NULL) {
2054 return (numfuncs > 1);
2058 * Ensure that the PCIM_MFDEV bit is properly set (or unset) depending on
2059 * whether or not is a multi-function being emulated in the pci 'slot'.
2062 pci_emul_hdrtype_fixup(int bus, int slot, int off, int bytes, uint32_t *rv)
2066 if (off <= PCIR_HDRTYPE && off + bytes > PCIR_HDRTYPE) {
2067 mfdev = pci_emul_is_mfdev(bus, slot);
2077 *rv &= ~(PCIM_MFDEV << 16);
2079 *rv |= (PCIM_MFDEV << 16);
2087 * Update device state in response to changes to the PCI command
2091 pci_emul_cmd_changed(struct pci_devinst *pi, uint16_t old)
2094 uint16_t changed, new;
2096 new = pci_get_cfgdata16(pi, PCIR_COMMAND);
2097 changed = old ^ new;
2100 * If the MMIO or I/O address space decoding has changed then
2101 * register/unregister all BARs that decode that address space.
2103 for (i = 0; i <= PCI_BARMAX_WITH_ROM; i++) {
2104 switch (pi->pi_bar[i].type) {
2106 case PCIBAR_MEMHI64:
2109 /* I/O address space decoding changed? */
2110 if (changed & PCIM_CMD_PORTEN) {
2111 if (new & PCIM_CMD_PORTEN)
2112 register_bar(pi, i);
2114 unregister_bar(pi, i);
2118 /* skip (un-)register of ROM if it disabled */
2124 /* MMIO address space decoding changed? */
2125 if (changed & PCIM_CMD_MEMEN) {
2126 if (new & PCIM_CMD_MEMEN)
2127 register_bar(pi, i);
2129 unregister_bar(pi, i);
2138 * If INTx has been unmasked and is pending, assert the
2141 pci_lintr_update(pi);
2145 pci_emul_cmdsts_write(struct pci_devinst *pi, int coff, uint32_t new, int bytes)
2148 uint32_t cmd, old, readonly;
2150 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND); /* stash old value */
2153 * From PCI Local Bus Specification 3.0 sections 6.2.2 and 6.2.3.
2155 * XXX Bits 8, 11, 12, 13, 14 and 15 in the status register are
2156 * 'write 1 to clear'. However these bits are not set to '1' by
2157 * any device emulation so it is simpler to treat them as readonly.
2159 rshift = (coff & 0x3) * 8;
2160 readonly = 0xFFFFF880 >> rshift;
2162 old = CFGREAD(pi, coff, bytes);
2164 new |= (old & readonly);
2165 CFGWRITE(pi, coff, new, bytes); /* update config */
2167 pci_emul_cmd_changed(pi, cmd);
2171 pci_cfgrw(int in, int bus, int slot, int func, int coff, int bytes,
2175 struct slotinfo *si;
2176 struct pci_devinst *pi;
2177 struct pci_devemu *pe;
2179 uint64_t addr, bar, mask;
2181 if ((bi = pci_businfo[bus]) != NULL) {
2182 si = &bi->slotinfo[slot];
2183 pi = si->si_funcs[func].fi_devi;
2188 * Just return if there is no device at this slot:func or if the
2189 * the guest is doing an un-aligned access.
2191 if (pi == NULL || (bytes != 1 && bytes != 2 && bytes != 4) ||
2192 (coff & (bytes - 1)) != 0) {
2199 * Ignore all writes beyond the standard config space and return all
2202 if (coff >= PCI_REGMAX + 1) {
2206 * Extended capabilities begin at offset 256 in config
2207 * space. Absence of extended capabilities is signaled
2208 * with all 0s in the extended capability header at
2211 if (coff <= PCI_REGMAX + 4)
2223 /* Let the device emulation override the default handler */
2224 if (pe->pe_cfgread != NULL) {
2225 needcfg = pe->pe_cfgread(pi, coff, bytes, valp);
2231 *valp = CFGREAD(pi, coff, bytes);
2233 pci_emul_hdrtype_fixup(bus, slot, coff, bytes, valp);
2235 /* Let the device emulation override the default handler */
2236 if (pe->pe_cfgwrite != NULL &&
2237 (*pe->pe_cfgwrite)(pi, coff, bytes, *valp) == 0)
2241 * Special handling for write to BAR and ROM registers
2243 if (is_pcir_bar(coff) || is_pcir_bios(coff)) {
2245 * Ignore writes to BAR registers that are not
2248 if (bytes != 4 || (coff & 0x3) != 0)
2251 if (is_pcir_bar(coff)) {
2252 idx = (coff - PCIR_BAR(0)) / 4;
2253 } else if (is_pcir_bios(coff)) {
2256 errx(4, "%s: invalid BAR offset %d", __func__,
2260 mask = ~(pi->pi_bar[idx].size - 1);
2261 switch (pi->pi_bar[idx].type) {
2263 pi->pi_bar[idx].addr = bar = 0;
2266 addr = *valp & mask;
2268 bar = addr | pi->pi_bar[idx].lobits;
2270 * Register the new BAR value for interception
2272 if (addr != pi->pi_bar[idx].addr) {
2273 update_bar_address(pi, addr, idx,
2278 addr = bar = *valp & mask;
2279 bar |= pi->pi_bar[idx].lobits;
2280 if (addr != pi->pi_bar[idx].addr) {
2281 update_bar_address(pi, addr, idx,
2286 addr = bar = *valp & mask;
2287 bar |= pi->pi_bar[idx].lobits;
2288 if (addr != (uint32_t)pi->pi_bar[idx].addr) {
2289 update_bar_address(pi, addr, idx,
2293 case PCIBAR_MEMHI64:
2294 mask = ~(pi->pi_bar[idx - 1].size - 1);
2295 addr = ((uint64_t)*valp << 32) & mask;
2297 if (bar != pi->pi_bar[idx - 1].addr >> 32) {
2298 update_bar_address(pi, addr, idx - 1,
2303 addr = bar = *valp & mask;
2304 if (memen(pi) && romen(pi)) {
2305 unregister_bar(pi, idx);
2307 pi->pi_bar[idx].addr = addr;
2308 pi->pi_bar[idx].lobits = *valp &
2310 /* romen could have changed it value */
2311 if (memen(pi) && romen(pi)) {
2312 register_bar(pi, idx);
2314 bar |= pi->pi_bar[idx].lobits;
2319 pci_set_cfgdata32(pi, coff, bar);
2321 } else if (pci_emul_iscap(pi, coff)) {
2322 pci_emul_capwrite(pi, coff, bytes, *valp, 0, 0);
2323 } else if (coff >= PCIR_COMMAND && coff < PCIR_REVID) {
2324 pci_emul_cmdsts_write(pi, coff, *valp, bytes);
2326 CFGWRITE(pi, coff, *valp, bytes);
2331 static int cfgenable, cfgbus, cfgslot, cfgfunc, cfgoff;
2334 pci_emul_cfgaddr(struct vmctx *ctx __unused, int in,
2335 int port __unused, int bytes, uint32_t *eax, void *arg __unused)
2341 *eax = (bytes == 2) ? 0xffff : 0xff;
2346 x = (cfgbus << 16) | (cfgslot << 11) | (cfgfunc << 8) | cfgoff;
2352 cfgenable = (x & CONF1_ENABLE) == CONF1_ENABLE;
2353 cfgoff = (x & PCI_REGMAX) & ~0x03;
2354 cfgfunc = (x >> 8) & PCI_FUNCMAX;
2355 cfgslot = (x >> 11) & PCI_SLOTMAX;
2356 cfgbus = (x >> 16) & PCI_BUSMAX;
2361 INOUT_PORT(pci_cfgaddr, CONF1_ADDR_PORT, IOPORT_F_INOUT, pci_emul_cfgaddr);
2364 pci_emul_cfgdata(struct vmctx *ctx __unused, int in, int port,
2365 int bytes, uint32_t *eax, void *arg __unused)
2369 assert(bytes == 1 || bytes == 2 || bytes == 4);
2371 coff = cfgoff + (port - CONF1_DATA_PORT);
2373 pci_cfgrw(in, cfgbus, cfgslot, cfgfunc, coff, bytes, eax);
2375 /* Ignore accesses to cfgdata if not enabled by cfgaddr */
2382 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+0, IOPORT_F_INOUT, pci_emul_cfgdata);
2383 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+1, IOPORT_F_INOUT, pci_emul_cfgdata);
2384 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+2, IOPORT_F_INOUT, pci_emul_cfgdata);
2385 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+3, IOPORT_F_INOUT, pci_emul_cfgdata);
2387 #ifdef BHYVE_SNAPSHOT
2389 * Saves/restores PCI device emulated state. Returns 0 on success.
2392 pci_snapshot_pci_dev(struct vm_snapshot_meta *meta)
2394 struct pci_devinst *pi;
2398 pi = meta->dev_data;
2400 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msi.enabled, meta, ret, done);
2401 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msi.addr, meta, ret, done);
2402 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msi.msg_data, meta, ret, done);
2403 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msi.maxmsgnum, meta, ret, done);
2405 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.enabled, meta, ret, done);
2406 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.table_bar, meta, ret, done);
2407 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.pba_bar, meta, ret, done);
2408 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.table_offset, meta, ret, done);
2409 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.table_count, meta, ret, done);
2410 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.pba_offset, meta, ret, done);
2411 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.pba_size, meta, ret, done);
2412 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.function_mask, meta, ret, done);
2414 SNAPSHOT_BUF_OR_LEAVE(pi->pi_cfgdata, sizeof(pi->pi_cfgdata),
2417 for (i = 0; i < (int)nitems(pi->pi_bar); i++) {
2418 SNAPSHOT_VAR_OR_LEAVE(pi->pi_bar[i].type, meta, ret, done);
2419 SNAPSHOT_VAR_OR_LEAVE(pi->pi_bar[i].size, meta, ret, done);
2420 SNAPSHOT_VAR_OR_LEAVE(pi->pi_bar[i].addr, meta, ret, done);
2423 /* Restore MSI-X table. */
2424 for (i = 0; i < pi->pi_msix.table_count; i++) {
2425 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.table[i].addr,
2427 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.table[i].msg_data,
2429 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.table[i].vector_control,
2438 pci_snapshot(struct vm_snapshot_meta *meta)
2440 struct pci_devemu *pde;
2441 struct pci_devinst *pdi;
2444 assert(meta->dev_name != NULL);
2446 pdi = meta->dev_data;
2449 if (pde->pe_snapshot == NULL)
2452 ret = pci_snapshot_pci_dev(meta);
2454 ret = (*pde->pe_snapshot)(meta);
2460 pci_pause(struct pci_devinst *pdi)
2462 struct pci_devemu *pde = pdi->pi_d;
2464 if (pde->pe_pause == NULL) {
2465 /* The pause/resume functionality is optional. */
2469 return (*pde->pe_pause)(pdi);
2473 pci_resume(struct pci_devinst *pdi)
2475 struct pci_devemu *pde = pdi->pi_d;
2477 if (pde->pe_resume == NULL) {
2478 /* The pause/resume functionality is optional. */
2482 return (*pde->pe_resume)(pdi);
2486 #define PCI_EMUL_TEST
2487 #ifdef PCI_EMUL_TEST
2489 * Define a dummy test device
2493 struct pci_emul_dsoftc {
2494 uint8_t ioregs[DIOSZ];
2495 uint8_t memregs[2][DMEMSZ];
2498 #define PCI_EMUL_MSI_MSGS 4
2499 #define PCI_EMUL_MSIX_MSGS 16
2502 pci_emul_dinit(struct pci_devinst *pi, nvlist_t *nvl __unused)
2505 struct pci_emul_dsoftc *sc;
2507 sc = calloc(1, sizeof(struct pci_emul_dsoftc));
2511 pci_set_cfgdata16(pi, PCIR_DEVICE, 0x0001);
2512 pci_set_cfgdata16(pi, PCIR_VENDOR, 0x10DD);
2513 pci_set_cfgdata8(pi, PCIR_CLASS, 0x02);
2515 error = pci_emul_add_msicap(pi, PCI_EMUL_MSI_MSGS);
2518 error = pci_emul_alloc_bar(pi, 0, PCIBAR_IO, DIOSZ);
2521 error = pci_emul_alloc_bar(pi, 1, PCIBAR_MEM32, DMEMSZ);
2524 error = pci_emul_alloc_bar(pi, 2, PCIBAR_MEM32, DMEMSZ);
2531 pci_emul_diow(struct pci_devinst *pi, int baridx, uint64_t offset, int size,
2535 struct pci_emul_dsoftc *sc = pi->pi_arg;
2538 if (offset + size > DIOSZ) {
2539 printf("diow: iow too large, offset %ld size %d\n",
2545 sc->ioregs[offset] = value & 0xff;
2546 } else if (size == 2) {
2547 *(uint16_t *)&sc->ioregs[offset] = value & 0xffff;
2548 } else if (size == 4) {
2549 *(uint32_t *)&sc->ioregs[offset] = value;
2551 printf("diow: iow unknown size %d\n", size);
2555 * Special magic value to generate an interrupt
2557 if (offset == 4 && size == 4 && pci_msi_enabled(pi))
2558 pci_generate_msi(pi, value % pci_msi_maxmsgnum(pi));
2560 if (value == 0xabcdef) {
2561 for (i = 0; i < pci_msi_maxmsgnum(pi); i++)
2562 pci_generate_msi(pi, i);
2566 if (baridx == 1 || baridx == 2) {
2567 if (offset + size > DMEMSZ) {
2568 printf("diow: memw too large, offset %ld size %d\n",
2573 i = baridx - 1; /* 'memregs' index */
2576 sc->memregs[i][offset] = value;
2577 } else if (size == 2) {
2578 *(uint16_t *)&sc->memregs[i][offset] = value;
2579 } else if (size == 4) {
2580 *(uint32_t *)&sc->memregs[i][offset] = value;
2581 } else if (size == 8) {
2582 *(uint64_t *)&sc->memregs[i][offset] = value;
2584 printf("diow: memw unknown size %d\n", size);
2588 * magic interrupt ??
2592 if (baridx > 2 || baridx < 0) {
2593 printf("diow: unknown bar idx %d\n", baridx);
2598 pci_emul_dior(struct pci_devinst *pi, int baridx, uint64_t offset, int size)
2600 struct pci_emul_dsoftc *sc = pi->pi_arg;
2605 if (offset + size > DIOSZ) {
2606 printf("dior: ior too large, offset %ld size %d\n",
2613 value = sc->ioregs[offset];
2614 } else if (size == 2) {
2615 value = *(uint16_t *) &sc->ioregs[offset];
2616 } else if (size == 4) {
2617 value = *(uint32_t *) &sc->ioregs[offset];
2619 printf("dior: ior unknown size %d\n", size);
2623 if (baridx == 1 || baridx == 2) {
2624 if (offset + size > DMEMSZ) {
2625 printf("dior: memr too large, offset %ld size %d\n",
2630 i = baridx - 1; /* 'memregs' index */
2633 value = sc->memregs[i][offset];
2634 } else if (size == 2) {
2635 value = *(uint16_t *) &sc->memregs[i][offset];
2636 } else if (size == 4) {
2637 value = *(uint32_t *) &sc->memregs[i][offset];
2638 } else if (size == 8) {
2639 value = *(uint64_t *) &sc->memregs[i][offset];
2641 printf("dior: ior unknown size %d\n", size);
2646 if (baridx > 2 || baridx < 0) {
2647 printf("dior: unknown bar idx %d\n", baridx);
2654 #ifdef BHYVE_SNAPSHOT
2655 struct pci_devinst *
2656 pci_next(const struct pci_devinst *cursor)
2658 unsigned bus = 0, slot = 0, func = 0;
2660 struct slotinfo *si;
2661 struct funcinfo *fi;
2663 bus = cursor ? cursor->pi_bus : 0;
2664 slot = cursor ? cursor->pi_slot : 0;
2665 func = cursor ? (cursor->pi_func + 1) : 0;
2667 for (; bus < MAXBUSES; bus++) {
2668 if ((bi = pci_businfo[bus]) == NULL)
2671 if (slot >= MAXSLOTS)
2674 for (; slot < MAXSLOTS; slot++) {
2675 si = &bi->slotinfo[slot];
2676 if (func >= MAXFUNCS)
2678 for (; func < MAXFUNCS; func++) {
2679 fi = &si->si_funcs[func];
2680 if (fi->fi_devi == NULL)
2683 return (fi->fi_devi);
2692 pci_emul_snapshot(struct vm_snapshot_meta *meta __unused)
2698 static const struct pci_devemu pci_dummy = {
2700 .pe_init = pci_emul_dinit,
2701 .pe_barwrite = pci_emul_diow,
2702 .pe_barread = pci_emul_dior,
2703 #ifdef BHYVE_SNAPSHOT
2704 .pe_snapshot = pci_emul_snapshot,
2707 PCI_EMUL_SET(pci_dummy);
2709 #endif /* PCI_EMUL_TEST */