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