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