2 * Copyright (c) 2011 NetApp, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/linker_set.h>
34 #include <sys/errno.h>
45 #include <machine/vmm.h>
56 #define CONF1_ADDR_PORT 0x0cf8
57 #define CONF1_DATA_PORT 0x0cfc
59 #define CONF1_ENABLE 0x80000000ul
61 #define CFGWRITE(pi,off,val,b) \
64 pci_set_cfgdata8((pi),(off),(val)); \
65 } else if ((b) == 2) { \
66 pci_set_cfgdata16((pi),(off),(val)); \
68 pci_set_cfgdata32((pi),(off),(val)); \
72 #define MAXSLOTS (PCI_SLOTMAX + 1)
73 #define MAXFUNCS (PCI_FUNCMAX + 1)
78 struct pci_devinst *fi_devi;
87 struct intxinfo si_intpins[4];
88 struct funcinfo si_funcs[MAXFUNCS];
89 } pci_slotinfo[MAXSLOTS];
91 SET_DECLARE(pci_devemu_set, struct pci_devemu);
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 struct pci_devemu *pci_emul_finddev(char *name);
106 static void pci_lintr_update(struct pci_devinst *pi);
108 static int pci_emul_devices;
109 static struct mem_range pci_mem_hole;
116 * Slot options are in the form:
118 * <slot>[:<func>],<emul>[,<config>]
122 * emul is a string describing the type of PCI device e.g. virtio-net
123 * config is an optional string, depending on the device, that can be
124 * used for configuration.
130 pci_parse_slot_usage(char *aopt)
133 fprintf(stderr, "Invalid PCI slot info field \"%s\"\n", aopt);
137 pci_parse_slot(char *opt)
139 char *slot, *func, *emul, *config;
141 int error, snum, fnum;
144 str = cpy = strdup(opt);
146 slot = strsep(&str, ",");
148 if (strchr(slot, ':') != NULL) {
150 (void) strsep(&func, ":");
153 emul = strsep(&str, ",");
157 pci_parse_slot_usage(opt);
162 fnum = func ? atoi(func) : 0;
164 if (snum < 0 || snum >= MAXSLOTS || fnum < 0 || fnum >= MAXFUNCS) {
165 pci_parse_slot_usage(opt);
169 if (pci_slotinfo[snum].si_funcs[fnum].fi_name != NULL) {
170 fprintf(stderr, "pci slot %d:%d already occupied!\n",
175 if (pci_emul_finddev(emul) == NULL) {
176 fprintf(stderr, "pci slot %d:%d: unknown device \"%s\"\n",
182 pci_slotinfo[snum].si_funcs[fnum].fi_name = emul;
183 pci_slotinfo[snum].si_funcs[fnum].fi_param = config;
193 pci_valid_pba_offset(struct pci_devinst *pi, uint64_t offset)
196 if (offset < pi->pi_msix.pba_offset)
199 if (offset >= pi->pi_msix.pba_offset + pi->pi_msix.pba_size) {
207 pci_emul_msix_twrite(struct pci_devinst *pi, uint64_t offset, int size,
210 int msix_entry_offset;
214 /* support only 4 or 8 byte writes */
215 if (size != 4 && size != 8)
219 * Return if table index is beyond what device supports
221 tab_index = offset / MSIX_TABLE_ENTRY_SIZE;
222 if (tab_index >= pi->pi_msix.table_count)
225 msix_entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
227 /* support only aligned writes */
228 if ((msix_entry_offset % size) != 0)
231 dest = (char *)(pi->pi_msix.table + tab_index);
232 dest += msix_entry_offset;
235 *((uint32_t *)dest) = value;
237 *((uint64_t *)dest) = value;
243 pci_emul_msix_tread(struct pci_devinst *pi, uint64_t offset, int size)
246 int msix_entry_offset;
248 uint64_t retval = ~0;
251 * The PCI standard only allows 4 and 8 byte accesses to the MSI-X
252 * table but we also allow 1 byte access to accomodate reads from
255 if (size != 1 && size != 4 && size != 8)
258 msix_entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
260 /* support only aligned reads */
261 if ((msix_entry_offset % size) != 0) {
265 tab_index = offset / MSIX_TABLE_ENTRY_SIZE;
267 if (tab_index < pi->pi_msix.table_count) {
268 /* valid MSI-X Table access */
269 dest = (char *)(pi->pi_msix.table + tab_index);
270 dest += msix_entry_offset;
273 retval = *((uint8_t *)dest);
275 retval = *((uint32_t *)dest);
277 retval = *((uint64_t *)dest);
278 } else if (pci_valid_pba_offset(pi, offset)) {
279 /* return 0 for PBA access */
287 pci_msix_table_bar(struct pci_devinst *pi)
290 if (pi->pi_msix.table != NULL)
291 return (pi->pi_msix.table_bar);
297 pci_msix_pba_bar(struct pci_devinst *pi)
300 if (pi->pi_msix.table != NULL)
301 return (pi->pi_msix.pba_bar);
307 pci_emul_io_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
308 uint32_t *eax, void *arg)
310 struct pci_devinst *pdi = arg;
311 struct pci_devemu *pe = pdi->pi_d;
315 for (i = 0; i <= PCI_BARMAX; i++) {
316 if (pdi->pi_bar[i].type == PCIBAR_IO &&
317 port >= pdi->pi_bar[i].addr &&
318 port + bytes <= pdi->pi_bar[i].addr + pdi->pi_bar[i].size) {
319 offset = port - pdi->pi_bar[i].addr;
321 *eax = (*pe->pe_barread)(ctx, vcpu, pdi, i,
324 (*pe->pe_barwrite)(ctx, vcpu, pdi, i, offset,
333 pci_emul_mem_handler(struct vmctx *ctx, int vcpu, int dir, uint64_t addr,
334 int size, uint64_t *val, void *arg1, long arg2)
336 struct pci_devinst *pdi = arg1;
337 struct pci_devemu *pe = pdi->pi_d;
339 int bidx = (int) arg2;
341 assert(bidx <= PCI_BARMAX);
342 assert(pdi->pi_bar[bidx].type == PCIBAR_MEM32 ||
343 pdi->pi_bar[bidx].type == PCIBAR_MEM64);
344 assert(addr >= pdi->pi_bar[bidx].addr &&
345 addr + size <= pdi->pi_bar[bidx].addr + pdi->pi_bar[bidx].size);
347 offset = addr - pdi->pi_bar[bidx].addr;
349 if (dir == MEM_F_WRITE)
350 (*pe->pe_barwrite)(ctx, vcpu, pdi, bidx, offset, size, *val);
352 *val = (*pe->pe_barread)(ctx, vcpu, pdi, bidx, offset, size);
359 pci_emul_alloc_resource(uint64_t *baseptr, uint64_t limit, uint64_t size,
364 assert((size & (size - 1)) == 0); /* must be a power of 2 */
366 base = roundup2(*baseptr, size);
368 if (base + size <= limit) {
370 *baseptr = base + size;
377 pci_emul_alloc_bar(struct pci_devinst *pdi, int idx, enum pcibar_type type,
381 return (pci_emul_alloc_pbar(pdi, idx, 0, type, size));
385 * Register (or unregister) the MMIO or I/O region associated with the BAR
386 * register 'idx' of an emulated pci device.
389 modify_bar_registration(struct pci_devinst *pi, int idx, int registration)
392 struct inout_port iop;
395 switch (pi->pi_bar[idx].type) {
397 bzero(&iop, sizeof(struct inout_port));
398 iop.name = pi->pi_name;
399 iop.port = pi->pi_bar[idx].addr;
400 iop.size = pi->pi_bar[idx].size;
402 iop.flags = IOPORT_F_INOUT;
403 iop.handler = pci_emul_io_handler;
405 error = register_inout(&iop);
407 error = unregister_inout(&iop);
411 bzero(&mr, sizeof(struct mem_range));
412 mr.name = pi->pi_name;
413 mr.base = pi->pi_bar[idx].addr;
414 mr.size = pi->pi_bar[idx].size;
417 mr.handler = pci_emul_mem_handler;
420 error = register_mem(&mr);
422 error = unregister_mem(&mr);
432 unregister_bar(struct pci_devinst *pi, int idx)
435 modify_bar_registration(pi, idx, 0);
439 register_bar(struct pci_devinst *pi, int idx)
442 modify_bar_registration(pi, idx, 1);
445 /* Are we decoding i/o port accesses for the emulated pci device? */
447 porten(struct pci_devinst *pi)
451 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND);
453 return (cmd & PCIM_CMD_PORTEN);
456 /* Are we decoding memory accesses for the emulated pci device? */
458 memen(struct pci_devinst *pi)
462 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND);
464 return (cmd & PCIM_CMD_MEMEN);
468 * Update the MMIO or I/O address that is decoded by the BAR register.
470 * If the pci device has enabled the address space decoding then intercept
471 * the address range decoded by the BAR register.
474 update_bar_address(struct pci_devinst *pi, uint64_t addr, int idx, int type)
478 if (pi->pi_bar[idx].type == PCIBAR_IO)
484 unregister_bar(pi, idx);
489 pi->pi_bar[idx].addr = addr;
492 pi->pi_bar[idx].addr &= ~0xffffffffUL;
493 pi->pi_bar[idx].addr |= addr;
496 pi->pi_bar[idx].addr &= 0xffffffff;
497 pi->pi_bar[idx].addr |= addr;
504 register_bar(pi, idx);
508 pci_emul_alloc_pbar(struct pci_devinst *pdi, int idx, uint64_t hostbase,
509 enum pcibar_type type, uint64_t size)
512 uint64_t *baseptr, limit, addr, mask, lobits, bar;
514 assert(idx >= 0 && idx <= PCI_BARMAX);
516 if ((size & (size - 1)) != 0)
517 size = 1UL << flsl(size); /* round up to a power of 2 */
519 /* Enforce minimum BAR sizes required by the PCI standard */
520 if (type == PCIBAR_IO) {
531 addr = mask = lobits = 0;
534 baseptr = &pci_emul_iobase;
535 limit = PCI_EMUL_IOLIMIT;
536 mask = PCIM_BAR_IO_BASE;
537 lobits = PCIM_BAR_IO_SPACE;
542 * Some drivers do not work well if the 64-bit BAR is allocated
543 * above 4GB. Allow for this by allocating small requests under
544 * 4GB unless then allocation size is larger than some arbitrary
545 * number (32MB currently).
547 if (size > 32 * 1024 * 1024) {
549 * XXX special case for device requiring peer-peer DMA
551 if (size == 0x100000000UL)
554 baseptr = &pci_emul_membase64;
555 limit = PCI_EMUL_MEMLIMIT64;
556 mask = PCIM_BAR_MEM_BASE;
557 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64 |
558 PCIM_BAR_MEM_PREFETCH;
561 baseptr = &pci_emul_membase32;
562 limit = PCI_EMUL_MEMLIMIT32;
563 mask = PCIM_BAR_MEM_BASE;
564 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64;
568 baseptr = &pci_emul_membase32;
569 limit = PCI_EMUL_MEMLIMIT32;
570 mask = PCIM_BAR_MEM_BASE;
571 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_32;
574 printf("pci_emul_alloc_base: invalid bar type %d\n", type);
578 if (baseptr != NULL) {
579 error = pci_emul_alloc_resource(baseptr, limit, size, &addr);
584 pdi->pi_bar[idx].type = type;
585 pdi->pi_bar[idx].addr = addr;
586 pdi->pi_bar[idx].size = size;
588 /* Initialize the BAR register in config space */
589 bar = (addr & mask) | lobits;
590 pci_set_cfgdata32(pdi, PCIR_BAR(idx), bar);
592 if (type == PCIBAR_MEM64) {
593 assert(idx + 1 <= PCI_BARMAX);
594 pdi->pi_bar[idx + 1].type = PCIBAR_MEMHI64;
595 pci_set_cfgdata32(pdi, PCIR_BAR(idx + 1), bar >> 32);
598 register_bar(pdi, idx);
603 #define CAP_START_OFFSET 0x40
605 pci_emul_add_capability(struct pci_devinst *pi, u_char *capdata, int caplen)
607 int i, capoff, capid, reallen;
610 static u_char endofcap[4] = {
611 PCIY_RESERVED, 0, 0, 0
614 assert(caplen > 0 && capdata[0] != PCIY_RESERVED);
616 reallen = roundup2(caplen, 4); /* dword aligned */
618 sts = pci_get_cfgdata16(pi, PCIR_STATUS);
619 if ((sts & PCIM_STATUS_CAPPRESENT) == 0) {
620 capoff = CAP_START_OFFSET;
621 pci_set_cfgdata8(pi, PCIR_CAP_PTR, capoff);
622 pci_set_cfgdata16(pi, PCIR_STATUS, sts|PCIM_STATUS_CAPPRESENT);
624 capoff = pci_get_cfgdata8(pi, PCIR_CAP_PTR);
626 assert((capoff & 0x3) == 0);
627 capid = pci_get_cfgdata8(pi, capoff);
628 if (capid == PCIY_RESERVED)
630 capoff = pci_get_cfgdata8(pi, capoff + 1);
634 /* Check if we have enough space */
635 if (capoff + reallen + sizeof(endofcap) > PCI_REGMAX + 1)
638 /* Copy the capability */
639 for (i = 0; i < caplen; i++)
640 pci_set_cfgdata8(pi, capoff + i, capdata[i]);
642 /* Set the next capability pointer */
643 pci_set_cfgdata8(pi, capoff + 1, capoff + reallen);
645 /* Copy of the reserved capability which serves as the end marker */
646 for (i = 0; i < sizeof(endofcap); i++)
647 pci_set_cfgdata8(pi, capoff + reallen + i, endofcap[i]);
652 static struct pci_devemu *
653 pci_emul_finddev(char *name)
655 struct pci_devemu **pdpp, *pdp;
657 SET_FOREACH(pdpp, pci_devemu_set) {
659 if (!strcmp(pdp->pe_emu, name)) {
668 pci_emul_init(struct vmctx *ctx, struct pci_devemu *pde, int slot, int func,
671 struct pci_devinst *pdi;
674 pdi = malloc(sizeof(struct pci_devinst));
675 bzero(pdi, sizeof(*pdi));
681 pthread_mutex_init(&pdi->pi_lintr.lock, NULL);
682 pdi->pi_lintr.pin = 0;
683 pdi->pi_lintr.state = IDLE;
684 pdi->pi_lintr.ioapic_irq = 0;
686 snprintf(pdi->pi_name, PI_NAMESZ, "%s-pci-%d", pde->pe_emu, slot);
688 /* Disable legacy interrupts */
689 pci_set_cfgdata8(pdi, PCIR_INTLINE, 255);
690 pci_set_cfgdata8(pdi, PCIR_INTPIN, 0);
692 pci_set_cfgdata8(pdi, PCIR_COMMAND,
693 PCIM_CMD_PORTEN | PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN);
695 err = (*pde->pe_init)(ctx, pdi, params);
700 pci_slotinfo[slot].si_funcs[func].fi_devi = pdi;
707 pci_populate_msicap(struct msicap *msicap, int msgnum, int nextptr)
711 CTASSERT(sizeof(struct msicap) == 14);
713 /* Number of msi messages must be a power of 2 between 1 and 32 */
714 assert((msgnum & (msgnum - 1)) == 0 && msgnum >= 1 && msgnum <= 32);
715 mmc = ffs(msgnum) - 1;
717 bzero(msicap, sizeof(struct msicap));
718 msicap->capid = PCIY_MSI;
719 msicap->nextptr = nextptr;
720 msicap->msgctrl = PCIM_MSICTRL_64BIT | (mmc << 1);
724 pci_emul_add_msicap(struct pci_devinst *pi, int msgnum)
726 struct msicap msicap;
728 pci_populate_msicap(&msicap, msgnum, 0);
730 return (pci_emul_add_capability(pi, (u_char *)&msicap, sizeof(msicap)));
734 pci_populate_msixcap(struct msixcap *msixcap, int msgnum, int barnum,
735 uint32_t msix_tab_size, int nextptr)
737 CTASSERT(sizeof(struct msixcap) == 12);
739 assert(msix_tab_size % 4096 == 0);
741 bzero(msixcap, sizeof(struct msixcap));
742 msixcap->capid = PCIY_MSIX;
743 msixcap->nextptr = nextptr;
746 * Message Control Register, all fields set to
747 * zero except for the Table Size.
748 * Note: Table size N is encoded as N-1
750 msixcap->msgctrl = msgnum - 1;
754 * - MSI-X table start at offset 0
755 * - PBA table starts at a 4K aligned offset after the MSI-X table
757 msixcap->table_info = barnum & PCIM_MSIX_BIR_MASK;
758 msixcap->pba_info = msix_tab_size | (barnum & PCIM_MSIX_BIR_MASK);
762 pci_msix_table_init(struct pci_devinst *pi, int table_entries)
766 assert(table_entries > 0);
767 assert(table_entries <= MAX_MSIX_TABLE_ENTRIES);
769 table_size = table_entries * MSIX_TABLE_ENTRY_SIZE;
770 pi->pi_msix.table = malloc(table_size);
771 bzero(pi->pi_msix.table, table_size);
773 /* set mask bit of vector control register */
774 for (i = 0; i < table_entries; i++)
775 pi->pi_msix.table[i].vector_control |= PCIM_MSIX_VCTRL_MASK;
779 pci_emul_add_msixcap(struct pci_devinst *pi, int msgnum, int barnum)
783 struct msixcap msixcap;
785 assert(msgnum >= 1 && msgnum <= MAX_MSIX_TABLE_ENTRIES);
786 assert(barnum >= 0 && barnum <= PCIR_MAX_BAR_0);
788 tab_size = msgnum * MSIX_TABLE_ENTRY_SIZE;
790 /* Align table size to nearest 4K */
791 tab_size = roundup2(tab_size, 4096);
793 pi->pi_msix.table_bar = barnum;
794 pi->pi_msix.pba_bar = barnum;
795 pi->pi_msix.table_offset = 0;
796 pi->pi_msix.table_count = msgnum;
797 pi->pi_msix.pba_offset = tab_size;
799 /* calculate the MMIO size required for MSI-X PBA */
800 pba_index = (msgnum - 1) / (PBA_TABLE_ENTRY_SIZE * 8);
801 pi->pi_msix.pba_size = (pba_index + 1) * PBA_TABLE_ENTRY_SIZE;
803 pci_msix_table_init(pi, msgnum);
805 pci_populate_msixcap(&msixcap, msgnum, barnum, tab_size, 0);
807 /* allocate memory for MSI-X Table and PBA */
808 pci_emul_alloc_bar(pi, barnum, PCIBAR_MEM32,
809 tab_size + pi->pi_msix.pba_size);
811 return (pci_emul_add_capability(pi, (u_char *)&msixcap,
816 msixcap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
817 int bytes, uint32_t val)
819 uint16_t msgctrl, rwmask;
822 off = offset - capoff;
823 table_bar = pi->pi_msix.table_bar;
824 /* Message Control Register */
825 if (off == 2 && bytes == 2) {
826 rwmask = PCIM_MSIXCTRL_MSIX_ENABLE | PCIM_MSIXCTRL_FUNCTION_MASK;
827 msgctrl = pci_get_cfgdata16(pi, offset);
829 msgctrl |= val & rwmask;
832 pi->pi_msix.enabled = val & PCIM_MSIXCTRL_MSIX_ENABLE;
833 pi->pi_msix.function_mask = val & PCIM_MSIXCTRL_FUNCTION_MASK;
834 pci_lintr_update(pi);
837 CFGWRITE(pi, offset, val, bytes);
841 msicap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
842 int bytes, uint32_t val)
844 uint16_t msgctrl, rwmask, msgdata, mme;
848 * If guest is writing to the message control register make sure
849 * we do not overwrite read-only fields.
851 if ((offset - capoff) == 2 && bytes == 2) {
852 rwmask = PCIM_MSICTRL_MME_MASK | PCIM_MSICTRL_MSI_ENABLE;
853 msgctrl = pci_get_cfgdata16(pi, offset);
855 msgctrl |= val & rwmask;
858 addrlo = pci_get_cfgdata32(pi, capoff + 4);
859 if (msgctrl & PCIM_MSICTRL_64BIT)
860 msgdata = pci_get_cfgdata16(pi, capoff + 12);
862 msgdata = pci_get_cfgdata16(pi, capoff + 8);
864 mme = msgctrl & PCIM_MSICTRL_MME_MASK;
865 pi->pi_msi.enabled = msgctrl & PCIM_MSICTRL_MSI_ENABLE ? 1 : 0;
866 if (pi->pi_msi.enabled) {
867 pi->pi_msi.addr = addrlo;
868 pi->pi_msi.msg_data = msgdata;
869 pi->pi_msi.maxmsgnum = 1 << (mme >> 4);
871 pi->pi_msi.maxmsgnum = 0;
873 pci_lintr_update(pi);
876 CFGWRITE(pi, offset, val, bytes);
880 pciecap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
881 int bytes, uint32_t val)
884 /* XXX don't write to the readonly parts */
885 CFGWRITE(pi, offset, val, bytes);
888 #define PCIECAP_VERSION 0x2
890 pci_emul_add_pciecap(struct pci_devinst *pi, int type)
893 struct pciecap pciecap;
895 CTASSERT(sizeof(struct pciecap) == 60);
897 if (type != PCIEM_TYPE_ROOT_PORT)
900 bzero(&pciecap, sizeof(pciecap));
902 pciecap.capid = PCIY_EXPRESS;
903 pciecap.pcie_capabilities = PCIECAP_VERSION | PCIEM_TYPE_ROOT_PORT;
904 pciecap.link_capabilities = 0x411; /* gen1, x1 */
905 pciecap.link_status = 0x11; /* gen1, x1 */
907 err = pci_emul_add_capability(pi, (u_char *)&pciecap, sizeof(pciecap));
912 * This function assumes that 'coff' is in the capabilities region of the
916 pci_emul_capwrite(struct pci_devinst *pi, int offset, int bytes, uint32_t val)
919 uint8_t capoff, nextoff;
921 /* Do not allow un-aligned writes */
922 if ((offset & (bytes - 1)) != 0)
925 /* Find the capability that we want to update */
926 capoff = CAP_START_OFFSET;
928 capid = pci_get_cfgdata8(pi, capoff);
929 if (capid == PCIY_RESERVED)
932 nextoff = pci_get_cfgdata8(pi, capoff + 1);
933 if (offset >= capoff && offset < nextoff)
938 assert(offset >= capoff);
941 * Capability ID and Next Capability Pointer are readonly.
942 * However, some o/s's do 4-byte writes that include these.
943 * For this case, trim the write back to 2 bytes and adjust
946 if (offset == capoff || offset == capoff + 1) {
947 if (offset == capoff && bytes == 4) {
957 msicap_cfgwrite(pi, capoff, offset, bytes, val);
960 msixcap_cfgwrite(pi, capoff, offset, bytes, val);
963 pciecap_cfgwrite(pi, capoff, offset, bytes, val);
971 pci_emul_iscap(struct pci_devinst *pi, int offset)
975 uint8_t capid, lastoff;
978 sts = pci_get_cfgdata16(pi, PCIR_STATUS);
979 if ((sts & PCIM_STATUS_CAPPRESENT) != 0) {
980 lastoff = pci_get_cfgdata8(pi, PCIR_CAP_PTR);
982 assert((lastoff & 0x3) == 0);
983 capid = pci_get_cfgdata8(pi, lastoff);
984 if (capid == PCIY_RESERVED)
986 lastoff = pci_get_cfgdata8(pi, lastoff + 1);
988 if (offset >= CAP_START_OFFSET && offset <= lastoff)
995 pci_emul_fallback_handler(struct vmctx *ctx, int vcpu, int dir, uint64_t addr,
996 int size, uint64_t *val, void *arg1, long arg2)
999 * Ignore writes; return 0xff's for reads. The mem read code
1000 * will take care of truncating to the correct size.
1002 if (dir == MEM_F_READ) {
1003 *val = 0xffffffffffffffff;
1010 init_pci(struct vmctx *ctx)
1012 struct pci_devemu *pde;
1013 struct funcinfo *fi;
1018 pci_emul_iobase = PCI_EMUL_IOBASE;
1019 pci_emul_membase32 = vm_get_lowmem_limit(ctx);
1020 pci_emul_membase64 = PCI_EMUL_MEMBASE64;
1022 for (slot = 0; slot < MAXSLOTS; slot++) {
1023 for (func = 0; func < MAXFUNCS; func++) {
1024 fi = &pci_slotinfo[slot].si_funcs[func];
1025 if (fi->fi_name != NULL) {
1026 pde = pci_emul_finddev(fi->fi_name);
1027 assert(pde != NULL);
1028 error = pci_emul_init(ctx, pde, slot, func,
1037 * The guest physical memory map looks like the following:
1038 * [0, lowmem) guest system memory
1039 * [lowmem, lowmem_limit) memory hole (may be absent)
1040 * [lowmem_limit, 4GB) PCI hole (32-bit BAR allocation)
1041 * [4GB, 4GB + highmem)
1043 * Accesses to memory addresses that are not allocated to system
1044 * memory or PCI devices return 0xff's.
1046 error = vm_get_memory_seg(ctx, 0, &lowmem, NULL);
1049 memset(&pci_mem_hole, 0, sizeof(struct mem_range));
1050 pci_mem_hole.name = "PCI hole";
1051 pci_mem_hole.flags = MEM_F_RW;
1052 pci_mem_hole.base = lowmem;
1053 pci_mem_hole.size = (4ULL * 1024 * 1024 * 1024) - lowmem;
1054 pci_mem_hole.handler = pci_emul_fallback_handler;
1056 error = register_mem_fallback(&pci_mem_hole);
1063 pci_prt_entry(int slot, int pin, int ioapic_irq, void *arg)
1068 dsdt_line(" Package (0x04)");
1070 dsdt_line(" 0x%X,", slot << 16 | 0xffff);
1071 dsdt_line(" 0x%02X,", pin - 1);
1072 dsdt_line(" Zero,");
1073 dsdt_line(" 0x%X", ioapic_irq);
1074 dsdt_line(" }%s", *count == 1 ? "" : ",");
1079 pci_write_dsdt(void)
1081 struct pci_devinst *pi;
1082 int count, slot, func;
1085 dsdt_line("Scope (_SB)");
1087 dsdt_line(" Device (PCI0)");
1089 dsdt_line(" Name (_HID, EisaId (\"PNP0A03\"))");
1090 dsdt_line(" Name (_ADR, Zero)");
1091 dsdt_line(" Name (_CRS, ResourceTemplate ()");
1093 dsdt_line(" WordBusNumber (ResourceProducer, MinFixed, "
1094 "MaxFixed, PosDecode,");
1095 dsdt_line(" 0x0000, // Granularity");
1096 dsdt_line(" 0x0000, // Range Minimum");
1097 dsdt_line(" 0x00FF, // Range Maximum");
1098 dsdt_line(" 0x0000, // Translation Offset");
1099 dsdt_line(" 0x0100, // Length");
1102 dsdt_fixed_ioport(0xCF8, 8);
1104 dsdt_line(" WordIO (ResourceProducer, MinFixed, MaxFixed, "
1105 "PosDecode, EntireRange,");
1106 dsdt_line(" 0x0000, // Granularity");
1107 dsdt_line(" 0x0000, // Range Minimum");
1108 dsdt_line(" 0x0CF7, // Range Maximum");
1109 dsdt_line(" 0x0000, // Translation Offset");
1110 dsdt_line(" 0x0CF8, // Length");
1111 dsdt_line(" ,, , TypeStatic)");
1112 dsdt_line(" WordIO (ResourceProducer, MinFixed, MaxFixed, "
1113 "PosDecode, EntireRange,");
1114 dsdt_line(" 0x0000, // Granularity");
1115 dsdt_line(" 0x0D00, // Range Minimum");
1116 dsdt_line(" 0xFFFF, // Range Maximum");
1117 dsdt_line(" 0x0000, // Translation Offset");
1118 dsdt_line(" 0xF300, // Length");
1119 dsdt_line(" ,, , TypeStatic)");
1120 dsdt_line(" DWordMemory (ResourceProducer, PosDecode, "
1121 "MinFixed, MaxFixed, NonCacheable, ReadWrite,");
1122 dsdt_line(" 0x00000000, // Granularity");
1123 dsdt_line(" 0x%08lX, // Range Minimum\n",
1125 dsdt_line(" 0x%08X, // Range Maximum\n",
1126 PCI_EMUL_MEMLIMIT32 - 1);
1127 dsdt_line(" 0x00000000, // Translation Offset");
1128 dsdt_line(" 0x%08lX, // Length\n",
1129 PCI_EMUL_MEMLIMIT32 - pci_mem_hole.base);
1130 dsdt_line(" ,, , AddressRangeMemory, TypeStatic)");
1131 dsdt_line(" QWordMemory (ResourceProducer, PosDecode, "
1132 "MinFixed, MaxFixed, NonCacheable, ReadWrite,");
1133 dsdt_line(" 0x0000000000000000, // Granularity");
1134 dsdt_line(" 0x%016lX, // Range Minimum\n",
1135 PCI_EMUL_MEMBASE64);
1136 dsdt_line(" 0x%016lX, // Range Maximum\n",
1137 PCI_EMUL_MEMLIMIT64 - 1);
1138 dsdt_line(" 0x0000000000000000, // Translation Offset");
1139 dsdt_line(" 0x%016lX, // Length\n",
1140 PCI_EMUL_MEMLIMIT64 - PCI_EMUL_MEMBASE64);
1141 dsdt_line(" ,, , AddressRangeMemory, TypeStatic)");
1143 count = pci_count_lintr();
1146 dsdt_line("Name (_PRT, Package (0x%02X)", count);
1148 pci_walk_lintr(pci_prt_entry, &count);
1154 for (slot = 0; slot < MAXSLOTS; slot++) {
1155 for (func = 0; func < MAXFUNCS; func++) {
1156 pi = pci_slotinfo[slot].si_funcs[func].fi_devi;
1157 if (pi != NULL && pi->pi_d->pe_write_dsdt != NULL)
1158 pi->pi_d->pe_write_dsdt(pi);
1169 pci_msi_enabled(struct pci_devinst *pi)
1171 return (pi->pi_msi.enabled);
1175 pci_msi_maxmsgnum(struct pci_devinst *pi)
1177 if (pi->pi_msi.enabled)
1178 return (pi->pi_msi.maxmsgnum);
1184 pci_msix_enabled(struct pci_devinst *pi)
1187 return (pi->pi_msix.enabled && !pi->pi_msi.enabled);
1191 pci_generate_msix(struct pci_devinst *pi, int index)
1193 struct msix_table_entry *mte;
1195 if (!pci_msix_enabled(pi))
1198 if (pi->pi_msix.function_mask)
1201 if (index >= pi->pi_msix.table_count)
1204 mte = &pi->pi_msix.table[index];
1205 if ((mte->vector_control & PCIM_MSIX_VCTRL_MASK) == 0) {
1206 /* XXX Set PBA bit if interrupt is disabled */
1207 vm_lapic_msi(pi->pi_vmctx, mte->addr, mte->msg_data);
1212 pci_generate_msi(struct pci_devinst *pi, int index)
1215 if (pci_msi_enabled(pi) && index < pci_msi_maxmsgnum(pi)) {
1216 vm_lapic_msi(pi->pi_vmctx, pi->pi_msi.addr,
1217 pi->pi_msi.msg_data + index);
1222 pci_lintr_permitted(struct pci_devinst *pi)
1226 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND);
1227 return (!(pi->pi_msi.enabled || pi->pi_msix.enabled ||
1228 (cmd & PCIM_CMD_INTxDIS)));
1232 pci_lintr_request(struct pci_devinst *pi)
1234 struct slotinfo *si;
1235 int bestpin, bestcount, irq, pin;
1238 * First, allocate a pin from our slot.
1240 si = &pci_slotinfo[pi->pi_slot];
1242 bestcount = si->si_intpins[0].ii_count;
1243 for (pin = 1; pin < 4; pin++) {
1244 if (si->si_intpins[pin].ii_count < bestcount) {
1246 bestcount = si->si_intpins[pin].ii_count;
1251 * Attempt to allocate an I/O APIC pin for this intpin. If
1252 * 8259A support is added we will need a separate field to
1253 * assign the intpin to an input pin on the PCI interrupt
1256 if (si->si_intpins[bestpin].ii_count == 0) {
1257 irq = ioapic_pci_alloc_irq();
1260 si->si_intpins[bestpin].ii_ioapic_irq = irq;
1262 irq = si->si_intpins[bestpin].ii_ioapic_irq;
1263 si->si_intpins[bestpin].ii_count++;
1265 pi->pi_lintr.pin = bestpin + 1;
1266 pi->pi_lintr.ioapic_irq = irq;
1267 pci_set_cfgdata8(pi, PCIR_INTLINE, irq);
1268 pci_set_cfgdata8(pi, PCIR_INTPIN, bestpin + 1);
1273 pci_lintr_assert(struct pci_devinst *pi)
1276 assert(pi->pi_lintr.pin > 0);
1278 pthread_mutex_lock(&pi->pi_lintr.lock);
1279 if (pi->pi_lintr.state == IDLE) {
1280 if (pci_lintr_permitted(pi)) {
1281 pi->pi_lintr.state = ASSERTED;
1282 vm_ioapic_assert_irq(pi->pi_vmctx,
1283 pi->pi_lintr.ioapic_irq);
1285 pi->pi_lintr.state = PENDING;
1287 pthread_mutex_unlock(&pi->pi_lintr.lock);
1291 pci_lintr_deassert(struct pci_devinst *pi)
1294 assert(pi->pi_lintr.pin > 0);
1296 pthread_mutex_lock(&pi->pi_lintr.lock);
1297 if (pi->pi_lintr.state == ASSERTED) {
1298 pi->pi_lintr.state = IDLE;
1299 vm_ioapic_deassert_irq(pi->pi_vmctx, pi->pi_lintr.ioapic_irq);
1300 } else if (pi->pi_lintr.state == PENDING)
1301 pi->pi_lintr.state = IDLE;
1302 pthread_mutex_unlock(&pi->pi_lintr.lock);
1306 pci_lintr_update(struct pci_devinst *pi)
1309 pthread_mutex_lock(&pi->pi_lintr.lock);
1310 if (pi->pi_lintr.state == ASSERTED && !pci_lintr_permitted(pi)) {
1311 vm_ioapic_deassert_irq(pi->pi_vmctx, pi->pi_lintr.ioapic_irq);
1312 pi->pi_lintr.state = PENDING;
1313 } else if (pi->pi_lintr.state == PENDING && pci_lintr_permitted(pi)) {
1314 pi->pi_lintr.state = ASSERTED;
1315 vm_ioapic_assert_irq(pi->pi_vmctx, pi->pi_lintr.ioapic_irq);
1317 pthread_mutex_unlock(&pi->pi_lintr.lock);
1321 pci_count_lintr(void)
1323 int count, slot, pin;
1326 for (slot = 0; slot < MAXSLOTS; slot++) {
1327 for (pin = 0; pin < 4; pin++) {
1328 if (pci_slotinfo[slot].si_intpins[pin].ii_count != 0)
1336 pci_walk_lintr(pci_lintr_cb cb, void *arg)
1338 struct intxinfo *ii;
1341 for (slot = 0; slot < MAXSLOTS; slot++) {
1342 for (pin = 0; pin < 4; pin++) {
1343 ii = &pci_slotinfo[slot].si_intpins[pin];
1344 if (ii->ii_count != 0)
1345 cb(slot, pin + 1, ii->ii_ioapic_irq, arg);
1351 * Return 1 if the emulated device in 'slot' is a multi-function device.
1352 * Return 0 otherwise.
1355 pci_emul_is_mfdev(int slot)
1360 for (f = 0; f < MAXFUNCS; f++) {
1361 if (pci_slotinfo[slot].si_funcs[f].fi_devi != NULL) {
1365 return (numfuncs > 1);
1369 * Ensure that the PCIM_MFDEV bit is properly set (or unset) depending on
1370 * whether or not is a multi-function being emulated in the pci 'slot'.
1373 pci_emul_hdrtype_fixup(int slot, int off, int bytes, uint32_t *rv)
1377 if (off <= PCIR_HDRTYPE && off + bytes > PCIR_HDRTYPE) {
1378 mfdev = pci_emul_is_mfdev(slot);
1388 *rv &= ~(PCIM_MFDEV << 16);
1390 *rv |= (PCIM_MFDEV << 16);
1397 static int cfgbus, cfgslot, cfgfunc, cfgoff;
1400 pci_emul_cfgaddr(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
1401 uint32_t *eax, void *arg)
1407 *eax = (bytes == 2) ? 0xffff : 0xff;
1412 x = (cfgbus << 16) |
1416 *eax = x | CONF1_ENABLE;
1419 cfgoff = x & PCI_REGMAX;
1420 cfgfunc = (x >> 8) & PCI_FUNCMAX;
1421 cfgslot = (x >> 11) & PCI_SLOTMAX;
1422 cfgbus = (x >> 16) & PCI_BUSMAX;
1427 INOUT_PORT(pci_cfgaddr, CONF1_ADDR_PORT, IOPORT_F_INOUT, pci_emul_cfgaddr);
1430 bits_changed(uint32_t old, uint32_t new, uint32_t mask)
1433 return ((old ^ new) & mask);
1437 pci_emul_cmdwrite(struct pci_devinst *pi, uint32_t new, int bytes)
1443 * The command register is at an offset of 4 bytes and thus the
1444 * guest could write 1, 2 or 4 bytes starting at this offset.
1447 old = pci_get_cfgdata16(pi, PCIR_COMMAND); /* stash old value */
1448 CFGWRITE(pi, PCIR_COMMAND, new, bytes); /* update config */
1449 new = pci_get_cfgdata16(pi, PCIR_COMMAND); /* get updated value */
1452 * If the MMIO or I/O address space decoding has changed then
1453 * register/unregister all BARs that decode that address space.
1455 for (i = 0; i <= PCI_BARMAX; i++) {
1456 switch (pi->pi_bar[i].type) {
1458 case PCIBAR_MEMHI64:
1461 /* I/O address space decoding changed? */
1462 if (bits_changed(old, new, PCIM_CMD_PORTEN)) {
1464 register_bar(pi, i);
1466 unregister_bar(pi, i);
1471 /* MMIO address space decoding changed? */
1472 if (bits_changed(old, new, PCIM_CMD_MEMEN)) {
1474 register_bar(pi, i);
1476 unregister_bar(pi, i);
1485 * If INTx has been unmasked and is pending, assert the
1488 pci_lintr_update(pi);
1492 pci_emul_cfgdata(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
1493 uint32_t *eax, void *arg)
1495 struct pci_devinst *pi;
1496 struct pci_devemu *pe;
1497 int coff, idx, needcfg;
1498 uint64_t addr, bar, mask;
1500 assert(bytes == 1 || bytes == 2 || bytes == 4);
1503 pi = pci_slotinfo[cfgslot].si_funcs[cfgfunc].fi_devi;
1507 coff = cfgoff + (port - CONF1_DATA_PORT);
1510 printf("pcicfg-%s from 0x%0x of %d bytes (%d/%d/%d)\n\r",
1511 in ? "read" : "write", coff, bytes, cfgbus, cfgslot, cfgfunc);
1515 * Just return if there is no device at this cfgslot:cfgfunc or
1516 * if the guest is doing an un-aligned access
1518 if (pi == NULL || (coff & (bytes - 1)) != 0) {
1530 /* Let the device emulation override the default handler */
1531 if (pe->pe_cfgread != NULL) {
1532 needcfg = pe->pe_cfgread(ctx, vcpu, pi,
1540 *eax = pci_get_cfgdata8(pi, coff);
1541 else if (bytes == 2)
1542 *eax = pci_get_cfgdata16(pi, coff);
1544 *eax = pci_get_cfgdata32(pi, coff);
1547 pci_emul_hdrtype_fixup(cfgslot, coff, bytes, eax);
1549 /* Let the device emulation override the default handler */
1550 if (pe->pe_cfgwrite != NULL &&
1551 (*pe->pe_cfgwrite)(ctx, vcpu, pi, coff, bytes, *eax) == 0)
1555 * Special handling for write to BAR registers
1557 if (coff >= PCIR_BAR(0) && coff < PCIR_BAR(PCI_BARMAX + 1)) {
1559 * Ignore writes to BAR registers that are not
1562 if (bytes != 4 || (coff & 0x3) != 0)
1564 idx = (coff - PCIR_BAR(0)) / 4;
1565 mask = ~(pi->pi_bar[idx].size - 1);
1566 switch (pi->pi_bar[idx].type) {
1568 pi->pi_bar[idx].addr = bar = 0;
1573 bar = addr | PCIM_BAR_IO_SPACE;
1575 * Register the new BAR value for interception
1577 if (addr != pi->pi_bar[idx].addr) {
1578 update_bar_address(pi, addr, idx,
1583 addr = bar = *eax & mask;
1584 bar |= PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_32;
1585 if (addr != pi->pi_bar[idx].addr) {
1586 update_bar_address(pi, addr, idx,
1591 addr = bar = *eax & mask;
1592 bar |= PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64 |
1593 PCIM_BAR_MEM_PREFETCH;
1594 if (addr != (uint32_t)pi->pi_bar[idx].addr) {
1595 update_bar_address(pi, addr, idx,
1599 case PCIBAR_MEMHI64:
1600 mask = ~(pi->pi_bar[idx - 1].size - 1);
1601 addr = ((uint64_t)*eax << 32) & mask;
1603 if (bar != pi->pi_bar[idx - 1].addr >> 32) {
1604 update_bar_address(pi, addr, idx - 1,
1611 pci_set_cfgdata32(pi, coff, bar);
1613 } else if (pci_emul_iscap(pi, coff)) {
1614 pci_emul_capwrite(pi, coff, bytes, *eax);
1615 } else if (coff == PCIR_COMMAND) {
1616 pci_emul_cmdwrite(pi, *eax, bytes);
1618 CFGWRITE(pi, coff, *eax, bytes);
1625 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+0, IOPORT_F_INOUT, pci_emul_cfgdata);
1626 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+1, IOPORT_F_INOUT, pci_emul_cfgdata);
1627 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+2, IOPORT_F_INOUT, pci_emul_cfgdata);
1628 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+3, IOPORT_F_INOUT, pci_emul_cfgdata);
1631 * I/O ports to configure PCI IRQ routing. We ignore all writes to it.
1634 pci_irq_port_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
1635 uint32_t *eax, void *arg)
1640 INOUT_PORT(pci_irq, 0xC00, IOPORT_F_OUT, pci_irq_port_handler);
1641 INOUT_PORT(pci_irq, 0xC01, IOPORT_F_OUT, pci_irq_port_handler);
1642 SYSRES_IO(0xC00, 2);
1644 #define PCI_EMUL_TEST
1645 #ifdef PCI_EMUL_TEST
1647 * Define a dummy test device
1651 struct pci_emul_dsoftc {
1652 uint8_t ioregs[DIOSZ];
1653 uint8_t memregs[DMEMSZ];
1656 #define PCI_EMUL_MSI_MSGS 4
1657 #define PCI_EMUL_MSIX_MSGS 16
1660 pci_emul_dinit(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
1663 struct pci_emul_dsoftc *sc;
1665 sc = malloc(sizeof(struct pci_emul_dsoftc));
1666 memset(sc, 0, sizeof(struct pci_emul_dsoftc));
1670 pci_set_cfgdata16(pi, PCIR_DEVICE, 0x0001);
1671 pci_set_cfgdata16(pi, PCIR_VENDOR, 0x10DD);
1672 pci_set_cfgdata8(pi, PCIR_CLASS, 0x02);
1674 error = pci_emul_add_msicap(pi, PCI_EMUL_MSI_MSGS);
1677 error = pci_emul_alloc_bar(pi, 0, PCIBAR_IO, DIOSZ);
1680 error = pci_emul_alloc_bar(pi, 1, PCIBAR_MEM32, DMEMSZ);
1687 pci_emul_diow(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx,
1688 uint64_t offset, int size, uint64_t value)
1691 struct pci_emul_dsoftc *sc = pi->pi_arg;
1694 if (offset + size > DIOSZ) {
1695 printf("diow: iow too large, offset %ld size %d\n",
1701 sc->ioregs[offset] = value & 0xff;
1702 } else if (size == 2) {
1703 *(uint16_t *)&sc->ioregs[offset] = value & 0xffff;
1704 } else if (size == 4) {
1705 *(uint32_t *)&sc->ioregs[offset] = value;
1707 printf("diow: iow unknown size %d\n", size);
1711 * Special magic value to generate an interrupt
1713 if (offset == 4 && size == 4 && pci_msi_enabled(pi))
1714 pci_generate_msi(pi, value % pci_msi_maxmsgnum(pi));
1716 if (value == 0xabcdef) {
1717 for (i = 0; i < pci_msi_maxmsgnum(pi); i++)
1718 pci_generate_msi(pi, i);
1723 if (offset + size > DMEMSZ) {
1724 printf("diow: memw too large, offset %ld size %d\n",
1730 sc->memregs[offset] = value;
1731 } else if (size == 2) {
1732 *(uint16_t *)&sc->memregs[offset] = value;
1733 } else if (size == 4) {
1734 *(uint32_t *)&sc->memregs[offset] = value;
1735 } else if (size == 8) {
1736 *(uint64_t *)&sc->memregs[offset] = value;
1738 printf("diow: memw unknown size %d\n", size);
1742 * magic interrupt ??
1747 printf("diow: unknown bar idx %d\n", baridx);
1752 pci_emul_dior(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx,
1753 uint64_t offset, int size)
1755 struct pci_emul_dsoftc *sc = pi->pi_arg;
1759 if (offset + size > DIOSZ) {
1760 printf("dior: ior too large, offset %ld size %d\n",
1766 value = sc->ioregs[offset];
1767 } else if (size == 2) {
1768 value = *(uint16_t *) &sc->ioregs[offset];
1769 } else if (size == 4) {
1770 value = *(uint32_t *) &sc->ioregs[offset];
1772 printf("dior: ior unknown size %d\n", size);
1777 if (offset + size > DMEMSZ) {
1778 printf("dior: memr too large, offset %ld size %d\n",
1784 value = sc->memregs[offset];
1785 } else if (size == 2) {
1786 value = *(uint16_t *) &sc->memregs[offset];
1787 } else if (size == 4) {
1788 value = *(uint32_t *) &sc->memregs[offset];
1789 } else if (size == 8) {
1790 value = *(uint64_t *) &sc->memregs[offset];
1792 printf("dior: ior unknown size %d\n", size);
1798 printf("dior: unknown bar idx %d\n", baridx);
1805 struct pci_devemu pci_dummy = {
1807 .pe_init = pci_emul_dinit,
1808 .pe_barwrite = pci_emul_diow,
1809 .pe_barread = pci_emul_dior
1811 PCI_EMUL_SET(pci_dummy);
1813 #endif /* PCI_EMUL_TEST */