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