xref: /freebsd/sys/arm64/acpica/acpi_machdep.c (revision 642870485c089b57000fe538d3485e272b038d59)
1 /*-
2  * Copyright (c) 2001 Mitsuru IWASAKI
3  * Copyright (c) 2015 The FreeBSD Foundation
4  * All rights reserved.
5  *
6  * This software was developed by Andrew Turner under
7  * sponsorship from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/bus.h>
36 #include <sys/kernel.h>
37 
38 #include <vm/vm.h>
39 #include <vm/pmap.h>
40 
41 #include <contrib/dev/acpica/include/acpi.h>
42 #include <contrib/dev/acpica/include/accommon.h>
43 #include <contrib/dev/acpica/include/actables.h>
44 
45 #include <dev/acpica/acpivar.h>
46 
47 extern struct bus_space memmap_bus;
48 
49 int
50 acpi_machdep_init(device_t dev)
51 {
52 
53 	return (0);
54 }
55 
56 int
57 acpi_machdep_quirks(int *quirks)
58 {
59 
60 	return (0);
61 }
62 
63 static void *
64 map_table(vm_paddr_t pa, int offset, const char *sig)
65 {
66 	ACPI_TABLE_HEADER *header;
67 	vm_offset_t length;
68 	void *table;
69 
70 	header = pmap_mapbios(pa, sizeof(ACPI_TABLE_HEADER));
71 	if (strncmp(header->Signature, sig, ACPI_NAME_SIZE) != 0) {
72 		pmap_unmapbios((vm_offset_t)header, sizeof(ACPI_TABLE_HEADER));
73 		return (NULL);
74 	}
75 	length = header->Length;
76 	pmap_unmapbios((vm_offset_t)header, sizeof(ACPI_TABLE_HEADER));
77 
78 	table = pmap_mapbios(pa, length);
79 	if (ACPI_FAILURE(AcpiTbChecksum(table, length))) {
80 		if (bootverbose)
81 			printf("ACPI: Failed checksum for table %s\n", sig);
82 #if (ACPI_CHECKSUM_ABORT)
83 		pmap_unmapbios(table, length);
84 		return (NULL);
85 #endif
86 	}
87 	return (table);
88 }
89 
90 /*
91  * See if a given ACPI table is the requested table.  Returns the
92  * length of the able if it matches or zero on failure.
93  */
94 static int
95 probe_table(vm_paddr_t address, const char *sig)
96 {
97 	ACPI_TABLE_HEADER *table;
98 
99 	table = pmap_mapbios(address, sizeof(ACPI_TABLE_HEADER));
100 	if (table == NULL) {
101 		if (bootverbose)
102 			printf("ACPI: Failed to map table at 0x%jx\n",
103 			    (uintmax_t)address);
104 		return (0);
105 	}
106 	if (bootverbose)
107 		printf("Table '%.4s' at 0x%jx\n", table->Signature,
108 		    (uintmax_t)address);
109 
110 	if (strncmp(table->Signature, sig, ACPI_NAME_SIZE) != 0) {
111 		pmap_unmapbios((vm_offset_t)table, sizeof(ACPI_TABLE_HEADER));
112 		return (0);
113 	}
114 	pmap_unmapbios((vm_offset_t)table, sizeof(ACPI_TABLE_HEADER));
115 	return (1);
116 }
117 
118 /* Unmap a table previously mapped via acpi_map_table(). */
119 void
120 acpi_unmap_table(void *table)
121 {
122 	ACPI_TABLE_HEADER *header;
123 
124 	header = (ACPI_TABLE_HEADER *)table;
125 	pmap_unmapbios((vm_offset_t)table, header->Length);
126 }
127 
128 /*
129  * Try to map a table at a given physical address previously returned
130  * by acpi_find_table().
131  */
132 void *
133 acpi_map_table(vm_paddr_t pa, const char *sig)
134 {
135 
136 	return (map_table(pa, 0, sig));
137 }
138 
139 /*
140  * Return the physical address of the requested table or zero if one
141  * is not found.
142  */
143 vm_paddr_t
144 acpi_find_table(const char *sig)
145 {
146 	ACPI_PHYSICAL_ADDRESS rsdp_ptr;
147 	ACPI_TABLE_RSDP *rsdp;
148 	ACPI_TABLE_XSDT *xsdt;
149 	ACPI_TABLE_HEADER *table;
150 	vm_paddr_t addr;
151 	int i, count;
152 
153 	if (resource_disabled("acpi", 0))
154 		return (0);
155 
156 	/*
157 	 * Map in the RSDP.  Since ACPI uses AcpiOsMapMemory() which in turn
158 	 * calls pmap_mapbios() to find the RSDP, we assume that we can use
159 	 * pmap_mapbios() to map the RSDP.
160 	 */
161 	if ((rsdp_ptr = AcpiOsGetRootPointer()) == 0)
162 		return (0);
163 	rsdp = pmap_mapbios(rsdp_ptr, sizeof(ACPI_TABLE_RSDP));
164 	if (rsdp == NULL) {
165 		if (bootverbose)
166 			printf("ACPI: Failed to map RSDP\n");
167 		return (0);
168 	}
169 
170 	addr = 0;
171 	if (rsdp->Revision >= 2 && rsdp->XsdtPhysicalAddress != 0) {
172 		/*
173 		 * AcpiOsGetRootPointer only verifies the checksum for
174 		 * the version 1.0 portion of the RSDP.  Version 2.0 has
175 		 * an additional checksum that we verify first.
176 		 */
177 		if (AcpiTbChecksum((UINT8 *)rsdp, ACPI_RSDP_XCHECKSUM_LENGTH)) {
178 			if (bootverbose)
179 				printf("ACPI: RSDP failed extended checksum\n");
180 			return (0);
181 		}
182 		xsdt = map_table(rsdp->XsdtPhysicalAddress, 2, ACPI_SIG_XSDT);
183 		if (xsdt == NULL) {
184 			if (bootverbose)
185 				printf("ACPI: Failed to map XSDT\n");
186 			pmap_unmapbios((vm_offset_t)rsdp,
187 			    sizeof(ACPI_TABLE_RSDP));
188 			return (0);
189 		}
190 		count = (xsdt->Header.Length - sizeof(ACPI_TABLE_HEADER)) /
191 		    sizeof(UINT64);
192 		for (i = 0; i < count; i++)
193 			if (probe_table(xsdt->TableOffsetEntry[i], sig)) {
194 				addr = xsdt->TableOffsetEntry[i];
195 				break;
196 			}
197 		acpi_unmap_table(xsdt);
198 	}
199 	pmap_unmapbios((vm_offset_t)rsdp, sizeof(ACPI_TABLE_RSDP));
200 
201 	if (addr == 0) {
202 		if (bootverbose)
203 			printf("ACPI: No %s table found\n", sig);
204 		return (0);
205 	}
206 	if (bootverbose)
207 		printf("%s: Found table at 0x%jx\n", sig, (uintmax_t)addr);
208 
209 	/*
210 	 * Verify that we can map the full table and that its checksum is
211 	 * correct, etc.
212 	 */
213 	table = map_table(addr, 0, sig);
214 	if (table == NULL)
215 		return (0);
216 	acpi_unmap_table(table);
217 
218 	return (addr);
219 }
220 
221 int
222 acpi_map_addr(struct acpi_generic_address *addr, bus_space_tag_t *tag,
223     bus_space_handle_t *handle, bus_size_t size)
224 {
225 	bus_addr_t phys;
226 
227 	/* Check if the device is Memory mapped */
228 	if (addr->SpaceId != 0)
229 		return (ENXIO);
230 
231 	phys = addr->Address;
232 	*tag = &memmap_bus;
233 
234 	return (bus_space_map(*tag, phys, size, 0, handle));
235 }
236