mptbl.c revision 221828
1/*- 2 * Copyright (c) 2011 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/mman.h> 34 35#include <stdio.h> 36#include <string.h> 37#include <machine/vmm.h> 38#include <machine/vmm_dev.h> 39 40#include "vmmapi.h" 41#include "mptable.h" 42 43#define LAPIC_PADDR (0xFEE00000) 44#define LAPIC_VERSION (16) 45 46#define IOAPIC_PADDR (0xFEC00000) 47#define IOAPIC_VERSION (0x11) 48 49extern int errno; 50 51static uint8_t 52mp_compute_checksum(void *base, size_t len) 53{ 54 uint8_t *bytes = base; 55 uint8_t sum = 0; 56 for(; len > 0; len--) { 57 sum += *bytes++; 58 } 59 return 256 - sum; 60} 61 62static void 63mp_build_mpfp(struct mp_floating_pointer *mpfp, vm_paddr_t mpfp_gpa) 64{ 65 memset(mpfp, 0, sizeof(*mpfp)); 66 memcpy(mpfp->signature, MPFP_SIGNATURE, MPFP_SIGNATURE_LEN); 67 mpfp->mptable_paddr = mpfp_gpa + sizeof(*mpfp); 68 mpfp->specrev = MP_SPECREV; 69 mpfp->feature2 = 0; 70 mpfp->checksum = mp_compute_checksum(mpfp, sizeof(*mpfp)); 71} 72 73static void 74mp_build_mpch(struct mp_config_hdr *mpch) 75{ 76 memset(mpch, 0, sizeof(*mpch)); 77 mpch->specrev = MP_SPECREV; 78 memcpy(mpch->signature, MPCH_SIGNATURE, MPCH_SIGNATURE_LEN); 79 memcpy(mpch->oemid, MPCH_OEMID, MPCH_OEMID_LEN); 80 memcpy(mpch->prodid, MPCH_PRODID, MPCH_PRODID_LEN); 81 mpch->lapic_paddr = LAPIC_PADDR; 82 83 84} 85 86static void 87mp_build_proc_entries(struct mpe_proc *mpep, int num_proc) 88{ 89 int i; 90 91 for (i = 0; i < num_proc; i++) { 92 memset(mpep, 0, sizeof(*mpep)); 93 mpep->entry_type = MP_ENTRY_PROC; 94 mpep->lapic_id = i; // XXX 95 mpep->lapic_version = LAPIC_VERSION; 96 mpep->proc_flags = (i == 0)?MPEP_FLAGS_BSP:0; 97 mpep->proc_flags |= MPEP_FLAGS_EN; 98 mpep->proc_signature = MPEP_SIGNATURE; 99 mpep->feature_flags = MPEP_FEATURES; 100 mpep++; 101 } 102 103} 104 105static void 106mp_build_bus_entries(struct mpe_bus *mpeb) 107{ 108 memset(mpeb, 0, sizeof(*mpeb)); 109 mpeb->entry_type = MP_ENTRY_BUS; 110 mpeb->busid = MPE_BUSID_ISA; 111 memcpy(mpeb->busname, MPE_BUSNAME_ISA, MPE_BUSNAME_LEN); 112 mpeb++; 113 114 memset(mpeb, 0, sizeof(*mpeb)); 115 mpeb->entry_type = MP_ENTRY_BUS; 116 mpeb->busid = MPE_BUSID_PCI; 117 memcpy(mpeb->busname, MPE_BUSNAME_PCI, MPE_BUSNAME_LEN); 118 119} 120 121static void 122mp_build_ioapic_entries(struct mpe_ioapic *mpei) 123{ 124 memset(mpei, 0, sizeof(*mpei)); 125 mpei->entry_type = MP_ENTRY_IOAPIC; 126 mpei->ioapic_id = MPE_IOAPIC_ID; 127 mpei->ioapic_version = IOAPIC_VERSION; 128 mpei->ioapic_flags = MPE_IOAPIC_FLAG_EN; 129 mpei->ioapic_paddr = IOAPIC_PADDR; 130} 131 132static void 133mp_build_ioint_entries(struct mpe_ioint *mpeii, int num_pins) 134{ 135 int pin; 136 137 /* 138 * The following config is taken from kernel mptable.c 139 * mptable_parse_default_config_ints(...), for now 140 * just use the default config, tweek later if needed. 141 */ 142 143 144 /* Run through all 16 pins. */ 145 for (pin = 0; pin < num_pins; pin++) { 146 memset(mpeii, 0, sizeof(*mpeii)); 147 mpeii->entry_type = MP_ENTRY_IOINT; 148 mpeii->src_bus_id = MPE_BUSID_ISA; 149 mpeii->dst_apic_id = MPE_IOAPIC_ID; 150 151 /* 152 * All default configs route IRQs from bus 0 to the first 16 pins 153 * of the first I/O APIC with an APIC ID of 2. 154 */ 155 mpeii->dst_apic_intin = pin; 156 switch (pin) { 157 case 0: 158 /* Pin 0 is an ExtINT pin. */ 159 mpeii->intr_type = MPEII_INTR_EXTINT; 160 break; 161 case 2: 162 /* IRQ 0 is routed to pin 2. */ 163 mpeii->intr_type = MPEII_INTR_INT; 164 mpeii->src_bus_irq = 0; 165 break; 166 case 5: 167 case 10: 168 case 11: 169 /* 170 * PCI Irqs set to level triggered. 171 */ 172 mpeii->intr_flags = MPEII_FLAGS_TRIGMODE_LEVEL; 173 mpeii->src_bus_id = MPE_BUSID_PCI; 174 default: 175 /* All other pins are identity mapped. */ 176 mpeii->intr_type = MPEII_INTR_INT; 177 mpeii->src_bus_irq = pin; 178 break; 179 } 180 mpeii++; 181 } 182 183} 184 185#define COPYSTR(dest, src, bytes) \ 186 memcpy(dest, src, bytes); \ 187 str[bytes] = 0; 188 189 190static void 191mptable_dump(struct mp_floating_pointer *mpfp, struct mp_config_hdr *mpch) 192{ 193 static char str[16]; 194 int i; 195 char *cur; 196 197 union mpe { 198 struct mpe_proc *proc; 199 struct mpe_bus *bus; 200 struct mpe_ioapic *ioapic; 201 struct mpe_ioint *ioint; 202 struct mpe_lint *lnit; 203 char *p; 204 }; 205 206 union mpe mpe; 207 208 printf(" MP Floating Pointer :\n"); 209 COPYSTR(str, mpfp->signature, 4); 210 printf(" signature: %s\n", str); 211 printf(" mpch paddr: %x\n", mpfp->mptable_paddr); 212 printf(" length: %x\n", mpfp->length); 213 printf(" specrec: %x\n", mpfp->specrev); 214 printf(" checksum: %x\n", mpfp->checksum); 215 printf(" feature1: %x\n", mpfp->feature1); 216 printf(" feature2: %x\n", mpfp->feature2); 217 printf(" feature3: %x\n", mpfp->feature3); 218 printf(" feature4: %x\n", mpfp->feature4); 219 220 printf(" MP Configuration Header :\n"); 221 COPYSTR(str, mpch->signature, 4); 222 printf(" signature: %s\n", str); 223 printf(" length: %x\n", mpch->length); 224 printf(" specrec: %x\n", mpch->specrev); 225 printf(" checksum: %x\n", mpch->checksum); 226 COPYSTR(str, mpch->oemid, MPCH_OEMID_LEN); 227 printf(" oemid: %s\n", str); 228 COPYSTR(str, mpch->prodid, MPCH_PRODID_LEN); 229 printf(" prodid: %s\n", str); 230 printf(" oem_ptr: %x\n", mpch->oem_ptr); 231 printf(" oem_sz: %x\n", mpch->oem_sz); 232 printf(" nr_entries: %x\n", mpch->nr_entries); 233 printf(" apic paddr: %x\n", mpch->lapic_paddr); 234 printf(" ext_length: %x\n", mpch->ext_length); 235 printf(" ext_checksum: %x\n", mpch->ext_checksum); 236 237 cur = (char *)mpch + sizeof(*mpch); 238 for (i = 0; i < mpch->nr_entries; i++) { 239 mpe.p = cur; 240 switch(*mpe.p) { 241 case MP_ENTRY_PROC: 242 printf(" MP Processor Entry :\n"); 243 printf(" lapic_id: %x\n", mpe.proc->lapic_id); 244 printf(" lapic_version: %x\n", mpe.proc->lapic_version); 245 printf(" proc_flags: %x\n", mpe.proc->proc_flags); 246 printf(" proc_signature: %x\n", mpe.proc->proc_signature); 247 printf(" feature_flags: %x\n", mpe.proc->feature_flags); 248 cur += sizeof(struct mpe_proc); 249 break; 250 case MP_ENTRY_BUS: 251 printf(" MP Bus Entry :\n"); 252 printf(" busid: %x\n", mpe.bus->busid); 253 COPYSTR(str, mpe.bus->busname, MPE_BUSNAME_LEN); 254 printf(" busname: %s\n", str); 255 cur += sizeof(struct mpe_bus); 256 break; 257 case MP_ENTRY_IOAPIC: 258 printf(" MP IOAPIC Entry :\n"); 259 printf(" ioapi_id: %x\n", mpe.ioapic->ioapic_id); 260 printf(" ioapi_version: %x\n", mpe.ioapic->ioapic_version); 261 printf(" ioapi_flags: %x\n", mpe.ioapic->ioapic_flags); 262 printf(" ioapi_paddr: %x\n", mpe.ioapic->ioapic_paddr); 263 cur += sizeof(struct mpe_ioapic); 264 break; 265 case MP_ENTRY_IOINT: 266 printf(" MP IO Interrupt Entry :\n"); 267 printf(" intr_type: %x\n", mpe.ioint->intr_type); 268 printf(" intr_flags: %x\n", mpe.ioint->intr_flags); 269 printf(" src_bus_id: %x\n", mpe.ioint->src_bus_id); 270 printf(" src_bus_irq: %x\n", mpe.ioint->src_bus_irq); 271 printf(" dst_apic_id: %x\n", mpe.ioint->dst_apic_id); 272 printf(" dst_apic_intin: %x\n", mpe.ioint->dst_apic_intin); 273 cur += sizeof(struct mpe_ioint); 274 break; 275 case MP_ENTRY_LINT: 276 printf(" MP Local Interrupt Entry :\n"); 277 cur += sizeof(struct mpe_lint); 278 break; 279 } 280 281 } 282} 283 284int 285vm_build_mptable(struct vmctx *ctx, vm_paddr_t gpa, int len, int ncpu, 286 void *oemp, int oemsz) 287{ 288 struct mp_config_hdr *mpch; 289 char *mapaddr; 290 char *startaddr; 291 int error; 292 293 mapaddr = vm_map_memory(ctx, gpa, len); 294 if (mapaddr == MAP_FAILED) { 295 printf("%s\n", strerror(errno)); 296 goto err; 297 } 298 startaddr = mapaddr; 299 300 mp_build_mpfp((struct mp_floating_pointer*) mapaddr, gpa); 301 mapaddr += sizeof(struct mp_floating_pointer); 302 303 mpch = (struct mp_config_hdr*)mapaddr; 304 mp_build_mpch(mpch); 305 mapaddr += sizeof(struct mp_config_hdr); 306 307 mp_build_proc_entries((struct mpe_proc*) mapaddr, ncpu); 308 mapaddr += (sizeof(struct mpe_proc)*ncpu); 309 mpch->nr_entries += ncpu; 310 311 mp_build_bus_entries((struct mpe_bus*)mapaddr); 312 mapaddr += (sizeof(struct mpe_bus)*MPE_NUM_BUSES); 313 mpch->nr_entries += MPE_NUM_BUSES; 314#if 0 315 mp_build_ioapic_entries((struct mpe_ioapic*)mapaddr); 316 mapaddr += sizeof(struct mpe_ioapic); 317 mpch->nr_entries++; 318 319 mp_build_ioint_entries((struct mpe_ioint*)mapaddr, MPEII_MAX_IRQ); 320 mapaddr += sizeof(struct mpe_ioint)*MPEII_MAX_IRQ; 321 mpch->nr_entries += MPEII_MAX_IRQ; 322 323#endif 324 if (oemp) { 325 mpch->oem_ptr = mapaddr - startaddr + gpa; 326 mpch->oem_sz = oemsz; 327 memcpy(mapaddr, oemp, oemsz); 328 } 329 mpch->length = (mapaddr) - ((char*) mpch); 330 mpch->checksum = mp_compute_checksum(mpch, sizeof(*mpch)); 331 332 333 // mptable_dump((struct mp_floating_pointer*)startaddr, mpch); 334err: 335 return (error); 336} 337