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