2 * Copyright (c) 2012 NetApp, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
33 #include <sys/errno.h>
34 #include <x86/mptable.h>
42 #define MPTABLE_BASE 0xF0000
44 /* floating pointer length + maximum length of configuration table */
45 #define MPTABLE_MAX_LENGTH (65536 + 16)
47 #define LAPIC_PADDR 0xFEE00000
48 #define LAPIC_VERSION 16
50 #define IOAPIC_PADDR 0xFEC00000
51 #define IOAPIC_VERSION 0x11
54 #define MPFP_SIG "_MP_"
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
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
68 ((MPEP_SIG_FAMILY << 8) | \
69 (MPEP_SIG_MODEL << 4) | \
72 #define MPEP_FEATURES (0xBFEBFBFF) /* XXX Intel i7 */
74 /* Define processor entry struct since <x86/mptable.h> gets it wrong */
75 typedef struct BPROCENTRY {
80 uint32_t cpu_signature;
81 uint32_t feature_flags;
85 CTASSERT(sizeof(struct BPROCENTRY) == 20);
87 /* Bus entry defines */
88 #define MPE_NUM_BUSES 2
89 #define MPE_BUSNAME_LEN 6
90 #define MPE_BUSNAME_ISA "ISA "
91 #define MPE_BUSNAME_PCI "PCI "
93 static void *oem_tbl_start;
94 static int oem_tbl_size;
97 mpt_compute_checksum(void *base, size_t len)
102 for(bytes = base, sum = 0; len > 0; len--) {
110 mpt_build_mpfp(mpfps_t mpfp, vm_paddr_t gpa)
113 memset(mpfp, 0, sizeof(*mpfp));
114 memcpy(mpfp->signature, MPFP_SIG, 4);
115 mpfp->pap = gpa + sizeof(*mpfp);
117 mpfp->spec_rev = MP_SPECREV;
118 mpfp->checksum = mpt_compute_checksum(mpfp, sizeof(*mpfp));
122 mpt_build_mpch(mpcth_t mpch)
125 memset(mpch, 0, sizeof(*mpch));
126 memcpy(mpch->signature, MPCH_SIG, 4);
127 mpch->spec_rev = MP_SPECREV;
128 memcpy(mpch->oem_id, MPCH_OEMID, MPCH_OEMID_LEN);
129 memcpy(mpch->product_id, MPCH_PRODID, MPCH_PRODID_LEN);
130 mpch->apic_address = LAPIC_PADDR;
134 mpt_build_proc_entries(bproc_entry_ptr mpep, int ncpu)
138 for (i = 0; i < ncpu; i++) {
139 memset(mpep, 0, sizeof(*mpep));
140 mpep->type = MPCT_ENTRY_PROCESSOR;
141 mpep->apic_id = i; // XXX
142 mpep->apic_version = LAPIC_VERSION;
143 mpep->cpu_flags = PROCENTRY_FLAG_EN;
145 mpep->cpu_flags |= PROCENTRY_FLAG_BP;
146 mpep->cpu_signature = MPEP_SIG;
147 mpep->feature_flags = MPEP_FEATURES;
153 mpt_build_bus_entries(bus_entry_ptr mpeb)
156 memset(mpeb, 0, sizeof(*mpeb));
157 mpeb->type = MPCT_ENTRY_BUS;
159 memcpy(mpeb->bus_type, MPE_BUSNAME_ISA, MPE_BUSNAME_LEN);
162 memset(mpeb, 0, sizeof(*mpeb));
163 mpeb->type = MPCT_ENTRY_BUS;
165 memcpy(mpeb->bus_type, MPE_BUSNAME_PCI, MPE_BUSNAME_LEN);
169 mpt_build_ioapic_entries(io_apic_entry_ptr mpei, int id)
172 memset(mpei, 0, sizeof(*mpei));
173 mpei->type = MPCT_ENTRY_IOAPIC;
175 mpei->apic_version = IOAPIC_VERSION;
176 mpei->apic_flags = IOAPICENTRY_FLAG_EN;
177 mpei->apic_address = IOAPIC_PADDR;
182 mpt_build_ioint_entries(struct mpe_ioint *mpeii, int num_pins, int id)
187 * The following config is taken from kernel mptable.c
188 * mptable_parse_default_config_ints(...), for now
189 * just use the default config, tweek later if needed.
193 /* Run through all 16 pins. */
194 for (pin = 0; pin < num_pins; pin++) {
195 memset(mpeii, 0, sizeof(*mpeii));
196 mpeii->entry_type = MP_ENTRY_IOINT;
197 mpeii->src_bus_id = MPE_BUSID_ISA;
198 mpeii->dst_apic_id = id;
201 * All default configs route IRQs from bus 0 to the first 16
202 * pins of the first I/O APIC with an APIC ID of 2.
204 mpeii->dst_apic_intin = pin;
207 /* Pin 0 is an ExtINT pin. */
208 mpeii->intr_type = MPEII_INTR_EXTINT;
211 /* IRQ 0 is routed to pin 2. */
212 mpeii->intr_type = MPEII_INTR_INT;
213 mpeii->src_bus_irq = 0;
219 * PCI Irqs set to level triggered.
221 mpeii->intr_flags = MPEII_FLAGS_TRIGMODE_LEVEL;
222 mpeii->src_bus_id = MPE_BUSID_PCI;
224 /* All other pins are identity mapped. */
225 mpeii->intr_type = MPEII_INTR_INT;
226 mpeii->src_bus_irq = pin;
234 #define COPYSTR(dest, src, bytes) \
235 memcpy(dest, src, bytes); \
239 mptable_dump(struct mp_floating_pointer *mpfp, struct mp_config_hdr *mpch)
246 struct mpe_proc *proc;
248 struct mpe_ioapic *ioapic;
249 struct mpe_ioint *ioint;
250 struct mpe_lint *lnit;
256 printf(" MP Floating Pointer :\n");
257 COPYSTR(str, mpfp->signature, 4);
258 printf("\tsignature:\t%s\n", str);
259 printf("\tmpch paddr:\t%x\n", mpfp->mptable_paddr);
260 printf("\tlength:\t%x\n", mpfp->length);
261 printf("\tspecrec:\t%x\n", mpfp->specrev);
262 printf("\tchecksum:\t%x\n", mpfp->checksum);
263 printf("\tfeature1:\t%x\n", mpfp->feature1);
264 printf("\tfeature2:\t%x\n", mpfp->feature2);
265 printf("\tfeature3:\t%x\n", mpfp->feature3);
266 printf("\tfeature4:\t%x\n", mpfp->feature4);
268 printf(" MP Configuration Header :\n");
269 COPYSTR(str, mpch->signature, 4);
270 printf(" signature: %s\n", str);
271 printf(" length: %x\n", mpch->length);
272 printf(" specrec: %x\n", mpch->specrev);
273 printf(" checksum: %x\n", mpch->checksum);
274 COPYSTR(str, mpch->oemid, MPCH_OEMID_LEN);
275 printf(" oemid: %s\n", str);
276 COPYSTR(str, mpch->prodid, MPCH_PRODID_LEN);
277 printf(" prodid: %s\n", str);
278 printf(" oem_ptr: %x\n", mpch->oem_ptr);
279 printf(" oem_sz: %x\n", mpch->oem_sz);
280 printf(" nr_entries: %x\n", mpch->nr_entries);
281 printf(" apic paddr: %x\n", mpch->lapic_paddr);
282 printf(" ext_length: %x\n", mpch->ext_length);
283 printf(" ext_checksum: %x\n", mpch->ext_checksum);
285 cur = (char *)mpch + sizeof(*mpch);
286 for (i = 0; i < mpch->nr_entries; i++) {
290 printf(" MP Processor Entry :\n");
291 printf(" lapic_id: %x\n", mpe.proc->lapic_id);
292 printf(" lapic_version: %x\n", mpe.proc->lapic_version);
293 printf(" proc_flags: %x\n", mpe.proc->proc_flags);
294 printf(" proc_signature: %x\n", mpe.proc->proc_signature);
295 printf(" feature_flags: %x\n", mpe.proc->feature_flags);
296 cur += sizeof(struct mpe_proc);
299 printf(" MP Bus Entry :\n");
300 printf(" busid: %x\n", mpe.bus->busid);
301 COPYSTR(str, mpe.bus->busname, MPE_BUSNAME_LEN);
302 printf(" busname: %s\n", str);
303 cur += sizeof(struct mpe_bus);
305 case MP_ENTRY_IOAPIC:
306 printf(" MP IOAPIC Entry :\n");
307 printf(" ioapi_id: %x\n", mpe.ioapic->ioapic_id);
308 printf(" ioapi_version: %x\n", mpe.ioapic->ioapic_version);
309 printf(" ioapi_flags: %x\n", mpe.ioapic->ioapic_flags);
310 printf(" ioapi_paddr: %x\n", mpe.ioapic->ioapic_paddr);
311 cur += sizeof(struct mpe_ioapic);
314 printf(" MP IO Interrupt Entry :\n");
315 printf(" intr_type: %x\n", mpe.ioint->intr_type);
316 printf(" intr_flags: %x\n", mpe.ioint->intr_flags);
317 printf(" src_bus_id: %x\n", mpe.ioint->src_bus_id);
318 printf(" src_bus_irq: %x\n", mpe.ioint->src_bus_irq);
319 printf(" dst_apic_id: %x\n", mpe.ioint->dst_apic_id);
320 printf(" dst_apic_intin: %x\n", mpe.ioint->dst_apic_intin);
321 cur += sizeof(struct mpe_ioint);
324 printf(" MP Local Interrupt Entry :\n");
325 cur += sizeof(struct mpe_lint);
334 mptable_add_oemtbl(void *tbl, int tblsz)
338 oem_tbl_size = tblsz;
342 mptable_build(struct vmctx *ctx, int ncpu, int ioapic)
346 io_apic_entry_ptr mpei;
347 bproc_entry_ptr mpep;
352 startaddr = paddr_guest2host(ctx, MPTABLE_BASE, MPTABLE_MAX_LENGTH);
353 if (startaddr == NULL) {
354 printf("mptable requires mapped mem\n");
358 curraddr = startaddr;
359 mpfp = (mpfps_t)curraddr;
360 mpt_build_mpfp(mpfp, MPTABLE_BASE);
361 curraddr += sizeof(*mpfp);
363 mpch = (mpcth_t)curraddr;
364 mpt_build_mpch(mpch);
365 curraddr += sizeof(*mpch);
367 mpep = (bproc_entry_ptr)curraddr;
368 mpt_build_proc_entries(mpep, ncpu);
369 curraddr += sizeof(*mpep) * ncpu;
370 mpch->entry_count += ncpu;
372 mpeb = (bus_entry_ptr) curraddr;
373 mpt_build_bus_entries(mpeb);
374 curraddr += sizeof(*mpeb) * MPE_NUM_BUSES;
375 mpch->entry_count += MPE_NUM_BUSES;
378 mpei = (io_apic_entry_ptr)curraddr;
379 mpt_build_ioapic_entries(mpei, ncpu + 1);
380 curraddr += sizeof(*mpei);
385 mpt_build_ioint_entries((struct mpe_ioint*)curraddr, MPEII_MAX_IRQ,
387 curraddr += sizeof(struct mpe_ioint) * MPEII_MAX_IRQ;
388 mpch->entry_count += MPEII_MAX_IRQ;
392 mpch->oem_table_pointer = curraddr - startaddr + MPTABLE_BASE;
393 mpch->oem_table_size = oem_tbl_size;
394 memcpy(curraddr, oem_tbl_start, oem_tbl_size);
397 mpch->base_table_length = curraddr - (char *)mpch;
398 mpch->checksum = mpt_compute_checksum(mpch, sizeof(*mpch));