acpi_platform.c revision 1.23 1 /* $NetBSD: acpi_platform.c,v 1.23 2021/02/04 22:36:52 thorpej 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 (at) 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.23 2021/02/04 22:36:52 thorpej 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_DEFAULT 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 static const struct acpi_spcr_baud_rate {
91 uint8_t id;
92 uint32_t baud_rate;
93 } acpi_spcr_baud_rates[] = {
94 { SPCR_BAUD_DEFAULT, 0 },
95 { SPCR_BAUD_9600, 9600 },
96 { SPCR_BAUD_19200, 19200 },
97 { SPCR_BAUD_57600, 57600 },
98 { SPCR_BAUD_115200, 115200 },
99 };
100
101 extern struct bus_space arm_generic_bs_tag;
102
103 #if NPLCOM > 0
104 static struct plcom_instance plcom_console;
105 #endif
106
107 struct arm32_bus_dma_tag acpi_coherent_dma_tag;
108 static struct arm32_dma_range acpi_coherent_ranges[] = {
109 [0] = {
110 .dr_sysbase = 0,
111 .dr_busbase = 0,
112 .dr_len = UINTPTR_MAX,
113 .dr_flags = _BUS_DMAMAP_COHERENT,
114 }
115 };
116
117 static const struct pmap_devmap *
118 acpi_platform_devmap(void)
119 {
120 static const struct pmap_devmap devmap[] = {
121 DEVMAP_ENTRY_END
122 };
123
124 return devmap;
125 }
126
127 static void
128 acpi_platform_bootstrap(void)
129 {
130 extern struct arm32_bus_dma_tag arm_generic_dma_tag;
131
132 acpi_coherent_dma_tag = arm_generic_dma_tag;
133 acpi_coherent_dma_tag._ranges = acpi_coherent_ranges;
134 acpi_coherent_dma_tag._nranges = __arraycount(acpi_coherent_ranges);
135 }
136
137 static void
138 acpi_platform_attach_uart(ACPI_TABLE_SPCR *spcr)
139 {
140 #if NCOM > 0
141 struct com_regs regs;
142 bus_space_handle_t dummy_bsh;
143 u_int reg_shift;
144 #endif
145 int baud_rate, n;
146
147 /*
148 * Only MMIO access is supported today.
149 */
150 if (spcr->SerialPort.SpaceId != ACPI_ADR_SPACE_SYSTEM_MEMORY) {
151 return;
152 }
153 if (le64toh(spcr->SerialPort.Address) == 0) {
154 return;
155 }
156
157 /*
158 * Lookup SPCR baud rate.
159 */
160 baud_rate = 0;
161 for (n = 0; n < __arraycount(acpi_spcr_baud_rates); n++) {
162 if (acpi_spcr_baud_rates[n].id == spcr->BaudRate) {
163 baud_rate = acpi_spcr_baud_rates[n].baud_rate;
164 break;
165 }
166 }
167
168 /*
169 * Attach console device.
170 */
171 switch (spcr->InterfaceType) {
172 #if NPLCOM > 0
173 case ACPI_DBG2_ARM_PL011:
174 case ACPI_DBG2_ARM_SBSA_32BIT:
175 case ACPI_DBG2_ARM_SBSA_GENERIC:
176 plcom_console.pi_type = PLCOM_TYPE_PL011;
177 plcom_console.pi_iot = &arm_generic_bs_tag;
178 plcom_console.pi_iobase = le64toh(spcr->SerialPort.Address);
179 plcom_console.pi_size = PL011COM_UART_SIZE;
180 plcom_console.pi_flags = PLC_FLAG_32BIT_ACCESS;
181
182 plcomcnattach(&plcom_console, baud_rate, 0, TTYDEF_CFLAG, -1);
183 break;
184 #endif
185
186 #if NCOM > 0
187 case ACPI_DBG2_16550_COMPATIBLE:
188 case ACPI_DBG2_16550_SUBSET:
189 memset(&dummy_bsh, 0, sizeof(dummy_bsh));
190 if (ACPI_ACCESS_BIT_WIDTH(spcr->SerialPort.AccessWidth) == 8) {
191 reg_shift = 0;
192 } else {
193 reg_shift = 2;
194 }
195 com_init_regs_stride(®s, &arm_generic_bs_tag, dummy_bsh,
196 le64toh(spcr->SerialPort.Address), reg_shift);
197 comcnattach1(®s, baud_rate, -1, COM_TYPE_NORMAL,
198 TTYDEF_CFLAG);
199 break;
200
201 case ACPI_DBG2_BCM2835:
202 memset(&dummy_bsh, 0, sizeof(dummy_bsh));
203 com_init_regs_stride(®s, &arm_generic_bs_tag, dummy_bsh,
204 le64toh(spcr->SerialPort.Address) + 0x40, 2);
205 comcnattach1(®s, baud_rate, -1, COM_TYPE_BCMAUXUART,
206 TTYDEF_CFLAG);
207 cn_set_magic("+++++");
208 break;
209 #endif
210
211 default:
212 printf("SPCR: kernel does not support interface type %#x\n",
213 spcr->InterfaceType);
214 break;
215 }
216
217 }
218
219 static void
220 acpi_platform_startup(void)
221 {
222 ACPI_TABLE_SPCR *spcr;
223 ACPI_TABLE_FADT *fadt;
224 #ifdef MULTIPROCESSOR
225 ACPI_TABLE_MADT *madt;
226 #endif
227
228 /*
229 * Setup serial console device
230 */
231 if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_SPCR, (void **)&spcr))) {
232 acpi_platform_attach_uart(spcr);
233 acpi_table_unmap((ACPI_TABLE_HEADER *)spcr);
234 }
235
236 /*
237 * Initialize PSCI 0.2+ if implemented
238 */
239 if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_FADT, (void **)&fadt))) {
240 const uint16_t boot_flags = le16toh(fadt->ArmBootFlags);
241 if ((boot_flags & ACPI_FADT_PSCI_COMPLIANT) != 0) {
242 if ((boot_flags & ACPI_FADT_PSCI_USE_HVC) != 0) {
243 psci_init(psci_call_hvc);
244 } else {
245 psci_init(psci_call_smc);
246 }
247 }
248 acpi_table_unmap((ACPI_TABLE_HEADER *)fadt);
249 }
250
251 #ifdef MULTIPROCESSOR
252 /*
253 * Count CPUs
254 */
255 if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_MADT, (void **)&madt))) {
256 char *end = (char *)madt + le32toh(madt->Header.Length);
257 char *where = (char *)madt + sizeof(ACPI_TABLE_MADT);
258 while (where < end) {
259 ACPI_SUBTABLE_HEADER *subtable =
260 (ACPI_SUBTABLE_HEADER *)where;
261 if (subtable->Type == ACPI_MADT_TYPE_GENERIC_INTERRUPT)
262 arm_cpu_max++;
263 where += subtable->Length;
264 }
265 acpi_table_unmap((ACPI_TABLE_HEADER *)madt);
266 }
267 #endif /* MULTIPROCESSOR */
268 }
269
270 static void
271 acpi_platform_init_attach_args(struct fdt_attach_args *faa)
272 {
273 extern struct bus_space arm_generic_bs_tag;
274
275 faa->faa_bst = &arm_generic_bs_tag;
276 faa->faa_dmat = &acpi_coherent_dma_tag;
277 }
278
279 static void
280 acpi_platform_device_register(device_t self, void *aux)
281 {
282 /* XXX Not ideal, but the only reasonable solution atm. */
283 acpi_device_register(self, aux);
284 fdtbus_device_register(self, aux);
285
286 #if NCOM > 0
287 prop_dictionary_t prop = device_properties(self);
288 ACPI_STATUS rv;
289
290 if (device_is_a(self, "com")) {
291 ACPI_TABLE_SPCR *spcr;
292
293 rv = acpi_table_find(ACPI_SIG_SPCR, (void **)&spcr);
294 if (ACPI_FAILURE(rv)) {
295 return;
296 }
297
298 if (spcr->SerialPort.SpaceId != ACPI_ADR_SPACE_SYSTEM_MEMORY) {
299 goto spcr_unmap;
300 }
301 if (le64toh(spcr->SerialPort.Address) == 0) {
302 goto spcr_unmap;
303 }
304 if (spcr->InterfaceType != ACPI_DBG2_16550_COMPATIBLE &&
305 spcr->InterfaceType != ACPI_DBG2_16550_SUBSET) {
306 goto spcr_unmap;
307 }
308
309 if (device_is_a(device_parent(self), "puc")) {
310 const struct puc_attach_args * const paa = aux;
311 int b, d, f;
312
313 const int s = pci_get_segment(paa->pc);
314 pci_decompose_tag(paa->pc, paa->tag, &b, &d, &f);
315
316 if (spcr->PciSegment == s && spcr->PciBus == b &&
317 spcr->PciDevice == d && spcr->PciFunction == f) {
318 prop_dictionary_set_bool(prop,
319 "force_console", true);
320 }
321 }
322
323 if (device_is_a(device_parent(self), "acpi")) {
324 struct acpi_attach_args * const aa = aux;
325 struct acpi_resources res;
326 struct acpi_mem *mem;
327
328 if (ACPI_FAILURE(acpi_resource_parse(self,
329 aa->aa_node->ad_handle, "_CRS", &res,
330 &acpi_resource_parse_ops_quiet))) {
331 goto spcr_unmap;
332 }
333
334 mem = acpi_res_mem(&res, 0);
335 if (mem == NULL) {
336 goto crs_cleanup;
337 }
338
339 if (mem->ar_base == le64toh(spcr->SerialPort.Address)) {
340 prop_dictionary_set_bool(prop,
341 "force_console", true);
342 }
343
344 crs_cleanup:
345 acpi_resource_cleanup(&res);
346 }
347
348 spcr_unmap:
349 acpi_table_unmap((ACPI_TABLE_HEADER *)spcr);
350 }
351 #endif
352 }
353
354 static void
355 acpi_platform_reset(void)
356 {
357 #ifdef EFI_RUNTIME
358 if (arm_efirt_reset(EFI_RESET_COLD) == 0)
359 return;
360 #endif
361 if (psci_available())
362 psci_system_reset();
363 }
364
365 static u_int
366 acpi_platform_uart_freq(void)
367 {
368 return 0;
369 }
370
371 static const struct arm_platform acpi_platform = {
372 .ap_devmap = acpi_platform_devmap,
373 .ap_bootstrap = acpi_platform_bootstrap,
374 .ap_startup = acpi_platform_startup,
375 .ap_init_attach_args = acpi_platform_init_attach_args,
376 .ap_device_register = acpi_platform_device_register,
377 .ap_reset = acpi_platform_reset,
378 .ap_delay = gtmr_delay,
379 .ap_uart_freq = acpi_platform_uart_freq,
380 };
381
382 ARM_PLATFORM(acpi, "netbsd,generic-acpi", &acpi_platform);
383