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_emul_io_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
259 uint32_t *eax, void *arg)
261 struct pci_devinst *pdi = arg;
262 struct pci_devemu *pe = pdi->pi_d;
266 for (i = 0; i <= PCI_BARMAX; i++) {
267 if (pdi->pi_bar[i].type == PCIBAR_IO &&
268 port >= pdi->pi_bar[i].addr &&
269 port + bytes <= pdi->pi_bar[i].addr + pdi->pi_bar[i].size) {
270 offset = port - pdi->pi_bar[i].addr;
272 *eax = (*pe->pe_barread)(ctx, vcpu, pdi, i,
275 (*pe->pe_barwrite)(ctx, vcpu, pdi, i, offset,
284 pci_emul_mem_handler(struct vmctx *ctx, int vcpu, int dir, uint64_t addr,
285 int size, uint64_t *val, void *arg1, long arg2)
287 struct pci_devinst *pdi = arg1;
288 struct pci_devemu *pe = pdi->pi_d;
290 int bidx = (int) arg2;
292 assert(bidx <= PCI_BARMAX);
293 assert(pdi->pi_bar[bidx].type == PCIBAR_MEM32 ||
294 pdi->pi_bar[bidx].type == PCIBAR_MEM64);
295 assert(addr >= pdi->pi_bar[bidx].addr &&
296 addr + size <= pdi->pi_bar[bidx].addr + pdi->pi_bar[bidx].size);
298 offset = addr - pdi->pi_bar[bidx].addr;
300 if (dir == MEM_F_WRITE)
301 (*pe->pe_barwrite)(ctx, vcpu, pdi, bidx, offset, size, *val);
303 *val = (*pe->pe_barread)(ctx, vcpu, pdi, bidx, offset, size);
310 pci_emul_alloc_resource(uint64_t *baseptr, uint64_t limit, uint64_t size,
315 assert((size & (size - 1)) == 0); /* must be a power of 2 */
317 base = roundup2(*baseptr, size);
319 if (base + size <= limit) {
321 *baseptr = base + size;
328 pci_emul_alloc_bar(struct pci_devinst *pdi, int idx, enum pcibar_type type,
332 return (pci_emul_alloc_pbar(pdi, idx, 0, type, size));
336 pci_emul_alloc_pbar(struct pci_devinst *pdi, int idx, uint64_t hostbase,
337 enum pcibar_type type, uint64_t size)
340 uint64_t *baseptr, limit, addr, mask, lobits, bar;
341 struct inout_port iop;
342 struct mem_range memp;
344 assert(idx >= 0 && idx <= PCI_BARMAX);
346 if ((size & (size - 1)) != 0)
347 size = 1UL << flsl(size); /* round up to a power of 2 */
352 addr = mask = lobits = 0;
356 pci_slotinfo[pdi->pi_slot][pdi->pi_func].si_legacy) {
357 assert(hostbase < PCI_EMUL_IOBASE);
360 baseptr = &pci_emul_iobase;
362 limit = PCI_EMUL_IOLIMIT;
363 mask = PCIM_BAR_IO_BASE;
364 lobits = PCIM_BAR_IO_SPACE;
369 * Some drivers do not work well if the 64-bit BAR is allocated
370 * above 4GB. Allow for this by allocating small requests under
371 * 4GB unless then allocation size is larger than some arbitrary
372 * number (32MB currently).
374 if (size > 32 * 1024 * 1024) {
376 * XXX special case for device requiring peer-peer DMA
378 if (size == 0x100000000UL)
381 baseptr = &pci_emul_membase64;
382 limit = PCI_EMUL_MEMLIMIT64;
383 mask = PCIM_BAR_MEM_BASE;
384 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64 |
385 PCIM_BAR_MEM_PREFETCH;
388 baseptr = &pci_emul_membase32;
389 limit = PCI_EMUL_MEMLIMIT32;
390 mask = PCIM_BAR_MEM_BASE;
391 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64;
395 baseptr = &pci_emul_membase32;
396 limit = PCI_EMUL_MEMLIMIT32;
397 mask = PCIM_BAR_MEM_BASE;
398 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_32;
401 printf("pci_emul_alloc_base: invalid bar type %d\n", type);
405 if (baseptr != NULL) {
406 error = pci_emul_alloc_resource(baseptr, limit, size, &addr);
411 pdi->pi_bar[idx].type = type;
412 pdi->pi_bar[idx].addr = addr;
413 pdi->pi_bar[idx].size = size;
415 /* Initialize the BAR register in config space */
416 bar = (addr & mask) | lobits;
417 pci_set_cfgdata32(pdi, PCIR_BAR(idx), bar);
419 if (type == PCIBAR_MEM64) {
420 assert(idx + 1 <= PCI_BARMAX);
421 pdi->pi_bar[idx + 1].type = PCIBAR_MEMHI64;
422 pci_set_cfgdata32(pdi, PCIR_BAR(idx + 1), bar >> 32);
425 /* add a handler to intercept accesses to the I/O bar */
426 if (type == PCIBAR_IO) {
427 iop.name = pdi->pi_name;
428 iop.flags = IOPORT_F_INOUT;
429 iop.handler = pci_emul_io_handler;
432 for (i = 0; i < size; i++) {
434 register_inout(&iop);
436 } else if (type == PCIBAR_MEM32 || type == PCIBAR_MEM64) {
437 /* add memory bar intercept handler */
438 memp.name = pdi->pi_name;
439 memp.flags = MEM_F_RW;
442 memp.handler = pci_emul_mem_handler;
446 error = register_mem(&memp);
453 #define CAP_START_OFFSET 0x40
455 pci_emul_add_capability(struct pci_devinst *pi, u_char *capdata, int caplen)
457 int i, capoff, capid, reallen;
460 static u_char endofcap[4] = {
461 PCIY_RESERVED, 0, 0, 0
464 assert(caplen > 0 && capdata[0] != PCIY_RESERVED);
466 reallen = roundup2(caplen, 4); /* dword aligned */
468 sts = pci_get_cfgdata16(pi, PCIR_STATUS);
469 if ((sts & PCIM_STATUS_CAPPRESENT) == 0) {
470 capoff = CAP_START_OFFSET;
471 pci_set_cfgdata8(pi, PCIR_CAP_PTR, capoff);
472 pci_set_cfgdata16(pi, PCIR_STATUS, sts|PCIM_STATUS_CAPPRESENT);
474 capoff = pci_get_cfgdata8(pi, PCIR_CAP_PTR);
476 assert((capoff & 0x3) == 0);
477 capid = pci_get_cfgdata8(pi, capoff);
478 if (capid == PCIY_RESERVED)
480 capoff = pci_get_cfgdata8(pi, capoff + 1);
484 /* Check if we have enough space */
485 if (capoff + reallen + sizeof(endofcap) > PCI_REGMAX + 1)
488 /* Copy the capability */
489 for (i = 0; i < caplen; i++)
490 pci_set_cfgdata8(pi, capoff + i, capdata[i]);
492 /* Set the next capability pointer */
493 pci_set_cfgdata8(pi, capoff + 1, capoff + reallen);
495 /* Copy of the reserved capability which serves as the end marker */
496 for (i = 0; i < sizeof(endofcap); i++)
497 pci_set_cfgdata8(pi, capoff + reallen + i, endofcap[i]);
502 static struct pci_devemu *
503 pci_emul_finddev(char *name)
505 struct pci_devemu **pdpp, *pdp;
507 SET_FOREACH(pdpp, pci_devemu_set) {
509 if (!strcmp(pdp->pe_emu, name)) {
518 pci_emul_init(struct vmctx *ctx, struct pci_devemu *pde, int slot, int func,
521 struct pci_devinst *pdi;
522 pdi = malloc(sizeof(struct pci_devinst));
523 bzero(pdi, sizeof(*pdi));
530 snprintf(pdi->pi_name, PI_NAMESZ, "%s-pci-%d", pde->pe_emu, slot);
532 /* Disable legacy interrupts */
533 pci_set_cfgdata8(pdi, PCIR_INTLINE, 255);
534 pci_set_cfgdata8(pdi, PCIR_INTPIN, 0);
536 pci_set_cfgdata8(pdi, PCIR_COMMAND,
537 PCIM_CMD_PORTEN | PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN);
539 if ((*pde->pe_init)(ctx, pdi, params) != 0) {
543 pci_slotinfo[slot][func].si_devi = pdi;
548 pci_populate_msicap(struct msicap *msicap, int msgnum, int nextptr)
552 CTASSERT(sizeof(struct msicap) == 14);
554 /* Number of msi messages must be a power of 2 between 1 and 32 */
555 assert((msgnum & (msgnum - 1)) == 0 && msgnum >= 1 && msgnum <= 32);
556 mmc = ffs(msgnum) - 1;
558 bzero(msicap, sizeof(struct msicap));
559 msicap->capid = PCIY_MSI;
560 msicap->nextptr = nextptr;
561 msicap->msgctrl = PCIM_MSICTRL_64BIT | (mmc << 1);
565 pci_emul_add_msicap(struct pci_devinst *pi, int msgnum)
567 struct msicap msicap;
569 pci_populate_msicap(&msicap, msgnum, 0);
571 return (pci_emul_add_capability(pi, (u_char *)&msicap, sizeof(msicap)));
575 pci_populate_msixcap(struct msixcap *msixcap, int msgnum, int barnum,
576 uint32_t msix_tab_size, int nextptr)
578 CTASSERT(sizeof(struct msixcap) == 12);
580 assert(msix_tab_size % 4096 == 0);
582 bzero(msixcap, sizeof(struct msixcap));
583 msixcap->capid = PCIY_MSIX;
584 msixcap->nextptr = nextptr;
587 * Message Control Register, all fields set to
588 * zero except for the Table Size.
589 * Note: Table size N is encoded as N-1
591 msixcap->msgctrl = msgnum - 1;
595 * - MSI-X table start at offset 0
596 * - PBA table starts at a 4K aligned offset after the MSI-X table
598 msixcap->table_info = barnum & PCIM_MSIX_BIR_MASK;
599 msixcap->pba_info = msix_tab_size | (barnum & PCIM_MSIX_BIR_MASK);
603 pci_msix_table_init(struct pci_devinst *pi, int table_entries)
607 assert(table_entries > 0);
608 assert(table_entries <= MAX_MSIX_TABLE_ENTRIES);
610 table_size = table_entries * MSIX_TABLE_ENTRY_SIZE;
611 pi->pi_msix.table = malloc(table_size);
612 bzero(pi->pi_msix.table, table_size);
614 /* set mask bit of vector control register */
615 for (i = 0; i < table_entries; i++)
616 pi->pi_msix.table[i].vector_control |= PCIM_MSIX_VCTRL_MASK;
620 pci_emul_add_msixcap(struct pci_devinst *pi, int msgnum, int barnum)
624 struct msixcap msixcap;
626 assert(msgnum >= 1 && msgnum <= MAX_MSIX_TABLE_ENTRIES);
627 assert(barnum >= 0 && barnum <= PCIR_MAX_BAR_0);
629 tab_size = msgnum * MSIX_TABLE_ENTRY_SIZE;
631 /* Align table size to nearest 4K */
632 tab_size = roundup2(tab_size, 4096);
634 pi->pi_msix.table_bar = barnum;
635 pi->pi_msix.pba_bar = barnum;
636 pi->pi_msix.table_offset = 0;
637 pi->pi_msix.table_count = msgnum;
638 pi->pi_msix.pba_offset = tab_size;
640 /* calculate the MMIO size required for MSI-X PBA */
641 pba_index = (msgnum - 1) / (PBA_TABLE_ENTRY_SIZE * 8);
642 pi->pi_msix.pba_size = (pba_index + 1) * PBA_TABLE_ENTRY_SIZE;
644 pci_msix_table_init(pi, msgnum);
646 pci_populate_msixcap(&msixcap, msgnum, barnum, tab_size, 0);
648 /* allocate memory for MSI-X Table and PBA */
649 pci_emul_alloc_bar(pi, barnum, PCIBAR_MEM32,
650 tab_size + pi->pi_msix.pba_size);
652 return (pci_emul_add_capability(pi, (u_char *)&msixcap,
657 msixcap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
658 int bytes, uint32_t val)
660 uint16_t msgctrl, rwmask;
663 off = offset - capoff;
664 table_bar = pi->pi_msix.table_bar;
665 /* Message Control Register */
666 if (off == 2 && bytes == 2) {
667 rwmask = PCIM_MSIXCTRL_MSIX_ENABLE | PCIM_MSIXCTRL_FUNCTION_MASK;
668 msgctrl = pci_get_cfgdata16(pi, offset);
670 msgctrl |= val & rwmask;
673 pi->pi_msix.enabled = val & PCIM_MSIXCTRL_MSIX_ENABLE;
674 pi->pi_msix.function_mask = val & PCIM_MSIXCTRL_FUNCTION_MASK;
677 CFGWRITE(pi, offset, val, bytes);
681 msicap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
682 int bytes, uint32_t val)
684 uint16_t msgctrl, rwmask, msgdata, mme;
688 * If guest is writing to the message control register make sure
689 * we do not overwrite read-only fields.
691 if ((offset - capoff) == 2 && bytes == 2) {
692 rwmask = PCIM_MSICTRL_MME_MASK | PCIM_MSICTRL_MSI_ENABLE;
693 msgctrl = pci_get_cfgdata16(pi, offset);
695 msgctrl |= val & rwmask;
698 addrlo = pci_get_cfgdata32(pi, capoff + 4);
699 if (msgctrl & PCIM_MSICTRL_64BIT)
700 msgdata = pci_get_cfgdata16(pi, capoff + 12);
702 msgdata = pci_get_cfgdata16(pi, capoff + 8);
705 * XXX check delivery mode, destination mode etc
707 mme = msgctrl & PCIM_MSICTRL_MME_MASK;
708 pi->pi_msi.enabled = msgctrl & PCIM_MSICTRL_MSI_ENABLE ? 1 : 0;
709 if (pi->pi_msi.enabled) {
710 pi->pi_msi.cpu = (addrlo >> 12) & 0xff;
711 pi->pi_msi.vector = msgdata & 0xff;
712 pi->pi_msi.msgnum = 1 << (mme >> 4);
715 pi->pi_msi.vector = 0;
716 pi->pi_msi.msgnum = 0;
720 CFGWRITE(pi, offset, val, bytes);
724 * This function assumes that 'coff' is in the capabilities region of the
728 pci_emul_capwrite(struct pci_devinst *pi, int offset, int bytes, uint32_t val)
731 uint8_t capoff, nextoff;
733 /* Do not allow un-aligned writes */
734 if ((offset & (bytes - 1)) != 0)
737 /* Find the capability that we want to update */
738 capoff = CAP_START_OFFSET;
740 capid = pci_get_cfgdata8(pi, capoff);
741 if (capid == PCIY_RESERVED)
744 nextoff = pci_get_cfgdata8(pi, capoff + 1);
745 if (offset >= capoff && offset < nextoff)
750 assert(offset >= capoff);
753 * Capability ID and Next Capability Pointer are readonly
755 if (offset == capoff || offset == capoff + 1)
760 msicap_cfgwrite(pi, capoff, offset, bytes, val);
763 msixcap_cfgwrite(pi, capoff, offset, bytes, val);
771 pci_emul_iscap(struct pci_devinst *pi, int offset)
775 uint8_t capid, lastoff;
778 sts = pci_get_cfgdata16(pi, PCIR_STATUS);
779 if ((sts & PCIM_STATUS_CAPPRESENT) != 0) {
780 lastoff = pci_get_cfgdata8(pi, PCIR_CAP_PTR);
782 assert((lastoff & 0x3) == 0);
783 capid = pci_get_cfgdata8(pi, lastoff);
784 if (capid == PCIY_RESERVED)
786 lastoff = pci_get_cfgdata8(pi, lastoff + 1);
788 if (offset >= CAP_START_OFFSET && offset <= lastoff)
795 init_pci(struct vmctx *ctx)
797 struct pci_devemu *pde;
801 pci_emul_iobase = PCI_EMUL_IOBASE;
802 pci_emul_membase32 = PCI_EMUL_MEMBASE32;
803 pci_emul_membase64 = PCI_EMUL_MEMBASE64;
805 for (slot = 0; slot < MAXSLOTS; slot++) {
806 for (func = 0; func < MAXFUNCS; func++) {
807 si = &pci_slotinfo[slot][func];
808 if (si->si_name != NULL) {
809 pde = pci_emul_finddev(si->si_name);
811 pci_emul_init(ctx, pde, slot, func,
819 * Allow ISA IRQs 5,10,11,12, and 15 to be available for
822 lirq[5].li_generic = 1;
823 lirq[10].li_generic = 1;
824 lirq[11].li_generic = 1;
825 lirq[12].li_generic = 1;
826 lirq[15].li_generic = 1;
830 pci_msi_enabled(struct pci_devinst *pi)
832 return (pi->pi_msi.enabled);
836 pci_msi_msgnum(struct pci_devinst *pi)
838 if (pi->pi_msi.enabled)
839 return (pi->pi_msi.msgnum);
845 pci_msix_enabled(struct pci_devinst *pi)
848 return (pi->pi_msix.enabled && !pi->pi_msi.enabled);
852 pci_generate_msix(struct pci_devinst *pi, int index)
854 struct msix_table_entry *mte;
856 if (!pci_msix_enabled(pi))
859 if (pi->pi_msix.function_mask)
862 if (index >= pi->pi_msix.table_count)
865 mte = &pi->pi_msix.table[index];
866 if ((mte->vector_control & PCIM_MSIX_VCTRL_MASK) == 0) {
867 /* XXX Set PBA bit if interrupt is disabled */
868 vm_lapic_irq(pi->pi_vmctx,
869 (mte->addr >> 12) & 0xff, mte->msg_data & 0xff);
874 pci_generate_msi(struct pci_devinst *pi, int msg)
877 if (pci_msi_enabled(pi) && msg < pci_msi_msgnum(pi)) {
878 vm_lapic_irq(pi->pi_vmctx,
880 pi->pi_msi.vector + msg);
885 pci_is_legacy(struct pci_devinst *pi)
888 return (pci_slotinfo[pi->pi_slot][pi->pi_func].si_legacy);
892 pci_lintr_alloc(struct pci_devinst *pi, int vec)
899 for (i = 0; i < NLIRQ; i++) {
900 if (lirq[i].li_generic &&
901 lirq[i].li_owner == NULL) {
907 if (lirq[vec].li_owner != NULL) {
913 lirq[vec].li_owner = pi;
914 pi->pi_lintr_pin = vec;
920 pci_lintr_request(struct pci_devinst *pi, int vec)
923 vec = pci_lintr_alloc(pi, vec);
924 pci_set_cfgdata8(pi, PCIR_INTLINE, vec);
925 pci_set_cfgdata8(pi, PCIR_INTPIN, 1);
930 pci_lintr_assert(struct pci_devinst *pi)
933 assert(pi->pi_lintr_pin);
934 ioapic_assert_pin(pi->pi_vmctx, pi->pi_lintr_pin);
938 pci_lintr_deassert(struct pci_devinst *pi)
941 assert(pi->pi_lintr_pin);
942 ioapic_deassert_pin(pi->pi_vmctx, pi->pi_lintr_pin);
946 * Return 1 if the emulated device in 'slot' is a multi-function device.
947 * Return 0 otherwise.
950 pci_emul_is_mfdev(int slot)
955 for (f = 0; f < MAXFUNCS; f++) {
956 if (pci_slotinfo[slot][f].si_devi != NULL) {
960 return (numfuncs > 1);
964 * Ensure that the PCIM_MFDEV bit is properly set (or unset) depending on
965 * whether or not is a multi-function being emulated in the pci 'slot'.
968 pci_emul_hdrtype_fixup(int slot, int off, int bytes, uint32_t *rv)
972 if (off <= PCIR_HDRTYPE && off + bytes > PCIR_HDRTYPE) {
973 mfdev = pci_emul_is_mfdev(slot);
983 *rv &= ~(PCIM_MFDEV << 16);
985 *rv |= (PCIM_MFDEV << 16);
992 static int cfgbus, cfgslot, cfgfunc, cfgoff;
995 pci_emul_cfgaddr(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
996 uint32_t *eax, void *arg)
1006 cfgoff = x & PCI_REGMAX;
1007 cfgfunc = (x >> 8) & PCI_FUNCMAX;
1008 cfgslot = (x >> 11) & PCI_SLOTMAX;
1009 cfgbus = (x >> 16) & PCI_BUSMAX;
1013 INOUT_PORT(pci_cfgaddr, CONF1_ADDR_PORT, IOPORT_F_OUT, pci_emul_cfgaddr);
1016 pci_emul_cfgdata(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
1017 uint32_t *eax, void *arg)
1019 struct pci_devinst *pi;
1020 struct pci_devemu *pe;
1021 int coff, idx, needcfg;
1024 assert(bytes == 1 || bytes == 2 || bytes == 4);
1027 pi = pci_slotinfo[cfgslot][cfgfunc].si_devi;
1031 coff = cfgoff + (port - CONF1_DATA_PORT);
1034 printf("pcicfg-%s from 0x%0x of %d bytes (%d/%d/%d)\n\r",
1035 in ? "read" : "write", coff, bytes, cfgbus, cfgslot, cfgfunc);
1039 * Just return if there is no device at this cfgslot:cfgfunc or
1040 * if the guest is doing an un-aligned access
1042 if (pi == NULL || (coff & (bytes - 1)) != 0) {
1054 /* Let the device emulation override the default handler */
1055 if (pe->pe_cfgread != NULL) {
1056 needcfg = pe->pe_cfgread(ctx, vcpu, pi,
1064 *eax = pci_get_cfgdata8(pi, coff);
1065 else if (bytes == 2)
1066 *eax = pci_get_cfgdata16(pi, coff);
1068 *eax = pci_get_cfgdata32(pi, coff);
1071 pci_emul_hdrtype_fixup(cfgslot, coff, bytes, eax);
1073 /* Let the device emulation override the default handler */
1074 if (pe->pe_cfgwrite != NULL &&
1075 (*pe->pe_cfgwrite)(ctx, vcpu, pi, coff, bytes, *eax) == 0)
1079 * Special handling for write to BAR registers
1081 if (coff >= PCIR_BAR(0) && coff < PCIR_BAR(PCI_BARMAX + 1)) {
1083 * Ignore writes to BAR registers that are not
1086 if (bytes != 4 || (coff & 0x3) != 0)
1088 idx = (coff - PCIR_BAR(0)) / 4;
1089 switch (pi->pi_bar[idx].type) {
1094 mask = ~(pi->pi_bar[idx].size - 1);
1095 mask &= PCIM_BAR_IO_BASE;
1096 bar = (*eax & mask) | PCIM_BAR_IO_SPACE;
1099 mask = ~(pi->pi_bar[idx].size - 1);
1100 mask &= PCIM_BAR_MEM_BASE;
1102 bar |= PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_32;
1105 mask = ~(pi->pi_bar[idx].size - 1);
1106 mask &= PCIM_BAR_MEM_BASE;
1108 bar |= PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64 |
1109 PCIM_BAR_MEM_PREFETCH;
1111 case PCIBAR_MEMHI64:
1112 mask = ~(pi->pi_bar[idx - 1].size - 1);
1113 mask &= PCIM_BAR_MEM_BASE;
1114 bar = ((uint64_t)*eax << 32) & mask;
1120 pci_set_cfgdata32(pi, coff, bar);
1122 } else if (pci_emul_iscap(pi, coff)) {
1123 pci_emul_capwrite(pi, coff, bytes, *eax);
1125 CFGWRITE(pi, coff, *eax, bytes);
1132 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+0, IOPORT_F_INOUT, pci_emul_cfgdata);
1133 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+1, IOPORT_F_INOUT, pci_emul_cfgdata);
1134 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+2, IOPORT_F_INOUT, pci_emul_cfgdata);
1135 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+3, IOPORT_F_INOUT, pci_emul_cfgdata);
1138 * I/O ports to configure PCI IRQ routing. We ignore all writes to it.
1141 pci_irq_port_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
1142 uint32_t *eax, void *arg)
1147 INOUT_PORT(pci_irq, 0xC00, IOPORT_F_OUT, pci_irq_port_handler);
1148 INOUT_PORT(pci_irq, 0xC01, IOPORT_F_OUT, pci_irq_port_handler);
1150 #define PCI_EMUL_TEST
1151 #ifdef PCI_EMUL_TEST
1153 * Define a dummy test device
1157 struct pci_emul_dsoftc {
1158 uint8_t ioregs[DIOSZ];
1159 uint8_t memregs[DMEMSZ];
1162 #define PCI_EMUL_MSI_MSGS 4
1163 #define PCI_EMUL_MSIX_MSGS 16
1166 pci_emul_dinit(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
1169 struct pci_emul_dsoftc *sc;
1171 sc = malloc(sizeof(struct pci_emul_dsoftc));
1172 memset(sc, 0, sizeof(struct pci_emul_dsoftc));
1176 pci_set_cfgdata16(pi, PCIR_DEVICE, 0x0001);
1177 pci_set_cfgdata16(pi, PCIR_VENDOR, 0x10DD);
1178 pci_set_cfgdata8(pi, PCIR_CLASS, 0x02);
1180 error = pci_emul_add_msicap(pi, PCI_EMUL_MSI_MSGS);
1183 error = pci_emul_alloc_bar(pi, 0, PCIBAR_IO, DIOSZ);
1186 error = pci_emul_alloc_bar(pi, 1, PCIBAR_MEM32, DMEMSZ);
1193 pci_emul_diow(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx,
1194 uint64_t offset, int size, uint64_t value)
1197 struct pci_emul_dsoftc *sc = pi->pi_arg;
1200 if (offset + size > DIOSZ) {
1201 printf("diow: iow too large, offset %ld size %d\n",
1207 sc->ioregs[offset] = value & 0xff;
1208 } else if (size == 2) {
1209 *(uint16_t *)&sc->ioregs[offset] = value & 0xffff;
1210 } else if (size == 4) {
1211 *(uint32_t *)&sc->ioregs[offset] = value;
1213 printf("diow: iow unknown size %d\n", size);
1217 * Special magic value to generate an interrupt
1219 if (offset == 4 && size == 4 && pci_msi_enabled(pi))
1220 pci_generate_msi(pi, value % pci_msi_msgnum(pi));
1222 if (value == 0xabcdef) {
1223 for (i = 0; i < pci_msi_msgnum(pi); i++)
1224 pci_generate_msi(pi, i);
1229 if (offset + size > DMEMSZ) {
1230 printf("diow: memw too large, offset %ld size %d\n",
1236 sc->memregs[offset] = value;
1237 } else if (size == 2) {
1238 *(uint16_t *)&sc->memregs[offset] = value;
1239 } else if (size == 4) {
1240 *(uint32_t *)&sc->memregs[offset] = value;
1241 } else if (size == 8) {
1242 *(uint64_t *)&sc->memregs[offset] = value;
1244 printf("diow: memw unknown size %d\n", size);
1248 * magic interrupt ??
1253 printf("diow: unknown bar idx %d\n", baridx);
1258 pci_emul_dior(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx,
1259 uint64_t offset, int size)
1261 struct pci_emul_dsoftc *sc = pi->pi_arg;
1265 if (offset + size > DIOSZ) {
1266 printf("dior: ior too large, offset %ld size %d\n",
1272 value = sc->ioregs[offset];
1273 } else if (size == 2) {
1274 value = *(uint16_t *) &sc->ioregs[offset];
1275 } else if (size == 4) {
1276 value = *(uint32_t *) &sc->ioregs[offset];
1278 printf("dior: ior unknown size %d\n", size);
1283 if (offset + size > DMEMSZ) {
1284 printf("dior: memr too large, offset %ld size %d\n",
1290 value = sc->memregs[offset];
1291 } else if (size == 2) {
1292 value = *(uint16_t *) &sc->memregs[offset];
1293 } else if (size == 4) {
1294 value = *(uint32_t *) &sc->memregs[offset];
1295 } else if (size == 8) {
1296 value = *(uint64_t *) &sc->memregs[offset];
1298 printf("dior: ior unknown size %d\n", size);
1304 printf("dior: unknown bar idx %d\n", baridx);
1311 struct pci_devemu pci_dummy = {
1313 .pe_init = pci_emul_dinit,
1314 .pe_barwrite = pci_emul_diow,
1315 .pe_barread = pci_emul_dior
1317 PCI_EMUL_SET(pci_dummy);
1319 #endif /* PCI_EMUL_TEST */