xref: /freebsd/sys/arm64/acpica/acpi_machdep.c (revision 8df8b2d3e51d1b816201d8a1fe8bc29fe192e562)
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 <machine/machdep.h>
42 
43 #include <contrib/dev/acpica/include/acpi.h>
44 #include <contrib/dev/acpica/include/accommon.h>
45 #include <contrib/dev/acpica/include/actables.h>
46 
47 #include <dev/acpica/acpivar.h>
48 
49 extern struct bus_space memmap_bus;
50 
51 int
52 acpi_machdep_init(device_t dev)
53 {
54 
55 	return (0);
56 }
57 
58 int
59 acpi_machdep_quirks(int *quirks)
60 {
61 
62 	return (0);
63 }
64 
65 static void *
66 map_table(vm_paddr_t pa, int offset, const char *sig)
67 {
68 	ACPI_TABLE_HEADER *header;
69 	vm_offset_t length;
70 	void *table;
71 
72 	header = pmap_mapbios(pa, sizeof(ACPI_TABLE_HEADER));
73 	if (strncmp(header->Signature, sig, ACPI_NAME_SIZE) != 0) {
74 		pmap_unmapbios((vm_offset_t)header, sizeof(ACPI_TABLE_HEADER));
75 		return (NULL);
76 	}
77 	length = header->Length;
78 	pmap_unmapbios((vm_offset_t)header, sizeof(ACPI_TABLE_HEADER));
79 
80 	table = pmap_mapbios(pa, length);
81 	if (ACPI_FAILURE(AcpiTbChecksum(table, length))) {
82 		if (bootverbose)
83 			printf("ACPI: Failed checksum for table %s\n", sig);
84 #if (ACPI_CHECKSUM_ABORT)
85 		pmap_unmapbios(table, length);
86 		return (NULL);
87 #endif
88 	}
89 	return (table);
90 }
91 
92 /*
93  * See if a given ACPI table is the requested table.  Returns the
94  * length of the able if it matches or zero on failure.
95  */
96 static int
97 probe_table(vm_paddr_t address, const char *sig)
98 {
99 	ACPI_TABLE_HEADER *table;
100 
101 	table = pmap_mapbios(address, sizeof(ACPI_TABLE_HEADER));
102 	if (table == NULL) {
103 		if (bootverbose)
104 			printf("ACPI: Failed to map table at 0x%jx\n",
105 			    (uintmax_t)address);
106 		return (0);
107 	}
108 	if (bootverbose)
109 		printf("Table '%.4s' at 0x%jx\n", table->Signature,
110 		    (uintmax_t)address);
111 
112 	if (strncmp(table->Signature, sig, ACPI_NAME_SIZE) != 0) {
113 		pmap_unmapbios((vm_offset_t)table, sizeof(ACPI_TABLE_HEADER));
114 		return (0);
115 	}
116 	pmap_unmapbios((vm_offset_t)table, sizeof(ACPI_TABLE_HEADER));
117 	return (1);
118 }
119 
120 /* Unmap a table previously mapped via acpi_map_table(). */
121 void
122 acpi_unmap_table(void *table)
123 {
124 	ACPI_TABLE_HEADER *header;
125 
126 	header = (ACPI_TABLE_HEADER *)table;
127 	pmap_unmapbios((vm_offset_t)table, header->Length);
128 }
129 
130 /*
131  * Try to map a table at a given physical address previously returned
132  * by acpi_find_table().
133  */
134 void *
135 acpi_map_table(vm_paddr_t pa, const char *sig)
136 {
137 
138 	return (map_table(pa, 0, sig));
139 }
140 
141 /*
142  * Return the physical address of the requested table or zero if one
143  * is not found.
144  */
145 vm_paddr_t
146 acpi_find_table(const char *sig)
147 {
148 	ACPI_PHYSICAL_ADDRESS rsdp_ptr;
149 	ACPI_TABLE_RSDP *rsdp;
150 	ACPI_TABLE_XSDT *xsdt;
151 	ACPI_TABLE_HEADER *table;
152 	vm_paddr_t addr;
153 	int i, count;
154 
155 	if (resource_disabled("acpi", 0))
156 		return (0);
157 
158 	/*
159 	 * Map in the RSDP.  Since ACPI uses AcpiOsMapMemory() which in turn
160 	 * calls pmap_mapbios() to find the RSDP, we assume that we can use
161 	 * pmap_mapbios() to map the RSDP.
162 	 */
163 	if ((rsdp_ptr = AcpiOsGetRootPointer()) == 0)
164 		return (0);
165 	rsdp = pmap_mapbios(rsdp_ptr, sizeof(ACPI_TABLE_RSDP));
166 	if (rsdp == NULL) {
167 		if (bootverbose)
168 			printf("ACPI: Failed to map RSDP\n");
169 		return (0);
170 	}
171 
172 	addr = 0;
173 	if (rsdp->Revision >= 2 && rsdp->XsdtPhysicalAddress != 0) {
174 		/*
175 		 * AcpiOsGetRootPointer only verifies the checksum for
176 		 * the version 1.0 portion of the RSDP.  Version 2.0 has
177 		 * an additional checksum that we verify first.
178 		 */
179 		if (AcpiTbChecksum((UINT8 *)rsdp, ACPI_RSDP_XCHECKSUM_LENGTH)) {
180 			if (bootverbose)
181 				printf("ACPI: RSDP failed extended checksum\n");
182 			return (0);
183 		}
184 		xsdt = map_table(rsdp->XsdtPhysicalAddress, 2, ACPI_SIG_XSDT);
185 		if (xsdt == NULL) {
186 			if (bootverbose)
187 				printf("ACPI: Failed to map XSDT\n");
188 			pmap_unmapbios((vm_offset_t)rsdp,
189 			    sizeof(ACPI_TABLE_RSDP));
190 			return (0);
191 		}
192 		count = (xsdt->Header.Length - sizeof(ACPI_TABLE_HEADER)) /
193 		    sizeof(UINT64);
194 		for (i = 0; i < count; i++)
195 			if (probe_table(xsdt->TableOffsetEntry[i], sig)) {
196 				addr = xsdt->TableOffsetEntry[i];
197 				break;
198 			}
199 		acpi_unmap_table(xsdt);
200 	}
201 	pmap_unmapbios((vm_offset_t)rsdp, sizeof(ACPI_TABLE_RSDP));
202 
203 	if (addr == 0) {
204 		if (bootverbose)
205 			printf("ACPI: No %s table found\n", sig);
206 		return (0);
207 	}
208 	if (bootverbose)
209 		printf("%s: Found table at 0x%jx\n", sig, (uintmax_t)addr);
210 
211 	/*
212 	 * Verify that we can map the full table and that its checksum is
213 	 * correct, etc.
214 	 */
215 	table = map_table(addr, 0, sig);
216 	if (table == NULL)
217 		return (0);
218 	acpi_unmap_table(table);
219 
220 	return (addr);
221 }
222 
223 int
224 acpi_map_addr(struct acpi_generic_address *addr, bus_space_tag_t *tag,
225     bus_space_handle_t *handle, bus_size_t size)
226 {
227 	bus_addr_t phys;
228 
229 	/* Check if the device is Memory mapped */
230 	if (addr->SpaceId != 0)
231 		return (ENXIO);
232 
233 	phys = addr->Address;
234 	*tag = &memmap_bus;
235 
236 	return (bus_space_map(*tag, phys, size, 0, handle));
237 }
238 
239 #if MAXMEMDOM > 1
240 static void
241 parse_pxm_tables(void *dummy)
242 {
243 
244 	/* Only parse ACPI tables when booting via ACPI */
245 	if (arm64_bus_method != ARM64_BUS_ACPI)
246 		return;
247 
248 	acpi_pxm_init(MAXCPU, (vm_paddr_t)1 << 40);
249 	acpi_pxm_parse_tables();
250 	acpi_pxm_set_mem_locality();
251 }
252 SYSINIT(parse_pxm_tables, SI_SUB_VM - 1, SI_ORDER_FIRST, parse_pxm_tables,
253     NULL);
254 #endif
255