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>
44 #include <machine/vmm.h>
53 #define CONF1_ADDR_PORT 0x0cf8
54 #define CONF1_DATA_PORT 0x0cfc
56 #define CONF1_ENABLE 0x80000000ul
58 #define CFGWRITE(pi,off,val,b) \
61 pci_set_cfgdata8((pi),(off),(val)); \
62 } else if ((b) == 2) { \
63 pci_set_cfgdata16((pi),(off),(val)); \
65 pci_set_cfgdata32((pi),(off),(val)); \
69 #define MAXSLOTS (PCI_SLOTMAX + 1)
70 #define MAXFUNCS (PCI_FUNCMAX + 1)
72 static struct slotinfo {
75 struct pci_devinst *si_devi;
77 } pci_slotinfo[MAXSLOTS][MAXFUNCS];
80 * Used to keep track of legacy interrupt owners/requestors
84 static struct lirqinfo {
87 struct pci_devinst *li_owner; /* XXX should be a list */
90 SET_DECLARE(pci_devemu_set, struct pci_devemu);
92 static uint64_t pci_emul_iobase;
93 static uint64_t pci_emul_membase32;
94 static uint64_t pci_emul_membase64;
96 #define PCI_EMUL_IOBASE 0x2000
97 #define PCI_EMUL_IOLIMIT 0x10000
99 #define PCI_EMUL_MEMLIMIT32 0xE0000000 /* 3.5GB */
101 #define PCI_EMUL_MEMBASE64 0xD000000000UL
102 #define PCI_EMUL_MEMLIMIT64 0xFD00000000UL
104 static struct pci_devemu *pci_emul_finddev(char *name);
106 static int pci_emul_devices;
113 * Slot options are in the form:
115 * <slot>[:<func>],<emul>[,<config>]
119 * emul is a string describing the type of PCI device e.g. virtio-net
120 * config is an optional string, depending on the device, that can be
121 * used for configuration.
127 pci_parse_slot_usage(char *aopt)
130 fprintf(stderr, "Invalid PCI slot info field \"%s\"\n", aopt);
134 pci_parse_slot(char *opt, int legacy)
136 char *slot, *func, *emul, *config;
138 int error, snum, fnum;
141 str = cpy = strdup(opt);
143 slot = strsep(&str, ",");
145 if (strchr(slot, ':') != NULL) {
147 (void) strsep(&func, ":");
150 emul = strsep(&str, ",");
154 pci_parse_slot_usage(opt);
159 fnum = func ? atoi(func) : 0;
161 if (snum < 0 || snum >= MAXSLOTS || fnum < 0 || fnum >= MAXFUNCS) {
162 pci_parse_slot_usage(opt);
166 if (pci_slotinfo[snum][fnum].si_name != NULL) {
167 fprintf(stderr, "pci slot %d:%d already occupied!\n",
172 if (pci_emul_finddev(emul) == NULL) {
173 fprintf(stderr, "pci slot %d:%d: unknown device \"%s\"\n",
179 pci_slotinfo[snum][fnum].si_name = emul;
180 pci_slotinfo[snum][fnum].si_param = config;
181 pci_slotinfo[snum][fnum].si_legacy = legacy;
191 pci_valid_pba_offset(struct pci_devinst *pi, uint64_t offset)
194 if (offset < pi->pi_msix.pba_offset)
197 if (offset >= pi->pi_msix.pba_offset + pi->pi_msix.pba_size) {
205 pci_emul_msix_twrite(struct pci_devinst *pi, uint64_t offset, int size,
208 int msix_entry_offset;
212 /* support only 4 or 8 byte writes */
213 if (size != 4 && size != 8)
217 * Return if table index is beyond what device supports
219 tab_index = offset / MSIX_TABLE_ENTRY_SIZE;
220 if (tab_index >= pi->pi_msix.table_count)
223 msix_entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
225 /* support only aligned writes */
226 if ((msix_entry_offset % size) != 0)
229 dest = (char *)(pi->pi_msix.table + tab_index);
230 dest += msix_entry_offset;
233 *((uint32_t *)dest) = value;
235 *((uint64_t *)dest) = value;
241 pci_emul_msix_tread(struct pci_devinst *pi, uint64_t offset, int size)
244 int msix_entry_offset;
246 uint64_t retval = ~0;
248 /* support only 4 or 8 byte reads */
249 if (size != 4 && size != 8)
252 msix_entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
254 /* support only aligned reads */
255 if ((msix_entry_offset % size) != 0) {
259 tab_index = offset / MSIX_TABLE_ENTRY_SIZE;
261 if (tab_index < pi->pi_msix.table_count) {
262 /* valid MSI-X Table access */
263 dest = (char *)(pi->pi_msix.table + tab_index);
264 dest += msix_entry_offset;
267 retval = *((uint32_t *)dest);
269 retval = *((uint64_t *)dest);
270 } else if (pci_valid_pba_offset(pi, offset)) {
271 /* return 0 for PBA access */
279 pci_msix_table_bar(struct pci_devinst *pi)
282 if (pi->pi_msix.table != NULL)
283 return (pi->pi_msix.table_bar);
289 pci_msix_pba_bar(struct pci_devinst *pi)
292 if (pi->pi_msix.table != NULL)
293 return (pi->pi_msix.pba_bar);
299 pci_emul_io_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
300 uint32_t *eax, void *arg)
302 struct pci_devinst *pdi = arg;
303 struct pci_devemu *pe = pdi->pi_d;
307 for (i = 0; i <= PCI_BARMAX; i++) {
308 if (pdi->pi_bar[i].type == PCIBAR_IO &&
309 port >= pdi->pi_bar[i].addr &&
310 port + bytes <= pdi->pi_bar[i].addr + pdi->pi_bar[i].size) {
311 offset = port - pdi->pi_bar[i].addr;
313 *eax = (*pe->pe_barread)(ctx, vcpu, pdi, i,
316 (*pe->pe_barwrite)(ctx, vcpu, pdi, i, offset,
325 pci_emul_mem_handler(struct vmctx *ctx, int vcpu, int dir, uint64_t addr,
326 int size, uint64_t *val, void *arg1, long arg2)
328 struct pci_devinst *pdi = arg1;
329 struct pci_devemu *pe = pdi->pi_d;
331 int bidx = (int) arg2;
333 assert(bidx <= PCI_BARMAX);
334 assert(pdi->pi_bar[bidx].type == PCIBAR_MEM32 ||
335 pdi->pi_bar[bidx].type == PCIBAR_MEM64);
336 assert(addr >= pdi->pi_bar[bidx].addr &&
337 addr + size <= pdi->pi_bar[bidx].addr + pdi->pi_bar[bidx].size);
339 offset = addr - pdi->pi_bar[bidx].addr;
341 if (dir == MEM_F_WRITE)
342 (*pe->pe_barwrite)(ctx, vcpu, pdi, bidx, offset, size, *val);
344 *val = (*pe->pe_barread)(ctx, vcpu, pdi, bidx, offset, size);
351 pci_emul_alloc_resource(uint64_t *baseptr, uint64_t limit, uint64_t size,
356 assert((size & (size - 1)) == 0); /* must be a power of 2 */
358 base = roundup2(*baseptr, size);
360 if (base + size <= limit) {
362 *baseptr = base + size;
369 pci_emul_alloc_bar(struct pci_devinst *pdi, int idx, enum pcibar_type type,
373 return (pci_emul_alloc_pbar(pdi, idx, 0, type, size));
377 * Register (or unregister) the MMIO or I/O region associated with the BAR
378 * register 'idx' of an emulated pci device.
381 modify_bar_registration(struct pci_devinst *pi, int idx, int registration)
384 struct inout_port iop;
387 switch (pi->pi_bar[idx].type) {
389 bzero(&iop, sizeof(struct inout_port));
390 iop.name = pi->pi_name;
391 iop.port = pi->pi_bar[idx].addr;
392 iop.size = pi->pi_bar[idx].size;
394 iop.flags = IOPORT_F_INOUT;
395 iop.handler = pci_emul_io_handler;
397 error = register_inout(&iop);
399 error = unregister_inout(&iop);
403 bzero(&mr, sizeof(struct mem_range));
404 mr.name = pi->pi_name;
405 mr.base = pi->pi_bar[idx].addr;
406 mr.size = pi->pi_bar[idx].size;
409 mr.handler = pci_emul_mem_handler;
412 error = register_mem(&mr);
414 error = unregister_mem(&mr);
424 unregister_bar(struct pci_devinst *pi, int idx)
427 modify_bar_registration(pi, idx, 0);
431 register_bar(struct pci_devinst *pi, int idx)
434 modify_bar_registration(pi, idx, 1);
437 /* Are we decoding i/o port accesses for the emulated pci device? */
439 porten(struct pci_devinst *pi)
443 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND);
445 return (cmd & PCIM_CMD_PORTEN);
448 /* Are we decoding memory accesses for the emulated pci device? */
450 memen(struct pci_devinst *pi)
454 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND);
456 return (cmd & PCIM_CMD_MEMEN);
460 * Update the MMIO or I/O address that is decoded by the BAR register.
462 * If the pci device has enabled the address space decoding then intercept
463 * the address range decoded by the BAR register.
466 update_bar_address(struct pci_devinst *pi, uint64_t addr, int idx, int type)
470 if (pi->pi_bar[idx].type == PCIBAR_IO)
476 unregister_bar(pi, idx);
481 pi->pi_bar[idx].addr = addr;
484 pi->pi_bar[idx].addr &= ~0xffffffffUL;
485 pi->pi_bar[idx].addr |= addr;
488 pi->pi_bar[idx].addr &= 0xffffffff;
489 pi->pi_bar[idx].addr |= addr;
496 register_bar(pi, idx);
500 pci_emul_alloc_pbar(struct pci_devinst *pdi, int idx, uint64_t hostbase,
501 enum pcibar_type type, uint64_t size)
504 uint64_t *baseptr, limit, addr, mask, lobits, bar;
506 assert(idx >= 0 && idx <= PCI_BARMAX);
508 if ((size & (size - 1)) != 0)
509 size = 1UL << flsl(size); /* round up to a power of 2 */
511 /* Enforce minimum BAR sizes required by the PCI standard */
512 if (type == PCIBAR_IO) {
523 addr = mask = lobits = 0;
527 pci_slotinfo[pdi->pi_slot][pdi->pi_func].si_legacy) {
528 assert(hostbase < PCI_EMUL_IOBASE);
531 baseptr = &pci_emul_iobase;
533 limit = PCI_EMUL_IOLIMIT;
534 mask = PCIM_BAR_IO_BASE;
535 lobits = PCIM_BAR_IO_SPACE;
540 * Some drivers do not work well if the 64-bit BAR is allocated
541 * above 4GB. Allow for this by allocating small requests under
542 * 4GB unless then allocation size is larger than some arbitrary
543 * number (32MB currently).
545 if (size > 32 * 1024 * 1024) {
547 * XXX special case for device requiring peer-peer DMA
549 if (size == 0x100000000UL)
552 baseptr = &pci_emul_membase64;
553 limit = PCI_EMUL_MEMLIMIT64;
554 mask = PCIM_BAR_MEM_BASE;
555 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64 |
556 PCIM_BAR_MEM_PREFETCH;
559 baseptr = &pci_emul_membase32;
560 limit = PCI_EMUL_MEMLIMIT32;
561 mask = PCIM_BAR_MEM_BASE;
562 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64;
566 baseptr = &pci_emul_membase32;
567 limit = PCI_EMUL_MEMLIMIT32;
568 mask = PCIM_BAR_MEM_BASE;
569 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_32;
572 printf("pci_emul_alloc_base: invalid bar type %d\n", type);
576 if (baseptr != NULL) {
577 error = pci_emul_alloc_resource(baseptr, limit, size, &addr);
582 pdi->pi_bar[idx].type = type;
583 pdi->pi_bar[idx].addr = addr;
584 pdi->pi_bar[idx].size = size;
586 /* Initialize the BAR register in config space */
587 bar = (addr & mask) | lobits;
588 pci_set_cfgdata32(pdi, PCIR_BAR(idx), bar);
590 if (type == PCIBAR_MEM64) {
591 assert(idx + 1 <= PCI_BARMAX);
592 pdi->pi_bar[idx + 1].type = PCIBAR_MEMHI64;
593 pci_set_cfgdata32(pdi, PCIR_BAR(idx + 1), bar >> 32);
596 register_bar(pdi, idx);
601 #define CAP_START_OFFSET 0x40
603 pci_emul_add_capability(struct pci_devinst *pi, u_char *capdata, int caplen)
605 int i, capoff, capid, reallen;
608 static u_char endofcap[4] = {
609 PCIY_RESERVED, 0, 0, 0
612 assert(caplen > 0 && capdata[0] != PCIY_RESERVED);
614 reallen = roundup2(caplen, 4); /* dword aligned */
616 sts = pci_get_cfgdata16(pi, PCIR_STATUS);
617 if ((sts & PCIM_STATUS_CAPPRESENT) == 0) {
618 capoff = CAP_START_OFFSET;
619 pci_set_cfgdata8(pi, PCIR_CAP_PTR, capoff);
620 pci_set_cfgdata16(pi, PCIR_STATUS, sts|PCIM_STATUS_CAPPRESENT);
622 capoff = pci_get_cfgdata8(pi, PCIR_CAP_PTR);
624 assert((capoff & 0x3) == 0);
625 capid = pci_get_cfgdata8(pi, capoff);
626 if (capid == PCIY_RESERVED)
628 capoff = pci_get_cfgdata8(pi, capoff + 1);
632 /* Check if we have enough space */
633 if (capoff + reallen + sizeof(endofcap) > PCI_REGMAX + 1)
636 /* Copy the capability */
637 for (i = 0; i < caplen; i++)
638 pci_set_cfgdata8(pi, capoff + i, capdata[i]);
640 /* Set the next capability pointer */
641 pci_set_cfgdata8(pi, capoff + 1, capoff + reallen);
643 /* Copy of the reserved capability which serves as the end marker */
644 for (i = 0; i < sizeof(endofcap); i++)
645 pci_set_cfgdata8(pi, capoff + reallen + i, endofcap[i]);
650 static struct pci_devemu *
651 pci_emul_finddev(char *name)
653 struct pci_devemu **pdpp, *pdp;
655 SET_FOREACH(pdpp, pci_devemu_set) {
657 if (!strcmp(pdp->pe_emu, name)) {
666 pci_emul_init(struct vmctx *ctx, struct pci_devemu *pde, int slot, int func,
669 struct pci_devinst *pdi;
670 pdi = malloc(sizeof(struct pci_devinst));
671 bzero(pdi, sizeof(*pdi));
678 snprintf(pdi->pi_name, PI_NAMESZ, "%s-pci-%d", pde->pe_emu, slot);
680 /* Disable legacy interrupts */
681 pci_set_cfgdata8(pdi, PCIR_INTLINE, 255);
682 pci_set_cfgdata8(pdi, PCIR_INTPIN, 0);
684 pci_set_cfgdata8(pdi, PCIR_COMMAND,
685 PCIM_CMD_PORTEN | PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN);
687 if ((*pde->pe_init)(ctx, pdi, params) != 0) {
691 pci_slotinfo[slot][func].si_devi = pdi;
696 pci_populate_msicap(struct msicap *msicap, int msgnum, int nextptr)
700 CTASSERT(sizeof(struct msicap) == 14);
702 /* Number of msi messages must be a power of 2 between 1 and 32 */
703 assert((msgnum & (msgnum - 1)) == 0 && msgnum >= 1 && msgnum <= 32);
704 mmc = ffs(msgnum) - 1;
706 bzero(msicap, sizeof(struct msicap));
707 msicap->capid = PCIY_MSI;
708 msicap->nextptr = nextptr;
709 msicap->msgctrl = PCIM_MSICTRL_64BIT | (mmc << 1);
713 pci_emul_add_msicap(struct pci_devinst *pi, int msgnum)
715 struct msicap msicap;
717 pci_populate_msicap(&msicap, msgnum, 0);
719 return (pci_emul_add_capability(pi, (u_char *)&msicap, sizeof(msicap)));
723 pci_populate_msixcap(struct msixcap *msixcap, int msgnum, int barnum,
724 uint32_t msix_tab_size, int nextptr)
726 CTASSERT(sizeof(struct msixcap) == 12);
728 assert(msix_tab_size % 4096 == 0);
730 bzero(msixcap, sizeof(struct msixcap));
731 msixcap->capid = PCIY_MSIX;
732 msixcap->nextptr = nextptr;
735 * Message Control Register, all fields set to
736 * zero except for the Table Size.
737 * Note: Table size N is encoded as N-1
739 msixcap->msgctrl = msgnum - 1;
743 * - MSI-X table start at offset 0
744 * - PBA table starts at a 4K aligned offset after the MSI-X table
746 msixcap->table_info = barnum & PCIM_MSIX_BIR_MASK;
747 msixcap->pba_info = msix_tab_size | (barnum & PCIM_MSIX_BIR_MASK);
751 pci_msix_table_init(struct pci_devinst *pi, int table_entries)
755 assert(table_entries > 0);
756 assert(table_entries <= MAX_MSIX_TABLE_ENTRIES);
758 table_size = table_entries * MSIX_TABLE_ENTRY_SIZE;
759 pi->pi_msix.table = malloc(table_size);
760 bzero(pi->pi_msix.table, table_size);
762 /* set mask bit of vector control register */
763 for (i = 0; i < table_entries; i++)
764 pi->pi_msix.table[i].vector_control |= PCIM_MSIX_VCTRL_MASK;
768 pci_emul_add_msixcap(struct pci_devinst *pi, int msgnum, int barnum)
772 struct msixcap msixcap;
774 assert(msgnum >= 1 && msgnum <= MAX_MSIX_TABLE_ENTRIES);
775 assert(barnum >= 0 && barnum <= PCIR_MAX_BAR_0);
777 tab_size = msgnum * MSIX_TABLE_ENTRY_SIZE;
779 /* Align table size to nearest 4K */
780 tab_size = roundup2(tab_size, 4096);
782 pi->pi_msix.table_bar = barnum;
783 pi->pi_msix.pba_bar = barnum;
784 pi->pi_msix.table_offset = 0;
785 pi->pi_msix.table_count = msgnum;
786 pi->pi_msix.pba_offset = tab_size;
788 /* calculate the MMIO size required for MSI-X PBA */
789 pba_index = (msgnum - 1) / (PBA_TABLE_ENTRY_SIZE * 8);
790 pi->pi_msix.pba_size = (pba_index + 1) * PBA_TABLE_ENTRY_SIZE;
792 pci_msix_table_init(pi, msgnum);
794 pci_populate_msixcap(&msixcap, msgnum, barnum, tab_size, 0);
796 /* allocate memory for MSI-X Table and PBA */
797 pci_emul_alloc_bar(pi, barnum, PCIBAR_MEM32,
798 tab_size + pi->pi_msix.pba_size);
800 return (pci_emul_add_capability(pi, (u_char *)&msixcap,
805 msixcap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
806 int bytes, uint32_t val)
808 uint16_t msgctrl, rwmask;
811 off = offset - capoff;
812 table_bar = pi->pi_msix.table_bar;
813 /* Message Control Register */
814 if (off == 2 && bytes == 2) {
815 rwmask = PCIM_MSIXCTRL_MSIX_ENABLE | PCIM_MSIXCTRL_FUNCTION_MASK;
816 msgctrl = pci_get_cfgdata16(pi, offset);
818 msgctrl |= val & rwmask;
821 pi->pi_msix.enabled = val & PCIM_MSIXCTRL_MSIX_ENABLE;
822 pi->pi_msix.function_mask = val & PCIM_MSIXCTRL_FUNCTION_MASK;
825 CFGWRITE(pi, offset, val, bytes);
829 msicap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
830 int bytes, uint32_t val)
832 uint16_t msgctrl, rwmask, msgdata, mme;
836 * If guest is writing to the message control register make sure
837 * we do not overwrite read-only fields.
839 if ((offset - capoff) == 2 && bytes == 2) {
840 rwmask = PCIM_MSICTRL_MME_MASK | PCIM_MSICTRL_MSI_ENABLE;
841 msgctrl = pci_get_cfgdata16(pi, offset);
843 msgctrl |= val & rwmask;
846 addrlo = pci_get_cfgdata32(pi, capoff + 4);
847 if (msgctrl & PCIM_MSICTRL_64BIT)
848 msgdata = pci_get_cfgdata16(pi, capoff + 12);
850 msgdata = pci_get_cfgdata16(pi, capoff + 8);
853 * XXX check delivery mode, destination mode etc
855 mme = msgctrl & PCIM_MSICTRL_MME_MASK;
856 pi->pi_msi.enabled = msgctrl & PCIM_MSICTRL_MSI_ENABLE ? 1 : 0;
857 if (pi->pi_msi.enabled) {
858 pi->pi_msi.cpu = (addrlo >> 12) & 0xff;
859 pi->pi_msi.vector = msgdata & 0xff;
860 pi->pi_msi.msgnum = 1 << (mme >> 4);
863 pi->pi_msi.vector = 0;
864 pi->pi_msi.msgnum = 0;
868 CFGWRITE(pi, offset, val, bytes);
872 pciecap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
873 int bytes, uint32_t val)
876 /* XXX don't write to the readonly parts */
877 CFGWRITE(pi, offset, val, bytes);
880 #define PCIECAP_VERSION 0x2
882 pci_emul_add_pciecap(struct pci_devinst *pi, int type)
885 struct pciecap pciecap;
887 CTASSERT(sizeof(struct pciecap) == 60);
889 if (type != PCIEM_TYPE_ROOT_PORT)
892 bzero(&pciecap, sizeof(pciecap));
894 pciecap.capid = PCIY_EXPRESS;
895 pciecap.pcie_capabilities = PCIECAP_VERSION | PCIEM_TYPE_ROOT_PORT;
896 pciecap.link_capabilities = 0x411; /* gen1, x1 */
897 pciecap.link_status = 0x11; /* gen1, x1 */
899 err = pci_emul_add_capability(pi, (u_char *)&pciecap, sizeof(pciecap));
904 * This function assumes that 'coff' is in the capabilities region of the
908 pci_emul_capwrite(struct pci_devinst *pi, int offset, int bytes, uint32_t val)
911 uint8_t capoff, nextoff;
913 /* Do not allow un-aligned writes */
914 if ((offset & (bytes - 1)) != 0)
917 /* Find the capability that we want to update */
918 capoff = CAP_START_OFFSET;
920 capid = pci_get_cfgdata8(pi, capoff);
921 if (capid == PCIY_RESERVED)
924 nextoff = pci_get_cfgdata8(pi, capoff + 1);
925 if (offset >= capoff && offset < nextoff)
930 assert(offset >= capoff);
933 * Capability ID and Next Capability Pointer are readonly
935 if (offset == capoff || offset == capoff + 1)
940 msicap_cfgwrite(pi, capoff, offset, bytes, val);
943 msixcap_cfgwrite(pi, capoff, offset, bytes, val);
946 pciecap_cfgwrite(pi, capoff, offset, bytes, val);
954 pci_emul_iscap(struct pci_devinst *pi, int offset)
958 uint8_t capid, lastoff;
961 sts = pci_get_cfgdata16(pi, PCIR_STATUS);
962 if ((sts & PCIM_STATUS_CAPPRESENT) != 0) {
963 lastoff = pci_get_cfgdata8(pi, PCIR_CAP_PTR);
965 assert((lastoff & 0x3) == 0);
966 capid = pci_get_cfgdata8(pi, lastoff);
967 if (capid == PCIY_RESERVED)
969 lastoff = pci_get_cfgdata8(pi, lastoff + 1);
971 if (offset >= CAP_START_OFFSET && offset <= lastoff)
978 pci_emul_fallback_handler(struct vmctx *ctx, int vcpu, int dir, uint64_t addr,
979 int size, uint64_t *val, void *arg1, long arg2)
982 * Ignore writes; return 0xff's for reads. The mem read code
983 * will take care of truncating to the correct size.
985 if (dir == MEM_F_READ) {
986 *val = 0xffffffffffffffff;
993 init_pci(struct vmctx *ctx)
995 struct mem_range memp;
996 struct pci_devemu *pde;
1002 pci_emul_iobase = PCI_EMUL_IOBASE;
1003 pci_emul_membase32 = vm_get_lowmem_limit(ctx);
1004 pci_emul_membase64 = PCI_EMUL_MEMBASE64;
1006 for (slot = 0; slot < MAXSLOTS; slot++) {
1007 for (func = 0; func < MAXFUNCS; func++) {
1008 si = &pci_slotinfo[slot][func];
1009 if (si->si_name != NULL) {
1010 pde = pci_emul_finddev(si->si_name);
1011 assert(pde != NULL);
1012 pci_emul_init(ctx, pde, slot, func,
1019 * Allow ISA IRQs 5,10,11,12, and 15 to be available for
1022 lirq[5].li_generic = 1;
1023 lirq[10].li_generic = 1;
1024 lirq[11].li_generic = 1;
1025 lirq[12].li_generic = 1;
1026 lirq[15].li_generic = 1;
1029 * The guest physical memory map looks like the following:
1030 * [0, lowmem) guest system memory
1031 * [lowmem, lowmem_limit) memory hole (may be absent)
1032 * [lowmem_limit, 4GB) PCI hole (32-bit BAR allocation)
1033 * [4GB, 4GB + highmem)
1035 * Accesses to memory addresses that are not allocated to system
1036 * memory or PCI devices return 0xff's.
1038 error = vm_get_memory_seg(ctx, 0, &lowmem);
1041 memset(&memp, 0, sizeof(struct mem_range));
1042 memp.name = "PCI hole";
1043 memp.flags = MEM_F_RW;
1045 memp.size = (4ULL * 1024 * 1024 * 1024) - lowmem;
1046 memp.handler = pci_emul_fallback_handler;
1048 error = register_mem_fallback(&memp);
1053 pci_msi_enabled(struct pci_devinst *pi)
1055 return (pi->pi_msi.enabled);
1059 pci_msi_msgnum(struct pci_devinst *pi)
1061 if (pi->pi_msi.enabled)
1062 return (pi->pi_msi.msgnum);
1068 pci_msix_enabled(struct pci_devinst *pi)
1071 return (pi->pi_msix.enabled && !pi->pi_msi.enabled);
1075 pci_generate_msix(struct pci_devinst *pi, int index)
1077 struct msix_table_entry *mte;
1079 if (!pci_msix_enabled(pi))
1082 if (pi->pi_msix.function_mask)
1085 if (index >= pi->pi_msix.table_count)
1088 mte = &pi->pi_msix.table[index];
1089 if ((mte->vector_control & PCIM_MSIX_VCTRL_MASK) == 0) {
1090 /* XXX Set PBA bit if interrupt is disabled */
1091 vm_lapic_irq(pi->pi_vmctx,
1092 (mte->addr >> 12) & 0xff, mte->msg_data & 0xff);
1097 pci_generate_msi(struct pci_devinst *pi, int msg)
1100 if (pci_msi_enabled(pi) && msg < pci_msi_msgnum(pi)) {
1101 vm_lapic_irq(pi->pi_vmctx,
1103 pi->pi_msi.vector + msg);
1108 pci_is_legacy(struct pci_devinst *pi)
1111 return (pci_slotinfo[pi->pi_slot][pi->pi_func].si_legacy);
1115 pci_lintr_alloc(struct pci_devinst *pi, int vec)
1119 assert(vec < NLIRQ);
1122 for (i = 0; i < NLIRQ; i++) {
1123 if (lirq[i].li_generic &&
1124 lirq[i].li_owner == NULL) {
1130 if (lirq[vec].li_owner != NULL) {
1136 lirq[vec].li_owner = pi;
1137 pi->pi_lintr_pin = vec;
1143 pci_lintr_request(struct pci_devinst *pi, int vec)
1146 vec = pci_lintr_alloc(pi, vec);
1147 pci_set_cfgdata8(pi, PCIR_INTLINE, vec);
1148 pci_set_cfgdata8(pi, PCIR_INTPIN, 1);
1153 pci_lintr_assert(struct pci_devinst *pi)
1156 assert(pi->pi_lintr_pin);
1157 ioapic_assert_pin(pi->pi_vmctx, pi->pi_lintr_pin);
1161 pci_lintr_deassert(struct pci_devinst *pi)
1164 assert(pi->pi_lintr_pin);
1165 ioapic_deassert_pin(pi->pi_vmctx, pi->pi_lintr_pin);
1169 * Return 1 if the emulated device in 'slot' is a multi-function device.
1170 * Return 0 otherwise.
1173 pci_emul_is_mfdev(int slot)
1178 for (f = 0; f < MAXFUNCS; f++) {
1179 if (pci_slotinfo[slot][f].si_devi != NULL) {
1183 return (numfuncs > 1);
1187 * Ensure that the PCIM_MFDEV bit is properly set (or unset) depending on
1188 * whether or not is a multi-function being emulated in the pci 'slot'.
1191 pci_emul_hdrtype_fixup(int slot, int off, int bytes, uint32_t *rv)
1195 if (off <= PCIR_HDRTYPE && off + bytes > PCIR_HDRTYPE) {
1196 mfdev = pci_emul_is_mfdev(slot);
1206 *rv &= ~(PCIM_MFDEV << 16);
1208 *rv |= (PCIM_MFDEV << 16);
1215 static int cfgbus, cfgslot, cfgfunc, cfgoff;
1218 pci_emul_cfgaddr(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
1219 uint32_t *eax, void *arg)
1225 *eax = (bytes == 2) ? 0xffff : 0xff;
1230 x = (cfgbus << 16) |
1234 *eax = x | CONF1_ENABLE;
1237 cfgoff = x & PCI_REGMAX;
1238 cfgfunc = (x >> 8) & PCI_FUNCMAX;
1239 cfgslot = (x >> 11) & PCI_SLOTMAX;
1240 cfgbus = (x >> 16) & PCI_BUSMAX;
1245 INOUT_PORT(pci_cfgaddr, CONF1_ADDR_PORT, IOPORT_F_INOUT, pci_emul_cfgaddr);
1248 bits_changed(uint32_t old, uint32_t new, uint32_t mask)
1251 return ((old ^ new) & mask);
1255 pci_emul_cmdwrite(struct pci_devinst *pi, uint32_t new, int bytes)
1261 * The command register is at an offset of 4 bytes and thus the
1262 * guest could write 1, 2 or 4 bytes starting at this offset.
1265 old = pci_get_cfgdata16(pi, PCIR_COMMAND); /* stash old value */
1266 CFGWRITE(pi, PCIR_COMMAND, new, bytes); /* update config */
1267 new = pci_get_cfgdata16(pi, PCIR_COMMAND); /* get updated value */
1270 * If the MMIO or I/O address space decoding has changed then
1271 * register/unregister all BARs that decode that address space.
1273 for (i = 0; i < PCI_BARMAX; i++) {
1274 switch (pi->pi_bar[i].type) {
1276 case PCIBAR_MEMHI64:
1279 /* I/O address space decoding changed? */
1280 if (bits_changed(old, new, PCIM_CMD_PORTEN)) {
1282 register_bar(pi, i);
1284 unregister_bar(pi, i);
1289 /* MMIO address space decoding changed? */
1290 if (bits_changed(old, new, PCIM_CMD_MEMEN)) {
1292 register_bar(pi, i);
1294 unregister_bar(pi, i);
1304 pci_emul_cfgdata(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
1305 uint32_t *eax, void *arg)
1307 struct pci_devinst *pi;
1308 struct pci_devemu *pe;
1309 int coff, idx, needcfg;
1310 uint64_t addr, bar, mask;
1312 assert(bytes == 1 || bytes == 2 || bytes == 4);
1315 pi = pci_slotinfo[cfgslot][cfgfunc].si_devi;
1319 coff = cfgoff + (port - CONF1_DATA_PORT);
1322 printf("pcicfg-%s from 0x%0x of %d bytes (%d/%d/%d)\n\r",
1323 in ? "read" : "write", coff, bytes, cfgbus, cfgslot, cfgfunc);
1327 * Just return if there is no device at this cfgslot:cfgfunc or
1328 * if the guest is doing an un-aligned access
1330 if (pi == NULL || (coff & (bytes - 1)) != 0) {
1342 /* Let the device emulation override the default handler */
1343 if (pe->pe_cfgread != NULL) {
1344 needcfg = pe->pe_cfgread(ctx, vcpu, pi,
1352 *eax = pci_get_cfgdata8(pi, coff);
1353 else if (bytes == 2)
1354 *eax = pci_get_cfgdata16(pi, coff);
1356 *eax = pci_get_cfgdata32(pi, coff);
1359 pci_emul_hdrtype_fixup(cfgslot, coff, bytes, eax);
1361 /* Let the device emulation override the default handler */
1362 if (pe->pe_cfgwrite != NULL &&
1363 (*pe->pe_cfgwrite)(ctx, vcpu, pi, coff, bytes, *eax) == 0)
1367 * Special handling for write to BAR registers
1369 if (coff >= PCIR_BAR(0) && coff < PCIR_BAR(PCI_BARMAX + 1)) {
1371 * Ignore writes to BAR registers that are not
1374 if (bytes != 4 || (coff & 0x3) != 0)
1376 idx = (coff - PCIR_BAR(0)) / 4;
1377 mask = ~(pi->pi_bar[idx].size - 1);
1378 switch (pi->pi_bar[idx].type) {
1380 pi->pi_bar[idx].addr = bar = 0;
1385 bar = addr | PCIM_BAR_IO_SPACE;
1387 * Register the new BAR value for interception
1389 if (addr != pi->pi_bar[idx].addr) {
1390 update_bar_address(pi, addr, idx,
1395 addr = bar = *eax & mask;
1396 bar |= PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_32;
1397 if (addr != pi->pi_bar[idx].addr) {
1398 update_bar_address(pi, addr, idx,
1403 addr = bar = *eax & mask;
1404 bar |= PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64 |
1405 PCIM_BAR_MEM_PREFETCH;
1406 if (addr != (uint32_t)pi->pi_bar[idx].addr) {
1407 update_bar_address(pi, addr, idx,
1411 case PCIBAR_MEMHI64:
1412 mask = ~(pi->pi_bar[idx - 1].size - 1);
1413 addr = ((uint64_t)*eax << 32) & mask;
1415 if (bar != pi->pi_bar[idx - 1].addr >> 32) {
1416 update_bar_address(pi, addr, idx - 1,
1423 pci_set_cfgdata32(pi, coff, bar);
1425 } else if (pci_emul_iscap(pi, coff)) {
1426 pci_emul_capwrite(pi, coff, bytes, *eax);
1427 } else if (coff == PCIR_COMMAND) {
1428 pci_emul_cmdwrite(pi, *eax, bytes);
1430 CFGWRITE(pi, coff, *eax, bytes);
1437 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+0, IOPORT_F_INOUT, pci_emul_cfgdata);
1438 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+1, IOPORT_F_INOUT, pci_emul_cfgdata);
1439 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+2, IOPORT_F_INOUT, pci_emul_cfgdata);
1440 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+3, IOPORT_F_INOUT, pci_emul_cfgdata);
1443 * I/O ports to configure PCI IRQ routing. We ignore all writes to it.
1446 pci_irq_port_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
1447 uint32_t *eax, void *arg)
1452 INOUT_PORT(pci_irq, 0xC00, IOPORT_F_OUT, pci_irq_port_handler);
1453 INOUT_PORT(pci_irq, 0xC01, IOPORT_F_OUT, pci_irq_port_handler);
1455 #define PCI_EMUL_TEST
1456 #ifdef PCI_EMUL_TEST
1458 * Define a dummy test device
1462 struct pci_emul_dsoftc {
1463 uint8_t ioregs[DIOSZ];
1464 uint8_t memregs[DMEMSZ];
1467 #define PCI_EMUL_MSI_MSGS 4
1468 #define PCI_EMUL_MSIX_MSGS 16
1471 pci_emul_dinit(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
1474 struct pci_emul_dsoftc *sc;
1476 sc = malloc(sizeof(struct pci_emul_dsoftc));
1477 memset(sc, 0, sizeof(struct pci_emul_dsoftc));
1481 pci_set_cfgdata16(pi, PCIR_DEVICE, 0x0001);
1482 pci_set_cfgdata16(pi, PCIR_VENDOR, 0x10DD);
1483 pci_set_cfgdata8(pi, PCIR_CLASS, 0x02);
1485 error = pci_emul_add_msicap(pi, PCI_EMUL_MSI_MSGS);
1488 error = pci_emul_alloc_bar(pi, 0, PCIBAR_IO, DIOSZ);
1491 error = pci_emul_alloc_bar(pi, 1, PCIBAR_MEM32, DMEMSZ);
1498 pci_emul_diow(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx,
1499 uint64_t offset, int size, uint64_t value)
1502 struct pci_emul_dsoftc *sc = pi->pi_arg;
1505 if (offset + size > DIOSZ) {
1506 printf("diow: iow too large, offset %ld size %d\n",
1512 sc->ioregs[offset] = value & 0xff;
1513 } else if (size == 2) {
1514 *(uint16_t *)&sc->ioregs[offset] = value & 0xffff;
1515 } else if (size == 4) {
1516 *(uint32_t *)&sc->ioregs[offset] = value;
1518 printf("diow: iow unknown size %d\n", size);
1522 * Special magic value to generate an interrupt
1524 if (offset == 4 && size == 4 && pci_msi_enabled(pi))
1525 pci_generate_msi(pi, value % pci_msi_msgnum(pi));
1527 if (value == 0xabcdef) {
1528 for (i = 0; i < pci_msi_msgnum(pi); i++)
1529 pci_generate_msi(pi, i);
1534 if (offset + size > DMEMSZ) {
1535 printf("diow: memw too large, offset %ld size %d\n",
1541 sc->memregs[offset] = value;
1542 } else if (size == 2) {
1543 *(uint16_t *)&sc->memregs[offset] = value;
1544 } else if (size == 4) {
1545 *(uint32_t *)&sc->memregs[offset] = value;
1546 } else if (size == 8) {
1547 *(uint64_t *)&sc->memregs[offset] = value;
1549 printf("diow: memw unknown size %d\n", size);
1553 * magic interrupt ??
1558 printf("diow: unknown bar idx %d\n", baridx);
1563 pci_emul_dior(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx,
1564 uint64_t offset, int size)
1566 struct pci_emul_dsoftc *sc = pi->pi_arg;
1570 if (offset + size > DIOSZ) {
1571 printf("dior: ior too large, offset %ld size %d\n",
1577 value = sc->ioregs[offset];
1578 } else if (size == 2) {
1579 value = *(uint16_t *) &sc->ioregs[offset];
1580 } else if (size == 4) {
1581 value = *(uint32_t *) &sc->ioregs[offset];
1583 printf("dior: ior unknown size %d\n", size);
1588 if (offset + size > DMEMSZ) {
1589 printf("dior: memr too large, offset %ld size %d\n",
1595 value = sc->memregs[offset];
1596 } else if (size == 2) {
1597 value = *(uint16_t *) &sc->memregs[offset];
1598 } else if (size == 4) {
1599 value = *(uint32_t *) &sc->memregs[offset];
1600 } else if (size == 8) {
1601 value = *(uint64_t *) &sc->memregs[offset];
1603 printf("dior: ior unknown size %d\n", size);
1609 printf("dior: unknown bar idx %d\n", baridx);
1616 struct pci_devemu pci_dummy = {
1618 .pe_init = pci_emul_dinit,
1619 .pe_barwrite = pci_emul_diow,
1620 .pe_barread = pci_emul_dior
1622 PCI_EMUL_SET(pci_dummy);
1624 #endif /* PCI_EMUL_TEST */