xref: /netbsd-src/sys/arch/arm/acpi/acpi_table.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /* $NetBSD: acpi_table.c,v 1.1 2018/10/12 22:15:04 jmcneill Exp $ */
2 
3 /*-
4  * Copyright (c) 2018 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jared McNeill <jmcneill@invisible.ca>.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * ACPI table functions for use in early boot before ACPICA can be safely
34  * initialized.
35  */
36 
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: acpi_table.c,v 1.1 2018/10/12 22:15:04 jmcneill Exp $");
39 
40 #include <sys/param.h>
41 #include <sys/bus.h>
42 #include <sys/cpu.h>
43 #include <sys/device.h>
44 
45 #include <uvm/uvm_extern.h>
46 
47 #include <dev/acpi/acpireg.h>
48 #include <dev/acpi/acpivar.h>
49 
50 #include <machine/acpi_machdep.h>
51 
52 #include <arch/arm/acpi/acpi_table.h>
53 
54 ACPI_STATUS
55 acpi_table_map(ACPI_PHYSICAL_ADDRESS pa, void **hdrp)
56 {
57 	ACPI_TABLE_HEADER *header;
58 	ACPI_STATUS rv;
59 	UINT32 length;
60 
61 	rv = acpi_md_OsMapMemory(pa, sizeof(*header), (void **)&header);
62 	if (ACPI_FAILURE(rv))
63 		return rv;
64 	length = header->Length;
65 	acpi_md_OsUnmapMemory(header, sizeof(*header));
66 
67 	return acpi_md_OsMapMemory(pa, length, hdrp);
68 }
69 
70 void
71 acpi_table_unmap(ACPI_TABLE_HEADER *hdrp)
72 {
73 	acpi_md_OsUnmapMemory(hdrp, hdrp->Length);
74 }
75 
76 ACPI_STATUS
77 acpi_table_find(const char *sig, void **hdrp)
78 {
79 	ACPI_TABLE_RSDP *rsdp;
80 	ACPI_TABLE_XSDT *xsdt;
81 	ACPI_TABLE_HEADER *header;
82 	ACPI_PHYSICAL_ADDRESS pa;
83 	ACPI_STATUS rv;
84 
85 	/* RDSP contains a pointer to the XSDT */
86 	pa = 0;
87 	rv = acpi_md_OsMapMemory(acpi_md_OsGetRootPointer(), sizeof(*rsdp), (void **)&rsdp);
88 	if (ACPI_FAILURE(rv))
89 		return rv;
90 	if (memcmp(rsdp->Signature, ACPI_SIG_RSDP, sizeof(rsdp->Signature)) == 0)
91 		pa = rsdp->XsdtPhysicalAddress;
92 	acpi_md_OsUnmapMemory(rsdp, sizeof(*rsdp));
93 	if (pa == 0)
94 		return AE_NOT_FOUND;
95 
96 	/* XSDT contains pointers to other tables */
97 	rv = acpi_table_map(pa, (void **)&xsdt);
98 	if (ACPI_FAILURE(rv))
99 		return rv;
100 	const u_int entries = (xsdt->Header.Length - sizeof(ACPI_TABLE_HEADER)) / ACPI_XSDT_ENTRY_SIZE;
101 	for (u_int n = 0; n < entries; n++) {
102 		rv = acpi_table_map(xsdt->TableOffsetEntry[n], (void **)&header);
103 		if (ACPI_FAILURE(rv))
104 			continue;
105 		if (memcmp(header->Signature, sig, sizeof(header->Signature)) == 0) {
106 			acpi_table_unmap((ACPI_TABLE_HEADER *)xsdt);
107 			*hdrp = header;
108 			return AE_OK;
109 		}
110 	}
111 
112 	/* Not found */
113 	acpi_table_unmap((ACPI_TABLE_HEADER *)xsdt);
114 	return AE_NOT_FOUND;
115 }
116