xref: /illumos-gate/usr/src/cmd/bhyve/common/basl.h (revision 5c4a5fe16715fb423db76577a6883b5bbecdbe45)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2022 Beckhoff Automation GmbH & Co. KG
5  */
6 
7 #pragma once
8 
9 #pragma GCC diagnostic push
10 #pragma GCC diagnostic ignored "-Wunused-parameter"
11 #include <contrib/dev/acpica/include/acpi.h>
12 #pragma GCC diagnostic pop
13 
14 #ifndef	__FreeBSD__
15 /* Until the in-gate ACPI is updated, map the new name to the old. */
16 #define	ACPI_NAMESEG_SIZE	ACPI_NAME_SIZE
17 /*
18  * Also to avoid type conflicts without having to pepper the code with ifdefs,
19  * redefine these constants to be uint8_t *
20  */
21 #undef ACPI_SIG_FACS
22 #undef ACPI_SIG_MCFG
23 #undef ACPI_SIG_HPET
24 #undef ACPI_SIG_MADT
25 #undef ACPI_SIG_DSDT
26 #undef ACPI_SIG_FADT
27 #undef ACPI_SIG_XSDT
28 #undef ACPI_SIG_RSDT
29 #undef ACPI_RSDP_NAME
30 #undef ACPI_SIG_SPCR
31 #undef ACPI_SIG_TPM2
32 #define	ACPI_SIG_FACS           (const uint8_t *)"FACS"
33 #define	ACPI_SIG_MCFG		(const uint8_t *)"MCFG"
34 #define	ACPI_SIG_HPET		(const uint8_t *)"HPET"
35 #define	ACPI_SIG_MADT		(const uint8_t *)"APIC"
36 #define	ACPI_SIG_DSDT		(const uint8_t *)"DSDT"
37 #define	ACPI_SIG_FADT		(const uint8_t *)"FACP"
38 #define	ACPI_SIG_XSDT		(const uint8_t *)"XSDT"
39 #define	ACPI_SIG_RSDT		(const uint8_t *)"RSDT"
40 #define	ACPI_RSDP_NAME		(const uint8_t *)"RSDP"
41 #define	ACPI_SIG_SPCR		(const uint8_t *)"SPCR"
42 #define	ACPI_SIG_TPM2		(const uint8_t *)"TPM2"
43 #endif /* !__FreeBSD__ */
44 
45 #include "qemu_fwcfg.h"
46 
47 #define ACPI_GAS_ACCESS_WIDTH_LEGACY 0
48 #define ACPI_GAS_ACCESS_WIDTH_UNDEFINED 0
49 #define ACPI_GAS_ACCESS_WIDTH_BYTE 1
50 #define ACPI_GAS_ACCESS_WIDTH_WORD 2
51 #define ACPI_GAS_ACCESS_WIDTH_DWORD 3
52 #define ACPI_GAS_ACCESS_WIDTH_QWORD 4
53 
54 #define ACPI_SPCR_INTERRUPT_TYPE_8259 0x1
55 #define ACPI_SPCR_INTERRUPT_TYPE_APIC 0x2
56 #define ACPI_SPCR_INTERRUPT_TYPE_SAPIC 0x4
57 #define ACPI_SPCR_INTERRUPT_TYPE_GIC 0x8
58 
59 #define ACPI_SPCR_BAUD_RATE_9600 3
60 #define ACPI_SPCR_BAUD_RATE_19200 4
61 #define ACPI_SPCR_BAUD_RATE_57600 6
62 #define ACPI_SPCR_BAUD_RATE_115200 7
63 
64 #define ACPI_SPCR_PARITY_NO_PARITY 0
65 
66 #define ACPI_SPCR_STOP_BITS_1 1
67 
68 #define ACPI_SPCR_FLOW_CONTROL_DCD 0x1
69 #define ACPI_SPCR_FLOW_CONTROL_RTS_CTS 0x2
70 #define ACPI_SPCR_FLOW_CONTROL_XON_XOFF 0x4
71 
72 #define ACPI_SPCR_TERMINAL_TYPE_VT100 0
73 #define ACPI_SPCR_TERMINAL_TYPE_VT100_PLUS 1
74 #define ACPI_SPCR_TERMINAL_TYPE_VT_UTF8 2
75 #define ACPI_SPCR_TERMINAL_TYPE_ANSI 3
76 
77 #define BHYVE_ACPI_BASE 0xf2400
78 
79 #define BASL_TABLE_ALIGNMENT 0x10
80 #define BASL_TABLE_ALIGNMENT_FACS 0x40
81 
82 #define BASL_TABLE_CHECKSUM_LEN_FULL_TABLE (-1U)
83 
84 #define BASL_EXEC(x)                                                         \
85 	do {                                                                 \
86 		const int error = (x);                                       \
87 		if (error) {                                                 \
88 			warnc(error,                                         \
89 			    "BASL failed @ %s:%d\n    Failed to execute %s", \
90 			    __func__, __LINE__, #x);                         \
91 			return (error);                                      \
92 		}                                                            \
93 	} while (0)
94 
95 struct basl_table;
96 
97 void basl_fill_gas(ACPI_GENERIC_ADDRESS *gas, uint8_t space_id,
98     uint8_t bit_width, uint8_t bit_offset, uint8_t access_width,
99     uint64_t address);
100 int basl_finish(void);
101 int basl_init(struct vmctx *ctx);
102 int basl_table_add_checksum(struct basl_table *const table, const uint32_t off,
103     const uint32_t start, const uint32_t len);
104 int basl_table_add_length(struct basl_table *const table, const uint32_t off,
105     const uint8_t size);
106 int basl_table_add_pointer(struct basl_table *const table,
107     const uint8_t src_signature[ACPI_NAMESEG_SIZE], const uint32_t off,
108     const uint8_t size);
109 int basl_table_append_bytes(struct basl_table *table, const void *bytes,
110     uint32_t len);
111 int basl_table_append_checksum(struct basl_table *table, uint32_t start,
112     uint32_t len);
113 /* Add an ACPI_TABLE_* to basl without its header. */
114 int basl_table_append_content(struct basl_table *table, void *data,
115     uint32_t len);
116 int basl_table_append_fwcfg(struct basl_table *table,
117     const uint8_t *fwcfg_name, uint32_t alignment,
118     uint8_t size);
119 int basl_table_append_gas(struct basl_table *table, uint8_t space_id,
120     uint8_t bit_width, uint8_t bit_offset, uint8_t access_width,
121     uint64_t address);
122 int basl_table_append_header(struct basl_table *table,
123     const uint8_t signature[ACPI_NAMESEG_SIZE], uint8_t revision,
124     uint32_t oem_revision);
125 int basl_table_append_int(struct basl_table *table, uint64_t val, uint8_t size);
126 int basl_table_append_length(struct basl_table *table, uint8_t size);
127 int basl_table_append_pointer(struct basl_table *table,
128     const uint8_t src_signature[ACPI_NAMESEG_SIZE], uint8_t size);
129 int basl_table_create(struct basl_table **table, struct vmctx *ctx,
130     const uint8_t *name, uint32_t alignment);
131 /* Adds the table to RSDT and XSDT */
132 int basl_table_register_to_rsdt(struct basl_table *table);
133