1 /*-
2  * Copyright (c) 2012 NetApp, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #ifndef _ACPI_H_
30 #define _ACPI_H_
31 
32 #include "hsm_ioctl_defs.h"
33 
34 #define	SCI_INT			9
35 
36 #define	SMI_CMD			0xb2
37 #define	ACPI_ENABLE		0xa0
38 #define	ACPI_DISABLE		0xa1
39 
40 #define	PM1A_EVT_ADDR		0x400
41 
42 #define	IO_PMTMR		0x0	/* PM Timer is disabled in ACPI */
43 
44 #define ACPI_MADT_TYPE_LOCAL_APIC   0U
45 #define ACPI_MADT_TYPE_IOAPIC       1U
46 #define ACPI_MADT_ENABLED           1U
47 #define ACPI_MADT_TYPE_LOCAL_APIC_NMI 4U
48 
49 struct acpi_table_hdr {
50 	/* ASCII table signature */
51 	char                    signature[4];
52 	/* Length of table in bytes, including this header */
53 	uint32_t                length;
54 	/* ACPI Specification minor version number */
55 	uint8_t                 revision;
56 	/* To make sum of entire table == 0 */
57 	uint8_t                 checksum;
58 	/* ASCII OEM identification */
59 	char                    oem_id[6];
60 	/* ASCII OEM table identification */
61 	char                    oem_table_id[8];
62 	/* OEM revision number */
63 	uint32_t                oem_revision;
64 	/* ASCII ASL compiler vendor ID */
65 	char                    asl_compiler_id[4];
66 	/* ASL compiler version */
67 	uint32_t                asl_compiler_revision;
68 } __attribute__((packed));
69 
70 struct acpi_table_madt {
71 	/* Common ACPI table header */
72 	struct acpi_table_hdr header;
73 	/* Physical address of local APIC */
74 	uint32_t                     address;
75 	uint32_t                     flags;
76 } __packed;
77 
78 struct acpi_subtable_header {
79 	uint8_t                   type;
80 	uint8_t                   length;
81 } __packed;
82 
83 struct acpi_madt_local_apic {
84 	struct acpi_subtable_header    header;
85 	/* ACPI processor id */
86 	uint8_t                        processor_id;
87 	/* Processor's local APIC id */
88 	uint8_t                        id;
89 	uint32_t                       lapic_flags;
90 } __packed;
91 
92 
93 /* All dynamic table entry no. */
94 #define NHLT_ENTRY_NO		8
95 
96 #define EFPRINTF(...) fprintf(__VA_ARGS__)
97 #define EFFLUSH(x) fflush(x)
98 
99 void acpi_table_enable(int num);
100 uint32_t get_acpi_base(void);
101 uint32_t get_acpi_table_length(void);
102 uint32_t get_acpi_wakingvector_offset(void);
103 uint32_t get_acpi_wakingvector_length(void);
104 
105 struct vmctx;
106 
107 int	acpi_build(struct vmctx *ctx, int ncpu);
108 void	dsdt_line(const char *fmt, ...);
109 void	dsdt_fixed_ioport(uint16_t iobase, uint16_t length);
110 void	dsdt_fixed_irq(uint8_t irq);
111 void	dsdt_fixed_mem32(uint32_t base, uint32_t length);
112 void	dsdt_indent(int levels);
113 void	dsdt_unindent(int levels);
114 void	sci_init(struct vmctx *ctx);
115 void	pm_write_dsdt(struct vmctx *ctx, int ncpu);
116 void	pm_backto_wakeup(struct vmctx *ctx);
117 void	inject_power_button_event(struct vmctx *ctx);
118 void	power_button_init(struct vmctx *ctx);
119 void	power_button_deinit(struct vmctx *ctx);
120 
121 int pcpuid_from_vcpuid(uint64_t guest_pcpu_bitmask, int vcpu_id);
122 int lapicid_from_pcpuid(int pcpu_id);
123 int lapic_to_pcpu(int lapic);
124 
125 int parse_madt(void);
126 int acrn_parse_iasl(char *arg);
127 int get_iasl_compiler(void);
128 int check_iasl_version(void);
129 
130 void osc_write_ospm_dsdt(struct vmctx *ctx, int ncpu);
131 
132 #endif /* _ACPI_H_ */
133