mptbl.c revision 242131
1/*- 2 * Copyright (c) 2012 NetApp, Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29#include <sys/cdefs.h> 30__FBSDID("$FreeBSD$"); 31 32#include <sys/types.h> 33#include <sys/errno.h> 34#include <x86/mptable.h> 35 36#include <stdio.h> 37#include <string.h> 38 39#include "fbsdrun.h" 40#include "mptbl.h" 41 42#define MPTABLE_BASE 0xF0000 43 44#define LAPIC_PADDR 0xFEE00000 45#define LAPIC_VERSION 16 46 47#define IOAPIC_PADDR 0xFEC00000 48#define IOAPIC_VERSION 0x11 49 50#define MP_SPECREV 4 51#define MPFP_SIG "_MP_" 52 53/* Configuration header defines */ 54#define MPCH_SIG "PCMP" 55#define MPCH_OEMID "BHyVe " 56#define MPCH_OEMID_LEN 8 57#define MPCH_PRODID "Hypervisor " 58#define MPCH_PRODID_LEN 12 59 60/* Processor entry defines */ 61#define MPEP_SIG_FAMILY 6 /* XXX bhyve should supply this */ 62#define MPEP_SIG_MODEL 26 63#define MPEP_SIG_STEPPING 5 64#define MPEP_SIG \ 65 ((MPEP_SIG_FAMILY << 8) | \ 66 (MPEP_SIG_MODEL << 4) | \ 67 (MPEP_SIG_STEPPING)) 68 69#define MPEP_FEATURES (0xBFEBFBFF) /* XXX Intel i7 */ 70 71/* Define processor entry struct since <x86/mptable.h> gets it wrong */ 72typedef struct BPROCENTRY { 73 u_char type; 74 u_char apic_id; 75 u_char apic_version; 76 u_char cpu_flags; 77 uint32_t cpu_signature; 78 uint32_t feature_flags; 79 uint32_t reserved1; 80 uint32_t reserved2; 81} *bproc_entry_ptr; 82CTASSERT(sizeof(struct BPROCENTRY) == 20); 83 84/* Bus entry defines */ 85#define MPE_NUM_BUSES 2 86#define MPE_BUSNAME_LEN 6 87#define MPE_BUSNAME_ISA "ISA " 88#define MPE_BUSNAME_PCI "PCI " 89 90static void *oem_tbl_start; 91static int oem_tbl_size; 92 93static uint8_t 94mpt_compute_checksum(void *base, size_t len) 95{ 96 uint8_t *bytes; 97 uint8_t sum; 98 99 for(bytes = base, sum = 0; len > 0; len--) { 100 sum += *bytes++; 101 } 102 103 return (256 - sum); 104} 105 106static void 107mpt_build_mpfp(mpfps_t mpfp, vm_paddr_t gpa) 108{ 109 110 memset(mpfp, 0, sizeof(*mpfp)); 111 memcpy(mpfp->signature, MPFP_SIG, 4); 112 mpfp->pap = gpa + sizeof(*mpfp); 113 mpfp->length = 1; 114 mpfp->spec_rev = MP_SPECREV; 115 mpfp->checksum = mpt_compute_checksum(mpfp, sizeof(*mpfp)); 116} 117 118static void 119mpt_build_mpch(mpcth_t mpch) 120{ 121 122 memset(mpch, 0, sizeof(*mpch)); 123 memcpy(mpch->signature, MPCH_SIG, 4); 124 mpch->spec_rev = MP_SPECREV; 125 memcpy(mpch->oem_id, MPCH_OEMID, MPCH_OEMID_LEN); 126 memcpy(mpch->product_id, MPCH_PRODID, MPCH_PRODID_LEN); 127 mpch->apic_address = LAPIC_PADDR; 128} 129 130static void 131mpt_build_proc_entries(bproc_entry_ptr mpep, int ncpu) 132{ 133 int i; 134 135 for (i = 0; i < ncpu; i++) { 136 memset(mpep, 0, sizeof(*mpep)); 137 mpep->type = MPCT_ENTRY_PROCESSOR; 138 mpep->apic_id = i; // XXX 139 mpep->apic_version = LAPIC_VERSION; 140 mpep->cpu_flags = PROCENTRY_FLAG_EN; 141 if (i == 0) 142 mpep->cpu_flags |= PROCENTRY_FLAG_BP; 143 mpep->cpu_signature = MPEP_SIG; 144 mpep->feature_flags = MPEP_FEATURES; 145 mpep++; 146 } 147} 148 149static void 150mpt_build_bus_entries(bus_entry_ptr mpeb) 151{ 152 153 memset(mpeb, 0, sizeof(*mpeb)); 154 mpeb->type = MPCT_ENTRY_BUS; 155 mpeb->bus_id = ISA; 156 memcpy(mpeb->bus_type, MPE_BUSNAME_ISA, MPE_BUSNAME_LEN); 157 mpeb++; 158 159 memset(mpeb, 0, sizeof(*mpeb)); 160 mpeb->type = MPCT_ENTRY_BUS; 161 mpeb->bus_id = PCI; 162 memcpy(mpeb->bus_type, MPE_BUSNAME_PCI, MPE_BUSNAME_LEN); 163} 164 165static void 166mpt_build_ioapic_entries(io_apic_entry_ptr mpei, int id) 167{ 168 169 memset(mpei, 0, sizeof(*mpei)); 170 mpei->type = MPCT_ENTRY_IOAPIC; 171 mpei->apic_id = id; 172 mpei->apic_version = IOAPIC_VERSION; 173 mpei->apic_flags = IOAPICENTRY_FLAG_EN; 174 mpei->apic_address = IOAPIC_PADDR; 175} 176 177#ifdef notyet 178static void 179mpt_build_ioint_entries(struct mpe_ioint *mpeii, int num_pins, int id) 180{ 181 int pin; 182 183 /* 184 * The following config is taken from kernel mptable.c 185 * mptable_parse_default_config_ints(...), for now 186 * just use the default config, tweek later if needed. 187 */ 188 189 190 /* Run through all 16 pins. */ 191 for (pin = 0; pin < num_pins; pin++) { 192 memset(mpeii, 0, sizeof(*mpeii)); 193 mpeii->entry_type = MP_ENTRY_IOINT; 194 mpeii->src_bus_id = MPE_BUSID_ISA; 195 mpeii->dst_apic_id = id; 196 197 /* 198 * All default configs route IRQs from bus 0 to the first 16 199 * pins of the first I/O APIC with an APIC ID of 2. 200 */ 201 mpeii->dst_apic_intin = pin; 202 switch (pin) { 203 case 0: 204 /* Pin 0 is an ExtINT pin. */ 205 mpeii->intr_type = MPEII_INTR_EXTINT; 206 break; 207 case 2: 208 /* IRQ 0 is routed to pin 2. */ 209 mpeii->intr_type = MPEII_INTR_INT; 210 mpeii->src_bus_irq = 0; 211 break; 212 case 5: 213 case 10: 214 case 11: 215 /* 216 * PCI Irqs set to level triggered. 217 */ 218 mpeii->intr_flags = MPEII_FLAGS_TRIGMODE_LEVEL; 219 mpeii->src_bus_id = MPE_BUSID_PCI; 220 default: 221 /* All other pins are identity mapped. */ 222 mpeii->intr_type = MPEII_INTR_INT; 223 mpeii->src_bus_irq = pin; 224 break; 225 } 226 mpeii++; 227 } 228 229} 230 231#define COPYSTR(dest, src, bytes) \ 232 memcpy(dest, src, bytes); \ 233 str[bytes] = 0; 234 235static void 236mptable_dump(struct mp_floating_pointer *mpfp, struct mp_config_hdr *mpch) 237{ 238 static char str[16]; 239 int i; 240 char *cur; 241 242 union mpe { 243 struct mpe_proc *proc; 244 struct mpe_bus *bus; 245 struct mpe_ioapic *ioapic; 246 struct mpe_ioint *ioint; 247 struct mpe_lint *lnit; 248 char *p; 249 }; 250 251 union mpe mpe; 252 253 printf(" MP Floating Pointer :\n"); 254 COPYSTR(str, mpfp->signature, 4); 255 printf("\tsignature:\t%s\n", str); 256 printf("\tmpch paddr:\t%x\n", mpfp->mptable_paddr); 257 printf("\tlength:\t%x\n", mpfp->length); 258 printf("\tspecrec:\t%x\n", mpfp->specrev); 259 printf("\tchecksum:\t%x\n", mpfp->checksum); 260 printf("\tfeature1:\t%x\n", mpfp->feature1); 261 printf("\tfeature2:\t%x\n", mpfp->feature2); 262 printf("\tfeature3:\t%x\n", mpfp->feature3); 263 printf("\tfeature4:\t%x\n", mpfp->feature4); 264 265 printf(" MP Configuration Header :\n"); 266 COPYSTR(str, mpch->signature, 4); 267 printf(" signature: %s\n", str); 268 printf(" length: %x\n", mpch->length); 269 printf(" specrec: %x\n", mpch->specrev); 270 printf(" checksum: %x\n", mpch->checksum); 271 COPYSTR(str, mpch->oemid, MPCH_OEMID_LEN); 272 printf(" oemid: %s\n", str); 273 COPYSTR(str, mpch->prodid, MPCH_PRODID_LEN); 274 printf(" prodid: %s\n", str); 275 printf(" oem_ptr: %x\n", mpch->oem_ptr); 276 printf(" oem_sz: %x\n", mpch->oem_sz); 277 printf(" nr_entries: %x\n", mpch->nr_entries); 278 printf(" apic paddr: %x\n", mpch->lapic_paddr); 279 printf(" ext_length: %x\n", mpch->ext_length); 280 printf(" ext_checksum: %x\n", mpch->ext_checksum); 281 282 cur = (char *)mpch + sizeof(*mpch); 283 for (i = 0; i < mpch->nr_entries; i++) { 284 mpe.p = cur; 285 switch(*mpe.p) { 286 case MP_ENTRY_PROC: 287 printf(" MP Processor Entry :\n"); 288 printf(" lapic_id: %x\n", mpe.proc->lapic_id); 289 printf(" lapic_version: %x\n", mpe.proc->lapic_version); 290 printf(" proc_flags: %x\n", mpe.proc->proc_flags); 291 printf(" proc_signature: %x\n", mpe.proc->proc_signature); 292 printf(" feature_flags: %x\n", mpe.proc->feature_flags); 293 cur += sizeof(struct mpe_proc); 294 break; 295 case MP_ENTRY_BUS: 296 printf(" MP Bus Entry :\n"); 297 printf(" busid: %x\n", mpe.bus->busid); 298 COPYSTR(str, mpe.bus->busname, MPE_BUSNAME_LEN); 299 printf(" busname: %s\n", str); 300 cur += sizeof(struct mpe_bus); 301 break; 302 case MP_ENTRY_IOAPIC: 303 printf(" MP IOAPIC Entry :\n"); 304 printf(" ioapi_id: %x\n", mpe.ioapic->ioapic_id); 305 printf(" ioapi_version: %x\n", mpe.ioapic->ioapic_version); 306 printf(" ioapi_flags: %x\n", mpe.ioapic->ioapic_flags); 307 printf(" ioapi_paddr: %x\n", mpe.ioapic->ioapic_paddr); 308 cur += sizeof(struct mpe_ioapic); 309 break; 310 case MP_ENTRY_IOINT: 311 printf(" MP IO Interrupt Entry :\n"); 312 printf(" intr_type: %x\n", mpe.ioint->intr_type); 313 printf(" intr_flags: %x\n", mpe.ioint->intr_flags); 314 printf(" src_bus_id: %x\n", mpe.ioint->src_bus_id); 315 printf(" src_bus_irq: %x\n", mpe.ioint->src_bus_irq); 316 printf(" dst_apic_id: %x\n", mpe.ioint->dst_apic_id); 317 printf(" dst_apic_intin: %x\n", mpe.ioint->dst_apic_intin); 318 cur += sizeof(struct mpe_ioint); 319 break; 320 case MP_ENTRY_LINT: 321 printf(" MP Local Interrupt Entry :\n"); 322 cur += sizeof(struct mpe_lint); 323 break; 324 } 325 326 } 327} 328#endif 329 330void 331mptable_add_oemtbl(void *tbl, int tblsz) 332{ 333 334 oem_tbl_start = tbl; 335 oem_tbl_size = tblsz; 336} 337 338int 339mptable_build(struct vmctx *ctx, int ncpu, int ioapic) 340{ 341 mpcth_t mpch; 342 bus_entry_ptr mpeb; 343 io_apic_entry_ptr mpei; 344 bproc_entry_ptr mpep; 345 mpfps_t mpfp; 346 char *curraddr; 347 char *startaddr; 348 349 if (paddr_guest2host(0) == NULL) { 350 printf("mptable requires mapped mem\n"); 351 return (ENOMEM); 352 } 353 354 startaddr = curraddr = paddr_guest2host(MPTABLE_BASE); 355 356 mpfp = (mpfps_t)curraddr; 357 mpt_build_mpfp(mpfp, MPTABLE_BASE); 358 curraddr += sizeof(*mpfp); 359 360 mpch = (mpcth_t)curraddr; 361 mpt_build_mpch(mpch); 362 curraddr += sizeof(*mpch); 363 364 mpep = (bproc_entry_ptr)curraddr; 365 mpt_build_proc_entries(mpep, ncpu); 366 curraddr += sizeof(*mpep) * ncpu; 367 mpch->entry_count += ncpu; 368 369 mpeb = (bus_entry_ptr) curraddr; 370 mpt_build_bus_entries(mpeb); 371 curraddr += sizeof(*mpeb) * MPE_NUM_BUSES; 372 mpch->entry_count += MPE_NUM_BUSES; 373 374 if (ioapic) { 375 mpei = (io_apic_entry_ptr)curraddr; 376 mpt_build_ioapic_entries(mpei, ncpu + 1); 377 curraddr += sizeof(*mpei); 378 mpch->entry_count++; 379 } 380 381#ifdef notyet 382 mpt_build_ioint_entries((struct mpe_ioint*)curraddr, MPEII_MAX_IRQ, 383 ncpu + 1); 384 curraddr += sizeof(struct mpe_ioint) * MPEII_MAX_IRQ; 385 mpch->entry_count += MPEII_MAX_IRQ; 386#endif 387 388 if (oem_tbl_start) { 389 mpch->oem_table_pointer = curraddr - startaddr + MPTABLE_BASE; 390 mpch->oem_table_size = oem_tbl_size; 391 memcpy(curraddr, oem_tbl_start, oem_tbl_size); 392 } 393 394 mpch->base_table_length = curraddr - (char *)mpch; 395 mpch->checksum = mpt_compute_checksum(mpch, sizeof(*mpch)); 396 397 return (0); 398} 399