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>
57 #define CONF1_ADDR_PORT 0x0cf8
58 #define CONF1_DATA_PORT 0x0cfc
60 #define CONF1_ENABLE 0x80000000ul
62 #define MAXBUSES (PCI_BUSMAX + 1)
63 #define MAXSLOTS (PCI_SLOTMAX + 1)
64 #define MAXFUNCS (PCI_FUNCMAX + 1)
69 struct pci_devinst *fi_devi;
79 struct intxinfo si_intpins[4];
80 struct funcinfo si_funcs[MAXFUNCS];
84 uint16_t iobase, iolimit; /* I/O window */
85 uint32_t membase32, memlimit32; /* mmio window below 4GB */
86 uint64_t membase64, memlimit64; /* mmio window above 4GB */
87 struct slotinfo slotinfo[MAXSLOTS];
90 static struct businfo *pci_businfo[MAXBUSES];
92 SET_DECLARE(pci_devemu_set, struct pci_devemu);
94 static uint64_t pci_emul_iobase;
95 static uint64_t pci_emul_membase32;
96 static uint64_t pci_emul_membase64;
98 #define PCI_EMUL_IOBASE 0x2000
99 #define PCI_EMUL_IOLIMIT 0x10000
101 #define PCI_EMUL_ECFG_BASE 0xE0000000 /* 3.5GB */
102 #define PCI_EMUL_ECFG_SIZE (MAXBUSES * 1024 * 1024) /* 1MB per bus */
103 SYSRES_MEM(PCI_EMUL_ECFG_BASE, PCI_EMUL_ECFG_SIZE);
105 #define PCI_EMUL_MEMLIMIT32 PCI_EMUL_ECFG_BASE
107 #define PCI_EMUL_MEMBASE64 0xD000000000UL
108 #define PCI_EMUL_MEMLIMIT64 0xFD00000000UL
110 static struct pci_devemu *pci_emul_finddev(char *name);
111 static void pci_lintr_route(struct pci_devinst *pi);
112 static void pci_lintr_update(struct pci_devinst *pi);
113 static void pci_cfgrw(struct vmctx *ctx, int vcpu, int in, int bus, int slot,
114 int func, int coff, int bytes, uint32_t *val);
117 CFGWRITE(struct pci_devinst *pi, int coff, uint32_t val, int bytes)
121 pci_set_cfgdata8(pi, coff, val);
123 pci_set_cfgdata16(pi, coff, val);
125 pci_set_cfgdata32(pi, coff, val);
128 static __inline uint32_t
129 CFGREAD(struct pci_devinst *pi, int coff, int bytes)
133 return (pci_get_cfgdata8(pi, coff));
135 return (pci_get_cfgdata16(pi, coff));
137 return (pci_get_cfgdata32(pi, coff));
145 * Slot options are in the form:
147 * <bus>:<slot>:<func>,<emul>[,<config>]
148 * <slot>[:<func>],<emul>[,<config>]
152 * emul is a string describing the type of PCI device e.g. virtio-net
153 * config is an optional string, depending on the device, that can be
154 * used for configuration.
160 pci_parse_slot_usage(char *aopt)
163 fprintf(stderr, "Invalid PCI slot info field \"%s\"\n", aopt);
167 pci_parse_slot(char *opt)
171 char *emul, *config, *str, *cp;
172 int error, bnum, snum, fnum;
177 emul = config = NULL;
178 if ((cp = strchr(str, ',')) != NULL) {
181 if ((cp = strchr(emul, ',')) != NULL) {
186 pci_parse_slot_usage(opt);
190 /* <bus>:<slot>:<func> */
191 if (sscanf(str, "%d:%d:%d", &bnum, &snum, &fnum) != 3) {
194 if (sscanf(str, "%d:%d", &snum, &fnum) != 2) {
197 if (sscanf(str, "%d", &snum) != 1) {
203 if (bnum < 0 || bnum >= MAXBUSES || snum < 0 || snum >= MAXSLOTS ||
204 fnum < 0 || fnum >= MAXFUNCS) {
205 pci_parse_slot_usage(opt);
209 if (pci_businfo[bnum] == NULL)
210 pci_businfo[bnum] = calloc(1, sizeof(struct businfo));
212 bi = pci_businfo[bnum];
213 si = &bi->slotinfo[snum];
215 if (si->si_funcs[fnum].fi_name != NULL) {
216 fprintf(stderr, "pci slot %d:%d already occupied!\n",
221 if (pci_emul_finddev(emul) == NULL) {
222 fprintf(stderr, "pci slot %d:%d: unknown device \"%s\"\n",
228 si->si_funcs[fnum].fi_name = emul;
229 si->si_funcs[fnum].fi_param = config;
239 pci_valid_pba_offset(struct pci_devinst *pi, uint64_t offset)
242 if (offset < pi->pi_msix.pba_offset)
245 if (offset >= pi->pi_msix.pba_offset + pi->pi_msix.pba_size) {
253 pci_emul_msix_twrite(struct pci_devinst *pi, uint64_t offset, int size,
256 int msix_entry_offset;
260 /* support only 4 or 8 byte writes */
261 if (size != 4 && size != 8)
265 * Return if table index is beyond what device supports
267 tab_index = offset / MSIX_TABLE_ENTRY_SIZE;
268 if (tab_index >= pi->pi_msix.table_count)
271 msix_entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
273 /* support only aligned writes */
274 if ((msix_entry_offset % size) != 0)
277 dest = (char *)(pi->pi_msix.table + tab_index);
278 dest += msix_entry_offset;
281 *((uint32_t *)dest) = value;
283 *((uint64_t *)dest) = value;
289 pci_emul_msix_tread(struct pci_devinst *pi, uint64_t offset, int size)
292 int msix_entry_offset;
294 uint64_t retval = ~0;
297 * The PCI standard only allows 4 and 8 byte accesses to the MSI-X
298 * table but we also allow 1 byte access to accomodate reads from
301 if (size != 1 && size != 4 && size != 8)
304 msix_entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
306 /* support only aligned reads */
307 if ((msix_entry_offset % size) != 0) {
311 tab_index = offset / MSIX_TABLE_ENTRY_SIZE;
313 if (tab_index < pi->pi_msix.table_count) {
314 /* valid MSI-X Table access */
315 dest = (char *)(pi->pi_msix.table + tab_index);
316 dest += msix_entry_offset;
319 retval = *((uint8_t *)dest);
321 retval = *((uint32_t *)dest);
323 retval = *((uint64_t *)dest);
324 } else if (pci_valid_pba_offset(pi, offset)) {
325 /* return 0 for PBA access */
333 pci_msix_table_bar(struct pci_devinst *pi)
336 if (pi->pi_msix.table != NULL)
337 return (pi->pi_msix.table_bar);
343 pci_msix_pba_bar(struct pci_devinst *pi)
346 if (pi->pi_msix.table != NULL)
347 return (pi->pi_msix.pba_bar);
353 pci_emul_io_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
354 uint32_t *eax, void *arg)
356 struct pci_devinst *pdi = arg;
357 struct pci_devemu *pe = pdi->pi_d;
361 for (i = 0; i <= PCI_BARMAX; i++) {
362 if (pdi->pi_bar[i].type == PCIBAR_IO &&
363 port >= pdi->pi_bar[i].addr &&
364 port + bytes <= pdi->pi_bar[i].addr + pdi->pi_bar[i].size) {
365 offset = port - pdi->pi_bar[i].addr;
367 *eax = (*pe->pe_barread)(ctx, vcpu, pdi, i,
370 (*pe->pe_barwrite)(ctx, vcpu, pdi, i, offset,
379 pci_emul_mem_handler(struct vmctx *ctx, int vcpu, int dir, uint64_t addr,
380 int size, uint64_t *val, void *arg1, long arg2)
382 struct pci_devinst *pdi = arg1;
383 struct pci_devemu *pe = pdi->pi_d;
385 int bidx = (int) arg2;
387 assert(bidx <= PCI_BARMAX);
388 assert(pdi->pi_bar[bidx].type == PCIBAR_MEM32 ||
389 pdi->pi_bar[bidx].type == PCIBAR_MEM64);
390 assert(addr >= pdi->pi_bar[bidx].addr &&
391 addr + size <= pdi->pi_bar[bidx].addr + pdi->pi_bar[bidx].size);
393 offset = addr - pdi->pi_bar[bidx].addr;
395 if (dir == MEM_F_WRITE) {
397 (*pe->pe_barwrite)(ctx, vcpu, pdi, bidx, offset,
398 4, *val & 0xffffffff);
399 (*pe->pe_barwrite)(ctx, vcpu, pdi, bidx, offset + 4,
402 (*pe->pe_barwrite)(ctx, vcpu, pdi, bidx, offset,
407 *val = (*pe->pe_barread)(ctx, vcpu, pdi, bidx,
409 *val |= (*pe->pe_barread)(ctx, vcpu, pdi, bidx,
410 offset + 4, 4) << 32;
412 *val = (*pe->pe_barread)(ctx, vcpu, pdi, bidx,
422 pci_emul_alloc_resource(uint64_t *baseptr, uint64_t limit, uint64_t size,
427 assert((size & (size - 1)) == 0); /* must be a power of 2 */
429 base = roundup2(*baseptr, size);
431 if (base + size <= limit) {
433 *baseptr = base + size;
440 pci_emul_alloc_bar(struct pci_devinst *pdi, int idx, enum pcibar_type type,
444 return (pci_emul_alloc_pbar(pdi, idx, 0, type, size));
448 * Register (or unregister) the MMIO or I/O region associated with the BAR
449 * register 'idx' of an emulated pci device.
452 modify_bar_registration(struct pci_devinst *pi, int idx, int registration)
455 struct inout_port iop;
458 switch (pi->pi_bar[idx].type) {
460 bzero(&iop, sizeof(struct inout_port));
461 iop.name = pi->pi_name;
462 iop.port = pi->pi_bar[idx].addr;
463 iop.size = pi->pi_bar[idx].size;
465 iop.flags = IOPORT_F_INOUT;
466 iop.handler = pci_emul_io_handler;
468 error = register_inout(&iop);
470 error = unregister_inout(&iop);
474 bzero(&mr, sizeof(struct mem_range));
475 mr.name = pi->pi_name;
476 mr.base = pi->pi_bar[idx].addr;
477 mr.size = pi->pi_bar[idx].size;
480 mr.handler = pci_emul_mem_handler;
483 error = register_mem(&mr);
485 error = unregister_mem(&mr);
495 unregister_bar(struct pci_devinst *pi, int idx)
498 modify_bar_registration(pi, idx, 0);
502 register_bar(struct pci_devinst *pi, int idx)
505 modify_bar_registration(pi, idx, 1);
508 /* Are we decoding i/o port accesses for the emulated pci device? */
510 porten(struct pci_devinst *pi)
514 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND);
516 return (cmd & PCIM_CMD_PORTEN);
519 /* Are we decoding memory accesses for the emulated pci device? */
521 memen(struct pci_devinst *pi)
525 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND);
527 return (cmd & PCIM_CMD_MEMEN);
531 * Update the MMIO or I/O address that is decoded by the BAR register.
533 * If the pci device has enabled the address space decoding then intercept
534 * the address range decoded by the BAR register.
537 update_bar_address(struct pci_devinst *pi, uint64_t addr, int idx, int type)
541 if (pi->pi_bar[idx].type == PCIBAR_IO)
547 unregister_bar(pi, idx);
552 pi->pi_bar[idx].addr = addr;
555 pi->pi_bar[idx].addr &= ~0xffffffffUL;
556 pi->pi_bar[idx].addr |= addr;
559 pi->pi_bar[idx].addr &= 0xffffffff;
560 pi->pi_bar[idx].addr |= addr;
567 register_bar(pi, idx);
571 pci_emul_alloc_pbar(struct pci_devinst *pdi, int idx, uint64_t hostbase,
572 enum pcibar_type type, uint64_t size)
575 uint64_t *baseptr, limit, addr, mask, lobits, bar;
577 assert(idx >= 0 && idx <= PCI_BARMAX);
579 if ((size & (size - 1)) != 0)
580 size = 1UL << flsl(size); /* round up to a power of 2 */
582 /* Enforce minimum BAR sizes required by the PCI standard */
583 if (type == PCIBAR_IO) {
594 addr = mask = lobits = 0;
597 baseptr = &pci_emul_iobase;
598 limit = PCI_EMUL_IOLIMIT;
599 mask = PCIM_BAR_IO_BASE;
600 lobits = PCIM_BAR_IO_SPACE;
605 * Some drivers do not work well if the 64-bit BAR is allocated
606 * above 4GB. Allow for this by allocating small requests under
607 * 4GB unless then allocation size is larger than some arbitrary
608 * number (32MB currently).
610 if (size > 32 * 1024 * 1024) {
612 * XXX special case for device requiring peer-peer DMA
614 if (size == 0x100000000UL)
617 baseptr = &pci_emul_membase64;
618 limit = PCI_EMUL_MEMLIMIT64;
619 mask = PCIM_BAR_MEM_BASE;
620 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64 |
621 PCIM_BAR_MEM_PREFETCH;
624 baseptr = &pci_emul_membase32;
625 limit = PCI_EMUL_MEMLIMIT32;
626 mask = PCIM_BAR_MEM_BASE;
627 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64;
631 baseptr = &pci_emul_membase32;
632 limit = PCI_EMUL_MEMLIMIT32;
633 mask = PCIM_BAR_MEM_BASE;
634 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_32;
637 printf("pci_emul_alloc_base: invalid bar type %d\n", type);
641 if (baseptr != NULL) {
642 error = pci_emul_alloc_resource(baseptr, limit, size, &addr);
647 pdi->pi_bar[idx].type = type;
648 pdi->pi_bar[idx].addr = addr;
649 pdi->pi_bar[idx].size = size;
651 /* Initialize the BAR register in config space */
652 bar = (addr & mask) | lobits;
653 pci_set_cfgdata32(pdi, PCIR_BAR(idx), bar);
655 if (type == PCIBAR_MEM64) {
656 assert(idx + 1 <= PCI_BARMAX);
657 pdi->pi_bar[idx + 1].type = PCIBAR_MEMHI64;
658 pci_set_cfgdata32(pdi, PCIR_BAR(idx + 1), bar >> 32);
661 register_bar(pdi, idx);
666 #define CAP_START_OFFSET 0x40
668 pci_emul_add_capability(struct pci_devinst *pi, u_char *capdata, int caplen)
670 int i, capoff, reallen;
675 reallen = roundup2(caplen, 4); /* dword aligned */
677 sts = pci_get_cfgdata16(pi, PCIR_STATUS);
678 if ((sts & PCIM_STATUS_CAPPRESENT) == 0)
679 capoff = CAP_START_OFFSET;
681 capoff = pi->pi_capend + 1;
683 /* Check if we have enough space */
684 if (capoff + reallen > PCI_REGMAX + 1)
687 /* Set the previous capability pointer */
688 if ((sts & PCIM_STATUS_CAPPRESENT) == 0) {
689 pci_set_cfgdata8(pi, PCIR_CAP_PTR, capoff);
690 pci_set_cfgdata16(pi, PCIR_STATUS, sts|PCIM_STATUS_CAPPRESENT);
692 pci_set_cfgdata8(pi, pi->pi_prevcap + 1, capoff);
694 /* Copy the capability */
695 for (i = 0; i < caplen; i++)
696 pci_set_cfgdata8(pi, capoff + i, capdata[i]);
698 /* Set the next capability pointer */
699 pci_set_cfgdata8(pi, capoff + 1, 0);
701 pi->pi_prevcap = capoff;
702 pi->pi_capend = capoff + reallen - 1;
706 static struct pci_devemu *
707 pci_emul_finddev(char *name)
709 struct pci_devemu **pdpp, *pdp;
711 SET_FOREACH(pdpp, pci_devemu_set) {
713 if (!strcmp(pdp->pe_emu, name)) {
722 pci_emul_init(struct vmctx *ctx, struct pci_devemu *pde, int bus, int slot,
723 int func, struct funcinfo *fi)
725 struct pci_devinst *pdi;
728 pdi = calloc(1, sizeof(struct pci_devinst));
734 pthread_mutex_init(&pdi->pi_lintr.lock, NULL);
735 pdi->pi_lintr.pin = 0;
736 pdi->pi_lintr.state = IDLE;
737 pdi->pi_lintr.pirq_pin = 0;
738 pdi->pi_lintr.ioapic_irq = 0;
740 snprintf(pdi->pi_name, PI_NAMESZ, "%s-pci-%d", pde->pe_emu, slot);
742 /* Disable legacy interrupts */
743 pci_set_cfgdata8(pdi, PCIR_INTLINE, 255);
744 pci_set_cfgdata8(pdi, PCIR_INTPIN, 0);
746 pci_set_cfgdata8(pdi, PCIR_COMMAND,
747 PCIM_CMD_PORTEN | PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN);
749 err = (*pde->pe_init)(ctx, pdi, fi->fi_param);
759 pci_populate_msicap(struct msicap *msicap, int msgnum, int nextptr)
763 CTASSERT(sizeof(struct msicap) == 14);
765 /* Number of msi messages must be a power of 2 between 1 and 32 */
766 assert((msgnum & (msgnum - 1)) == 0 && msgnum >= 1 && msgnum <= 32);
767 mmc = ffs(msgnum) - 1;
769 bzero(msicap, sizeof(struct msicap));
770 msicap->capid = PCIY_MSI;
771 msicap->nextptr = nextptr;
772 msicap->msgctrl = PCIM_MSICTRL_64BIT | (mmc << 1);
776 pci_emul_add_msicap(struct pci_devinst *pi, int msgnum)
778 struct msicap msicap;
780 pci_populate_msicap(&msicap, msgnum, 0);
782 return (pci_emul_add_capability(pi, (u_char *)&msicap, sizeof(msicap)));
786 pci_populate_msixcap(struct msixcap *msixcap, int msgnum, int barnum,
787 uint32_t msix_tab_size)
789 CTASSERT(sizeof(struct msixcap) == 12);
791 assert(msix_tab_size % 4096 == 0);
793 bzero(msixcap, sizeof(struct msixcap));
794 msixcap->capid = PCIY_MSIX;
797 * Message Control Register, all fields set to
798 * zero except for the Table Size.
799 * Note: Table size N is encoded as N-1
801 msixcap->msgctrl = msgnum - 1;
805 * - MSI-X table start at offset 0
806 * - PBA table starts at a 4K aligned offset after the MSI-X table
808 msixcap->table_info = barnum & PCIM_MSIX_BIR_MASK;
809 msixcap->pba_info = msix_tab_size | (barnum & PCIM_MSIX_BIR_MASK);
813 pci_msix_table_init(struct pci_devinst *pi, int table_entries)
817 assert(table_entries > 0);
818 assert(table_entries <= MAX_MSIX_TABLE_ENTRIES);
820 table_size = table_entries * MSIX_TABLE_ENTRY_SIZE;
821 pi->pi_msix.table = calloc(1, table_size);
823 /* set mask bit of vector control register */
824 for (i = 0; i < table_entries; i++)
825 pi->pi_msix.table[i].vector_control |= PCIM_MSIX_VCTRL_MASK;
829 pci_emul_add_msixcap(struct pci_devinst *pi, int msgnum, int barnum)
832 struct msixcap msixcap;
834 assert(msgnum >= 1 && msgnum <= MAX_MSIX_TABLE_ENTRIES);
835 assert(barnum >= 0 && barnum <= PCIR_MAX_BAR_0);
837 tab_size = msgnum * MSIX_TABLE_ENTRY_SIZE;
839 /* Align table size to nearest 4K */
840 tab_size = roundup2(tab_size, 4096);
842 pi->pi_msix.table_bar = barnum;
843 pi->pi_msix.pba_bar = barnum;
844 pi->pi_msix.table_offset = 0;
845 pi->pi_msix.table_count = msgnum;
846 pi->pi_msix.pba_offset = tab_size;
847 pi->pi_msix.pba_size = PBA_SIZE(msgnum);
849 pci_msix_table_init(pi, msgnum);
851 pci_populate_msixcap(&msixcap, msgnum, barnum, tab_size);
853 /* allocate memory for MSI-X Table and PBA */
854 pci_emul_alloc_bar(pi, barnum, PCIBAR_MEM32,
855 tab_size + pi->pi_msix.pba_size);
857 return (pci_emul_add_capability(pi, (u_char *)&msixcap,
862 msixcap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
863 int bytes, uint32_t val)
865 uint16_t msgctrl, rwmask;
868 off = offset - capoff;
869 table_bar = pi->pi_msix.table_bar;
870 /* Message Control Register */
871 if (off == 2 && bytes == 2) {
872 rwmask = PCIM_MSIXCTRL_MSIX_ENABLE | PCIM_MSIXCTRL_FUNCTION_MASK;
873 msgctrl = pci_get_cfgdata16(pi, offset);
875 msgctrl |= val & rwmask;
878 pi->pi_msix.enabled = val & PCIM_MSIXCTRL_MSIX_ENABLE;
879 pi->pi_msix.function_mask = val & PCIM_MSIXCTRL_FUNCTION_MASK;
880 pci_lintr_update(pi);
883 CFGWRITE(pi, offset, val, bytes);
887 msicap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
888 int bytes, uint32_t val)
890 uint16_t msgctrl, rwmask, msgdata, mme;
894 * If guest is writing to the message control register make sure
895 * we do not overwrite read-only fields.
897 if ((offset - capoff) == 2 && bytes == 2) {
898 rwmask = PCIM_MSICTRL_MME_MASK | PCIM_MSICTRL_MSI_ENABLE;
899 msgctrl = pci_get_cfgdata16(pi, offset);
901 msgctrl |= val & rwmask;
904 addrlo = pci_get_cfgdata32(pi, capoff + 4);
905 if (msgctrl & PCIM_MSICTRL_64BIT)
906 msgdata = pci_get_cfgdata16(pi, capoff + 12);
908 msgdata = pci_get_cfgdata16(pi, capoff + 8);
910 mme = msgctrl & PCIM_MSICTRL_MME_MASK;
911 pi->pi_msi.enabled = msgctrl & PCIM_MSICTRL_MSI_ENABLE ? 1 : 0;
912 if (pi->pi_msi.enabled) {
913 pi->pi_msi.addr = addrlo;
914 pi->pi_msi.msg_data = msgdata;
915 pi->pi_msi.maxmsgnum = 1 << (mme >> 4);
917 pi->pi_msi.maxmsgnum = 0;
919 pci_lintr_update(pi);
922 CFGWRITE(pi, offset, val, bytes);
926 pciecap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
927 int bytes, uint32_t val)
930 /* XXX don't write to the readonly parts */
931 CFGWRITE(pi, offset, val, bytes);
934 #define PCIECAP_VERSION 0x2
936 pci_emul_add_pciecap(struct pci_devinst *pi, int type)
939 struct pciecap pciecap;
941 CTASSERT(sizeof(struct pciecap) == 60);
943 if (type != PCIEM_TYPE_ROOT_PORT)
946 bzero(&pciecap, sizeof(pciecap));
948 pciecap.capid = PCIY_EXPRESS;
949 pciecap.pcie_capabilities = PCIECAP_VERSION | PCIEM_TYPE_ROOT_PORT;
950 pciecap.link_capabilities = 0x411; /* gen1, x1 */
951 pciecap.link_status = 0x11; /* gen1, x1 */
953 err = pci_emul_add_capability(pi, (u_char *)&pciecap, sizeof(pciecap));
958 * This function assumes that 'coff' is in the capabilities region of the
962 pci_emul_capwrite(struct pci_devinst *pi, int offset, int bytes, uint32_t val)
965 uint8_t capoff, nextoff;
967 /* Do not allow un-aligned writes */
968 if ((offset & (bytes - 1)) != 0)
971 /* Find the capability that we want to update */
972 capoff = CAP_START_OFFSET;
974 nextoff = pci_get_cfgdata8(pi, capoff + 1);
977 if (offset >= capoff && offset < nextoff)
982 assert(offset >= capoff);
985 * Capability ID and Next Capability Pointer are readonly.
986 * However, some o/s's do 4-byte writes that include these.
987 * For this case, trim the write back to 2 bytes and adjust
990 if (offset == capoff || offset == capoff + 1) {
991 if (offset == capoff && bytes == 4) {
999 capid = pci_get_cfgdata8(pi, capoff);
1002 msicap_cfgwrite(pi, capoff, offset, bytes, val);
1005 msixcap_cfgwrite(pi, capoff, offset, bytes, val);
1008 pciecap_cfgwrite(pi, capoff, offset, bytes, val);
1016 pci_emul_iscap(struct pci_devinst *pi, int offset)
1020 sts = pci_get_cfgdata16(pi, PCIR_STATUS);
1021 if ((sts & PCIM_STATUS_CAPPRESENT) != 0) {
1022 if (offset >= CAP_START_OFFSET && offset <= pi->pi_capend)
1029 pci_emul_fallback_handler(struct vmctx *ctx, int vcpu, int dir, uint64_t addr,
1030 int size, uint64_t *val, void *arg1, long arg2)
1033 * Ignore writes; return 0xff's for reads. The mem read code
1034 * will take care of truncating to the correct size.
1036 if (dir == MEM_F_READ) {
1037 *val = 0xffffffffffffffff;
1044 pci_emul_ecfg_handler(struct vmctx *ctx, int vcpu, int dir, uint64_t addr,
1045 int bytes, uint64_t *val, void *arg1, long arg2)
1047 int bus, slot, func, coff, in;
1049 coff = addr & 0xfff;
1050 func = (addr >> 12) & 0x7;
1051 slot = (addr >> 15) & 0x1f;
1052 bus = (addr >> 20) & 0xff;
1053 in = (dir == MEM_F_READ);
1056 pci_cfgrw(ctx, vcpu, in, bus, slot, func, coff, bytes, (uint32_t *)val);
1064 return (PCI_EMUL_ECFG_BASE);
1067 #define BUSIO_ROUNDUP 32
1068 #define BUSMEM_ROUNDUP (1024 * 1024)
1071 init_pci(struct vmctx *ctx)
1073 struct mem_range mr;
1074 struct pci_devemu *pde;
1076 struct slotinfo *si;
1077 struct funcinfo *fi;
1079 int bus, slot, func;
1082 pci_emul_iobase = PCI_EMUL_IOBASE;
1083 pci_emul_membase32 = vm_get_lowmem_limit(ctx);
1084 pci_emul_membase64 = PCI_EMUL_MEMBASE64;
1086 for (bus = 0; bus < MAXBUSES; bus++) {
1087 if ((bi = pci_businfo[bus]) == NULL)
1090 * Keep track of the i/o and memory resources allocated to
1093 bi->iobase = pci_emul_iobase;
1094 bi->membase32 = pci_emul_membase32;
1095 bi->membase64 = pci_emul_membase64;
1097 for (slot = 0; slot < MAXSLOTS; slot++) {
1098 si = &bi->slotinfo[slot];
1099 for (func = 0; func < MAXFUNCS; func++) {
1100 fi = &si->si_funcs[func];
1101 if (fi->fi_name == NULL)
1103 pde = pci_emul_finddev(fi->fi_name);
1104 assert(pde != NULL);
1105 error = pci_emul_init(ctx, pde, bus, slot,
1113 * Add some slop to the I/O and memory resources decoded by
1114 * this bus to give a guest some flexibility if it wants to
1115 * reprogram the BARs.
1117 pci_emul_iobase += BUSIO_ROUNDUP;
1118 pci_emul_iobase = roundup2(pci_emul_iobase, BUSIO_ROUNDUP);
1119 bi->iolimit = pci_emul_iobase;
1121 pci_emul_membase32 += BUSMEM_ROUNDUP;
1122 pci_emul_membase32 = roundup2(pci_emul_membase32,
1124 bi->memlimit32 = pci_emul_membase32;
1126 pci_emul_membase64 += BUSMEM_ROUNDUP;
1127 pci_emul_membase64 = roundup2(pci_emul_membase64,
1129 bi->memlimit64 = pci_emul_membase64;
1133 * PCI backends are initialized before routing INTx interrupts
1134 * so that LPC devices are able to reserve ISA IRQs before
1135 * routing PIRQ pins.
1137 for (bus = 0; bus < MAXBUSES; bus++) {
1138 if ((bi = pci_businfo[bus]) == NULL)
1141 for (slot = 0; slot < MAXSLOTS; slot++) {
1142 si = &bi->slotinfo[slot];
1143 for (func = 0; func < MAXFUNCS; func++) {
1144 fi = &si->si_funcs[func];
1145 if (fi->fi_devi == NULL)
1147 pci_lintr_route(fi->fi_devi);
1154 * The guest physical memory map looks like the following:
1155 * [0, lowmem) guest system memory
1156 * [lowmem, lowmem_limit) memory hole (may be absent)
1157 * [lowmem_limit, 0xE0000000) PCI hole (32-bit BAR allocation)
1158 * [0xE0000000, 0xF0000000) PCI extended config window
1159 * [0xF0000000, 4GB) LAPIC, IOAPIC, HPET, firmware
1160 * [4GB, 4GB + highmem)
1164 * Accesses to memory addresses that are not allocated to system
1165 * memory or PCI devices return 0xff's.
1167 lowmem = vm_get_lowmem_size(ctx);
1168 bzero(&mr, sizeof(struct mem_range));
1169 mr.name = "PCI hole";
1170 mr.flags = MEM_F_RW | MEM_F_IMMUTABLE;
1172 mr.size = (4ULL * 1024 * 1024 * 1024) - lowmem;
1173 mr.handler = pci_emul_fallback_handler;
1174 error = register_mem_fallback(&mr);
1177 /* PCI extended config space */
1178 bzero(&mr, sizeof(struct mem_range));
1179 mr.name = "PCI ECFG";
1180 mr.flags = MEM_F_RW | MEM_F_IMMUTABLE;
1181 mr.base = PCI_EMUL_ECFG_BASE;
1182 mr.size = PCI_EMUL_ECFG_SIZE;
1183 mr.handler = pci_emul_ecfg_handler;
1184 error = register_mem(&mr);
1191 pci_apic_prt_entry(int bus, int slot, int pin, int pirq_pin, int ioapic_irq,
1195 dsdt_line(" Package ()");
1197 dsdt_line(" 0x%X,", slot << 16 | 0xffff);
1198 dsdt_line(" 0x%02X,", pin - 1);
1199 dsdt_line(" Zero,");
1200 dsdt_line(" 0x%X", ioapic_irq);
1205 pci_pirq_prt_entry(int bus, int slot, int pin, int pirq_pin, int ioapic_irq,
1210 name = lpc_pirq_name(pirq_pin);
1213 dsdt_line(" Package ()");
1215 dsdt_line(" 0x%X,", slot << 16 | 0xffff);
1216 dsdt_line(" 0x%02X,", pin - 1);
1217 dsdt_line(" %s,", name);
1224 * A bhyve virtual machine has a flat PCI hierarchy with a root port
1225 * corresponding to each PCI bus.
1228 pci_bus_write_dsdt(int bus)
1231 struct slotinfo *si;
1232 struct pci_devinst *pi;
1233 int count, func, slot;
1236 * If there are no devices on this 'bus' then just return.
1238 if ((bi = pci_businfo[bus]) == NULL) {
1240 * Bus 0 is special because it decodes the I/O ports used
1241 * for PCI config space access even if there are no devices
1248 dsdt_line(" Device (PC%02X)", bus);
1250 dsdt_line(" Name (_HID, EisaId (\"PNP0A03\"))");
1251 dsdt_line(" Name (_ADR, Zero)");
1253 dsdt_line(" Method (_BBN, 0, NotSerialized)");
1255 dsdt_line(" Return (0x%08X)", bus);
1257 dsdt_line(" Name (_CRS, ResourceTemplate ()");
1259 dsdt_line(" WordBusNumber (ResourceProducer, MinFixed, "
1260 "MaxFixed, PosDecode,");
1261 dsdt_line(" 0x0000, // Granularity");
1262 dsdt_line(" 0x%04X, // Range Minimum", bus);
1263 dsdt_line(" 0x%04X, // Range Maximum", bus);
1264 dsdt_line(" 0x0000, // Translation Offset");
1265 dsdt_line(" 0x0001, // Length");
1270 dsdt_fixed_ioport(0xCF8, 8);
1273 dsdt_line(" WordIO (ResourceProducer, MinFixed, MaxFixed, "
1274 "PosDecode, EntireRange,");
1275 dsdt_line(" 0x0000, // Granularity");
1276 dsdt_line(" 0x0000, // Range Minimum");
1277 dsdt_line(" 0x0CF7, // Range Maximum");
1278 dsdt_line(" 0x0000, // Translation Offset");
1279 dsdt_line(" 0x0CF8, // Length");
1280 dsdt_line(" ,, , TypeStatic)");
1282 dsdt_line(" WordIO (ResourceProducer, MinFixed, MaxFixed, "
1283 "PosDecode, EntireRange,");
1284 dsdt_line(" 0x0000, // Granularity");
1285 dsdt_line(" 0x0D00, // Range Minimum");
1286 dsdt_line(" 0x%04X, // Range Maximum",
1287 PCI_EMUL_IOBASE - 1);
1288 dsdt_line(" 0x0000, // Translation Offset");
1289 dsdt_line(" 0x%04X, // Length",
1290 PCI_EMUL_IOBASE - 0x0D00);
1291 dsdt_line(" ,, , TypeStatic)");
1301 dsdt_line(" WordIO (ResourceProducer, MinFixed, MaxFixed, "
1302 "PosDecode, EntireRange,");
1303 dsdt_line(" 0x0000, // Granularity");
1304 dsdt_line(" 0x%04X, // Range Minimum", bi->iobase);
1305 dsdt_line(" 0x%04X, // Range Maximum",
1307 dsdt_line(" 0x0000, // Translation Offset");
1308 dsdt_line(" 0x%04X, // Length",
1309 bi->iolimit - bi->iobase);
1310 dsdt_line(" ,, , TypeStatic)");
1312 /* mmio window (32-bit) */
1313 dsdt_line(" DWordMemory (ResourceProducer, PosDecode, "
1314 "MinFixed, MaxFixed, NonCacheable, ReadWrite,");
1315 dsdt_line(" 0x00000000, // Granularity");
1316 dsdt_line(" 0x%08X, // Range Minimum\n", bi->membase32);
1317 dsdt_line(" 0x%08X, // Range Maximum\n",
1318 bi->memlimit32 - 1);
1319 dsdt_line(" 0x00000000, // Translation Offset");
1320 dsdt_line(" 0x%08X, // Length\n",
1321 bi->memlimit32 - bi->membase32);
1322 dsdt_line(" ,, , AddressRangeMemory, TypeStatic)");
1324 /* mmio window (64-bit) */
1325 dsdt_line(" QWordMemory (ResourceProducer, PosDecode, "
1326 "MinFixed, MaxFixed, NonCacheable, ReadWrite,");
1327 dsdt_line(" 0x0000000000000000, // Granularity");
1328 dsdt_line(" 0x%016lX, // Range Minimum\n", bi->membase64);
1329 dsdt_line(" 0x%016lX, // Range Maximum\n",
1330 bi->memlimit64 - 1);
1331 dsdt_line(" 0x0000000000000000, // Translation Offset");
1332 dsdt_line(" 0x%016lX, // Length\n",
1333 bi->memlimit64 - bi->membase64);
1334 dsdt_line(" ,, , AddressRangeMemory, TypeStatic)");
1337 count = pci_count_lintr(bus);
1340 dsdt_line("Name (PPRT, Package ()");
1342 pci_walk_lintr(bus, pci_pirq_prt_entry, NULL);
1344 dsdt_line("Name (APRT, Package ()");
1346 pci_walk_lintr(bus, pci_apic_prt_entry, NULL);
1348 dsdt_line("Method (_PRT, 0, NotSerialized)");
1350 dsdt_line(" If (PICM)");
1352 dsdt_line(" Return (APRT)");
1356 dsdt_line(" Return (PPRT)");
1363 for (slot = 0; slot < MAXSLOTS; slot++) {
1364 si = &bi->slotinfo[slot];
1365 for (func = 0; func < MAXFUNCS; func++) {
1366 pi = si->si_funcs[func].fi_devi;
1367 if (pi != NULL && pi->pi_d->pe_write_dsdt != NULL)
1368 pi->pi_d->pe_write_dsdt(pi);
1377 pci_write_dsdt(void)
1382 dsdt_line("Name (PICM, 0x00)");
1383 dsdt_line("Method (_PIC, 1, NotSerialized)");
1385 dsdt_line(" Store (Arg0, PICM)");
1388 dsdt_line("Scope (_SB)");
1390 for (bus = 0; bus < MAXBUSES; bus++)
1391 pci_bus_write_dsdt(bus);
1397 pci_bus_configured(int bus)
1399 assert(bus >= 0 && bus < MAXBUSES);
1400 return (pci_businfo[bus] != NULL);
1404 pci_msi_enabled(struct pci_devinst *pi)
1406 return (pi->pi_msi.enabled);
1410 pci_msi_maxmsgnum(struct pci_devinst *pi)
1412 if (pi->pi_msi.enabled)
1413 return (pi->pi_msi.maxmsgnum);
1419 pci_msix_enabled(struct pci_devinst *pi)
1422 return (pi->pi_msix.enabled && !pi->pi_msi.enabled);
1426 pci_generate_msix(struct pci_devinst *pi, int index)
1428 struct msix_table_entry *mte;
1430 if (!pci_msix_enabled(pi))
1433 if (pi->pi_msix.function_mask)
1436 if (index >= pi->pi_msix.table_count)
1439 mte = &pi->pi_msix.table[index];
1440 if ((mte->vector_control & PCIM_MSIX_VCTRL_MASK) == 0) {
1441 /* XXX Set PBA bit if interrupt is disabled */
1442 vm_lapic_msi(pi->pi_vmctx, mte->addr, mte->msg_data);
1447 pci_generate_msi(struct pci_devinst *pi, int index)
1450 if (pci_msi_enabled(pi) && index < pci_msi_maxmsgnum(pi)) {
1451 vm_lapic_msi(pi->pi_vmctx, pi->pi_msi.addr,
1452 pi->pi_msi.msg_data + index);
1457 pci_lintr_permitted(struct pci_devinst *pi)
1461 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND);
1462 return (!(pi->pi_msi.enabled || pi->pi_msix.enabled ||
1463 (cmd & PCIM_CMD_INTxDIS)));
1467 pci_lintr_request(struct pci_devinst *pi)
1470 struct slotinfo *si;
1471 int bestpin, bestcount, pin;
1473 bi = pci_businfo[pi->pi_bus];
1477 * Just allocate a pin from our slot. The pin will be
1478 * assigned IRQs later when interrupts are routed.
1480 si = &bi->slotinfo[pi->pi_slot];
1482 bestcount = si->si_intpins[0].ii_count;
1483 for (pin = 1; pin < 4; pin++) {
1484 if (si->si_intpins[pin].ii_count < bestcount) {
1486 bestcount = si->si_intpins[pin].ii_count;
1490 si->si_intpins[bestpin].ii_count++;
1491 pi->pi_lintr.pin = bestpin + 1;
1492 pci_set_cfgdata8(pi, PCIR_INTPIN, bestpin + 1);
1496 pci_lintr_route(struct pci_devinst *pi)
1499 struct intxinfo *ii;
1501 if (pi->pi_lintr.pin == 0)
1504 bi = pci_businfo[pi->pi_bus];
1506 ii = &bi->slotinfo[pi->pi_slot].si_intpins[pi->pi_lintr.pin - 1];
1509 * Attempt to allocate an I/O APIC pin for this intpin if one
1510 * is not yet assigned.
1512 if (ii->ii_ioapic_irq == 0)
1513 ii->ii_ioapic_irq = ioapic_pci_alloc_irq();
1514 assert(ii->ii_ioapic_irq > 0);
1517 * Attempt to allocate a PIRQ pin for this intpin if one is
1520 if (ii->ii_pirq_pin == 0)
1521 ii->ii_pirq_pin = pirq_alloc_pin(pi->pi_vmctx);
1522 assert(ii->ii_pirq_pin > 0);
1524 pi->pi_lintr.ioapic_irq = ii->ii_ioapic_irq;
1525 pi->pi_lintr.pirq_pin = ii->ii_pirq_pin;
1526 pci_set_cfgdata8(pi, PCIR_INTLINE, pirq_irq(ii->ii_pirq_pin));
1530 pci_lintr_assert(struct pci_devinst *pi)
1533 assert(pi->pi_lintr.pin > 0);
1535 pthread_mutex_lock(&pi->pi_lintr.lock);
1536 if (pi->pi_lintr.state == IDLE) {
1537 if (pci_lintr_permitted(pi)) {
1538 pi->pi_lintr.state = ASSERTED;
1541 pi->pi_lintr.state = PENDING;
1543 pthread_mutex_unlock(&pi->pi_lintr.lock);
1547 pci_lintr_deassert(struct pci_devinst *pi)
1550 assert(pi->pi_lintr.pin > 0);
1552 pthread_mutex_lock(&pi->pi_lintr.lock);
1553 if (pi->pi_lintr.state == ASSERTED) {
1554 pi->pi_lintr.state = IDLE;
1555 pci_irq_deassert(pi);
1556 } else if (pi->pi_lintr.state == PENDING)
1557 pi->pi_lintr.state = IDLE;
1558 pthread_mutex_unlock(&pi->pi_lintr.lock);
1562 pci_lintr_update(struct pci_devinst *pi)
1565 pthread_mutex_lock(&pi->pi_lintr.lock);
1566 if (pi->pi_lintr.state == ASSERTED && !pci_lintr_permitted(pi)) {
1567 pci_irq_deassert(pi);
1568 pi->pi_lintr.state = PENDING;
1569 } else if (pi->pi_lintr.state == PENDING && pci_lintr_permitted(pi)) {
1570 pi->pi_lintr.state = ASSERTED;
1573 pthread_mutex_unlock(&pi->pi_lintr.lock);
1577 pci_count_lintr(int bus)
1579 int count, slot, pin;
1580 struct slotinfo *slotinfo;
1583 if (pci_businfo[bus] != NULL) {
1584 for (slot = 0; slot < MAXSLOTS; slot++) {
1585 slotinfo = &pci_businfo[bus]->slotinfo[slot];
1586 for (pin = 0; pin < 4; pin++) {
1587 if (slotinfo->si_intpins[pin].ii_count != 0)
1596 pci_walk_lintr(int bus, pci_lintr_cb cb, void *arg)
1599 struct slotinfo *si;
1600 struct intxinfo *ii;
1603 if ((bi = pci_businfo[bus]) == NULL)
1606 for (slot = 0; slot < MAXSLOTS; slot++) {
1607 si = &bi->slotinfo[slot];
1608 for (pin = 0; pin < 4; pin++) {
1609 ii = &si->si_intpins[pin];
1610 if (ii->ii_count != 0)
1611 cb(bus, slot, pin + 1, ii->ii_pirq_pin,
1612 ii->ii_ioapic_irq, arg);
1618 * Return 1 if the emulated device in 'slot' is a multi-function device.
1619 * Return 0 otherwise.
1622 pci_emul_is_mfdev(int bus, int slot)
1625 struct slotinfo *si;
1629 if ((bi = pci_businfo[bus]) != NULL) {
1630 si = &bi->slotinfo[slot];
1631 for (f = 0; f < MAXFUNCS; f++) {
1632 if (si->si_funcs[f].fi_devi != NULL) {
1637 return (numfuncs > 1);
1641 * Ensure that the PCIM_MFDEV bit is properly set (or unset) depending on
1642 * whether or not is a multi-function being emulated in the pci 'slot'.
1645 pci_emul_hdrtype_fixup(int bus, int slot, int off, int bytes, uint32_t *rv)
1649 if (off <= PCIR_HDRTYPE && off + bytes > PCIR_HDRTYPE) {
1650 mfdev = pci_emul_is_mfdev(bus, slot);
1660 *rv &= ~(PCIM_MFDEV << 16);
1662 *rv |= (PCIM_MFDEV << 16);
1670 pci_emul_cmdsts_write(struct pci_devinst *pi, int coff, uint32_t new, int bytes)
1673 uint32_t cmd, cmd2, changed, old, readonly;
1675 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND); /* stash old value */
1678 * From PCI Local Bus Specification 3.0 sections 6.2.2 and 6.2.3.
1680 * XXX Bits 8, 11, 12, 13, 14 and 15 in the status register are
1681 * 'write 1 to clear'. However these bits are not set to '1' by
1682 * any device emulation so it is simpler to treat them as readonly.
1684 rshift = (coff & 0x3) * 8;
1685 readonly = 0xFFFFF880 >> rshift;
1687 old = CFGREAD(pi, coff, bytes);
1689 new |= (old & readonly);
1690 CFGWRITE(pi, coff, new, bytes); /* update config */
1692 cmd2 = pci_get_cfgdata16(pi, PCIR_COMMAND); /* get updated value */
1693 changed = cmd ^ cmd2;
1696 * If the MMIO or I/O address space decoding has changed then
1697 * register/unregister all BARs that decode that address space.
1699 for (i = 0; i <= PCI_BARMAX; i++) {
1700 switch (pi->pi_bar[i].type) {
1702 case PCIBAR_MEMHI64:
1705 /* I/O address space decoding changed? */
1706 if (changed & PCIM_CMD_PORTEN) {
1708 register_bar(pi, i);
1710 unregister_bar(pi, i);
1715 /* MMIO address space decoding changed? */
1716 if (changed & PCIM_CMD_MEMEN) {
1718 register_bar(pi, i);
1720 unregister_bar(pi, i);
1729 * If INTx has been unmasked and is pending, assert the
1732 pci_lintr_update(pi);
1736 pci_cfgrw(struct vmctx *ctx, int vcpu, int in, int bus, int slot, int func,
1737 int coff, int bytes, uint32_t *eax)
1740 struct slotinfo *si;
1741 struct pci_devinst *pi;
1742 struct pci_devemu *pe;
1744 uint64_t addr, bar, mask;
1746 if ((bi = pci_businfo[bus]) != NULL) {
1747 si = &bi->slotinfo[slot];
1748 pi = si->si_funcs[func].fi_devi;
1753 * Just return if there is no device at this slot:func or if the
1754 * the guest is doing an un-aligned access.
1756 if (pi == NULL || (bytes != 1 && bytes != 2 && bytes != 4) ||
1757 (coff & (bytes - 1)) != 0) {
1764 * Ignore all writes beyond the standard config space and return all
1767 if (coff >= PCI_REGMAX + 1) {
1771 * Extended capabilities begin at offset 256 in config
1772 * space. Absence of extended capabilities is signaled
1773 * with all 0s in the extended capability header at
1776 if (coff <= PCI_REGMAX + 4)
1788 /* Let the device emulation override the default handler */
1789 if (pe->pe_cfgread != NULL) {
1790 needcfg = pe->pe_cfgread(ctx, vcpu, pi, coff, bytes,
1797 *eax = CFGREAD(pi, coff, bytes);
1799 pci_emul_hdrtype_fixup(bus, slot, coff, bytes, eax);
1801 /* Let the device emulation override the default handler */
1802 if (pe->pe_cfgwrite != NULL &&
1803 (*pe->pe_cfgwrite)(ctx, vcpu, pi, coff, bytes, *eax) == 0)
1807 * Special handling for write to BAR registers
1809 if (coff >= PCIR_BAR(0) && coff < PCIR_BAR(PCI_BARMAX + 1)) {
1811 * Ignore writes to BAR registers that are not
1814 if (bytes != 4 || (coff & 0x3) != 0)
1816 idx = (coff - PCIR_BAR(0)) / 4;
1817 mask = ~(pi->pi_bar[idx].size - 1);
1818 switch (pi->pi_bar[idx].type) {
1820 pi->pi_bar[idx].addr = bar = 0;
1825 bar = addr | PCIM_BAR_IO_SPACE;
1827 * Register the new BAR value for interception
1829 if (addr != pi->pi_bar[idx].addr) {
1830 update_bar_address(pi, addr, idx,
1835 addr = bar = *eax & mask;
1836 bar |= PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_32;
1837 if (addr != pi->pi_bar[idx].addr) {
1838 update_bar_address(pi, addr, idx,
1843 addr = bar = *eax & mask;
1844 bar |= PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64 |
1845 PCIM_BAR_MEM_PREFETCH;
1846 if (addr != (uint32_t)pi->pi_bar[idx].addr) {
1847 update_bar_address(pi, addr, idx,
1851 case PCIBAR_MEMHI64:
1852 mask = ~(pi->pi_bar[idx - 1].size - 1);
1853 addr = ((uint64_t)*eax << 32) & mask;
1855 if (bar != pi->pi_bar[idx - 1].addr >> 32) {
1856 update_bar_address(pi, addr, idx - 1,
1863 pci_set_cfgdata32(pi, coff, bar);
1865 } else if (pci_emul_iscap(pi, coff)) {
1866 pci_emul_capwrite(pi, coff, bytes, *eax);
1867 } else if (coff >= PCIR_COMMAND && coff < PCIR_REVID) {
1868 pci_emul_cmdsts_write(pi, coff, *eax, bytes);
1870 CFGWRITE(pi, coff, *eax, bytes);
1875 static int cfgenable, cfgbus, cfgslot, cfgfunc, cfgoff;
1878 pci_emul_cfgaddr(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
1879 uint32_t *eax, void *arg)
1885 *eax = (bytes == 2) ? 0xffff : 0xff;
1890 x = (cfgbus << 16) | (cfgslot << 11) | (cfgfunc << 8) | cfgoff;
1896 cfgenable = (x & CONF1_ENABLE) == CONF1_ENABLE;
1897 cfgoff = x & PCI_REGMAX;
1898 cfgfunc = (x >> 8) & PCI_FUNCMAX;
1899 cfgslot = (x >> 11) & PCI_SLOTMAX;
1900 cfgbus = (x >> 16) & PCI_BUSMAX;
1905 INOUT_PORT(pci_cfgaddr, CONF1_ADDR_PORT, IOPORT_F_INOUT, pci_emul_cfgaddr);
1908 pci_emul_cfgdata(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
1909 uint32_t *eax, void *arg)
1913 assert(bytes == 1 || bytes == 2 || bytes == 4);
1915 coff = cfgoff + (port - CONF1_DATA_PORT);
1917 pci_cfgrw(ctx, vcpu, in, cfgbus, cfgslot, cfgfunc, coff, bytes,
1920 /* Ignore accesses to cfgdata if not enabled by cfgaddr */
1927 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+0, IOPORT_F_INOUT, pci_emul_cfgdata);
1928 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+1, IOPORT_F_INOUT, pci_emul_cfgdata);
1929 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+2, IOPORT_F_INOUT, pci_emul_cfgdata);
1930 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+3, IOPORT_F_INOUT, pci_emul_cfgdata);
1932 #define PCI_EMUL_TEST
1933 #ifdef PCI_EMUL_TEST
1935 * Define a dummy test device
1939 struct pci_emul_dsoftc {
1940 uint8_t ioregs[DIOSZ];
1941 uint8_t memregs[2][DMEMSZ];
1944 #define PCI_EMUL_MSI_MSGS 4
1945 #define PCI_EMUL_MSIX_MSGS 16
1948 pci_emul_dinit(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
1951 struct pci_emul_dsoftc *sc;
1953 sc = calloc(1, sizeof(struct pci_emul_dsoftc));
1957 pci_set_cfgdata16(pi, PCIR_DEVICE, 0x0001);
1958 pci_set_cfgdata16(pi, PCIR_VENDOR, 0x10DD);
1959 pci_set_cfgdata8(pi, PCIR_CLASS, 0x02);
1961 error = pci_emul_add_msicap(pi, PCI_EMUL_MSI_MSGS);
1964 error = pci_emul_alloc_bar(pi, 0, PCIBAR_IO, DIOSZ);
1967 error = pci_emul_alloc_bar(pi, 1, PCIBAR_MEM32, DMEMSZ);
1970 error = pci_emul_alloc_bar(pi, 2, PCIBAR_MEM32, DMEMSZ);
1977 pci_emul_diow(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx,
1978 uint64_t offset, int size, uint64_t value)
1981 struct pci_emul_dsoftc *sc = pi->pi_arg;
1984 if (offset + size > DIOSZ) {
1985 printf("diow: iow too large, offset %ld size %d\n",
1991 sc->ioregs[offset] = value & 0xff;
1992 } else if (size == 2) {
1993 *(uint16_t *)&sc->ioregs[offset] = value & 0xffff;
1994 } else if (size == 4) {
1995 *(uint32_t *)&sc->ioregs[offset] = value;
1997 printf("diow: iow unknown size %d\n", size);
2001 * Special magic value to generate an interrupt
2003 if (offset == 4 && size == 4 && pci_msi_enabled(pi))
2004 pci_generate_msi(pi, value % pci_msi_maxmsgnum(pi));
2006 if (value == 0xabcdef) {
2007 for (i = 0; i < pci_msi_maxmsgnum(pi); i++)
2008 pci_generate_msi(pi, i);
2012 if (baridx == 1 || baridx == 2) {
2013 if (offset + size > DMEMSZ) {
2014 printf("diow: memw too large, offset %ld size %d\n",
2019 i = baridx - 1; /* 'memregs' index */
2022 sc->memregs[i][offset] = value;
2023 } else if (size == 2) {
2024 *(uint16_t *)&sc->memregs[i][offset] = value;
2025 } else if (size == 4) {
2026 *(uint32_t *)&sc->memregs[i][offset] = value;
2027 } else if (size == 8) {
2028 *(uint64_t *)&sc->memregs[i][offset] = value;
2030 printf("diow: memw unknown size %d\n", size);
2034 * magic interrupt ??
2039 printf("diow: unknown bar idx %d\n", baridx);
2044 pci_emul_dior(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx,
2045 uint64_t offset, int size)
2047 struct pci_emul_dsoftc *sc = pi->pi_arg;
2052 if (offset + size > DIOSZ) {
2053 printf("dior: ior too large, offset %ld size %d\n",
2059 value = sc->ioregs[offset];
2060 } else if (size == 2) {
2061 value = *(uint16_t *) &sc->ioregs[offset];
2062 } else if (size == 4) {
2063 value = *(uint32_t *) &sc->ioregs[offset];
2065 printf("dior: ior unknown size %d\n", size);
2069 if (baridx == 1 || baridx == 2) {
2070 if (offset + size > DMEMSZ) {
2071 printf("dior: memr too large, offset %ld size %d\n",
2076 i = baridx - 1; /* 'memregs' index */
2079 value = sc->memregs[i][offset];
2080 } else if (size == 2) {
2081 value = *(uint16_t *) &sc->memregs[i][offset];
2082 } else if (size == 4) {
2083 value = *(uint32_t *) &sc->memregs[i][offset];
2084 } else if (size == 8) {
2085 value = *(uint64_t *) &sc->memregs[i][offset];
2087 printf("dior: ior unknown size %d\n", size);
2093 printf("dior: unknown bar idx %d\n", baridx);
2100 struct pci_devemu pci_dummy = {
2102 .pe_init = pci_emul_dinit,
2103 .pe_barwrite = pci_emul_diow,
2104 .pe_barread = pci_emul_dior
2106 PCI_EMUL_SET(pci_dummy);
2108 #endif /* PCI_EMUL_TEST */