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