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>
42 #include <machine/vmm.h>
52 #define CONF1_ADDR_PORT 0x0cf8
53 #define CONF1_DATA_PORT 0x0cfc
55 #define CFGWRITE(pi,off,val,b) \
58 pci_set_cfgdata8((pi),(off),(val)); \
59 } else if ((b) == 2) { \
60 pci_set_cfgdata16((pi),(off),(val)); \
62 pci_set_cfgdata32((pi),(off),(val)); \
66 #define MAXSLOTS (PCI_SLOTMAX + 1)
67 #define MAXFUNCS (PCI_FUNCMAX + 1)
69 static struct slotinfo {
72 struct pci_devinst *si_devi;
74 } pci_slotinfo[MAXSLOTS][MAXFUNCS];
77 * Used to keep track of legacy interrupt owners/requestors
81 static struct lirqinfo {
84 struct pci_devinst *li_owner; /* XXX should be a list */
87 SET_DECLARE(pci_devemu_set, struct pci_devemu);
89 static uint64_t pci_emul_iobase;
90 static uint64_t pci_emul_membase32;
91 static uint64_t pci_emul_membase64;
93 #define PCI_EMUL_IOBASE 0x2000
94 #define PCI_EMUL_IOLIMIT 0x10000
96 #define PCI_EMUL_MEMBASE32 (lomem_sz)
97 #define PCI_EMUL_MEMLIMIT32 0xE0000000 /* 3.5GB */
99 #define PCI_EMUL_MEMBASE64 0xD000000000UL
100 #define PCI_EMUL_MEMLIMIT64 0xFD00000000UL
102 static int pci_emul_devices;
109 * Slot options are in the form:
111 * <slot>[:<func>],<emul>[,<config>]
115 * emul is a string describing the type of PCI device e.g. virtio-net
116 * config is an optional string, depending on the device, that can be
117 * used for configuration.
123 pci_parse_slot_usage(char *aopt)
125 printf("Invalid PCI slot info field \"%s\"\n", aopt);
130 pci_parse_slot(char *opt, int legacy)
132 char *slot, *func, *emul, *config;
136 str = cpy = strdup(opt);
140 if (strchr(str, ':') != NULL) {
141 slot = strsep(&str, ":");
142 func = strsep(&str, ",");
144 slot = strsep(&str, ",");
148 emul = strsep(&str, ",");
150 config = strsep(&str, ",");
154 pci_parse_slot_usage(cpy);
159 fnum = func ? atoi(func) : 0;
160 if (snum < 0 || snum >= MAXSLOTS || fnum < 0 || fnum >= MAXFUNCS) {
161 pci_parse_slot_usage(cpy);
163 pci_slotinfo[snum][fnum].si_name = emul;
164 pci_slotinfo[snum][fnum].si_param = config;
165 pci_slotinfo[snum][fnum].si_legacy = legacy;
170 pci_valid_pba_offset(struct pci_devinst *pi, uint64_t offset)
173 if (offset < pi->pi_msix.pba_offset)
176 if (offset >= pi->pi_msix.pba_offset + pi->pi_msix.pba_size) {
184 pci_emul_msix_twrite(struct pci_devinst *pi, uint64_t offset, int size,
187 int msix_entry_offset;
191 /* support only 4 or 8 byte writes */
192 if (size != 4 && size != 8)
196 * Return if table index is beyond what device supports
198 tab_index = offset / MSIX_TABLE_ENTRY_SIZE;
199 if (tab_index >= pi->pi_msix.table_count)
202 msix_entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
204 /* support only aligned writes */
205 if ((msix_entry_offset % size) != 0)
208 dest = (char *)(pi->pi_msix.table + tab_index);
209 dest += msix_entry_offset;
212 *((uint32_t *)dest) = value;
214 *((uint64_t *)dest) = value;
220 pci_emul_msix_tread(struct pci_devinst *pi, uint64_t offset, int size)
223 int msix_entry_offset;
225 uint64_t retval = ~0;
227 /* support only 4 or 8 byte reads */
228 if (size != 4 && size != 8)
231 msix_entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
233 /* support only aligned reads */
234 if ((msix_entry_offset % size) != 0) {
238 tab_index = offset / MSIX_TABLE_ENTRY_SIZE;
240 if (tab_index < pi->pi_msix.table_count) {
241 /* valid MSI-X Table access */
242 dest = (char *)(pi->pi_msix.table + tab_index);
243 dest += msix_entry_offset;
246 retval = *((uint32_t *)dest);
248 retval = *((uint64_t *)dest);
249 } else if (pci_valid_pba_offset(pi, offset)) {
250 /* return 0 for PBA access */
258 pci_msix_table_bar(struct pci_devinst *pi)
261 if (pi->pi_msix.table != NULL)
262 return (pi->pi_msix.table_bar);
268 pci_msix_pba_bar(struct pci_devinst *pi)
271 if (pi->pi_msix.table != NULL)
272 return (pi->pi_msix.pba_bar);
278 pci_emul_io_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
279 uint32_t *eax, void *arg)
281 struct pci_devinst *pdi = arg;
282 struct pci_devemu *pe = pdi->pi_d;
286 for (i = 0; i <= PCI_BARMAX; i++) {
287 if (pdi->pi_bar[i].type == PCIBAR_IO &&
288 port >= pdi->pi_bar[i].addr &&
289 port + bytes <= pdi->pi_bar[i].addr + pdi->pi_bar[i].size) {
290 offset = port - pdi->pi_bar[i].addr;
292 *eax = (*pe->pe_barread)(ctx, vcpu, pdi, i,
295 (*pe->pe_barwrite)(ctx, vcpu, pdi, i, offset,
304 pci_emul_mem_handler(struct vmctx *ctx, int vcpu, int dir, uint64_t addr,
305 int size, uint64_t *val, void *arg1, long arg2)
307 struct pci_devinst *pdi = arg1;
308 struct pci_devemu *pe = pdi->pi_d;
310 int bidx = (int) arg2;
312 assert(bidx <= PCI_BARMAX);
313 assert(pdi->pi_bar[bidx].type == PCIBAR_MEM32 ||
314 pdi->pi_bar[bidx].type == PCIBAR_MEM64);
315 assert(addr >= pdi->pi_bar[bidx].addr &&
316 addr + size <= pdi->pi_bar[bidx].addr + pdi->pi_bar[bidx].size);
318 offset = addr - pdi->pi_bar[bidx].addr;
320 if (dir == MEM_F_WRITE)
321 (*pe->pe_barwrite)(ctx, vcpu, pdi, bidx, offset, size, *val);
323 *val = (*pe->pe_barread)(ctx, vcpu, pdi, bidx, offset, size);
330 pci_emul_alloc_resource(uint64_t *baseptr, uint64_t limit, uint64_t size,
335 assert((size & (size - 1)) == 0); /* must be a power of 2 */
337 base = roundup2(*baseptr, size);
339 if (base + size <= limit) {
341 *baseptr = base + size;
348 pci_emul_alloc_bar(struct pci_devinst *pdi, int idx, enum pcibar_type type,
352 return (pci_emul_alloc_pbar(pdi, idx, 0, type, size));
356 pci_emul_alloc_pbar(struct pci_devinst *pdi, int idx, uint64_t hostbase,
357 enum pcibar_type type, uint64_t size)
360 uint64_t *baseptr, limit, addr, mask, lobits, bar;
361 struct inout_port iop;
362 struct mem_range memp;
364 assert(idx >= 0 && idx <= PCI_BARMAX);
366 if ((size & (size - 1)) != 0)
367 size = 1UL << flsl(size); /* round up to a power of 2 */
372 addr = mask = lobits = 0;
376 pci_slotinfo[pdi->pi_slot][pdi->pi_func].si_legacy) {
377 assert(hostbase < PCI_EMUL_IOBASE);
380 baseptr = &pci_emul_iobase;
382 limit = PCI_EMUL_IOLIMIT;
383 mask = PCIM_BAR_IO_BASE;
384 lobits = PCIM_BAR_IO_SPACE;
389 * Some drivers do not work well if the 64-bit BAR is allocated
390 * above 4GB. Allow for this by allocating small requests under
391 * 4GB unless then allocation size is larger than some arbitrary
392 * number (32MB currently).
394 if (size > 32 * 1024 * 1024) {
396 * XXX special case for device requiring peer-peer DMA
398 if (size == 0x100000000UL)
401 baseptr = &pci_emul_membase64;
402 limit = PCI_EMUL_MEMLIMIT64;
403 mask = PCIM_BAR_MEM_BASE;
404 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64 |
405 PCIM_BAR_MEM_PREFETCH;
408 baseptr = &pci_emul_membase32;
409 limit = PCI_EMUL_MEMLIMIT32;
410 mask = PCIM_BAR_MEM_BASE;
411 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64;
415 baseptr = &pci_emul_membase32;
416 limit = PCI_EMUL_MEMLIMIT32;
417 mask = PCIM_BAR_MEM_BASE;
418 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_32;
421 printf("pci_emul_alloc_base: invalid bar type %d\n", type);
425 if (baseptr != NULL) {
426 error = pci_emul_alloc_resource(baseptr, limit, size, &addr);
431 pdi->pi_bar[idx].type = type;
432 pdi->pi_bar[idx].addr = addr;
433 pdi->pi_bar[idx].size = size;
435 /* Initialize the BAR register in config space */
436 bar = (addr & mask) | lobits;
437 pci_set_cfgdata32(pdi, PCIR_BAR(idx), bar);
439 if (type == PCIBAR_MEM64) {
440 assert(idx + 1 <= PCI_BARMAX);
441 pdi->pi_bar[idx + 1].type = PCIBAR_MEMHI64;
442 pci_set_cfgdata32(pdi, PCIR_BAR(idx + 1), bar >> 32);
445 /* add a handler to intercept accesses to the I/O bar */
446 if (type == PCIBAR_IO) {
447 iop.name = pdi->pi_name;
448 iop.flags = IOPORT_F_INOUT;
449 iop.handler = pci_emul_io_handler;
452 for (i = 0; i < size; i++) {
454 register_inout(&iop);
456 } else if (type == PCIBAR_MEM32 || type == PCIBAR_MEM64) {
457 /* add memory bar intercept handler */
458 memp.name = pdi->pi_name;
459 memp.flags = MEM_F_RW;
462 memp.handler = pci_emul_mem_handler;
466 error = register_mem(&memp);
473 #define CAP_START_OFFSET 0x40
475 pci_emul_add_capability(struct pci_devinst *pi, u_char *capdata, int caplen)
477 int i, capoff, capid, reallen;
480 static u_char endofcap[4] = {
481 PCIY_RESERVED, 0, 0, 0
484 assert(caplen > 0 && capdata[0] != PCIY_RESERVED);
486 reallen = roundup2(caplen, 4); /* dword aligned */
488 sts = pci_get_cfgdata16(pi, PCIR_STATUS);
489 if ((sts & PCIM_STATUS_CAPPRESENT) == 0) {
490 capoff = CAP_START_OFFSET;
491 pci_set_cfgdata8(pi, PCIR_CAP_PTR, capoff);
492 pci_set_cfgdata16(pi, PCIR_STATUS, sts|PCIM_STATUS_CAPPRESENT);
494 capoff = pci_get_cfgdata8(pi, PCIR_CAP_PTR);
496 assert((capoff & 0x3) == 0);
497 capid = pci_get_cfgdata8(pi, capoff);
498 if (capid == PCIY_RESERVED)
500 capoff = pci_get_cfgdata8(pi, capoff + 1);
504 /* Check if we have enough space */
505 if (capoff + reallen + sizeof(endofcap) > PCI_REGMAX + 1)
508 /* Copy the capability */
509 for (i = 0; i < caplen; i++)
510 pci_set_cfgdata8(pi, capoff + i, capdata[i]);
512 /* Set the next capability pointer */
513 pci_set_cfgdata8(pi, capoff + 1, capoff + reallen);
515 /* Copy of the reserved capability which serves as the end marker */
516 for (i = 0; i < sizeof(endofcap); i++)
517 pci_set_cfgdata8(pi, capoff + reallen + i, endofcap[i]);
522 static struct pci_devemu *
523 pci_emul_finddev(char *name)
525 struct pci_devemu **pdpp, *pdp;
527 SET_FOREACH(pdpp, pci_devemu_set) {
529 if (!strcmp(pdp->pe_emu, name)) {
538 pci_emul_init(struct vmctx *ctx, struct pci_devemu *pde, int slot, int func,
541 struct pci_devinst *pdi;
542 pdi = malloc(sizeof(struct pci_devinst));
543 bzero(pdi, sizeof(*pdi));
550 snprintf(pdi->pi_name, PI_NAMESZ, "%s-pci-%d", pde->pe_emu, slot);
552 /* Disable legacy interrupts */
553 pci_set_cfgdata8(pdi, PCIR_INTLINE, 255);
554 pci_set_cfgdata8(pdi, PCIR_INTPIN, 0);
556 pci_set_cfgdata8(pdi, PCIR_COMMAND,
557 PCIM_CMD_PORTEN | PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN);
559 if ((*pde->pe_init)(ctx, pdi, params) != 0) {
563 pci_slotinfo[slot][func].si_devi = pdi;
568 pci_populate_msicap(struct msicap *msicap, int msgnum, int nextptr)
572 CTASSERT(sizeof(struct msicap) == 14);
574 /* Number of msi messages must be a power of 2 between 1 and 32 */
575 assert((msgnum & (msgnum - 1)) == 0 && msgnum >= 1 && msgnum <= 32);
576 mmc = ffs(msgnum) - 1;
578 bzero(msicap, sizeof(struct msicap));
579 msicap->capid = PCIY_MSI;
580 msicap->nextptr = nextptr;
581 msicap->msgctrl = PCIM_MSICTRL_64BIT | (mmc << 1);
585 pci_emul_add_msicap(struct pci_devinst *pi, int msgnum)
587 struct msicap msicap;
589 pci_populate_msicap(&msicap, msgnum, 0);
591 return (pci_emul_add_capability(pi, (u_char *)&msicap, sizeof(msicap)));
595 pci_populate_msixcap(struct msixcap *msixcap, int msgnum, int barnum,
596 uint32_t msix_tab_size, int nextptr)
598 CTASSERT(sizeof(struct msixcap) == 12);
600 assert(msix_tab_size % 4096 == 0);
602 bzero(msixcap, sizeof(struct msixcap));
603 msixcap->capid = PCIY_MSIX;
604 msixcap->nextptr = nextptr;
607 * Message Control Register, all fields set to
608 * zero except for the Table Size.
609 * Note: Table size N is encoded as N-1
611 msixcap->msgctrl = msgnum - 1;
615 * - MSI-X table start at offset 0
616 * - PBA table starts at a 4K aligned offset after the MSI-X table
618 msixcap->table_info = barnum & PCIM_MSIX_BIR_MASK;
619 msixcap->pba_info = msix_tab_size | (barnum & PCIM_MSIX_BIR_MASK);
623 pci_msix_table_init(struct pci_devinst *pi, int table_entries)
627 assert(table_entries > 0);
628 assert(table_entries <= MAX_MSIX_TABLE_ENTRIES);
630 table_size = table_entries * MSIX_TABLE_ENTRY_SIZE;
631 pi->pi_msix.table = malloc(table_size);
632 bzero(pi->pi_msix.table, table_size);
634 /* set mask bit of vector control register */
635 for (i = 0; i < table_entries; i++)
636 pi->pi_msix.table[i].vector_control |= PCIM_MSIX_VCTRL_MASK;
640 pci_emul_add_msixcap(struct pci_devinst *pi, int msgnum, int barnum)
644 struct msixcap msixcap;
646 assert(msgnum >= 1 && msgnum <= MAX_MSIX_TABLE_ENTRIES);
647 assert(barnum >= 0 && barnum <= PCIR_MAX_BAR_0);
649 tab_size = msgnum * MSIX_TABLE_ENTRY_SIZE;
651 /* Align table size to nearest 4K */
652 tab_size = roundup2(tab_size, 4096);
654 pi->pi_msix.table_bar = barnum;
655 pi->pi_msix.pba_bar = barnum;
656 pi->pi_msix.table_offset = 0;
657 pi->pi_msix.table_count = msgnum;
658 pi->pi_msix.pba_offset = tab_size;
660 /* calculate the MMIO size required for MSI-X PBA */
661 pba_index = (msgnum - 1) / (PBA_TABLE_ENTRY_SIZE * 8);
662 pi->pi_msix.pba_size = (pba_index + 1) * PBA_TABLE_ENTRY_SIZE;
664 pci_msix_table_init(pi, msgnum);
666 pci_populate_msixcap(&msixcap, msgnum, barnum, tab_size, 0);
668 /* allocate memory for MSI-X Table and PBA */
669 pci_emul_alloc_bar(pi, barnum, PCIBAR_MEM32,
670 tab_size + pi->pi_msix.pba_size);
672 return (pci_emul_add_capability(pi, (u_char *)&msixcap,
677 msixcap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
678 int bytes, uint32_t val)
680 uint16_t msgctrl, rwmask;
683 off = offset - capoff;
684 table_bar = pi->pi_msix.table_bar;
685 /* Message Control Register */
686 if (off == 2 && bytes == 2) {
687 rwmask = PCIM_MSIXCTRL_MSIX_ENABLE | PCIM_MSIXCTRL_FUNCTION_MASK;
688 msgctrl = pci_get_cfgdata16(pi, offset);
690 msgctrl |= val & rwmask;
693 pi->pi_msix.enabled = val & PCIM_MSIXCTRL_MSIX_ENABLE;
694 pi->pi_msix.function_mask = val & PCIM_MSIXCTRL_FUNCTION_MASK;
697 CFGWRITE(pi, offset, val, bytes);
701 msicap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
702 int bytes, uint32_t val)
704 uint16_t msgctrl, rwmask, msgdata, mme;
708 * If guest is writing to the message control register make sure
709 * we do not overwrite read-only fields.
711 if ((offset - capoff) == 2 && bytes == 2) {
712 rwmask = PCIM_MSICTRL_MME_MASK | PCIM_MSICTRL_MSI_ENABLE;
713 msgctrl = pci_get_cfgdata16(pi, offset);
715 msgctrl |= val & rwmask;
718 addrlo = pci_get_cfgdata32(pi, capoff + 4);
719 if (msgctrl & PCIM_MSICTRL_64BIT)
720 msgdata = pci_get_cfgdata16(pi, capoff + 12);
722 msgdata = pci_get_cfgdata16(pi, capoff + 8);
725 * XXX check delivery mode, destination mode etc
727 mme = msgctrl & PCIM_MSICTRL_MME_MASK;
728 pi->pi_msi.enabled = msgctrl & PCIM_MSICTRL_MSI_ENABLE ? 1 : 0;
729 if (pi->pi_msi.enabled) {
730 pi->pi_msi.cpu = (addrlo >> 12) & 0xff;
731 pi->pi_msi.vector = msgdata & 0xff;
732 pi->pi_msi.msgnum = 1 << (mme >> 4);
735 pi->pi_msi.vector = 0;
736 pi->pi_msi.msgnum = 0;
740 CFGWRITE(pi, offset, val, bytes);
744 pciecap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
745 int bytes, uint32_t val)
748 /* XXX don't write to the readonly parts */
749 CFGWRITE(pi, offset, val, bytes);
752 #define PCIECAP_VERSION 0x2
754 pci_emul_add_pciecap(struct pci_devinst *pi, int type)
757 struct pciecap pciecap;
759 CTASSERT(sizeof(struct pciecap) == 60);
761 if (type != PCIEM_TYPE_ROOT_PORT)
764 bzero(&pciecap, sizeof(pciecap));
766 pciecap.capid = PCIY_EXPRESS;
767 pciecap.pcie_capabilities = PCIECAP_VERSION | PCIEM_TYPE_ROOT_PORT;
768 pciecap.link_capabilities = 0x411; /* gen1, x1 */
769 pciecap.link_status = 0x11; /* gen1, x1 */
771 err = pci_emul_add_capability(pi, (u_char *)&pciecap, sizeof(pciecap));
776 * This function assumes that 'coff' is in the capabilities region of the
780 pci_emul_capwrite(struct pci_devinst *pi, int offset, int bytes, uint32_t val)
783 uint8_t capoff, nextoff;
785 /* Do not allow un-aligned writes */
786 if ((offset & (bytes - 1)) != 0)
789 /* Find the capability that we want to update */
790 capoff = CAP_START_OFFSET;
792 capid = pci_get_cfgdata8(pi, capoff);
793 if (capid == PCIY_RESERVED)
796 nextoff = pci_get_cfgdata8(pi, capoff + 1);
797 if (offset >= capoff && offset < nextoff)
802 assert(offset >= capoff);
805 * Capability ID and Next Capability Pointer are readonly
807 if (offset == capoff || offset == capoff + 1)
812 msicap_cfgwrite(pi, capoff, offset, bytes, val);
815 msixcap_cfgwrite(pi, capoff, offset, bytes, val);
818 pciecap_cfgwrite(pi, capoff, offset, bytes, val);
826 pci_emul_iscap(struct pci_devinst *pi, int offset)
830 uint8_t capid, lastoff;
833 sts = pci_get_cfgdata16(pi, PCIR_STATUS);
834 if ((sts & PCIM_STATUS_CAPPRESENT) != 0) {
835 lastoff = pci_get_cfgdata8(pi, PCIR_CAP_PTR);
837 assert((lastoff & 0x3) == 0);
838 capid = pci_get_cfgdata8(pi, lastoff);
839 if (capid == PCIY_RESERVED)
841 lastoff = pci_get_cfgdata8(pi, lastoff + 1);
843 if (offset >= CAP_START_OFFSET && offset <= lastoff)
850 init_pci(struct vmctx *ctx)
852 struct pci_devemu *pde;
856 pci_emul_iobase = PCI_EMUL_IOBASE;
857 pci_emul_membase32 = PCI_EMUL_MEMBASE32;
858 pci_emul_membase64 = PCI_EMUL_MEMBASE64;
860 for (slot = 0; slot < MAXSLOTS; slot++) {
861 for (func = 0; func < MAXFUNCS; func++) {
862 si = &pci_slotinfo[slot][func];
863 if (si->si_name != NULL) {
864 pde = pci_emul_finddev(si->si_name);
866 pci_emul_init(ctx, pde, slot, func,
874 * Allow ISA IRQs 5,10,11,12, and 15 to be available for
877 lirq[5].li_generic = 1;
878 lirq[10].li_generic = 1;
879 lirq[11].li_generic = 1;
880 lirq[12].li_generic = 1;
881 lirq[15].li_generic = 1;
885 pci_msi_enabled(struct pci_devinst *pi)
887 return (pi->pi_msi.enabled);
891 pci_msi_msgnum(struct pci_devinst *pi)
893 if (pi->pi_msi.enabled)
894 return (pi->pi_msi.msgnum);
900 pci_msix_enabled(struct pci_devinst *pi)
903 return (pi->pi_msix.enabled && !pi->pi_msi.enabled);
907 pci_generate_msix(struct pci_devinst *pi, int index)
909 struct msix_table_entry *mte;
911 if (!pci_msix_enabled(pi))
914 if (pi->pi_msix.function_mask)
917 if (index >= pi->pi_msix.table_count)
920 mte = &pi->pi_msix.table[index];
921 if ((mte->vector_control & PCIM_MSIX_VCTRL_MASK) == 0) {
922 /* XXX Set PBA bit if interrupt is disabled */
923 vm_lapic_irq(pi->pi_vmctx,
924 (mte->addr >> 12) & 0xff, mte->msg_data & 0xff);
929 pci_generate_msi(struct pci_devinst *pi, int msg)
932 if (pci_msi_enabled(pi) && msg < pci_msi_msgnum(pi)) {
933 vm_lapic_irq(pi->pi_vmctx,
935 pi->pi_msi.vector + msg);
940 pci_is_legacy(struct pci_devinst *pi)
943 return (pci_slotinfo[pi->pi_slot][pi->pi_func].si_legacy);
947 pci_lintr_alloc(struct pci_devinst *pi, int vec)
954 for (i = 0; i < NLIRQ; i++) {
955 if (lirq[i].li_generic &&
956 lirq[i].li_owner == NULL) {
962 if (lirq[vec].li_owner != NULL) {
968 lirq[vec].li_owner = pi;
969 pi->pi_lintr_pin = vec;
975 pci_lintr_request(struct pci_devinst *pi, int vec)
978 vec = pci_lintr_alloc(pi, vec);
979 pci_set_cfgdata8(pi, PCIR_INTLINE, vec);
980 pci_set_cfgdata8(pi, PCIR_INTPIN, 1);
985 pci_lintr_assert(struct pci_devinst *pi)
988 assert(pi->pi_lintr_pin);
989 ioapic_assert_pin(pi->pi_vmctx, pi->pi_lintr_pin);
993 pci_lintr_deassert(struct pci_devinst *pi)
996 assert(pi->pi_lintr_pin);
997 ioapic_deassert_pin(pi->pi_vmctx, pi->pi_lintr_pin);
1001 * Return 1 if the emulated device in 'slot' is a multi-function device.
1002 * Return 0 otherwise.
1005 pci_emul_is_mfdev(int slot)
1010 for (f = 0; f < MAXFUNCS; f++) {
1011 if (pci_slotinfo[slot][f].si_devi != NULL) {
1015 return (numfuncs > 1);
1019 * Ensure that the PCIM_MFDEV bit is properly set (or unset) depending on
1020 * whether or not is a multi-function being emulated in the pci 'slot'.
1023 pci_emul_hdrtype_fixup(int slot, int off, int bytes, uint32_t *rv)
1027 if (off <= PCIR_HDRTYPE && off + bytes > PCIR_HDRTYPE) {
1028 mfdev = pci_emul_is_mfdev(slot);
1038 *rv &= ~(PCIM_MFDEV << 16);
1040 *rv |= (PCIM_MFDEV << 16);
1047 static int cfgbus, cfgslot, cfgfunc, cfgoff;
1050 pci_emul_cfgaddr(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
1051 uint32_t *eax, void *arg)
1061 cfgoff = x & PCI_REGMAX;
1062 cfgfunc = (x >> 8) & PCI_FUNCMAX;
1063 cfgslot = (x >> 11) & PCI_SLOTMAX;
1064 cfgbus = (x >> 16) & PCI_BUSMAX;
1068 INOUT_PORT(pci_cfgaddr, CONF1_ADDR_PORT, IOPORT_F_OUT, pci_emul_cfgaddr);
1071 pci_emul_cfgdata(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
1072 uint32_t *eax, void *arg)
1074 struct pci_devinst *pi;
1075 struct pci_devemu *pe;
1076 int coff, idx, needcfg;
1079 assert(bytes == 1 || bytes == 2 || bytes == 4);
1082 pi = pci_slotinfo[cfgslot][cfgfunc].si_devi;
1086 coff = cfgoff + (port - CONF1_DATA_PORT);
1089 printf("pcicfg-%s from 0x%0x of %d bytes (%d/%d/%d)\n\r",
1090 in ? "read" : "write", coff, bytes, cfgbus, cfgslot, cfgfunc);
1094 * Just return if there is no device at this cfgslot:cfgfunc or
1095 * if the guest is doing an un-aligned access
1097 if (pi == NULL || (coff & (bytes - 1)) != 0) {
1109 /* Let the device emulation override the default handler */
1110 if (pe->pe_cfgread != NULL) {
1111 needcfg = pe->pe_cfgread(ctx, vcpu, pi,
1119 *eax = pci_get_cfgdata8(pi, coff);
1120 else if (bytes == 2)
1121 *eax = pci_get_cfgdata16(pi, coff);
1123 *eax = pci_get_cfgdata32(pi, coff);
1126 pci_emul_hdrtype_fixup(cfgslot, coff, bytes, eax);
1128 /* Let the device emulation override the default handler */
1129 if (pe->pe_cfgwrite != NULL &&
1130 (*pe->pe_cfgwrite)(ctx, vcpu, pi, coff, bytes, *eax) == 0)
1134 * Special handling for write to BAR registers
1136 if (coff >= PCIR_BAR(0) && coff < PCIR_BAR(PCI_BARMAX + 1)) {
1138 * Ignore writes to BAR registers that are not
1141 if (bytes != 4 || (coff & 0x3) != 0)
1143 idx = (coff - PCIR_BAR(0)) / 4;
1144 switch (pi->pi_bar[idx].type) {
1149 mask = ~(pi->pi_bar[idx].size - 1);
1150 mask &= PCIM_BAR_IO_BASE;
1151 bar = (*eax & mask) | PCIM_BAR_IO_SPACE;
1154 mask = ~(pi->pi_bar[idx].size - 1);
1155 mask &= PCIM_BAR_MEM_BASE;
1157 bar |= PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_32;
1160 mask = ~(pi->pi_bar[idx].size - 1);
1161 mask &= PCIM_BAR_MEM_BASE;
1163 bar |= PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64 |
1164 PCIM_BAR_MEM_PREFETCH;
1166 case PCIBAR_MEMHI64:
1167 mask = ~(pi->pi_bar[idx - 1].size - 1);
1168 mask &= PCIM_BAR_MEM_BASE;
1169 bar = ((uint64_t)*eax << 32) & mask;
1175 pci_set_cfgdata32(pi, coff, bar);
1177 } else if (pci_emul_iscap(pi, coff)) {
1178 pci_emul_capwrite(pi, coff, bytes, *eax);
1180 CFGWRITE(pi, coff, *eax, bytes);
1187 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+0, IOPORT_F_INOUT, pci_emul_cfgdata);
1188 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+1, IOPORT_F_INOUT, pci_emul_cfgdata);
1189 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+2, IOPORT_F_INOUT, pci_emul_cfgdata);
1190 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+3, IOPORT_F_INOUT, pci_emul_cfgdata);
1193 * I/O ports to configure PCI IRQ routing. We ignore all writes to it.
1196 pci_irq_port_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
1197 uint32_t *eax, void *arg)
1202 INOUT_PORT(pci_irq, 0xC00, IOPORT_F_OUT, pci_irq_port_handler);
1203 INOUT_PORT(pci_irq, 0xC01, IOPORT_F_OUT, pci_irq_port_handler);
1205 #define PCI_EMUL_TEST
1206 #ifdef PCI_EMUL_TEST
1208 * Define a dummy test device
1212 struct pci_emul_dsoftc {
1213 uint8_t ioregs[DIOSZ];
1214 uint8_t memregs[DMEMSZ];
1217 #define PCI_EMUL_MSI_MSGS 4
1218 #define PCI_EMUL_MSIX_MSGS 16
1221 pci_emul_dinit(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
1224 struct pci_emul_dsoftc *sc;
1226 sc = malloc(sizeof(struct pci_emul_dsoftc));
1227 memset(sc, 0, sizeof(struct pci_emul_dsoftc));
1231 pci_set_cfgdata16(pi, PCIR_DEVICE, 0x0001);
1232 pci_set_cfgdata16(pi, PCIR_VENDOR, 0x10DD);
1233 pci_set_cfgdata8(pi, PCIR_CLASS, 0x02);
1235 error = pci_emul_add_msicap(pi, PCI_EMUL_MSI_MSGS);
1238 error = pci_emul_alloc_bar(pi, 0, PCIBAR_IO, DIOSZ);
1241 error = pci_emul_alloc_bar(pi, 1, PCIBAR_MEM32, DMEMSZ);
1248 pci_emul_diow(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx,
1249 uint64_t offset, int size, uint64_t value)
1252 struct pci_emul_dsoftc *sc = pi->pi_arg;
1255 if (offset + size > DIOSZ) {
1256 printf("diow: iow too large, offset %ld size %d\n",
1262 sc->ioregs[offset] = value & 0xff;
1263 } else if (size == 2) {
1264 *(uint16_t *)&sc->ioregs[offset] = value & 0xffff;
1265 } else if (size == 4) {
1266 *(uint32_t *)&sc->ioregs[offset] = value;
1268 printf("diow: iow unknown size %d\n", size);
1272 * Special magic value to generate an interrupt
1274 if (offset == 4 && size == 4 && pci_msi_enabled(pi))
1275 pci_generate_msi(pi, value % pci_msi_msgnum(pi));
1277 if (value == 0xabcdef) {
1278 for (i = 0; i < pci_msi_msgnum(pi); i++)
1279 pci_generate_msi(pi, i);
1284 if (offset + size > DMEMSZ) {
1285 printf("diow: memw too large, offset %ld size %d\n",
1291 sc->memregs[offset] = value;
1292 } else if (size == 2) {
1293 *(uint16_t *)&sc->memregs[offset] = value;
1294 } else if (size == 4) {
1295 *(uint32_t *)&sc->memregs[offset] = value;
1296 } else if (size == 8) {
1297 *(uint64_t *)&sc->memregs[offset] = value;
1299 printf("diow: memw unknown size %d\n", size);
1303 * magic interrupt ??
1308 printf("diow: unknown bar idx %d\n", baridx);
1313 pci_emul_dior(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx,
1314 uint64_t offset, int size)
1316 struct pci_emul_dsoftc *sc = pi->pi_arg;
1320 if (offset + size > DIOSZ) {
1321 printf("dior: ior too large, offset %ld size %d\n",
1327 value = sc->ioregs[offset];
1328 } else if (size == 2) {
1329 value = *(uint16_t *) &sc->ioregs[offset];
1330 } else if (size == 4) {
1331 value = *(uint32_t *) &sc->ioregs[offset];
1333 printf("dior: ior unknown size %d\n", size);
1338 if (offset + size > DMEMSZ) {
1339 printf("dior: memr too large, offset %ld size %d\n",
1345 value = sc->memregs[offset];
1346 } else if (size == 2) {
1347 value = *(uint16_t *) &sc->memregs[offset];
1348 } else if (size == 4) {
1349 value = *(uint32_t *) &sc->memregs[offset];
1350 } else if (size == 8) {
1351 value = *(uint64_t *) &sc->memregs[offset];
1353 printf("dior: ior unknown size %d\n", size);
1359 printf("dior: unknown bar idx %d\n", baridx);
1366 struct pci_devemu pci_dummy = {
1368 .pe_init = pci_emul_dinit,
1369 .pe_barwrite = pci_emul_diow,
1370 .pe_barread = pci_emul_dior
1372 PCI_EMUL_SET(pci_dummy);
1374 #endif /* PCI_EMUL_TEST */