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