xref: /freebsd/sys/amd64/acpica/acpi_machdep.c (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2001 Mitsuru IWASAKI
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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR 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 
29 #include <sys/param.h>
30 #include <sys/bus.h>
31 #include <sys/kernel.h>
32 #include <sys/module.h>
33 #include <sys/sysctl.h>
34 
35 #include <vm/vm.h>
36 #include <vm/pmap.h>
37 
38 #include <contrib/dev/acpica/include/acpi.h>
39 #include <contrib/dev/acpica/include/accommon.h>
40 #include <contrib/dev/acpica/include/actables.h>
41 
42 #include <dev/acpica/acpivar.h>
43 
44 #include <machine/nexusvar.h>
45 
46 int acpi_resume_beep;
47 SYSCTL_INT(_debug_acpi, OID_AUTO, resume_beep, CTLFLAG_RWTUN,
48     &acpi_resume_beep, 0, "Beep the PC speaker when resuming");
49 
50 int acpi_reset_video;
51 TUNABLE_INT("hw.acpi.reset_video", &acpi_reset_video);
52 
53 static int intr_model = ACPI_INTR_PIC;
54 
55 int
56 acpi_machdep_init(device_t dev)
57 {
58 	struct acpi_softc *sc;
59 
60 	sc = device_get_softc(dev);
61 
62 	acpi_apm_init(sc);
63 	acpi_install_wakeup_handler(sc);
64 
65 	if (intr_model != ACPI_INTR_PIC)
66 		acpi_SetIntrModel(intr_model);
67 
68 	SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx,
69 	    SYSCTL_CHILDREN(sc->acpi_sysctl_tree), OID_AUTO,
70 	    "reset_video", CTLFLAG_RW, &acpi_reset_video, 0,
71 	    "Call the VESA reset BIOS vector on the resume path");
72 
73 	return (0);
74 }
75 
76 void
77 acpi_SetDefaultIntrModel(int model)
78 {
79 
80 	intr_model = model;
81 }
82 
83 int
84 acpi_machdep_quirks(int *quirks)
85 {
86 
87 	return (0);
88 }
89 
90 /*
91  * Map a table.  First map the header to determine the table length and then map
92  * the entire table.
93  */
94 static void *
95 map_table(vm_paddr_t pa, const char *sig)
96 {
97 	ACPI_TABLE_HEADER *header;
98 	vm_size_t length;
99 	void *table;
100 
101 	header = pmap_mapbios(pa, sizeof(ACPI_TABLE_HEADER));
102 	if (strncmp(header->Signature, sig, ACPI_NAMESEG_SIZE) != 0) {
103 		pmap_unmapbios(header, sizeof(ACPI_TABLE_HEADER));
104 		return (NULL);
105 	}
106 	length = header->Length;
107 	pmap_unmapbios(header, sizeof(ACPI_TABLE_HEADER));
108 	table = pmap_mapbios(pa, length);
109 	if (ACPI_FAILURE(AcpiUtChecksum(table, length))) {
110 		if (bootverbose)
111 			printf("ACPI: Failed checksum for table %s\n", sig);
112 #if (ACPI_CHECKSUM_ABORT)
113 		pmap_unmapbios(table, length);
114 		return (NULL);
115 #endif
116 	}
117 	return (table);
118 }
119 
120 /*
121  * See if a given ACPI table is the requested table.  Returns the
122  * length of the table if it matches or zero on failure.
123  */
124 static int
125 probe_table(vm_paddr_t address, const char *sig)
126 {
127 	ACPI_TABLE_HEADER *table;
128 	int ret;
129 
130 	table = pmap_mapbios(address, sizeof(ACPI_TABLE_HEADER));
131 	ret = strncmp(table->Signature, sig, ACPI_NAMESEG_SIZE) == 0;
132 	pmap_unmapbios(table, sizeof(ACPI_TABLE_HEADER));
133 	return (ret);
134 }
135 
136 /*
137  * Try to map a table at a given physical address previously returned
138  * by acpi_find_table().
139  */
140 void *
141 acpi_map_table(vm_paddr_t pa, const char *sig)
142 {
143 
144 	return (map_table(pa, sig));
145 }
146 
147 /* Unmap a table previously mapped via acpi_map_table(). */
148 void
149 acpi_unmap_table(void *table)
150 {
151 	ACPI_TABLE_HEADER *header;
152 
153 	header = (ACPI_TABLE_HEADER *)table;
154 	pmap_unmapbios(table, header->Length);
155 }
156 
157 /*
158  * Return the physical address of the requested table or zero if one
159  * is not found.
160  */
161 vm_paddr_t
162 acpi_find_table(const char *sig)
163 {
164 	ACPI_PHYSICAL_ADDRESS rsdp_ptr;
165 	ACPI_TABLE_RSDP *rsdp;
166 	ACPI_TABLE_RSDT *rsdt;
167 	ACPI_TABLE_XSDT *xsdt;
168 	ACPI_TABLE_HEADER *table;
169 	vm_paddr_t addr;
170 	int i, count;
171 
172 	if (resource_disabled("acpi", 0))
173 		return (0);
174 
175 	/*
176 	 * Map in the RSDP.  Since ACPI uses AcpiOsMapMemory() which in turn
177 	 * calls pmap_mapbios() to find the RSDP, we assume that we can use
178 	 * pmap_mapbios() to map the RSDP.
179 	 */
180 	if ((rsdp_ptr = AcpiOsGetRootPointer()) == 0)
181 		return (0);
182 	rsdp = pmap_mapbios(rsdp_ptr, sizeof(ACPI_TABLE_RSDP));
183 	if (rsdp == NULL) {
184 		printf("ACPI: Failed to map RSDP\n");
185 		return (0);
186 	}
187 
188 	/*
189 	 * For ACPI >= 2.0, use the XSDT if it is available.
190 	 * Otherwise, use the RSDT.
191 	 */
192 	addr = 0;
193 	if (rsdp->Revision >= 2 && rsdp->XsdtPhysicalAddress != 0) {
194 		/*
195 		 * AcpiOsGetRootPointer only verifies the checksum for
196 		 * the version 1.0 portion of the RSDP.  Version 2.0 has
197 		 * an additional checksum that we verify first.
198 		 */
199 		if (AcpiUtChecksum((UINT8 *)rsdp, ACPI_RSDP_XCHECKSUM_LENGTH)) {
200 			printf("ACPI: RSDP failed extended checksum\n");
201 			pmap_unmapbios(rsdp, sizeof(ACPI_TABLE_RSDP));
202 			return (0);
203 		}
204 		xsdt = map_table(rsdp->XsdtPhysicalAddress, ACPI_SIG_XSDT);
205 		if (xsdt == NULL) {
206 			printf("ACPI: Failed to map XSDT\n");
207 			pmap_unmapbios(rsdp, sizeof(ACPI_TABLE_RSDP));
208 			return (0);
209 		}
210 		count = (xsdt->Header.Length - sizeof(ACPI_TABLE_HEADER)) /
211 		    sizeof(UINT64);
212 		for (i = 0; i < count; i++)
213 			if (probe_table(xsdt->TableOffsetEntry[i], sig)) {
214 				addr = xsdt->TableOffsetEntry[i];
215 				break;
216 			}
217 		acpi_unmap_table(xsdt);
218 	} else {
219 		rsdt = map_table(rsdp->RsdtPhysicalAddress, ACPI_SIG_RSDT);
220 		if (rsdt == NULL) {
221 			printf("ACPI: Failed to map RSDT\n");
222 			pmap_unmapbios(rsdp, sizeof(ACPI_TABLE_RSDP));
223 			return (0);
224 		}
225 		count = (rsdt->Header.Length - sizeof(ACPI_TABLE_HEADER)) /
226 		    sizeof(UINT32);
227 		for (i = 0; i < count; i++)
228 			if (probe_table(rsdt->TableOffsetEntry[i], sig)) {
229 				addr = rsdt->TableOffsetEntry[i];
230 				break;
231 			}
232 		acpi_unmap_table(rsdt);
233 	}
234 	pmap_unmapbios(rsdp, sizeof(ACPI_TABLE_RSDP));
235 	if (addr == 0)
236 		return (0);
237 
238 	/*
239 	 * Verify that we can map the full table and that its checksum is
240 	 * correct, etc.
241 	 */
242 	table = map_table(addr, sig);
243 	if (table == NULL)
244 		return (0);
245 	acpi_unmap_table(table);
246 
247 	return (addr);
248 }
249 
250 /*
251  * ACPI nexus(4) driver.
252  */
253 static int
254 nexus_acpi_probe(device_t dev)
255 {
256 	int error;
257 
258 	error = acpi_identify();
259 	if (error)
260 		return (error);
261 	device_quiet(dev);
262 	return (BUS_PROBE_DEFAULT);
263 }
264 
265 static int
266 nexus_acpi_attach(device_t dev)
267 {
268 
269 	nexus_init_resources();
270 	bus_generic_probe(dev);
271 	if (BUS_ADD_CHILD(dev, 10, "acpi", 0) == NULL)
272 		panic("failed to add acpi0 device");
273 
274 	return (bus_generic_attach(dev));
275 }
276 
277 static device_method_t nexus_acpi_methods[] = {
278 	/* Device interface */
279 	DEVMETHOD(device_probe,		nexus_acpi_probe),
280 	DEVMETHOD(device_attach,	nexus_acpi_attach),
281 	{ 0, 0 }
282 };
283 
284 DEFINE_CLASS_1(nexus, nexus_acpi_driver, nexus_acpi_methods, 1, nexus_driver);
285 
286 DRIVER_MODULE(nexus_acpi, root, nexus_acpi_driver, 0, 0);
287