xref: /netbsd-src/sys/arch/arm/acpi/acpi_platform.c (revision 9fd8799cb5ceb66c69f2eb1a6d26a1d587ba1f1e)
1 /* $NetBSD: acpi_platform.c,v 1.21 2020/10/10 15:25:31 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 #include "com.h"
33 #include "plcom.h"
34 #include "opt_efi.h"
35 #include "opt_multiprocessor.h"
36 
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: acpi_platform.c,v 1.21 2020/10/10 15:25:31 jmcneill Exp $");
39 
40 #include <sys/param.h>
41 #include <sys/bus.h>
42 #include <sys/cpu.h>
43 #include <sys/device.h>
44 #include <sys/termios.h>
45 
46 #include <dev/fdt/fdtvar.h>
47 #include <arm/fdt/arm_fdtvar.h>
48 
49 #include <uvm/uvm_extern.h>
50 
51 #include <machine/bootconfig.h>
52 #include <arm/cpufunc.h>
53 #include <arm/locore.h>
54 
55 #include <arm/cortex/gtmr_var.h>
56 
57 #include <arm/arm/psci.h>
58 #include <arm/fdt/psci_fdtvar.h>
59 
60 #include <evbarm/fdt/platform.h>
61 
62 #include <evbarm/dev/plcomreg.h>
63 #include <evbarm/dev/plcomvar.h>
64 #include <dev/ic/ns16550reg.h>
65 #include <dev/ic/comreg.h>
66 #include <dev/ic/comvar.h>
67 
68 #if NCOM > 0
69 #include <dev/pci/pcireg.h>
70 #include <dev/pci/pcivar.h>
71 #include <dev/pci/pucvar.h>
72 #endif
73 
74 #ifdef EFI_RUNTIME
75 #include <arm/arm/efi_runtime.h>
76 #endif
77 
78 #include <dev/acpi/acpireg.h>
79 #include <dev/acpi/acpivar.h>
80 #include <arm/acpi/acpi_table.h>
81 
82 #include <libfdt.h>
83 
84 #define	SPCR_BAUD_UNKNOWN			0
85 #define	SPCR_BAUD_9600				3
86 #define	SPCR_BAUD_19200				4
87 #define	SPCR_BAUD_57600				6
88 #define	SPCR_BAUD_115200			7
89 
90 extern struct bus_space arm_generic_bs_tag;
91 extern struct bus_space arm_generic_a4x_bs_tag;
92 
93 #if NPLCOM > 0
94 static struct plcom_instance plcom_console;
95 #endif
96 
97 struct arm32_bus_dma_tag acpi_coherent_dma_tag;
98 static struct arm32_dma_range acpi_coherent_ranges[] = {
99 	[0] = {
100 		.dr_sysbase = 0,
101 		.dr_busbase = 0,
102 		.dr_len = UINTPTR_MAX,
103 	 	.dr_flags = _BUS_DMAMAP_COHERENT,
104 	}
105 };
106 
107 static const struct pmap_devmap *
108 acpi_platform_devmap(void)
109 {
110 	static const struct pmap_devmap devmap[] = {
111 		DEVMAP_ENTRY_END
112 	};
113 
114 	return devmap;
115 }
116 
117 static void
118 acpi_platform_bootstrap(void)
119 {
120 	extern struct arm32_bus_dma_tag arm_generic_dma_tag;
121 
122 	acpi_coherent_dma_tag = arm_generic_dma_tag;
123 	acpi_coherent_dma_tag._ranges = acpi_coherent_ranges;
124 	acpi_coherent_dma_tag._nranges = __arraycount(acpi_coherent_ranges);
125 }
126 
127 static void
128 acpi_platform_startup(void)
129 {
130 	ACPI_TABLE_SPCR *spcr;
131 	ACPI_TABLE_FADT *fadt;
132 #ifdef MULTIPROCESSOR
133 	ACPI_TABLE_MADT *madt;
134 #endif
135 	int baud_rate;
136 
137 	/*
138 	 * Setup serial console device
139 	 */
140 	if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_SPCR, (void **)&spcr))) {
141 
142 		switch (spcr->BaudRate) {
143 		case SPCR_BAUD_9600:
144 			baud_rate = 9600;
145 			break;
146 		case SPCR_BAUD_19200:
147 			baud_rate = 19200;
148 			break;
149 		case SPCR_BAUD_57600:
150 			baud_rate = 57600;
151 			break;
152 		case SPCR_BAUD_115200:
153 		case SPCR_BAUD_UNKNOWN:
154 		default:
155 			baud_rate = 115200;
156 			break;
157 		}
158 
159 		if (spcr->SerialPort.SpaceId == ACPI_ADR_SPACE_SYSTEM_MEMORY &&
160 		    le64toh(spcr->SerialPort.Address) != 0) {
161 			switch (spcr->InterfaceType) {
162 #if NPLCOM > 0
163 			case ACPI_DBG2_ARM_PL011:
164 			case ACPI_DBG2_ARM_SBSA_32BIT:
165 			case ACPI_DBG2_ARM_SBSA_GENERIC:
166 				plcom_console.pi_type = PLCOM_TYPE_PL011;
167 				plcom_console.pi_iot = &arm_generic_bs_tag;
168 				plcom_console.pi_iobase = le64toh(spcr->SerialPort.Address);
169 				plcom_console.pi_size = PL011COM_UART_SIZE;
170 				plcom_console.pi_flags = PLC_FLAG_32BIT_ACCESS;
171 
172 				plcomcnattach(&plcom_console, baud_rate, 0, TTYDEF_CFLAG, -1);
173 				break;
174 #endif
175 #if NCOM > 0
176 			case ACPI_DBG2_16550_COMPATIBLE:
177 			case ACPI_DBG2_16550_SUBSET:
178 				if (ACPI_ACCESS_BIT_WIDTH(spcr->SerialPort.AccessWidth) == 8) {
179 					comcnattach(&arm_generic_bs_tag, le64toh(spcr->SerialPort.Address), baud_rate, -1,
180 					    COM_TYPE_NORMAL, TTYDEF_CFLAG);
181 				} else {
182 					comcnattach(&arm_generic_a4x_bs_tag, le64toh(spcr->SerialPort.Address), baud_rate, -1,
183 					    COM_TYPE_NORMAL, TTYDEF_CFLAG);
184 				}
185 				break;
186 			case ACPI_DBG2_BCM2835:
187 				comcnattach(&arm_generic_a4x_bs_tag, le64toh(spcr->SerialPort.Address) + 0x40, baud_rate, -1,
188 				    COM_TYPE_BCMAUXUART, TTYDEF_CFLAG);
189 				cn_set_magic("+++++");
190 				break;
191 #endif
192 			default:
193 				printf("SPCR: kernel does not support interface type %#x\n", spcr->InterfaceType);
194 				break;
195 			}
196 		}
197 		acpi_table_unmap((ACPI_TABLE_HEADER *)spcr);
198 	}
199 
200 	/*
201 	 * Initialize PSCI 0.2+ if implemented
202 	 */
203 	if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_FADT, (void **)&fadt))) {
204 		if (le16toh(fadt->ArmBootFlags) & ACPI_FADT_PSCI_COMPLIANT) {
205 			if (le16toh(fadt->ArmBootFlags) & ACPI_FADT_PSCI_USE_HVC) {
206 				psci_init(psci_call_hvc);
207 			} else {
208 				psci_init(psci_call_smc);
209 			}
210 		}
211 		acpi_table_unmap((ACPI_TABLE_HEADER *)fadt);
212 	}
213 
214 #ifdef MULTIPROCESSOR
215 	/*
216 	 * Count CPUs
217 	 */
218 	if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_MADT, (void **)&madt))) {
219 		char *end = (char *)madt + le32toh(madt->Header.Length);
220 		char *where = (char *)madt + sizeof(ACPI_TABLE_MADT);
221 		while (where < end) {
222 			ACPI_SUBTABLE_HEADER *subtable = (ACPI_SUBTABLE_HEADER *)where;
223 			if (subtable->Type == ACPI_MADT_TYPE_GENERIC_INTERRUPT)
224 				arm_cpu_max++;
225 			where += subtable->Length;
226 		}
227 		acpi_table_unmap((ACPI_TABLE_HEADER *)madt);
228 	}
229 #endif /* MULTIPROCESSOR */
230 }
231 
232 static void
233 acpi_platform_init_attach_args(struct fdt_attach_args *faa)
234 {
235 	extern struct bus_space arm_generic_bs_tag;
236 
237 	faa->faa_bst = &arm_generic_bs_tag;
238 	faa->faa_dmat = &acpi_coherent_dma_tag;
239 }
240 
241 static void
242 acpi_platform_device_register(device_t self, void *aux)
243 {
244 #if NCOM > 0
245 	prop_dictionary_t prop = device_properties(self);
246 
247 	if (device_is_a(self, "com")) {
248 		ACPI_TABLE_SPCR *spcr;
249 
250 		if (ACPI_FAILURE(acpi_table_find(ACPI_SIG_SPCR, (void **)&spcr)))
251 			return;
252 
253 		if (spcr->SerialPort.SpaceId != ACPI_ADR_SPACE_SYSTEM_MEMORY)
254 			goto spcr_unmap;
255 		if (le64toh(spcr->SerialPort.Address) == 0)
256 			goto spcr_unmap;
257 		if (spcr->InterfaceType != ACPI_DBG2_16550_COMPATIBLE &&
258 		    spcr->InterfaceType != ACPI_DBG2_16550_SUBSET)
259 			goto spcr_unmap;
260 
261 		if (device_is_a(device_parent(self), "puc")) {
262 			const struct puc_attach_args * const paa = aux;
263 			int b, d, f;
264 
265 			const int s = pci_get_segment(paa->pc);
266 			pci_decompose_tag(paa->pc, paa->tag, &b, &d, &f);
267 
268 			if (spcr->PciSegment == s && spcr->PciBus == b &&
269 			    spcr->PciDevice == d && spcr->PciFunction == f)
270 				prop_dictionary_set_bool(prop, "force_console", true);
271 		}
272 
273 		if (device_is_a(device_parent(self), "acpi")) {
274 			struct acpi_attach_args * const aa = aux;
275 			struct acpi_resources res;
276 			struct acpi_mem *mem;
277 
278 			if (ACPI_FAILURE(acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
279 			    &res, &acpi_resource_parse_ops_quiet)))
280 				goto spcr_unmap;
281 
282 			mem = acpi_res_mem(&res, 0);
283 			if (mem == NULL)
284 				goto crs_cleanup;
285 
286 			if (mem->ar_base == le64toh(spcr->SerialPort.Address))
287 				prop_dictionary_set_bool(prop, "force_console", true);
288 
289 crs_cleanup:
290 			acpi_resource_cleanup(&res);
291 		}
292 
293 spcr_unmap:
294 		acpi_table_unmap((ACPI_TABLE_HEADER *)spcr);
295 	}
296 #endif
297 }
298 
299 static void
300 acpi_platform_reset(void)
301 {
302 #ifdef EFI_RUNTIME
303 	if (arm_efirt_reset(EFI_RESET_COLD) == 0)
304 		return;
305 #endif
306 	if (psci_available())
307 		psci_system_reset();
308 }
309 
310 static u_int
311 acpi_platform_uart_freq(void)
312 {
313 	return 0;
314 }
315 
316 static const struct arm_platform acpi_platform = {
317 	.ap_devmap = acpi_platform_devmap,
318 	.ap_bootstrap = acpi_platform_bootstrap,
319 	.ap_startup = acpi_platform_startup,
320 	.ap_init_attach_args = acpi_platform_init_attach_args,
321 	.ap_device_register = acpi_platform_device_register,
322 	.ap_reset = acpi_platform_reset,
323 	.ap_delay = gtmr_delay,
324 	.ap_uart_freq = acpi_platform_uart_freq,
325 };
326 
327 ARM_PLATFORM(acpi, "netbsd,generic-acpi", &acpi_platform);
328