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>
44 #define MPTABLE_BASE 0xF0000
46 /* floating pointer length + maximum length of configuration table */
47 #define MPTABLE_MAX_LENGTH (65536 + 16)
49 #define LAPIC_PADDR 0xFEE00000
50 #define LAPIC_VERSION 16
52 #define IOAPIC_PADDR 0xFEC00000
53 #define IOAPIC_VERSION 0x11
56 #define MPFP_SIG "_MP_"
58 /* Configuration header defines */
59 #define MPCH_SIG "PCMP"
60 #define MPCH_OEMID "BHyVe "
61 #define MPCH_OEMID_LEN 8
62 #define MPCH_PRODID "Hypervisor "
63 #define MPCH_PRODID_LEN 12
65 /* Processor entry defines */
66 #define MPEP_SIG_FAMILY 6 /* XXX bhyve should supply this */
67 #define MPEP_SIG_MODEL 26
68 #define MPEP_SIG_STEPPING 5
70 ((MPEP_SIG_FAMILY << 8) | \
71 (MPEP_SIG_MODEL << 4) | \
74 #define MPEP_FEATURES (0xBFEBFBFF) /* XXX Intel i7 */
76 /* Number of local intr entries */
77 #define MPEII_NUM_LOCAL_IRQ 2
79 /* Bus entry defines */
80 #define MPE_NUM_BUSES 2
81 #define MPE_BUSNAME_LEN 6
82 #define MPE_BUSNAME_ISA "ISA "
83 #define MPE_BUSNAME_PCI "PCI "
85 static void *oem_tbl_start;
86 static int oem_tbl_size;
89 mpt_compute_checksum(void *base, size_t len)
94 for(bytes = base, sum = 0; len > 0; len--) {
102 mpt_build_mpfp(mpfps_t mpfp, vm_paddr_t gpa)
105 memset(mpfp, 0, sizeof(*mpfp));
106 memcpy(mpfp->signature, MPFP_SIG, 4);
107 mpfp->pap = gpa + sizeof(*mpfp);
109 mpfp->spec_rev = MP_SPECREV;
110 mpfp->checksum = mpt_compute_checksum(mpfp, sizeof(*mpfp));
114 mpt_build_mpch(mpcth_t mpch)
117 memset(mpch, 0, sizeof(*mpch));
118 memcpy(mpch->signature, MPCH_SIG, 4);
119 mpch->spec_rev = MP_SPECREV;
120 memcpy(mpch->oem_id, MPCH_OEMID, MPCH_OEMID_LEN);
121 memcpy(mpch->product_id, MPCH_PRODID, MPCH_PRODID_LEN);
122 mpch->apic_address = LAPIC_PADDR;
126 mpt_build_proc_entries(proc_entry_ptr mpep, int ncpu)
130 for (i = 0; i < ncpu; i++) {
131 memset(mpep, 0, sizeof(*mpep));
132 mpep->type = MPCT_ENTRY_PROCESSOR;
133 mpep->apic_id = i; // XXX
134 mpep->apic_version = LAPIC_VERSION;
135 mpep->cpu_flags = PROCENTRY_FLAG_EN;
137 mpep->cpu_flags |= PROCENTRY_FLAG_BP;
138 mpep->cpu_signature = MPEP_SIG;
139 mpep->feature_flags = MPEP_FEATURES;
145 mpt_build_localint_entries(int_entry_ptr mpie)
148 /* Hardcode LINT0 as ExtINT on all CPUs. */
149 memset(mpie, 0, sizeof(*mpie));
150 mpie->type = MPCT_ENTRY_LOCAL_INT;
151 mpie->int_type = INTENTRY_TYPE_EXTINT;
152 mpie->int_flags = INTENTRY_FLAGS_POLARITY_CONFORM |
153 INTENTRY_FLAGS_TRIGGER_CONFORM;
154 mpie->dst_apic_id = 0xff;
155 mpie->dst_apic_int = 0;
158 /* Hardcode LINT1 as NMI on all CPUs. */
159 memset(mpie, 0, sizeof(*mpie));
160 mpie->type = MPCT_ENTRY_LOCAL_INT;
161 mpie->int_type = INTENTRY_TYPE_NMI;
162 mpie->int_flags = INTENTRY_FLAGS_POLARITY_CONFORM |
163 INTENTRY_FLAGS_TRIGGER_CONFORM;
164 mpie->dst_apic_id = 0xff;
165 mpie->dst_apic_int = 1;
169 mpt_build_bus_entries(bus_entry_ptr mpeb)
172 memset(mpeb, 0, sizeof(*mpeb));
173 mpeb->type = MPCT_ENTRY_BUS;
175 memcpy(mpeb->bus_type, MPE_BUSNAME_PCI, MPE_BUSNAME_LEN);
178 memset(mpeb, 0, sizeof(*mpeb));
179 mpeb->type = MPCT_ENTRY_BUS;
181 memcpy(mpeb->bus_type, MPE_BUSNAME_ISA, MPE_BUSNAME_LEN);
185 mpt_build_ioapic_entries(io_apic_entry_ptr mpei, int id)
188 memset(mpei, 0, sizeof(*mpei));
189 mpei->type = MPCT_ENTRY_IOAPIC;
191 mpei->apic_version = IOAPIC_VERSION;
192 mpei->apic_flags = IOAPICENTRY_FLAG_EN;
193 mpei->apic_address = IOAPIC_PADDR;
197 mpt_count_ioint_entries(void)
201 * Always include entries for the first 16 pins along with a entry
202 * for each active PCI INTx pin.
204 return (16 + pci_count_lintr());
208 mpt_generate_pci_int(int slot, int pin, int ioapic_irq, void *arg)
210 int_entry_ptr *mpiep, mpie;
214 memset(mpie, 0, sizeof(*mpie));
217 * This is always after another I/O interrupt entry, so cheat
218 * and fetch the I/O APIC ID from the prior entry.
220 mpie->type = MPCT_ENTRY_INT;
221 mpie->int_type = INTENTRY_TYPE_INT;
222 mpie->src_bus_id = 0;
223 mpie->src_bus_irq = slot << 2 | (pin - 1);
224 mpie->dst_apic_id = mpie[-1].dst_apic_id;
225 mpie->dst_apic_int = ioapic_irq;
231 mpt_build_ioint_entries(int_entry_ptr mpie, int id)
236 * The following config is taken from kernel mptable.c
237 * mptable_parse_default_config_ints(...), for now
238 * just use the default config, tweek later if needed.
241 /* First, generate the first 16 pins. */
242 for (pin = 0; pin < 16; pin++) {
243 memset(mpie, 0, sizeof(*mpie));
244 mpie->type = MPCT_ENTRY_INT;
245 mpie->src_bus_id = 1;
246 mpie->dst_apic_id = id;
249 * All default configs route IRQs from bus 0 to the first 16
250 * pins of the first I/O APIC with an APIC ID of 2.
252 mpie->dst_apic_int = pin;
255 /* Pin 0 is an ExtINT pin. */
256 mpie->int_type = INTENTRY_TYPE_EXTINT;
259 /* IRQ 0 is routed to pin 2. */
260 mpie->int_type = INTENTRY_TYPE_INT;
261 mpie->src_bus_irq = 0;
264 /* ACPI SCI is level triggered and active-lo. */
265 mpie->int_flags = INTENTRY_FLAGS_POLARITY_ACTIVELO |
266 INTENTRY_FLAGS_TRIGGER_LEVEL;
267 mpie->int_type = INTENTRY_TYPE_INT;
268 mpie->src_bus_irq = SCI_INT;
271 /* All other pins are identity mapped. */
272 mpie->int_type = INTENTRY_TYPE_INT;
273 mpie->src_bus_irq = pin;
279 /* Next, generate entries for any PCI INTx interrupts. */
280 pci_walk_lintr(mpt_generate_pci_int, &mpie);
284 mptable_add_oemtbl(void *tbl, int tblsz)
288 oem_tbl_size = tblsz;
292 mptable_build(struct vmctx *ctx, int ncpu)
296 io_apic_entry_ptr mpei;
304 startaddr = paddr_guest2host(ctx, MPTABLE_BASE, MPTABLE_MAX_LENGTH);
305 if (startaddr == NULL) {
306 printf("mptable requires mapped mem\n");
310 curraddr = startaddr;
311 mpfp = (mpfps_t)curraddr;
312 mpt_build_mpfp(mpfp, MPTABLE_BASE);
313 curraddr += sizeof(*mpfp);
315 mpch = (mpcth_t)curraddr;
316 mpt_build_mpch(mpch);
317 curraddr += sizeof(*mpch);
319 mpep = (proc_entry_ptr)curraddr;
320 mpt_build_proc_entries(mpep, ncpu);
321 curraddr += sizeof(*mpep) * ncpu;
322 mpch->entry_count += ncpu;
324 mpeb = (bus_entry_ptr) curraddr;
325 mpt_build_bus_entries(mpeb);
326 curraddr += sizeof(*mpeb) * MPE_NUM_BUSES;
327 mpch->entry_count += MPE_NUM_BUSES;
329 mpei = (io_apic_entry_ptr)curraddr;
330 mpt_build_ioapic_entries(mpei, 0);
331 curraddr += sizeof(*mpei);
334 mpie = (int_entry_ptr) curraddr;
335 ioints = mpt_count_ioint_entries();
336 mpt_build_ioint_entries(mpie, 0);
337 curraddr += sizeof(*mpie) * ioints;
338 mpch->entry_count += ioints;
340 mpie = (int_entry_ptr)curraddr;
341 mpt_build_localint_entries(mpie);
342 curraddr += sizeof(*mpie) * MPEII_NUM_LOCAL_IRQ;
343 mpch->entry_count += MPEII_NUM_LOCAL_IRQ;
346 mpch->oem_table_pointer = curraddr - startaddr + MPTABLE_BASE;
347 mpch->oem_table_size = oem_tbl_size;
348 memcpy(curraddr, oem_tbl_start, oem_tbl_size);
351 mpch->base_table_length = curraddr - (char *)mpch;
352 mpch->checksum = mpt_compute_checksum(mpch, mpch->base_table_length);