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