xref: /freebsd/usr.sbin/bhyve/acpi.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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).
36  *
37  * The tables are placed in the guest's ROM area just below 1MB physical,
38  * above the MPTable.
39  */
40 
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 #include <sys/param.h>
45 #include <sys/endian.h>
46 #include <sys/errno.h>
47 #include <sys/stat.h>
48 
49 #include <err.h>
50 #include <paths.h>
51 #include <stdarg.h>
52 #include <stddef.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57 
58 #include <machine/vmm.h>
59 #include <vmmapi.h>
60 
61 #include "bhyverun.h"
62 #include "acpi.h"
63 #include "basl.h"
64 #include "pci_emul.h"
65 #include "vmgenc.h"
66 
67 #define	BHYVE_ASL_TEMPLATE	"bhyve.XXXXXXX"
68 #define BHYVE_ASL_SUFFIX	".aml"
69 #define BHYVE_ASL_COMPILER	"/usr/sbin/iasl"
70 
71 #define BHYVE_ADDRESS_IOAPIC 	0xFEC00000
72 #define BHYVE_ADDRESS_HPET 	0xFED00000
73 #define BHYVE_ADDRESS_LAPIC 	0xFEE00000
74 
75 static int basl_keep_temps;
76 static int basl_verbose_iasl;
77 static int basl_ncpu;
78 static uint32_t hpet_capabilities;
79 
80 /*
81  * Contains the full pathname of the template to be passed
82  * to mkstemp/mktemps(3)
83  */
84 static char basl_template[MAXPATHLEN];
85 static char basl_stemplate[MAXPATHLEN];
86 
87 /*
88  * State for dsdt_line(), dsdt_indent(), and dsdt_unindent().
89  */
90 static FILE *dsdt_fp;
91 static int dsdt_indent_level;
92 static int dsdt_error;
93 
94 struct basl_fio {
95 	int	fd;
96 	FILE	*fp;
97 	char	f_name[MAXPATHLEN];
98 };
99 
100 #define EFPRINTF(...) \
101 	if (fprintf(__VA_ARGS__) < 0) goto err_exit
102 
103 #define EFFLUSH(x) \
104 	if (fflush(x) != 0) goto err_exit
105 
106 /*
107  * A list for additional ACPI devices like a TPM.
108  */
109 struct acpi_device_list_entry {
110 	SLIST_ENTRY(acpi_device_list_entry) chain;
111 	const struct acpi_device *dev;
112 };
113 static SLIST_HEAD(acpi_device_list,
114     acpi_device_list_entry) acpi_devices = SLIST_HEAD_INITIALIZER(acpi_devices);
115 
116 int
117 acpi_tables_add_device(const struct acpi_device *const dev)
118 {
119 	struct acpi_device_list_entry *const entry = calloc(1, sizeof(*entry));
120 	if (entry == NULL) {
121 		return (ENOMEM);
122 	}
123 
124 	entry->dev = dev;
125 	SLIST_INSERT_HEAD(&acpi_devices, entry, chain);
126 
127 	return (0);
128 }
129 
130 /*
131  * Helper routines for writing to the DSDT from other modules.
132  */
133 void
134 dsdt_line(const char *fmt, ...)
135 {
136 	va_list ap;
137 
138 	if (dsdt_error != 0)
139 		return;
140 
141 	if (strcmp(fmt, "") != 0) {
142 		if (dsdt_indent_level != 0)
143 			EFPRINTF(dsdt_fp, "%*c", dsdt_indent_level * 2, ' ');
144 		va_start(ap, fmt);
145 		if (vfprintf(dsdt_fp, fmt, ap) < 0) {
146 			va_end(ap);
147 			goto err_exit;
148 		}
149 		va_end(ap);
150 	}
151 	EFPRINTF(dsdt_fp, "\n");
152 	return;
153 
154 err_exit:
155 	dsdt_error = errno;
156 }
157 
158 void
159 dsdt_indent(int levels)
160 {
161 
162 	dsdt_indent_level += levels;
163 	assert(dsdt_indent_level >= 0);
164 }
165 
166 void
167 dsdt_unindent(int levels)
168 {
169 
170 	assert(dsdt_indent_level >= levels);
171 	dsdt_indent_level -= levels;
172 }
173 
174 void
175 dsdt_fixed_ioport(uint16_t iobase, uint16_t length)
176 {
177 
178 	dsdt_line("IO (Decode16,");
179 	dsdt_line("  0x%04X,             // Range Minimum", iobase);
180 	dsdt_line("  0x%04X,             // Range Maximum", iobase);
181 	dsdt_line("  0x01,               // Alignment");
182 	dsdt_line("  0x%02X,               // Length", length);
183 	dsdt_line("  )");
184 }
185 
186 void
187 dsdt_fixed_irq(uint8_t irq)
188 {
189 
190 	dsdt_line("IRQNoFlags ()");
191 	dsdt_line("  {%d}", irq);
192 }
193 
194 void
195 dsdt_fixed_mem32(uint32_t base, uint32_t length)
196 {
197 
198 	dsdt_line("Memory32Fixed (ReadWrite,");
199 	dsdt_line("  0x%08X,         // Address Base", base);
200 	dsdt_line("  0x%08X,         // Address Length", length);
201 	dsdt_line("  )");
202 }
203 
204 static int
205 basl_fwrite_dsdt(FILE *fp)
206 {
207 	dsdt_fp = fp;
208 	dsdt_error = 0;
209 	dsdt_indent_level = 0;
210 
211 	dsdt_line("/*");
212 	dsdt_line(" * bhyve DSDT template");
213 	dsdt_line(" */");
214 	dsdt_line("DefinitionBlock (\"bhyve_dsdt.aml\", \"DSDT\", 2,"
215 		 "\"BHYVE \", \"BVDSDT  \", 0x00000001)");
216 	dsdt_line("{");
217 	dsdt_line("  Name (_S5, Package ()");
218 	dsdt_line("  {");
219 	dsdt_line("      0x05,");
220 	dsdt_line("      Zero,");
221 	dsdt_line("  })");
222 
223 	pci_write_dsdt();
224 
225 	dsdt_line("");
226 	dsdt_line("  Scope (_SB.PC00)");
227 	dsdt_line("  {");
228 	dsdt_line("    Device (HPET)");
229 	dsdt_line("    {");
230 	dsdt_line("      Name (_HID, EISAID(\"PNP0103\"))");
231 	dsdt_line("      Name (_UID, 0)");
232 	dsdt_line("      Name (_CRS, ResourceTemplate ()");
233 	dsdt_line("      {");
234 	dsdt_indent(4);
235 	dsdt_fixed_mem32(0xFED00000, 0x400);
236 	dsdt_unindent(4);
237 	dsdt_line("      })");
238 	dsdt_line("    }");
239 	dsdt_line("  }");
240 
241 	vmgenc_write_dsdt();
242 
243 	const struct acpi_device_list_entry *entry;
244 	SLIST_FOREACH(entry, &acpi_devices, chain) {
245 		BASL_EXEC(acpi_device_write_dsdt(entry->dev));
246 	}
247 
248 	dsdt_line("}");
249 
250 	if (dsdt_error != 0)
251 		return (dsdt_error);
252 
253 	EFFLUSH(fp);
254 
255 	return (0);
256 
257 err_exit:
258 	return (errno);
259 }
260 
261 static int
262 basl_open(struct basl_fio *bf, int suffix)
263 {
264 	int err;
265 
266 	err = 0;
267 
268 	if (suffix) {
269 		strlcpy(bf->f_name, basl_stemplate, MAXPATHLEN);
270 		bf->fd = mkstemps(bf->f_name, strlen(BHYVE_ASL_SUFFIX));
271 	} else {
272 		strlcpy(bf->f_name, basl_template, MAXPATHLEN);
273 		bf->fd = mkstemp(bf->f_name);
274 	}
275 
276 	if (bf->fd > 0) {
277 		bf->fp = fdopen(bf->fd, "w+");
278 		if (bf->fp == NULL) {
279 			unlink(bf->f_name);
280 			close(bf->fd);
281 		}
282 	} else {
283 		err = 1;
284 	}
285 
286 	return (err);
287 }
288 
289 static void
290 basl_close(struct basl_fio *bf)
291 {
292 
293 	if (!basl_keep_temps)
294 		unlink(bf->f_name);
295 	fclose(bf->fp);
296 }
297 
298 static int
299 basl_start(struct basl_fio *in, struct basl_fio *out)
300 {
301 	int err;
302 
303 	err = basl_open(in, 0);
304 	if (!err) {
305 		err = basl_open(out, 1);
306 		if (err) {
307 			basl_close(in);
308 		}
309 	}
310 
311 	return (err);
312 }
313 
314 static void
315 basl_end(struct basl_fio *in, struct basl_fio *out)
316 {
317 
318 	basl_close(in);
319 	basl_close(out);
320 }
321 
322 static int
323 basl_load(struct vmctx *ctx, int fd)
324 {
325 	struct stat sb;
326 	void *addr;
327 
328 	if (fstat(fd, &sb) < 0)
329 		return (errno);
330 
331 	addr = calloc(1, sb.st_size);
332 	if (addr == NULL)
333 		return (EFAULT);
334 
335 	if (read(fd, addr, sb.st_size) < 0)
336 		return (errno);
337 
338 	struct basl_table *table;
339 
340 	uint8_t name[ACPI_NAMESEG_SIZE + 1] = { 0 };
341 	memcpy(name, addr, sizeof(name) - 1 /* last char is '\0' */);
342 	BASL_EXEC(basl_table_create(&table, ctx, name, BASL_TABLE_ALIGNMENT));
343 	BASL_EXEC(basl_table_append_bytes(table, addr, sb.st_size));
344 
345 	return (0);
346 }
347 
348 static int
349 basl_compile(struct vmctx *ctx, int (*fwrite_section)(FILE *))
350 {
351 	struct basl_fio io[2];
352 	static char iaslbuf[3*MAXPATHLEN + 10];
353 	const char *fmt;
354 	int err;
355 
356 	err = basl_start(&io[0], &io[1]);
357 	if (!err) {
358 		err = (*fwrite_section)(io[0].fp);
359 
360 		if (!err) {
361 			/*
362 			 * iasl sends the results of the compilation to
363 			 * stdout. Shut this down by using the shell to
364 			 * redirect stdout to /dev/null, unless the user
365 			 * has requested verbose output for debugging
366 			 * purposes
367 			 */
368 			fmt = basl_verbose_iasl ?
369 				"%s -p %s %s" :
370 				"/bin/sh -c \"%s -p %s %s\" 1> /dev/null";
371 
372 			snprintf(iaslbuf, sizeof(iaslbuf),
373 				 fmt,
374 				 BHYVE_ASL_COMPILER,
375 				 io[1].f_name, io[0].f_name);
376 			err = system(iaslbuf);
377 
378 			if (!err) {
379 				/*
380 				 * Copy the aml output file into guest
381 				 * memory at the specified location
382 				 */
383 				err = basl_load(ctx, io[1].fd);
384 			}
385 		}
386 		basl_end(&io[0], &io[1]);
387 	}
388 
389 	return (err);
390 }
391 
392 static int
393 basl_make_templates(void)
394 {
395 	const char *tmpdir;
396 	int err;
397 	int len;
398 
399 	err = 0;
400 
401 	/*
402 	 *
403 	 */
404 	if ((tmpdir = getenv("BHYVE_TMPDIR")) == NULL || *tmpdir == '\0' ||
405 	    (tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0') {
406 		tmpdir = _PATH_TMP;
407 	}
408 
409 	len = strlen(tmpdir);
410 
411 	if ((len + sizeof(BHYVE_ASL_TEMPLATE) + 1) < MAXPATHLEN) {
412 		strcpy(basl_template, tmpdir);
413 		while (len > 0 && basl_template[len - 1] == '/')
414 			len--;
415 		basl_template[len] = '/';
416 		strcpy(&basl_template[len + 1], BHYVE_ASL_TEMPLATE);
417 	} else
418 		err = E2BIG;
419 
420 	if (!err) {
421 		/*
422 		 * len has been initialized (and maybe adjusted) above
423 		 */
424 		if ((len + sizeof(BHYVE_ASL_TEMPLATE) + 1 +
425 		     sizeof(BHYVE_ASL_SUFFIX)) < MAXPATHLEN) {
426 			strcpy(basl_stemplate, tmpdir);
427 			basl_stemplate[len] = '/';
428 			strcpy(&basl_stemplate[len + 1], BHYVE_ASL_TEMPLATE);
429 			len = strlen(basl_stemplate);
430 			strcpy(&basl_stemplate[len], BHYVE_ASL_SUFFIX);
431 		} else
432 			err = E2BIG;
433 	}
434 
435 	return (err);
436 }
437 
438 static int
439 build_dsdt(struct vmctx *const ctx)
440 {
441 	BASL_EXEC(basl_compile(ctx, basl_fwrite_dsdt));
442 
443 	return (0);
444 }
445 
446 static int
447 build_facs(struct vmctx *const ctx)
448 {
449 	ACPI_TABLE_FACS facs;
450 	struct basl_table *table;
451 
452 	BASL_EXEC(basl_table_create(&table, ctx, ACPI_SIG_FACS,
453 	    BASL_TABLE_ALIGNMENT_FACS));
454 
455 	memset(&facs, 0, sizeof(facs));
456 	memcpy(facs.Signature, ACPI_SIG_FACS, ACPI_NAMESEG_SIZE);
457 	facs.Length = sizeof(facs);
458 	facs.Version = htole32(2);
459 	BASL_EXEC(basl_table_append_bytes(table, &facs, sizeof(facs)));
460 
461 	return (0);
462 }
463 
464 static int
465 build_fadt(struct vmctx *const ctx)
466 {
467 	ACPI_TABLE_FADT fadt;
468 	struct basl_table *table;
469 
470 	BASL_EXEC(basl_table_create(&table, ctx, ACPI_SIG_FADT,
471 	    BASL_TABLE_ALIGNMENT));
472 
473 	memset(&fadt, 0, sizeof(fadt));
474 	BASL_EXEC(basl_table_append_header(table, ACPI_SIG_FADT, 5, 1));
475 	fadt.Facs = htole32(0); /* patched by basl */
476 	fadt.Dsdt = htole32(0); /* patched by basl */
477 	fadt.SciInterrupt = htole16(SCI_INT);
478 	fadt.SmiCommand = htole32(SMI_CMD);
479 	fadt.AcpiEnable = BHYVE_ACPI_ENABLE;
480 	fadt.AcpiDisable = BHYVE_ACPI_DISABLE;
481 	fadt.Pm1aEventBlock = htole32(PM1A_EVT_ADDR);
482 	fadt.Pm1aControlBlock = htole32(PM1A_CNT_ADDR);
483 	fadt.PmTimerBlock = htole32(IO_PMTMR);
484 	fadt.Gpe0Block = htole32(IO_GPE0_BLK);
485 	fadt.Pm1EventLength = 4;
486 	fadt.Pm1ControlLength = 2;
487 	fadt.PmTimerLength = 4;
488 	fadt.Gpe0BlockLength = IO_GPE0_LEN;
489 	fadt.Century = 0x32;
490 	fadt.BootFlags = htole16(ACPI_FADT_NO_VGA | ACPI_FADT_NO_ASPM);
491 	fadt.Flags = htole32(ACPI_FADT_WBINVD | ACPI_FADT_C1_SUPPORTED |
492 	    ACPI_FADT_SLEEP_BUTTON | ACPI_FADT_32BIT_TIMER |
493 	    ACPI_FADT_RESET_REGISTER | ACPI_FADT_HEADLESS |
494 	    ACPI_FADT_APIC_PHYSICAL);
495 	basl_fill_gas(&fadt.ResetRegister, ACPI_ADR_SPACE_SYSTEM_IO, 8, 0,
496 	    ACPI_GAS_ACCESS_WIDTH_BYTE, 0xCF9);
497 	fadt.ResetValue = 6;
498 	fadt.MinorRevision = 1;
499 	fadt.XFacs = htole64(0); /* patched by basl */
500 	fadt.XDsdt = htole64(0); /* patched by basl */
501 	basl_fill_gas(&fadt.XPm1aEventBlock, ACPI_ADR_SPACE_SYSTEM_IO, 0x20, 0,
502 	    ACPI_GAS_ACCESS_WIDTH_WORD, PM1A_EVT_ADDR);
503 	basl_fill_gas(&fadt.XPm1bEventBlock, ACPI_ADR_SPACE_SYSTEM_IO, 0, 0,
504 	    ACPI_GAS_ACCESS_WIDTH_UNDEFINED, 0);
505 	basl_fill_gas(&fadt.XPm1aControlBlock, ACPI_ADR_SPACE_SYSTEM_IO, 0x10,
506 	    0, ACPI_GAS_ACCESS_WIDTH_WORD, PM1A_CNT_ADDR);
507 	basl_fill_gas(&fadt.XPm1bControlBlock, ACPI_ADR_SPACE_SYSTEM_IO, 0, 0,
508 	    ACPI_GAS_ACCESS_WIDTH_UNDEFINED, 0);
509 	basl_fill_gas(&fadt.XPm2ControlBlock, ACPI_ADR_SPACE_SYSTEM_IO, 8, 0,
510 	    ACPI_GAS_ACCESS_WIDTH_UNDEFINED, 0);
511 	basl_fill_gas(&fadt.XPmTimerBlock, ACPI_ADR_SPACE_SYSTEM_IO, 0x20, 0,
512 	    ACPI_GAS_ACCESS_WIDTH_DWORD, IO_PMTMR);
513 	basl_fill_gas(&fadt.XGpe0Block, ACPI_ADR_SPACE_SYSTEM_IO,
514 	    IO_GPE0_LEN * 8, 0, ACPI_GAS_ACCESS_WIDTH_BYTE, IO_GPE0_BLK);
515 	basl_fill_gas(&fadt.XGpe1Block, ACPI_ADR_SPACE_SYSTEM_IO, 0, 0,
516 	    ACPI_GAS_ACCESS_WIDTH_UNDEFINED, 0);
517 	basl_fill_gas(&fadt.SleepControl, ACPI_ADR_SPACE_SYSTEM_IO, 8, 0,
518 	    ACPI_GAS_ACCESS_WIDTH_BYTE, 0);
519 	basl_fill_gas(&fadt.SleepStatus, ACPI_ADR_SPACE_SYSTEM_IO, 8, 0,
520 	    ACPI_GAS_ACCESS_WIDTH_BYTE, 0);
521 	BASL_EXEC(basl_table_append_content(table, &fadt, sizeof(fadt)));
522 
523 	BASL_EXEC(basl_table_add_pointer(table, ACPI_SIG_FACS,
524 	    offsetof(ACPI_TABLE_FADT, Facs), sizeof(fadt.Facs)));
525 	BASL_EXEC(basl_table_add_pointer(table, ACPI_SIG_DSDT,
526 	    offsetof(ACPI_TABLE_FADT, Dsdt), sizeof(fadt.Dsdt)));
527 	BASL_EXEC(basl_table_add_pointer(table, ACPI_SIG_FACS,
528 	    offsetof(ACPI_TABLE_FADT, XFacs), sizeof(fadt.XFacs)));
529 	BASL_EXEC(basl_table_add_pointer(table, ACPI_SIG_DSDT,
530 	    offsetof(ACPI_TABLE_FADT, XDsdt), sizeof(fadt.XDsdt)));
531 
532 	BASL_EXEC(basl_table_register_to_rsdt(table));
533 
534 	return (0);
535 }
536 
537 static int
538 build_hpet(struct vmctx *const ctx)
539 {
540 	ACPI_TABLE_HPET hpet;
541 	struct basl_table *table;
542 
543 	BASL_EXEC(basl_table_create(&table, ctx, ACPI_SIG_HPET,
544 	    BASL_TABLE_ALIGNMENT));
545 
546 	memset(&hpet, 0, sizeof(hpet));
547 	BASL_EXEC(basl_table_append_header(table, ACPI_SIG_HPET, 1, 1));
548 	hpet.Id = htole32(hpet_capabilities);
549 	basl_fill_gas(&hpet.Address, ACPI_ADR_SPACE_SYSTEM_MEMORY, 0, 0,
550 	    ACPI_GAS_ACCESS_WIDTH_LEGACY, BHYVE_ADDRESS_HPET);
551 	hpet.Flags = ACPI_HPET_PAGE_PROTECT4;
552 	BASL_EXEC(basl_table_append_content(table, &hpet, sizeof(hpet)));
553 
554 	BASL_EXEC(basl_table_register_to_rsdt(table));
555 
556 	return (0);
557 }
558 
559 static int
560 build_madt(struct vmctx *const ctx)
561 {
562 	ACPI_TABLE_MADT madt;
563 	ACPI_MADT_LOCAL_APIC madt_lapic;
564 	ACPI_MADT_IO_APIC madt_ioapic;
565 	ACPI_MADT_INTERRUPT_OVERRIDE madt_irq_override;
566 	ACPI_MADT_LOCAL_APIC_NMI madt_lapic_nmi;
567 	struct basl_table *table;
568 
569 	BASL_EXEC(basl_table_create(&table, ctx, ACPI_SIG_MADT,
570 	    BASL_TABLE_ALIGNMENT));
571 
572 	memset(&madt, 0, sizeof(madt));
573 	BASL_EXEC(basl_table_append_header(table, ACPI_SIG_MADT, 1, 1));
574 	madt.Address = htole32(BHYVE_ADDRESS_LAPIC);
575 	madt.Flags = htole32(ACPI_MADT_PCAT_COMPAT);
576 	BASL_EXEC(basl_table_append_content(table, &madt, sizeof(madt)));
577 
578 	/* Local APIC for each CPU */
579 	for (int i = 0; i < basl_ncpu; ++i) {
580 		memset(&madt_lapic, 0, sizeof(madt_lapic));
581 		madt_lapic.Header.Type = ACPI_MADT_TYPE_LOCAL_APIC;
582 		madt_lapic.Header.Length = sizeof(madt_lapic);
583 		madt_lapic.ProcessorId = i;
584 		madt_lapic.Id = i;
585 		madt_lapic.LapicFlags = htole32(ACPI_MADT_ENABLED);
586 		BASL_EXEC(basl_table_append_bytes(table, &madt_lapic,
587 		    sizeof(madt_lapic)));
588 	}
589 
590 	/* I/O APIC */
591 	memset(&madt_ioapic, 0, sizeof(madt_ioapic));
592 	madt_ioapic.Header.Type = ACPI_MADT_TYPE_IO_APIC;
593 	madt_ioapic.Header.Length = sizeof(madt_ioapic);
594 	madt_ioapic.Address = htole32(BHYVE_ADDRESS_IOAPIC);
595 	BASL_EXEC(
596 	    basl_table_append_bytes(table, &madt_ioapic, sizeof(madt_ioapic)));
597 
598 	/* Legacy IRQ0 is connected to pin 2 of the I/O APIC */
599 	memset(&madt_irq_override, 0, sizeof(madt_irq_override));
600 	madt_irq_override.Header.Type = ACPI_MADT_TYPE_INTERRUPT_OVERRIDE;
601 	madt_irq_override.Header.Length = sizeof(madt_irq_override);
602 	madt_irq_override.GlobalIrq = htole32(2);
603 	madt_irq_override.IntiFlags = htole16(
604 	    ACPI_MADT_POLARITY_ACTIVE_HIGH | ACPI_MADT_TRIGGER_EDGE);
605 	BASL_EXEC(basl_table_append_bytes(table, &madt_irq_override,
606 	    sizeof(madt_irq_override)));
607 
608 	memset(&madt_irq_override, 0, sizeof(madt_irq_override));
609 	madt_irq_override.Header.Type = ACPI_MADT_TYPE_INTERRUPT_OVERRIDE;
610 	madt_irq_override.Header.Length = sizeof(madt_irq_override);
611 	madt_irq_override.SourceIrq = SCI_INT;
612 	madt_irq_override.GlobalIrq = htole32(SCI_INT);
613 	madt_irq_override.IntiFlags = htole16(
614 	    ACPI_MADT_POLARITY_ACTIVE_LOW | ACPI_MADT_TRIGGER_LEVEL);
615 	BASL_EXEC(basl_table_append_bytes(table, &madt_irq_override,
616 	    sizeof(madt_irq_override)));
617 
618 	/* Local APIC NMI is conntected to LINT 1 on all CPUs */
619 	memset(&madt_lapic_nmi, 0, sizeof(madt_lapic_nmi));
620 	madt_lapic_nmi.Header.Type = ACPI_MADT_TYPE_LOCAL_APIC_NMI;
621 	madt_lapic_nmi.Header.Length = sizeof(madt_lapic_nmi);
622 	madt_lapic_nmi.ProcessorId = 0xFF;
623 	madt_lapic_nmi.IntiFlags = htole16(
624 	    ACPI_MADT_POLARITY_ACTIVE_HIGH | ACPI_MADT_TRIGGER_EDGE);
625 	madt_lapic_nmi.Lint = 1;
626 	BASL_EXEC(basl_table_append_bytes(table, &madt_lapic_nmi,
627 	    sizeof(madt_lapic_nmi)));
628 
629 	BASL_EXEC(basl_table_register_to_rsdt(table));
630 
631 	return (0);
632 }
633 
634 static int
635 build_mcfg(struct vmctx *const ctx)
636 {
637 	ACPI_TABLE_MCFG mcfg;
638 	ACPI_MCFG_ALLOCATION mcfg_allocation;
639 	struct basl_table *table;
640 
641 	BASL_EXEC(basl_table_create(&table, ctx, ACPI_SIG_MCFG,
642 	    BASL_TABLE_ALIGNMENT));
643 
644 	memset(&mcfg, 0, sizeof(mcfg));
645 	BASL_EXEC(basl_table_append_header(table, ACPI_SIG_MCFG, 1, 1));
646 	BASL_EXEC(basl_table_append_content(table, &mcfg, sizeof(mcfg)));
647 
648 	memset(&mcfg_allocation, 0, sizeof(mcfg_allocation));
649 	mcfg_allocation.Address = htole64(pci_ecfg_base());
650 	mcfg_allocation.EndBusNumber = 0xFF;
651 	BASL_EXEC(basl_table_append_bytes(table, &mcfg_allocation,
652 	    sizeof(mcfg_allocation)));
653 
654 	BASL_EXEC(basl_table_register_to_rsdt(table));
655 
656 	return (0);
657 }
658 
659 static int
660 build_rsdp(struct vmctx *const ctx)
661 {
662 	ACPI_TABLE_RSDP rsdp;
663 	struct basl_table *table;
664 
665 	BASL_EXEC(basl_table_create(&table, ctx, ACPI_RSDP_NAME,
666 	    BASL_TABLE_ALIGNMENT));
667 
668 	memset(&rsdp, 0, sizeof(rsdp));
669 	memcpy(rsdp.Signature, ACPI_SIG_RSDP, 8);
670 	rsdp.Checksum = 0; /* patched by basl */
671 	memcpy(rsdp.OemId, "BHYVE ", ACPI_OEM_ID_SIZE);
672 	rsdp.Revision = 2;
673 	rsdp.RsdtPhysicalAddress = htole32(0); /* patched by basl */
674 	rsdp.Length = htole32(0);	       /* patched by basl */
675 	rsdp.XsdtPhysicalAddress = htole64(0); /* patched by basl */
676 	rsdp.ExtendedChecksum = 0;	       /* patched by basl */
677 	BASL_EXEC(basl_table_append_bytes(table, &rsdp, sizeof(rsdp)));
678 
679 	BASL_EXEC(basl_table_add_checksum(table,
680 	    offsetof(ACPI_TABLE_RSDP, Checksum), 0, 20));
681 	BASL_EXEC(basl_table_add_pointer(table, ACPI_SIG_RSDT,
682 	    offsetof(ACPI_TABLE_RSDP, RsdtPhysicalAddress),
683 	    sizeof(rsdp.RsdtPhysicalAddress)));
684 	BASL_EXEC(basl_table_add_length(table,
685 	    offsetof(ACPI_TABLE_RSDP, Length), sizeof(rsdp.Length)));
686 	BASL_EXEC(basl_table_add_pointer(table, ACPI_SIG_XSDT,
687 	    offsetof(ACPI_TABLE_RSDP, XsdtPhysicalAddress),
688 	    sizeof(rsdp.XsdtPhysicalAddress)));
689 	BASL_EXEC(basl_table_add_checksum(table,
690 	    offsetof(ACPI_TABLE_RSDP, ExtendedChecksum), 0,
691 	    BASL_TABLE_CHECKSUM_LEN_FULL_TABLE));
692 
693 	return (0);
694 }
695 
696 static int
697 build_spcr(struct vmctx *const ctx)
698 {
699 	ACPI_TABLE_SPCR spcr;
700 	struct basl_table *table;
701 
702 	BASL_EXEC(basl_table_create(&table, ctx, ACPI_SIG_SPCR,
703 	    BASL_TABLE_ALIGNMENT));
704 
705 	memset(&spcr, 0, sizeof(spcr));
706 	BASL_EXEC(basl_table_append_header(table, ACPI_SIG_SPCR, 1, 1));
707 	spcr.InterfaceType = ACPI_DBG2_16550_COMPATIBLE;
708 	basl_fill_gas(&spcr.SerialPort, ACPI_ADR_SPACE_SYSTEM_IO, 8, 0,
709 	    ACPI_GAS_ACCESS_WIDTH_LEGACY, 0x3F8);
710 	spcr.InterruptType = ACPI_SPCR_INTERRUPT_TYPE_8259;
711 	spcr.PcInterrupt = 4;
712 	spcr.BaudRate = ACPI_SPCR_BAUD_RATE_115200;
713 	spcr.Parity = ACPI_SPCR_PARITY_NO_PARITY;
714 	spcr.StopBits = ACPI_SPCR_STOP_BITS_1;
715 	spcr.FlowControl = 3; /* RTS/CTS | DCD */
716 	spcr.TerminalType = ACPI_SPCR_TERMINAL_TYPE_VT_UTF8;
717 	BASL_EXEC(basl_table_append_content(table, &spcr, sizeof(spcr)));
718 
719 	BASL_EXEC(basl_table_register_to_rsdt(table));
720 
721 	return (0);
722 }
723 
724 int
725 acpi_build(struct vmctx *ctx, int ncpu)
726 {
727 	int err;
728 
729 	basl_ncpu = ncpu;
730 
731 	err = vm_get_hpet_capabilities(ctx, &hpet_capabilities);
732 	if (err != 0)
733 		return (err);
734 
735 	/*
736 	 * For debug, allow the user to have iasl compiler output sent
737 	 * to stdout rather than /dev/null
738 	 */
739 	if (getenv("BHYVE_ACPI_VERBOSE_IASL"))
740 		basl_verbose_iasl = 1;
741 
742 	/*
743 	 * Allow the user to keep the generated ASL files for debugging
744 	 * instead of deleting them following use
745 	 */
746 	if (getenv("BHYVE_ACPI_KEEPTMPS"))
747 		basl_keep_temps = 1;
748 
749 	BASL_EXEC(basl_init(ctx));
750 
751 	BASL_EXEC(basl_make_templates());
752 
753 	/*
754 	 * Generate ACPI tables and copy them into guest memory.
755 	 *
756 	 * According to UEFI Specification v6.3 chapter 5.1 the FADT should be
757 	 * the first table pointed to by XSDT. For that reason, build it as the
758 	 * first table after XSDT.
759 	 */
760 	BASL_EXEC(build_rsdp(ctx));
761 	BASL_EXEC(build_fadt(ctx));
762 	BASL_EXEC(build_madt(ctx));
763 	BASL_EXEC(build_hpet(ctx));
764 	BASL_EXEC(build_mcfg(ctx));
765 	BASL_EXEC(build_facs(ctx));
766 	BASL_EXEC(build_spcr(ctx));
767 
768 	/* Build ACPI device-specific tables such as a TPM2 table. */
769 	const struct acpi_device_list_entry *entry;
770 	SLIST_FOREACH(entry, &acpi_devices, chain) {
771 		BASL_EXEC(acpi_device_build_table(entry->dev));
772 	}
773 
774 	BASL_EXEC(build_dsdt(ctx));
775 
776 	BASL_EXEC(basl_finish());
777 
778 	return (0);
779 }
780