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);
618 if (pe->pe_baraddr != NULL)
619 (*pe->pe_baraddr)(pi, idx, registration,
620 pi->pi_bar[idx].addr);
624 bzero(&mr, sizeof(struct mem_range));
625 mr.name = pi->pi_name;
626 mr.base = pi->pi_bar[idx].addr;
627 mr.size = pi->pi_bar[idx].size;
630 mr.handler = pci_emul_mem_handler;
633 error = register_mem(&mr);
635 error = unregister_mem(&mr);
636 if (pe->pe_baraddr != NULL)
637 (*pe->pe_baraddr)(pi, idx, registration,
638 pi->pi_bar[idx].addr);
642 if (pe->pe_baraddr != NULL)
643 (*pe->pe_baraddr)(pi, idx, registration,
644 pi->pi_bar[idx].addr);
654 unregister_bar(struct pci_devinst *pi, int idx)
657 modify_bar_registration(pi, idx, 0);
661 register_bar(struct pci_devinst *pi, int idx)
664 modify_bar_registration(pi, idx, 1);
667 /* Is the ROM enabled for the emulated pci device? */
669 romen(struct pci_devinst *pi)
671 return (pi->pi_bar[PCI_ROM_IDX].lobits & PCIM_BIOS_ENABLE) ==
675 /* Are we decoding i/o port accesses for the emulated pci device? */
677 porten(struct pci_devinst *pi)
681 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND);
683 return (cmd & PCIM_CMD_PORTEN);
686 /* Are we decoding memory accesses for the emulated pci device? */
688 memen(struct pci_devinst *pi)
692 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND);
694 return (cmd & PCIM_CMD_MEMEN);
698 * Update the MMIO or I/O address that is decoded by the BAR register.
700 * If the pci device has enabled the address space decoding then intercept
701 * the address range decoded by the BAR register.
704 update_bar_address(struct pci_devinst *pi, uint64_t addr, int idx, int type)
708 if (pi->pi_bar[idx].type == PCIBAR_IO)
714 unregister_bar(pi, idx);
719 pi->pi_bar[idx].addr = addr;
722 pi->pi_bar[idx].addr &= ~0xffffffffUL;
723 pi->pi_bar[idx].addr |= addr;
726 pi->pi_bar[idx].addr &= 0xffffffff;
727 pi->pi_bar[idx].addr |= addr;
734 register_bar(pi, idx);
738 pci_emul_alloc_bar(struct pci_devinst *pdi, int idx, enum pcibar_type type,
741 assert((type == PCIBAR_ROM) || (idx >= 0 && idx <= PCI_BARMAX));
742 assert((type != PCIBAR_ROM) || (idx == PCI_ROM_IDX));
744 if ((size & (size - 1)) != 0)
745 size = 1UL << flsl(size); /* round up to a power of 2 */
747 /* Enforce minimum BAR sizes required by the PCI standard */
748 if (type == PCIBAR_IO) {
751 } else if (type == PCIBAR_ROM) {
752 if (size < ~PCIM_BIOS_ADDR_MASK + 1)
753 size = ~PCIM_BIOS_ADDR_MASK + 1;
760 * To reduce fragmentation of the MMIO space, we allocate the BARs by
761 * size. Therefore, don't allocate the BAR yet. We create a list of all
762 * BAR allocation which is sorted by BAR size. When all PCI devices are
763 * initialized, we will assign an address to the BARs.
766 /* create a new list entry */
767 struct pci_bar_allocation *const new_bar = malloc(sizeof(*new_bar));
768 memset(new_bar, 0, sizeof(*new_bar));
771 new_bar->type = type;
772 new_bar->size = size;
775 * Search for a BAR which size is lower than the size of our newly
778 struct pci_bar_allocation *bar = NULL;
779 TAILQ_FOREACH(bar, &pci_bars, chain) {
780 if (bar->size < size) {
787 * Either the list is empty or new BAR is the smallest BAR of
788 * the list. Append it to the end of our list.
790 TAILQ_INSERT_TAIL(&pci_bars, new_bar, chain);
793 * The found BAR is smaller than our new BAR. For that reason,
794 * insert our new BAR before the found BAR.
796 TAILQ_INSERT_BEFORE(bar, new_bar, chain);
800 * pci_passthru devices synchronize their physical and virtual command
801 * register on init. For that reason, the virtual cmd reg should be
802 * updated as early as possible.
807 enbit = PCIM_CMD_PORTEN;
811 enbit = PCIM_CMD_MEMEN;
818 const uint16_t cmd = pci_get_cfgdata16(pdi, PCIR_COMMAND);
819 pci_set_cfgdata16(pdi, PCIR_COMMAND, cmd | enbit);
825 pci_emul_assign_bar(struct pci_devinst *const pdi, const int idx,
826 const enum pcibar_type type, const uint64_t size)
829 uint64_t *baseptr, limit, addr, mask, lobits, bar;
834 addr = mask = lobits = 0;
837 baseptr = &pci_emul_iobase;
838 limit = PCI_EMUL_IOLIMIT;
839 mask = PCIM_BAR_IO_BASE;
840 lobits = PCIM_BAR_IO_SPACE;
845 * Some drivers do not work well if the 64-bit BAR is allocated
846 * above 4GB. Allow for this by allocating small requests under
847 * 4GB unless then allocation size is larger than some arbitrary
848 * number (128MB currently).
850 if (size > 128 * 1024 * 1024) {
851 baseptr = &pci_emul_membase64;
852 limit = pci_emul_memlim64;
853 mask = PCIM_BAR_MEM_BASE;
854 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64 |
855 PCIM_BAR_MEM_PREFETCH;
857 baseptr = &pci_emul_membase32;
858 limit = PCI_EMUL_MEMLIMIT32;
859 mask = PCIM_BAR_MEM_BASE;
860 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64;
864 baseptr = &pci_emul_membase32;
865 limit = PCI_EMUL_MEMLIMIT32;
866 mask = PCIM_BAR_MEM_BASE;
867 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_32;
870 /* do not claim memory for ROM. OVMF will do it for us. */
873 mask = PCIM_BIOS_ADDR_MASK;
877 printf("pci_emul_alloc_base: invalid bar type %d\n", type);
881 if (baseptr != NULL) {
882 error = pci_emul_alloc_resource(baseptr, limit, size, &addr);
889 pdi->pi_bar[idx].type = type;
890 pdi->pi_bar[idx].addr = addr;
891 pdi->pi_bar[idx].size = size;
893 * passthru devices are using same lobits as physical device they set
896 if (pdi->pi_bar[idx].lobits != 0) {
897 lobits = pdi->pi_bar[idx].lobits;
899 pdi->pi_bar[idx].lobits = lobits;
902 /* Initialize the BAR register in config space */
903 bar = (addr & mask) | lobits;
904 pci_set_cfgdata32(pdi, PCIR_BAR(idx), bar);
906 if (type == PCIBAR_MEM64) {
907 assert(idx + 1 <= PCI_BARMAX);
908 pdi->pi_bar[idx + 1].type = PCIBAR_MEMHI64;
909 pci_set_cfgdata32(pdi, PCIR_BAR(idx + 1), bar >> 32);
912 if (type != PCIBAR_ROM) {
913 register_bar(pdi, idx);
920 pci_emul_alloc_rom(struct pci_devinst *const pdi, const uint64_t size,
923 /* allocate ROM space once on first call */
924 if (pci_emul_rombase == 0) {
925 pci_emul_rombase = vm_create_devmem(pdi->pi_vmctx, VM_PCIROM,
926 "pcirom", PCI_EMUL_ROMSIZE);
927 if (pci_emul_rombase == MAP_FAILED) {
928 warnx("%s: failed to create rom segment", __func__);
931 pci_emul_romlim = pci_emul_rombase + PCI_EMUL_ROMSIZE;
932 pci_emul_romoffset = 0;
935 /* ROM size should be a power of 2 and greater than 2 KB */
936 const uint64_t rom_size = MAX(1UL << flsl(size),
937 ~PCIM_BIOS_ADDR_MASK + 1);
939 /* check if ROM fits into ROM space */
940 if (pci_emul_romoffset + rom_size > PCI_EMUL_ROMSIZE) {
941 warnx("%s: no space left in rom segment:", __func__);
942 warnx("%16lu bytes left",
943 PCI_EMUL_ROMSIZE - pci_emul_romoffset);
944 warnx("%16lu bytes required by %d/%d/%d", rom_size, pdi->pi_bus,
945 pdi->pi_slot, pdi->pi_func);
949 /* allocate ROM BAR */
950 const int error = pci_emul_alloc_bar(pdi, PCI_ROM_IDX, PCIBAR_ROM,
956 *addr = pci_emul_rombase + pci_emul_romoffset;
958 /* save offset into ROM Space */
959 pdi->pi_romoffset = pci_emul_romoffset;
961 /* increase offset for next ROM */
962 pci_emul_romoffset += rom_size;
968 pci_emul_add_boot_device(struct pci_devinst *pi, int bootindex)
970 struct boot_device *new_device, *device;
972 /* don't permit a negative bootindex */
974 errx(4, "Invalid bootindex %d for %s", bootindex, pi->pi_name);
977 /* alloc new boot device */
978 new_device = calloc(1, sizeof(struct boot_device));
979 if (new_device == NULL) {
982 new_device->pdi = pi;
983 new_device->bootindex = bootindex;
985 /* search for boot device with higher boot index */
986 TAILQ_FOREACH(device, &boot_devices, boot_device_chain) {
987 if (device->bootindex == bootindex) {
989 "Could not set bootindex %d for %s. Bootindex already occupied by %s",
990 bootindex, pi->pi_name, device->pdi->pi_name);
991 } else if (device->bootindex > bootindex) {
996 /* add boot device to queue */
997 if (device == NULL) {
998 TAILQ_INSERT_TAIL(&boot_devices, new_device, boot_device_chain);
1000 TAILQ_INSERT_BEFORE(device, new_device, boot_device_chain);
1006 #define CAP_START_OFFSET 0x40
1008 pci_emul_add_capability(struct pci_devinst *pi, u_char *capdata, int caplen)
1010 int i, capoff, reallen;
1015 reallen = roundup2(caplen, 4); /* dword aligned */
1017 sts = pci_get_cfgdata16(pi, PCIR_STATUS);
1018 if ((sts & PCIM_STATUS_CAPPRESENT) == 0)
1019 capoff = CAP_START_OFFSET;
1021 capoff = pi->pi_capend + 1;
1023 /* Check if we have enough space */
1024 if (capoff + reallen > PCI_REGMAX + 1)
1027 /* Set the previous capability pointer */
1028 if ((sts & PCIM_STATUS_CAPPRESENT) == 0) {
1029 pci_set_cfgdata8(pi, PCIR_CAP_PTR, capoff);
1030 pci_set_cfgdata16(pi, PCIR_STATUS, sts|PCIM_STATUS_CAPPRESENT);
1032 pci_set_cfgdata8(pi, pi->pi_prevcap + 1, capoff);
1034 /* Copy the capability */
1035 for (i = 0; i < caplen; i++)
1036 pci_set_cfgdata8(pi, capoff + i, capdata[i]);
1038 /* Set the next capability pointer */
1039 pci_set_cfgdata8(pi, capoff + 1, 0);
1041 pi->pi_prevcap = capoff;
1042 pi->pi_capend = capoff + reallen - 1;
1046 static struct pci_devemu *
1047 pci_emul_finddev(const char *name)
1049 struct pci_devemu **pdpp, *pdp;
1051 SET_FOREACH(pdpp, pci_devemu_set) {
1053 if (!strcmp(pdp->pe_emu, name)) {
1062 pci_emul_init(struct vmctx *ctx, struct pci_devemu *pde, int bus, int slot,
1063 int func, struct funcinfo *fi)
1065 struct pci_devinst *pdi;
1068 pdi = calloc(1, sizeof(struct pci_devinst));
1070 pdi->pi_vmctx = ctx;
1072 pdi->pi_slot = slot;
1073 pdi->pi_func = func;
1074 pthread_mutex_init(&pdi->pi_lintr.lock, NULL);
1075 pdi->pi_lintr.pin = 0;
1076 pdi->pi_lintr.state = IDLE;
1077 pdi->pi_lintr.pirq_pin = 0;
1078 pdi->pi_lintr.ioapic_irq = 0;
1080 snprintf(pdi->pi_name, PI_NAMESZ, "%s@pci.%d.%d.%d", pde->pe_emu, bus,
1083 /* Disable legacy interrupts */
1084 pci_set_cfgdata8(pdi, PCIR_INTLINE, 255);
1085 pci_set_cfgdata8(pdi, PCIR_INTPIN, 0);
1087 pci_set_cfgdata8(pdi, PCIR_COMMAND, PCIM_CMD_BUSMASTEREN);
1089 err = (*pde->pe_init)(pdi, fi->fi_config);
1099 pci_populate_msicap(struct msicap *msicap, int msgnum, int nextptr)
1103 /* Number of msi messages must be a power of 2 between 1 and 32 */
1104 assert((msgnum & (msgnum - 1)) == 0 && msgnum >= 1 && msgnum <= 32);
1105 mmc = ffs(msgnum) - 1;
1107 bzero(msicap, sizeof(struct msicap));
1108 msicap->capid = PCIY_MSI;
1109 msicap->nextptr = nextptr;
1110 msicap->msgctrl = PCIM_MSICTRL_64BIT | (mmc << 1);
1114 pci_emul_add_msicap(struct pci_devinst *pi, int msgnum)
1116 struct msicap msicap;
1118 pci_populate_msicap(&msicap, msgnum, 0);
1120 return (pci_emul_add_capability(pi, (u_char *)&msicap, sizeof(msicap)));
1124 pci_populate_msixcap(struct msixcap *msixcap, int msgnum, int barnum,
1125 uint32_t msix_tab_size)
1128 assert(msix_tab_size % 4096 == 0);
1130 bzero(msixcap, sizeof(struct msixcap));
1131 msixcap->capid = PCIY_MSIX;
1134 * Message Control Register, all fields set to
1135 * zero except for the Table Size.
1136 * Note: Table size N is encoded as N-1
1138 msixcap->msgctrl = msgnum - 1;
1142 * - MSI-X table start at offset 0
1143 * - PBA table starts at a 4K aligned offset after the MSI-X table
1145 msixcap->table_info = barnum & PCIM_MSIX_BIR_MASK;
1146 msixcap->pba_info = msix_tab_size | (barnum & PCIM_MSIX_BIR_MASK);
1150 pci_msix_table_init(struct pci_devinst *pi, int table_entries)
1154 assert(table_entries > 0);
1155 assert(table_entries <= MAX_MSIX_TABLE_ENTRIES);
1157 table_size = table_entries * MSIX_TABLE_ENTRY_SIZE;
1158 pi->pi_msix.table = calloc(1, table_size);
1160 /* set mask bit of vector control register */
1161 for (i = 0; i < table_entries; i++)
1162 pi->pi_msix.table[i].vector_control |= PCIM_MSIX_VCTRL_MASK;
1166 pci_emul_add_msixcap(struct pci_devinst *pi, int msgnum, int barnum)
1169 struct msixcap msixcap;
1171 assert(msgnum >= 1 && msgnum <= MAX_MSIX_TABLE_ENTRIES);
1172 assert(barnum >= 0 && barnum <= PCIR_MAX_BAR_0);
1174 tab_size = msgnum * MSIX_TABLE_ENTRY_SIZE;
1176 /* Align table size to nearest 4K */
1177 tab_size = roundup2(tab_size, 4096);
1179 pi->pi_msix.table_bar = barnum;
1180 pi->pi_msix.pba_bar = barnum;
1181 pi->pi_msix.table_offset = 0;
1182 pi->pi_msix.table_count = msgnum;
1183 pi->pi_msix.pba_offset = tab_size;
1184 pi->pi_msix.pba_size = PBA_SIZE(msgnum);
1186 pci_msix_table_init(pi, msgnum);
1188 pci_populate_msixcap(&msixcap, msgnum, barnum, tab_size);
1190 /* allocate memory for MSI-X Table and PBA */
1191 pci_emul_alloc_bar(pi, barnum, PCIBAR_MEM32,
1192 tab_size + pi->pi_msix.pba_size);
1194 return (pci_emul_add_capability(pi, (u_char *)&msixcap,
1199 msixcap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
1200 int bytes, uint32_t val)
1202 uint16_t msgctrl, rwmask;
1205 off = offset - capoff;
1206 /* Message Control Register */
1207 if (off == 2 && bytes == 2) {
1208 rwmask = PCIM_MSIXCTRL_MSIX_ENABLE | PCIM_MSIXCTRL_FUNCTION_MASK;
1209 msgctrl = pci_get_cfgdata16(pi, offset);
1211 msgctrl |= val & rwmask;
1214 pi->pi_msix.enabled = val & PCIM_MSIXCTRL_MSIX_ENABLE;
1215 pi->pi_msix.function_mask = val & PCIM_MSIXCTRL_FUNCTION_MASK;
1216 pci_lintr_update(pi);
1219 CFGWRITE(pi, offset, val, bytes);
1223 msicap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
1224 int bytes, uint32_t val)
1226 uint16_t msgctrl, rwmask, msgdata, mme;
1230 * If guest is writing to the message control register make sure
1231 * we do not overwrite read-only fields.
1233 if ((offset - capoff) == 2 && bytes == 2) {
1234 rwmask = PCIM_MSICTRL_MME_MASK | PCIM_MSICTRL_MSI_ENABLE;
1235 msgctrl = pci_get_cfgdata16(pi, offset);
1237 msgctrl |= val & rwmask;
1240 CFGWRITE(pi, offset, val, bytes);
1242 msgctrl = pci_get_cfgdata16(pi, capoff + 2);
1243 addrlo = pci_get_cfgdata32(pi, capoff + 4);
1244 if (msgctrl & PCIM_MSICTRL_64BIT)
1245 msgdata = pci_get_cfgdata16(pi, capoff + 12);
1247 msgdata = pci_get_cfgdata16(pi, capoff + 8);
1249 mme = msgctrl & PCIM_MSICTRL_MME_MASK;
1250 pi->pi_msi.enabled = msgctrl & PCIM_MSICTRL_MSI_ENABLE ? 1 : 0;
1251 if (pi->pi_msi.enabled) {
1252 pi->pi_msi.addr = addrlo;
1253 pi->pi_msi.msg_data = msgdata;
1254 pi->pi_msi.maxmsgnum = 1 << (mme >> 4);
1256 pi->pi_msi.maxmsgnum = 0;
1258 pci_lintr_update(pi);
1262 pciecap_cfgwrite(struct pci_devinst *pi, int capoff __unused, int offset,
1263 int bytes, uint32_t val)
1266 /* XXX don't write to the readonly parts */
1267 CFGWRITE(pi, offset, val, bytes);
1270 #define PCIECAP_VERSION 0x2
1272 pci_emul_add_pciecap(struct pci_devinst *pi, int type)
1275 struct pciecap pciecap;
1277 bzero(&pciecap, sizeof(pciecap));
1280 * Use the integrated endpoint type for endpoints on a root complex bus.
1282 * NB: bhyve currently only supports a single PCI bus that is the root
1283 * complex bus, so all endpoints are integrated.
1285 if ((type == PCIEM_TYPE_ENDPOINT) && (pi->pi_bus == 0))
1286 type = PCIEM_TYPE_ROOT_INT_EP;
1288 pciecap.capid = PCIY_EXPRESS;
1289 pciecap.pcie_capabilities = PCIECAP_VERSION | type;
1290 if (type != PCIEM_TYPE_ROOT_INT_EP) {
1291 pciecap.link_capabilities = 0x411; /* gen1, x1 */
1292 pciecap.link_status = 0x11; /* gen1, x1 */
1295 err = pci_emul_add_capability(pi, (u_char *)&pciecap, sizeof(pciecap));
1300 * This function assumes that 'coff' is in the capabilities region of the
1301 * config space. A capoff parameter of zero will force a search for the
1305 pci_emul_capwrite(struct pci_devinst *pi, int offset, int bytes, uint32_t val,
1306 uint8_t capoff, int capid)
1310 /* Do not allow un-aligned writes */
1311 if ((offset & (bytes - 1)) != 0)
1315 /* Find the capability that we want to update */
1316 capoff = CAP_START_OFFSET;
1318 nextoff = pci_get_cfgdata8(pi, capoff + 1);
1321 if (offset >= capoff && offset < nextoff)
1326 assert(offset >= capoff);
1327 capid = pci_get_cfgdata8(pi, capoff);
1331 * Capability ID and Next Capability Pointer are readonly.
1332 * However, some o/s's do 4-byte writes that include these.
1333 * For this case, trim the write back to 2 bytes and adjust
1336 if (offset == capoff || offset == capoff + 1) {
1337 if (offset == capoff && bytes == 4) {
1347 msicap_cfgwrite(pi, capoff, offset, bytes, val);
1350 msixcap_cfgwrite(pi, capoff, offset, bytes, val);
1353 pciecap_cfgwrite(pi, capoff, offset, bytes, val);
1361 pci_emul_iscap(struct pci_devinst *pi, int offset)
1365 sts = pci_get_cfgdata16(pi, PCIR_STATUS);
1366 if ((sts & PCIM_STATUS_CAPPRESENT) != 0) {
1367 if (offset >= CAP_START_OFFSET && offset <= pi->pi_capend)
1374 pci_emul_fallback_handler(struct vcpu *vcpu __unused, int dir,
1375 uint64_t addr __unused, int size __unused, uint64_t *val,
1376 void *arg1 __unused, long arg2 __unused)
1379 * Ignore writes; return 0xff's for reads. The mem read code
1380 * will take care of truncating to the correct size.
1382 if (dir == MEM_F_READ) {
1383 *val = 0xffffffffffffffff;
1390 pci_emul_ecfg_handler(struct vcpu *vcpu __unused, int dir, uint64_t addr,
1391 int bytes, uint64_t *val, void *arg1 __unused, long arg2 __unused)
1393 int bus, slot, func, coff, in;
1395 coff = addr & 0xfff;
1396 func = (addr >> 12) & 0x7;
1397 slot = (addr >> 15) & 0x1f;
1398 bus = (addr >> 20) & 0xff;
1399 in = (dir == MEM_F_READ);
1402 pci_cfgrw(in, bus, slot, func, coff, bytes, (uint32_t *)val);
1410 return (PCI_EMUL_ECFG_BASE);
1414 init_bootorder(void)
1416 struct boot_device *device;
1419 size_t bootorder_len;
1421 if (TAILQ_EMPTY(&boot_devices))
1424 fp = open_memstream(&bootorder, &bootorder_len);
1425 TAILQ_FOREACH(device, &boot_devices, boot_device_chain) {
1426 fprintf(fp, "/pci@i0cf8/pci@%d,%d\n",
1427 device->pdi->pi_slot, device->pdi->pi_func);
1431 return (qemu_fwcfg_add_file("bootorder", bootorder_len, bootorder));
1434 #define BUSIO_ROUNDUP 32
1435 #define BUSMEM32_ROUNDUP (1024 * 1024)
1436 #define BUSMEM64_ROUNDUP (512 * 1024 * 1024)
1439 init_pci(struct vmctx *ctx)
1441 char node_name[sizeof("pci.XXX.XX.X")];
1442 struct mem_range mr;
1443 struct pci_devemu *pde;
1445 struct slotinfo *si;
1446 struct funcinfo *fi;
1450 int bus, slot, func;
1453 if (vm_get_lowmem_limit(ctx) > PCI_EMUL_MEMBASE32)
1454 errx(EX_OSERR, "Invalid lowmem limit");
1456 pci_emul_iobase = PCI_EMUL_IOBASE;
1457 pci_emul_membase32 = PCI_EMUL_MEMBASE32;
1459 pci_emul_membase64 = 4*GB + vm_get_highmem_size(ctx);
1460 pci_emul_membase64 = roundup2(pci_emul_membase64, PCI_EMUL_MEMSIZE64);
1461 pci_emul_memlim64 = pci_emul_membase64 + PCI_EMUL_MEMSIZE64;
1463 TAILQ_INIT(&boot_devices);
1465 for (bus = 0; bus < MAXBUSES; bus++) {
1466 snprintf(node_name, sizeof(node_name), "pci.%d", bus);
1467 nvl = find_config_node(node_name);
1470 pci_businfo[bus] = calloc(1, sizeof(struct businfo));
1471 bi = pci_businfo[bus];
1474 * Keep track of the i/o and memory resources allocated to
1477 bi->iobase = pci_emul_iobase;
1478 bi->membase32 = pci_emul_membase32;
1479 bi->membase64 = pci_emul_membase64;
1481 /* first run: init devices */
1482 for (slot = 0; slot < MAXSLOTS; slot++) {
1483 si = &bi->slotinfo[slot];
1484 for (func = 0; func < MAXFUNCS; func++) {
1485 fi = &si->si_funcs[func];
1486 snprintf(node_name, sizeof(node_name),
1487 "pci.%d.%d.%d", bus, slot, func);
1488 nvl = find_config_node(node_name);
1492 fi->fi_config = nvl;
1493 emul = get_config_value_node(nvl, "device");
1495 EPRINTLN("pci slot %d:%d:%d: missing "
1496 "\"device\" value", bus, slot, func);
1499 pde = pci_emul_finddev(emul);
1501 EPRINTLN("pci slot %d:%d:%d: unknown "
1502 "device \"%s\"", bus, slot, func,
1506 if (pde->pe_alias != NULL) {
1507 EPRINTLN("pci slot %d:%d:%d: legacy "
1508 "device \"%s\", use \"%s\" instead",
1509 bus, slot, func, emul,
1514 error = pci_emul_init(ctx, pde, bus, slot,
1521 /* second run: assign BARs and free list */
1522 struct pci_bar_allocation *bar;
1523 struct pci_bar_allocation *bar_tmp;
1524 TAILQ_FOREACH_SAFE(bar, &pci_bars, chain, bar_tmp) {
1525 pci_emul_assign_bar(bar->pdi, bar->idx, bar->type,
1529 TAILQ_INIT(&pci_bars);
1532 * Add some slop to the I/O and memory resources decoded by
1533 * this bus to give a guest some flexibility if it wants to
1534 * reprogram the BARs.
1536 pci_emul_iobase += BUSIO_ROUNDUP;
1537 pci_emul_iobase = roundup2(pci_emul_iobase, BUSIO_ROUNDUP);
1538 bi->iolimit = pci_emul_iobase;
1540 pci_emul_membase32 += BUSMEM32_ROUNDUP;
1541 pci_emul_membase32 = roundup2(pci_emul_membase32,
1543 bi->memlimit32 = pci_emul_membase32;
1545 pci_emul_membase64 += BUSMEM64_ROUNDUP;
1546 pci_emul_membase64 = roundup2(pci_emul_membase64,
1548 bi->memlimit64 = pci_emul_membase64;
1552 * PCI backends are initialized before routing INTx interrupts
1553 * so that LPC devices are able to reserve ISA IRQs before
1554 * routing PIRQ pins.
1556 for (bus = 0; bus < MAXBUSES; bus++) {
1557 if ((bi = pci_businfo[bus]) == NULL)
1560 for (slot = 0; slot < MAXSLOTS; slot++) {
1561 si = &bi->slotinfo[slot];
1562 for (func = 0; func < MAXFUNCS; func++) {
1563 fi = &si->si_funcs[func];
1564 if (fi->fi_devi == NULL)
1566 pci_lintr_route(fi->fi_devi);
1572 if ((error = init_bootorder()) != 0) {
1573 warnx("%s: Unable to init bootorder", __func__);
1578 * The guest physical memory map looks like the following:
1579 * [0, lowmem) guest system memory
1580 * [lowmem, 0xC0000000) memory hole (may be absent)
1581 * [0xC0000000, 0xE0000000) PCI hole (32-bit BAR allocation)
1582 * [0xE0000000, 0xF0000000) PCI extended config window
1583 * [0xF0000000, 4GB) LAPIC, IOAPIC, HPET, firmware
1584 * [4GB, 4GB + highmem)
1588 * Accesses to memory addresses that are not allocated to system
1589 * memory or PCI devices return 0xff's.
1591 lowmem = vm_get_lowmem_size(ctx);
1592 bzero(&mr, sizeof(struct mem_range));
1593 mr.name = "PCI hole";
1594 mr.flags = MEM_F_RW | MEM_F_IMMUTABLE;
1596 mr.size = (4ULL * 1024 * 1024 * 1024) - lowmem;
1597 mr.handler = pci_emul_fallback_handler;
1598 error = register_mem_fallback(&mr);
1601 /* PCI extended config space */
1602 bzero(&mr, sizeof(struct mem_range));
1603 mr.name = "PCI ECFG";
1604 mr.flags = MEM_F_RW | MEM_F_IMMUTABLE;
1605 mr.base = PCI_EMUL_ECFG_BASE;
1606 mr.size = PCI_EMUL_ECFG_SIZE;
1607 mr.handler = pci_emul_ecfg_handler;
1608 error = register_mem(&mr);
1615 pci_apic_prt_entry(int bus __unused, int slot, int pin, int pirq_pin __unused,
1616 int ioapic_irq, void *arg __unused)
1619 dsdt_line(" Package ()");
1621 dsdt_line(" 0x%X,", slot << 16 | 0xffff);
1622 dsdt_line(" 0x%02X,", pin - 1);
1623 dsdt_line(" Zero,");
1624 dsdt_line(" 0x%X", ioapic_irq);
1629 pci_pirq_prt_entry(int bus __unused, int slot, int pin, int pirq_pin,
1630 int ioapic_irq __unused, void *arg __unused)
1634 name = lpc_pirq_name(pirq_pin);
1637 dsdt_line(" Package ()");
1639 dsdt_line(" 0x%X,", slot << 16 | 0xffff);
1640 dsdt_line(" 0x%02X,", pin - 1);
1641 dsdt_line(" %s,", name);
1648 * A bhyve virtual machine has a flat PCI hierarchy with a root port
1649 * corresponding to each PCI bus.
1652 pci_bus_write_dsdt(int bus)
1655 struct slotinfo *si;
1656 struct pci_devinst *pi;
1657 int count, func, slot;
1660 * If there are no devices on this 'bus' then just return.
1662 if ((bi = pci_businfo[bus]) == NULL) {
1664 * Bus 0 is special because it decodes the I/O ports used
1665 * for PCI config space access even if there are no devices
1672 dsdt_line(" Device (PC%02X)", bus);
1674 dsdt_line(" Name (_HID, EisaId (\"PNP0A03\"))");
1676 dsdt_line(" Method (_BBN, 0, NotSerialized)");
1678 dsdt_line(" Return (0x%08X)", bus);
1680 dsdt_line(" Name (_CRS, ResourceTemplate ()");
1682 dsdt_line(" WordBusNumber (ResourceProducer, MinFixed, "
1683 "MaxFixed, PosDecode,");
1684 dsdt_line(" 0x0000, // Granularity");
1685 dsdt_line(" 0x%04X, // Range Minimum", bus);
1686 dsdt_line(" 0x%04X, // Range Maximum", bus);
1687 dsdt_line(" 0x0000, // Translation Offset");
1688 dsdt_line(" 0x0001, // Length");
1693 dsdt_fixed_ioport(0xCF8, 8);
1696 dsdt_line(" WordIO (ResourceProducer, MinFixed, MaxFixed, "
1697 "PosDecode, EntireRange,");
1698 dsdt_line(" 0x0000, // Granularity");
1699 dsdt_line(" 0x0000, // Range Minimum");
1700 dsdt_line(" 0x0CF7, // Range Maximum");
1701 dsdt_line(" 0x0000, // Translation Offset");
1702 dsdt_line(" 0x0CF8, // Length");
1703 dsdt_line(" ,, , TypeStatic)");
1705 dsdt_line(" WordIO (ResourceProducer, MinFixed, MaxFixed, "
1706 "PosDecode, EntireRange,");
1707 dsdt_line(" 0x0000, // Granularity");
1708 dsdt_line(" 0x0D00, // Range Minimum");
1709 dsdt_line(" 0x%04X, // Range Maximum",
1710 PCI_EMUL_IOBASE - 1);
1711 dsdt_line(" 0x0000, // Translation Offset");
1712 dsdt_line(" 0x%04X, // Length",
1713 PCI_EMUL_IOBASE - 0x0D00);
1714 dsdt_line(" ,, , TypeStatic)");
1724 dsdt_line(" WordIO (ResourceProducer, MinFixed, MaxFixed, "
1725 "PosDecode, EntireRange,");
1726 dsdt_line(" 0x0000, // Granularity");
1727 dsdt_line(" 0x%04X, // Range Minimum", bi->iobase);
1728 dsdt_line(" 0x%04X, // Range Maximum",
1730 dsdt_line(" 0x0000, // Translation Offset");
1731 dsdt_line(" 0x%04X, // Length",
1732 bi->iolimit - bi->iobase);
1733 dsdt_line(" ,, , TypeStatic)");
1735 /* mmio window (32-bit) */
1736 dsdt_line(" DWordMemory (ResourceProducer, PosDecode, "
1737 "MinFixed, MaxFixed, NonCacheable, ReadWrite,");
1738 dsdt_line(" 0x00000000, // Granularity");
1739 dsdt_line(" 0x%08X, // Range Minimum\n", bi->membase32);
1740 dsdt_line(" 0x%08X, // Range Maximum\n",
1741 bi->memlimit32 - 1);
1742 dsdt_line(" 0x00000000, // Translation Offset");
1743 dsdt_line(" 0x%08X, // Length\n",
1744 bi->memlimit32 - bi->membase32);
1745 dsdt_line(" ,, , AddressRangeMemory, TypeStatic)");
1747 /* mmio window (64-bit) */
1748 dsdt_line(" QWordMemory (ResourceProducer, PosDecode, "
1749 "MinFixed, MaxFixed, NonCacheable, ReadWrite,");
1750 dsdt_line(" 0x0000000000000000, // Granularity");
1751 dsdt_line(" 0x%016lX, // Range Minimum\n", bi->membase64);
1752 dsdt_line(" 0x%016lX, // Range Maximum\n",
1753 bi->memlimit64 - 1);
1754 dsdt_line(" 0x0000000000000000, // Translation Offset");
1755 dsdt_line(" 0x%016lX, // Length\n",
1756 bi->memlimit64 - bi->membase64);
1757 dsdt_line(" ,, , AddressRangeMemory, TypeStatic)");
1760 count = pci_count_lintr(bus);
1763 dsdt_line("Name (PPRT, Package ()");
1765 pci_walk_lintr(bus, pci_pirq_prt_entry, NULL);
1767 dsdt_line("Name (APRT, Package ()");
1769 pci_walk_lintr(bus, pci_apic_prt_entry, NULL);
1771 dsdt_line("Method (_PRT, 0, NotSerialized)");
1773 dsdt_line(" If (PICM)");
1775 dsdt_line(" Return (APRT)");
1779 dsdt_line(" Return (PPRT)");
1786 for (slot = 0; slot < MAXSLOTS; slot++) {
1787 si = &bi->slotinfo[slot];
1788 for (func = 0; func < MAXFUNCS; func++) {
1789 pi = si->si_funcs[func].fi_devi;
1790 if (pi != NULL && pi->pi_d->pe_write_dsdt != NULL)
1791 pi->pi_d->pe_write_dsdt(pi);
1800 pci_write_dsdt(void)
1805 dsdt_line("Name (PICM, 0x00)");
1806 dsdt_line("Method (_PIC, 1, NotSerialized)");
1808 dsdt_line(" Store (Arg0, PICM)");
1811 dsdt_line("Scope (_SB)");
1813 for (bus = 0; bus < MAXBUSES; bus++)
1814 pci_bus_write_dsdt(bus);
1820 pci_bus_configured(int bus)
1822 assert(bus >= 0 && bus < MAXBUSES);
1823 return (pci_businfo[bus] != NULL);
1827 pci_msi_enabled(struct pci_devinst *pi)
1829 return (pi->pi_msi.enabled);
1833 pci_msi_maxmsgnum(struct pci_devinst *pi)
1835 if (pi->pi_msi.enabled)
1836 return (pi->pi_msi.maxmsgnum);
1842 pci_msix_enabled(struct pci_devinst *pi)
1845 return (pi->pi_msix.enabled && !pi->pi_msi.enabled);
1849 pci_generate_msix(struct pci_devinst *pi, int index)
1851 struct msix_table_entry *mte;
1853 if (!pci_msix_enabled(pi))
1856 if (pi->pi_msix.function_mask)
1859 if (index >= pi->pi_msix.table_count)
1862 mte = &pi->pi_msix.table[index];
1863 if ((mte->vector_control & PCIM_MSIX_VCTRL_MASK) == 0) {
1864 /* XXX Set PBA bit if interrupt is disabled */
1865 vm_lapic_msi(pi->pi_vmctx, mte->addr, mte->msg_data);
1870 pci_generate_msi(struct pci_devinst *pi, int index)
1873 if (pci_msi_enabled(pi) && index < pci_msi_maxmsgnum(pi)) {
1874 vm_lapic_msi(pi->pi_vmctx, pi->pi_msi.addr,
1875 pi->pi_msi.msg_data + index);
1880 pci_lintr_permitted(struct pci_devinst *pi)
1884 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND);
1885 return (!(pi->pi_msi.enabled || pi->pi_msix.enabled ||
1886 (cmd & PCIM_CMD_INTxDIS)));
1890 pci_lintr_request(struct pci_devinst *pi)
1893 struct slotinfo *si;
1894 int bestpin, bestcount, pin;
1896 bi = pci_businfo[pi->pi_bus];
1900 * Just allocate a pin from our slot. The pin will be
1901 * assigned IRQs later when interrupts are routed.
1903 si = &bi->slotinfo[pi->pi_slot];
1905 bestcount = si->si_intpins[0].ii_count;
1906 for (pin = 1; pin < 4; pin++) {
1907 if (si->si_intpins[pin].ii_count < bestcount) {
1909 bestcount = si->si_intpins[pin].ii_count;
1913 si->si_intpins[bestpin].ii_count++;
1914 pi->pi_lintr.pin = bestpin + 1;
1915 pci_set_cfgdata8(pi, PCIR_INTPIN, bestpin + 1);
1919 pci_lintr_route(struct pci_devinst *pi)
1922 struct intxinfo *ii;
1924 if (pi->pi_lintr.pin == 0)
1927 bi = pci_businfo[pi->pi_bus];
1929 ii = &bi->slotinfo[pi->pi_slot].si_intpins[pi->pi_lintr.pin - 1];
1932 * Attempt to allocate an I/O APIC pin for this intpin if one
1933 * is not yet assigned.
1935 if (ii->ii_ioapic_irq == 0)
1936 ii->ii_ioapic_irq = ioapic_pci_alloc_irq(pi);
1937 assert(ii->ii_ioapic_irq > 0);
1940 * Attempt to allocate a PIRQ pin for this intpin if one is
1943 if (ii->ii_pirq_pin == 0)
1944 ii->ii_pirq_pin = pirq_alloc_pin(pi);
1945 assert(ii->ii_pirq_pin > 0);
1947 pi->pi_lintr.ioapic_irq = ii->ii_ioapic_irq;
1948 pi->pi_lintr.pirq_pin = ii->ii_pirq_pin;
1949 pci_set_cfgdata8(pi, PCIR_INTLINE, pirq_irq(ii->ii_pirq_pin));
1953 pci_lintr_assert(struct pci_devinst *pi)
1956 assert(pi->pi_lintr.pin > 0);
1958 pthread_mutex_lock(&pi->pi_lintr.lock);
1959 if (pi->pi_lintr.state == IDLE) {
1960 if (pci_lintr_permitted(pi)) {
1961 pi->pi_lintr.state = ASSERTED;
1964 pi->pi_lintr.state = PENDING;
1966 pthread_mutex_unlock(&pi->pi_lintr.lock);
1970 pci_lintr_deassert(struct pci_devinst *pi)
1973 assert(pi->pi_lintr.pin > 0);
1975 pthread_mutex_lock(&pi->pi_lintr.lock);
1976 if (pi->pi_lintr.state == ASSERTED) {
1977 pi->pi_lintr.state = IDLE;
1978 pci_irq_deassert(pi);
1979 } else if (pi->pi_lintr.state == PENDING)
1980 pi->pi_lintr.state = IDLE;
1981 pthread_mutex_unlock(&pi->pi_lintr.lock);
1985 pci_lintr_update(struct pci_devinst *pi)
1988 pthread_mutex_lock(&pi->pi_lintr.lock);
1989 if (pi->pi_lintr.state == ASSERTED && !pci_lintr_permitted(pi)) {
1990 pci_irq_deassert(pi);
1991 pi->pi_lintr.state = PENDING;
1992 } else if (pi->pi_lintr.state == PENDING && pci_lintr_permitted(pi)) {
1993 pi->pi_lintr.state = ASSERTED;
1996 pthread_mutex_unlock(&pi->pi_lintr.lock);
2000 pci_count_lintr(int bus)
2002 int count, slot, pin;
2003 struct slotinfo *slotinfo;
2006 if (pci_businfo[bus] != NULL) {
2007 for (slot = 0; slot < MAXSLOTS; slot++) {
2008 slotinfo = &pci_businfo[bus]->slotinfo[slot];
2009 for (pin = 0; pin < 4; pin++) {
2010 if (slotinfo->si_intpins[pin].ii_count != 0)
2019 pci_walk_lintr(int bus, pci_lintr_cb cb, void *arg)
2022 struct slotinfo *si;
2023 struct intxinfo *ii;
2026 if ((bi = pci_businfo[bus]) == NULL)
2029 for (slot = 0; slot < MAXSLOTS; slot++) {
2030 si = &bi->slotinfo[slot];
2031 for (pin = 0; pin < 4; pin++) {
2032 ii = &si->si_intpins[pin];
2033 if (ii->ii_count != 0)
2034 cb(bus, slot, pin + 1, ii->ii_pirq_pin,
2035 ii->ii_ioapic_irq, arg);
2041 * Return 1 if the emulated device in 'slot' is a multi-function device.
2042 * Return 0 otherwise.
2045 pci_emul_is_mfdev(int bus, int slot)
2048 struct slotinfo *si;
2052 if ((bi = pci_businfo[bus]) != NULL) {
2053 si = &bi->slotinfo[slot];
2054 for (f = 0; f < MAXFUNCS; f++) {
2055 if (si->si_funcs[f].fi_devi != NULL) {
2060 return (numfuncs > 1);
2064 * Ensure that the PCIM_MFDEV bit is properly set (or unset) depending on
2065 * whether or not is a multi-function being emulated in the pci 'slot'.
2068 pci_emul_hdrtype_fixup(int bus, int slot, int off, int bytes, uint32_t *rv)
2072 if (off <= PCIR_HDRTYPE && off + bytes > PCIR_HDRTYPE) {
2073 mfdev = pci_emul_is_mfdev(bus, slot);
2083 *rv &= ~(PCIM_MFDEV << 16);
2085 *rv |= (PCIM_MFDEV << 16);
2093 * Update device state in response to changes to the PCI command
2097 pci_emul_cmd_changed(struct pci_devinst *pi, uint16_t old)
2100 uint16_t changed, new;
2102 new = pci_get_cfgdata16(pi, PCIR_COMMAND);
2103 changed = old ^ new;
2106 * If the MMIO or I/O address space decoding has changed then
2107 * register/unregister all BARs that decode that address space.
2109 for (i = 0; i <= PCI_BARMAX_WITH_ROM; i++) {
2110 switch (pi->pi_bar[i].type) {
2112 case PCIBAR_MEMHI64:
2115 /* I/O address space decoding changed? */
2116 if (changed & PCIM_CMD_PORTEN) {
2117 if (new & PCIM_CMD_PORTEN)
2118 register_bar(pi, i);
2120 unregister_bar(pi, i);
2124 /* skip (un-)register of ROM if it disabled */
2130 /* MMIO address space decoding changed? */
2131 if (changed & PCIM_CMD_MEMEN) {
2132 if (new & PCIM_CMD_MEMEN)
2133 register_bar(pi, i);
2135 unregister_bar(pi, i);
2144 * If INTx has been unmasked and is pending, assert the
2147 pci_lintr_update(pi);
2151 pci_emul_cmdsts_write(struct pci_devinst *pi, int coff, uint32_t new, int bytes)
2154 uint32_t cmd, old, readonly;
2156 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND); /* stash old value */
2159 * From PCI Local Bus Specification 3.0 sections 6.2.2 and 6.2.3.
2161 * XXX Bits 8, 11, 12, 13, 14 and 15 in the status register are
2162 * 'write 1 to clear'. However these bits are not set to '1' by
2163 * any device emulation so it is simpler to treat them as readonly.
2165 rshift = (coff & 0x3) * 8;
2166 readonly = 0xFFFFF880 >> rshift;
2168 old = CFGREAD(pi, coff, bytes);
2170 new |= (old & readonly);
2171 CFGWRITE(pi, coff, new, bytes); /* update config */
2173 pci_emul_cmd_changed(pi, cmd);
2177 pci_cfgrw(int in, int bus, int slot, int func, int coff, int bytes,
2181 struct slotinfo *si;
2182 struct pci_devinst *pi;
2183 struct pci_devemu *pe;
2185 uint64_t addr, bar, mask;
2187 if ((bi = pci_businfo[bus]) != NULL) {
2188 si = &bi->slotinfo[slot];
2189 pi = si->si_funcs[func].fi_devi;
2194 * Just return if there is no device at this slot:func or if the
2195 * the guest is doing an un-aligned access.
2197 if (pi == NULL || (bytes != 1 && bytes != 2 && bytes != 4) ||
2198 (coff & (bytes - 1)) != 0) {
2205 * Ignore all writes beyond the standard config space and return all
2208 if (coff >= PCI_REGMAX + 1) {
2212 * Extended capabilities begin at offset 256 in config
2213 * space. Absence of extended capabilities is signaled
2214 * with all 0s in the extended capability header at
2217 if (coff <= PCI_REGMAX + 4)
2229 /* Let the device emulation override the default handler */
2230 if (pe->pe_cfgread != NULL) {
2231 needcfg = pe->pe_cfgread(pi, coff, bytes, eax);
2237 *eax = CFGREAD(pi, coff, bytes);
2239 pci_emul_hdrtype_fixup(bus, slot, coff, bytes, eax);
2241 /* Let the device emulation override the default handler */
2242 if (pe->pe_cfgwrite != NULL &&
2243 (*pe->pe_cfgwrite)(pi, coff, bytes, *eax) == 0)
2247 * Special handling for write to BAR and ROM registers
2249 if (is_pcir_bar(coff) || is_pcir_bios(coff)) {
2251 * Ignore writes to BAR registers that are not
2254 if (bytes != 4 || (coff & 0x3) != 0)
2257 if (is_pcir_bar(coff)) {
2258 idx = (coff - PCIR_BAR(0)) / 4;
2259 } else if (is_pcir_bios(coff)) {
2262 errx(4, "%s: invalid BAR offset %d", __func__,
2266 mask = ~(pi->pi_bar[idx].size - 1);
2267 switch (pi->pi_bar[idx].type) {
2269 pi->pi_bar[idx].addr = bar = 0;
2274 bar = addr | pi->pi_bar[idx].lobits;
2276 * Register the new BAR value for interception
2278 if (addr != pi->pi_bar[idx].addr) {
2279 update_bar_address(pi, addr, idx,
2284 addr = bar = *eax & mask;
2285 bar |= pi->pi_bar[idx].lobits;
2286 if (addr != pi->pi_bar[idx].addr) {
2287 update_bar_address(pi, addr, idx,
2292 addr = bar = *eax & mask;
2293 bar |= pi->pi_bar[idx].lobits;
2294 if (addr != (uint32_t)pi->pi_bar[idx].addr) {
2295 update_bar_address(pi, addr, idx,
2299 case PCIBAR_MEMHI64:
2300 mask = ~(pi->pi_bar[idx - 1].size - 1);
2301 addr = ((uint64_t)*eax << 32) & mask;
2303 if (bar != pi->pi_bar[idx - 1].addr >> 32) {
2304 update_bar_address(pi, addr, idx - 1,
2309 addr = bar = *eax & mask;
2310 if (memen(pi) && romen(pi)) {
2311 unregister_bar(pi, idx);
2313 pi->pi_bar[idx].addr = addr;
2314 pi->pi_bar[idx].lobits = *eax &
2316 /* romen could have changed it value */
2317 if (memen(pi) && romen(pi)) {
2318 register_bar(pi, idx);
2320 bar |= pi->pi_bar[idx].lobits;
2325 pci_set_cfgdata32(pi, coff, bar);
2327 } else if (pci_emul_iscap(pi, coff)) {
2328 pci_emul_capwrite(pi, coff, bytes, *eax, 0, 0);
2329 } else if (coff >= PCIR_COMMAND && coff < PCIR_REVID) {
2330 pci_emul_cmdsts_write(pi, coff, *eax, bytes);
2332 CFGWRITE(pi, coff, *eax, bytes);
2337 static int cfgenable, cfgbus, cfgslot, cfgfunc, cfgoff;
2340 pci_emul_cfgaddr(struct vmctx *ctx __unused, int in,
2341 int port __unused, int bytes, uint32_t *eax, void *arg __unused)
2347 *eax = (bytes == 2) ? 0xffff : 0xff;
2352 x = (cfgbus << 16) | (cfgslot << 11) | (cfgfunc << 8) | cfgoff;
2358 cfgenable = (x & CONF1_ENABLE) == CONF1_ENABLE;
2359 cfgoff = (x & PCI_REGMAX) & ~0x03;
2360 cfgfunc = (x >> 8) & PCI_FUNCMAX;
2361 cfgslot = (x >> 11) & PCI_SLOTMAX;
2362 cfgbus = (x >> 16) & PCI_BUSMAX;
2367 INOUT_PORT(pci_cfgaddr, CONF1_ADDR_PORT, IOPORT_F_INOUT, pci_emul_cfgaddr);
2370 pci_emul_cfgdata(struct vmctx *ctx __unused, int in, int port,
2371 int bytes, uint32_t *eax, void *arg __unused)
2375 assert(bytes == 1 || bytes == 2 || bytes == 4);
2377 coff = cfgoff + (port - CONF1_DATA_PORT);
2379 pci_cfgrw(in, cfgbus, cfgslot, cfgfunc, coff, bytes, eax);
2381 /* Ignore accesses to cfgdata if not enabled by cfgaddr */
2388 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+0, IOPORT_F_INOUT, pci_emul_cfgdata);
2389 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+1, IOPORT_F_INOUT, pci_emul_cfgdata);
2390 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+2, IOPORT_F_INOUT, pci_emul_cfgdata);
2391 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+3, IOPORT_F_INOUT, pci_emul_cfgdata);
2393 #ifdef BHYVE_SNAPSHOT
2395 * Saves/restores PCI device emulated state. Returns 0 on success.
2398 pci_snapshot_pci_dev(struct vm_snapshot_meta *meta)
2400 struct pci_devinst *pi;
2404 pi = meta->dev_data;
2406 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msi.enabled, meta, ret, done);
2407 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msi.addr, meta, ret, done);
2408 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msi.msg_data, meta, ret, done);
2409 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msi.maxmsgnum, meta, ret, done);
2411 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.enabled, meta, ret, done);
2412 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.table_bar, meta, ret, done);
2413 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.pba_bar, meta, ret, done);
2414 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.table_offset, meta, ret, done);
2415 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.table_count, meta, ret, done);
2416 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.pba_offset, meta, ret, done);
2417 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.pba_size, meta, ret, done);
2418 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.function_mask, meta, ret, done);
2420 SNAPSHOT_BUF_OR_LEAVE(pi->pi_cfgdata, sizeof(pi->pi_cfgdata),
2423 for (i = 0; i < (int)nitems(pi->pi_bar); i++) {
2424 SNAPSHOT_VAR_OR_LEAVE(pi->pi_bar[i].type, meta, ret, done);
2425 SNAPSHOT_VAR_OR_LEAVE(pi->pi_bar[i].size, meta, ret, done);
2426 SNAPSHOT_VAR_OR_LEAVE(pi->pi_bar[i].addr, meta, ret, done);
2429 /* Restore MSI-X table. */
2430 for (i = 0; i < pi->pi_msix.table_count; i++) {
2431 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.table[i].addr,
2433 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.table[i].msg_data,
2435 SNAPSHOT_VAR_OR_LEAVE(pi->pi_msix.table[i].vector_control,
2444 pci_snapshot(struct vm_snapshot_meta *meta)
2446 struct pci_devemu *pde;
2447 struct pci_devinst *pdi;
2450 assert(meta->dev_name != NULL);
2452 pdi = meta->dev_data;
2455 if (pde->pe_snapshot == NULL)
2458 ret = pci_snapshot_pci_dev(meta);
2460 ret = (*pde->pe_snapshot)(meta);
2466 pci_pause(struct pci_devinst *pdi)
2468 struct pci_devemu *pde = pdi->pi_d;
2470 if (pde->pe_pause == NULL) {
2471 /* The pause/resume functionality is optional. */
2475 return (*pde->pe_pause)(pdi);
2479 pci_resume(struct pci_devinst *pdi)
2481 struct pci_devemu *pde = pdi->pi_d;
2483 if (pde->pe_resume == NULL) {
2484 /* The pause/resume functionality is optional. */
2488 return (*pde->pe_resume)(pdi);
2492 #define PCI_EMUL_TEST
2493 #ifdef PCI_EMUL_TEST
2495 * Define a dummy test device
2499 struct pci_emul_dsoftc {
2500 uint8_t ioregs[DIOSZ];
2501 uint8_t memregs[2][DMEMSZ];
2504 #define PCI_EMUL_MSI_MSGS 4
2505 #define PCI_EMUL_MSIX_MSGS 16
2508 pci_emul_dinit(struct pci_devinst *pi, nvlist_t *nvl __unused)
2511 struct pci_emul_dsoftc *sc;
2513 sc = calloc(1, sizeof(struct pci_emul_dsoftc));
2517 pci_set_cfgdata16(pi, PCIR_DEVICE, 0x0001);
2518 pci_set_cfgdata16(pi, PCIR_VENDOR, 0x10DD);
2519 pci_set_cfgdata8(pi, PCIR_CLASS, 0x02);
2521 error = pci_emul_add_msicap(pi, PCI_EMUL_MSI_MSGS);
2524 error = pci_emul_alloc_bar(pi, 0, PCIBAR_IO, DIOSZ);
2527 error = pci_emul_alloc_bar(pi, 1, PCIBAR_MEM32, DMEMSZ);
2530 error = pci_emul_alloc_bar(pi, 2, PCIBAR_MEM32, DMEMSZ);
2537 pci_emul_diow(struct pci_devinst *pi, int baridx, uint64_t offset, int size,
2541 struct pci_emul_dsoftc *sc = pi->pi_arg;
2544 if (offset + size > DIOSZ) {
2545 printf("diow: iow too large, offset %ld size %d\n",
2551 sc->ioregs[offset] = value & 0xff;
2552 } else if (size == 2) {
2553 *(uint16_t *)&sc->ioregs[offset] = value & 0xffff;
2554 } else if (size == 4) {
2555 *(uint32_t *)&sc->ioregs[offset] = value;
2557 printf("diow: iow unknown size %d\n", size);
2561 * Special magic value to generate an interrupt
2563 if (offset == 4 && size == 4 && pci_msi_enabled(pi))
2564 pci_generate_msi(pi, value % pci_msi_maxmsgnum(pi));
2566 if (value == 0xabcdef) {
2567 for (i = 0; i < pci_msi_maxmsgnum(pi); i++)
2568 pci_generate_msi(pi, i);
2572 if (baridx == 1 || baridx == 2) {
2573 if (offset + size > DMEMSZ) {
2574 printf("diow: memw too large, offset %ld size %d\n",
2579 i = baridx - 1; /* 'memregs' index */
2582 sc->memregs[i][offset] = value;
2583 } else if (size == 2) {
2584 *(uint16_t *)&sc->memregs[i][offset] = value;
2585 } else if (size == 4) {
2586 *(uint32_t *)&sc->memregs[i][offset] = value;
2587 } else if (size == 8) {
2588 *(uint64_t *)&sc->memregs[i][offset] = value;
2590 printf("diow: memw unknown size %d\n", size);
2594 * magic interrupt ??
2598 if (baridx > 2 || baridx < 0) {
2599 printf("diow: unknown bar idx %d\n", baridx);
2604 pci_emul_dior(struct pci_devinst *pi, int baridx, uint64_t offset, int size)
2606 struct pci_emul_dsoftc *sc = pi->pi_arg;
2611 if (offset + size > DIOSZ) {
2612 printf("dior: ior too large, offset %ld size %d\n",
2619 value = sc->ioregs[offset];
2620 } else if (size == 2) {
2621 value = *(uint16_t *) &sc->ioregs[offset];
2622 } else if (size == 4) {
2623 value = *(uint32_t *) &sc->ioregs[offset];
2625 printf("dior: ior unknown size %d\n", size);
2629 if (baridx == 1 || baridx == 2) {
2630 if (offset + size > DMEMSZ) {
2631 printf("dior: memr too large, offset %ld size %d\n",
2636 i = baridx - 1; /* 'memregs' index */
2639 value = sc->memregs[i][offset];
2640 } else if (size == 2) {
2641 value = *(uint16_t *) &sc->memregs[i][offset];
2642 } else if (size == 4) {
2643 value = *(uint32_t *) &sc->memregs[i][offset];
2644 } else if (size == 8) {
2645 value = *(uint64_t *) &sc->memregs[i][offset];
2647 printf("dior: ior unknown size %d\n", size);
2652 if (baridx > 2 || baridx < 0) {
2653 printf("dior: unknown bar idx %d\n", baridx);
2660 #ifdef BHYVE_SNAPSHOT
2661 struct pci_devinst *
2662 pci_next(const struct pci_devinst *cursor)
2664 unsigned bus = 0, slot = 0, func = 0;
2666 struct slotinfo *si;
2667 struct funcinfo *fi;
2669 bus = cursor ? cursor->pi_bus : 0;
2670 slot = cursor ? cursor->pi_slot : 0;
2671 func = cursor ? (cursor->pi_func + 1) : 0;
2673 for (; bus < MAXBUSES; bus++) {
2674 if ((bi = pci_businfo[bus]) == NULL)
2677 if (slot >= MAXSLOTS)
2680 for (; slot < MAXSLOTS; slot++) {
2681 si = &bi->slotinfo[slot];
2682 if (func >= MAXFUNCS)
2684 for (; func < MAXFUNCS; func++) {
2685 fi = &si->si_funcs[func];
2686 if (fi->fi_devi == NULL)
2689 return (fi->fi_devi);
2698 pci_emul_snapshot(struct vm_snapshot_meta *meta __unused)
2704 static const struct pci_devemu pci_dummy = {
2706 .pe_init = pci_emul_dinit,
2707 .pe_barwrite = pci_emul_diow,
2708 .pe_barread = pci_emul_dior,
2709 #ifdef BHYVE_SNAPSHOT
2710 .pe_snapshot = pci_emul_snapshot,
2713 PCI_EMUL_SET(pci_dummy);
2715 #endif /* PCI_EMUL_TEST */