xref: /illumos-gate/usr/src/cmd/bhyve/amd64/mptbl.c (revision 5c4a5fe16715fb423db76577a6883b5bbecdbe45)
1*5c4a5fe1SAndy Fiddaman /*-
2*5c4a5fe1SAndy Fiddaman  * SPDX-License-Identifier: BSD-2-Clause
3*5c4a5fe1SAndy Fiddaman  *
4*5c4a5fe1SAndy Fiddaman  * Copyright (c) 2012 NetApp, Inc.
5*5c4a5fe1SAndy Fiddaman  * All rights reserved.
6*5c4a5fe1SAndy Fiddaman  *
7*5c4a5fe1SAndy Fiddaman  * Redistribution and use in source and binary forms, with or without
8*5c4a5fe1SAndy Fiddaman  * modification, are permitted provided that the following conditions
9*5c4a5fe1SAndy Fiddaman  * are met:
10*5c4a5fe1SAndy Fiddaman  * 1. Redistributions of source code must retain the above copyright
11*5c4a5fe1SAndy Fiddaman  *    notice, this list of conditions and the following disclaimer.
12*5c4a5fe1SAndy Fiddaman  * 2. Redistributions in binary form must reproduce the above copyright
13*5c4a5fe1SAndy Fiddaman  *    notice, this list of conditions and the following disclaimer in the
14*5c4a5fe1SAndy Fiddaman  *    documentation and/or other materials provided with the distribution.
15*5c4a5fe1SAndy Fiddaman  *
16*5c4a5fe1SAndy Fiddaman  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17*5c4a5fe1SAndy Fiddaman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*5c4a5fe1SAndy Fiddaman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*5c4a5fe1SAndy Fiddaman  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20*5c4a5fe1SAndy Fiddaman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*5c4a5fe1SAndy Fiddaman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*5c4a5fe1SAndy Fiddaman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*5c4a5fe1SAndy Fiddaman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*5c4a5fe1SAndy Fiddaman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*5c4a5fe1SAndy Fiddaman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*5c4a5fe1SAndy Fiddaman  * SUCH DAMAGE.
27*5c4a5fe1SAndy Fiddaman  */
28*5c4a5fe1SAndy Fiddaman 
29*5c4a5fe1SAndy Fiddaman 
30*5c4a5fe1SAndy Fiddaman #include <sys/types.h>
31*5c4a5fe1SAndy Fiddaman #include <sys/errno.h>
32*5c4a5fe1SAndy Fiddaman #include <x86/mptable.h>
33*5c4a5fe1SAndy Fiddaman 
34*5c4a5fe1SAndy Fiddaman #include <stdio.h>
35*5c4a5fe1SAndy Fiddaman #include <string.h>
36*5c4a5fe1SAndy Fiddaman 
37*5c4a5fe1SAndy Fiddaman #include "acpi.h"
38*5c4a5fe1SAndy Fiddaman #include "debug.h"
39*5c4a5fe1SAndy Fiddaman #include "bhyverun.h"
40*5c4a5fe1SAndy Fiddaman #include "mptbl.h"
41*5c4a5fe1SAndy Fiddaman #include "pci_emul.h"
42*5c4a5fe1SAndy Fiddaman 
43*5c4a5fe1SAndy Fiddaman #define MPTABLE_BASE		0xE0000
44*5c4a5fe1SAndy Fiddaman 
45*5c4a5fe1SAndy Fiddaman /* floating pointer length + maximum length of configuration table */
46*5c4a5fe1SAndy Fiddaman #define	MPTABLE_MAX_LENGTH	(65536 + 16)
47*5c4a5fe1SAndy Fiddaman 
48*5c4a5fe1SAndy Fiddaman #define LAPIC_PADDR		0xFEE00000
49*5c4a5fe1SAndy Fiddaman #define LAPIC_VERSION 		16
50*5c4a5fe1SAndy Fiddaman 
51*5c4a5fe1SAndy Fiddaman #define IOAPIC_PADDR		0xFEC00000
52*5c4a5fe1SAndy Fiddaman #define IOAPIC_VERSION		0x11
53*5c4a5fe1SAndy Fiddaman 
54*5c4a5fe1SAndy Fiddaman #define MP_SPECREV		4
55*5c4a5fe1SAndy Fiddaman #define MPFP_SIG		"_MP_"
56*5c4a5fe1SAndy Fiddaman 
57*5c4a5fe1SAndy Fiddaman /* Configuration header defines */
58*5c4a5fe1SAndy Fiddaman #define MPCH_SIG		"PCMP"
59*5c4a5fe1SAndy Fiddaman #define MPCH_OEMID		"BHyVe   "
60*5c4a5fe1SAndy Fiddaman #define MPCH_OEMID_LEN          8
61*5c4a5fe1SAndy Fiddaman #define MPCH_PRODID             "Hypervisor  "
62*5c4a5fe1SAndy Fiddaman #define MPCH_PRODID_LEN         12
63*5c4a5fe1SAndy Fiddaman 
64*5c4a5fe1SAndy Fiddaman /* Processor entry defines */
65*5c4a5fe1SAndy Fiddaman #define MPEP_SIG_FAMILY		6	/* XXX bhyve should supply this */
66*5c4a5fe1SAndy Fiddaman #define MPEP_SIG_MODEL		26
67*5c4a5fe1SAndy Fiddaman #define MPEP_SIG_STEPPING	5
68*5c4a5fe1SAndy Fiddaman #define MPEP_SIG		\
69*5c4a5fe1SAndy Fiddaman 	((MPEP_SIG_FAMILY << 8) | \
70*5c4a5fe1SAndy Fiddaman 	 (MPEP_SIG_MODEL << 4)	| \
71*5c4a5fe1SAndy Fiddaman 	 (MPEP_SIG_STEPPING))
72*5c4a5fe1SAndy Fiddaman 
73*5c4a5fe1SAndy Fiddaman #define MPEP_FEATURES           (0xBFEBFBFF) /* XXX Intel i7 */
74*5c4a5fe1SAndy Fiddaman 
75*5c4a5fe1SAndy Fiddaman /* Number of local intr entries */
76*5c4a5fe1SAndy Fiddaman #define	MPEII_NUM_LOCAL_IRQ	2
77*5c4a5fe1SAndy Fiddaman 
78*5c4a5fe1SAndy Fiddaman /* Bus entry defines */
79*5c4a5fe1SAndy Fiddaman #define MPE_NUM_BUSES		2
80*5c4a5fe1SAndy Fiddaman #define MPE_BUSNAME_LEN		6
81*5c4a5fe1SAndy Fiddaman #define MPE_BUSNAME_ISA		"ISA   "
82*5c4a5fe1SAndy Fiddaman #define MPE_BUSNAME_PCI		"PCI   "
83*5c4a5fe1SAndy Fiddaman 
84*5c4a5fe1SAndy Fiddaman static void *oem_tbl_start;
85*5c4a5fe1SAndy Fiddaman static int oem_tbl_size;
86*5c4a5fe1SAndy Fiddaman 
87*5c4a5fe1SAndy Fiddaman static uint8_t
mpt_compute_checksum(void * base,size_t len)88*5c4a5fe1SAndy Fiddaman mpt_compute_checksum(void *base, size_t len)
89*5c4a5fe1SAndy Fiddaman {
90*5c4a5fe1SAndy Fiddaman 	uint8_t	*bytes;
91*5c4a5fe1SAndy Fiddaman 	uint8_t	sum;
92*5c4a5fe1SAndy Fiddaman 
93*5c4a5fe1SAndy Fiddaman 	for(bytes = base, sum = 0; len > 0; len--) {
94*5c4a5fe1SAndy Fiddaman 		sum += *bytes++;
95*5c4a5fe1SAndy Fiddaman 	}
96*5c4a5fe1SAndy Fiddaman 
97*5c4a5fe1SAndy Fiddaman 	return (256 - sum);
98*5c4a5fe1SAndy Fiddaman }
99*5c4a5fe1SAndy Fiddaman 
100*5c4a5fe1SAndy Fiddaman static void
mpt_build_mpfp(mpfps_t mpfp,vm_paddr_t gpa)101*5c4a5fe1SAndy Fiddaman mpt_build_mpfp(mpfps_t mpfp, vm_paddr_t gpa)
102*5c4a5fe1SAndy Fiddaman {
103*5c4a5fe1SAndy Fiddaman 
104*5c4a5fe1SAndy Fiddaman 	memset(mpfp, 0, sizeof(*mpfp));
105*5c4a5fe1SAndy Fiddaman 	memcpy(mpfp->signature, MPFP_SIG, 4);
106*5c4a5fe1SAndy Fiddaman 	mpfp->pap = gpa + sizeof(*mpfp);
107*5c4a5fe1SAndy Fiddaman 	mpfp->length = 1;
108*5c4a5fe1SAndy Fiddaman 	mpfp->spec_rev = MP_SPECREV;
109*5c4a5fe1SAndy Fiddaman 	mpfp->checksum = mpt_compute_checksum(mpfp, sizeof(*mpfp));
110*5c4a5fe1SAndy Fiddaman }
111*5c4a5fe1SAndy Fiddaman 
112*5c4a5fe1SAndy Fiddaman static void
mpt_build_mpch(mpcth_t mpch)113*5c4a5fe1SAndy Fiddaman mpt_build_mpch(mpcth_t mpch)
114*5c4a5fe1SAndy Fiddaman {
115*5c4a5fe1SAndy Fiddaman 
116*5c4a5fe1SAndy Fiddaman 	memset(mpch, 0, sizeof(*mpch));
117*5c4a5fe1SAndy Fiddaman 	memcpy(mpch->signature, MPCH_SIG, 4);
118*5c4a5fe1SAndy Fiddaman 	mpch->spec_rev = MP_SPECREV;
119*5c4a5fe1SAndy Fiddaman 	memcpy(mpch->oem_id, MPCH_OEMID, MPCH_OEMID_LEN);
120*5c4a5fe1SAndy Fiddaman 	memcpy(mpch->product_id, MPCH_PRODID, MPCH_PRODID_LEN);
121*5c4a5fe1SAndy Fiddaman 	mpch->apic_address = LAPIC_PADDR;
122*5c4a5fe1SAndy Fiddaman }
123*5c4a5fe1SAndy Fiddaman 
124*5c4a5fe1SAndy Fiddaman static void
mpt_build_proc_entries(proc_entry_ptr mpep,int ncpu)125*5c4a5fe1SAndy Fiddaman mpt_build_proc_entries(proc_entry_ptr mpep, int ncpu)
126*5c4a5fe1SAndy Fiddaman {
127*5c4a5fe1SAndy Fiddaman 	int i;
128*5c4a5fe1SAndy Fiddaman 
129*5c4a5fe1SAndy Fiddaman 	for (i = 0; i < ncpu; i++) {
130*5c4a5fe1SAndy Fiddaman 		memset(mpep, 0, sizeof(*mpep));
131*5c4a5fe1SAndy Fiddaman 		mpep->type = MPCT_ENTRY_PROCESSOR;
132*5c4a5fe1SAndy Fiddaman 		mpep->apic_id = i; // XXX
133*5c4a5fe1SAndy Fiddaman 		mpep->apic_version = LAPIC_VERSION;
134*5c4a5fe1SAndy Fiddaman 		mpep->cpu_flags = PROCENTRY_FLAG_EN;
135*5c4a5fe1SAndy Fiddaman 		if (i == 0)
136*5c4a5fe1SAndy Fiddaman 			mpep->cpu_flags |= PROCENTRY_FLAG_BP;
137*5c4a5fe1SAndy Fiddaman 		mpep->cpu_signature = MPEP_SIG;
138*5c4a5fe1SAndy Fiddaman 		mpep->feature_flags = MPEP_FEATURES;
139*5c4a5fe1SAndy Fiddaman 		mpep++;
140*5c4a5fe1SAndy Fiddaman 	}
141*5c4a5fe1SAndy Fiddaman }
142*5c4a5fe1SAndy Fiddaman 
143*5c4a5fe1SAndy Fiddaman static void
mpt_build_localint_entries(int_entry_ptr mpie)144*5c4a5fe1SAndy Fiddaman mpt_build_localint_entries(int_entry_ptr mpie)
145*5c4a5fe1SAndy Fiddaman {
146*5c4a5fe1SAndy Fiddaman 
147*5c4a5fe1SAndy Fiddaman 	/* Hardcode LINT0 as ExtINT on all CPUs. */
148*5c4a5fe1SAndy Fiddaman 	memset(mpie, 0, sizeof(*mpie));
149*5c4a5fe1SAndy Fiddaman 	mpie->type = MPCT_ENTRY_LOCAL_INT;
150*5c4a5fe1SAndy Fiddaman 	mpie->int_type = INTENTRY_TYPE_EXTINT;
151*5c4a5fe1SAndy Fiddaman 	mpie->int_flags = INTENTRY_FLAGS_POLARITY_CONFORM |
152*5c4a5fe1SAndy Fiddaman 	    INTENTRY_FLAGS_TRIGGER_CONFORM;
153*5c4a5fe1SAndy Fiddaman 	mpie->dst_apic_id = 0xff;
154*5c4a5fe1SAndy Fiddaman 	mpie->dst_apic_int = 0;
155*5c4a5fe1SAndy Fiddaman 	mpie++;
156*5c4a5fe1SAndy Fiddaman 
157*5c4a5fe1SAndy Fiddaman 	/* Hardcode LINT1 as NMI on all CPUs. */
158*5c4a5fe1SAndy Fiddaman 	memset(mpie, 0, sizeof(*mpie));
159*5c4a5fe1SAndy Fiddaman 	mpie->type = MPCT_ENTRY_LOCAL_INT;
160*5c4a5fe1SAndy Fiddaman 	mpie->int_type = INTENTRY_TYPE_NMI;
161*5c4a5fe1SAndy Fiddaman 	mpie->int_flags = INTENTRY_FLAGS_POLARITY_CONFORM |
162*5c4a5fe1SAndy Fiddaman 	    INTENTRY_FLAGS_TRIGGER_CONFORM;
163*5c4a5fe1SAndy Fiddaman 	mpie->dst_apic_id = 0xff;
164*5c4a5fe1SAndy Fiddaman 	mpie->dst_apic_int = 1;
165*5c4a5fe1SAndy Fiddaman }
166*5c4a5fe1SAndy Fiddaman 
167*5c4a5fe1SAndy Fiddaman static void
mpt_build_bus_entries(bus_entry_ptr mpeb)168*5c4a5fe1SAndy Fiddaman mpt_build_bus_entries(bus_entry_ptr mpeb)
169*5c4a5fe1SAndy Fiddaman {
170*5c4a5fe1SAndy Fiddaman 
171*5c4a5fe1SAndy Fiddaman 	memset(mpeb, 0, sizeof(*mpeb));
172*5c4a5fe1SAndy Fiddaman 	mpeb->type = MPCT_ENTRY_BUS;
173*5c4a5fe1SAndy Fiddaman 	mpeb->bus_id = 0;
174*5c4a5fe1SAndy Fiddaman 	memcpy(mpeb->bus_type, MPE_BUSNAME_PCI, MPE_BUSNAME_LEN);
175*5c4a5fe1SAndy Fiddaman 	mpeb++;
176*5c4a5fe1SAndy Fiddaman 
177*5c4a5fe1SAndy Fiddaman 	memset(mpeb, 0, sizeof(*mpeb));
178*5c4a5fe1SAndy Fiddaman 	mpeb->type = MPCT_ENTRY_BUS;
179*5c4a5fe1SAndy Fiddaman 	mpeb->bus_id = 1;
180*5c4a5fe1SAndy Fiddaman 	memcpy(mpeb->bus_type, MPE_BUSNAME_ISA, MPE_BUSNAME_LEN);
181*5c4a5fe1SAndy Fiddaman }
182*5c4a5fe1SAndy Fiddaman 
183*5c4a5fe1SAndy Fiddaman static void
mpt_build_ioapic_entries(io_apic_entry_ptr mpei,int id)184*5c4a5fe1SAndy Fiddaman mpt_build_ioapic_entries(io_apic_entry_ptr mpei, int id)
185*5c4a5fe1SAndy Fiddaman {
186*5c4a5fe1SAndy Fiddaman 
187*5c4a5fe1SAndy Fiddaman 	memset(mpei, 0, sizeof(*mpei));
188*5c4a5fe1SAndy Fiddaman 	mpei->type = MPCT_ENTRY_IOAPIC;
189*5c4a5fe1SAndy Fiddaman 	mpei->apic_id = id;
190*5c4a5fe1SAndy Fiddaman 	mpei->apic_version = IOAPIC_VERSION;
191*5c4a5fe1SAndy Fiddaman 	mpei->apic_flags = IOAPICENTRY_FLAG_EN;
192*5c4a5fe1SAndy Fiddaman 	mpei->apic_address = IOAPIC_PADDR;
193*5c4a5fe1SAndy Fiddaman }
194*5c4a5fe1SAndy Fiddaman 
195*5c4a5fe1SAndy Fiddaman static int
mpt_count_ioint_entries(void)196*5c4a5fe1SAndy Fiddaman mpt_count_ioint_entries(void)
197*5c4a5fe1SAndy Fiddaman {
198*5c4a5fe1SAndy Fiddaman 	int bus, count;
199*5c4a5fe1SAndy Fiddaman 
200*5c4a5fe1SAndy Fiddaman 	count = 0;
201*5c4a5fe1SAndy Fiddaman 	for (bus = 0; bus <= PCI_BUSMAX; bus++)
202*5c4a5fe1SAndy Fiddaman 		count += pci_count_lintr(bus);
203*5c4a5fe1SAndy Fiddaman 
204*5c4a5fe1SAndy Fiddaman 	/*
205*5c4a5fe1SAndy Fiddaman 	 * Always include entries for the first 16 pins along with a entry
206*5c4a5fe1SAndy Fiddaman 	 * for each active PCI INTx pin.
207*5c4a5fe1SAndy Fiddaman 	 */
208*5c4a5fe1SAndy Fiddaman 	return (16 + count);
209*5c4a5fe1SAndy Fiddaman }
210*5c4a5fe1SAndy Fiddaman 
211*5c4a5fe1SAndy Fiddaman static void
mpt_generate_pci_int(int bus,int slot,int pin,int pirq_pin __unused,int ioapic_irq,void * arg)212*5c4a5fe1SAndy Fiddaman mpt_generate_pci_int(int bus, int slot, int pin, int pirq_pin __unused,
213*5c4a5fe1SAndy Fiddaman     int ioapic_irq, void *arg)
214*5c4a5fe1SAndy Fiddaman {
215*5c4a5fe1SAndy Fiddaman 	int_entry_ptr *mpiep, mpie;
216*5c4a5fe1SAndy Fiddaman 
217*5c4a5fe1SAndy Fiddaman 	mpiep = arg;
218*5c4a5fe1SAndy Fiddaman 	mpie = *mpiep;
219*5c4a5fe1SAndy Fiddaman 	memset(mpie, 0, sizeof(*mpie));
220*5c4a5fe1SAndy Fiddaman 
221*5c4a5fe1SAndy Fiddaman 	/*
222*5c4a5fe1SAndy Fiddaman 	 * This is always after another I/O interrupt entry, so cheat
223*5c4a5fe1SAndy Fiddaman 	 * and fetch the I/O APIC ID from the prior entry.
224*5c4a5fe1SAndy Fiddaman 	 */
225*5c4a5fe1SAndy Fiddaman 	mpie->type = MPCT_ENTRY_INT;
226*5c4a5fe1SAndy Fiddaman 	mpie->int_type = INTENTRY_TYPE_INT;
227*5c4a5fe1SAndy Fiddaman 	mpie->src_bus_id = bus;
228*5c4a5fe1SAndy Fiddaman 	mpie->src_bus_irq = slot << 2 | (pin - 1);
229*5c4a5fe1SAndy Fiddaman 	mpie->dst_apic_id = mpie[-1].dst_apic_id;
230*5c4a5fe1SAndy Fiddaman 	mpie->dst_apic_int = ioapic_irq;
231*5c4a5fe1SAndy Fiddaman 
232*5c4a5fe1SAndy Fiddaman 	*mpiep = mpie + 1;
233*5c4a5fe1SAndy Fiddaman }
234*5c4a5fe1SAndy Fiddaman 
235*5c4a5fe1SAndy Fiddaman static void
mpt_build_ioint_entries(int_entry_ptr mpie,int id)236*5c4a5fe1SAndy Fiddaman mpt_build_ioint_entries(int_entry_ptr mpie, int id)
237*5c4a5fe1SAndy Fiddaman {
238*5c4a5fe1SAndy Fiddaman 	int pin, bus;
239*5c4a5fe1SAndy Fiddaman 
240*5c4a5fe1SAndy Fiddaman 	/*
241*5c4a5fe1SAndy Fiddaman 	 * The following config is taken from kernel mptable.c
242*5c4a5fe1SAndy Fiddaman 	 * mptable_parse_default_config_ints(...), for now
243*5c4a5fe1SAndy Fiddaman 	 * just use the default config, tweek later if needed.
244*5c4a5fe1SAndy Fiddaman 	 */
245*5c4a5fe1SAndy Fiddaman 
246*5c4a5fe1SAndy Fiddaman 	/* First, generate the first 16 pins. */
247*5c4a5fe1SAndy Fiddaman 	for (pin = 0; pin < 16; pin++) {
248*5c4a5fe1SAndy Fiddaman 		memset(mpie, 0, sizeof(*mpie));
249*5c4a5fe1SAndy Fiddaman 		mpie->type = MPCT_ENTRY_INT;
250*5c4a5fe1SAndy Fiddaman 		mpie->src_bus_id = 1;
251*5c4a5fe1SAndy Fiddaman 		mpie->dst_apic_id = id;
252*5c4a5fe1SAndy Fiddaman 
253*5c4a5fe1SAndy Fiddaman 		/*
254*5c4a5fe1SAndy Fiddaman 		 * All default configs route IRQs from bus 0 to the first 16
255*5c4a5fe1SAndy Fiddaman 		 * pins of the first I/O APIC with an APIC ID of 2.
256*5c4a5fe1SAndy Fiddaman 		 */
257*5c4a5fe1SAndy Fiddaman 		mpie->dst_apic_int = pin;
258*5c4a5fe1SAndy Fiddaman 		switch (pin) {
259*5c4a5fe1SAndy Fiddaman 		case 0:
260*5c4a5fe1SAndy Fiddaman 			/* Pin 0 is an ExtINT pin. */
261*5c4a5fe1SAndy Fiddaman 			mpie->int_type = INTENTRY_TYPE_EXTINT;
262*5c4a5fe1SAndy Fiddaman 			break;
263*5c4a5fe1SAndy Fiddaman 		case 2:
264*5c4a5fe1SAndy Fiddaman 			/* IRQ 0 is routed to pin 2. */
265*5c4a5fe1SAndy Fiddaman 			mpie->int_type = INTENTRY_TYPE_INT;
266*5c4a5fe1SAndy Fiddaman 			mpie->src_bus_irq = 0;
267*5c4a5fe1SAndy Fiddaman 			break;
268*5c4a5fe1SAndy Fiddaman 		case SCI_INT:
269*5c4a5fe1SAndy Fiddaman 			/* ACPI SCI is level triggered and active-lo. */
270*5c4a5fe1SAndy Fiddaman 			mpie->int_flags = INTENTRY_FLAGS_POLARITY_ACTIVELO |
271*5c4a5fe1SAndy Fiddaman 			    INTENTRY_FLAGS_TRIGGER_LEVEL;
272*5c4a5fe1SAndy Fiddaman 			mpie->int_type = INTENTRY_TYPE_INT;
273*5c4a5fe1SAndy Fiddaman 			mpie->src_bus_irq = SCI_INT;
274*5c4a5fe1SAndy Fiddaman 			break;
275*5c4a5fe1SAndy Fiddaman 		default:
276*5c4a5fe1SAndy Fiddaman 			/* All other pins are identity mapped. */
277*5c4a5fe1SAndy Fiddaman 			mpie->int_type = INTENTRY_TYPE_INT;
278*5c4a5fe1SAndy Fiddaman 			mpie->src_bus_irq = pin;
279*5c4a5fe1SAndy Fiddaman 			break;
280*5c4a5fe1SAndy Fiddaman 		}
281*5c4a5fe1SAndy Fiddaman 		mpie++;
282*5c4a5fe1SAndy Fiddaman 	}
283*5c4a5fe1SAndy Fiddaman 
284*5c4a5fe1SAndy Fiddaman 	/* Next, generate entries for any PCI INTx interrupts. */
285*5c4a5fe1SAndy Fiddaman 	for (bus = 0; bus <= PCI_BUSMAX; bus++)
286*5c4a5fe1SAndy Fiddaman 		pci_walk_lintr(bus, mpt_generate_pci_int, &mpie);
287*5c4a5fe1SAndy Fiddaman }
288*5c4a5fe1SAndy Fiddaman 
289*5c4a5fe1SAndy Fiddaman void
mptable_add_oemtbl(void * tbl,int tblsz)290*5c4a5fe1SAndy Fiddaman mptable_add_oemtbl(void *tbl, int tblsz)
291*5c4a5fe1SAndy Fiddaman {
292*5c4a5fe1SAndy Fiddaman 
293*5c4a5fe1SAndy Fiddaman 	oem_tbl_start = tbl;
294*5c4a5fe1SAndy Fiddaman 	oem_tbl_size = tblsz;
295*5c4a5fe1SAndy Fiddaman }
296*5c4a5fe1SAndy Fiddaman 
297*5c4a5fe1SAndy Fiddaman int
mptable_build(struct vmctx * ctx,int ncpu)298*5c4a5fe1SAndy Fiddaman mptable_build(struct vmctx *ctx, int ncpu)
299*5c4a5fe1SAndy Fiddaman {
300*5c4a5fe1SAndy Fiddaman 	mpcth_t			mpch;
301*5c4a5fe1SAndy Fiddaman 	bus_entry_ptr		mpeb;
302*5c4a5fe1SAndy Fiddaman 	io_apic_entry_ptr	mpei;
303*5c4a5fe1SAndy Fiddaman 	proc_entry_ptr		mpep;
304*5c4a5fe1SAndy Fiddaman 	mpfps_t			mpfp;
305*5c4a5fe1SAndy Fiddaman 	int_entry_ptr		mpie;
306*5c4a5fe1SAndy Fiddaman 	int			ioints, bus;
307*5c4a5fe1SAndy Fiddaman 	char 			*curraddr;
308*5c4a5fe1SAndy Fiddaman 	char 			*startaddr;
309*5c4a5fe1SAndy Fiddaman 
310*5c4a5fe1SAndy Fiddaman 	startaddr = paddr_guest2host(ctx, MPTABLE_BASE, MPTABLE_MAX_LENGTH);
311*5c4a5fe1SAndy Fiddaman 	if (startaddr == NULL) {
312*5c4a5fe1SAndy Fiddaman 		EPRINTLN("mptable requires mapped mem");
313*5c4a5fe1SAndy Fiddaman 		return (ENOMEM);
314*5c4a5fe1SAndy Fiddaman 	}
315*5c4a5fe1SAndy Fiddaman 
316*5c4a5fe1SAndy Fiddaman 	/*
317*5c4a5fe1SAndy Fiddaman 	 * There is no way to advertise multiple PCI hierarchies via MPtable
318*5c4a5fe1SAndy Fiddaman 	 * so require that there is no PCI hierarchy with a non-zero bus
319*5c4a5fe1SAndy Fiddaman 	 * number.
320*5c4a5fe1SAndy Fiddaman 	 */
321*5c4a5fe1SAndy Fiddaman 	for (bus = 1; bus <= PCI_BUSMAX; bus++) {
322*5c4a5fe1SAndy Fiddaman 		if (pci_bus_configured(bus)) {
323*5c4a5fe1SAndy Fiddaman 			EPRINTLN("MPtable is incompatible with "
324*5c4a5fe1SAndy Fiddaman 			    "multiple PCI hierarchies.");
325*5c4a5fe1SAndy Fiddaman 			EPRINTLN("MPtable generation can be disabled "
326*5c4a5fe1SAndy Fiddaman 			    "by passing the -Y option to bhyve(8).");
327*5c4a5fe1SAndy Fiddaman 			return (EINVAL);
328*5c4a5fe1SAndy Fiddaman 		}
329*5c4a5fe1SAndy Fiddaman 	}
330*5c4a5fe1SAndy Fiddaman 
331*5c4a5fe1SAndy Fiddaman 	curraddr = startaddr;
332*5c4a5fe1SAndy Fiddaman 	mpfp = (mpfps_t)curraddr;
333*5c4a5fe1SAndy Fiddaman 	mpt_build_mpfp(mpfp, MPTABLE_BASE);
334*5c4a5fe1SAndy Fiddaman 	curraddr += sizeof(*mpfp);
335*5c4a5fe1SAndy Fiddaman 
336*5c4a5fe1SAndy Fiddaman 	mpch = (mpcth_t)curraddr;
337*5c4a5fe1SAndy Fiddaman 	mpt_build_mpch(mpch);
338*5c4a5fe1SAndy Fiddaman 	curraddr += sizeof(*mpch);
339*5c4a5fe1SAndy Fiddaman 
340*5c4a5fe1SAndy Fiddaman 	mpep = (proc_entry_ptr)curraddr;
341*5c4a5fe1SAndy Fiddaman 	mpt_build_proc_entries(mpep, ncpu);
342*5c4a5fe1SAndy Fiddaman 	curraddr += sizeof(*mpep) * ncpu;
343*5c4a5fe1SAndy Fiddaman 	mpch->entry_count += ncpu;
344*5c4a5fe1SAndy Fiddaman 
345*5c4a5fe1SAndy Fiddaman 	mpeb = (bus_entry_ptr) curraddr;
346*5c4a5fe1SAndy Fiddaman 	mpt_build_bus_entries(mpeb);
347*5c4a5fe1SAndy Fiddaman 	curraddr += sizeof(*mpeb) * MPE_NUM_BUSES;
348*5c4a5fe1SAndy Fiddaman 	mpch->entry_count += MPE_NUM_BUSES;
349*5c4a5fe1SAndy Fiddaman 
350*5c4a5fe1SAndy Fiddaman 	mpei = (io_apic_entry_ptr)curraddr;
351*5c4a5fe1SAndy Fiddaman 	mpt_build_ioapic_entries(mpei, 0);
352*5c4a5fe1SAndy Fiddaman 	curraddr += sizeof(*mpei);
353*5c4a5fe1SAndy Fiddaman 	mpch->entry_count++;
354*5c4a5fe1SAndy Fiddaman 
355*5c4a5fe1SAndy Fiddaman 	mpie = (int_entry_ptr) curraddr;
356*5c4a5fe1SAndy Fiddaman 	ioints = mpt_count_ioint_entries();
357*5c4a5fe1SAndy Fiddaman 	mpt_build_ioint_entries(mpie, 0);
358*5c4a5fe1SAndy Fiddaman 	curraddr += sizeof(*mpie) * ioints;
359*5c4a5fe1SAndy Fiddaman 	mpch->entry_count += ioints;
360*5c4a5fe1SAndy Fiddaman 
361*5c4a5fe1SAndy Fiddaman 	mpie = (int_entry_ptr)curraddr;
362*5c4a5fe1SAndy Fiddaman 	mpt_build_localint_entries(mpie);
363*5c4a5fe1SAndy Fiddaman 	curraddr += sizeof(*mpie) * MPEII_NUM_LOCAL_IRQ;
364*5c4a5fe1SAndy Fiddaman 	mpch->entry_count += MPEII_NUM_LOCAL_IRQ;
365*5c4a5fe1SAndy Fiddaman 
366*5c4a5fe1SAndy Fiddaman 	if (oem_tbl_start) {
367*5c4a5fe1SAndy Fiddaman 		mpch->oem_table_pointer = curraddr - startaddr + MPTABLE_BASE;
368*5c4a5fe1SAndy Fiddaman 		mpch->oem_table_size = oem_tbl_size;
369*5c4a5fe1SAndy Fiddaman 		memcpy(curraddr, oem_tbl_start, oem_tbl_size);
370*5c4a5fe1SAndy Fiddaman 	}
371*5c4a5fe1SAndy Fiddaman 
372*5c4a5fe1SAndy Fiddaman 	mpch->base_table_length = curraddr - (char *)mpch;
373*5c4a5fe1SAndy Fiddaman 	mpch->checksum = mpt_compute_checksum(mpch, mpch->base_table_length);
374*5c4a5fe1SAndy Fiddaman 
375*5c4a5fe1SAndy Fiddaman 	return (0);
376*5c4a5fe1SAndy Fiddaman }
377