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