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