mptbl.c revision 267393
1221828Sgrehan/*-
2242131Sgrehan * Copyright (c) 2012 NetApp, Inc.
3221828Sgrehan * All rights reserved.
4221828Sgrehan *
5221828Sgrehan * Redistribution and use in source and binary forms, with or without
6221828Sgrehan * modification, are permitted provided that the following conditions
7221828Sgrehan * are met:
8221828Sgrehan * 1. Redistributions of source code must retain the above copyright
9221828Sgrehan *    notice, this list of conditions and the following disclaimer.
10221828Sgrehan * 2. Redistributions in binary form must reproduce the above copyright
11221828Sgrehan *    notice, this list of conditions and the following disclaimer in the
12221828Sgrehan *    documentation and/or other materials provided with the distribution.
13221828Sgrehan *
14221828Sgrehan * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15221828Sgrehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16221828Sgrehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17221828Sgrehan * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18221828Sgrehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19221828Sgrehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20221828Sgrehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21221828Sgrehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22221828Sgrehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23221828Sgrehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24221828Sgrehan * SUCH DAMAGE.
25221828Sgrehan *
26221828Sgrehan * $FreeBSD: stable/10/usr.sbin/bhyve/mptbl.c 267393 2014-06-12 13:13:15Z jhb $
27221828Sgrehan */
28221828Sgrehan
29221828Sgrehan#include <sys/cdefs.h>
30221828Sgrehan__FBSDID("$FreeBSD: stable/10/usr.sbin/bhyve/mptbl.c 267393 2014-06-12 13:13:15Z jhb $");
31221828Sgrehan
32221828Sgrehan#include <sys/types.h>
33242131Sgrehan#include <sys/errno.h>
34242131Sgrehan#include <x86/mptable.h>
35221828Sgrehan
36221828Sgrehan#include <stdio.h>
37221828Sgrehan#include <string.h>
38221828Sgrehan
39261090Sjhb#include "acpi.h"
40244167Sgrehan#include "bhyverun.h"
41242131Sgrehan#include "mptbl.h"
42267393Sjhb#include "pci_emul.h"
43221828Sgrehan
44242131Sgrehan#define MPTABLE_BASE		0xF0000
45221828Sgrehan
46247523Sneel/* floating pointer length + maximum length of configuration table */
47247523Sneel#define	MPTABLE_MAX_LENGTH	(65536 + 16)
48247523Sneel
49242131Sgrehan#define LAPIC_PADDR		0xFEE00000
50242131Sgrehan#define LAPIC_VERSION 		16
51221828Sgrehan
52242131Sgrehan#define IOAPIC_PADDR		0xFEC00000
53242131Sgrehan#define IOAPIC_VERSION		0x11
54221828Sgrehan
55242131Sgrehan#define MP_SPECREV		4
56242131Sgrehan#define MPFP_SIG		"_MP_"
57242131Sgrehan
58242131Sgrehan/* Configuration header defines */
59242131Sgrehan#define MPCH_SIG		"PCMP"
60242131Sgrehan#define MPCH_OEMID		"BHyVe   "
61242131Sgrehan#define MPCH_OEMID_LEN          8
62242131Sgrehan#define MPCH_PRODID             "Hypervisor  "
63242131Sgrehan#define MPCH_PRODID_LEN         12
64242131Sgrehan
65242131Sgrehan/* Processor entry defines */
66242131Sgrehan#define MPEP_SIG_FAMILY		6	/* XXX bhyve should supply this */
67242131Sgrehan#define MPEP_SIG_MODEL		26
68242131Sgrehan#define MPEP_SIG_STEPPING	5
69242131Sgrehan#define MPEP_SIG		\
70242131Sgrehan	((MPEP_SIG_FAMILY << 8) | \
71242131Sgrehan	 (MPEP_SIG_MODEL << 4)	| \
72242131Sgrehan	 (MPEP_SIG_STEPPING))
73242131Sgrehan
74242131Sgrehan#define MPEP_FEATURES           (0xBFEBFBFF) /* XXX Intel i7 */
75242131Sgrehan
76262350Sjhb/* Number of local intr entries */
77262350Sjhb#define	MPEII_NUM_LOCAL_IRQ	2
78262350Sjhb
79242131Sgrehan/* Bus entry defines */
80242131Sgrehan#define MPE_NUM_BUSES		2
81242131Sgrehan#define MPE_BUSNAME_LEN		6
82242131Sgrehan#define MPE_BUSNAME_ISA		"ISA   "
83242131Sgrehan#define MPE_BUSNAME_PCI		"PCI   "
84242131Sgrehan
85242131Sgrehanstatic void *oem_tbl_start;
86242131Sgrehanstatic int oem_tbl_size;
87242131Sgrehan
88221828Sgrehanstatic uint8_t
89242131Sgrehanmpt_compute_checksum(void *base, size_t len)
90221828Sgrehan{
91242131Sgrehan	uint8_t	*bytes;
92242131Sgrehan	uint8_t	sum;
93242131Sgrehan
94242131Sgrehan	for(bytes = base, sum = 0; len > 0; len--) {
95221828Sgrehan		sum += *bytes++;
96221828Sgrehan	}
97242131Sgrehan
98242131Sgrehan	return (256 - sum);
99221828Sgrehan}
100221828Sgrehan
101221828Sgrehanstatic void
102242131Sgrehanmpt_build_mpfp(mpfps_t mpfp, vm_paddr_t gpa)
103221828Sgrehan{
104242131Sgrehan
105221828Sgrehan	memset(mpfp, 0, sizeof(*mpfp));
106242131Sgrehan	memcpy(mpfp->signature, MPFP_SIG, 4);
107242131Sgrehan	mpfp->pap = gpa + sizeof(*mpfp);
108242131Sgrehan	mpfp->length = 1;
109242131Sgrehan	mpfp->spec_rev = MP_SPECREV;
110242131Sgrehan	mpfp->checksum = mpt_compute_checksum(mpfp, sizeof(*mpfp));
111221828Sgrehan}
112221828Sgrehan
113221828Sgrehanstatic void
114242131Sgrehanmpt_build_mpch(mpcth_t mpch)
115221828Sgrehan{
116242131Sgrehan
117221828Sgrehan	memset(mpch, 0, sizeof(*mpch));
118242131Sgrehan	memcpy(mpch->signature, MPCH_SIG, 4);
119242131Sgrehan	mpch->spec_rev = MP_SPECREV;
120242131Sgrehan	memcpy(mpch->oem_id, MPCH_OEMID, MPCH_OEMID_LEN);
121242131Sgrehan	memcpy(mpch->product_id, MPCH_PRODID, MPCH_PRODID_LEN);
122242131Sgrehan	mpch->apic_address = LAPIC_PADDR;
123221828Sgrehan}
124221828Sgrehan
125221828Sgrehanstatic void
126259837Sjhbmpt_build_proc_entries(proc_entry_ptr mpep, int ncpu)
127221828Sgrehan{
128221828Sgrehan	int i;
129221828Sgrehan
130242131Sgrehan	for (i = 0; i < ncpu; i++) {
131221828Sgrehan		memset(mpep, 0, sizeof(*mpep));
132242131Sgrehan		mpep->type = MPCT_ENTRY_PROCESSOR;
133242131Sgrehan		mpep->apic_id = i; // XXX
134242131Sgrehan		mpep->apic_version = LAPIC_VERSION;
135242131Sgrehan		mpep->cpu_flags = PROCENTRY_FLAG_EN;
136242131Sgrehan		if (i == 0)
137242131Sgrehan			mpep->cpu_flags |= PROCENTRY_FLAG_BP;
138242131Sgrehan		mpep->cpu_signature = MPEP_SIG;
139221828Sgrehan		mpep->feature_flags = MPEP_FEATURES;
140221828Sgrehan		mpep++;
141221828Sgrehan	}
142221828Sgrehan}
143221828Sgrehan
144221828Sgrehanstatic void
145262350Sjhbmpt_build_localint_entries(int_entry_ptr mpie)
146262350Sjhb{
147262350Sjhb
148262350Sjhb	/* Hardcode LINT0 as ExtINT on all CPUs. */
149262350Sjhb	memset(mpie, 0, sizeof(*mpie));
150262350Sjhb	mpie->type = MPCT_ENTRY_LOCAL_INT;
151262350Sjhb	mpie->int_type = INTENTRY_TYPE_EXTINT;
152262350Sjhb	mpie->int_flags = INTENTRY_FLAGS_POLARITY_CONFORM |
153262350Sjhb	    INTENTRY_FLAGS_TRIGGER_CONFORM;
154262350Sjhb	mpie->dst_apic_id = 0xff;
155262350Sjhb	mpie->dst_apic_int = 0;
156262350Sjhb	mpie++;
157262350Sjhb
158262350Sjhb	/* Hardcode LINT1 as NMI on all CPUs. */
159262350Sjhb	memset(mpie, 0, sizeof(*mpie));
160262350Sjhb	mpie->type = MPCT_ENTRY_LOCAL_INT;
161262350Sjhb	mpie->int_type = INTENTRY_TYPE_NMI;
162262350Sjhb	mpie->int_flags = INTENTRY_FLAGS_POLARITY_CONFORM |
163262350Sjhb	    INTENTRY_FLAGS_TRIGGER_CONFORM;
164262350Sjhb	mpie->dst_apic_id = 0xff;
165262350Sjhb	mpie->dst_apic_int = 1;
166262350Sjhb}
167262350Sjhb
168262350Sjhbstatic void
169242131Sgrehanmpt_build_bus_entries(bus_entry_ptr mpeb)
170221828Sgrehan{
171242131Sgrehan
172221828Sgrehan	memset(mpeb, 0, sizeof(*mpeb));
173242131Sgrehan	mpeb->type = MPCT_ENTRY_BUS;
174256755Sgrehan	mpeb->bus_id = 0;
175256755Sgrehan	memcpy(mpeb->bus_type, MPE_BUSNAME_PCI, MPE_BUSNAME_LEN);
176221828Sgrehan	mpeb++;
177221828Sgrehan
178221828Sgrehan	memset(mpeb, 0, sizeof(*mpeb));
179242131Sgrehan	mpeb->type = MPCT_ENTRY_BUS;
180256755Sgrehan	mpeb->bus_id = 1;
181256755Sgrehan	memcpy(mpeb->bus_type, MPE_BUSNAME_ISA, MPE_BUSNAME_LEN);
182221828Sgrehan}
183221828Sgrehan
184221828Sgrehanstatic void
185242131Sgrehanmpt_build_ioapic_entries(io_apic_entry_ptr mpei, int id)
186221828Sgrehan{
187242131Sgrehan
188221828Sgrehan	memset(mpei, 0, sizeof(*mpei));
189242131Sgrehan	mpei->type = MPCT_ENTRY_IOAPIC;
190242131Sgrehan	mpei->apic_id = id;
191242131Sgrehan	mpei->apic_version = IOAPIC_VERSION;
192242131Sgrehan	mpei->apic_flags = IOAPICENTRY_FLAG_EN;
193242131Sgrehan	mpei->apic_address = IOAPIC_PADDR;
194221828Sgrehan}
195221828Sgrehan
196267393Sjhbstatic int
197267393Sjhbmpt_count_ioint_entries(void)
198267393Sjhb{
199267393Sjhb
200267393Sjhb	/*
201267393Sjhb	 * Always include entries for the first 16 pins along with a entry
202267393Sjhb	 * for each active PCI INTx pin.
203267393Sjhb	 */
204267393Sjhb	return (16 + pci_count_lintr());
205267393Sjhb}
206267393Sjhb
207221828Sgrehanstatic void
208267393Sjhbmpt_generate_pci_int(int slot, int pin, int ioapic_irq, void *arg)
209221828Sgrehan{
210267393Sjhb	int_entry_ptr *mpiep, mpie;
211267393Sjhb
212267393Sjhb	mpiep = arg;
213267393Sjhb	mpie = *mpiep;
214267393Sjhb	memset(mpie, 0, sizeof(*mpie));
215267393Sjhb
216267393Sjhb	/*
217267393Sjhb	 * This is always after another I/O interrupt entry, so cheat
218267393Sjhb	 * and fetch the I/O APIC ID from the prior entry.
219267393Sjhb	 */
220267393Sjhb	mpie->type = MPCT_ENTRY_INT;
221267393Sjhb	mpie->int_type = INTENTRY_TYPE_INT;
222267393Sjhb	mpie->src_bus_id = 0;
223267393Sjhb	mpie->src_bus_irq = slot << 2 | (pin - 1);
224267393Sjhb	mpie->dst_apic_id = mpie[-1].dst_apic_id;
225267393Sjhb	mpie->dst_apic_int = ioapic_irq;
226267393Sjhb
227267393Sjhb	*mpiep = mpie + 1;
228267393Sjhb}
229267393Sjhb
230267393Sjhbstatic void
231267393Sjhbmpt_build_ioint_entries(int_entry_ptr mpie, int id)
232267393Sjhb{
233221828Sgrehan	int pin;
234221828Sgrehan
235221828Sgrehan	/*
236221828Sgrehan	 * The following config is taken from kernel mptable.c
237221828Sgrehan	 * mptable_parse_default_config_ints(...), for now
238221828Sgrehan	 * just use the default config, tweek later if needed.
239221828Sgrehan	 */
240221828Sgrehan
241267393Sjhb	/* First, generate the first 16 pins. */
242267393Sjhb	for (pin = 0; pin < 16; pin++) {
243256755Sgrehan		memset(mpie, 0, sizeof(*mpie));
244256755Sgrehan		mpie->type = MPCT_ENTRY_INT;
245256755Sgrehan		mpie->src_bus_id = 1;
246256755Sgrehan		mpie->dst_apic_id = id;
247221828Sgrehan
248221828Sgrehan		/*
249242131Sgrehan		 * All default configs route IRQs from bus 0 to the first 16
250242131Sgrehan		 * pins of the first I/O APIC with an APIC ID of 2.
251221828Sgrehan		 */
252256755Sgrehan		mpie->dst_apic_int = pin;
253221828Sgrehan		switch (pin) {
254221828Sgrehan		case 0:
255221828Sgrehan			/* Pin 0 is an ExtINT pin. */
256256755Sgrehan			mpie->int_type = INTENTRY_TYPE_EXTINT;
257221828Sgrehan			break;
258221828Sgrehan		case 2:
259221828Sgrehan			/* IRQ 0 is routed to pin 2. */
260256755Sgrehan			mpie->int_type = INTENTRY_TYPE_INT;
261256755Sgrehan			mpie->src_bus_irq = 0;
262221828Sgrehan			break;
263261090Sjhb		case SCI_INT:
264261090Sjhb			/* ACPI SCI is level triggered and active-lo. */
265261090Sjhb			mpie->int_flags = INTENTRY_FLAGS_POLARITY_ACTIVELO |
266261090Sjhb			    INTENTRY_FLAGS_TRIGGER_LEVEL;
267261090Sjhb			mpie->int_type = INTENTRY_TYPE_INT;
268261090Sjhb			mpie->src_bus_irq = SCI_INT;
269261090Sjhb			break;
270221828Sgrehan		default:
271221828Sgrehan			/* All other pins are identity mapped. */
272256755Sgrehan			mpie->int_type = INTENTRY_TYPE_INT;
273256755Sgrehan			mpie->src_bus_irq = pin;
274221828Sgrehan			break;
275221828Sgrehan		}
276256755Sgrehan		mpie++;
277221828Sgrehan	}
278221828Sgrehan
279267393Sjhb	/* Next, generate entries for any PCI INTx interrupts. */
280267393Sjhb	pci_walk_lintr(mpt_generate_pci_int, &mpie);
281221828Sgrehan}
282221828Sgrehan
283242131Sgrehanvoid
284242131Sgrehanmptable_add_oemtbl(void *tbl, int tblsz)
285242131Sgrehan{
286242131Sgrehan
287242131Sgrehan	oem_tbl_start = tbl;
288242131Sgrehan	oem_tbl_size = tblsz;
289242131Sgrehan}
290242131Sgrehan
291221828Sgrehanint
292259301Sgrehanmptable_build(struct vmctx *ctx, int ncpu)
293221828Sgrehan{
294242131Sgrehan	mpcth_t			mpch;
295242131Sgrehan	bus_entry_ptr		mpeb;
296242131Sgrehan	io_apic_entry_ptr	mpei;
297259837Sjhb	proc_entry_ptr		mpep;
298242131Sgrehan	mpfps_t			mpfp;
299256755Sgrehan	int_entry_ptr		mpie;
300267393Sjhb	int			ioints;
301242131Sgrehan	char 			*curraddr;
302221828Sgrehan	char 			*startaddr;
303221828Sgrehan
304248477Sneel	startaddr = paddr_guest2host(ctx, MPTABLE_BASE, MPTABLE_MAX_LENGTH);
305247523Sneel	if (startaddr == NULL) {
306242131Sgrehan		printf("mptable requires mapped mem\n");
307242131Sgrehan		return (ENOMEM);
308221828Sgrehan	}
309221828Sgrehan
310247523Sneel	curraddr = startaddr;
311242131Sgrehan	mpfp = (mpfps_t)curraddr;
312242131Sgrehan	mpt_build_mpfp(mpfp, MPTABLE_BASE);
313242131Sgrehan	curraddr += sizeof(*mpfp);
314221828Sgrehan
315242131Sgrehan	mpch = (mpcth_t)curraddr;
316242131Sgrehan	mpt_build_mpch(mpch);
317242131Sgrehan	curraddr += sizeof(*mpch);
318221828Sgrehan
319259837Sjhb	mpep = (proc_entry_ptr)curraddr;
320242131Sgrehan	mpt_build_proc_entries(mpep, ncpu);
321242131Sgrehan	curraddr += sizeof(*mpep) * ncpu;
322242131Sgrehan	mpch->entry_count += ncpu;
323221828Sgrehan
324242131Sgrehan	mpeb = (bus_entry_ptr) curraddr;
325242131Sgrehan	mpt_build_bus_entries(mpeb);
326242131Sgrehan	curraddr += sizeof(*mpeb) * MPE_NUM_BUSES;
327242131Sgrehan	mpch->entry_count += MPE_NUM_BUSES;
328242131Sgrehan
329259301Sgrehan	mpei = (io_apic_entry_ptr)curraddr;
330259301Sgrehan	mpt_build_ioapic_entries(mpei, 0);
331259301Sgrehan	curraddr += sizeof(*mpei);
332259301Sgrehan	mpch->entry_count++;
333239042Sneel
334256755Sgrehan	mpie = (int_entry_ptr) curraddr;
335267393Sjhb	ioints = mpt_count_ioint_entries();
336267393Sjhb	mpt_build_ioint_entries(mpie, 0);
337267393Sjhb	curraddr += sizeof(*mpie) * ioints;
338267393Sjhb	mpch->entry_count += ioints;
339221828Sgrehan
340262350Sjhb	mpie = (int_entry_ptr)curraddr;
341262350Sjhb	mpt_build_localint_entries(mpie);
342262350Sjhb	curraddr += sizeof(*mpie) * MPEII_NUM_LOCAL_IRQ;
343262350Sjhb	mpch->entry_count += MPEII_NUM_LOCAL_IRQ;
344262350Sjhb
345242131Sgrehan	if (oem_tbl_start) {
346242131Sgrehan		mpch->oem_table_pointer = curraddr - startaddr + MPTABLE_BASE;
347242131Sgrehan		mpch->oem_table_size = oem_tbl_size;
348242131Sgrehan		memcpy(curraddr, oem_tbl_start, oem_tbl_size);
349221828Sgrehan	}
350221828Sgrehan
351242131Sgrehan	mpch->base_table_length = curraddr - (char *)mpch;
352249173Sgrehan	mpch->checksum = mpt_compute_checksum(mpch, mpch->base_table_length);
353221828Sgrehan
354242131Sgrehan	return (0);
355221828Sgrehan}
356