acpi.c revision 267450
1218885Sdim/*-
2218885Sdim * Copyright (c) 2012 NetApp, Inc.
3218885Sdim * All rights reserved.
4218885Sdim *
5218885Sdim * Redistribution and use in source and binary forms, with or without
6218885Sdim * modification, are permitted provided that the following conditions
7218885Sdim * are met:
8218885Sdim * 1. Redistributions of source code must retain the above copyright
9218885Sdim *    notice, this list of conditions and the following disclaimer.
10218885Sdim * 2. Redistributions in binary form must reproduce the above copyright
11218885Sdim *    notice, this list of conditions and the following disclaimer in the
12218885Sdim *    documentation and/or other materials provided with the distribution.
13218885Sdim *
14218885Sdim * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15218885Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16218885Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17263508Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18218885Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19218885Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20218885Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21218885Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22218885Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23218885Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24234353Sdim * SUCH DAMAGE.
25234353Sdim *
26251662Sdim * $FreeBSD: stable/10/usr.sbin/bhyve/acpi.c 267450 2014-06-13 21:30:40Z jhb $
27224145Sdim */
28218885Sdim
29218885Sdim/*
30251662Sdim * bhyve ACPI table generator.
31251662Sdim *
32251662Sdim * Create the minimal set of ACPI tables required to boot FreeBSD (and
33251662Sdim * hopefully other o/s's) by writing out ASL template files for each of
34251662Sdim * the tables and the compiling them to AML with the Intel iasl compiler.
35251662Sdim * The AML files are then read into guest memory.
36263508Sdim *
37263508Sdim *  The tables are placed in the guest's ROM area just below 1MB physical,
38263508Sdim * above the MPTable.
39263508Sdim *
40218885Sdim *  Layout
41263508Sdim *  ------
42263508Sdim *   RSDP  ->   0xf2400    (36 bytes fixed)
43218885Sdim *     RSDT  ->   0xf2440    (36 bytes + 4*N table addrs, 2 used)
44218885Sdim *     XSDT  ->   0xf2480    (36 bytes + 8*N table addrs, 2 used)
45263508Sdim *       MADT  ->   0xf2500  (depends on #CPUs)
46263508Sdim *       FADT  ->   0xf2600  (268 bytes)
47263508Sdim *       HPET  ->   0xf2740  (56 bytes)
48263508Sdim *         FACS  ->   0xf2780 (64 bytes)
49263508Sdim *         DSDT  ->   0xf2800 (variable - can go up to 0x100000)
50263508Sdim */
51263508Sdim
52263508Sdim#include <sys/cdefs.h>
53263508Sdim__FBSDID("$FreeBSD: stable/10/usr.sbin/bhyve/acpi.c 267450 2014-06-13 21:30:40Z jhb $");
54263508Sdim
55263508Sdim#include <sys/param.h>
56263508Sdim#include <sys/errno.h>
57263508Sdim#include <sys/stat.h>
58263508Sdim
59263508Sdim#include <paths.h>
60263508Sdim#include <stdarg.h>
61263508Sdim#include <stdio.h>
62263508Sdim#include <stdlib.h>
63263508Sdim#include <string.h>
64263508Sdim#include <unistd.h>
65263508Sdim
66263508Sdim#include <machine/vmm.h>
67263508Sdim#include <vmmapi.h>
68263508Sdim
69263508Sdim#include "bhyverun.h"
70263508Sdim#include "acpi.h"
71263508Sdim#include "pci_emul.h"
72263508Sdim
73263508Sdim/*
74263508Sdim * Define the base address of the ACPI tables, and the offsets to
75263508Sdim * the individual tables
76263508Sdim */
77218885Sdim#define BHYVE_ACPI_BASE		0xf2400
78218885Sdim#define RSDT_OFFSET		0x040
79218885Sdim#define XSDT_OFFSET		0x080
80218885Sdim#define MADT_OFFSET		0x100
81234353Sdim#define FADT_OFFSET		0x200
82218885Sdim#define	HPET_OFFSET		0x340
83218885Sdim#define FACS_OFFSET		0x380
84218885Sdim#define DSDT_OFFSET		0x400
85
86#define	BHYVE_ASL_TEMPLATE	"bhyve.XXXXXXX"
87#define BHYVE_ASL_SUFFIX	".aml"
88#define BHYVE_ASL_COMPILER	"/usr/sbin/iasl"
89
90static int basl_keep_temps;
91static int basl_verbose_iasl;
92static int basl_ncpu;
93static uint32_t basl_acpi_base = BHYVE_ACPI_BASE;
94static uint32_t hpet_capabilities;
95
96/*
97 * Contains the full pathname of the template to be passed
98 * to mkstemp/mktemps(3)
99 */
100static char basl_template[MAXPATHLEN];
101static char basl_stemplate[MAXPATHLEN];
102
103/*
104 * State for dsdt_line(), dsdt_indent(), and dsdt_unindent().
105 */
106static FILE *dsdt_fp;
107static int dsdt_indent_level;
108static int dsdt_error;
109
110struct basl_fio {
111	int	fd;
112	FILE	*fp;
113	char	f_name[MAXPATHLEN];
114};
115
116#define EFPRINTF(...) \
117	err = fprintf(__VA_ARGS__); if (err < 0) goto err_exit;
118
119#define EFFLUSH(x) \
120	err = fflush(x); if (err != 0) goto err_exit;
121
122static int
123basl_fwrite_rsdp(FILE *fp)
124{
125	int err;
126
127	err = 0;
128
129	EFPRINTF(fp, "/*\n");
130	EFPRINTF(fp, " * bhyve RSDP template\n");
131	EFPRINTF(fp, " */\n");
132	EFPRINTF(fp, "[0008]\t\tSignature : \"RSD PTR \"\n");
133	EFPRINTF(fp, "[0001]\t\tChecksum : 43\n");
134	EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
135	EFPRINTF(fp, "[0001]\t\tRevision : 02\n");
136	EFPRINTF(fp, "[0004]\t\tRSDT Address : %08X\n",
137	    basl_acpi_base + RSDT_OFFSET);
138	EFPRINTF(fp, "[0004]\t\tLength : 00000024\n");
139	EFPRINTF(fp, "[0008]\t\tXSDT Address : 00000000%08X\n",
140	    basl_acpi_base + XSDT_OFFSET);
141	EFPRINTF(fp, "[0001]\t\tExtended Checksum : 00\n");
142	EFPRINTF(fp, "[0003]\t\tReserved : 000000\n");
143
144	EFFLUSH(fp);
145
146	return (0);
147
148err_exit:
149	return (errno);
150}
151
152static int
153basl_fwrite_rsdt(FILE *fp)
154{
155	int err;
156
157	err = 0;
158
159	EFPRINTF(fp, "/*\n");
160	EFPRINTF(fp, " * bhyve RSDT template\n");
161	EFPRINTF(fp, " */\n");
162	EFPRINTF(fp, "[0004]\t\tSignature : \"RSDT\"\n");
163	EFPRINTF(fp, "[0004]\t\tTable Length : 00000000\n");
164	EFPRINTF(fp, "[0001]\t\tRevision : 01\n");
165	EFPRINTF(fp, "[0001]\t\tChecksum : 00\n");
166	EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
167	EFPRINTF(fp, "[0008]\t\tOem Table ID : \"BVRSDT  \"\n");
168	EFPRINTF(fp, "[0004]\t\tOem Revision : 00000001\n");
169	/* iasl will fill in the compiler ID/revision fields */
170	EFPRINTF(fp, "[0004]\t\tAsl Compiler ID : \"xxxx\"\n");
171	EFPRINTF(fp, "[0004]\t\tAsl Compiler Revision : 00000000\n");
172	EFPRINTF(fp, "\n");
173
174	/* Add in pointers to the MADT, FADT and HPET */
175	EFPRINTF(fp, "[0004]\t\tACPI Table Address 0 : %08X\n",
176	    basl_acpi_base + MADT_OFFSET);
177	EFPRINTF(fp, "[0004]\t\tACPI Table Address 1 : %08X\n",
178	    basl_acpi_base + FADT_OFFSET);
179	EFPRINTF(fp, "[0004]\t\tACPI Table Address 2 : %08X\n",
180	    basl_acpi_base + HPET_OFFSET);
181
182	EFFLUSH(fp);
183
184	return (0);
185
186err_exit:
187	return (errno);
188}
189
190static int
191basl_fwrite_xsdt(FILE *fp)
192{
193	int err;
194
195	err = 0;
196
197	EFPRINTF(fp, "/*\n");
198	EFPRINTF(fp, " * bhyve XSDT template\n");
199	EFPRINTF(fp, " */\n");
200	EFPRINTF(fp, "[0004]\t\tSignature : \"XSDT\"\n");
201	EFPRINTF(fp, "[0004]\t\tTable Length : 00000000\n");
202	EFPRINTF(fp, "[0001]\t\tRevision : 01\n");
203	EFPRINTF(fp, "[0001]\t\tChecksum : 00\n");
204	EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
205	EFPRINTF(fp, "[0008]\t\tOem Table ID : \"BVXSDT  \"\n");
206	EFPRINTF(fp, "[0004]\t\tOem Revision : 00000001\n");
207	/* iasl will fill in the compiler ID/revision fields */
208	EFPRINTF(fp, "[0004]\t\tAsl Compiler ID : \"xxxx\"\n");
209	EFPRINTF(fp, "[0004]\t\tAsl Compiler Revision : 00000000\n");
210	EFPRINTF(fp, "\n");
211
212	/* Add in pointers to the MADT, FADT and HPET */
213	EFPRINTF(fp, "[0004]\t\tACPI Table Address 0 : 00000000%08X\n",
214	    basl_acpi_base + MADT_OFFSET);
215	EFPRINTF(fp, "[0004]\t\tACPI Table Address 1 : 00000000%08X\n",
216	    basl_acpi_base + FADT_OFFSET);
217	EFPRINTF(fp, "[0004]\t\tACPI Table Address 2 : 00000000%08X\n",
218	    basl_acpi_base + HPET_OFFSET);
219
220	EFFLUSH(fp);
221
222	return (0);
223
224err_exit:
225	return (errno);
226}
227
228static int
229basl_fwrite_madt(FILE *fp)
230{
231	int err;
232	int i;
233
234	err = 0;
235
236	EFPRINTF(fp, "/*\n");
237	EFPRINTF(fp, " * bhyve MADT template\n");
238	EFPRINTF(fp, " */\n");
239	EFPRINTF(fp, "[0004]\t\tSignature : \"APIC\"\n");
240	EFPRINTF(fp, "[0004]\t\tTable Length : 00000000\n");
241	EFPRINTF(fp, "[0001]\t\tRevision : 01\n");
242	EFPRINTF(fp, "[0001]\t\tChecksum : 00\n");
243	EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
244	EFPRINTF(fp, "[0008]\t\tOem Table ID : \"BVMADT  \"\n");
245	EFPRINTF(fp, "[0004]\t\tOem Revision : 00000001\n");
246
247	/* iasl will fill in the compiler ID/revision fields */
248	EFPRINTF(fp, "[0004]\t\tAsl Compiler ID : \"xxxx\"\n");
249	EFPRINTF(fp, "[0004]\t\tAsl Compiler Revision : 00000000\n");
250	EFPRINTF(fp, "\n");
251
252	EFPRINTF(fp, "[0004]\t\tLocal Apic Address : FEE00000\n");
253	EFPRINTF(fp, "[0004]\t\tFlags (decoded below) : 00000001\n");
254	EFPRINTF(fp, "\t\t\tPC-AT Compatibility : 1\n");
255	EFPRINTF(fp, "\n");
256
257	/* Add a Processor Local APIC entry for each CPU */
258	for (i = 0; i < basl_ncpu; i++) {
259		EFPRINTF(fp, "[0001]\t\tSubtable Type : 00\n");
260		EFPRINTF(fp, "[0001]\t\tLength : 08\n");
261		/* iasl expects hex values for the proc and apic id's */
262		EFPRINTF(fp, "[0001]\t\tProcessor ID : %02x\n", i);
263		EFPRINTF(fp, "[0001]\t\tLocal Apic ID : %02x\n", i);
264		EFPRINTF(fp, "[0004]\t\tFlags (decoded below) : 00000001\n");
265		EFPRINTF(fp, "\t\t\tProcessor Enabled : 1\n");
266		EFPRINTF(fp, "\n");
267	}
268
269	/* Always a single IOAPIC entry, with ID 0 */
270	EFPRINTF(fp, "[0001]\t\tSubtable Type : 01\n");
271	EFPRINTF(fp, "[0001]\t\tLength : 0C\n");
272	/* iasl expects a hex value for the i/o apic id */
273	EFPRINTF(fp, "[0001]\t\tI/O Apic ID : %02x\n", 0);
274	EFPRINTF(fp, "[0001]\t\tReserved : 00\n");
275	EFPRINTF(fp, "[0004]\t\tAddress : fec00000\n");
276	EFPRINTF(fp, "[0004]\t\tInterrupt : 00000000\n");
277	EFPRINTF(fp, "\n");
278
279	/* Legacy IRQ0 is connected to pin 2 of the IOAPIC */
280	EFPRINTF(fp, "[0001]\t\tSubtable Type : 02\n");
281	EFPRINTF(fp, "[0001]\t\tLength : 0A\n");
282	EFPRINTF(fp, "[0001]\t\tBus : 00\n");
283	EFPRINTF(fp, "[0001]\t\tSource : 00\n");
284	EFPRINTF(fp, "[0004]\t\tInterrupt : 00000002\n");
285	EFPRINTF(fp, "[0002]\t\tFlags (decoded below) : 0005\n");
286	EFPRINTF(fp, "\t\t\tPolarity : 1\n");
287	EFPRINTF(fp, "\t\t\tTrigger Mode : 1\n");
288	EFPRINTF(fp, "\n");
289
290	EFPRINTF(fp, "[0001]\t\tSubtable Type : 02\n");
291	EFPRINTF(fp, "[0001]\t\tLength : 0A\n");
292	EFPRINTF(fp, "[0001]\t\tBus : 00\n");
293	EFPRINTF(fp, "[0001]\t\tSource : %02X\n", SCI_INT);
294	EFPRINTF(fp, "[0004]\t\tInterrupt : %08X\n", SCI_INT);
295	EFPRINTF(fp, "[0002]\t\tFlags (decoded below) : 0000\n");
296	EFPRINTF(fp, "\t\t\tPolarity : 3\n");
297	EFPRINTF(fp, "\t\t\tTrigger Mode : 3\n");
298	EFPRINTF(fp, "\n");
299
300	/* Local APIC NMI is connected to LINT 1 on all CPUs */
301	EFPRINTF(fp, "[0001]\t\tSubtable Type : 04\n");
302	EFPRINTF(fp, "[0001]\t\tLength : 06\n");
303	EFPRINTF(fp, "[0001]\t\tProcessorId : FF\n");
304	EFPRINTF(fp, "[0002]\t\tFlags (decoded below) : 0005\n");
305	EFPRINTF(fp, "\t\t\tPolarity : 1\n");
306	EFPRINTF(fp, "\t\t\tTrigger Mode : 1\n");
307	EFPRINTF(fp, "[0001]\t\tInterrupt : 01\n");
308	EFPRINTF(fp, "\n");
309
310	EFFLUSH(fp);
311
312	return (0);
313
314err_exit:
315	return (errno);
316}
317
318static int
319basl_fwrite_fadt(FILE *fp)
320{
321	int err;
322
323	err = 0;
324
325	EFPRINTF(fp, "/*\n");
326	EFPRINTF(fp, " * bhyve FADT template\n");
327	EFPRINTF(fp, " */\n");
328	EFPRINTF(fp, "[0004]\t\tSignature : \"FACP\"\n");
329	EFPRINTF(fp, "[0004]\t\tTable Length : 0000010C\n");
330	EFPRINTF(fp, "[0001]\t\tRevision : 05\n");
331	EFPRINTF(fp, "[0001]\t\tChecksum : 00\n");
332	EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
333	EFPRINTF(fp, "[0008]\t\tOem Table ID : \"BVFACP  \"\n");
334	EFPRINTF(fp, "[0004]\t\tOem Revision : 00000001\n");
335	/* iasl will fill in the compiler ID/revision fields */
336	EFPRINTF(fp, "[0004]\t\tAsl Compiler ID : \"xxxx\"\n");
337	EFPRINTF(fp, "[0004]\t\tAsl Compiler Revision : 00000000\n");
338	EFPRINTF(fp, "\n");
339
340	EFPRINTF(fp, "[0004]\t\tFACS Address : %08X\n",
341	    basl_acpi_base + FACS_OFFSET);
342	EFPRINTF(fp, "[0004]\t\tDSDT Address : %08X\n",
343	    basl_acpi_base + DSDT_OFFSET);
344	EFPRINTF(fp, "[0001]\t\tModel : 01\n");
345	EFPRINTF(fp, "[0001]\t\tPM Profile : 00 [Unspecified]\n");
346	EFPRINTF(fp, "[0002]\t\tSCI Interrupt : %04X\n",
347	    SCI_INT);
348	EFPRINTF(fp, "[0004]\t\tSMI Command Port : %08X\n",
349	    SMI_CMD);
350	EFPRINTF(fp, "[0001]\t\tACPI Enable Value : %02X\n",
351	    BHYVE_ACPI_ENABLE);
352	EFPRINTF(fp, "[0001]\t\tACPI Disable Value : %02X\n",
353	    BHYVE_ACPI_DISABLE);
354	EFPRINTF(fp, "[0001]\t\tS4BIOS Command : 00\n");
355	EFPRINTF(fp, "[0001]\t\tP-State Control : 00\n");
356	EFPRINTF(fp, "[0004]\t\tPM1A Event Block Address : %08X\n",
357	    PM1A_EVT_ADDR);
358	EFPRINTF(fp, "[0004]\t\tPM1B Event Block Address : 00000000\n");
359	EFPRINTF(fp, "[0004]\t\tPM1A Control Block Address : %08X\n",
360	    PM1A_CNT_ADDR);
361	EFPRINTF(fp, "[0004]\t\tPM1B Control Block Address : 00000000\n");
362	EFPRINTF(fp, "[0004]\t\tPM2 Control Block Address : 00000000\n");
363	EFPRINTF(fp, "[0004]\t\tPM Timer Block Address : %08X\n",
364	    IO_PMTMR);
365	EFPRINTF(fp, "[0004]\t\tGPE0 Block Address : 00000000\n");
366	EFPRINTF(fp, "[0004]\t\tGPE1 Block Address : 00000000\n");
367	EFPRINTF(fp, "[0001]\t\tPM1 Event Block Length : 04\n");
368	EFPRINTF(fp, "[0001]\t\tPM1 Control Block Length : 02\n");
369	EFPRINTF(fp, "[0001]\t\tPM2 Control Block Length : 00\n");
370	EFPRINTF(fp, "[0001]\t\tPM Timer Block Length : 04\n");
371	EFPRINTF(fp, "[0001]\t\tGPE0 Block Length : 00\n");
372	EFPRINTF(fp, "[0001]\t\tGPE1 Block Length : 00\n");
373	EFPRINTF(fp, "[0001]\t\tGPE1 Base Offset : 00\n");
374	EFPRINTF(fp, "[0001]\t\t_CST Support : 00\n");
375	EFPRINTF(fp, "[0002]\t\tC2 Latency : 0000\n");
376	EFPRINTF(fp, "[0002]\t\tC3 Latency : 0000\n");
377	EFPRINTF(fp, "[0002]\t\tCPU Cache Size : 0000\n");
378	EFPRINTF(fp, "[0002]\t\tCache Flush Stride : 0000\n");
379	EFPRINTF(fp, "[0001]\t\tDuty Cycle Offset : 00\n");
380	EFPRINTF(fp, "[0001]\t\tDuty Cycle Width : 00\n");
381	EFPRINTF(fp, "[0001]\t\tRTC Day Alarm Index : 00\n");
382	EFPRINTF(fp, "[0001]\t\tRTC Month Alarm Index : 00\n");
383	EFPRINTF(fp, "[0001]\t\tRTC Century Index : 00\n");
384	EFPRINTF(fp, "[0002]\t\tBoot Flags (decoded below) : 0000\n");
385	EFPRINTF(fp, "\t\t\tLegacy Devices Supported (V2) : 0\n");
386	EFPRINTF(fp, "\t\t\t8042 Present on ports 60/64 (V2) : 0\n");
387	EFPRINTF(fp, "\t\t\tVGA Not Present (V4) : 1\n");
388	EFPRINTF(fp, "\t\t\tMSI Not Supported (V4) : 0\n");
389	EFPRINTF(fp, "\t\t\tPCIe ASPM Not Supported (V4) : 1\n");
390	EFPRINTF(fp, "\t\t\tCMOS RTC Not Present (V5) : 0\n");
391	EFPRINTF(fp, "[0001]\t\tReserved : 00\n");
392	EFPRINTF(fp, "[0004]\t\tFlags (decoded below) : 00000000\n");
393	EFPRINTF(fp, "\t\t\tWBINVD instruction is operational (V1) : 1\n");
394	EFPRINTF(fp, "\t\t\tWBINVD flushes all caches (V1) : 0\n");
395	EFPRINTF(fp, "\t\t\tAll CPUs support C1 (V1) : 1\n");
396	EFPRINTF(fp, "\t\t\tC2 works on MP system (V1) : 0\n");
397	EFPRINTF(fp, "\t\t\tControl Method Power Button (V1) : 0\n");
398	EFPRINTF(fp, "\t\t\tControl Method Sleep Button (V1) : 1\n");
399	EFPRINTF(fp, "\t\t\tRTC wake not in fixed reg space (V1) : 0\n");
400	EFPRINTF(fp, "\t\t\tRTC can wake system from S4 (V1) : 0\n");
401	EFPRINTF(fp, "\t\t\t32-bit PM Timer (V1) : 1\n");
402	EFPRINTF(fp, "\t\t\tDocking Supported (V1) : 0\n");
403	EFPRINTF(fp, "\t\t\tReset Register Supported (V2) : 1\n");
404	EFPRINTF(fp, "\t\t\tSealed Case (V3) : 0\n");
405	EFPRINTF(fp, "\t\t\tHeadless - No Video (V3) : 1\n");
406	EFPRINTF(fp, "\t\t\tUse native instr after SLP_TYPx (V3) : 0\n");
407	EFPRINTF(fp, "\t\t\tPCIEXP_WAK Bits Supported (V4) : 0\n");
408	EFPRINTF(fp, "\t\t\tUse Platform Timer (V4) : 0\n");
409	EFPRINTF(fp, "\t\t\tRTC_STS valid on S4 wake (V4) : 0\n");
410	EFPRINTF(fp, "\t\t\tRemote Power-on capable (V4) : 0\n");
411	EFPRINTF(fp, "\t\t\tUse APIC Cluster Model (V4) : 0\n");
412	EFPRINTF(fp, "\t\t\tUse APIC Physical Destination Mode (V4) : 1\n");
413	EFPRINTF(fp, "\t\t\tHardware Reduced (V5) : 0\n");
414	EFPRINTF(fp, "\t\t\tLow Power S0 Idle (V5) : 0\n");
415	EFPRINTF(fp, "\n");
416
417	EFPRINTF(fp,
418	    "[0012]\t\tReset Register : [Generic Address Structure]\n");
419	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
420	EFPRINTF(fp, "[0001]\t\tBit Width : 08\n");
421	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
422	EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 01 [Byte Access:8]\n");
423	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000CF9\n");
424	EFPRINTF(fp, "\n");
425
426	EFPRINTF(fp, "[0001]\t\tValue to cause reset : 06\n");
427	EFPRINTF(fp, "[0003]\t\tReserved : 000000\n");
428	EFPRINTF(fp, "[0008]\t\tFACS Address : 00000000%08X\n",
429	    basl_acpi_base + FACS_OFFSET);
430	EFPRINTF(fp, "[0008]\t\tDSDT Address : 00000000%08X\n",
431	    basl_acpi_base + DSDT_OFFSET);
432	EFPRINTF(fp,
433	    "[0012]\t\tPM1A Event Block : [Generic Address Structure]\n");
434	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
435	EFPRINTF(fp, "[0001]\t\tBit Width : 20\n");
436	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
437	EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 02 [Word Access:16]\n");
438	EFPRINTF(fp, "[0008]\t\tAddress : 00000000%08X\n",
439	    PM1A_EVT_ADDR);
440	EFPRINTF(fp, "\n");
441
442	EFPRINTF(fp,
443	    "[0012]\t\tPM1B Event Block : [Generic Address Structure]\n");
444	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
445	EFPRINTF(fp, "[0001]\t\tBit Width : 00\n");
446	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
447	EFPRINTF(fp,
448	    "[0001]\t\tEncoded Access Width : 00 [Undefined/Legacy]\n");
449	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
450	EFPRINTF(fp, "\n");
451
452	EFPRINTF(fp,
453	    "[0012]\t\tPM1A Control Block : [Generic Address Structure]\n");
454	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
455	EFPRINTF(fp, "[0001]\t\tBit Width : 10\n");
456	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
457	EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 02 [Word Access:16]\n");
458	EFPRINTF(fp, "[0008]\t\tAddress : 00000000%08X\n",
459	    PM1A_CNT_ADDR);
460	EFPRINTF(fp, "\n");
461
462	EFPRINTF(fp,
463	    "[0012]\t\tPM1B Control Block : [Generic Address Structure]\n");
464	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
465	EFPRINTF(fp, "[0001]\t\tBit Width : 00\n");
466	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
467	EFPRINTF(fp,
468	    "[0001]\t\tEncoded Access Width : 00 [Undefined/Legacy]\n");
469	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
470	EFPRINTF(fp, "\n");
471
472	EFPRINTF(fp,
473	    "[0012]\t\tPM2 Control Block : [Generic Address Structure]\n");
474	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
475	EFPRINTF(fp, "[0001]\t\tBit Width : 08\n");
476	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
477	EFPRINTF(fp,
478	    "[0001]\t\tEncoded Access Width : 00 [Undefined/Legacy]\n");
479	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
480	EFPRINTF(fp, "\n");
481
482	/* Valid for bhyve */
483	EFPRINTF(fp,
484	    "[0012]\t\tPM Timer Block : [Generic Address Structure]\n");
485	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
486	EFPRINTF(fp, "[0001]\t\tBit Width : 32\n");
487	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
488	EFPRINTF(fp,
489	    "[0001]\t\tEncoded Access Width : 03 [DWord Access:32]\n");
490	EFPRINTF(fp, "[0008]\t\tAddress : 00000000%08X\n",
491	    IO_PMTMR);
492	EFPRINTF(fp, "\n");
493
494	EFPRINTF(fp, "[0012]\t\tGPE0 Block : [Generic Address Structure]\n");
495	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
496	EFPRINTF(fp, "[0001]\t\tBit Width : 80\n");
497	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
498	EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 01 [Byte Access:8]\n");
499	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
500	EFPRINTF(fp, "\n");
501
502	EFPRINTF(fp, "[0012]\t\tGPE1 Block : [Generic Address Structure]\n");
503	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
504	EFPRINTF(fp, "[0001]\t\tBit Width : 00\n");
505	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
506	EFPRINTF(fp,
507	    "[0001]\t\tEncoded Access Width : 00 [Undefined/Legacy]\n");
508	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
509	EFPRINTF(fp, "\n");
510
511	EFPRINTF(fp,
512	   "[0012]\t\tSleep Control Register : [Generic Address Structure]\n");
513	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
514	EFPRINTF(fp, "[0001]\t\tBit Width : 08\n");
515	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
516	EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 01 [Byte Access:8]\n");
517	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
518	EFPRINTF(fp, "\n");
519
520	EFPRINTF(fp,
521	    "[0012]\t\tSleep Status Register : [Generic Address Structure]\n");
522	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
523	EFPRINTF(fp, "[0001]\t\tBit Width : 08\n");
524	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
525	EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 01 [Byte Access:8]\n");
526	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
527
528	EFFLUSH(fp);
529
530	return (0);
531
532err_exit:
533	return (errno);
534}
535
536static int
537basl_fwrite_hpet(FILE *fp)
538{
539	int err;
540
541	err = 0;
542
543	EFPRINTF(fp, "/*\n");
544	EFPRINTF(fp, " * bhyve HPET template\n");
545	EFPRINTF(fp, " */\n");
546	EFPRINTF(fp, "[0004]\t\tSignature : \"HPET\"\n");
547	EFPRINTF(fp, "[0004]\t\tTable Length : 00000000\n");
548	EFPRINTF(fp, "[0001]\t\tRevision : 01\n");
549	EFPRINTF(fp, "[0001]\t\tChecksum : 00\n");
550	EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
551	EFPRINTF(fp, "[0008]\t\tOem Table ID : \"BVHPET  \"\n");
552	EFPRINTF(fp, "[0004]\t\tOem Revision : 00000001\n");
553
554	/* iasl will fill in the compiler ID/revision fields */
555	EFPRINTF(fp, "[0004]\t\tAsl Compiler ID : \"xxxx\"\n");
556	EFPRINTF(fp, "[0004]\t\tAsl Compiler Revision : 00000000\n");
557	EFPRINTF(fp, "\n");
558
559	EFPRINTF(fp, "[0004]\t\tTimer Block ID : %08X\n", hpet_capabilities);
560	EFPRINTF(fp,
561	    "[0012]\t\tTimer Block Register : [Generic Address Structure]\n");
562	EFPRINTF(fp, "[0001]\t\tSpace ID : 00 [SystemMemory]\n");
563	EFPRINTF(fp, "[0001]\t\tBit Width : 00\n");
564	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
565	EFPRINTF(fp,
566		 "[0001]\t\tEncoded Access Width : 00 [Undefined/Legacy]\n");
567	EFPRINTF(fp, "[0008]\t\tAddress : 00000000FED00000\n");
568	EFPRINTF(fp, "\n");
569
570	EFPRINTF(fp, "[0001]\t\tHPET Number : 00\n");
571	EFPRINTF(fp, "[0002]\t\tMinimum Clock Ticks : 0000\n");
572	EFPRINTF(fp, "[0004]\t\tFlags (decoded below) : 00000001\n");
573	EFPRINTF(fp, "\t\t\t4K Page Protect : 1\n");
574	EFPRINTF(fp, "\t\t\t64K Page Protect : 0\n");
575	EFPRINTF(fp, "\n");
576
577	EFFLUSH(fp);
578
579	return (0);
580
581err_exit:
582	return (errno);
583}
584
585static int
586basl_fwrite_facs(FILE *fp)
587{
588	int err;
589
590	err = 0;
591
592	EFPRINTF(fp, "/*\n");
593	EFPRINTF(fp, " * bhyve FACS template\n");
594	EFPRINTF(fp, " */\n");
595	EFPRINTF(fp, "[0004]\t\tSignature : \"FACS\"\n");
596	EFPRINTF(fp, "[0004]\t\tLength : 00000040\n");
597	EFPRINTF(fp, "[0004]\t\tHardware Signature : 00000000\n");
598	EFPRINTF(fp, "[0004]\t\t32 Firmware Waking Vector : 00000000\n");
599	EFPRINTF(fp, "[0004]\t\tGlobal Lock : 00000000\n");
600	EFPRINTF(fp, "[0004]\t\tFlags (decoded below) : 00000000\n");
601	EFPRINTF(fp, "\t\t\tS4BIOS Support Present : 0\n");
602	EFPRINTF(fp, "\t\t\t64-bit Wake Supported (V2) : 0\n");
603	EFPRINTF(fp,
604	    "[0008]\t\t64 Firmware Waking Vector : 0000000000000000\n");
605	EFPRINTF(fp, "[0001]\t\tVersion : 02\n");
606	EFPRINTF(fp, "[0003]\t\tReserved : 000000\n");
607	EFPRINTF(fp, "[0004]\t\tOspmFlags (decoded below) : 00000000\n");
608	EFPRINTF(fp, "\t\t\t64-bit Wake Env Required (V2) : 0\n");
609
610	EFFLUSH(fp);
611
612	return (0);
613
614err_exit:
615	return (errno);
616}
617
618/*
619 * Helper routines for writing to the DSDT from other modules.
620 */
621void
622dsdt_line(const char *fmt, ...)
623{
624	va_list ap;
625	int err;
626
627	if (dsdt_error != 0)
628		return;
629
630	if (strcmp(fmt, "") != 0) {
631		if (dsdt_indent_level != 0)
632			EFPRINTF(dsdt_fp, "%*c", dsdt_indent_level * 2, ' ');
633		va_start(ap, fmt);
634		if (vfprintf(dsdt_fp, fmt, ap) < 0)
635			goto err_exit;
636		va_end(ap);
637	}
638	EFPRINTF(dsdt_fp, "\n");
639	return;
640
641err_exit:
642	dsdt_error = errno;
643}
644
645void
646dsdt_indent(int levels)
647{
648
649	dsdt_indent_level += levels;
650	assert(dsdt_indent_level >= 0);
651}
652
653void
654dsdt_unindent(int levels)
655{
656
657	assert(dsdt_indent_level >= levels);
658	dsdt_indent_level -= levels;
659}
660
661void
662dsdt_fixed_ioport(uint16_t iobase, uint16_t length)
663{
664
665	dsdt_line("IO (Decode16,");
666	dsdt_line("  0x%04X,             // Range Minimum", iobase);
667	dsdt_line("  0x%04X,             // Range Maximum", iobase);
668	dsdt_line("  0x01,               // Alignment");
669	dsdt_line("  0x%02X,               // Length", length);
670	dsdt_line("  )");
671}
672
673void
674dsdt_fixed_irq(uint8_t irq)
675{
676
677	dsdt_line("IRQNoFlags ()");
678	dsdt_line("  {%d}", irq);
679}
680
681void
682dsdt_fixed_mem32(uint32_t base, uint32_t length)
683{
684
685	dsdt_line("Memory32Fixed (ReadWrite,");
686	dsdt_line("  0x%08X,         // Address Base", base);
687	dsdt_line("  0x%08X,         // Address Length", length);
688	dsdt_line("  )");
689}
690
691static int
692basl_fwrite_dsdt(FILE *fp)
693{
694	int err;
695
696	err = 0;
697	dsdt_fp = fp;
698	dsdt_error = 0;
699	dsdt_indent_level = 0;
700
701	dsdt_line("/*");
702	dsdt_line(" * bhyve DSDT template");
703	dsdt_line(" */");
704	dsdt_line("DefinitionBlock (\"bhyve_dsdt.aml\", \"DSDT\", 2,"
705		 "\"BHYVE \", \"BVDSDT  \", 0x00000001)");
706	dsdt_line("{");
707	dsdt_line("  Name (_S5, Package (0x02)");
708	dsdt_line("  {");
709	dsdt_line("      0x05,");
710	dsdt_line("      Zero,");
711	dsdt_line("  })");
712
713	pci_write_dsdt();
714
715	dsdt_line("");
716	dsdt_line("  Scope (_SB.PCI0)");
717	dsdt_line("  {");
718	dsdt_line("    Device (HPET)");
719	dsdt_line("    {");
720	dsdt_line("      Name (_HID, EISAID(\"PNP0103\"))");
721	dsdt_line("      Name (_UID, 0)");
722	dsdt_line("      Name (_CRS, ResourceTemplate ()");
723	dsdt_line("      {");
724	dsdt_indent(4);
725	dsdt_fixed_mem32(0xFED00000, 0x400);
726	dsdt_unindent(4);
727	dsdt_line("      })");
728	dsdt_line("    }");
729	dsdt_line("  }");
730	dsdt_line("}");
731
732	if (dsdt_error != 0)
733		return (dsdt_error);
734
735	EFFLUSH(fp);
736
737	return (0);
738
739err_exit:
740	return (errno);
741}
742
743static int
744basl_open(struct basl_fio *bf, int suffix)
745{
746	int err;
747
748	err = 0;
749
750	if (suffix) {
751		strncpy(bf->f_name, basl_stemplate, MAXPATHLEN);
752		bf->fd = mkstemps(bf->f_name, strlen(BHYVE_ASL_SUFFIX));
753	} else {
754		strncpy(bf->f_name, basl_template, MAXPATHLEN);
755		bf->fd = mkstemp(bf->f_name);
756	}
757
758	if (bf->fd > 0) {
759		bf->fp = fdopen(bf->fd, "w+");
760		if (bf->fp == NULL) {
761			unlink(bf->f_name);
762			close(bf->fd);
763		}
764	} else {
765		err = 1;
766	}
767
768	return (err);
769}
770
771static void
772basl_close(struct basl_fio *bf)
773{
774
775	if (!basl_keep_temps)
776		unlink(bf->f_name);
777	fclose(bf->fp);
778}
779
780static int
781basl_start(struct basl_fio *in, struct basl_fio *out)
782{
783	int err;
784
785	err = basl_open(in, 0);
786	if (!err) {
787		err = basl_open(out, 1);
788		if (err) {
789			basl_close(in);
790		}
791	}
792
793	return (err);
794}
795
796static void
797basl_end(struct basl_fio *in, struct basl_fio *out)
798{
799
800	basl_close(in);
801	basl_close(out);
802}
803
804static int
805basl_load(struct vmctx *ctx, int fd, uint64_t off)
806{
807	struct stat sb;
808	void *gaddr;
809
810	if (fstat(fd, &sb) < 0)
811		return (errno);
812
813	gaddr = paddr_guest2host(ctx, basl_acpi_base + off, sb.st_size);
814	if (gaddr == NULL)
815		return (EFAULT);
816
817	if (read(fd, gaddr, sb.st_size) < 0)
818		return (errno);
819
820	return (0);
821}
822
823static int
824basl_compile(struct vmctx *ctx, int (*fwrite_section)(FILE *), uint64_t offset)
825{
826	struct basl_fio io[2];
827	static char iaslbuf[3*MAXPATHLEN + 10];
828	char *fmt;
829	int err;
830
831	err = basl_start(&io[0], &io[1]);
832	if (!err) {
833		err = (*fwrite_section)(io[0].fp);
834
835		if (!err) {
836			/*
837			 * iasl sends the results of the compilation to
838			 * stdout. Shut this down by using the shell to
839			 * redirect stdout to /dev/null, unless the user
840			 * has requested verbose output for debugging
841			 * purposes
842			 */
843			fmt = basl_verbose_iasl ?
844				"%s -p %s %s" :
845				"/bin/sh -c \"%s -p %s %s\" 1> /dev/null";
846
847			snprintf(iaslbuf, sizeof(iaslbuf),
848				 fmt,
849				 BHYVE_ASL_COMPILER,
850				 io[1].f_name, io[0].f_name);
851			err = system(iaslbuf);
852
853			if (!err) {
854				/*
855				 * Copy the aml output file into guest
856				 * memory at the specified location
857				 */
858				err = basl_load(ctx, io[1].fd, offset);
859			}
860		}
861		basl_end(&io[0], &io[1]);
862	}
863
864	return (err);
865}
866
867static int
868basl_make_templates(void)
869{
870	const char *tmpdir;
871	int err;
872	int len;
873
874	err = 0;
875
876	/*
877	 *
878	 */
879	if ((tmpdir = getenv("BHYVE_TMPDIR")) == NULL || *tmpdir == '\0' ||
880	    (tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0') {
881		tmpdir = _PATH_TMP;
882	}
883
884	len = strlen(tmpdir);
885
886	if ((len + sizeof(BHYVE_ASL_TEMPLATE) + 1) < MAXPATHLEN) {
887		strcpy(basl_template, tmpdir);
888		while (len > 0 && basl_template[len - 1] == '/')
889			len--;
890		basl_template[len] = '/';
891		strcpy(&basl_template[len + 1], BHYVE_ASL_TEMPLATE);
892	} else
893		err = E2BIG;
894
895	if (!err) {
896		/*
897		 * len has been intialized (and maybe adjusted) above
898		 */
899		if ((len + sizeof(BHYVE_ASL_TEMPLATE) + 1 +
900		     sizeof(BHYVE_ASL_SUFFIX)) < MAXPATHLEN) {
901			strcpy(basl_stemplate, tmpdir);
902			basl_stemplate[len] = '/';
903			strcpy(&basl_stemplate[len + 1], BHYVE_ASL_TEMPLATE);
904			len = strlen(basl_stemplate);
905			strcpy(&basl_stemplate[len], BHYVE_ASL_SUFFIX);
906		} else
907			err = E2BIG;
908	}
909
910	return (err);
911}
912
913static struct {
914	int	(*wsect)(FILE *fp);
915	uint64_t  offset;
916} basl_ftables[] =
917{
918	{ basl_fwrite_rsdp, 0},
919	{ basl_fwrite_rsdt, RSDT_OFFSET },
920	{ basl_fwrite_xsdt, XSDT_OFFSET },
921	{ basl_fwrite_madt, MADT_OFFSET },
922	{ basl_fwrite_fadt, FADT_OFFSET },
923	{ basl_fwrite_hpet, HPET_OFFSET },
924	{ basl_fwrite_facs, FACS_OFFSET },
925	{ basl_fwrite_dsdt, DSDT_OFFSET },
926	{ NULL }
927};
928
929int
930acpi_build(struct vmctx *ctx, int ncpu)
931{
932	int err;
933	int i;
934
935	basl_ncpu = ncpu;
936
937	err = vm_get_hpet_capabilities(ctx, &hpet_capabilities);
938	if (err != 0)
939		return (err);
940
941	/*
942	 * For debug, allow the user to have iasl compiler output sent
943	 * to stdout rather than /dev/null
944	 */
945	if (getenv("BHYVE_ACPI_VERBOSE_IASL"))
946		basl_verbose_iasl = 1;
947
948	/*
949	 * Allow the user to keep the generated ASL files for debugging
950	 * instead of deleting them following use
951	 */
952	if (getenv("BHYVE_ACPI_KEEPTMPS"))
953		basl_keep_temps = 1;
954
955	i = 0;
956	err = basl_make_templates();
957
958	/*
959	 * Run through all the ASL files, compiling them and
960	 * copying them into guest memory
961	 */
962	while (!err && basl_ftables[i].wsect != NULL) {
963		err = basl_compile(ctx, basl_ftables[i].wsect,
964				   basl_ftables[i].offset);
965		i++;
966	}
967
968	return (err);
969}
970