1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
/* $NetBSD: acpi_platform.c,v 1.36 2023/04/07 08:55:29 skrll Exp $ */
/*-
* Copyright (c) 2018 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jared McNeill <jmcneill@invisible.ca>.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "com.h"
#include "plcom.h"
#include "opt_efi.h"
#include "opt_multiprocessor.h"
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: acpi_platform.c,v 1.36 2023/04/07 08:55:29 skrll Exp $");
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/cpu.h>
#include <sys/device.h>
#include <sys/termios.h>
#include <sys/kprintf.h>
#include <dev/fdt/fdtvar.h>
#include <uvm/uvm_extern.h>
#include <machine/bootconfig.h>
#include <arm/cpufunc.h>
#include <arm/locore.h>
#include <arm/cortex/gtmr_var.h>
#include <arm/arm/smccc.h>
#include <arm/arm/psci.h>
#include <arm/fdt/psci_fdtvar.h>
#include <evbarm/fdt/platform.h>
#include <evbarm/dev/plcomreg.h>
#include <evbarm/dev/plcomvar.h>
#include <dev/ic/ns16550reg.h>
#include <dev/ic/comreg.h>
#include <dev/ic/comvar.h>
#if NCOM > 0
#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pucvar.h>
#endif
#ifdef EFI_RUNTIME
#include <arm/arm/efi_runtime.h>
#endif
#include <dev/acpi/acpireg.h>
#include <dev/acpi/acpivar.h>
#include <arm/acpi/acpi_table.h>
#include <libfdt.h>
#define SPCR_BAUD_DEFAULT 0
#define SPCR_BAUD_9600 3
#define SPCR_BAUD_19200 4
#define SPCR_BAUD_57600 6
#define SPCR_BAUD_115200 7
static const struct acpi_spcr_baud_rate {
uint8_t id;
int baud_rate;
} acpi_spcr_baud_rates[] = {
/*
* SPCR_BAUD_DEFAULT means:
* "As is, operating system relies on the current configuration
* of serial port until the full featured driver will be
* initialized."
*
* We don't currently have a good way of telling the UART driver
* to detect the currently configured baud rate, so just pick
* something sensible here.
*
* In the past we have tried baud_rate values of 0 and -1, but
* these cause problems with the com(4) driver.
*/
{ SPCR_BAUD_DEFAULT, 115200 },
{ SPCR_BAUD_9600, 9600 },
{ SPCR_BAUD_19200, 19200 },
{ SPCR_BAUD_57600, 57600 },
{ SPCR_BAUD_115200, 115200 },
};
extern struct bus_space arm_generic_bs_tag;
#if NPLCOM > 0
static struct plcom_instance plcom_console;
#endif
struct arm32_bus_dma_tag acpi_coherent_dma_tag;
static struct arm32_dma_range acpi_coherent_ranges[] = {
[0] = {
.dr_sysbase = 0,
.dr_busbase = 0,
.dr_len = UINTPTR_MAX,
.dr_flags = _BUS_DMAMAP_COHERENT,
}
};
static const struct pmap_devmap *
acpi_platform_devmap(void)
{
static const struct pmap_devmap devmap[] = {
DEVMAP_ENTRY_END
};
return devmap;
}
static void
acpi_platform_bootstrap(void)
{
extern struct arm32_bus_dma_tag arm_generic_dma_tag;
acpi_coherent_dma_tag = arm_generic_dma_tag;
acpi_coherent_dma_tag._ranges = acpi_coherent_ranges;
acpi_coherent_dma_tag._nranges = __arraycount(acpi_coherent_ranges);
}
static void
acpi_platform_attach_uart(ACPI_TABLE_SPCR *spcr)
{
#if NCOM > 0
struct com_regs regs;
bus_space_handle_t dummy_bsh;
u_int reg_shift;
#endif
int baud_rate, n;
/*
* Only MMIO access is supported today.
*/
if (spcr->SerialPort.SpaceId != ACPI_ADR_SPACE_SYSTEM_MEMORY) {
return;
}
if (le64toh(spcr->SerialPort.Address) == 0) {
return;
}
/*
* Lookup SPCR baud rate.
*/
baud_rate = 0;
for (n = 0; n < __arraycount(acpi_spcr_baud_rates); n++) {
if (acpi_spcr_baud_rates[n].id == spcr->BaudRate) {
baud_rate = acpi_spcr_baud_rates[n].baud_rate;
break;
}
}
/*
* Attach console device.
*/
switch (spcr->InterfaceType) {
#if NPLCOM > 0
case ACPI_DBG2_ARM_PL011:
case ACPI_DBG2_ARM_SBSA_32BIT:
case ACPI_DBG2_ARM_SBSA_GENERIC:
if (spcr->InterfaceType == ACPI_DBG2_ARM_PL011)
plcom_console.pi_type = PLCOM_TYPE_PL011;
else
plcom_console.pi_type = PLCOM_TYPE_GENERIC_UART;
plcom_console.pi_iot = &arm_generic_bs_tag;
plcom_console.pi_iobase = le64toh(spcr->SerialPort.Address);
plcom_console.pi_size = PL011COM_UART_SIZE;
plcom_console.pi_flags = PLC_FLAG_32BIT_ACCESS;
plcomcnattach(&plcom_console, baud_rate, 0, TTYDEF_CFLAG, -1);
break;
#endif
#if NCOM > 0
case ACPI_DBG2_16550_COMPATIBLE:
case ACPI_DBG2_16550_SUBSET:
case ACPI_DBG2_16550_WITH_GAS:
memset(&dummy_bsh, 0, sizeof(dummy_bsh));
switch (spcr->SerialPort.BitWidth) {
case 8:
reg_shift = 0;
break;
case 16:
reg_shift = 1;
break;
case 32:
reg_shift = 2;
break;
default:
/*
* Bit width 0 is possible for types 0 and 1. Otherwise,
* possibly buggy firmware.
*/
if (spcr->InterfaceType == ACPI_DBG2_16550_COMPATIBLE) {
reg_shift = 0;
} else {
reg_shift = 2;
}
break;
}
com_init_regs_stride(®s, &arm_generic_bs_tag, dummy_bsh,
le64toh(spcr->SerialPort.Address), reg_shift);
comcnattach1(®s, baud_rate, -1, COM_TYPE_NORMAL,
TTYDEF_CFLAG);
break;
case ACPI_DBG2_BCM2835:
memset(&dummy_bsh, 0, sizeof(dummy_bsh));
com_init_regs_stride(®s, &arm_generic_bs_tag, dummy_bsh,
le64toh(spcr->SerialPort.Address) + 0x40, 2);
comcnattach1(®s, baud_rate, -1, COM_TYPE_BCMAUXUART,
TTYDEF_CFLAG);
cn_set_magic("+++++");
break;
#endif
default:
printf("SPCR: kernel does not support interface type %#x\n",
spcr->InterfaceType);
break;
}
/*
* UEFI firmware may leave the console in an undesireable state (wrong
* foreground/background colour, etc). Reset the terminal and clear
* text from the cursor to the end of the screen.
*/
printf_flags(TOCONS|NOTSTAMP, "\033[0m");
printf_flags(TOCONS|NOTSTAMP, "\033[0J");
}
static void
acpi_platform_startup(void)
{
ACPI_TABLE_SPCR *spcr;
ACPI_TABLE_FADT *fadt;
#ifdef MULTIPROCESSOR
ACPI_TABLE_MADT *madt;
#endif
/*
* Setup serial console device
*/
if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_SPCR, (void **)&spcr))) {
acpi_platform_attach_uart(spcr);
acpi_table_unmap((ACPI_TABLE_HEADER *)spcr);
}
/*
* Initialize PSCI 0.2+ if implemented
*/
if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_FADT, (void **)&fadt))) {
const uint16_t boot_flags = le16toh(fadt->ArmBootFlags);
if ((boot_flags & ACPI_FADT_PSCI_COMPLIANT) != 0) {
if ((boot_flags & ACPI_FADT_PSCI_USE_HVC) != 0) {
psci_init(psci_call_hvc);
} else {
psci_init(psci_call_smc);
}
smccc_probe();
}
acpi_table_unmap((ACPI_TABLE_HEADER *)fadt);
}
#ifdef MULTIPROCESSOR
/*
* Count CPUs
*/
if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_MADT, (void **)&madt))) {
char *end = (char *)madt + le32toh(madt->Header.Length);
char *where = (char *)madt + sizeof(ACPI_TABLE_MADT);
while (where < end) {
ACPI_SUBTABLE_HEADER *subtable =
(ACPI_SUBTABLE_HEADER *)where;
if (subtable->Type == ACPI_MADT_TYPE_GENERIC_INTERRUPT)
arm_cpu_max++;
where += subtable->Length;
}
acpi_table_unmap((ACPI_TABLE_HEADER *)madt);
}
#endif /* MULTIPROCESSOR */
}
static void
acpi_platform_init_attach_args(struct fdt_attach_args *faa)
{
extern struct bus_space arm_generic_bs_tag;
faa->faa_bst = &arm_generic_bs_tag;
faa->faa_dmat = &acpi_coherent_dma_tag;
}
static void
acpi_platform_device_register(device_t self, void *aux)
{
#if NCOM > 0
prop_dictionary_t prop = device_properties(self);
ACPI_STATUS rv;
if (device_is_a(self, "com")) {
ACPI_TABLE_SPCR *spcr;
rv = acpi_table_find(ACPI_SIG_SPCR, (void **)&spcr);
if (ACPI_FAILURE(rv)) {
return;
}
if (spcr->SerialPort.SpaceId != ACPI_ADR_SPACE_SYSTEM_MEMORY) {
goto spcr_unmap;
}
if (le64toh(spcr->SerialPort.Address) == 0) {
goto spcr_unmap;
}
if (spcr->InterfaceType != ACPI_DBG2_16550_COMPATIBLE &&
spcr->InterfaceType != ACPI_DBG2_16550_SUBSET) {
goto spcr_unmap;
}
if (device_is_a(device_parent(self), "puc")) {
const struct puc_attach_args * const paa = aux;
int b, d, f;
const int s = pci_get_segment(paa->pc);
pci_decompose_tag(paa->pc, paa->tag, &b, &d, &f);
if (spcr->PciSegment == s && spcr->PciBus == b &&
spcr->PciDevice == d && spcr->PciFunction == f) {
prop_dictionary_set_bool(prop,
"force_console", true);
}
}
if (device_is_a(device_parent(self), "acpi")) {
struct acpi_attach_args * const aa = aux;
struct acpi_resources res;
struct acpi_mem *mem;
if (ACPI_FAILURE(acpi_resource_parse(self,
aa->aa_node->ad_handle, "_CRS", &res,
&acpi_resource_parse_ops_quiet))) {
goto spcr_unmap;
}
mem = acpi_res_mem(&res, 0);
if (mem == NULL) {
goto crs_cleanup;
}
if (mem->ar_base == le64toh(spcr->SerialPort.Address)) {
prop_dictionary_set_bool(prop,
"force_console", true);
}
crs_cleanup:
acpi_resource_cleanup(&res);
}
spcr_unmap:
acpi_table_unmap((ACPI_TABLE_HEADER *)spcr);
}
#endif
}
static void
acpi_platform_reset(void)
{
#ifdef EFI_RUNTIME
if (arm_efirt_reset(EFI_RESET_COLD) == 0)
return;
#endif
if (psci_available())
psci_system_reset();
}
static u_int
acpi_platform_uart_freq(void)
{
return 0;
}
static const struct fdt_platform acpi_platform = {
.fp_devmap = acpi_platform_devmap,
.fp_bootstrap = acpi_platform_bootstrap,
.fp_startup = acpi_platform_startup,
.fp_init_attach_args = acpi_platform_init_attach_args,
.fp_device_register = acpi_platform_device_register,
.fp_reset = acpi_platform_reset,
.fp_delay = gtmr_delay,
.fp_uart_freq = acpi_platform_uart_freq,
};
FDT_PLATFORM(acpi, "netbsd,generic-acpi", &acpi_platform);
|