mptbl.c revision 259301
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: stable/10/usr.sbin/bhyve/mptbl.c 259301 2013-12-13 06:59:18Z grehan $ 27 */ 28 29#include <sys/cdefs.h> 30__FBSDID("$FreeBSD: stable/10/usr.sbin/bhyve/mptbl.c 259301 2013-12-13 06:59:18Z grehan $"); 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 "bhyverun.h" 40#include "mptbl.h" 41 42#define MPTABLE_BASE 0xF0000 43 44/* floating pointer length + maximum length of configuration table */ 45#define MPTABLE_MAX_LENGTH (65536 + 16) 46 47#define LAPIC_PADDR 0xFEE00000 48#define LAPIC_VERSION 16 49 50#define IOAPIC_PADDR 0xFEC00000 51#define IOAPIC_VERSION 0x11 52 53#define MP_SPECREV 4 54#define MPFP_SIG "_MP_" 55 56/* Configuration header defines */ 57#define MPCH_SIG "PCMP" 58#define MPCH_OEMID "BHyVe " 59#define MPCH_OEMID_LEN 8 60#define MPCH_PRODID "Hypervisor " 61#define MPCH_PRODID_LEN 12 62 63/* Processor entry defines */ 64#define MPEP_SIG_FAMILY 6 /* XXX bhyve should supply this */ 65#define MPEP_SIG_MODEL 26 66#define MPEP_SIG_STEPPING 5 67#define MPEP_SIG \ 68 ((MPEP_SIG_FAMILY << 8) | \ 69 (MPEP_SIG_MODEL << 4) | \ 70 (MPEP_SIG_STEPPING)) 71 72#define MPEP_FEATURES (0xBFEBFBFF) /* XXX Intel i7 */ 73 74/* Number of i/o intr entries */ 75#define MPEII_MAX_IRQ 16 76 77/* Define processor entry struct since <x86/mptable.h> gets it wrong */ 78typedef struct BPROCENTRY { 79 u_char type; 80 u_char apic_id; 81 u_char apic_version; 82 u_char cpu_flags; 83 uint32_t cpu_signature; 84 uint32_t feature_flags; 85 uint32_t reserved1; 86 uint32_t reserved2; 87} *bproc_entry_ptr; 88CTASSERT(sizeof(struct BPROCENTRY) == 20); 89 90/* Bus entry defines */ 91#define MPE_NUM_BUSES 2 92#define MPE_BUSNAME_LEN 6 93#define MPE_BUSNAME_ISA "ISA " 94#define MPE_BUSNAME_PCI "PCI " 95 96static void *oem_tbl_start; 97static int oem_tbl_size; 98 99static uint8_t 100mpt_compute_checksum(void *base, size_t len) 101{ 102 uint8_t *bytes; 103 uint8_t sum; 104 105 for(bytes = base, sum = 0; len > 0; len--) { 106 sum += *bytes++; 107 } 108 109 return (256 - sum); 110} 111 112static void 113mpt_build_mpfp(mpfps_t mpfp, vm_paddr_t gpa) 114{ 115 116 memset(mpfp, 0, sizeof(*mpfp)); 117 memcpy(mpfp->signature, MPFP_SIG, 4); 118 mpfp->pap = gpa + sizeof(*mpfp); 119 mpfp->length = 1; 120 mpfp->spec_rev = MP_SPECREV; 121 mpfp->checksum = mpt_compute_checksum(mpfp, sizeof(*mpfp)); 122} 123 124static void 125mpt_build_mpch(mpcth_t mpch) 126{ 127 128 memset(mpch, 0, sizeof(*mpch)); 129 memcpy(mpch->signature, MPCH_SIG, 4); 130 mpch->spec_rev = MP_SPECREV; 131 memcpy(mpch->oem_id, MPCH_OEMID, MPCH_OEMID_LEN); 132 memcpy(mpch->product_id, MPCH_PRODID, MPCH_PRODID_LEN); 133 mpch->apic_address = LAPIC_PADDR; 134} 135 136static void 137mpt_build_proc_entries(bproc_entry_ptr mpep, int ncpu) 138{ 139 int i; 140 141 for (i = 0; i < ncpu; i++) { 142 memset(mpep, 0, sizeof(*mpep)); 143 mpep->type = MPCT_ENTRY_PROCESSOR; 144 mpep->apic_id = i; // XXX 145 mpep->apic_version = LAPIC_VERSION; 146 mpep->cpu_flags = PROCENTRY_FLAG_EN; 147 if (i == 0) 148 mpep->cpu_flags |= PROCENTRY_FLAG_BP; 149 mpep->cpu_signature = MPEP_SIG; 150 mpep->feature_flags = MPEP_FEATURES; 151 mpep++; 152 } 153} 154 155static void 156mpt_build_bus_entries(bus_entry_ptr mpeb) 157{ 158 159 memset(mpeb, 0, sizeof(*mpeb)); 160 mpeb->type = MPCT_ENTRY_BUS; 161 mpeb->bus_id = 0; 162 memcpy(mpeb->bus_type, MPE_BUSNAME_PCI, MPE_BUSNAME_LEN); 163 mpeb++; 164 165 memset(mpeb, 0, sizeof(*mpeb)); 166 mpeb->type = MPCT_ENTRY_BUS; 167 mpeb->bus_id = 1; 168 memcpy(mpeb->bus_type, MPE_BUSNAME_ISA, MPE_BUSNAME_LEN); 169} 170 171static void 172mpt_build_ioapic_entries(io_apic_entry_ptr mpei, int id) 173{ 174 175 memset(mpei, 0, sizeof(*mpei)); 176 mpei->type = MPCT_ENTRY_IOAPIC; 177 mpei->apic_id = id; 178 mpei->apic_version = IOAPIC_VERSION; 179 mpei->apic_flags = IOAPICENTRY_FLAG_EN; 180 mpei->apic_address = IOAPIC_PADDR; 181} 182 183static void 184mpt_build_ioint_entries(int_entry_ptr mpie, int num_pins, int id) 185{ 186 int pin; 187 188 /* 189 * The following config is taken from kernel mptable.c 190 * mptable_parse_default_config_ints(...), for now 191 * just use the default config, tweek later if needed. 192 */ 193 194 /* Run through all 16 pins. */ 195 for (pin = 0; pin < num_pins; pin++) { 196 memset(mpie, 0, sizeof(*mpie)); 197 mpie->type = MPCT_ENTRY_INT; 198 mpie->src_bus_id = 1; 199 mpie->dst_apic_id = id; 200 201 /* 202 * All default configs route IRQs from bus 0 to the first 16 203 * pins of the first I/O APIC with an APIC ID of 2. 204 */ 205 mpie->dst_apic_int = pin; 206 switch (pin) { 207 case 0: 208 /* Pin 0 is an ExtINT pin. */ 209 mpie->int_type = INTENTRY_TYPE_EXTINT; 210 break; 211 case 2: 212 /* IRQ 0 is routed to pin 2. */ 213 mpie->int_type = INTENTRY_TYPE_INT; 214 mpie->src_bus_irq = 0; 215 break; 216 case 5: 217 case 10: 218 case 11: 219 /* 220 * PCI Irqs set to level triggered. 221 */ 222 mpie->int_flags = INTENTRY_FLAGS_TRIGGER_LEVEL; 223 mpie->src_bus_id = 0; 224 /* fall through.. */ 225 default: 226 /* All other pins are identity mapped. */ 227 mpie->int_type = INTENTRY_TYPE_INT; 228 mpie->src_bus_irq = pin; 229 break; 230 } 231 mpie++; 232 } 233 234} 235 236void 237mptable_add_oemtbl(void *tbl, int tblsz) 238{ 239 240 oem_tbl_start = tbl; 241 oem_tbl_size = tblsz; 242} 243 244int 245mptable_build(struct vmctx *ctx, int ncpu) 246{ 247 mpcth_t mpch; 248 bus_entry_ptr mpeb; 249 io_apic_entry_ptr mpei; 250 bproc_entry_ptr mpep; 251 mpfps_t mpfp; 252 int_entry_ptr mpie; 253 char *curraddr; 254 char *startaddr; 255 256 startaddr = paddr_guest2host(ctx, MPTABLE_BASE, MPTABLE_MAX_LENGTH); 257 if (startaddr == NULL) { 258 printf("mptable requires mapped mem\n"); 259 return (ENOMEM); 260 } 261 262 curraddr = startaddr; 263 mpfp = (mpfps_t)curraddr; 264 mpt_build_mpfp(mpfp, MPTABLE_BASE); 265 curraddr += sizeof(*mpfp); 266 267 mpch = (mpcth_t)curraddr; 268 mpt_build_mpch(mpch); 269 curraddr += sizeof(*mpch); 270 271 mpep = (bproc_entry_ptr)curraddr; 272 mpt_build_proc_entries(mpep, ncpu); 273 curraddr += sizeof(*mpep) * ncpu; 274 mpch->entry_count += ncpu; 275 276 mpeb = (bus_entry_ptr) curraddr; 277 mpt_build_bus_entries(mpeb); 278 curraddr += sizeof(*mpeb) * MPE_NUM_BUSES; 279 mpch->entry_count += MPE_NUM_BUSES; 280 281 mpei = (io_apic_entry_ptr)curraddr; 282 mpt_build_ioapic_entries(mpei, 0); 283 curraddr += sizeof(*mpei); 284 mpch->entry_count++; 285 286 mpie = (int_entry_ptr) curraddr; 287 mpt_build_ioint_entries(mpie, MPEII_MAX_IRQ, 0); 288 curraddr += sizeof(*mpie) * MPEII_MAX_IRQ; 289 mpch->entry_count += MPEII_MAX_IRQ; 290 291 if (oem_tbl_start) { 292 mpch->oem_table_pointer = curraddr - startaddr + MPTABLE_BASE; 293 mpch->oem_table_size = oem_tbl_size; 294 memcpy(curraddr, oem_tbl_start, oem_tbl_size); 295 } 296 297 mpch->base_table_length = curraddr - (char *)mpch; 298 mpch->checksum = mpt_compute_checksum(mpch, mpch->base_table_length); 299 300 return (0); 301} 302