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>
54 #define CONF1_ADDR_PORT 0x0cf8
55 #define CONF1_DATA_PORT 0x0cfc
57 #define CFGWRITE(pi,off,val,b) \
60 pci_set_cfgdata8((pi),(off),(val)); \
61 } else if ((b) == 2) { \
62 pci_set_cfgdata16((pi),(off),(val)); \
64 pci_set_cfgdata32((pi),(off),(val)); \
68 #define MAXSLOTS (PCI_SLOTMAX + 1)
69 #define MAXFUNCS (PCI_FUNCMAX + 1)
71 static struct slotinfo {
74 struct pci_devinst *si_devi;
76 } pci_slotinfo[MAXSLOTS][MAXFUNCS];
79 * Used to keep track of legacy interrupt owners/requestors
83 static struct lirqinfo {
86 struct pci_devinst *li_owner; /* XXX should be a list */
89 SET_DECLARE(pci_devemu_set, struct pci_devemu);
91 static uint32_t pci_hole_startaddr;
93 static uint64_t pci_emul_iobase;
94 static uint64_t pci_emul_membase32;
95 static uint64_t pci_emul_membase64;
97 #define PCI_EMUL_IOBASE 0x2000
98 #define PCI_EMUL_IOLIMIT 0x10000
100 #define PCI_EMUL_MEMLIMIT32 0xE0000000 /* 3.5GB */
102 #define PCI_EMUL_MEMBASE64 0xD000000000UL
103 #define PCI_EMUL_MEMLIMIT64 0xFD00000000UL
105 static int pci_emul_devices;
112 * Slot options are in the form:
114 * <slot>[:<func>],<emul>[,<config>]
118 * emul is a string describing the type of PCI device e.g. virtio-net
119 * config is an optional string, depending on the device, that can be
120 * used for configuration.
126 pci_parse_slot_usage(char *aopt)
128 printf("Invalid PCI slot info field \"%s\"\n", aopt);
133 pci_parse_slot(char *opt, int legacy)
135 char *slot, *func, *emul, *config;
139 str = cpy = strdup(opt);
143 if (strchr(str, ':') != NULL) {
144 slot = strsep(&str, ":");
145 func = strsep(&str, ",");
147 slot = strsep(&str, ",");
151 emul = strsep(&str, ",");
153 config = strsep(&str, ",");
157 pci_parse_slot_usage(cpy);
162 fnum = func ? atoi(func) : 0;
163 if (snum < 0 || snum >= MAXSLOTS || fnum < 0 || fnum >= MAXFUNCS) {
164 pci_parse_slot_usage(cpy);
166 pci_slotinfo[snum][fnum].si_name = emul;
167 pci_slotinfo[snum][fnum].si_param = config;
168 pci_slotinfo[snum][fnum].si_legacy = legacy;
173 pci_valid_pba_offset(struct pci_devinst *pi, uint64_t offset)
176 if (offset < pi->pi_msix.pba_offset)
179 if (offset >= pi->pi_msix.pba_offset + pi->pi_msix.pba_size) {
187 pci_emul_msix_twrite(struct pci_devinst *pi, uint64_t offset, int size,
190 int msix_entry_offset;
194 /* support only 4 or 8 byte writes */
195 if (size != 4 && size != 8)
199 * Return if table index is beyond what device supports
201 tab_index = offset / MSIX_TABLE_ENTRY_SIZE;
202 if (tab_index >= pi->pi_msix.table_count)
205 msix_entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
207 /* support only aligned writes */
208 if ((msix_entry_offset % size) != 0)
211 dest = (char *)(pi->pi_msix.table + tab_index);
212 dest += msix_entry_offset;
215 *((uint32_t *)dest) = value;
217 *((uint64_t *)dest) = value;
223 pci_emul_msix_tread(struct pci_devinst *pi, uint64_t offset, int size)
226 int msix_entry_offset;
228 uint64_t retval = ~0;
230 /* support only 4 or 8 byte reads */
231 if (size != 4 && size != 8)
234 msix_entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
236 /* support only aligned reads */
237 if ((msix_entry_offset % size) != 0) {
241 tab_index = offset / MSIX_TABLE_ENTRY_SIZE;
243 if (tab_index < pi->pi_msix.table_count) {
244 /* valid MSI-X Table access */
245 dest = (char *)(pi->pi_msix.table + tab_index);
246 dest += msix_entry_offset;
249 retval = *((uint32_t *)dest);
251 retval = *((uint64_t *)dest);
252 } else if (pci_valid_pba_offset(pi, offset)) {
253 /* return 0 for PBA access */
261 pci_msix_table_bar(struct pci_devinst *pi)
264 if (pi->pi_msix.table != NULL)
265 return (pi->pi_msix.table_bar);
271 pci_msix_pba_bar(struct pci_devinst *pi)
274 if (pi->pi_msix.table != NULL)
275 return (pi->pi_msix.pba_bar);
281 pci_emul_io_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
282 uint32_t *eax, void *arg)
284 struct pci_devinst *pdi = arg;
285 struct pci_devemu *pe = pdi->pi_d;
289 for (i = 0; i <= PCI_BARMAX; i++) {
290 if (pdi->pi_bar[i].type == PCIBAR_IO &&
291 port >= pdi->pi_bar[i].addr &&
292 port + bytes <= pdi->pi_bar[i].addr + pdi->pi_bar[i].size) {
293 offset = port - pdi->pi_bar[i].addr;
295 *eax = (*pe->pe_barread)(ctx, vcpu, pdi, i,
298 (*pe->pe_barwrite)(ctx, vcpu, pdi, i, offset,
307 pci_emul_mem_handler(struct vmctx *ctx, int vcpu, int dir, uint64_t addr,
308 int size, uint64_t *val, void *arg1, long arg2)
310 struct pci_devinst *pdi = arg1;
311 struct pci_devemu *pe = pdi->pi_d;
313 int bidx = (int) arg2;
315 assert(bidx <= PCI_BARMAX);
316 assert(pdi->pi_bar[bidx].type == PCIBAR_MEM32 ||
317 pdi->pi_bar[bidx].type == PCIBAR_MEM64);
318 assert(addr >= pdi->pi_bar[bidx].addr &&
319 addr + size <= pdi->pi_bar[bidx].addr + pdi->pi_bar[bidx].size);
321 offset = addr - pdi->pi_bar[bidx].addr;
323 if (dir == MEM_F_WRITE)
324 (*pe->pe_barwrite)(ctx, vcpu, pdi, bidx, offset, size, *val);
326 *val = (*pe->pe_barread)(ctx, vcpu, pdi, bidx, offset, size);
333 pci_emul_alloc_resource(uint64_t *baseptr, uint64_t limit, uint64_t size,
338 assert((size & (size - 1)) == 0); /* must be a power of 2 */
340 base = roundup2(*baseptr, size);
342 if (base + size <= limit) {
344 *baseptr = base + size;
351 pci_emul_alloc_bar(struct pci_devinst *pdi, int idx, enum pcibar_type type,
355 return (pci_emul_alloc_pbar(pdi, idx, 0, type, size));
359 * Register (or unregister) the MMIO or I/O region associated with the BAR
360 * register 'idx' of an emulated pci device.
363 modify_bar_registration(struct pci_devinst *pi, int idx, int registration)
366 struct inout_port iop;
369 switch (pi->pi_bar[idx].type) {
371 bzero(&iop, sizeof(struct inout_port));
372 iop.name = pi->pi_name;
373 iop.port = pi->pi_bar[idx].addr;
374 iop.size = pi->pi_bar[idx].size;
376 iop.flags = IOPORT_F_INOUT;
377 iop.handler = pci_emul_io_handler;
379 error = register_inout(&iop);
381 error = unregister_inout(&iop);
385 bzero(&mr, sizeof(struct mem_range));
386 mr.name = pi->pi_name;
387 mr.base = pi->pi_bar[idx].addr;
388 mr.size = pi->pi_bar[idx].size;
391 mr.handler = pci_emul_mem_handler;
394 error = register_mem(&mr);
396 error = unregister_mem(&mr);
406 unregister_bar(struct pci_devinst *pi, int idx)
409 modify_bar_registration(pi, idx, 0);
413 register_bar(struct pci_devinst *pi, int idx)
416 modify_bar_registration(pi, idx, 1);
419 /* Are we decoding i/o port accesses for the emulated pci device? */
421 porten(struct pci_devinst *pi)
425 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND);
427 return (cmd & PCIM_CMD_PORTEN);
430 /* Are we decoding memory accesses for the emulated pci device? */
432 memen(struct pci_devinst *pi)
436 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND);
438 return (cmd & PCIM_CMD_MEMEN);
442 * Update the MMIO or I/O address that is decoded by the BAR register.
444 * If the pci device has enabled the address space decoding then intercept
445 * the address range decoded by the BAR register.
448 update_bar_address(struct pci_devinst *pi, uint64_t addr, int idx, int type)
452 if (pi->pi_bar[idx].type == PCIBAR_IO)
458 unregister_bar(pi, idx);
463 pi->pi_bar[idx].addr = addr;
466 pi->pi_bar[idx].addr &= ~0xffffffffUL;
467 pi->pi_bar[idx].addr |= addr;
470 pi->pi_bar[idx].addr &= 0xffffffff;
471 pi->pi_bar[idx].addr |= addr;
478 register_bar(pi, idx);
482 pci_emul_alloc_pbar(struct pci_devinst *pdi, int idx, uint64_t hostbase,
483 enum pcibar_type type, uint64_t size)
486 uint64_t *baseptr, limit, addr, mask, lobits, bar;
488 assert(idx >= 0 && idx <= PCI_BARMAX);
490 if ((size & (size - 1)) != 0)
491 size = 1UL << flsl(size); /* round up to a power of 2 */
493 /* Enforce minimum BAR sizes required by the PCI standard */
494 if (type == PCIBAR_IO) {
505 addr = mask = lobits = 0;
509 pci_slotinfo[pdi->pi_slot][pdi->pi_func].si_legacy) {
510 assert(hostbase < PCI_EMUL_IOBASE);
513 baseptr = &pci_emul_iobase;
515 limit = PCI_EMUL_IOLIMIT;
516 mask = PCIM_BAR_IO_BASE;
517 lobits = PCIM_BAR_IO_SPACE;
522 * Some drivers do not work well if the 64-bit BAR is allocated
523 * above 4GB. Allow for this by allocating small requests under
524 * 4GB unless then allocation size is larger than some arbitrary
525 * number (32MB currently).
527 if (size > 32 * 1024 * 1024) {
529 * XXX special case for device requiring peer-peer DMA
531 if (size == 0x100000000UL)
534 baseptr = &pci_emul_membase64;
535 limit = PCI_EMUL_MEMLIMIT64;
536 mask = PCIM_BAR_MEM_BASE;
537 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64 |
538 PCIM_BAR_MEM_PREFETCH;
541 baseptr = &pci_emul_membase32;
542 limit = PCI_EMUL_MEMLIMIT32;
543 mask = PCIM_BAR_MEM_BASE;
544 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64;
548 baseptr = &pci_emul_membase32;
549 limit = PCI_EMUL_MEMLIMIT32;
550 mask = PCIM_BAR_MEM_BASE;
551 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_32;
554 printf("pci_emul_alloc_base: invalid bar type %d\n", type);
558 if (baseptr != NULL) {
559 error = pci_emul_alloc_resource(baseptr, limit, size, &addr);
564 pdi->pi_bar[idx].type = type;
565 pdi->pi_bar[idx].addr = addr;
566 pdi->pi_bar[idx].size = size;
568 /* Initialize the BAR register in config space */
569 bar = (addr & mask) | lobits;
570 pci_set_cfgdata32(pdi, PCIR_BAR(idx), bar);
572 if (type == PCIBAR_MEM64) {
573 assert(idx + 1 <= PCI_BARMAX);
574 pdi->pi_bar[idx + 1].type = PCIBAR_MEMHI64;
575 pci_set_cfgdata32(pdi, PCIR_BAR(idx + 1), bar >> 32);
578 register_bar(pdi, idx);
583 #define CAP_START_OFFSET 0x40
585 pci_emul_add_capability(struct pci_devinst *pi, u_char *capdata, int caplen)
587 int i, capoff, capid, reallen;
590 static u_char endofcap[4] = {
591 PCIY_RESERVED, 0, 0, 0
594 assert(caplen > 0 && capdata[0] != PCIY_RESERVED);
596 reallen = roundup2(caplen, 4); /* dword aligned */
598 sts = pci_get_cfgdata16(pi, PCIR_STATUS);
599 if ((sts & PCIM_STATUS_CAPPRESENT) == 0) {
600 capoff = CAP_START_OFFSET;
601 pci_set_cfgdata8(pi, PCIR_CAP_PTR, capoff);
602 pci_set_cfgdata16(pi, PCIR_STATUS, sts|PCIM_STATUS_CAPPRESENT);
604 capoff = pci_get_cfgdata8(pi, PCIR_CAP_PTR);
606 assert((capoff & 0x3) == 0);
607 capid = pci_get_cfgdata8(pi, capoff);
608 if (capid == PCIY_RESERVED)
610 capoff = pci_get_cfgdata8(pi, capoff + 1);
614 /* Check if we have enough space */
615 if (capoff + reallen + sizeof(endofcap) > PCI_REGMAX + 1)
618 /* Copy the capability */
619 for (i = 0; i < caplen; i++)
620 pci_set_cfgdata8(pi, capoff + i, capdata[i]);
622 /* Set the next capability pointer */
623 pci_set_cfgdata8(pi, capoff + 1, capoff + reallen);
625 /* Copy of the reserved capability which serves as the end marker */
626 for (i = 0; i < sizeof(endofcap); i++)
627 pci_set_cfgdata8(pi, capoff + reallen + i, endofcap[i]);
632 static struct pci_devemu *
633 pci_emul_finddev(char *name)
635 struct pci_devemu **pdpp, *pdp;
637 SET_FOREACH(pdpp, pci_devemu_set) {
639 if (!strcmp(pdp->pe_emu, name)) {
648 pci_emul_init(struct vmctx *ctx, struct pci_devemu *pde, int slot, int func,
651 struct pci_devinst *pdi;
652 pdi = malloc(sizeof(struct pci_devinst));
653 bzero(pdi, sizeof(*pdi));
660 snprintf(pdi->pi_name, PI_NAMESZ, "%s-pci-%d", pde->pe_emu, slot);
662 /* Disable legacy interrupts */
663 pci_set_cfgdata8(pdi, PCIR_INTLINE, 255);
664 pci_set_cfgdata8(pdi, PCIR_INTPIN, 0);
666 pci_set_cfgdata8(pdi, PCIR_COMMAND,
667 PCIM_CMD_PORTEN | PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN);
669 if ((*pde->pe_init)(ctx, pdi, params) != 0) {
673 pci_slotinfo[slot][func].si_devi = pdi;
678 pci_populate_msicap(struct msicap *msicap, int msgnum, int nextptr)
682 CTASSERT(sizeof(struct msicap) == 14);
684 /* Number of msi messages must be a power of 2 between 1 and 32 */
685 assert((msgnum & (msgnum - 1)) == 0 && msgnum >= 1 && msgnum <= 32);
686 mmc = ffs(msgnum) - 1;
688 bzero(msicap, sizeof(struct msicap));
689 msicap->capid = PCIY_MSI;
690 msicap->nextptr = nextptr;
691 msicap->msgctrl = PCIM_MSICTRL_64BIT | (mmc << 1);
695 pci_emul_add_msicap(struct pci_devinst *pi, int msgnum)
697 struct msicap msicap;
699 pci_populate_msicap(&msicap, msgnum, 0);
701 return (pci_emul_add_capability(pi, (u_char *)&msicap, sizeof(msicap)));
705 pci_populate_msixcap(struct msixcap *msixcap, int msgnum, int barnum,
706 uint32_t msix_tab_size, int nextptr)
708 CTASSERT(sizeof(struct msixcap) == 12);
710 assert(msix_tab_size % 4096 == 0);
712 bzero(msixcap, sizeof(struct msixcap));
713 msixcap->capid = PCIY_MSIX;
714 msixcap->nextptr = nextptr;
717 * Message Control Register, all fields set to
718 * zero except for the Table Size.
719 * Note: Table size N is encoded as N-1
721 msixcap->msgctrl = msgnum - 1;
725 * - MSI-X table start at offset 0
726 * - PBA table starts at a 4K aligned offset after the MSI-X table
728 msixcap->table_info = barnum & PCIM_MSIX_BIR_MASK;
729 msixcap->pba_info = msix_tab_size | (barnum & PCIM_MSIX_BIR_MASK);
733 pci_msix_table_init(struct pci_devinst *pi, int table_entries)
737 assert(table_entries > 0);
738 assert(table_entries <= MAX_MSIX_TABLE_ENTRIES);
740 table_size = table_entries * MSIX_TABLE_ENTRY_SIZE;
741 pi->pi_msix.table = malloc(table_size);
742 bzero(pi->pi_msix.table, table_size);
744 /* set mask bit of vector control register */
745 for (i = 0; i < table_entries; i++)
746 pi->pi_msix.table[i].vector_control |= PCIM_MSIX_VCTRL_MASK;
750 pci_emul_add_msixcap(struct pci_devinst *pi, int msgnum, int barnum)
754 struct msixcap msixcap;
756 assert(msgnum >= 1 && msgnum <= MAX_MSIX_TABLE_ENTRIES);
757 assert(barnum >= 0 && barnum <= PCIR_MAX_BAR_0);
759 tab_size = msgnum * MSIX_TABLE_ENTRY_SIZE;
761 /* Align table size to nearest 4K */
762 tab_size = roundup2(tab_size, 4096);
764 pi->pi_msix.table_bar = barnum;
765 pi->pi_msix.pba_bar = barnum;
766 pi->pi_msix.table_offset = 0;
767 pi->pi_msix.table_count = msgnum;
768 pi->pi_msix.pba_offset = tab_size;
770 /* calculate the MMIO size required for MSI-X PBA */
771 pba_index = (msgnum - 1) / (PBA_TABLE_ENTRY_SIZE * 8);
772 pi->pi_msix.pba_size = (pba_index + 1) * PBA_TABLE_ENTRY_SIZE;
774 pci_msix_table_init(pi, msgnum);
776 pci_populate_msixcap(&msixcap, msgnum, barnum, tab_size, 0);
778 /* allocate memory for MSI-X Table and PBA */
779 pci_emul_alloc_bar(pi, barnum, PCIBAR_MEM32,
780 tab_size + pi->pi_msix.pba_size);
782 return (pci_emul_add_capability(pi, (u_char *)&msixcap,
787 msixcap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
788 int bytes, uint32_t val)
790 uint16_t msgctrl, rwmask;
793 off = offset - capoff;
794 table_bar = pi->pi_msix.table_bar;
795 /* Message Control Register */
796 if (off == 2 && bytes == 2) {
797 rwmask = PCIM_MSIXCTRL_MSIX_ENABLE | PCIM_MSIXCTRL_FUNCTION_MASK;
798 msgctrl = pci_get_cfgdata16(pi, offset);
800 msgctrl |= val & rwmask;
803 pi->pi_msix.enabled = val & PCIM_MSIXCTRL_MSIX_ENABLE;
804 pi->pi_msix.function_mask = val & PCIM_MSIXCTRL_FUNCTION_MASK;
807 CFGWRITE(pi, offset, val, bytes);
811 msicap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
812 int bytes, uint32_t val)
814 uint16_t msgctrl, rwmask, msgdata, mme;
818 * If guest is writing to the message control register make sure
819 * we do not overwrite read-only fields.
821 if ((offset - capoff) == 2 && bytes == 2) {
822 rwmask = PCIM_MSICTRL_MME_MASK | PCIM_MSICTRL_MSI_ENABLE;
823 msgctrl = pci_get_cfgdata16(pi, offset);
825 msgctrl |= val & rwmask;
828 addrlo = pci_get_cfgdata32(pi, capoff + 4);
829 if (msgctrl & PCIM_MSICTRL_64BIT)
830 msgdata = pci_get_cfgdata16(pi, capoff + 12);
832 msgdata = pci_get_cfgdata16(pi, capoff + 8);
835 * XXX check delivery mode, destination mode etc
837 mme = msgctrl & PCIM_MSICTRL_MME_MASK;
838 pi->pi_msi.enabled = msgctrl & PCIM_MSICTRL_MSI_ENABLE ? 1 : 0;
839 if (pi->pi_msi.enabled) {
840 pi->pi_msi.cpu = (addrlo >> 12) & 0xff;
841 pi->pi_msi.vector = msgdata & 0xff;
842 pi->pi_msi.msgnum = 1 << (mme >> 4);
845 pi->pi_msi.vector = 0;
846 pi->pi_msi.msgnum = 0;
850 CFGWRITE(pi, offset, val, bytes);
854 pciecap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
855 int bytes, uint32_t val)
858 /* XXX don't write to the readonly parts */
859 CFGWRITE(pi, offset, val, bytes);
862 #define PCIECAP_VERSION 0x2
864 pci_emul_add_pciecap(struct pci_devinst *pi, int type)
867 struct pciecap pciecap;
869 CTASSERT(sizeof(struct pciecap) == 60);
871 if (type != PCIEM_TYPE_ROOT_PORT)
874 bzero(&pciecap, sizeof(pciecap));
876 pciecap.capid = PCIY_EXPRESS;
877 pciecap.pcie_capabilities = PCIECAP_VERSION | PCIEM_TYPE_ROOT_PORT;
878 pciecap.link_capabilities = 0x411; /* gen1, x1 */
879 pciecap.link_status = 0x11; /* gen1, x1 */
881 err = pci_emul_add_capability(pi, (u_char *)&pciecap, sizeof(pciecap));
886 * This function assumes that 'coff' is in the capabilities region of the
890 pci_emul_capwrite(struct pci_devinst *pi, int offset, int bytes, uint32_t val)
893 uint8_t capoff, nextoff;
895 /* Do not allow un-aligned writes */
896 if ((offset & (bytes - 1)) != 0)
899 /* Find the capability that we want to update */
900 capoff = CAP_START_OFFSET;
902 capid = pci_get_cfgdata8(pi, capoff);
903 if (capid == PCIY_RESERVED)
906 nextoff = pci_get_cfgdata8(pi, capoff + 1);
907 if (offset >= capoff && offset < nextoff)
912 assert(offset >= capoff);
915 * Capability ID and Next Capability Pointer are readonly
917 if (offset == capoff || offset == capoff + 1)
922 msicap_cfgwrite(pi, capoff, offset, bytes, val);
925 msixcap_cfgwrite(pi, capoff, offset, bytes, val);
928 pciecap_cfgwrite(pi, capoff, offset, bytes, val);
936 pci_emul_iscap(struct pci_devinst *pi, int offset)
940 uint8_t capid, lastoff;
943 sts = pci_get_cfgdata16(pi, PCIR_STATUS);
944 if ((sts & PCIM_STATUS_CAPPRESENT) != 0) {
945 lastoff = pci_get_cfgdata8(pi, PCIR_CAP_PTR);
947 assert((lastoff & 0x3) == 0);
948 capid = pci_get_cfgdata8(pi, lastoff);
949 if (capid == PCIY_RESERVED)
951 lastoff = pci_get_cfgdata8(pi, lastoff + 1);
953 if (offset >= CAP_START_OFFSET && offset <= lastoff)
960 pci_emul_fallback_handler(struct vmctx *ctx, int vcpu, int dir, uint64_t addr,
961 int size, uint64_t *val, void *arg1, long arg2)
964 * Ignore writes; return 0xff's for reads. The mem read code
965 * will take care of truncating to the correct size.
967 if (dir == MEM_F_READ) {
968 *val = 0xffffffffffffffff;
975 init_pci(struct vmctx *ctx)
977 struct mem_range memp;
978 struct pci_devemu *pde;
983 pci_hole_startaddr = vm_get_lowmem_limit(ctx);
985 pci_emul_iobase = PCI_EMUL_IOBASE;
986 pci_emul_membase32 = pci_hole_startaddr;
987 pci_emul_membase64 = PCI_EMUL_MEMBASE64;
989 for (slot = 0; slot < MAXSLOTS; slot++) {
990 for (func = 0; func < MAXFUNCS; func++) {
991 si = &pci_slotinfo[slot][func];
992 if (si->si_name != NULL) {
993 pde = pci_emul_finddev(si->si_name);
995 pci_emul_init(ctx, pde, slot, func,
1003 * Allow ISA IRQs 5,10,11,12, and 15 to be available for
1006 lirq[5].li_generic = 1;
1007 lirq[10].li_generic = 1;
1008 lirq[11].li_generic = 1;
1009 lirq[12].li_generic = 1;
1010 lirq[15].li_generic = 1;
1013 * Setup the PCI hole to return 0xff's when accessed in a region
1016 memset(&memp, 0, sizeof(struct mem_range));
1017 memp.name = "PCI hole";
1018 memp.flags = MEM_F_RW;
1019 memp.base = pci_hole_startaddr;
1020 memp.size = (4ULL * 1024 * 1024 * 1024) - pci_hole_startaddr;
1021 memp.handler = pci_emul_fallback_handler;
1023 error = register_mem_fallback(&memp);
1028 pci_msi_enabled(struct pci_devinst *pi)
1030 return (pi->pi_msi.enabled);
1034 pci_msi_msgnum(struct pci_devinst *pi)
1036 if (pi->pi_msi.enabled)
1037 return (pi->pi_msi.msgnum);
1043 pci_msix_enabled(struct pci_devinst *pi)
1046 return (pi->pi_msix.enabled && !pi->pi_msi.enabled);
1050 pci_generate_msix(struct pci_devinst *pi, int index)
1052 struct msix_table_entry *mte;
1054 if (!pci_msix_enabled(pi))
1057 if (pi->pi_msix.function_mask)
1060 if (index >= pi->pi_msix.table_count)
1063 mte = &pi->pi_msix.table[index];
1064 if ((mte->vector_control & PCIM_MSIX_VCTRL_MASK) == 0) {
1065 /* XXX Set PBA bit if interrupt is disabled */
1066 vm_lapic_irq(pi->pi_vmctx,
1067 (mte->addr >> 12) & 0xff, mte->msg_data & 0xff);
1072 pci_generate_msi(struct pci_devinst *pi, int msg)
1075 if (pci_msi_enabled(pi) && msg < pci_msi_msgnum(pi)) {
1076 vm_lapic_irq(pi->pi_vmctx,
1078 pi->pi_msi.vector + msg);
1083 pci_is_legacy(struct pci_devinst *pi)
1086 return (pci_slotinfo[pi->pi_slot][pi->pi_func].si_legacy);
1090 pci_lintr_alloc(struct pci_devinst *pi, int vec)
1094 assert(vec < NLIRQ);
1097 for (i = 0; i < NLIRQ; i++) {
1098 if (lirq[i].li_generic &&
1099 lirq[i].li_owner == NULL) {
1105 if (lirq[vec].li_owner != NULL) {
1111 lirq[vec].li_owner = pi;
1112 pi->pi_lintr_pin = vec;
1118 pci_lintr_request(struct pci_devinst *pi, int vec)
1121 vec = pci_lintr_alloc(pi, vec);
1122 pci_set_cfgdata8(pi, PCIR_INTLINE, vec);
1123 pci_set_cfgdata8(pi, PCIR_INTPIN, 1);
1128 pci_lintr_assert(struct pci_devinst *pi)
1131 assert(pi->pi_lintr_pin);
1132 ioapic_assert_pin(pi->pi_vmctx, pi->pi_lintr_pin);
1136 pci_lintr_deassert(struct pci_devinst *pi)
1139 assert(pi->pi_lintr_pin);
1140 ioapic_deassert_pin(pi->pi_vmctx, pi->pi_lintr_pin);
1144 * Return 1 if the emulated device in 'slot' is a multi-function device.
1145 * Return 0 otherwise.
1148 pci_emul_is_mfdev(int slot)
1153 for (f = 0; f < MAXFUNCS; f++) {
1154 if (pci_slotinfo[slot][f].si_devi != NULL) {
1158 return (numfuncs > 1);
1162 * Ensure that the PCIM_MFDEV bit is properly set (or unset) depending on
1163 * whether or not is a multi-function being emulated in the pci 'slot'.
1166 pci_emul_hdrtype_fixup(int slot, int off, int bytes, uint32_t *rv)
1170 if (off <= PCIR_HDRTYPE && off + bytes > PCIR_HDRTYPE) {
1171 mfdev = pci_emul_is_mfdev(slot);
1181 *rv &= ~(PCIM_MFDEV << 16);
1183 *rv |= (PCIM_MFDEV << 16);
1190 static int cfgbus, cfgslot, cfgfunc, cfgoff;
1193 pci_emul_cfgaddr(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
1194 uint32_t *eax, void *arg)
1204 cfgoff = x & PCI_REGMAX;
1205 cfgfunc = (x >> 8) & PCI_FUNCMAX;
1206 cfgslot = (x >> 11) & PCI_SLOTMAX;
1207 cfgbus = (x >> 16) & PCI_BUSMAX;
1211 INOUT_PORT(pci_cfgaddr, CONF1_ADDR_PORT, IOPORT_F_OUT, pci_emul_cfgaddr);
1214 bits_changed(uint32_t old, uint32_t new, uint32_t mask)
1217 return ((old ^ new) & mask);
1221 pci_emul_cmdwrite(struct pci_devinst *pi, uint32_t new, int bytes)
1227 * The command register is at an offset of 4 bytes and thus the
1228 * guest could write 1, 2 or 4 bytes starting at this offset.
1231 old = pci_get_cfgdata16(pi, PCIR_COMMAND); /* stash old value */
1232 CFGWRITE(pi, PCIR_COMMAND, new, bytes); /* update config */
1233 new = pci_get_cfgdata16(pi, PCIR_COMMAND); /* get updated value */
1236 * If the MMIO or I/O address space decoding has changed then
1237 * register/unregister all BARs that decode that address space.
1239 for (i = 0; i < PCI_BARMAX; i++) {
1240 switch (pi->pi_bar[i].type) {
1242 case PCIBAR_MEMHI64:
1245 /* I/O address space decoding changed? */
1246 if (bits_changed(old, new, PCIM_CMD_PORTEN)) {
1248 register_bar(pi, i);
1250 unregister_bar(pi, i);
1255 /* MMIO address space decoding changed? */
1256 if (bits_changed(old, new, PCIM_CMD_MEMEN)) {
1258 register_bar(pi, i);
1260 unregister_bar(pi, i);
1270 pci_emul_cfgdata(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
1271 uint32_t *eax, void *arg)
1273 struct pci_devinst *pi;
1274 struct pci_devemu *pe;
1275 int coff, idx, needcfg;
1276 uint64_t addr, bar, mask;
1278 assert(bytes == 1 || bytes == 2 || bytes == 4);
1281 pi = pci_slotinfo[cfgslot][cfgfunc].si_devi;
1285 coff = cfgoff + (port - CONF1_DATA_PORT);
1288 printf("pcicfg-%s from 0x%0x of %d bytes (%d/%d/%d)\n\r",
1289 in ? "read" : "write", coff, bytes, cfgbus, cfgslot, cfgfunc);
1293 * Just return if there is no device at this cfgslot:cfgfunc or
1294 * if the guest is doing an un-aligned access
1296 if (pi == NULL || (coff & (bytes - 1)) != 0) {
1308 /* Let the device emulation override the default handler */
1309 if (pe->pe_cfgread != NULL) {
1310 needcfg = pe->pe_cfgread(ctx, vcpu, pi,
1318 *eax = pci_get_cfgdata8(pi, coff);
1319 else if (bytes == 2)
1320 *eax = pci_get_cfgdata16(pi, coff);
1322 *eax = pci_get_cfgdata32(pi, coff);
1325 pci_emul_hdrtype_fixup(cfgslot, coff, bytes, eax);
1327 /* Let the device emulation override the default handler */
1328 if (pe->pe_cfgwrite != NULL &&
1329 (*pe->pe_cfgwrite)(ctx, vcpu, pi, coff, bytes, *eax) == 0)
1333 * Special handling for write to BAR registers
1335 if (coff >= PCIR_BAR(0) && coff < PCIR_BAR(PCI_BARMAX + 1)) {
1337 * Ignore writes to BAR registers that are not
1340 if (bytes != 4 || (coff & 0x3) != 0)
1342 idx = (coff - PCIR_BAR(0)) / 4;
1343 mask = ~(pi->pi_bar[idx].size - 1);
1344 switch (pi->pi_bar[idx].type) {
1346 pi->pi_bar[idx].addr = bar = 0;
1351 bar = addr | PCIM_BAR_IO_SPACE;
1353 * Register the new BAR value for interception
1355 if (addr != pi->pi_bar[idx].addr) {
1356 update_bar_address(pi, addr, idx,
1361 addr = bar = *eax & mask;
1362 bar |= PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_32;
1363 if (addr != pi->pi_bar[idx].addr) {
1364 update_bar_address(pi, addr, idx,
1369 addr = bar = *eax & mask;
1370 bar |= PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64 |
1371 PCIM_BAR_MEM_PREFETCH;
1372 if (addr != (uint32_t)pi->pi_bar[idx].addr) {
1373 update_bar_address(pi, addr, idx,
1377 case PCIBAR_MEMHI64:
1378 mask = ~(pi->pi_bar[idx - 1].size - 1);
1379 addr = ((uint64_t)*eax << 32) & mask;
1381 if (bar != pi->pi_bar[idx - 1].addr >> 32) {
1382 update_bar_address(pi, addr, idx - 1,
1389 pci_set_cfgdata32(pi, coff, bar);
1391 } else if (pci_emul_iscap(pi, coff)) {
1392 pci_emul_capwrite(pi, coff, bytes, *eax);
1393 } else if (coff == PCIR_COMMAND) {
1394 pci_emul_cmdwrite(pi, *eax, bytes);
1396 CFGWRITE(pi, coff, *eax, bytes);
1403 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+0, IOPORT_F_INOUT, pci_emul_cfgdata);
1404 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+1, IOPORT_F_INOUT, pci_emul_cfgdata);
1405 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+2, IOPORT_F_INOUT, pci_emul_cfgdata);
1406 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+3, IOPORT_F_INOUT, pci_emul_cfgdata);
1409 * I/O ports to configure PCI IRQ routing. We ignore all writes to it.
1412 pci_irq_port_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
1413 uint32_t *eax, void *arg)
1418 INOUT_PORT(pci_irq, 0xC00, IOPORT_F_OUT, pci_irq_port_handler);
1419 INOUT_PORT(pci_irq, 0xC01, IOPORT_F_OUT, pci_irq_port_handler);
1421 #define PCI_EMUL_TEST
1422 #ifdef PCI_EMUL_TEST
1424 * Define a dummy test device
1428 struct pci_emul_dsoftc {
1429 uint8_t ioregs[DIOSZ];
1430 uint8_t memregs[DMEMSZ];
1433 #define PCI_EMUL_MSI_MSGS 4
1434 #define PCI_EMUL_MSIX_MSGS 16
1437 pci_emul_dinit(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
1440 struct pci_emul_dsoftc *sc;
1442 sc = malloc(sizeof(struct pci_emul_dsoftc));
1443 memset(sc, 0, sizeof(struct pci_emul_dsoftc));
1447 pci_set_cfgdata16(pi, PCIR_DEVICE, 0x0001);
1448 pci_set_cfgdata16(pi, PCIR_VENDOR, 0x10DD);
1449 pci_set_cfgdata8(pi, PCIR_CLASS, 0x02);
1451 error = pci_emul_add_msicap(pi, PCI_EMUL_MSI_MSGS);
1454 error = pci_emul_alloc_bar(pi, 0, PCIBAR_IO, DIOSZ);
1457 error = pci_emul_alloc_bar(pi, 1, PCIBAR_MEM32, DMEMSZ);
1464 pci_emul_diow(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx,
1465 uint64_t offset, int size, uint64_t value)
1468 struct pci_emul_dsoftc *sc = pi->pi_arg;
1471 if (offset + size > DIOSZ) {
1472 printf("diow: iow too large, offset %ld size %d\n",
1478 sc->ioregs[offset] = value & 0xff;
1479 } else if (size == 2) {
1480 *(uint16_t *)&sc->ioregs[offset] = value & 0xffff;
1481 } else if (size == 4) {
1482 *(uint32_t *)&sc->ioregs[offset] = value;
1484 printf("diow: iow unknown size %d\n", size);
1488 * Special magic value to generate an interrupt
1490 if (offset == 4 && size == 4 && pci_msi_enabled(pi))
1491 pci_generate_msi(pi, value % pci_msi_msgnum(pi));
1493 if (value == 0xabcdef) {
1494 for (i = 0; i < pci_msi_msgnum(pi); i++)
1495 pci_generate_msi(pi, i);
1500 if (offset + size > DMEMSZ) {
1501 printf("diow: memw too large, offset %ld size %d\n",
1507 sc->memregs[offset] = value;
1508 } else if (size == 2) {
1509 *(uint16_t *)&sc->memregs[offset] = value;
1510 } else if (size == 4) {
1511 *(uint32_t *)&sc->memregs[offset] = value;
1512 } else if (size == 8) {
1513 *(uint64_t *)&sc->memregs[offset] = value;
1515 printf("diow: memw unknown size %d\n", size);
1519 * magic interrupt ??
1524 printf("diow: unknown bar idx %d\n", baridx);
1529 pci_emul_dior(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx,
1530 uint64_t offset, int size)
1532 struct pci_emul_dsoftc *sc = pi->pi_arg;
1536 if (offset + size > DIOSZ) {
1537 printf("dior: ior too large, offset %ld size %d\n",
1543 value = sc->ioregs[offset];
1544 } else if (size == 2) {
1545 value = *(uint16_t *) &sc->ioregs[offset];
1546 } else if (size == 4) {
1547 value = *(uint32_t *) &sc->ioregs[offset];
1549 printf("dior: ior unknown size %d\n", size);
1554 if (offset + size > DMEMSZ) {
1555 printf("dior: memr too large, offset %ld size %d\n",
1561 value = sc->memregs[offset];
1562 } else if (size == 2) {
1563 value = *(uint16_t *) &sc->memregs[offset];
1564 } else if (size == 4) {
1565 value = *(uint32_t *) &sc->memregs[offset];
1566 } else if (size == 8) {
1567 value = *(uint64_t *) &sc->memregs[offset];
1569 printf("dior: ior unknown size %d\n", size);
1575 printf("dior: unknown bar idx %d\n", baridx);
1582 struct pci_devemu pci_dummy = {
1584 .pe_init = pci_emul_dinit,
1585 .pe_barwrite = pci_emul_diow,
1586 .pe_barread = pci_emul_dior
1588 PCI_EMUL_SET(pci_dummy);
1590 #endif /* PCI_EMUL_TEST */