mptbl.c revision 244167
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$
27221828Sgrehan */
28221828Sgrehan
29221828Sgrehan#include <sys/cdefs.h>
30221828Sgrehan__FBSDID("$FreeBSD$");
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
39244167Sgrehan#include "bhyverun.h"
40242131Sgrehan#include "mptbl.h"
41221828Sgrehan
42242131Sgrehan#define MPTABLE_BASE		0xF0000
43221828Sgrehan
44242131Sgrehan#define LAPIC_PADDR		0xFEE00000
45242131Sgrehan#define LAPIC_VERSION 		16
46221828Sgrehan
47242131Sgrehan#define IOAPIC_PADDR		0xFEC00000
48242131Sgrehan#define IOAPIC_VERSION		0x11
49221828Sgrehan
50242131Sgrehan#define MP_SPECREV		4
51242131Sgrehan#define MPFP_SIG		"_MP_"
52242131Sgrehan
53242131Sgrehan/* Configuration header defines */
54242131Sgrehan#define MPCH_SIG		"PCMP"
55242131Sgrehan#define MPCH_OEMID		"BHyVe   "
56242131Sgrehan#define MPCH_OEMID_LEN          8
57242131Sgrehan#define MPCH_PRODID             "Hypervisor  "
58242131Sgrehan#define MPCH_PRODID_LEN         12
59242131Sgrehan
60242131Sgrehan/* Processor entry defines */
61242131Sgrehan#define MPEP_SIG_FAMILY		6	/* XXX bhyve should supply this */
62242131Sgrehan#define MPEP_SIG_MODEL		26
63242131Sgrehan#define MPEP_SIG_STEPPING	5
64242131Sgrehan#define MPEP_SIG		\
65242131Sgrehan	((MPEP_SIG_FAMILY << 8) | \
66242131Sgrehan	 (MPEP_SIG_MODEL << 4)	| \
67242131Sgrehan	 (MPEP_SIG_STEPPING))
68242131Sgrehan
69242131Sgrehan#define MPEP_FEATURES           (0xBFEBFBFF) /* XXX Intel i7 */
70242131Sgrehan
71242131Sgrehan/* Define processor entry struct since <x86/mptable.h> gets it wrong */
72242131Sgrehantypedef struct BPROCENTRY {
73242131Sgrehan	u_char		type;
74242131Sgrehan	u_char		apic_id;
75242131Sgrehan	u_char		apic_version;
76242131Sgrehan	u_char		cpu_flags;
77242131Sgrehan	uint32_t	cpu_signature;
78242131Sgrehan	uint32_t	feature_flags;
79242131Sgrehan	uint32_t	reserved1;
80242131Sgrehan	uint32_t	reserved2;
81242131Sgrehan}      *bproc_entry_ptr;
82242131SgrehanCTASSERT(sizeof(struct BPROCENTRY) == 20);
83242131Sgrehan
84242131Sgrehan/* Bus entry defines */
85242131Sgrehan#define MPE_NUM_BUSES		2
86242131Sgrehan#define MPE_BUSNAME_LEN		6
87242131Sgrehan#define MPE_BUSNAME_ISA		"ISA   "
88242131Sgrehan#define MPE_BUSNAME_PCI		"PCI   "
89242131Sgrehan
90242131Sgrehanstatic void *oem_tbl_start;
91242131Sgrehanstatic int oem_tbl_size;
92242131Sgrehan
93221828Sgrehanstatic uint8_t
94242131Sgrehanmpt_compute_checksum(void *base, size_t len)
95221828Sgrehan{
96242131Sgrehan	uint8_t	*bytes;
97242131Sgrehan	uint8_t	sum;
98242131Sgrehan
99242131Sgrehan	for(bytes = base, sum = 0; len > 0; len--) {
100221828Sgrehan		sum += *bytes++;
101221828Sgrehan	}
102242131Sgrehan
103242131Sgrehan	return (256 - sum);
104221828Sgrehan}
105221828Sgrehan
106221828Sgrehanstatic void
107242131Sgrehanmpt_build_mpfp(mpfps_t mpfp, vm_paddr_t gpa)
108221828Sgrehan{
109242131Sgrehan
110221828Sgrehan	memset(mpfp, 0, sizeof(*mpfp));
111242131Sgrehan	memcpy(mpfp->signature, MPFP_SIG, 4);
112242131Sgrehan	mpfp->pap = gpa + sizeof(*mpfp);
113242131Sgrehan	mpfp->length = 1;
114242131Sgrehan	mpfp->spec_rev = MP_SPECREV;
115242131Sgrehan	mpfp->checksum = mpt_compute_checksum(mpfp, sizeof(*mpfp));
116221828Sgrehan}
117221828Sgrehan
118221828Sgrehanstatic void
119242131Sgrehanmpt_build_mpch(mpcth_t mpch)
120221828Sgrehan{
121242131Sgrehan
122221828Sgrehan	memset(mpch, 0, sizeof(*mpch));
123242131Sgrehan	memcpy(mpch->signature, MPCH_SIG, 4);
124242131Sgrehan	mpch->spec_rev = MP_SPECREV;
125242131Sgrehan	memcpy(mpch->oem_id, MPCH_OEMID, MPCH_OEMID_LEN);
126242131Sgrehan	memcpy(mpch->product_id, MPCH_PRODID, MPCH_PRODID_LEN);
127242131Sgrehan	mpch->apic_address = LAPIC_PADDR;
128221828Sgrehan}
129221828Sgrehan
130221828Sgrehanstatic void
131242131Sgrehanmpt_build_proc_entries(bproc_entry_ptr mpep, int ncpu)
132221828Sgrehan{
133221828Sgrehan	int i;
134221828Sgrehan
135242131Sgrehan	for (i = 0; i < ncpu; i++) {
136221828Sgrehan		memset(mpep, 0, sizeof(*mpep));
137242131Sgrehan		mpep->type = MPCT_ENTRY_PROCESSOR;
138242131Sgrehan		mpep->apic_id = i; // XXX
139242131Sgrehan		mpep->apic_version = LAPIC_VERSION;
140242131Sgrehan		mpep->cpu_flags = PROCENTRY_FLAG_EN;
141242131Sgrehan		if (i == 0)
142242131Sgrehan			mpep->cpu_flags |= PROCENTRY_FLAG_BP;
143242131Sgrehan		mpep->cpu_signature = MPEP_SIG;
144221828Sgrehan		mpep->feature_flags = MPEP_FEATURES;
145221828Sgrehan		mpep++;
146221828Sgrehan	}
147221828Sgrehan}
148221828Sgrehan
149221828Sgrehanstatic void
150242131Sgrehanmpt_build_bus_entries(bus_entry_ptr mpeb)
151221828Sgrehan{
152242131Sgrehan
153221828Sgrehan	memset(mpeb, 0, sizeof(*mpeb));
154242131Sgrehan	mpeb->type = MPCT_ENTRY_BUS;
155242131Sgrehan	mpeb->bus_id = ISA;
156242131Sgrehan	memcpy(mpeb->bus_type, MPE_BUSNAME_ISA, MPE_BUSNAME_LEN);
157221828Sgrehan	mpeb++;
158221828Sgrehan
159221828Sgrehan	memset(mpeb, 0, sizeof(*mpeb));
160242131Sgrehan	mpeb->type = MPCT_ENTRY_BUS;
161242131Sgrehan	mpeb->bus_id = PCI;
162242131Sgrehan	memcpy(mpeb->bus_type, MPE_BUSNAME_PCI, MPE_BUSNAME_LEN);
163221828Sgrehan}
164221828Sgrehan
165221828Sgrehanstatic void
166242131Sgrehanmpt_build_ioapic_entries(io_apic_entry_ptr mpei, int id)
167221828Sgrehan{
168242131Sgrehan
169221828Sgrehan	memset(mpei, 0, sizeof(*mpei));
170242131Sgrehan	mpei->type = MPCT_ENTRY_IOAPIC;
171242131Sgrehan	mpei->apic_id = id;
172242131Sgrehan	mpei->apic_version = IOAPIC_VERSION;
173242131Sgrehan	mpei->apic_flags = IOAPICENTRY_FLAG_EN;
174242131Sgrehan	mpei->apic_address = IOAPIC_PADDR;
175221828Sgrehan}
176221828Sgrehan
177239042Sneel#ifdef notyet
178221828Sgrehanstatic void
179242131Sgrehanmpt_build_ioint_entries(struct mpe_ioint *mpeii, int num_pins, int id)
180221828Sgrehan{
181221828Sgrehan	int pin;
182221828Sgrehan
183221828Sgrehan	/*
184221828Sgrehan	 * The following config is taken from kernel mptable.c
185221828Sgrehan	 * mptable_parse_default_config_ints(...), for now
186221828Sgrehan	 * just use the default config, tweek later if needed.
187221828Sgrehan	 */
188221828Sgrehan
189221828Sgrehan
190221828Sgrehan	/* Run through all 16 pins. */
191221828Sgrehan	for (pin = 0; pin < num_pins; pin++) {
192221828Sgrehan		memset(mpeii, 0, sizeof(*mpeii));
193221828Sgrehan		mpeii->entry_type = MP_ENTRY_IOINT;
194221828Sgrehan		mpeii->src_bus_id = MPE_BUSID_ISA;
195239042Sneel		mpeii->dst_apic_id = id;
196221828Sgrehan
197221828Sgrehan		/*
198242131Sgrehan		 * All default configs route IRQs from bus 0 to the first 16
199242131Sgrehan		 * pins of the first I/O APIC with an APIC ID of 2.
200221828Sgrehan		 */
201221828Sgrehan		mpeii->dst_apic_intin = pin;
202221828Sgrehan		switch (pin) {
203221828Sgrehan		case 0:
204221828Sgrehan			/* Pin 0 is an ExtINT pin. */
205221828Sgrehan			mpeii->intr_type = MPEII_INTR_EXTINT;
206221828Sgrehan			break;
207221828Sgrehan		case 2:
208221828Sgrehan			/* IRQ 0 is routed to pin 2. */
209221828Sgrehan			mpeii->intr_type = MPEII_INTR_INT;
210221828Sgrehan			mpeii->src_bus_irq = 0;
211221828Sgrehan			break;
212221828Sgrehan		case 5:
213221828Sgrehan		case 10:
214221828Sgrehan		case 11:
215221828Sgrehan			/*
216221828Sgrehan			 * PCI Irqs set to level triggered.
217221828Sgrehan			 */
218221828Sgrehan			mpeii->intr_flags = MPEII_FLAGS_TRIGMODE_LEVEL;
219221828Sgrehan			mpeii->src_bus_id = MPE_BUSID_PCI;
220221828Sgrehan		default:
221221828Sgrehan			/* All other pins are identity mapped. */
222221828Sgrehan			mpeii->intr_type = MPEII_INTR_INT;
223221828Sgrehan			mpeii->src_bus_irq = pin;
224221828Sgrehan			break;
225221828Sgrehan		}
226221828Sgrehan		mpeii++;
227221828Sgrehan	}
228221828Sgrehan
229221828Sgrehan}
230221828Sgrehan
231221828Sgrehan#define COPYSTR(dest, src, bytes)		\
232221828Sgrehan	memcpy(dest, src, bytes); 		\
233221828Sgrehan	str[bytes] = 0;
234221828Sgrehan
235221828Sgrehanstatic void
236221828Sgrehanmptable_dump(struct mp_floating_pointer *mpfp, struct mp_config_hdr *mpch)
237221828Sgrehan{
238221828Sgrehan	static char 	 str[16];
239221828Sgrehan	int 		 i;
240221828Sgrehan	char 		*cur;
241221828Sgrehan
242221828Sgrehan	union mpe {
243221828Sgrehan		struct mpe_proc 	*proc;
244221828Sgrehan		struct mpe_bus  	*bus;
245221828Sgrehan		struct mpe_ioapic 	*ioapic;
246221828Sgrehan		struct mpe_ioint 	*ioint;
247221828Sgrehan		struct mpe_lint 	*lnit;
248221828Sgrehan		char   			*p;
249221828Sgrehan	};
250221828Sgrehan
251221828Sgrehan	union mpe mpe;
252221828Sgrehan
253221828Sgrehan	printf(" MP Floating Pointer :\n");
254221828Sgrehan	COPYSTR(str, mpfp->signature, 4);
255242131Sgrehan	printf("\tsignature:\t%s\n", str);
256242131Sgrehan	printf("\tmpch paddr:\t%x\n", mpfp->mptable_paddr);
257242131Sgrehan	printf("\tlength:\t%x\n", mpfp->length);
258242131Sgrehan	printf("\tspecrec:\t%x\n", mpfp->specrev);
259242131Sgrehan	printf("\tchecksum:\t%x\n", mpfp->checksum);
260242131Sgrehan	printf("\tfeature1:\t%x\n", mpfp->feature1);
261242131Sgrehan	printf("\tfeature2:\t%x\n", mpfp->feature2);
262242131Sgrehan	printf("\tfeature3:\t%x\n", mpfp->feature3);
263242131Sgrehan	printf("\tfeature4:\t%x\n", mpfp->feature4);
264221828Sgrehan
265221828Sgrehan	printf(" MP Configuration Header :\n");
266221828Sgrehan	COPYSTR(str, mpch->signature, 4);
267221828Sgrehan	printf("    signature: 		%s\n", str);
268221828Sgrehan	printf("    length: 		%x\n", mpch->length);
269221828Sgrehan	printf("    specrec: 		%x\n", mpch->specrev);
270221828Sgrehan	printf("    checksum: 		%x\n", mpch->checksum);
271221828Sgrehan	COPYSTR(str, mpch->oemid, MPCH_OEMID_LEN);
272221828Sgrehan	printf("    oemid: 		%s\n", str);
273221828Sgrehan	COPYSTR(str, mpch->prodid, MPCH_PRODID_LEN);
274221828Sgrehan	printf("    prodid: 		%s\n", str);
275221828Sgrehan	printf("    oem_ptr: 		%x\n", mpch->oem_ptr);
276221828Sgrehan	printf("    oem_sz: 		%x\n", mpch->oem_sz);
277221828Sgrehan	printf("    nr_entries: 	%x\n", mpch->nr_entries);
278221828Sgrehan	printf("    apic paddr: 	%x\n", mpch->lapic_paddr);
279221828Sgrehan	printf("    ext_length: 	%x\n", mpch->ext_length);
280221828Sgrehan	printf("    ext_checksum: 	%x\n", mpch->ext_checksum);
281221828Sgrehan
282221828Sgrehan	cur = (char *)mpch + sizeof(*mpch);
283221828Sgrehan	for (i = 0; i < mpch->nr_entries; i++) {
284221828Sgrehan		mpe.p = cur;
285221828Sgrehan		switch(*mpe.p) {
286221828Sgrehan			case MP_ENTRY_PROC:
287221828Sgrehan				printf(" MP Processor Entry :\n");
288221828Sgrehan				printf("	lapic_id: 	%x\n", mpe.proc->lapic_id);
289221828Sgrehan				printf("	lapic_version:	%x\n", mpe.proc->lapic_version);
290221828Sgrehan				printf("	proc_flags: 	%x\n", mpe.proc->proc_flags);
291221828Sgrehan				printf("	proc_signature: %x\n", mpe.proc->proc_signature);
292221828Sgrehan				printf("	feature_flags: 	%x\n", mpe.proc->feature_flags);
293221828Sgrehan				cur += sizeof(struct mpe_proc);
294221828Sgrehan				break;
295221828Sgrehan			case MP_ENTRY_BUS:
296221828Sgrehan				printf(" MP Bus Entry :\n");
297221828Sgrehan				printf("	busid: 		%x\n", mpe.bus->busid);
298221828Sgrehan				COPYSTR(str, mpe.bus->busname, MPE_BUSNAME_LEN);
299221828Sgrehan				printf("	busname: 	%s\n", str);
300221828Sgrehan				cur += sizeof(struct mpe_bus);
301221828Sgrehan				break;
302221828Sgrehan			case MP_ENTRY_IOAPIC:
303221828Sgrehan				printf(" MP IOAPIC Entry :\n");
304221828Sgrehan				printf("	ioapi_id: 		%x\n", mpe.ioapic->ioapic_id);
305221828Sgrehan				printf("	ioapi_version: 		%x\n", mpe.ioapic->ioapic_version);
306221828Sgrehan				printf("	ioapi_flags: 		%x\n", mpe.ioapic->ioapic_flags);
307221828Sgrehan				printf("	ioapi_paddr: 		%x\n", mpe.ioapic->ioapic_paddr);
308221828Sgrehan				cur += sizeof(struct mpe_ioapic);
309221828Sgrehan				break;
310221828Sgrehan			case MP_ENTRY_IOINT:
311221828Sgrehan				printf(" MP IO Interrupt Entry :\n");
312221828Sgrehan				printf("	intr_type: 		%x\n", mpe.ioint->intr_type);
313221828Sgrehan				printf("	intr_flags: 		%x\n", mpe.ioint->intr_flags);
314221828Sgrehan				printf("	src_bus_id: 		%x\n", mpe.ioint->src_bus_id);
315221828Sgrehan				printf("	src_bus_irq: 		%x\n", mpe.ioint->src_bus_irq);
316221828Sgrehan				printf("	dst_apic_id: 		%x\n", mpe.ioint->dst_apic_id);
317221828Sgrehan				printf("	dst_apic_intin:		%x\n", mpe.ioint->dst_apic_intin);
318221828Sgrehan				cur += sizeof(struct mpe_ioint);
319221828Sgrehan				break;
320221828Sgrehan			case MP_ENTRY_LINT:
321221828Sgrehan				printf(" MP Local Interrupt Entry :\n");
322221828Sgrehan				cur += sizeof(struct mpe_lint);
323221828Sgrehan				break;
324221828Sgrehan		}
325221828Sgrehan
326221828Sgrehan	}
327221828Sgrehan}
328221942Sjhb#endif
329221828Sgrehan
330242131Sgrehanvoid
331242131Sgrehanmptable_add_oemtbl(void *tbl, int tblsz)
332242131Sgrehan{
333242131Sgrehan
334242131Sgrehan	oem_tbl_start = tbl;
335242131Sgrehan	oem_tbl_size = tblsz;
336242131Sgrehan}
337242131Sgrehan
338221828Sgrehanint
339242131Sgrehanmptable_build(struct vmctx *ctx, int ncpu, int ioapic)
340221828Sgrehan{
341242131Sgrehan	mpcth_t			mpch;
342242131Sgrehan	bus_entry_ptr		mpeb;
343242131Sgrehan	io_apic_entry_ptr	mpei;
344242131Sgrehan	bproc_entry_ptr		mpep;
345242131Sgrehan	mpfps_t			mpfp;
346242131Sgrehan	char 			*curraddr;
347221828Sgrehan	char 			*startaddr;
348221828Sgrehan
349242131Sgrehan	if (paddr_guest2host(0) == NULL) {
350242131Sgrehan		printf("mptable requires mapped mem\n");
351242131Sgrehan		return (ENOMEM);
352221828Sgrehan	}
353221828Sgrehan
354242131Sgrehan	startaddr = curraddr = paddr_guest2host(MPTABLE_BASE);
355221828Sgrehan
356242131Sgrehan	mpfp = (mpfps_t)curraddr;
357242131Sgrehan	mpt_build_mpfp(mpfp, MPTABLE_BASE);
358242131Sgrehan	curraddr += sizeof(*mpfp);
359221828Sgrehan
360242131Sgrehan	mpch = (mpcth_t)curraddr;
361242131Sgrehan	mpt_build_mpch(mpch);
362242131Sgrehan	curraddr += sizeof(*mpch);
363221828Sgrehan
364242131Sgrehan	mpep = (bproc_entry_ptr)curraddr;
365242131Sgrehan	mpt_build_proc_entries(mpep, ncpu);
366242131Sgrehan	curraddr += sizeof(*mpep) * ncpu;
367242131Sgrehan	mpch->entry_count += ncpu;
368221828Sgrehan
369242131Sgrehan	mpeb = (bus_entry_ptr) curraddr;
370242131Sgrehan	mpt_build_bus_entries(mpeb);
371242131Sgrehan	curraddr += sizeof(*mpeb) * MPE_NUM_BUSES;
372242131Sgrehan	mpch->entry_count += MPE_NUM_BUSES;
373242131Sgrehan
374239042Sneel	if (ioapic) {
375242131Sgrehan		mpei = (io_apic_entry_ptr)curraddr;
376242131Sgrehan		mpt_build_ioapic_entries(mpei, ncpu + 1);
377242131Sgrehan		curraddr += sizeof(*mpei);
378242131Sgrehan		mpch->entry_count++;
379239042Sneel	}
380239042Sneel
381239042Sneel#ifdef notyet
382242131Sgrehan	mpt_build_ioint_entries((struct mpe_ioint*)curraddr, MPEII_MAX_IRQ,
383239042Sneel				ncpu + 1);
384242131Sgrehan	curraddr += sizeof(struct mpe_ioint) * MPEII_MAX_IRQ;
385242131Sgrehan	mpch->entry_count += MPEII_MAX_IRQ;
386242131Sgrehan#endif
387221828Sgrehan
388242131Sgrehan	if (oem_tbl_start) {
389242131Sgrehan		mpch->oem_table_pointer = curraddr - startaddr + MPTABLE_BASE;
390242131Sgrehan		mpch->oem_table_size = oem_tbl_size;
391242131Sgrehan		memcpy(curraddr, oem_tbl_start, oem_tbl_size);
392221828Sgrehan	}
393221828Sgrehan
394242131Sgrehan	mpch->base_table_length = curraddr - (char *)mpch;
395242131Sgrehan	mpch->checksum = mpt_compute_checksum(mpch, sizeof(*mpch));
396221828Sgrehan
397242131Sgrehan	return (0);
398221828Sgrehan}
399