xref: /freebsd/sys/i386/acpica/acpi_machdep.c (revision 9a14aa017b21c292740c00ee098195cd46642730)
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 uint32_t acpi_resume_beep;
50 TUNABLE_INT("debug.acpi.resume_beep", &acpi_resume_beep);
51 SYSCTL_UINT(_debug_acpi, OID_AUTO, resume_beep, CTLFLAG_RW, &acpi_resume_beep,
52     0, "Beep the PC speaker when resuming");
53 
54 uint32_t 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 	acpi_install_wakeup_handler(sc);
68 
69 	if (intr_model == ACPI_INTR_PIC)
70 		BUS_CONFIG_INTR(dev, AcpiGbl_FADT.SciInterrupt,
71 		    INTR_TRIGGER_LEVEL, INTR_POLARITY_LOW);
72 	else
73 		acpi_SetIntrModel(intr_model);
74 
75 	SYSCTL_ADD_UINT(&sc->acpi_sysctl_ctx,
76 	    SYSCTL_CHILDREN(sc->acpi_sysctl_tree), OID_AUTO,
77 	    "reset_video", CTLFLAG_RW, &acpi_reset_video, 0,
78 	    "Call the VESA reset BIOS vector on the resume path");
79 
80 	return (0);
81 }
82 
83 void
84 acpi_SetDefaultIntrModel(int model)
85 {
86 
87 	intr_model = model;
88 }
89 
90 /* Check BIOS date.  If 1998 or older, disable ACPI. */
91 int
92 acpi_machdep_quirks(int *quirks)
93 {
94 	char *va;
95 	int year;
96 
97 	/* BIOS address 0xffff5 contains the date in the format mm/dd/yy. */
98 	va = pmap_mapbios(0xffff0, 16);
99 	sscanf(va + 11, "%2d", &year);
100 	pmap_unmapbios((vm_offset_t)va, 16);
101 
102 	/*
103 	 * Date must be >= 1/1/1999 or we don't trust ACPI.  Note that this
104 	 * check must be changed by my 114th birthday.
105 	 */
106 	if (year > 90 && year < 99)
107 		*quirks = ACPI_Q_BROKEN;
108 
109 	return (0);
110 }
111 
112 void
113 acpi_cpu_c1()
114 {
115 
116 	__asm __volatile("sti; hlt");
117 }
118 
119 /*
120  * Support for mapping ACPI tables during early boot.  This abuses the
121  * crashdump map because the kernel cannot allocate KVA in
122  * pmap_mapbios() when this is used.  This makes the following
123  * assumptions about how we use this KVA: pages 0 and 1 are used to
124  * map in the header of each table found via the RSDT or XSDT and
125  * pages 2 to n are used to map in the RSDT or XSDT.  This has to use
126  * 2 pages for the table headers in case a header spans a page
127  * boundary.
128  *
129  * XXX: We don't ensure the table fits in the available address space
130  * in the crashdump map.
131  */
132 
133 /*
134  * Map some memory using the crashdump map.  'offset' is an offset in
135  * pages into the crashdump map to use for the start of the mapping.
136  */
137 static void *
138 table_map(vm_paddr_t pa, int offset, vm_offset_t length)
139 {
140 	vm_offset_t va, off;
141 	void *data;
142 
143 	off = pa & PAGE_MASK;
144 	length = roundup(length + off, PAGE_SIZE);
145 	pa = pa & PG_FRAME;
146 	va = (vm_offset_t)pmap_kenter_temporary(pa, offset) +
147 	    (offset * PAGE_SIZE);
148 	data = (void *)(va + off);
149 	length -= PAGE_SIZE;
150 	while (length > 0) {
151 		va += PAGE_SIZE;
152 		pa += PAGE_SIZE;
153 		length -= PAGE_SIZE;
154 		pmap_kenter(va, pa);
155 		invlpg(va);
156 	}
157 	return (data);
158 }
159 
160 /* Unmap memory previously mapped with table_map(). */
161 static void
162 table_unmap(void *data, vm_offset_t length)
163 {
164 	vm_offset_t va, off;
165 
166 	va = (vm_offset_t)data;
167 	off = va & PAGE_MASK;
168 	length = roundup(length + off, PAGE_SIZE);
169 	va &= ~PAGE_MASK;
170 	while (length > 0) {
171 		pmap_kremove(va);
172 		invlpg(va);
173 		va += PAGE_SIZE;
174 		length -= PAGE_SIZE;
175 	}
176 }
177 
178 /*
179  * Map a table at a given offset into the crashdump map.  It first
180  * maps the header to determine the table length and then maps the
181  * entire table.
182  */
183 static void *
184 map_table(vm_paddr_t pa, int offset, const char *sig)
185 {
186 	ACPI_TABLE_HEADER *header;
187 	vm_offset_t length;
188 	void *table;
189 
190 	header = table_map(pa, offset, sizeof(ACPI_TABLE_HEADER));
191 	if (strncmp(header->Signature, sig, ACPI_NAME_SIZE) != 0) {
192 		table_unmap(header, sizeof(ACPI_TABLE_HEADER));
193 		return (NULL);
194 	}
195 	length = header->Length;
196 	table_unmap(header, sizeof(ACPI_TABLE_HEADER));
197 	table = table_map(pa, offset, length);
198 	if (ACPI_FAILURE(AcpiTbChecksum(table, length))) {
199 		if (bootverbose)
200 			printf("ACPI: Failed checksum for table %s\n", sig);
201 #if (ACPI_CHECKSUM_ABORT)
202 		table_unmap(table, length);
203 		return (NULL);
204 #endif
205 	}
206 	return (table);
207 }
208 
209 /*
210  * See if a given ACPI table is the requested table.  Returns the
211  * length of the able if it matches or zero on failure.
212  */
213 static int
214 probe_table(vm_paddr_t address, const char *sig)
215 {
216 	ACPI_TABLE_HEADER *table;
217 
218 	table = table_map(address, 0, sizeof(ACPI_TABLE_HEADER));
219 	if (table == NULL) {
220 		if (bootverbose)
221 			printf("ACPI: Failed to map table at 0x%jx\n",
222 			    (uintmax_t)address);
223 		return (0);
224 	}
225 	if (bootverbose)
226 		printf("Table '%.4s' at 0x%jx\n", table->Signature,
227 		    (uintmax_t)address);
228 
229 	if (strncmp(table->Signature, sig, ACPI_NAME_SIZE) != 0) {
230 		table_unmap(table, sizeof(ACPI_TABLE_HEADER));
231 		return (0);
232 	}
233 	table_unmap(table, sizeof(ACPI_TABLE_HEADER));
234 	return (1);
235 }
236 
237 /*
238  * Try to map a table at a given physical address previously returned
239  * by acpi_find_table().
240  */
241 void *
242 acpi_map_table(vm_paddr_t pa, const char *sig)
243 {
244 
245 	return (map_table(pa, 0, sig));
246 }
247 
248 /* Unmap a table previously mapped via acpi_map_table(). */
249 void
250 acpi_unmap_table(void *table)
251 {
252 	ACPI_TABLE_HEADER *header;
253 
254 	header = (ACPI_TABLE_HEADER *)table;
255 	table_unmap(table, header->Length);
256 }
257 
258 /*
259  * Return the physical address of the requested table or zero if one
260  * is not found.
261  */
262 vm_paddr_t
263 acpi_find_table(const char *sig)
264 {
265 	ACPI_PHYSICAL_ADDRESS rsdp_ptr;
266 	ACPI_TABLE_RSDP *rsdp;
267 	ACPI_TABLE_RSDT *rsdt;
268 	ACPI_TABLE_XSDT *xsdt;
269 	ACPI_TABLE_HEADER *table;
270 	vm_paddr_t addr;
271 	int i, count;
272 
273 	if (resource_disabled("acpi", 0))
274 		return (0);
275 
276 	/*
277 	 * Map in the RSDP.  Since ACPI uses AcpiOsMapMemory() which in turn
278 	 * calls pmap_mapbios() to find the RSDP, we assume that we can use
279 	 * pmap_mapbios() to map the RSDP.
280 	 */
281 	if ((rsdp_ptr = AcpiOsGetRootPointer()) == 0)
282 		return (0);
283 	rsdp = pmap_mapbios(rsdp_ptr, sizeof(ACPI_TABLE_RSDP));
284 	if (rsdp == NULL) {
285 		if (bootverbose)
286 			printf("ACPI: Failed to map RSDP\n");
287 		return (0);
288 	}
289 
290 	/*
291 	 * For ACPI >= 2.0, use the XSDT if it is available.
292 	 * Otherwise, use the RSDT.  We map the XSDT or RSDT at page 2
293 	 * in the crashdump area.  Pages 0 and 1 are used to map in the
294 	 * headers of candidate ACPI tables.
295 	 */
296 	addr = 0;
297 	if (rsdp->Revision >= 2 && rsdp->XsdtPhysicalAddress != 0) {
298 		/*
299 		 * AcpiOsGetRootPointer only verifies the checksum for
300 		 * the version 1.0 portion of the RSDP.  Version 2.0 has
301 		 * an additional checksum that we verify first.
302 		 */
303 		if (AcpiTbChecksum((UINT8 *)rsdp, ACPI_RSDP_XCHECKSUM_LENGTH)) {
304 			if (bootverbose)
305 				printf("ACPI: RSDP failed extended checksum\n");
306 			return (0);
307 		}
308 		xsdt = map_table(rsdp->XsdtPhysicalAddress, 2, ACPI_SIG_XSDT);
309 		if (xsdt == NULL) {
310 			if (bootverbose)
311 				printf("ACPI: Failed to map XSDT\n");
312 			return (0);
313 		}
314 		count = (xsdt->Header.Length - sizeof(ACPI_TABLE_HEADER)) /
315 		    sizeof(UINT64);
316 		for (i = 0; i < count; i++)
317 			if (probe_table(xsdt->TableOffsetEntry[i], sig)) {
318 				addr = xsdt->TableOffsetEntry[i];
319 				break;
320 			}
321 		acpi_unmap_table(xsdt);
322 	} else {
323 		rsdt = map_table(rsdp->RsdtPhysicalAddress, 2, ACPI_SIG_RSDT);
324 		if (rsdt == NULL) {
325 			if (bootverbose)
326 				printf("ACPI: Failed to map RSDT\n");
327 			return (0);
328 		}
329 		count = (rsdt->Header.Length - sizeof(ACPI_TABLE_HEADER)) /
330 		    sizeof(UINT32);
331 		for (i = 0; i < count; i++)
332 			if (probe_table(rsdt->TableOffsetEntry[i], sig)) {
333 				addr = rsdt->TableOffsetEntry[i];
334 				break;
335 			}
336 		acpi_unmap_table(rsdt);
337 	}
338 	pmap_unmapbios((vm_offset_t)rsdp, sizeof(ACPI_TABLE_RSDP));
339 	if (addr == 0) {
340 		if (bootverbose)
341 			printf("ACPI: No %s table found\n", sig);
342 		return (0);
343 	}
344 	if (bootverbose)
345 		printf("%s: Found table at 0x%jx\n", sig, (uintmax_t)addr);
346 
347 	/*
348 	 * Verify that we can map the full table and that its checksum is
349 	 * correct, etc.
350 	 */
351 	table = map_table(addr, 0, sig);
352 	if (table == NULL)
353 		return (0);
354 	acpi_unmap_table(table);
355 
356 	return (addr);
357 }
358 
359 /*
360  * ACPI nexus(4) driver.
361  */
362 static int
363 nexus_acpi_probe(device_t dev)
364 {
365 	int error;
366 
367 	error = acpi_identify();
368 	if (error)
369 		return (error);
370 
371 	return (BUS_PROBE_DEFAULT);
372 }
373 
374 static int
375 nexus_acpi_attach(device_t dev)
376 {
377 
378 	nexus_init_resources();
379 	bus_generic_probe(dev);
380 	if (BUS_ADD_CHILD(dev, 10, "acpi", 0) == NULL)
381 		panic("failed to add acpi0 device");
382 
383 	return (bus_generic_attach(dev));
384 }
385 
386 static device_method_t nexus_acpi_methods[] = {
387 	/* Device interface */
388 	DEVMETHOD(device_probe,		nexus_acpi_probe),
389 	DEVMETHOD(device_attach,	nexus_acpi_attach),
390 
391 	{ 0, 0 }
392 };
393 
394 DEFINE_CLASS_1(nexus, nexus_acpi_driver, nexus_acpi_methods, 1, nexus_driver);
395 static devclass_t nexus_devclass;
396 
397 DRIVER_MODULE(nexus_acpi, root, nexus_acpi_driver, nexus_devclass, 0, 0);
398