mptbl.c revision 248477
1221828Sgrehan/*- 2242131Sgrehan * Copyright (c) 2012 NetApp, Inc. 3221828Sgrehan * All rights reserved. 4221828Sgrehan * 5221828Sgrehan * Redistribution and use in source and binary forms, with or without 6221828Sgrehan * modification, are permitted provided that the following conditions 7221828Sgrehan * are met: 8221828Sgrehan * 1. Redistributions of source code must retain the above copyright 9221828Sgrehan * notice, this list of conditions and the following disclaimer. 10221828Sgrehan * 2. Redistributions in binary form must reproduce the above copyright 11221828Sgrehan * notice, this list of conditions and the following disclaimer in the 12221828Sgrehan * documentation and/or other materials provided with the distribution. 13221828Sgrehan * 14221828Sgrehan * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 15221828Sgrehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16221828Sgrehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17221828Sgrehan * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 18221828Sgrehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19221828Sgrehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20221828Sgrehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21221828Sgrehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22221828Sgrehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23221828Sgrehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24221828Sgrehan * SUCH DAMAGE. 25221828Sgrehan * 26221828Sgrehan * $FreeBSD: head/usr.sbin/bhyve/mptbl.c 248477 2013-03-18 22:38:30Z neel $ 27221828Sgrehan */ 28221828Sgrehan 29221828Sgrehan#include <sys/cdefs.h> 30221828Sgrehan__FBSDID("$FreeBSD: head/usr.sbin/bhyve/mptbl.c 248477 2013-03-18 22:38:30Z neel $"); 31221828Sgrehan 32221828Sgrehan#include <sys/types.h> 33242131Sgrehan#include <sys/errno.h> 34242131Sgrehan#include <x86/mptable.h> 35221828Sgrehan 36221828Sgrehan#include <stdio.h> 37221828Sgrehan#include <string.h> 38221828Sgrehan 39244167Sgrehan#include "bhyverun.h" 40242131Sgrehan#include "mptbl.h" 41221828Sgrehan 42242131Sgrehan#define MPTABLE_BASE 0xF0000 43221828Sgrehan 44247523Sneel/* floating pointer length + maximum length of configuration table */ 45247523Sneel#define MPTABLE_MAX_LENGTH (65536 + 16) 46247523Sneel 47242131Sgrehan#define LAPIC_PADDR 0xFEE00000 48242131Sgrehan#define LAPIC_VERSION 16 49221828Sgrehan 50242131Sgrehan#define IOAPIC_PADDR 0xFEC00000 51242131Sgrehan#define IOAPIC_VERSION 0x11 52221828Sgrehan 53242131Sgrehan#define MP_SPECREV 4 54242131Sgrehan#define MPFP_SIG "_MP_" 55242131Sgrehan 56242131Sgrehan/* Configuration header defines */ 57242131Sgrehan#define MPCH_SIG "PCMP" 58242131Sgrehan#define MPCH_OEMID "BHyVe " 59242131Sgrehan#define MPCH_OEMID_LEN 8 60242131Sgrehan#define MPCH_PRODID "Hypervisor " 61242131Sgrehan#define MPCH_PRODID_LEN 12 62242131Sgrehan 63242131Sgrehan/* Processor entry defines */ 64242131Sgrehan#define MPEP_SIG_FAMILY 6 /* XXX bhyve should supply this */ 65242131Sgrehan#define MPEP_SIG_MODEL 26 66242131Sgrehan#define MPEP_SIG_STEPPING 5 67242131Sgrehan#define MPEP_SIG \ 68242131Sgrehan ((MPEP_SIG_FAMILY << 8) | \ 69242131Sgrehan (MPEP_SIG_MODEL << 4) | \ 70242131Sgrehan (MPEP_SIG_STEPPING)) 71242131Sgrehan 72242131Sgrehan#define MPEP_FEATURES (0xBFEBFBFF) /* XXX Intel i7 */ 73242131Sgrehan 74242131Sgrehan/* Define processor entry struct since <x86/mptable.h> gets it wrong */ 75242131Sgrehantypedef struct BPROCENTRY { 76242131Sgrehan u_char type; 77242131Sgrehan u_char apic_id; 78242131Sgrehan u_char apic_version; 79242131Sgrehan u_char cpu_flags; 80242131Sgrehan uint32_t cpu_signature; 81242131Sgrehan uint32_t feature_flags; 82242131Sgrehan uint32_t reserved1; 83242131Sgrehan uint32_t reserved2; 84242131Sgrehan} *bproc_entry_ptr; 85242131SgrehanCTASSERT(sizeof(struct BPROCENTRY) == 20); 86242131Sgrehan 87242131Sgrehan/* Bus entry defines */ 88242131Sgrehan#define MPE_NUM_BUSES 2 89242131Sgrehan#define MPE_BUSNAME_LEN 6 90242131Sgrehan#define MPE_BUSNAME_ISA "ISA " 91242131Sgrehan#define MPE_BUSNAME_PCI "PCI " 92242131Sgrehan 93242131Sgrehanstatic void *oem_tbl_start; 94242131Sgrehanstatic int oem_tbl_size; 95242131Sgrehan 96221828Sgrehanstatic uint8_t 97242131Sgrehanmpt_compute_checksum(void *base, size_t len) 98221828Sgrehan{ 99242131Sgrehan uint8_t *bytes; 100242131Sgrehan uint8_t sum; 101242131Sgrehan 102242131Sgrehan for(bytes = base, sum = 0; len > 0; len--) { 103221828Sgrehan sum += *bytes++; 104221828Sgrehan } 105242131Sgrehan 106242131Sgrehan return (256 - sum); 107221828Sgrehan} 108221828Sgrehan 109221828Sgrehanstatic void 110242131Sgrehanmpt_build_mpfp(mpfps_t mpfp, vm_paddr_t gpa) 111221828Sgrehan{ 112242131Sgrehan 113221828Sgrehan memset(mpfp, 0, sizeof(*mpfp)); 114242131Sgrehan memcpy(mpfp->signature, MPFP_SIG, 4); 115242131Sgrehan mpfp->pap = gpa + sizeof(*mpfp); 116242131Sgrehan mpfp->length = 1; 117242131Sgrehan mpfp->spec_rev = MP_SPECREV; 118242131Sgrehan mpfp->checksum = mpt_compute_checksum(mpfp, sizeof(*mpfp)); 119221828Sgrehan} 120221828Sgrehan 121221828Sgrehanstatic void 122242131Sgrehanmpt_build_mpch(mpcth_t mpch) 123221828Sgrehan{ 124242131Sgrehan 125221828Sgrehan memset(mpch, 0, sizeof(*mpch)); 126242131Sgrehan memcpy(mpch->signature, MPCH_SIG, 4); 127242131Sgrehan mpch->spec_rev = MP_SPECREV; 128242131Sgrehan memcpy(mpch->oem_id, MPCH_OEMID, MPCH_OEMID_LEN); 129242131Sgrehan memcpy(mpch->product_id, MPCH_PRODID, MPCH_PRODID_LEN); 130242131Sgrehan mpch->apic_address = LAPIC_PADDR; 131221828Sgrehan} 132221828Sgrehan 133221828Sgrehanstatic void 134242131Sgrehanmpt_build_proc_entries(bproc_entry_ptr mpep, int ncpu) 135221828Sgrehan{ 136221828Sgrehan int i; 137221828Sgrehan 138242131Sgrehan for (i = 0; i < ncpu; i++) { 139221828Sgrehan memset(mpep, 0, sizeof(*mpep)); 140242131Sgrehan mpep->type = MPCT_ENTRY_PROCESSOR; 141242131Sgrehan mpep->apic_id = i; // XXX 142242131Sgrehan mpep->apic_version = LAPIC_VERSION; 143242131Sgrehan mpep->cpu_flags = PROCENTRY_FLAG_EN; 144242131Sgrehan if (i == 0) 145242131Sgrehan mpep->cpu_flags |= PROCENTRY_FLAG_BP; 146242131Sgrehan mpep->cpu_signature = MPEP_SIG; 147221828Sgrehan mpep->feature_flags = MPEP_FEATURES; 148221828Sgrehan mpep++; 149221828Sgrehan } 150221828Sgrehan} 151221828Sgrehan 152221828Sgrehanstatic void 153242131Sgrehanmpt_build_bus_entries(bus_entry_ptr mpeb) 154221828Sgrehan{ 155242131Sgrehan 156221828Sgrehan memset(mpeb, 0, sizeof(*mpeb)); 157242131Sgrehan mpeb->type = MPCT_ENTRY_BUS; 158242131Sgrehan mpeb->bus_id = ISA; 159242131Sgrehan memcpy(mpeb->bus_type, MPE_BUSNAME_ISA, MPE_BUSNAME_LEN); 160221828Sgrehan mpeb++; 161221828Sgrehan 162221828Sgrehan memset(mpeb, 0, sizeof(*mpeb)); 163242131Sgrehan mpeb->type = MPCT_ENTRY_BUS; 164242131Sgrehan mpeb->bus_id = PCI; 165242131Sgrehan memcpy(mpeb->bus_type, MPE_BUSNAME_PCI, MPE_BUSNAME_LEN); 166221828Sgrehan} 167221828Sgrehan 168221828Sgrehanstatic void 169242131Sgrehanmpt_build_ioapic_entries(io_apic_entry_ptr mpei, int id) 170221828Sgrehan{ 171242131Sgrehan 172221828Sgrehan memset(mpei, 0, sizeof(*mpei)); 173242131Sgrehan mpei->type = MPCT_ENTRY_IOAPIC; 174242131Sgrehan mpei->apic_id = id; 175242131Sgrehan mpei->apic_version = IOAPIC_VERSION; 176242131Sgrehan mpei->apic_flags = IOAPICENTRY_FLAG_EN; 177242131Sgrehan mpei->apic_address = IOAPIC_PADDR; 178221828Sgrehan} 179221828Sgrehan 180239042Sneel#ifdef notyet 181221828Sgrehanstatic void 182242131Sgrehanmpt_build_ioint_entries(struct mpe_ioint *mpeii, int num_pins, int id) 183221828Sgrehan{ 184221828Sgrehan int pin; 185221828Sgrehan 186221828Sgrehan /* 187221828Sgrehan * The following config is taken from kernel mptable.c 188221828Sgrehan * mptable_parse_default_config_ints(...), for now 189221828Sgrehan * just use the default config, tweek later if needed. 190221828Sgrehan */ 191221828Sgrehan 192221828Sgrehan 193221828Sgrehan /* Run through all 16 pins. */ 194221828Sgrehan for (pin = 0; pin < num_pins; pin++) { 195221828Sgrehan memset(mpeii, 0, sizeof(*mpeii)); 196221828Sgrehan mpeii->entry_type = MP_ENTRY_IOINT; 197221828Sgrehan mpeii->src_bus_id = MPE_BUSID_ISA; 198239042Sneel mpeii->dst_apic_id = id; 199221828Sgrehan 200221828Sgrehan /* 201242131Sgrehan * All default configs route IRQs from bus 0 to the first 16 202242131Sgrehan * pins of the first I/O APIC with an APIC ID of 2. 203221828Sgrehan */ 204221828Sgrehan mpeii->dst_apic_intin = pin; 205221828Sgrehan switch (pin) { 206221828Sgrehan case 0: 207221828Sgrehan /* Pin 0 is an ExtINT pin. */ 208221828Sgrehan mpeii->intr_type = MPEII_INTR_EXTINT; 209221828Sgrehan break; 210221828Sgrehan case 2: 211221828Sgrehan /* IRQ 0 is routed to pin 2. */ 212221828Sgrehan mpeii->intr_type = MPEII_INTR_INT; 213221828Sgrehan mpeii->src_bus_irq = 0; 214221828Sgrehan break; 215221828Sgrehan case 5: 216221828Sgrehan case 10: 217221828Sgrehan case 11: 218221828Sgrehan /* 219221828Sgrehan * PCI Irqs set to level triggered. 220221828Sgrehan */ 221221828Sgrehan mpeii->intr_flags = MPEII_FLAGS_TRIGMODE_LEVEL; 222221828Sgrehan mpeii->src_bus_id = MPE_BUSID_PCI; 223221828Sgrehan default: 224221828Sgrehan /* All other pins are identity mapped. */ 225221828Sgrehan mpeii->intr_type = MPEII_INTR_INT; 226221828Sgrehan mpeii->src_bus_irq = pin; 227221828Sgrehan break; 228221828Sgrehan } 229221828Sgrehan mpeii++; 230221828Sgrehan } 231221828Sgrehan 232221828Sgrehan} 233221828Sgrehan 234221828Sgrehan#define COPYSTR(dest, src, bytes) \ 235221828Sgrehan memcpy(dest, src, bytes); \ 236221828Sgrehan str[bytes] = 0; 237221828Sgrehan 238221828Sgrehanstatic void 239221828Sgrehanmptable_dump(struct mp_floating_pointer *mpfp, struct mp_config_hdr *mpch) 240221828Sgrehan{ 241221828Sgrehan static char str[16]; 242221828Sgrehan int i; 243221828Sgrehan char *cur; 244221828Sgrehan 245221828Sgrehan union mpe { 246221828Sgrehan struct mpe_proc *proc; 247221828Sgrehan struct mpe_bus *bus; 248221828Sgrehan struct mpe_ioapic *ioapic; 249221828Sgrehan struct mpe_ioint *ioint; 250221828Sgrehan struct mpe_lint *lnit; 251221828Sgrehan char *p; 252221828Sgrehan }; 253221828Sgrehan 254221828Sgrehan union mpe mpe; 255221828Sgrehan 256221828Sgrehan printf(" MP Floating Pointer :\n"); 257221828Sgrehan COPYSTR(str, mpfp->signature, 4); 258242131Sgrehan printf("\tsignature:\t%s\n", str); 259242131Sgrehan printf("\tmpch paddr:\t%x\n", mpfp->mptable_paddr); 260242131Sgrehan printf("\tlength:\t%x\n", mpfp->length); 261242131Sgrehan printf("\tspecrec:\t%x\n", mpfp->specrev); 262242131Sgrehan printf("\tchecksum:\t%x\n", mpfp->checksum); 263242131Sgrehan printf("\tfeature1:\t%x\n", mpfp->feature1); 264242131Sgrehan printf("\tfeature2:\t%x\n", mpfp->feature2); 265242131Sgrehan printf("\tfeature3:\t%x\n", mpfp->feature3); 266242131Sgrehan printf("\tfeature4:\t%x\n", mpfp->feature4); 267221828Sgrehan 268221828Sgrehan printf(" MP Configuration Header :\n"); 269221828Sgrehan COPYSTR(str, mpch->signature, 4); 270221828Sgrehan printf(" signature: %s\n", str); 271221828Sgrehan printf(" length: %x\n", mpch->length); 272221828Sgrehan printf(" specrec: %x\n", mpch->specrev); 273221828Sgrehan printf(" checksum: %x\n", mpch->checksum); 274221828Sgrehan COPYSTR(str, mpch->oemid, MPCH_OEMID_LEN); 275221828Sgrehan printf(" oemid: %s\n", str); 276221828Sgrehan COPYSTR(str, mpch->prodid, MPCH_PRODID_LEN); 277221828Sgrehan printf(" prodid: %s\n", str); 278221828Sgrehan printf(" oem_ptr: %x\n", mpch->oem_ptr); 279221828Sgrehan printf(" oem_sz: %x\n", mpch->oem_sz); 280221828Sgrehan printf(" nr_entries: %x\n", mpch->nr_entries); 281221828Sgrehan printf(" apic paddr: %x\n", mpch->lapic_paddr); 282221828Sgrehan printf(" ext_length: %x\n", mpch->ext_length); 283221828Sgrehan printf(" ext_checksum: %x\n", mpch->ext_checksum); 284221828Sgrehan 285221828Sgrehan cur = (char *)mpch + sizeof(*mpch); 286221828Sgrehan for (i = 0; i < mpch->nr_entries; i++) { 287221828Sgrehan mpe.p = cur; 288221828Sgrehan switch(*mpe.p) { 289221828Sgrehan case MP_ENTRY_PROC: 290221828Sgrehan printf(" MP Processor Entry :\n"); 291221828Sgrehan printf(" lapic_id: %x\n", mpe.proc->lapic_id); 292221828Sgrehan printf(" lapic_version: %x\n", mpe.proc->lapic_version); 293221828Sgrehan printf(" proc_flags: %x\n", mpe.proc->proc_flags); 294221828Sgrehan printf(" proc_signature: %x\n", mpe.proc->proc_signature); 295221828Sgrehan printf(" feature_flags: %x\n", mpe.proc->feature_flags); 296221828Sgrehan cur += sizeof(struct mpe_proc); 297221828Sgrehan break; 298221828Sgrehan case MP_ENTRY_BUS: 299221828Sgrehan printf(" MP Bus Entry :\n"); 300221828Sgrehan printf(" busid: %x\n", mpe.bus->busid); 301221828Sgrehan COPYSTR(str, mpe.bus->busname, MPE_BUSNAME_LEN); 302221828Sgrehan printf(" busname: %s\n", str); 303221828Sgrehan cur += sizeof(struct mpe_bus); 304221828Sgrehan break; 305221828Sgrehan case MP_ENTRY_IOAPIC: 306221828Sgrehan printf(" MP IOAPIC Entry :\n"); 307221828Sgrehan printf(" ioapi_id: %x\n", mpe.ioapic->ioapic_id); 308221828Sgrehan printf(" ioapi_version: %x\n", mpe.ioapic->ioapic_version); 309221828Sgrehan printf(" ioapi_flags: %x\n", mpe.ioapic->ioapic_flags); 310221828Sgrehan printf(" ioapi_paddr: %x\n", mpe.ioapic->ioapic_paddr); 311221828Sgrehan cur += sizeof(struct mpe_ioapic); 312221828Sgrehan break; 313221828Sgrehan case MP_ENTRY_IOINT: 314221828Sgrehan printf(" MP IO Interrupt Entry :\n"); 315221828Sgrehan printf(" intr_type: %x\n", mpe.ioint->intr_type); 316221828Sgrehan printf(" intr_flags: %x\n", mpe.ioint->intr_flags); 317221828Sgrehan printf(" src_bus_id: %x\n", mpe.ioint->src_bus_id); 318221828Sgrehan printf(" src_bus_irq: %x\n", mpe.ioint->src_bus_irq); 319221828Sgrehan printf(" dst_apic_id: %x\n", mpe.ioint->dst_apic_id); 320221828Sgrehan printf(" dst_apic_intin: %x\n", mpe.ioint->dst_apic_intin); 321221828Sgrehan cur += sizeof(struct mpe_ioint); 322221828Sgrehan break; 323221828Sgrehan case MP_ENTRY_LINT: 324221828Sgrehan printf(" MP Local Interrupt Entry :\n"); 325221828Sgrehan cur += sizeof(struct mpe_lint); 326221828Sgrehan break; 327221828Sgrehan } 328221828Sgrehan 329221828Sgrehan } 330221828Sgrehan} 331221942Sjhb#endif 332221828Sgrehan 333242131Sgrehanvoid 334242131Sgrehanmptable_add_oemtbl(void *tbl, int tblsz) 335242131Sgrehan{ 336242131Sgrehan 337242131Sgrehan oem_tbl_start = tbl; 338242131Sgrehan oem_tbl_size = tblsz; 339242131Sgrehan} 340242131Sgrehan 341221828Sgrehanint 342242131Sgrehanmptable_build(struct vmctx *ctx, int ncpu, int ioapic) 343221828Sgrehan{ 344242131Sgrehan mpcth_t mpch; 345242131Sgrehan bus_entry_ptr mpeb; 346242131Sgrehan io_apic_entry_ptr mpei; 347242131Sgrehan bproc_entry_ptr mpep; 348242131Sgrehan mpfps_t mpfp; 349242131Sgrehan char *curraddr; 350221828Sgrehan char *startaddr; 351221828Sgrehan 352248477Sneel startaddr = paddr_guest2host(ctx, MPTABLE_BASE, MPTABLE_MAX_LENGTH); 353247523Sneel if (startaddr == NULL) { 354242131Sgrehan printf("mptable requires mapped mem\n"); 355242131Sgrehan return (ENOMEM); 356221828Sgrehan } 357221828Sgrehan 358247523Sneel curraddr = startaddr; 359242131Sgrehan mpfp = (mpfps_t)curraddr; 360242131Sgrehan mpt_build_mpfp(mpfp, MPTABLE_BASE); 361242131Sgrehan curraddr += sizeof(*mpfp); 362221828Sgrehan 363242131Sgrehan mpch = (mpcth_t)curraddr; 364242131Sgrehan mpt_build_mpch(mpch); 365242131Sgrehan curraddr += sizeof(*mpch); 366221828Sgrehan 367242131Sgrehan mpep = (bproc_entry_ptr)curraddr; 368242131Sgrehan mpt_build_proc_entries(mpep, ncpu); 369242131Sgrehan curraddr += sizeof(*mpep) * ncpu; 370242131Sgrehan mpch->entry_count += ncpu; 371221828Sgrehan 372242131Sgrehan mpeb = (bus_entry_ptr) curraddr; 373242131Sgrehan mpt_build_bus_entries(mpeb); 374242131Sgrehan curraddr += sizeof(*mpeb) * MPE_NUM_BUSES; 375242131Sgrehan mpch->entry_count += MPE_NUM_BUSES; 376242131Sgrehan 377239042Sneel if (ioapic) { 378242131Sgrehan mpei = (io_apic_entry_ptr)curraddr; 379242131Sgrehan mpt_build_ioapic_entries(mpei, ncpu + 1); 380242131Sgrehan curraddr += sizeof(*mpei); 381242131Sgrehan mpch->entry_count++; 382239042Sneel } 383239042Sneel 384239042Sneel#ifdef notyet 385242131Sgrehan mpt_build_ioint_entries((struct mpe_ioint*)curraddr, MPEII_MAX_IRQ, 386239042Sneel ncpu + 1); 387242131Sgrehan curraddr += sizeof(struct mpe_ioint) * MPEII_MAX_IRQ; 388242131Sgrehan mpch->entry_count += MPEII_MAX_IRQ; 389242131Sgrehan#endif 390221828Sgrehan 391242131Sgrehan if (oem_tbl_start) { 392242131Sgrehan mpch->oem_table_pointer = curraddr - startaddr + MPTABLE_BASE; 393242131Sgrehan mpch->oem_table_size = oem_tbl_size; 394242131Sgrehan memcpy(curraddr, oem_tbl_start, oem_tbl_size); 395221828Sgrehan } 396221828Sgrehan 397242131Sgrehan mpch->base_table_length = curraddr - (char *)mpch; 398242131Sgrehan mpch->checksum = mpt_compute_checksum(mpch, sizeof(*mpch)); 399221828Sgrehan 400242131Sgrehan return (0); 401221828Sgrehan} 402