xref: /freebsd/sys/amd64/acpica/acpi_machdep.c (revision 884a2a699669ec61e2366e3e358342dbc94be24a)
1 /*-
2  * Copyright (c) 2001 Mitsuru IWASAKI
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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR 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 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/sysctl.h>
35 
36 #include <vm/vm.h>
37 #include <vm/pmap.h>
38 
39 #include <contrib/dev/acpica/include/acpi.h>
40 #include <contrib/dev/acpica/include/accommon.h>
41 #include <contrib/dev/acpica/include/actables.h>
42 
43 #include <dev/acpica/acpivar.h>
44 
45 #include <machine/nexusvar.h>
46 
47 SYSCTL_DECL(_debug_acpi);
48 
49 int acpi_resume_beep;
50 TUNABLE_INT("debug.acpi.resume_beep", &acpi_resume_beep);
51 SYSCTL_INT(_debug_acpi, OID_AUTO, resume_beep, CTLFLAG_RW, &acpi_resume_beep,
52     0, "Beep the PC speaker when resuming");
53 
54 int acpi_reset_video;
55 TUNABLE_INT("hw.acpi.reset_video", &acpi_reset_video);
56 
57 static int intr_model = ACPI_INTR_PIC;
58 
59 int
60 acpi_machdep_init(device_t dev)
61 {
62 	struct acpi_softc *sc;
63 
64 	sc = device_get_softc(dev);
65 
66 	acpi_apm_init(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 void
94 acpi_cpu_c1()
95 {
96 
97 	__asm __volatile("sti; hlt");
98 }
99 
100 /*
101  * Support for mapping ACPI tables during early boot.  Currently this
102  * uses the crashdump map to map each table.  However, the crashdump
103  * map is created in pmap_bootstrap() right after the direct map, so
104  * we should be able to just use pmap_mapbios() here instead.
105  *
106  * This makes the following assumptions about how we use this KVA:
107  * pages 0 and 1 are used to map in the header of each table found via
108  * the RSDT or XSDT and pages 2 to n are used to map in the RSDT or
109  * XSDT.  This has to use 2 pages for the table headers in case a
110  * header spans a page boundary.
111  *
112  * XXX: We don't ensure the table fits in the available address space
113  * in the crashdump map.
114  */
115 
116 /*
117  * Map some memory using the crashdump map.  'offset' is an offset in
118  * pages into the crashdump map to use for the start of the mapping.
119  */
120 static void *
121 table_map(vm_paddr_t pa, int offset, vm_offset_t length)
122 {
123 	vm_offset_t va, off;
124 	void *data;
125 
126 	off = pa & PAGE_MASK;
127 	length = roundup(length + off, PAGE_SIZE);
128 	pa = pa & PG_FRAME;
129 	va = (vm_offset_t)pmap_kenter_temporary(pa, offset) +
130 	    (offset * PAGE_SIZE);
131 	data = (void *)(va + off);
132 	length -= PAGE_SIZE;
133 	while (length > 0) {
134 		va += PAGE_SIZE;
135 		pa += PAGE_SIZE;
136 		length -= PAGE_SIZE;
137 		pmap_kenter(va, pa);
138 		invlpg(va);
139 	}
140 	return (data);
141 }
142 
143 /* Unmap memory previously mapped with table_map(). */
144 static void
145 table_unmap(void *data, vm_offset_t length)
146 {
147 	vm_offset_t va, off;
148 
149 	va = (vm_offset_t)data;
150 	off = va & PAGE_MASK;
151 	length = roundup(length + off, PAGE_SIZE);
152 	va &= ~PAGE_MASK;
153 	while (length > 0) {
154 		pmap_kremove(va);
155 		invlpg(va);
156 		va += PAGE_SIZE;
157 		length -= PAGE_SIZE;
158 	}
159 }
160 
161 /*
162  * Map a table at a given offset into the crashdump map.  It first
163  * maps the header to determine the table length and then maps the
164  * entire table.
165  */
166 static void *
167 map_table(vm_paddr_t pa, int offset, const char *sig)
168 {
169 	ACPI_TABLE_HEADER *header;
170 	vm_offset_t length;
171 	void *table;
172 
173 	header = table_map(pa, offset, sizeof(ACPI_TABLE_HEADER));
174 	if (strncmp(header->Signature, sig, ACPI_NAME_SIZE) != 0) {
175 		table_unmap(header, sizeof(ACPI_TABLE_HEADER));
176 		return (NULL);
177 	}
178 	length = header->Length;
179 	table_unmap(header, sizeof(ACPI_TABLE_HEADER));
180 	table = table_map(pa, offset, length);
181 	if (ACPI_FAILURE(AcpiTbChecksum(table, length))) {
182 		if (bootverbose)
183 			printf("ACPI: Failed checksum for table %s\n", sig);
184 #if (ACPI_CHECKSUM_ABORT)
185 		table_unmap(table, length);
186 		return (NULL);
187 #endif
188 	}
189 	return (table);
190 }
191 
192 /*
193  * See if a given ACPI table is the requested table.  Returns the
194  * length of the able if it matches or zero on failure.
195  */
196 static int
197 probe_table(vm_paddr_t address, const char *sig)
198 {
199 	ACPI_TABLE_HEADER *table;
200 
201 	table = table_map(address, 0, sizeof(ACPI_TABLE_HEADER));
202 	if (table == NULL) {
203 		if (bootverbose)
204 			printf("ACPI: Failed to map table at 0x%jx\n",
205 			    (uintmax_t)address);
206 		return (0);
207 	}
208 	if (bootverbose)
209 		printf("Table '%.4s' at 0x%jx\n", table->Signature,
210 		    (uintmax_t)address);
211 
212 	if (strncmp(table->Signature, sig, ACPI_NAME_SIZE) != 0) {
213 		table_unmap(table, sizeof(ACPI_TABLE_HEADER));
214 		return (0);
215 	}
216 	table_unmap(table, sizeof(ACPI_TABLE_HEADER));
217 	return (1);
218 }
219 
220 /*
221  * Try to map a table at a given physical address previously returned
222  * by acpi_find_table().
223  */
224 void *
225 acpi_map_table(vm_paddr_t pa, const char *sig)
226 {
227 
228 	return (map_table(pa, 0, sig));
229 }
230 
231 /* Unmap a table previously mapped via acpi_map_table(). */
232 void
233 acpi_unmap_table(void *table)
234 {
235 	ACPI_TABLE_HEADER *header;
236 
237 	header = (ACPI_TABLE_HEADER *)table;
238 	table_unmap(table, header->Length);
239 }
240 
241 /*
242  * Return the physical address of the requested table or zero if one
243  * is not found.
244  */
245 vm_paddr_t
246 acpi_find_table(const char *sig)
247 {
248 	ACPI_PHYSICAL_ADDRESS rsdp_ptr;
249 	ACPI_TABLE_RSDP *rsdp;
250 	ACPI_TABLE_RSDT *rsdt;
251 	ACPI_TABLE_XSDT *xsdt;
252 	ACPI_TABLE_HEADER *table;
253 	vm_paddr_t addr;
254 	int i, count;
255 
256 	if (resource_disabled("acpi", 0))
257 		return (0);
258 
259 	/*
260 	 * Map in the RSDP.  Since ACPI uses AcpiOsMapMemory() which in turn
261 	 * calls pmap_mapbios() to find the RSDP, we assume that we can use
262 	 * pmap_mapbios() to map the RSDP.
263 	 */
264 	if ((rsdp_ptr = AcpiOsGetRootPointer()) == 0)
265 		return (0);
266 	rsdp = pmap_mapbios(rsdp_ptr, sizeof(ACPI_TABLE_RSDP));
267 	if (rsdp == NULL) {
268 		if (bootverbose)
269 			printf("ACPI: Failed to map RSDP\n");
270 		return (0);
271 	}
272 
273 	/*
274 	 * For ACPI >= 2.0, use the XSDT if it is available.
275 	 * Otherwise, use the RSDT.  We map the XSDT or RSDT at page 2
276 	 * in the crashdump area.  Pages 0 and 1 are used to map in the
277 	 * headers of candidate ACPI tables.
278 	 */
279 	addr = 0;
280 	if (rsdp->Revision >= 2 && rsdp->XsdtPhysicalAddress != 0) {
281 		/*
282 		 * AcpiOsGetRootPointer only verifies the checksum for
283 		 * the version 1.0 portion of the RSDP.  Version 2.0 has
284 		 * an additional checksum that we verify first.
285 		 */
286 		if (AcpiTbChecksum((UINT8 *)rsdp, ACPI_RSDP_XCHECKSUM_LENGTH)) {
287 			if (bootverbose)
288 				printf("ACPI: RSDP failed extended checksum\n");
289 			return (0);
290 		}
291 		xsdt = map_table(rsdp->XsdtPhysicalAddress, 2, ACPI_SIG_XSDT);
292 		if (xsdt == NULL) {
293 			if (bootverbose)
294 				printf("ACPI: Failed to map XSDT\n");
295 			return (0);
296 		}
297 		count = (xsdt->Header.Length - sizeof(ACPI_TABLE_HEADER)) /
298 		    sizeof(UINT64);
299 		for (i = 0; i < count; i++)
300 			if (probe_table(xsdt->TableOffsetEntry[i], sig)) {
301 				addr = xsdt->TableOffsetEntry[i];
302 				break;
303 			}
304 		acpi_unmap_table(xsdt);
305 	} else {
306 		rsdt = map_table(rsdp->RsdtPhysicalAddress, 2, ACPI_SIG_RSDT);
307 		if (rsdt == NULL) {
308 			if (bootverbose)
309 				printf("ACPI: Failed to map RSDT\n");
310 			return (0);
311 		}
312 		count = (rsdt->Header.Length - sizeof(ACPI_TABLE_HEADER)) /
313 		    sizeof(UINT32);
314 		for (i = 0; i < count; i++)
315 			if (probe_table(rsdt->TableOffsetEntry[i], sig)) {
316 				addr = rsdt->TableOffsetEntry[i];
317 				break;
318 			}
319 		acpi_unmap_table(rsdt);
320 	}
321 	pmap_unmapbios((vm_offset_t)rsdp, sizeof(ACPI_TABLE_RSDP));
322 	if (addr == 0) {
323 		if (bootverbose)
324 			printf("ACPI: No %s table found\n", sig);
325 		return (0);
326 	}
327 	if (bootverbose)
328 		printf("%s: Found table at 0x%jx\n", sig, (uintmax_t)addr);
329 
330 	/*
331 	 * Verify that we can map the full table and that its checksum is
332 	 * correct, etc.
333 	 */
334 	table = map_table(addr, 0, sig);
335 	if (table == NULL)
336 		return (0);
337 	acpi_unmap_table(table);
338 
339 	return (addr);
340 }
341 
342 /*
343  * ACPI nexus(4) driver.
344  */
345 static int
346 nexus_acpi_probe(device_t dev)
347 {
348 	int error;
349 
350 	error = acpi_identify();
351 	if (error)
352 		return (error);
353 
354 	return (BUS_PROBE_DEFAULT);
355 }
356 
357 static int
358 nexus_acpi_attach(device_t dev)
359 {
360 	device_t acpi_dev;
361 	int error;
362 
363 	nexus_init_resources();
364 	bus_generic_probe(dev);
365 	acpi_dev = BUS_ADD_CHILD(dev, 10, "acpi", 0);
366 	if (acpi_dev == NULL)
367 		panic("failed to add acpi0 device");
368 
369 	error = bus_generic_attach(dev);
370 	if (error == 0)
371 		acpi_install_wakeup_handler(device_get_softc(acpi_dev));
372 
373 	return (error);
374 }
375 
376 static device_method_t nexus_acpi_methods[] = {
377 	/* Device interface */
378 	DEVMETHOD(device_probe,		nexus_acpi_probe),
379 	DEVMETHOD(device_attach,	nexus_acpi_attach),
380 
381 	{ 0, 0 }
382 };
383 
384 DEFINE_CLASS_1(nexus, nexus_acpi_driver, nexus_acpi_methods, 1, nexus_driver);
385 static devclass_t nexus_devclass;
386 
387 DRIVER_MODULE(nexus_acpi, root, nexus_acpi_driver, nexus_devclass, 0, 0);
388