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