2 * Copyright (c) 2011 NetApp, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/linker_set.h>
34 #include <sys/errno.h>
45 #include <machine/vmm.h>
56 #define CONF1_ADDR_PORT 0x0cf8
57 #define CONF1_DATA_PORT 0x0cfc
59 #define CONF1_ENABLE 0x80000000ul
61 #define CFGWRITE(pi,off,val,b) \
64 pci_set_cfgdata8((pi),(off),(val)); \
65 } else if ((b) == 2) { \
66 pci_set_cfgdata16((pi),(off),(val)); \
68 pci_set_cfgdata32((pi),(off),(val)); \
72 #define MAXBUSES (PCI_BUSMAX + 1)
73 #define MAXSLOTS (PCI_SLOTMAX + 1)
74 #define MAXFUNCS (PCI_FUNCMAX + 1)
79 struct pci_devinst *fi_devi;
88 struct intxinfo si_intpins[4];
89 struct funcinfo si_funcs[MAXFUNCS];
93 uint16_t iobase, iolimit; /* I/O window */
94 uint32_t membase32, memlimit32; /* mmio window below 4GB */
95 uint64_t membase64, memlimit64; /* mmio window above 4GB */
96 struct slotinfo slotinfo[MAXSLOTS];
99 static struct businfo *pci_businfo[MAXBUSES];
101 SET_DECLARE(pci_devemu_set, struct pci_devemu);
103 static uint64_t pci_emul_iobase;
104 static uint64_t pci_emul_membase32;
105 static uint64_t pci_emul_membase64;
107 #define PCI_EMUL_IOBASE 0x2000
108 #define PCI_EMUL_IOLIMIT 0x10000
110 #define PCI_EMUL_MEMLIMIT32 0xE0000000 /* 3.5GB */
112 #define PCI_EMUL_MEMBASE64 0xD000000000UL
113 #define PCI_EMUL_MEMLIMIT64 0xFD00000000UL
115 static struct pci_devemu *pci_emul_finddev(char *name);
116 static void pci_lintr_update(struct pci_devinst *pi);
118 static struct mem_range pci_mem_hole;
125 * Slot options are in the form:
127 * <bus>:<slot>:<func>,<emul>[,<config>]
128 * <slot>[:<func>],<emul>[,<config>]
132 * emul is a string describing the type of PCI device e.g. virtio-net
133 * config is an optional string, depending on the device, that can be
134 * used for configuration.
140 pci_parse_slot_usage(char *aopt)
143 fprintf(stderr, "Invalid PCI slot info field \"%s\"\n", aopt);
147 pci_parse_slot(char *opt)
151 char *emul, *config, *str, *cp;
152 int error, bnum, snum, fnum;
157 emul = config = NULL;
158 if ((cp = strchr(str, ',')) != NULL) {
161 if ((cp = strchr(emul, ',')) != NULL) {
166 pci_parse_slot_usage(opt);
170 /* <bus>:<slot>:<func> */
171 if (sscanf(str, "%d:%d:%d", &bnum, &snum, &fnum) != 3) {
174 if (sscanf(str, "%d:%d", &snum, &fnum) != 2) {
177 if (sscanf(str, "%d", &snum) != 1) {
183 if (bnum < 0 || bnum >= MAXBUSES || snum < 0 || snum >= MAXSLOTS ||
184 fnum < 0 || fnum >= MAXFUNCS) {
185 pci_parse_slot_usage(opt);
189 if (pci_businfo[bnum] == NULL)
190 pci_businfo[bnum] = calloc(1, sizeof(struct businfo));
192 bi = pci_businfo[bnum];
193 si = &bi->slotinfo[snum];
195 if (si->si_funcs[fnum].fi_name != NULL) {
196 fprintf(stderr, "pci slot %d:%d already occupied!\n",
201 if (pci_emul_finddev(emul) == NULL) {
202 fprintf(stderr, "pci slot %d:%d: unknown device \"%s\"\n",
208 si->si_funcs[fnum].fi_name = emul;
209 si->si_funcs[fnum].fi_param = config;
219 pci_valid_pba_offset(struct pci_devinst *pi, uint64_t offset)
222 if (offset < pi->pi_msix.pba_offset)
225 if (offset >= pi->pi_msix.pba_offset + pi->pi_msix.pba_size) {
233 pci_emul_msix_twrite(struct pci_devinst *pi, uint64_t offset, int size,
236 int msix_entry_offset;
240 /* support only 4 or 8 byte writes */
241 if (size != 4 && size != 8)
245 * Return if table index is beyond what device supports
247 tab_index = offset / MSIX_TABLE_ENTRY_SIZE;
248 if (tab_index >= pi->pi_msix.table_count)
251 msix_entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
253 /* support only aligned writes */
254 if ((msix_entry_offset % size) != 0)
257 dest = (char *)(pi->pi_msix.table + tab_index);
258 dest += msix_entry_offset;
261 *((uint32_t *)dest) = value;
263 *((uint64_t *)dest) = value;
269 pci_emul_msix_tread(struct pci_devinst *pi, uint64_t offset, int size)
272 int msix_entry_offset;
274 uint64_t retval = ~0;
277 * The PCI standard only allows 4 and 8 byte accesses to the MSI-X
278 * table but we also allow 1 byte access to accomodate reads from
281 if (size != 1 && size != 4 && size != 8)
284 msix_entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
286 /* support only aligned reads */
287 if ((msix_entry_offset % size) != 0) {
291 tab_index = offset / MSIX_TABLE_ENTRY_SIZE;
293 if (tab_index < pi->pi_msix.table_count) {
294 /* valid MSI-X Table access */
295 dest = (char *)(pi->pi_msix.table + tab_index);
296 dest += msix_entry_offset;
299 retval = *((uint8_t *)dest);
301 retval = *((uint32_t *)dest);
303 retval = *((uint64_t *)dest);
304 } else if (pci_valid_pba_offset(pi, offset)) {
305 /* return 0 for PBA access */
313 pci_msix_table_bar(struct pci_devinst *pi)
316 if (pi->pi_msix.table != NULL)
317 return (pi->pi_msix.table_bar);
323 pci_msix_pba_bar(struct pci_devinst *pi)
326 if (pi->pi_msix.table != NULL)
327 return (pi->pi_msix.pba_bar);
333 pci_emul_io_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
334 uint32_t *eax, void *arg)
336 struct pci_devinst *pdi = arg;
337 struct pci_devemu *pe = pdi->pi_d;
341 for (i = 0; i <= PCI_BARMAX; i++) {
342 if (pdi->pi_bar[i].type == PCIBAR_IO &&
343 port >= pdi->pi_bar[i].addr &&
344 port + bytes <= pdi->pi_bar[i].addr + pdi->pi_bar[i].size) {
345 offset = port - pdi->pi_bar[i].addr;
347 *eax = (*pe->pe_barread)(ctx, vcpu, pdi, i,
350 (*pe->pe_barwrite)(ctx, vcpu, pdi, i, offset,
359 pci_emul_mem_handler(struct vmctx *ctx, int vcpu, int dir, uint64_t addr,
360 int size, uint64_t *val, void *arg1, long arg2)
362 struct pci_devinst *pdi = arg1;
363 struct pci_devemu *pe = pdi->pi_d;
365 int bidx = (int) arg2;
367 assert(bidx <= PCI_BARMAX);
368 assert(pdi->pi_bar[bidx].type == PCIBAR_MEM32 ||
369 pdi->pi_bar[bidx].type == PCIBAR_MEM64);
370 assert(addr >= pdi->pi_bar[bidx].addr &&
371 addr + size <= pdi->pi_bar[bidx].addr + pdi->pi_bar[bidx].size);
373 offset = addr - pdi->pi_bar[bidx].addr;
375 if (dir == MEM_F_WRITE) {
377 (*pe->pe_barwrite)(ctx, vcpu, pdi, bidx, offset,
378 4, *val & 0xffffffff);
379 (*pe->pe_barwrite)(ctx, vcpu, pdi, bidx, offset + 4,
382 (*pe->pe_barwrite)(ctx, vcpu, pdi, bidx, offset,
387 *val = (*pe->pe_barread)(ctx, vcpu, pdi, bidx,
389 *val |= (*pe->pe_barread)(ctx, vcpu, pdi, bidx,
390 offset + 4, 4) << 32;
392 *val = (*pe->pe_barread)(ctx, vcpu, pdi, bidx,
402 pci_emul_alloc_resource(uint64_t *baseptr, uint64_t limit, uint64_t size,
407 assert((size & (size - 1)) == 0); /* must be a power of 2 */
409 base = roundup2(*baseptr, size);
411 if (base + size <= limit) {
413 *baseptr = base + size;
420 pci_emul_alloc_bar(struct pci_devinst *pdi, int idx, enum pcibar_type type,
424 return (pci_emul_alloc_pbar(pdi, idx, 0, type, size));
428 * Register (or unregister) the MMIO or I/O region associated with the BAR
429 * register 'idx' of an emulated pci device.
432 modify_bar_registration(struct pci_devinst *pi, int idx, int registration)
435 struct inout_port iop;
438 switch (pi->pi_bar[idx].type) {
440 bzero(&iop, sizeof(struct inout_port));
441 iop.name = pi->pi_name;
442 iop.port = pi->pi_bar[idx].addr;
443 iop.size = pi->pi_bar[idx].size;
445 iop.flags = IOPORT_F_INOUT;
446 iop.handler = pci_emul_io_handler;
448 error = register_inout(&iop);
450 error = unregister_inout(&iop);
454 bzero(&mr, sizeof(struct mem_range));
455 mr.name = pi->pi_name;
456 mr.base = pi->pi_bar[idx].addr;
457 mr.size = pi->pi_bar[idx].size;
460 mr.handler = pci_emul_mem_handler;
463 error = register_mem(&mr);
465 error = unregister_mem(&mr);
475 unregister_bar(struct pci_devinst *pi, int idx)
478 modify_bar_registration(pi, idx, 0);
482 register_bar(struct pci_devinst *pi, int idx)
485 modify_bar_registration(pi, idx, 1);
488 /* Are we decoding i/o port accesses for the emulated pci device? */
490 porten(struct pci_devinst *pi)
494 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND);
496 return (cmd & PCIM_CMD_PORTEN);
499 /* Are we decoding memory accesses for the emulated pci device? */
501 memen(struct pci_devinst *pi)
505 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND);
507 return (cmd & PCIM_CMD_MEMEN);
511 * Update the MMIO or I/O address that is decoded by the BAR register.
513 * If the pci device has enabled the address space decoding then intercept
514 * the address range decoded by the BAR register.
517 update_bar_address(struct pci_devinst *pi, uint64_t addr, int idx, int type)
521 if (pi->pi_bar[idx].type == PCIBAR_IO)
527 unregister_bar(pi, idx);
532 pi->pi_bar[idx].addr = addr;
535 pi->pi_bar[idx].addr &= ~0xffffffffUL;
536 pi->pi_bar[idx].addr |= addr;
539 pi->pi_bar[idx].addr &= 0xffffffff;
540 pi->pi_bar[idx].addr |= addr;
547 register_bar(pi, idx);
551 pci_emul_alloc_pbar(struct pci_devinst *pdi, int idx, uint64_t hostbase,
552 enum pcibar_type type, uint64_t size)
555 uint64_t *baseptr, limit, addr, mask, lobits, bar;
557 assert(idx >= 0 && idx <= PCI_BARMAX);
559 if ((size & (size - 1)) != 0)
560 size = 1UL << flsl(size); /* round up to a power of 2 */
562 /* Enforce minimum BAR sizes required by the PCI standard */
563 if (type == PCIBAR_IO) {
574 addr = mask = lobits = 0;
577 baseptr = &pci_emul_iobase;
578 limit = PCI_EMUL_IOLIMIT;
579 mask = PCIM_BAR_IO_BASE;
580 lobits = PCIM_BAR_IO_SPACE;
585 * Some drivers do not work well if the 64-bit BAR is allocated
586 * above 4GB. Allow for this by allocating small requests under
587 * 4GB unless then allocation size is larger than some arbitrary
588 * number (32MB currently).
590 if (size > 32 * 1024 * 1024) {
592 * XXX special case for device requiring peer-peer DMA
594 if (size == 0x100000000UL)
597 baseptr = &pci_emul_membase64;
598 limit = PCI_EMUL_MEMLIMIT64;
599 mask = PCIM_BAR_MEM_BASE;
600 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64 |
601 PCIM_BAR_MEM_PREFETCH;
604 baseptr = &pci_emul_membase32;
605 limit = PCI_EMUL_MEMLIMIT32;
606 mask = PCIM_BAR_MEM_BASE;
607 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64;
611 baseptr = &pci_emul_membase32;
612 limit = PCI_EMUL_MEMLIMIT32;
613 mask = PCIM_BAR_MEM_BASE;
614 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_32;
617 printf("pci_emul_alloc_base: invalid bar type %d\n", type);
621 if (baseptr != NULL) {
622 error = pci_emul_alloc_resource(baseptr, limit, size, &addr);
627 pdi->pi_bar[idx].type = type;
628 pdi->pi_bar[idx].addr = addr;
629 pdi->pi_bar[idx].size = size;
631 /* Initialize the BAR register in config space */
632 bar = (addr & mask) | lobits;
633 pci_set_cfgdata32(pdi, PCIR_BAR(idx), bar);
635 if (type == PCIBAR_MEM64) {
636 assert(idx + 1 <= PCI_BARMAX);
637 pdi->pi_bar[idx + 1].type = PCIBAR_MEMHI64;
638 pci_set_cfgdata32(pdi, PCIR_BAR(idx + 1), bar >> 32);
641 register_bar(pdi, idx);
646 #define CAP_START_OFFSET 0x40
648 pci_emul_add_capability(struct pci_devinst *pi, u_char *capdata, int caplen)
650 int i, capoff, reallen;
655 reallen = roundup2(caplen, 4); /* dword aligned */
657 sts = pci_get_cfgdata16(pi, PCIR_STATUS);
658 if ((sts & PCIM_STATUS_CAPPRESENT) == 0)
659 capoff = CAP_START_OFFSET;
661 capoff = pi->pi_capend + 1;
663 /* Check if we have enough space */
664 if (capoff + reallen > PCI_REGMAX + 1)
667 /* Set the previous capability pointer */
668 if ((sts & PCIM_STATUS_CAPPRESENT) == 0) {
669 pci_set_cfgdata8(pi, PCIR_CAP_PTR, capoff);
670 pci_set_cfgdata16(pi, PCIR_STATUS, sts|PCIM_STATUS_CAPPRESENT);
672 pci_set_cfgdata8(pi, pi->pi_prevcap + 1, capoff);
674 /* Copy the capability */
675 for (i = 0; i < caplen; i++)
676 pci_set_cfgdata8(pi, capoff + i, capdata[i]);
678 /* Set the next capability pointer */
679 pci_set_cfgdata8(pi, capoff + 1, 0);
681 pi->pi_prevcap = capoff;
682 pi->pi_capend = capoff + reallen - 1;
686 static struct pci_devemu *
687 pci_emul_finddev(char *name)
689 struct pci_devemu **pdpp, *pdp;
691 SET_FOREACH(pdpp, pci_devemu_set) {
693 if (!strcmp(pdp->pe_emu, name)) {
702 pci_emul_init(struct vmctx *ctx, struct pci_devemu *pde, int bus, int slot,
703 int func, struct funcinfo *fi)
705 struct pci_devinst *pdi;
708 pdi = malloc(sizeof(struct pci_devinst));
709 bzero(pdi, sizeof(*pdi));
715 pthread_mutex_init(&pdi->pi_lintr.lock, NULL);
716 pdi->pi_lintr.pin = 0;
717 pdi->pi_lintr.state = IDLE;
718 pdi->pi_lintr.ioapic_irq = 0;
720 snprintf(pdi->pi_name, PI_NAMESZ, "%s-pci-%d", pde->pe_emu, slot);
722 /* Disable legacy interrupts */
723 pci_set_cfgdata8(pdi, PCIR_INTLINE, 255);
724 pci_set_cfgdata8(pdi, PCIR_INTPIN, 0);
726 pci_set_cfgdata8(pdi, PCIR_COMMAND,
727 PCIM_CMD_PORTEN | PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN);
729 err = (*pde->pe_init)(ctx, pdi, fi->fi_param);
739 pci_populate_msicap(struct msicap *msicap, int msgnum, int nextptr)
743 CTASSERT(sizeof(struct msicap) == 14);
745 /* Number of msi messages must be a power of 2 between 1 and 32 */
746 assert((msgnum & (msgnum - 1)) == 0 && msgnum >= 1 && msgnum <= 32);
747 mmc = ffs(msgnum) - 1;
749 bzero(msicap, sizeof(struct msicap));
750 msicap->capid = PCIY_MSI;
751 msicap->nextptr = nextptr;
752 msicap->msgctrl = PCIM_MSICTRL_64BIT | (mmc << 1);
756 pci_emul_add_msicap(struct pci_devinst *pi, int msgnum)
758 struct msicap msicap;
760 pci_populate_msicap(&msicap, msgnum, 0);
762 return (pci_emul_add_capability(pi, (u_char *)&msicap, sizeof(msicap)));
766 pci_populate_msixcap(struct msixcap *msixcap, int msgnum, int barnum,
767 uint32_t msix_tab_size)
769 CTASSERT(sizeof(struct msixcap) == 12);
771 assert(msix_tab_size % 4096 == 0);
773 bzero(msixcap, sizeof(struct msixcap));
774 msixcap->capid = PCIY_MSIX;
777 * Message Control Register, all fields set to
778 * zero except for the Table Size.
779 * Note: Table size N is encoded as N-1
781 msixcap->msgctrl = msgnum - 1;
785 * - MSI-X table start at offset 0
786 * - PBA table starts at a 4K aligned offset after the MSI-X table
788 msixcap->table_info = barnum & PCIM_MSIX_BIR_MASK;
789 msixcap->pba_info = msix_tab_size | (barnum & PCIM_MSIX_BIR_MASK);
793 pci_msix_table_init(struct pci_devinst *pi, int table_entries)
797 assert(table_entries > 0);
798 assert(table_entries <= MAX_MSIX_TABLE_ENTRIES);
800 table_size = table_entries * MSIX_TABLE_ENTRY_SIZE;
801 pi->pi_msix.table = malloc(table_size);
802 bzero(pi->pi_msix.table, table_size);
804 /* set mask bit of vector control register */
805 for (i = 0; i < table_entries; i++)
806 pi->pi_msix.table[i].vector_control |= PCIM_MSIX_VCTRL_MASK;
810 pci_emul_add_msixcap(struct pci_devinst *pi, int msgnum, int barnum)
813 struct msixcap msixcap;
815 assert(msgnum >= 1 && msgnum <= MAX_MSIX_TABLE_ENTRIES);
816 assert(barnum >= 0 && barnum <= PCIR_MAX_BAR_0);
818 tab_size = msgnum * MSIX_TABLE_ENTRY_SIZE;
820 /* Align table size to nearest 4K */
821 tab_size = roundup2(tab_size, 4096);
823 pi->pi_msix.table_bar = barnum;
824 pi->pi_msix.pba_bar = barnum;
825 pi->pi_msix.table_offset = 0;
826 pi->pi_msix.table_count = msgnum;
827 pi->pi_msix.pba_offset = tab_size;
828 pi->pi_msix.pba_size = PBA_SIZE(msgnum);
830 pci_msix_table_init(pi, msgnum);
832 pci_populate_msixcap(&msixcap, msgnum, barnum, tab_size);
834 /* allocate memory for MSI-X Table and PBA */
835 pci_emul_alloc_bar(pi, barnum, PCIBAR_MEM32,
836 tab_size + pi->pi_msix.pba_size);
838 return (pci_emul_add_capability(pi, (u_char *)&msixcap,
843 msixcap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
844 int bytes, uint32_t val)
846 uint16_t msgctrl, rwmask;
849 off = offset - capoff;
850 table_bar = pi->pi_msix.table_bar;
851 /* Message Control Register */
852 if (off == 2 && bytes == 2) {
853 rwmask = PCIM_MSIXCTRL_MSIX_ENABLE | PCIM_MSIXCTRL_FUNCTION_MASK;
854 msgctrl = pci_get_cfgdata16(pi, offset);
856 msgctrl |= val & rwmask;
859 pi->pi_msix.enabled = val & PCIM_MSIXCTRL_MSIX_ENABLE;
860 pi->pi_msix.function_mask = val & PCIM_MSIXCTRL_FUNCTION_MASK;
861 pci_lintr_update(pi);
864 CFGWRITE(pi, offset, val, bytes);
868 msicap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
869 int bytes, uint32_t val)
871 uint16_t msgctrl, rwmask, msgdata, mme;
875 * If guest is writing to the message control register make sure
876 * we do not overwrite read-only fields.
878 if ((offset - capoff) == 2 && bytes == 2) {
879 rwmask = PCIM_MSICTRL_MME_MASK | PCIM_MSICTRL_MSI_ENABLE;
880 msgctrl = pci_get_cfgdata16(pi, offset);
882 msgctrl |= val & rwmask;
885 addrlo = pci_get_cfgdata32(pi, capoff + 4);
886 if (msgctrl & PCIM_MSICTRL_64BIT)
887 msgdata = pci_get_cfgdata16(pi, capoff + 12);
889 msgdata = pci_get_cfgdata16(pi, capoff + 8);
891 mme = msgctrl & PCIM_MSICTRL_MME_MASK;
892 pi->pi_msi.enabled = msgctrl & PCIM_MSICTRL_MSI_ENABLE ? 1 : 0;
893 if (pi->pi_msi.enabled) {
894 pi->pi_msi.addr = addrlo;
895 pi->pi_msi.msg_data = msgdata;
896 pi->pi_msi.maxmsgnum = 1 << (mme >> 4);
898 pi->pi_msi.maxmsgnum = 0;
900 pci_lintr_update(pi);
903 CFGWRITE(pi, offset, val, bytes);
907 pciecap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
908 int bytes, uint32_t val)
911 /* XXX don't write to the readonly parts */
912 CFGWRITE(pi, offset, val, bytes);
915 #define PCIECAP_VERSION 0x2
917 pci_emul_add_pciecap(struct pci_devinst *pi, int type)
920 struct pciecap pciecap;
922 CTASSERT(sizeof(struct pciecap) == 60);
924 if (type != PCIEM_TYPE_ROOT_PORT)
927 bzero(&pciecap, sizeof(pciecap));
929 pciecap.capid = PCIY_EXPRESS;
930 pciecap.pcie_capabilities = PCIECAP_VERSION | PCIEM_TYPE_ROOT_PORT;
931 pciecap.link_capabilities = 0x411; /* gen1, x1 */
932 pciecap.link_status = 0x11; /* gen1, x1 */
934 err = pci_emul_add_capability(pi, (u_char *)&pciecap, sizeof(pciecap));
939 * This function assumes that 'coff' is in the capabilities region of the
943 pci_emul_capwrite(struct pci_devinst *pi, int offset, int bytes, uint32_t val)
946 uint8_t capoff, nextoff;
948 /* Do not allow un-aligned writes */
949 if ((offset & (bytes - 1)) != 0)
952 /* Find the capability that we want to update */
953 capoff = CAP_START_OFFSET;
955 nextoff = pci_get_cfgdata8(pi, capoff + 1);
958 if (offset >= capoff && offset < nextoff)
963 assert(offset >= capoff);
966 * Capability ID and Next Capability Pointer are readonly.
967 * However, some o/s's do 4-byte writes that include these.
968 * For this case, trim the write back to 2 bytes and adjust
971 if (offset == capoff || offset == capoff + 1) {
972 if (offset == capoff && bytes == 4) {
980 capid = pci_get_cfgdata8(pi, capoff);
983 msicap_cfgwrite(pi, capoff, offset, bytes, val);
986 msixcap_cfgwrite(pi, capoff, offset, bytes, val);
989 pciecap_cfgwrite(pi, capoff, offset, bytes, val);
997 pci_emul_iscap(struct pci_devinst *pi, int offset)
1001 sts = pci_get_cfgdata16(pi, PCIR_STATUS);
1002 if ((sts & PCIM_STATUS_CAPPRESENT) != 0) {
1003 if (offset >= CAP_START_OFFSET && offset <= pi->pi_capend)
1010 pci_emul_fallback_handler(struct vmctx *ctx, int vcpu, int dir, uint64_t addr,
1011 int size, uint64_t *val, void *arg1, long arg2)
1014 * Ignore writes; return 0xff's for reads. The mem read code
1015 * will take care of truncating to the correct size.
1017 if (dir == MEM_F_READ) {
1018 *val = 0xffffffffffffffff;
1024 #define BUSIO_ROUNDUP 32
1025 #define BUSMEM_ROUNDUP (1024 * 1024)
1028 init_pci(struct vmctx *ctx)
1030 struct pci_devemu *pde;
1032 struct slotinfo *si;
1033 struct funcinfo *fi;
1035 int bus, slot, func;
1038 pci_emul_iobase = PCI_EMUL_IOBASE;
1039 pci_emul_membase32 = vm_get_lowmem_limit(ctx);
1040 pci_emul_membase64 = PCI_EMUL_MEMBASE64;
1042 for (bus = 0; bus < MAXBUSES; bus++) {
1043 if ((bi = pci_businfo[bus]) == NULL)
1046 * Keep track of the i/o and memory resources allocated to
1049 bi->iobase = pci_emul_iobase;
1050 bi->membase32 = pci_emul_membase32;
1051 bi->membase64 = pci_emul_membase64;
1053 for (slot = 0; slot < MAXSLOTS; slot++) {
1054 si = &bi->slotinfo[slot];
1055 for (func = 0; func < MAXFUNCS; func++) {
1056 fi = &si->si_funcs[func];
1057 if (fi->fi_name == NULL)
1059 pde = pci_emul_finddev(fi->fi_name);
1060 assert(pde != NULL);
1061 error = pci_emul_init(ctx, pde, bus, slot,
1069 * Add some slop to the I/O and memory resources decoded by
1070 * this bus to give a guest some flexibility if it wants to
1071 * reprogram the BARs.
1073 pci_emul_iobase += BUSIO_ROUNDUP;
1074 pci_emul_iobase = roundup2(pci_emul_iobase, BUSIO_ROUNDUP);
1075 bi->iolimit = pci_emul_iobase;
1077 pci_emul_membase32 += BUSMEM_ROUNDUP;
1078 pci_emul_membase32 = roundup2(pci_emul_membase32,
1080 bi->memlimit32 = pci_emul_membase32;
1082 pci_emul_membase64 += BUSMEM_ROUNDUP;
1083 pci_emul_membase64 = roundup2(pci_emul_membase64,
1085 bi->memlimit64 = pci_emul_membase64;
1089 * The guest physical memory map looks like the following:
1090 * [0, lowmem) guest system memory
1091 * [lowmem, lowmem_limit) memory hole (may be absent)
1092 * [lowmem_limit, 4GB) PCI hole (32-bit BAR allocation)
1093 * [4GB, 4GB + highmem)
1095 * Accesses to memory addresses that are not allocated to system
1096 * memory or PCI devices return 0xff's.
1098 error = vm_get_memory_seg(ctx, 0, &lowmem, NULL);
1101 memset(&pci_mem_hole, 0, sizeof(struct mem_range));
1102 pci_mem_hole.name = "PCI hole";
1103 pci_mem_hole.flags = MEM_F_RW;
1104 pci_mem_hole.base = lowmem;
1105 pci_mem_hole.size = (4ULL * 1024 * 1024 * 1024) - lowmem;
1106 pci_mem_hole.handler = pci_emul_fallback_handler;
1108 error = register_mem_fallback(&pci_mem_hole);
1115 pci_prt_entry(int bus, int slot, int pin, int ioapic_irq, void *arg)
1120 dsdt_line(" Package (0x04)");
1122 dsdt_line(" 0x%X,", slot << 16 | 0xffff);
1123 dsdt_line(" 0x%02X,", pin - 1);
1124 dsdt_line(" Zero,");
1125 dsdt_line(" 0x%X", ioapic_irq);
1126 dsdt_line(" }%s", *count == 1 ? "" : ",");
1131 * A bhyve virtual machine has a flat PCI hierarchy with a root port
1132 * corresponding to each PCI bus.
1135 pci_bus_write_dsdt(int bus)
1138 struct slotinfo *si;
1139 struct pci_devinst *pi;
1140 int count, slot, func;
1143 * If there are no devices on this 'bus' then just return.
1145 if ((bi = pci_businfo[bus]) == NULL) {
1147 * Bus 0 is special because it decodes the I/O ports used
1148 * for PCI config space access even if there are no devices
1156 dsdt_line("Scope (_SB)");
1158 dsdt_line(" Device (PC%02X)", bus);
1160 dsdt_line(" Name (_HID, EisaId (\"PNP0A03\"))");
1161 dsdt_line(" Name (_ADR, Zero)");
1163 dsdt_line(" Method (_BBN, 0, NotSerialized)");
1165 dsdt_line(" Return (0x%08X)", bus);
1167 dsdt_line(" Name (_CRS, ResourceTemplate ()");
1169 dsdt_line(" WordBusNumber (ResourceProducer, MinFixed, "
1170 "MaxFixed, PosDecode,");
1171 dsdt_line(" 0x0000, // Granularity");
1172 dsdt_line(" 0x%04X, // Range Minimum", bus);
1173 dsdt_line(" 0x%04X, // Range Maximum", bus);
1174 dsdt_line(" 0x0000, // Translation Offset");
1175 dsdt_line(" 0x0001, // Length");
1180 dsdt_fixed_ioport(0xCF8, 8);
1183 dsdt_line(" WordIO (ResourceProducer, MinFixed, MaxFixed, "
1184 "PosDecode, EntireRange,");
1185 dsdt_line(" 0x0000, // Granularity");
1186 dsdt_line(" 0x0000, // Range Minimum");
1187 dsdt_line(" 0x0CF7, // Range Maximum");
1188 dsdt_line(" 0x0000, // Translation Offset");
1189 dsdt_line(" 0x0CF8, // Length");
1190 dsdt_line(" ,, , TypeStatic)");
1192 dsdt_line(" WordIO (ResourceProducer, MinFixed, MaxFixed, "
1193 "PosDecode, EntireRange,");
1194 dsdt_line(" 0x0000, // Granularity");
1195 dsdt_line(" 0x0D00, // Range Minimum");
1196 dsdt_line(" 0x%04X, // Range Maximum",
1197 PCI_EMUL_IOBASE - 1);
1198 dsdt_line(" 0x0000, // Translation Offset");
1199 dsdt_line(" 0x%04X, // Length",
1200 PCI_EMUL_IOBASE - 0x0D00);
1201 dsdt_line(" ,, , TypeStatic)");
1211 dsdt_line(" WordIO (ResourceProducer, MinFixed, MaxFixed, "
1212 "PosDecode, EntireRange,");
1213 dsdt_line(" 0x0000, // Granularity");
1214 dsdt_line(" 0x%04X, // Range Minimum", bi->iobase);
1215 dsdt_line(" 0x%04X, // Range Maximum",
1217 dsdt_line(" 0x0000, // Translation Offset");
1218 dsdt_line(" 0x%04X, // Length",
1219 bi->iolimit - bi->iobase);
1220 dsdt_line(" ,, , TypeStatic)");
1222 /* mmio window (32-bit) */
1223 dsdt_line(" DWordMemory (ResourceProducer, PosDecode, "
1224 "MinFixed, MaxFixed, NonCacheable, ReadWrite,");
1225 dsdt_line(" 0x00000000, // Granularity");
1226 dsdt_line(" 0x%08X, // Range Minimum\n", bi->membase32);
1227 dsdt_line(" 0x%08X, // Range Maximum\n",
1228 bi->memlimit32 - 1);
1229 dsdt_line(" 0x00000000, // Translation Offset");
1230 dsdt_line(" 0x%08X, // Length\n",
1231 bi->memlimit32 - bi->membase32);
1232 dsdt_line(" ,, , AddressRangeMemory, TypeStatic)");
1234 /* mmio window (64-bit) */
1235 dsdt_line(" QWordMemory (ResourceProducer, PosDecode, "
1236 "MinFixed, MaxFixed, NonCacheable, ReadWrite,");
1237 dsdt_line(" 0x0000000000000000, // Granularity");
1238 dsdt_line(" 0x%016lX, // Range Minimum\n", bi->membase64);
1239 dsdt_line(" 0x%016lX, // Range Maximum\n",
1240 bi->memlimit64 - 1);
1241 dsdt_line(" 0x0000000000000000, // Translation Offset");
1242 dsdt_line(" 0x%016lX, // Length\n",
1243 bi->memlimit64 - bi->membase64);
1244 dsdt_line(" ,, , AddressRangeMemory, TypeStatic)");
1247 count = pci_count_lintr(bus);
1250 dsdt_line("Name (_PRT, Package (0x%02X)", count);
1252 pci_walk_lintr(bus, pci_prt_entry, &count);
1258 for (slot = 0; slot < MAXSLOTS; slot++) {
1259 si = &bi->slotinfo[slot];
1260 for (func = 0; func < MAXFUNCS; func++) {
1261 pi = si->si_funcs[func].fi_devi;
1262 if (pi != NULL && pi->pi_d->pe_write_dsdt != NULL)
1263 pi->pi_d->pe_write_dsdt(pi);
1274 pci_write_dsdt(void)
1278 for (bus = 0; bus < MAXBUSES; bus++)
1279 pci_bus_write_dsdt(bus);
1283 pci_bus_configured(int bus)
1285 assert(bus >= 0 && bus < MAXBUSES);
1286 return (pci_businfo[bus] != NULL);
1290 pci_msi_enabled(struct pci_devinst *pi)
1292 return (pi->pi_msi.enabled);
1296 pci_msi_maxmsgnum(struct pci_devinst *pi)
1298 if (pi->pi_msi.enabled)
1299 return (pi->pi_msi.maxmsgnum);
1305 pci_msix_enabled(struct pci_devinst *pi)
1308 return (pi->pi_msix.enabled && !pi->pi_msi.enabled);
1312 pci_generate_msix(struct pci_devinst *pi, int index)
1314 struct msix_table_entry *mte;
1316 if (!pci_msix_enabled(pi))
1319 if (pi->pi_msix.function_mask)
1322 if (index >= pi->pi_msix.table_count)
1325 mte = &pi->pi_msix.table[index];
1326 if ((mte->vector_control & PCIM_MSIX_VCTRL_MASK) == 0) {
1327 /* XXX Set PBA bit if interrupt is disabled */
1328 vm_lapic_msi(pi->pi_vmctx, mte->addr, mte->msg_data);
1333 pci_generate_msi(struct pci_devinst *pi, int index)
1336 if (pci_msi_enabled(pi) && index < pci_msi_maxmsgnum(pi)) {
1337 vm_lapic_msi(pi->pi_vmctx, pi->pi_msi.addr,
1338 pi->pi_msi.msg_data + index);
1343 pci_lintr_permitted(struct pci_devinst *pi)
1347 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND);
1348 return (!(pi->pi_msi.enabled || pi->pi_msix.enabled ||
1349 (cmd & PCIM_CMD_INTxDIS)));
1353 pci_lintr_request(struct pci_devinst *pi)
1356 struct slotinfo *si;
1357 int bestpin, bestcount, irq, pin;
1359 bi = pci_businfo[pi->pi_bus];
1363 * First, allocate a pin from our slot.
1365 si = &bi->slotinfo[pi->pi_slot];
1367 bestcount = si->si_intpins[0].ii_count;
1368 for (pin = 1; pin < 4; pin++) {
1369 if (si->si_intpins[pin].ii_count < bestcount) {
1371 bestcount = si->si_intpins[pin].ii_count;
1376 * Attempt to allocate an I/O APIC pin for this intpin. If
1377 * 8259A support is added we will need a separate field to
1378 * assign the intpin to an input pin on the PCI interrupt
1381 if (si->si_intpins[bestpin].ii_count == 0) {
1382 irq = ioapic_pci_alloc_irq();
1385 si->si_intpins[bestpin].ii_ioapic_irq = irq;
1387 irq = si->si_intpins[bestpin].ii_ioapic_irq;
1388 si->si_intpins[bestpin].ii_count++;
1390 pi->pi_lintr.pin = bestpin + 1;
1391 pi->pi_lintr.ioapic_irq = irq;
1392 pci_set_cfgdata8(pi, PCIR_INTLINE, irq);
1393 pci_set_cfgdata8(pi, PCIR_INTPIN, bestpin + 1);
1398 pci_lintr_assert(struct pci_devinst *pi)
1401 assert(pi->pi_lintr.pin > 0);
1403 pthread_mutex_lock(&pi->pi_lintr.lock);
1404 if (pi->pi_lintr.state == IDLE) {
1405 if (pci_lintr_permitted(pi)) {
1406 pi->pi_lintr.state = ASSERTED;
1407 vm_ioapic_assert_irq(pi->pi_vmctx,
1408 pi->pi_lintr.ioapic_irq);
1410 pi->pi_lintr.state = PENDING;
1412 pthread_mutex_unlock(&pi->pi_lintr.lock);
1416 pci_lintr_deassert(struct pci_devinst *pi)
1419 assert(pi->pi_lintr.pin > 0);
1421 pthread_mutex_lock(&pi->pi_lintr.lock);
1422 if (pi->pi_lintr.state == ASSERTED) {
1423 pi->pi_lintr.state = IDLE;
1424 vm_ioapic_deassert_irq(pi->pi_vmctx, pi->pi_lintr.ioapic_irq);
1425 } else if (pi->pi_lintr.state == PENDING)
1426 pi->pi_lintr.state = IDLE;
1427 pthread_mutex_unlock(&pi->pi_lintr.lock);
1431 pci_lintr_update(struct pci_devinst *pi)
1434 pthread_mutex_lock(&pi->pi_lintr.lock);
1435 if (pi->pi_lintr.state == ASSERTED && !pci_lintr_permitted(pi)) {
1436 vm_ioapic_deassert_irq(pi->pi_vmctx, pi->pi_lintr.ioapic_irq);
1437 pi->pi_lintr.state = PENDING;
1438 } else if (pi->pi_lintr.state == PENDING && pci_lintr_permitted(pi)) {
1439 pi->pi_lintr.state = ASSERTED;
1440 vm_ioapic_assert_irq(pi->pi_vmctx, pi->pi_lintr.ioapic_irq);
1442 pthread_mutex_unlock(&pi->pi_lintr.lock);
1446 pci_count_lintr(int bus)
1448 int count, slot, pin;
1449 struct slotinfo *slotinfo;
1452 if (pci_businfo[bus] != NULL) {
1453 for (slot = 0; slot < MAXSLOTS; slot++) {
1454 slotinfo = &pci_businfo[bus]->slotinfo[slot];
1455 for (pin = 0; pin < 4; pin++) {
1456 if (slotinfo->si_intpins[pin].ii_count != 0)
1465 pci_walk_lintr(int bus, pci_lintr_cb cb, void *arg)
1468 struct slotinfo *si;
1469 struct intxinfo *ii;
1472 if ((bi = pci_businfo[bus]) == NULL)
1475 for (slot = 0; slot < MAXSLOTS; slot++) {
1476 si = &bi->slotinfo[slot];
1477 for (pin = 0; pin < 4; pin++) {
1478 ii = &si->si_intpins[pin];
1479 if (ii->ii_count != 0)
1480 cb(bus, slot, pin + 1, ii->ii_ioapic_irq, arg);
1486 * Return 1 if the emulated device in 'slot' is a multi-function device.
1487 * Return 0 otherwise.
1490 pci_emul_is_mfdev(int bus, int slot)
1493 struct slotinfo *si;
1497 if ((bi = pci_businfo[bus]) != NULL) {
1498 si = &bi->slotinfo[slot];
1499 for (f = 0; f < MAXFUNCS; f++) {
1500 if (si->si_funcs[f].fi_devi != NULL) {
1505 return (numfuncs > 1);
1509 * Ensure that the PCIM_MFDEV bit is properly set (or unset) depending on
1510 * whether or not is a multi-function being emulated in the pci 'slot'.
1513 pci_emul_hdrtype_fixup(int bus, int slot, int off, int bytes, uint32_t *rv)
1517 if (off <= PCIR_HDRTYPE && off + bytes > PCIR_HDRTYPE) {
1518 mfdev = pci_emul_is_mfdev(bus, slot);
1528 *rv &= ~(PCIM_MFDEV << 16);
1530 *rv |= (PCIM_MFDEV << 16);
1537 static int cfgenable, cfgbus, cfgslot, cfgfunc, cfgoff;
1540 pci_emul_cfgaddr(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
1541 uint32_t *eax, void *arg)
1547 *eax = (bytes == 2) ? 0xffff : 0xff;
1552 x = (cfgbus << 16) |
1561 cfgenable = (x & CONF1_ENABLE) == CONF1_ENABLE;
1562 cfgoff = x & PCI_REGMAX;
1563 cfgfunc = (x >> 8) & PCI_FUNCMAX;
1564 cfgslot = (x >> 11) & PCI_SLOTMAX;
1565 cfgbus = (x >> 16) & PCI_BUSMAX;
1570 INOUT_PORT(pci_cfgaddr, CONF1_ADDR_PORT, IOPORT_F_INOUT, pci_emul_cfgaddr);
1573 bits_changed(uint32_t old, uint32_t new, uint32_t mask)
1576 return ((old ^ new) & mask);
1580 pci_emul_cmdwrite(struct pci_devinst *pi, uint32_t new, int bytes)
1586 * The command register is at an offset of 4 bytes and thus the
1587 * guest could write 1, 2 or 4 bytes starting at this offset.
1590 old = pci_get_cfgdata16(pi, PCIR_COMMAND); /* stash old value */
1591 CFGWRITE(pi, PCIR_COMMAND, new, bytes); /* update config */
1592 new = pci_get_cfgdata16(pi, PCIR_COMMAND); /* get updated value */
1595 * If the MMIO or I/O address space decoding has changed then
1596 * register/unregister all BARs that decode that address space.
1598 for (i = 0; i <= PCI_BARMAX; i++) {
1599 switch (pi->pi_bar[i].type) {
1601 case PCIBAR_MEMHI64:
1604 /* I/O address space decoding changed? */
1605 if (bits_changed(old, new, PCIM_CMD_PORTEN)) {
1607 register_bar(pi, i);
1609 unregister_bar(pi, i);
1614 /* MMIO address space decoding changed? */
1615 if (bits_changed(old, new, PCIM_CMD_MEMEN)) {
1617 register_bar(pi, i);
1619 unregister_bar(pi, i);
1628 * If INTx has been unmasked and is pending, assert the
1631 pci_lintr_update(pi);
1635 pci_emul_cfgdata(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
1636 uint32_t *eax, void *arg)
1639 struct slotinfo *si;
1640 struct pci_devinst *pi;
1641 struct pci_devemu *pe;
1642 int coff, idx, needcfg;
1643 uint64_t addr, bar, mask;
1645 assert(bytes == 1 || bytes == 2 || bytes == 4);
1647 if ((bi = pci_businfo[cfgbus]) != NULL) {
1648 si = &bi->slotinfo[cfgslot];
1649 pi = si->si_funcs[cfgfunc].fi_devi;
1653 coff = cfgoff + (port - CONF1_DATA_PORT);
1656 printf("pcicfg-%s from 0x%0x of %d bytes (%d/%d/%d)\n\r",
1657 in ? "read" : "write", coff, bytes, cfgbus, cfgslot, cfgfunc);
1661 * Just return if there is no device at this cfgslot:cfgfunc,
1662 * if the guest is doing an un-aligned access, or if the config
1663 * address word isn't enabled.
1665 if (!cfgenable || pi == NULL || (coff & (bytes - 1)) != 0) {
1677 /* Let the device emulation override the default handler */
1678 if (pe->pe_cfgread != NULL) {
1679 needcfg = pe->pe_cfgread(ctx, vcpu, pi,
1687 *eax = pci_get_cfgdata8(pi, coff);
1688 else if (bytes == 2)
1689 *eax = pci_get_cfgdata16(pi, coff);
1691 *eax = pci_get_cfgdata32(pi, coff);
1694 pci_emul_hdrtype_fixup(cfgbus, cfgslot, coff, bytes, eax);
1696 /* Let the device emulation override the default handler */
1697 if (pe->pe_cfgwrite != NULL &&
1698 (*pe->pe_cfgwrite)(ctx, vcpu, pi, coff, bytes, *eax) == 0)
1702 * Special handling for write to BAR registers
1704 if (coff >= PCIR_BAR(0) && coff < PCIR_BAR(PCI_BARMAX + 1)) {
1706 * Ignore writes to BAR registers that are not
1709 if (bytes != 4 || (coff & 0x3) != 0)
1711 idx = (coff - PCIR_BAR(0)) / 4;
1712 mask = ~(pi->pi_bar[idx].size - 1);
1713 switch (pi->pi_bar[idx].type) {
1715 pi->pi_bar[idx].addr = bar = 0;
1720 bar = addr | PCIM_BAR_IO_SPACE;
1722 * Register the new BAR value for interception
1724 if (addr != pi->pi_bar[idx].addr) {
1725 update_bar_address(pi, addr, idx,
1730 addr = bar = *eax & mask;
1731 bar |= PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_32;
1732 if (addr != pi->pi_bar[idx].addr) {
1733 update_bar_address(pi, addr, idx,
1738 addr = bar = *eax & mask;
1739 bar |= PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64 |
1740 PCIM_BAR_MEM_PREFETCH;
1741 if (addr != (uint32_t)pi->pi_bar[idx].addr) {
1742 update_bar_address(pi, addr, idx,
1746 case PCIBAR_MEMHI64:
1747 mask = ~(pi->pi_bar[idx - 1].size - 1);
1748 addr = ((uint64_t)*eax << 32) & mask;
1750 if (bar != pi->pi_bar[idx - 1].addr >> 32) {
1751 update_bar_address(pi, addr, idx - 1,
1758 pci_set_cfgdata32(pi, coff, bar);
1760 } else if (pci_emul_iscap(pi, coff)) {
1761 pci_emul_capwrite(pi, coff, bytes, *eax);
1762 } else if (coff == PCIR_COMMAND) {
1763 pci_emul_cmdwrite(pi, *eax, bytes);
1765 CFGWRITE(pi, coff, *eax, bytes);
1772 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+0, IOPORT_F_INOUT, pci_emul_cfgdata);
1773 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+1, IOPORT_F_INOUT, pci_emul_cfgdata);
1774 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+2, IOPORT_F_INOUT, pci_emul_cfgdata);
1775 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+3, IOPORT_F_INOUT, pci_emul_cfgdata);
1778 * I/O ports to configure PCI IRQ routing. We ignore all writes to it.
1781 pci_irq_port_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
1782 uint32_t *eax, void *arg)
1787 INOUT_PORT(pci_irq, 0xC00, IOPORT_F_OUT, pci_irq_port_handler);
1788 INOUT_PORT(pci_irq, 0xC01, IOPORT_F_OUT, pci_irq_port_handler);
1789 SYSRES_IO(0xC00, 2);
1791 #define PCI_EMUL_TEST
1792 #ifdef PCI_EMUL_TEST
1794 * Define a dummy test device
1798 struct pci_emul_dsoftc {
1799 uint8_t ioregs[DIOSZ];
1800 uint8_t memregs[DMEMSZ];
1803 #define PCI_EMUL_MSI_MSGS 4
1804 #define PCI_EMUL_MSIX_MSGS 16
1807 pci_emul_dinit(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
1810 struct pci_emul_dsoftc *sc;
1812 sc = malloc(sizeof(struct pci_emul_dsoftc));
1813 memset(sc, 0, sizeof(struct pci_emul_dsoftc));
1817 pci_set_cfgdata16(pi, PCIR_DEVICE, 0x0001);
1818 pci_set_cfgdata16(pi, PCIR_VENDOR, 0x10DD);
1819 pci_set_cfgdata8(pi, PCIR_CLASS, 0x02);
1821 error = pci_emul_add_msicap(pi, PCI_EMUL_MSI_MSGS);
1824 error = pci_emul_alloc_bar(pi, 0, PCIBAR_IO, DIOSZ);
1827 error = pci_emul_alloc_bar(pi, 1, PCIBAR_MEM32, DMEMSZ);
1834 pci_emul_diow(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx,
1835 uint64_t offset, int size, uint64_t value)
1838 struct pci_emul_dsoftc *sc = pi->pi_arg;
1841 if (offset + size > DIOSZ) {
1842 printf("diow: iow too large, offset %ld size %d\n",
1848 sc->ioregs[offset] = value & 0xff;
1849 } else if (size == 2) {
1850 *(uint16_t *)&sc->ioregs[offset] = value & 0xffff;
1851 } else if (size == 4) {
1852 *(uint32_t *)&sc->ioregs[offset] = value;
1854 printf("diow: iow unknown size %d\n", size);
1858 * Special magic value to generate an interrupt
1860 if (offset == 4 && size == 4 && pci_msi_enabled(pi))
1861 pci_generate_msi(pi, value % pci_msi_maxmsgnum(pi));
1863 if (value == 0xabcdef) {
1864 for (i = 0; i < pci_msi_maxmsgnum(pi); i++)
1865 pci_generate_msi(pi, i);
1870 if (offset + size > DMEMSZ) {
1871 printf("diow: memw too large, offset %ld size %d\n",
1877 sc->memregs[offset] = value;
1878 } else if (size == 2) {
1879 *(uint16_t *)&sc->memregs[offset] = value;
1880 } else if (size == 4) {
1881 *(uint32_t *)&sc->memregs[offset] = value;
1882 } else if (size == 8) {
1883 *(uint64_t *)&sc->memregs[offset] = value;
1885 printf("diow: memw unknown size %d\n", size);
1889 * magic interrupt ??
1894 printf("diow: unknown bar idx %d\n", baridx);
1899 pci_emul_dior(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx,
1900 uint64_t offset, int size)
1902 struct pci_emul_dsoftc *sc = pi->pi_arg;
1906 if (offset + size > DIOSZ) {
1907 printf("dior: ior too large, offset %ld size %d\n",
1913 value = sc->ioregs[offset];
1914 } else if (size == 2) {
1915 value = *(uint16_t *) &sc->ioregs[offset];
1916 } else if (size == 4) {
1917 value = *(uint32_t *) &sc->ioregs[offset];
1919 printf("dior: ior unknown size %d\n", size);
1924 if (offset + size > DMEMSZ) {
1925 printf("dior: memr too large, offset %ld size %d\n",
1931 value = sc->memregs[offset];
1932 } else if (size == 2) {
1933 value = *(uint16_t *) &sc->memregs[offset];
1934 } else if (size == 4) {
1935 value = *(uint32_t *) &sc->memregs[offset];
1936 } else if (size == 8) {
1937 value = *(uint64_t *) &sc->memregs[offset];
1939 printf("dior: ior unknown size %d\n", size);
1945 printf("dior: unknown bar idx %d\n", baridx);
1952 struct pci_devemu pci_dummy = {
1954 .pe_init = pci_emul_dinit,
1955 .pe_barwrite = pci_emul_diow,
1956 .pe_barread = pci_emul_dior
1958 PCI_EMUL_SET(pci_dummy);
1960 #endif /* PCI_EMUL_TEST */