xref: /freebsd/sys/arm64/acpica/acpi_machdep.c (revision 401ab69cff8fa2320a9f8ea4baa114a6da6c952b)
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 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/kernel.h>
35 
36 #include <vm/vm.h>
37 #include <vm/pmap.h>
38 
39 #include <machine/machdep.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, const char *sig)
65 {
66 	ACPI_TABLE_HEADER *header;
67 	vm_size_t length;
68 	void *table;
69 
70 	header = pmap_mapbios(pa, sizeof(ACPI_TABLE_HEADER));
71 	if (strncmp(header->Signature, sig, ACPI_NAMESEG_SIZE) != 0) {
72 		pmap_unmapbios(header, sizeof(ACPI_TABLE_HEADER));
73 		return (NULL);
74 	}
75 	length = header->Length;
76 	pmap_unmapbios(header, sizeof(ACPI_TABLE_HEADER));
77 
78 	table = pmap_mapbios(pa, length);
79 	if (ACPI_FAILURE(AcpiUtChecksum(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 table 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 
107 	if (strncmp(table->Signature, sig, ACPI_NAMESEG_SIZE) != 0) {
108 		pmap_unmapbios(table, sizeof(ACPI_TABLE_HEADER));
109 		return (0);
110 	}
111 	pmap_unmapbios(table, sizeof(ACPI_TABLE_HEADER));
112 	return (1);
113 }
114 
115 /* Unmap a table previously mapped via acpi_map_table(). */
116 void
117 acpi_unmap_table(void *table)
118 {
119 	ACPI_TABLE_HEADER *header;
120 
121 	header = (ACPI_TABLE_HEADER *)table;
122 	pmap_unmapbios(table, header->Length);
123 }
124 
125 /*
126  * Try to map a table at a given physical address previously returned
127  * by acpi_find_table().
128  */
129 void *
130 acpi_map_table(vm_paddr_t pa, const char *sig)
131 {
132 
133 	return (map_table(pa, sig));
134 }
135 
136 /*
137  * Return the physical address of the requested table or zero if one
138  * is not found.
139  */
140 vm_paddr_t
141 acpi_find_table(const char *sig)
142 {
143 	ACPI_PHYSICAL_ADDRESS rsdp_ptr;
144 	ACPI_TABLE_RSDP *rsdp;
145 	ACPI_TABLE_XSDT *xsdt;
146 	ACPI_TABLE_HEADER *table;
147 	vm_paddr_t addr;
148 	int i, count;
149 
150 	if (resource_disabled("acpi", 0))
151 		return (0);
152 
153 	/*
154 	 * Map in the RSDP.  Since ACPI uses AcpiOsMapMemory() which in turn
155 	 * calls pmap_mapbios() to find the RSDP, we assume that we can use
156 	 * pmap_mapbios() to map the RSDP.
157 	 */
158 	if ((rsdp_ptr = AcpiOsGetRootPointer()) == 0)
159 		return (0);
160 	rsdp = pmap_mapbios(rsdp_ptr, sizeof(ACPI_TABLE_RSDP));
161 	if (rsdp == NULL) {
162 		printf("ACPI: Failed to map RSDP\n");
163 		return (0);
164 	}
165 
166 	addr = 0;
167 	if (rsdp->Revision >= 2 && rsdp->XsdtPhysicalAddress != 0) {
168 		/*
169 		 * AcpiOsGetRootPointer only verifies the checksum for
170 		 * the version 1.0 portion of the RSDP.  Version 2.0 has
171 		 * an additional checksum that we verify first.
172 		 */
173 		if (AcpiUtChecksum((UINT8 *)rsdp, ACPI_RSDP_XCHECKSUM_LENGTH)) {
174 			printf("ACPI: RSDP failed extended checksum\n");
175 			pmap_unmapbios(rsdp, sizeof(ACPI_TABLE_RSDP));
176 			return (0);
177 		}
178 		xsdt = map_table(rsdp->XsdtPhysicalAddress, ACPI_SIG_XSDT);
179 		if (xsdt == NULL) {
180 			printf("ACPI: Failed to map XSDT\n");
181 			pmap_unmapbios(rsdp, sizeof(ACPI_TABLE_RSDP));
182 			return (0);
183 		}
184 		count = (xsdt->Header.Length - sizeof(ACPI_TABLE_HEADER)) /
185 		    sizeof(UINT64);
186 		for (i = 0; i < count; i++)
187 			if (probe_table(xsdt->TableOffsetEntry[i], sig)) {
188 				addr = xsdt->TableOffsetEntry[i];
189 				break;
190 			}
191 		acpi_unmap_table(xsdt);
192 	} else {
193 		printf("ACPI: Unsupported RSDP version %d and XSDT %#lx\n",
194 		    rsdp->Revision, rsdp->XsdtPhysicalAddress);
195 	}
196 	pmap_unmapbios(rsdp, sizeof(ACPI_TABLE_RSDP));
197 
198 	if (addr == 0)
199 		return (0);
200 
201 	/*
202 	 * Verify that we can map the full table and that its checksum is
203 	 * correct, etc.
204 	 */
205 	table = map_table(addr, sig);
206 	if (table == NULL)
207 		return (0);
208 	acpi_unmap_table(table);
209 
210 	return (addr);
211 }
212 
213 int
214 acpi_map_addr(struct acpi_generic_address *addr, bus_space_tag_t *tag,
215     bus_space_handle_t *handle, bus_size_t size)
216 {
217 	bus_addr_t phys;
218 
219 	/* Check if the device is Memory mapped */
220 	if (addr->SpaceId != 0)
221 		return (ENXIO);
222 
223 	phys = addr->Address;
224 	*tag = &memmap_bus;
225 
226 	return (bus_space_map(*tag, phys, size, 0, handle));
227 }
228 
229 #if MAXMEMDOM > 1
230 static void
231 parse_pxm_tables(void *dummy)
232 {
233 	uint64_t mmfr0, parange;
234 
235 	/* Only parse ACPI tables when booting via ACPI */
236 	if (arm64_bus_method != ARM64_BUS_ACPI)
237 		return;
238 
239 	if (!get_kernel_reg(ID_AA64MMFR0_EL1, &mmfr0)) {
240 		/* chosen arbitrarily */
241 		mmfr0 = ID_AA64MMFR0_PARange_1T;
242 	}
243 
244 	switch (ID_AA64MMFR0_PARange_VAL(mmfr0)) {
245 	case ID_AA64MMFR0_PARange_4G:
246 		parange = (vm_paddr_t)4 << 30 /* GiB */;
247 		break;
248 	case ID_AA64MMFR0_PARange_64G:
249 		parange = (vm_paddr_t)64 << 30 /* GiB */;
250 		break;
251 	case ID_AA64MMFR0_PARange_1T:
252 		parange = (vm_paddr_t)1 << 40 /* TiB */;
253 		break;
254 	case ID_AA64MMFR0_PARange_4T:
255 		parange = (vm_paddr_t)4 << 40 /* TiB */;
256 		break;
257 	case ID_AA64MMFR0_PARange_16T:
258 		parange = (vm_paddr_t)16 << 40 /* TiB */;
259 		break;
260 	case ID_AA64MMFR0_PARange_256T:
261 		parange = (vm_paddr_t)256 << 40 /* TiB */;
262 		break;
263 	case ID_AA64MMFR0_PARange_4P:
264 		parange = (vm_paddr_t)4 << 50 /* PiB */;
265 		break;
266 	default:
267 		/* chosen arbitrarily */
268 		parange = (vm_paddr_t)1 << 40 /* TiB */;
269 		printf("Unknown value for PARange in mmfr0 (%#lx)\n", mmfr0);
270 		break;
271 	}
272 
273 	acpi_pxm_init(MAXCPU, parange);
274 	acpi_pxm_parse_tables();
275 	acpi_pxm_set_mem_locality();
276 }
277 SYSINIT(parse_pxm_tables, SI_SUB_VM - 1, SI_ORDER_FIRST, parse_pxm_tables,
278     NULL);
279 #endif
280