From 062b878f589680b83443010baba98311b19f2746 Mon Sep 17 00:00:00 2001 From: Peter Grehan Date: Thu, 17 Oct 2013 22:01:17 +0000 Subject: [PATCH] Changes required for OpenBSD/amd64: - Allow a hostbridge to be created with AMD as a vendor. This passes the OpenBSD check to allow the use of MSI on a PCI bus. - Enable the i/o interrupt section of the mptable, and populate it with unity ISA mappings. This allows the 'legacy' IRQ mappings of the PCI serial port to be set up. Delete unused print routine that was obscuring code. - Use the '-W' option to enable virtio single-vector MSI rather than an environment variable. Update the virtio net/block drivers to query this flag when setting up interrupts.: bhyverun.c - Fix the arithmetic used to derive the century byte in RTC CMOS, as well as encoding it in BCD. Reviewed by: neel MFC after: 3 days --- usr.sbin/bhyve/bhyverun.c | 16 +++- usr.sbin/bhyve/bhyverun.h | 1 + usr.sbin/bhyve/mptbl.c | 150 ++++++------------------------ usr.sbin/bhyve/pci_hostbridge.c | 16 ++++ usr.sbin/bhyve/pci_virtio_block.c | 9 +- usr.sbin/bhyve/pci_virtio_net.c | 13 +-- usr.sbin/bhyve/rtc.c | 2 +- 7 files changed, 60 insertions(+), 147 deletions(-) diff --git a/usr.sbin/bhyve/bhyverun.c b/usr.sbin/bhyve/bhyverun.c index f679ad5694e..bebd1861c1d 100644 --- a/usr.sbin/bhyve/bhyverun.c +++ b/usr.sbin/bhyve/bhyverun.c @@ -81,6 +81,7 @@ int guest_ncpus; static int pincpu = -1; static int guest_vmexit_on_hlt, guest_vmexit_on_pause, disable_x2apic; +static int virtio_msix = 1; static int foundcpus; @@ -120,7 +121,7 @@ usage(int code) { fprintf(stderr, - "Usage: %s [-aehAHIP][-g ][-s ][-S ]" + "Usage: %s [-aehAHIPW][-g ][-s ][-S ]" "[-c vcpus][-p pincpu][-m mem]" " \n" " -a: local apic is in XAPIC mode (default is X2APIC)\n" @@ -131,6 +132,7 @@ usage(int code) " -H: vmexit from the guest on hlt\n" " -I: present an ioapic to the guest\n" " -P: vmexit from the guest on pause\n" + " -W: force virtio to use single-vector MSI\n" " -e: exit on unhandled i/o access\n" " -h: help\n" " -s: PCI slot config\n" @@ -169,6 +171,13 @@ fbsdrun_vmexit_on_hlt(void) return (guest_vmexit_on_hlt); } +int +fbsdrun_virtio_msix(void) +{ + + return (virtio_msix); +} + static void * fbsdrun_start_thread(void *param) { @@ -544,7 +553,7 @@ main(int argc, char *argv[]) ioapic = 0; memsize = 256 * MB; - while ((c = getopt(argc, argv, "abehAHIPp:g:c:s:S:m:")) != -1) { + while ((c = getopt(argc, argv, "abehAHIPWp:g:c:s:S:m:")) != -1) { switch (c) { case 'a': disable_x2apic = 1; @@ -591,6 +600,9 @@ main(int argc, char *argv[]) case 'e': strictio = 1; break; + case 'W': + virtio_msix = 0; + break; case 'h': usage(0); default: diff --git a/usr.sbin/bhyve/bhyverun.h b/usr.sbin/bhyve/bhyverun.h index d074d657650..f5246d45a74 100644 --- a/usr.sbin/bhyve/bhyverun.h +++ b/usr.sbin/bhyve/bhyverun.h @@ -47,4 +47,5 @@ int fbsdrun_muxed(void); int fbsdrun_vmexit_on_hlt(void); int fbsdrun_vmexit_on_pause(void); int fbsdrun_disable_x2apic(void); +int fbsdrun_virtio_msix(void); #endif diff --git a/usr.sbin/bhyve/mptbl.c b/usr.sbin/bhyve/mptbl.c index 74db2ea96c3..5687d90e24a 100644 --- a/usr.sbin/bhyve/mptbl.c +++ b/usr.sbin/bhyve/mptbl.c @@ -71,6 +71,9 @@ __FBSDID("$FreeBSD$"); #define MPEP_FEATURES (0xBFEBFBFF) /* XXX Intel i7 */ +/* Number of i/o intr entries */ +#define MPEII_MAX_IRQ 16 + /* Define processor entry struct since gets it wrong */ typedef struct BPROCENTRY { u_char type; @@ -155,14 +158,14 @@ mpt_build_bus_entries(bus_entry_ptr mpeb) memset(mpeb, 0, sizeof(*mpeb)); mpeb->type = MPCT_ENTRY_BUS; - mpeb->bus_id = ISA; - memcpy(mpeb->bus_type, MPE_BUSNAME_ISA, MPE_BUSNAME_LEN); + mpeb->bus_id = 0; + memcpy(mpeb->bus_type, MPE_BUSNAME_PCI, MPE_BUSNAME_LEN); mpeb++; memset(mpeb, 0, sizeof(*mpeb)); mpeb->type = MPCT_ENTRY_BUS; - mpeb->bus_id = PCI; - memcpy(mpeb->bus_type, MPE_BUSNAME_PCI, MPE_BUSNAME_LEN); + mpeb->bus_id = 1; + memcpy(mpeb->bus_type, MPE_BUSNAME_ISA, MPE_BUSNAME_LEN); } static void @@ -177,9 +180,8 @@ mpt_build_ioapic_entries(io_apic_entry_ptr mpei, int id) mpei->apic_address = IOAPIC_PADDR; } -#ifdef notyet static void -mpt_build_ioint_entries(struct mpe_ioint *mpeii, int num_pins, int id) +mpt_build_ioint_entries(int_entry_ptr mpie, int num_pins, int id) { int pin; @@ -189,28 +191,27 @@ mpt_build_ioint_entries(struct mpe_ioint *mpeii, int num_pins, int id) * just use the default config, tweek later if needed. */ - /* Run through all 16 pins. */ for (pin = 0; pin < num_pins; pin++) { - memset(mpeii, 0, sizeof(*mpeii)); - mpeii->entry_type = MP_ENTRY_IOINT; - mpeii->src_bus_id = MPE_BUSID_ISA; - mpeii->dst_apic_id = id; + memset(mpie, 0, sizeof(*mpie)); + mpie->type = MPCT_ENTRY_INT; + mpie->src_bus_id = 1; + mpie->dst_apic_id = id; /* * All default configs route IRQs from bus 0 to the first 16 * pins of the first I/O APIC with an APIC ID of 2. */ - mpeii->dst_apic_intin = pin; + mpie->dst_apic_int = pin; switch (pin) { case 0: /* Pin 0 is an ExtINT pin. */ - mpeii->intr_type = MPEII_INTR_EXTINT; + mpie->int_type = INTENTRY_TYPE_EXTINT; break; case 2: /* IRQ 0 is routed to pin 2. */ - mpeii->intr_type = MPEII_INTR_INT; - mpeii->src_bus_irq = 0; + mpie->int_type = INTENTRY_TYPE_INT; + mpie->src_bus_irq = 0; break; case 5: case 10: @@ -218,118 +219,20 @@ mpt_build_ioint_entries(struct mpe_ioint *mpeii, int num_pins, int id) /* * PCI Irqs set to level triggered. */ - mpeii->intr_flags = MPEII_FLAGS_TRIGMODE_LEVEL; - mpeii->src_bus_id = MPE_BUSID_PCI; + mpie->int_flags = INTENTRY_FLAGS_TRIGGER_LEVEL; + mpie->src_bus_id = 0; + /* fall through.. */ default: /* All other pins are identity mapped. */ - mpeii->intr_type = MPEII_INTR_INT; - mpeii->src_bus_irq = pin; + mpie->int_type = INTENTRY_TYPE_INT; + mpie->src_bus_irq = pin; break; } - mpeii++; + mpie++; } } -#define COPYSTR(dest, src, bytes) \ - memcpy(dest, src, bytes); \ - str[bytes] = 0; - -static void -mptable_dump(struct mp_floating_pointer *mpfp, struct mp_config_hdr *mpch) -{ - static char str[16]; - int i; - char *cur; - - union mpe { - struct mpe_proc *proc; - struct mpe_bus *bus; - struct mpe_ioapic *ioapic; - struct mpe_ioint *ioint; - struct mpe_lint *lnit; - char *p; - }; - - union mpe mpe; - - printf(" MP Floating Pointer :\n"); - COPYSTR(str, mpfp->signature, 4); - printf("\tsignature:\t%s\n", str); - printf("\tmpch paddr:\t%x\n", mpfp->mptable_paddr); - printf("\tlength:\t%x\n", mpfp->length); - printf("\tspecrec:\t%x\n", mpfp->specrev); - printf("\tchecksum:\t%x\n", mpfp->checksum); - printf("\tfeature1:\t%x\n", mpfp->feature1); - printf("\tfeature2:\t%x\n", mpfp->feature2); - printf("\tfeature3:\t%x\n", mpfp->feature3); - printf("\tfeature4:\t%x\n", mpfp->feature4); - - printf(" MP Configuration Header :\n"); - COPYSTR(str, mpch->signature, 4); - printf(" signature: %s\n", str); - printf(" length: %x\n", mpch->length); - printf(" specrec: %x\n", mpch->specrev); - printf(" checksum: %x\n", mpch->checksum); - COPYSTR(str, mpch->oemid, MPCH_OEMID_LEN); - printf(" oemid: %s\n", str); - COPYSTR(str, mpch->prodid, MPCH_PRODID_LEN); - printf(" prodid: %s\n", str); - printf(" oem_ptr: %x\n", mpch->oem_ptr); - printf(" oem_sz: %x\n", mpch->oem_sz); - printf(" nr_entries: %x\n", mpch->nr_entries); - printf(" apic paddr: %x\n", mpch->lapic_paddr); - printf(" ext_length: %x\n", mpch->ext_length); - printf(" ext_checksum: %x\n", mpch->ext_checksum); - - cur = (char *)mpch + sizeof(*mpch); - for (i = 0; i < mpch->nr_entries; i++) { - mpe.p = cur; - switch(*mpe.p) { - case MP_ENTRY_PROC: - printf(" MP Processor Entry :\n"); - printf(" lapic_id: %x\n", mpe.proc->lapic_id); - printf(" lapic_version: %x\n", mpe.proc->lapic_version); - printf(" proc_flags: %x\n", mpe.proc->proc_flags); - printf(" proc_signature: %x\n", mpe.proc->proc_signature); - printf(" feature_flags: %x\n", mpe.proc->feature_flags); - cur += sizeof(struct mpe_proc); - break; - case MP_ENTRY_BUS: - printf(" MP Bus Entry :\n"); - printf(" busid: %x\n", mpe.bus->busid); - COPYSTR(str, mpe.bus->busname, MPE_BUSNAME_LEN); - printf(" busname: %s\n", str); - cur += sizeof(struct mpe_bus); - break; - case MP_ENTRY_IOAPIC: - printf(" MP IOAPIC Entry :\n"); - printf(" ioapi_id: %x\n", mpe.ioapic->ioapic_id); - printf(" ioapi_version: %x\n", mpe.ioapic->ioapic_version); - printf(" ioapi_flags: %x\n", mpe.ioapic->ioapic_flags); - printf(" ioapi_paddr: %x\n", mpe.ioapic->ioapic_paddr); - cur += sizeof(struct mpe_ioapic); - break; - case MP_ENTRY_IOINT: - printf(" MP IO Interrupt Entry :\n"); - printf(" intr_type: %x\n", mpe.ioint->intr_type); - printf(" intr_flags: %x\n", mpe.ioint->intr_flags); - printf(" src_bus_id: %x\n", mpe.ioint->src_bus_id); - printf(" src_bus_irq: %x\n", mpe.ioint->src_bus_irq); - printf(" dst_apic_id: %x\n", mpe.ioint->dst_apic_id); - printf(" dst_apic_intin: %x\n", mpe.ioint->dst_apic_intin); - cur += sizeof(struct mpe_ioint); - break; - case MP_ENTRY_LINT: - printf(" MP Local Interrupt Entry :\n"); - cur += sizeof(struct mpe_lint); - break; - } - - } -} -#endif - void mptable_add_oemtbl(void *tbl, int tblsz) { @@ -346,6 +249,7 @@ mptable_build(struct vmctx *ctx, int ncpu, int ioapic) io_apic_entry_ptr mpei; bproc_entry_ptr mpep; mpfps_t mpfp; + int_entry_ptr mpie; char *curraddr; char *startaddr; @@ -381,12 +285,10 @@ mptable_build(struct vmctx *ctx, int ncpu, int ioapic) mpch->entry_count++; } -#ifdef notyet - mpt_build_ioint_entries((struct mpe_ioint*)curraddr, MPEII_MAX_IRQ, - ncpu + 1); - curraddr += sizeof(struct mpe_ioint) * MPEII_MAX_IRQ; + mpie = (int_entry_ptr) curraddr; + mpt_build_ioint_entries(mpie, MPEII_MAX_IRQ, ncpu + 1); + curraddr += sizeof(*mpie) * MPEII_MAX_IRQ; mpch->entry_count += MPEII_MAX_IRQ; -#endif if (oem_tbl_start) { mpch->oem_table_pointer = curraddr - startaddr + MPTABLE_BASE; diff --git a/usr.sbin/bhyve/pci_hostbridge.c b/usr.sbin/bhyve/pci_hostbridge.c index dee0a47e5d7..54a25aeac57 100644 --- a/usr.sbin/bhyve/pci_hostbridge.c +++ b/usr.sbin/bhyve/pci_hostbridge.c @@ -47,6 +47,22 @@ pci_hostbridge_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) return (0); } +static int +pci_amd_hostbridge_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) +{ + (void) pci_hostbridge_init(ctx, pi, opts); + pci_set_cfgdata16(pi, PCIR_VENDOR, 0x1022); /* AMD */ + pci_set_cfgdata16(pi, PCIR_DEVICE, 0x7432); /* made up */ + + return (0); +} + +struct pci_devemu pci_de_amd_hostbridge = { + .pe_emu = "amd_hostbridge", + .pe_init = pci_amd_hostbridge_init, +}; +PCI_EMUL_SET(pci_de_amd_hostbridge); + struct pci_devemu pci_de_hostbridge = { .pe_emu = "hostbridge", .pe_init = pci_hostbridge_init, diff --git a/usr.sbin/bhyve/pci_virtio_block.c b/usr.sbin/bhyve/pci_virtio_block.c index a190497b8f3..a7556d76663 100644 --- a/usr.sbin/bhyve/pci_virtio_block.c +++ b/usr.sbin/bhyve/pci_virtio_block.c @@ -256,8 +256,6 @@ pci_vtblk_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) off_t size; int fd; int sectsz; - int use_msix; - const char *env_msi; if (opts == NULL) { printf("virtio-block: backing device required\n"); @@ -336,12 +334,7 @@ pci_vtblk_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_STORAGE); pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_TYPE_BLOCK); - use_msix = 1; - if ((env_msi = getenv("BHYVE_USE_MSI"))) { - if (strcasecmp(env_msi, "yes") == 0) - use_msix = 0; - } - if (vi_intr_init(&sc->vbsc_vs, 1, use_msix)) + if (vi_intr_init(&sc->vbsc_vs, 1, fbsdrun_virtio_msix())) return (1); vi_set_io_bar(&sc->vbsc_vs, 0); return (0); diff --git a/usr.sbin/bhyve/pci_virtio_net.c b/usr.sbin/bhyve/pci_virtio_net.c index 29399490390..ae20231a203 100644 --- a/usr.sbin/bhyve/pci_virtio_net.c +++ b/usr.sbin/bhyve/pci_virtio_net.c @@ -509,11 +509,9 @@ pci_vtnet_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) char nstr[80]; char tname[MAXCOMLEN + 1]; struct pci_vtnet_softc *sc; - const char *env_msi; char *devname; char *vtopts; int mac_provided; - int use_msix; sc = malloc(sizeof(struct pci_vtnet_softc)); memset(sc, 0, sizeof(struct pci_vtnet_softc)); @@ -530,15 +528,6 @@ pci_vtnet_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) sc->vsc_queues[VTNET_CTLQ].vq_notify = pci_vtnet_ping_ctlq; #endif - /* - * Use MSI if set by user - */ - use_msix = 1; - if ((env_msi = getenv("BHYVE_USE_MSI")) != NULL) { - if (strcasecmp(env_msi, "yes") == 0) - use_msix = 0; - } - /* * Attempt to open the tap device and read the MAC address * if specified @@ -623,7 +612,7 @@ pci_vtnet_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) sc->vsc_config.status = 1; /* use BAR 1 to map MSI-X table and PBA, if we're using MSI-X */ - if (vi_intr_init(&sc->vsc_vs, 1, use_msix)) + if (vi_intr_init(&sc->vsc_vs, 1, fbsdrun_virtio_msix())) return (1); /* use BAR 0 to map config regs in IO space */ diff --git a/usr.sbin/bhyve/rtc.c b/usr.sbin/bhyve/rtc.c index c1a84d74761..f3ce2bc9725 100644 --- a/usr.sbin/bhyve/rtc.c +++ b/usr.sbin/bhyve/rtc.c @@ -331,7 +331,7 @@ rtc_init(struct vmctx *ctx) memset(rtc_nvram, 0, sizeof(rtc_nvram)); - rtc_nvram[nvoff(RTC_CENTURY)] = rtcout(tm.tm_year / 100); + rtc_nvram[nvoff(RTC_CENTURY)] = bin2bcd((tm.tm_year + 1900) / 100); /* XXX init diag/reset code/equipment/checksum ? */