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