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