acpi_platform.c revision 1.12.2.3 1 1.12.2.3 martin /* $NetBSD: acpi_platform.c,v 1.12.2.3 2020/04/13 08:03:32 martin Exp $ */
2 1.12.2.2 christos
3 1.12.2.2 christos /*-
4 1.12.2.2 christos * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 1.12.2.2 christos * All rights reserved.
6 1.12.2.2 christos *
7 1.12.2.2 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.12.2.2 christos * by Jared McNeill <jmcneill (at) invisible.ca>.
9 1.12.2.2 christos *
10 1.12.2.2 christos * Redistribution and use in source and binary forms, with or without
11 1.12.2.2 christos * modification, are permitted provided that the following conditions
12 1.12.2.2 christos * are met:
13 1.12.2.2 christos * 1. Redistributions of source code must retain the above copyright
14 1.12.2.2 christos * notice, this list of conditions and the following disclaimer.
15 1.12.2.2 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.12.2.2 christos * notice, this list of conditions and the following disclaimer in the
17 1.12.2.2 christos * documentation and/or other materials provided with the distribution.
18 1.12.2.2 christos *
19 1.12.2.2 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.12.2.2 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.12.2.2 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.12.2.2 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.12.2.2 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.12.2.2 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.12.2.2 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.12.2.2 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.12.2.2 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.12.2.2 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.12.2.2 christos * POSSIBILITY OF SUCH DAMAGE.
30 1.12.2.2 christos */
31 1.12.2.2 christos
32 1.12.2.2 christos #include "com.h"
33 1.12.2.2 christos #include "plcom.h"
34 1.12.2.3 martin #include "wsdisplay.h"
35 1.12.2.3 martin #include "genfb.h"
36 1.12.2.2 christos #include "opt_efi.h"
37 1.12.2.2 christos #include "opt_multiprocessor.h"
38 1.12.2.2 christos
39 1.12.2.2 christos #include <sys/cdefs.h>
40 1.12.2.3 martin __KERNEL_RCSID(0, "$NetBSD: acpi_platform.c,v 1.12.2.3 2020/04/13 08:03:32 martin Exp $");
41 1.12.2.2 christos
42 1.12.2.2 christos #include <sys/param.h>
43 1.12.2.2 christos #include <sys/bus.h>
44 1.12.2.2 christos #include <sys/cpu.h>
45 1.12.2.2 christos #include <sys/device.h>
46 1.12.2.2 christos #include <sys/termios.h>
47 1.12.2.2 christos
48 1.12.2.2 christos #include <dev/fdt/fdtvar.h>
49 1.12.2.2 christos #include <arm/fdt/arm_fdtvar.h>
50 1.12.2.2 christos
51 1.12.2.2 christos #include <uvm/uvm_extern.h>
52 1.12.2.2 christos
53 1.12.2.2 christos #include <machine/bootconfig.h>
54 1.12.2.2 christos #include <arm/cpufunc.h>
55 1.12.2.2 christos #include <arm/locore.h>
56 1.12.2.2 christos
57 1.12.2.2 christos #include <arm/cortex/gtmr_var.h>
58 1.12.2.2 christos
59 1.12.2.2 christos #include <arm/arm/psci.h>
60 1.12.2.2 christos #include <arm/fdt/psci_fdtvar.h>
61 1.12.2.2 christos
62 1.12.2.2 christos #include <evbarm/fdt/platform.h>
63 1.12.2.2 christos
64 1.12.2.2 christos #include <evbarm/dev/plcomreg.h>
65 1.12.2.2 christos #include <evbarm/dev/plcomvar.h>
66 1.12.2.2 christos #include <dev/ic/ns16550reg.h>
67 1.12.2.2 christos #include <dev/ic/comreg.h>
68 1.12.2.2 christos #include <dev/ic/comvar.h>
69 1.12.2.2 christos
70 1.12.2.2 christos #if NCOM > 0
71 1.12.2.2 christos #include <dev/pci/pcireg.h>
72 1.12.2.2 christos #include <dev/pci/pcivar.h>
73 1.12.2.2 christos #include <dev/pci/pucvar.h>
74 1.12.2.2 christos #endif
75 1.12.2.2 christos
76 1.12.2.3 martin #if NWSDISPLAY > 0 && NGENFB > 0
77 1.12.2.3 martin #include <arm/acpi/acpi_simplefb.h>
78 1.12.2.3 martin #endif
79 1.12.2.3 martin
80 1.12.2.2 christos #ifdef EFI_RUNTIME
81 1.12.2.2 christos #include <arm/arm/efi_runtime.h>
82 1.12.2.2 christos #endif
83 1.12.2.2 christos
84 1.12.2.2 christos #include <dev/acpi/acpireg.h>
85 1.12.2.2 christos #include <dev/acpi/acpivar.h>
86 1.12.2.2 christos #include <arm/acpi/acpi_table.h>
87 1.12.2.2 christos
88 1.12.2.2 christos #include <libfdt.h>
89 1.12.2.2 christos
90 1.12.2.2 christos #define SPCR_BAUD_UNKNOWN 0
91 1.12.2.2 christos #define SPCR_BAUD_9600 3
92 1.12.2.2 christos #define SPCR_BAUD_19200 4
93 1.12.2.2 christos #define SPCR_BAUD_57600 6
94 1.12.2.2 christos #define SPCR_BAUD_115200 7
95 1.12.2.2 christos
96 1.12.2.2 christos extern struct bus_space arm_generic_bs_tag;
97 1.12.2.2 christos extern struct bus_space arm_generic_a4x_bs_tag;
98 1.12.2.2 christos
99 1.12.2.2 christos #if NPLCOM > 0
100 1.12.2.2 christos static struct plcom_instance plcom_console;
101 1.12.2.2 christos #endif
102 1.12.2.2 christos
103 1.12.2.3 martin struct arm32_bus_dma_tag acpi_coherent_dma_tag;
104 1.12.2.3 martin static struct arm32_dma_range acpi_coherent_ranges[] = {
105 1.12.2.3 martin [0] = {
106 1.12.2.3 martin .dr_sysbase = 0,
107 1.12.2.3 martin .dr_busbase = 0,
108 1.12.2.3 martin .dr_len = UINTPTR_MAX,
109 1.12.2.3 martin .dr_flags = _BUS_DMAMAP_COHERENT,
110 1.12.2.3 martin }
111 1.12.2.3 martin };
112 1.12.2.3 martin
113 1.12.2.2 christos static const struct pmap_devmap *
114 1.12.2.2 christos acpi_platform_devmap(void)
115 1.12.2.2 christos {
116 1.12.2.2 christos static const struct pmap_devmap devmap[] = {
117 1.12.2.2 christos DEVMAP_ENTRY_END
118 1.12.2.2 christos };
119 1.12.2.2 christos
120 1.12.2.2 christos return devmap;
121 1.12.2.2 christos }
122 1.12.2.2 christos
123 1.12.2.2 christos static void
124 1.12.2.2 christos acpi_platform_bootstrap(void)
125 1.12.2.2 christos {
126 1.12.2.3 martin extern struct arm32_bus_dma_tag arm_generic_dma_tag;
127 1.12.2.3 martin
128 1.12.2.3 martin acpi_coherent_dma_tag = arm_generic_dma_tag;
129 1.12.2.3 martin acpi_coherent_dma_tag._ranges = acpi_coherent_ranges;
130 1.12.2.3 martin acpi_coherent_dma_tag._nranges = __arraycount(acpi_coherent_ranges);
131 1.12.2.2 christos }
132 1.12.2.2 christos
133 1.12.2.2 christos static void
134 1.12.2.2 christos acpi_platform_startup(void)
135 1.12.2.2 christos {
136 1.12.2.2 christos ACPI_TABLE_SPCR *spcr;
137 1.12.2.2 christos ACPI_TABLE_FADT *fadt;
138 1.12.2.2 christos #ifdef MULTIPROCESSOR
139 1.12.2.2 christos ACPI_TABLE_MADT *madt;
140 1.12.2.2 christos #endif
141 1.12.2.2 christos int baud_rate;
142 1.12.2.2 christos
143 1.12.2.2 christos /*
144 1.12.2.2 christos * Setup serial console device
145 1.12.2.2 christos */
146 1.12.2.2 christos if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_SPCR, (void **)&spcr))) {
147 1.12.2.2 christos
148 1.12.2.2 christos switch (spcr->BaudRate) {
149 1.12.2.2 christos case SPCR_BAUD_9600:
150 1.12.2.2 christos baud_rate = 9600;
151 1.12.2.2 christos break;
152 1.12.2.2 christos case SPCR_BAUD_19200:
153 1.12.2.2 christos baud_rate = 19200;
154 1.12.2.2 christos break;
155 1.12.2.2 christos case SPCR_BAUD_57600:
156 1.12.2.2 christos baud_rate = 57600;
157 1.12.2.2 christos break;
158 1.12.2.2 christos case SPCR_BAUD_115200:
159 1.12.2.2 christos case SPCR_BAUD_UNKNOWN:
160 1.12.2.2 christos default:
161 1.12.2.2 christos baud_rate = 115200;
162 1.12.2.2 christos break;
163 1.12.2.2 christos }
164 1.12.2.2 christos
165 1.12.2.2 christos if (spcr->SerialPort.SpaceId == ACPI_ADR_SPACE_SYSTEM_MEMORY &&
166 1.12.2.2 christos spcr->SerialPort.Address != 0) {
167 1.12.2.2 christos switch (spcr->InterfaceType) {
168 1.12.2.2 christos #if NPLCOM > 0
169 1.12.2.2 christos case ACPI_DBG2_ARM_PL011:
170 1.12.2.2 christos case ACPI_DBG2_ARM_SBSA_32BIT:
171 1.12.2.2 christos case ACPI_DBG2_ARM_SBSA_GENERIC:
172 1.12.2.2 christos plcom_console.pi_type = PLCOM_TYPE_PL011;
173 1.12.2.2 christos plcom_console.pi_iot = &arm_generic_bs_tag;
174 1.12.2.2 christos plcom_console.pi_iobase = spcr->SerialPort.Address;
175 1.12.2.2 christos plcom_console.pi_size = PL011COM_UART_SIZE;
176 1.12.2.3 martin plcom_console.pi_flags = PLC_FLAG_32BIT_ACCESS;
177 1.12.2.2 christos
178 1.12.2.2 christos plcomcnattach(&plcom_console, baud_rate, 0, TTYDEF_CFLAG, -1);
179 1.12.2.2 christos break;
180 1.12.2.2 christos #endif
181 1.12.2.2 christos #if NCOM > 0
182 1.12.2.2 christos case ACPI_DBG2_16550_COMPATIBLE:
183 1.12.2.2 christos case ACPI_DBG2_16550_SUBSET:
184 1.12.2.2 christos if (ACPI_ACCESS_BIT_WIDTH(spcr->SerialPort.AccessWidth) == 8) {
185 1.12.2.2 christos comcnattach(&arm_generic_bs_tag, spcr->SerialPort.Address, baud_rate, -1,
186 1.12.2.2 christos COM_TYPE_NORMAL, TTYDEF_CFLAG);
187 1.12.2.2 christos } else {
188 1.12.2.2 christos comcnattach(&arm_generic_a4x_bs_tag, spcr->SerialPort.Address, baud_rate, -1,
189 1.12.2.2 christos COM_TYPE_NORMAL, TTYDEF_CFLAG);
190 1.12.2.2 christos }
191 1.12.2.2 christos break;
192 1.12.2.2 christos case ACPI_DBG2_BCM2835:
193 1.12.2.2 christos comcnattach(&arm_generic_a4x_bs_tag, spcr->SerialPort.Address + 0x40, baud_rate, -1,
194 1.12.2.2 christos COM_TYPE_BCMAUXUART, TTYDEF_CFLAG);
195 1.12.2.2 christos cn_set_magic("+++++");
196 1.12.2.2 christos break;
197 1.12.2.2 christos #endif
198 1.12.2.2 christos default:
199 1.12.2.2 christos printf("SPCR: kernel does not support interface type %#x\n", spcr->InterfaceType);
200 1.12.2.2 christos break;
201 1.12.2.2 christos }
202 1.12.2.2 christos }
203 1.12.2.2 christos acpi_table_unmap((ACPI_TABLE_HEADER *)spcr);
204 1.12.2.2 christos }
205 1.12.2.2 christos
206 1.12.2.2 christos /*
207 1.12.2.2 christos * Initialize PSCI 0.2+ if implemented
208 1.12.2.2 christos */
209 1.12.2.2 christos if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_FADT, (void **)&fadt))) {
210 1.12.2.2 christos if (fadt->ArmBootFlags & ACPI_FADT_PSCI_COMPLIANT) {
211 1.12.2.2 christos if (fadt->ArmBootFlags & ACPI_FADT_PSCI_USE_HVC) {
212 1.12.2.2 christos psci_init(psci_call_hvc);
213 1.12.2.2 christos } else {
214 1.12.2.2 christos psci_init(psci_call_smc);
215 1.12.2.2 christos }
216 1.12.2.2 christos }
217 1.12.2.2 christos acpi_table_unmap((ACPI_TABLE_HEADER *)fadt);
218 1.12.2.2 christos }
219 1.12.2.2 christos
220 1.12.2.2 christos #ifdef MULTIPROCESSOR
221 1.12.2.2 christos /*
222 1.12.2.2 christos * Count CPUs
223 1.12.2.2 christos */
224 1.12.2.2 christos if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_MADT, (void **)&madt))) {
225 1.12.2.2 christos char *end = (char *)madt + madt->Header.Length;
226 1.12.2.2 christos char *where = (char *)madt + sizeof(ACPI_TABLE_MADT);
227 1.12.2.2 christos while (where < end) {
228 1.12.2.2 christos ACPI_SUBTABLE_HEADER *subtable = (ACPI_SUBTABLE_HEADER *)where;
229 1.12.2.2 christos if (subtable->Type == ACPI_MADT_TYPE_GENERIC_INTERRUPT)
230 1.12.2.2 christos arm_cpu_max++;
231 1.12.2.2 christos where += subtable->Length;
232 1.12.2.2 christos }
233 1.12.2.2 christos acpi_table_unmap((ACPI_TABLE_HEADER *)madt);
234 1.12.2.2 christos }
235 1.12.2.2 christos #endif /* MULTIPROCESSOR */
236 1.12.2.2 christos }
237 1.12.2.2 christos
238 1.12.2.2 christos static void
239 1.12.2.2 christos acpi_platform_init_attach_args(struct fdt_attach_args *faa)
240 1.12.2.2 christos {
241 1.12.2.2 christos extern struct bus_space arm_generic_bs_tag;
242 1.12.2.2 christos extern struct bus_space arm_generic_a4x_bs_tag;
243 1.12.2.2 christos
244 1.12.2.2 christos faa->faa_bst = &arm_generic_bs_tag;
245 1.12.2.2 christos faa->faa_a4x_bst = &arm_generic_a4x_bs_tag;
246 1.12.2.3 martin faa->faa_dmat = &acpi_coherent_dma_tag;
247 1.12.2.2 christos }
248 1.12.2.2 christos
249 1.12.2.2 christos static void
250 1.12.2.2 christos acpi_platform_device_register(device_t self, void *aux)
251 1.12.2.2 christos {
252 1.12.2.3 martin #if NWSDISPLAY > 0 && NGENFB > 0
253 1.12.2.3 martin if (device_is_a(self, "armfdt")) {
254 1.12.2.3 martin /*
255 1.12.2.3 martin * Setup framebuffer console, if present.
256 1.12.2.3 martin */
257 1.12.2.3 martin acpi_simplefb_preattach();
258 1.12.2.3 martin }
259 1.12.2.3 martin #endif
260 1.12.2.3 martin
261 1.12.2.2 christos #if NCOM > 0
262 1.12.2.2 christos prop_dictionary_t prop = device_properties(self);
263 1.12.2.2 christos
264 1.12.2.2 christos if (device_is_a(self, "com")) {
265 1.12.2.2 christos ACPI_TABLE_SPCR *spcr;
266 1.12.2.2 christos
267 1.12.2.2 christos if (ACPI_FAILURE(acpi_table_find(ACPI_SIG_SPCR, (void **)&spcr)))
268 1.12.2.2 christos return;
269 1.12.2.2 christos
270 1.12.2.2 christos if (spcr->SerialPort.SpaceId != ACPI_ADR_SPACE_SYSTEM_MEMORY)
271 1.12.2.2 christos goto spcr_unmap;
272 1.12.2.2 christos if (spcr->SerialPort.Address == 0)
273 1.12.2.2 christos goto spcr_unmap;
274 1.12.2.2 christos if (spcr->InterfaceType != ACPI_DBG2_16550_COMPATIBLE &&
275 1.12.2.2 christos spcr->InterfaceType != ACPI_DBG2_16550_SUBSET)
276 1.12.2.2 christos goto spcr_unmap;
277 1.12.2.2 christos
278 1.12.2.2 christos if (device_is_a(device_parent(self), "puc")) {
279 1.12.2.2 christos const struct puc_attach_args * const paa = aux;
280 1.12.2.2 christos int b, d, f;
281 1.12.2.2 christos
282 1.12.2.2 christos const int s = pci_get_segment(paa->pc);
283 1.12.2.2 christos pci_decompose_tag(paa->pc, paa->tag, &b, &d, &f);
284 1.12.2.2 christos
285 1.12.2.2 christos if (spcr->PciSegment == s && spcr->PciBus == b &&
286 1.12.2.2 christos spcr->PciDevice == d && spcr->PciFunction == f)
287 1.12.2.2 christos prop_dictionary_set_bool(prop, "force_console", true);
288 1.12.2.2 christos }
289 1.12.2.2 christos
290 1.12.2.2 christos if (device_is_a(device_parent(self), "acpi")) {
291 1.12.2.2 christos struct acpi_attach_args * const aa = aux;
292 1.12.2.2 christos struct acpi_resources res;
293 1.12.2.2 christos struct acpi_mem *mem;
294 1.12.2.2 christos
295 1.12.2.2 christos if (ACPI_FAILURE(acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
296 1.12.2.2 christos &res, &acpi_resource_parse_ops_quiet)))
297 1.12.2.2 christos goto spcr_unmap;
298 1.12.2.2 christos
299 1.12.2.2 christos mem = acpi_res_mem(&res, 0);
300 1.12.2.2 christos if (mem == NULL)
301 1.12.2.2 christos goto crs_cleanup;
302 1.12.2.2 christos
303 1.12.2.2 christos if (mem->ar_base == spcr->SerialPort.Address)
304 1.12.2.2 christos prop_dictionary_set_bool(prop, "force_console", true);
305 1.12.2.2 christos
306 1.12.2.2 christos crs_cleanup:
307 1.12.2.2 christos acpi_resource_cleanup(&res);
308 1.12.2.2 christos }
309 1.12.2.2 christos
310 1.12.2.2 christos spcr_unmap:
311 1.12.2.2 christos acpi_table_unmap((ACPI_TABLE_HEADER *)spcr);
312 1.12.2.2 christos }
313 1.12.2.2 christos #endif
314 1.12.2.2 christos }
315 1.12.2.2 christos
316 1.12.2.2 christos static void
317 1.12.2.2 christos acpi_platform_reset(void)
318 1.12.2.2 christos {
319 1.12.2.2 christos #ifdef EFI_RUNTIME
320 1.12.2.2 christos if (arm_efirt_reset(EFI_RESET_COLD) == 0)
321 1.12.2.2 christos return;
322 1.12.2.2 christos #endif
323 1.12.2.2 christos if (psci_available())
324 1.12.2.2 christos psci_system_reset();
325 1.12.2.2 christos }
326 1.12.2.2 christos
327 1.12.2.2 christos static u_int
328 1.12.2.2 christos acpi_platform_uart_freq(void)
329 1.12.2.2 christos {
330 1.12.2.2 christos return 0;
331 1.12.2.2 christos }
332 1.12.2.2 christos
333 1.12.2.2 christos static const struct arm_platform acpi_platform = {
334 1.12.2.2 christos .ap_devmap = acpi_platform_devmap,
335 1.12.2.2 christos .ap_bootstrap = acpi_platform_bootstrap,
336 1.12.2.2 christos .ap_startup = acpi_platform_startup,
337 1.12.2.2 christos .ap_init_attach_args = acpi_platform_init_attach_args,
338 1.12.2.2 christos .ap_device_register = acpi_platform_device_register,
339 1.12.2.2 christos .ap_reset = acpi_platform_reset,
340 1.12.2.2 christos .ap_delay = gtmr_delay,
341 1.12.2.2 christos .ap_uart_freq = acpi_platform_uart_freq,
342 1.12.2.2 christos };
343 1.12.2.2 christos
344 1.12.2.3 martin ARM_PLATFORM(acpi, "netbsd,generic-acpi", &acpi_platform);
345