Home | History | Annotate | Line # | Download | only in fdt
psci_fdt.c revision 1.7
      1 /* $NetBSD: psci_fdt.c,v 1.7 2018/07/09 09:10:28 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include "opt_multiprocessor.h"
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: psci_fdt.c,v 1.7 2018/07/09 09:10:28 jmcneill Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/bus.h>
     36 #include <sys/device.h>
     37 #include <sys/systm.h>
     38 #include <sys/kernel.h>
     39 #include <sys/atomic.h>
     40 
     41 #include <dev/fdt/fdtvar.h>
     42 
     43 #include <arm/locore.h>
     44 #include <arm/armreg.h>
     45 
     46 #include <arm/arm/psci.h>
     47 #include <arm/fdt/psci_fdt.h>
     48 
     49 static int	psci_fdt_match(device_t, cfdata_t, void *);
     50 static void	psci_fdt_attach(device_t, device_t, void *);
     51 
     52 static int	psci_fdt_init(const int);
     53 
     54 static const char * const compatible[] = {
     55 	"arm,psci",
     56 	"arm,psci-0.2",
     57 	"arm,psci-1.0",
     58 	NULL
     59 };
     60 
     61 CFATTACH_DECL_NEW(psci_fdt, 0, psci_fdt_match, psci_fdt_attach, NULL, NULL);
     62 
     63 static void
     64 psci_fdt_power_reset(device_t dev)
     65 {
     66 	delay(500000);
     67 	psci_system_reset();
     68 }
     69 
     70 static void
     71 psci_fdt_power_poweroff(device_t dev)
     72 {
     73 	delay(500000);
     74 	psci_system_off();
     75 }
     76 
     77 static const struct fdtbus_power_controller_func psci_power_funcs = {
     78 	.reset = psci_fdt_power_reset,
     79 	.poweroff = psci_fdt_power_poweroff,
     80 };
     81 
     82 static int
     83 psci_fdt_match(device_t parent, cfdata_t cf, void *aux)
     84 {
     85 	struct fdt_attach_args * const faa = aux;
     86 
     87 	return of_match_compatible(faa->faa_phandle, compatible);
     88 }
     89 
     90 static void
     91 psci_fdt_attach(device_t parent, device_t self, void *aux)
     92 {
     93 	struct fdt_attach_args * const faa = aux;
     94 	const int phandle = faa->faa_phandle;
     95 
     96 	psci_fdt_init(phandle);
     97 
     98 	const uint32_t ver = psci_version();
     99 	const u_int ver_maj = __SHIFTOUT(ver, PSCI_VERSION_MAJOR);
    100 	const u_int ver_min = __SHIFTOUT(ver, PSCI_VERSION_MINOR);
    101 
    102 	aprint_naive("\n");
    103 	aprint_normal(": PSCI %u.%u\n", ver_maj, ver_min);
    104 
    105 	fdtbus_register_power_controller(self, phandle,
    106 	    &psci_power_funcs);
    107 }
    108 
    109 static int
    110 psci_fdt_init(const int phandle)
    111 {
    112 	const char *method, *psciver;
    113 	uint32_t val;
    114 
    115 	method = fdtbus_get_string(phandle, "method");
    116 	psciver = fdtbus_get_string(phandle, "compatible");
    117 	if (method == NULL || psciver == NULL) {
    118 		aprint_error("PSCI: missing required property on /psci\n");
    119 		return EINVAL;
    120 	}
    121 
    122 	if (strcmp(method, "smc") == 0)
    123 		psci_init(psci_call_smc);
    124 	else if (strcmp(method, "hvc") == 0)
    125 		psci_init(psci_call_hvc);
    126 	else {
    127 		aprint_error("PSCI: unsupported method '%s'\n", method);
    128 		return EINVAL;
    129 	}
    130 
    131 	/*
    132 	 * If the first compatible string is "arm,psci" then we
    133 	 * are dealing with PSCI 0.1
    134 	 */
    135 	if (strcmp(psciver, "arm,psci") == 0) {
    136 		psci_clearfunc();
    137 		if (of_getprop_uint32(phandle, "cpu_on", &val) == 0)
    138 			psci_setfunc(PSCI_FUNC_CPU_ON, val);
    139 	}
    140 
    141 	return 0;
    142 }
    143 
    144 static int
    145 psci_fdt_preinit(void)
    146 {
    147 	const int phandle = OF_finddevice("/psci");
    148 	if (phandle == -1) {
    149 		aprint_error("PSCI: no /psci node found\n");
    150 		return ENODEV;
    151 	}
    152 
    153 	return psci_fdt_init(phandle);
    154 }
    155 
    156 static bus_addr_t psci_fdt_read_mpidr_aff(void)
    157 {
    158 #ifdef __aarch64__
    159 	return reg_mpidr_el1_read() & (MPIDR_AFF3|MPIDR_AFF2|MPIDR_AFF1|MPIDR_AFF0);
    160 #else
    161 	return armreg_mpidr_read() & (MPIDR_AFF2|MPIDR_AFF1|MPIDR_AFF0);
    162 #endif
    163 }
    164 
    165 static register_t
    166 psci_fdt_mpstart_pa(void)
    167 {
    168 #ifdef __aarch64__
    169 	extern void aarch64_mpstart(void);
    170 	return (register_t)aarch64_kern_vtophys(aarch64_mpstart);
    171 #else
    172 	return (register_t)cortex_mpstart;
    173 #endif
    174 }
    175 
    176 void
    177 psci_fdt_bootstrap(void)
    178 {
    179 #ifdef MULTIPROCESSOR
    180 	extern void cortex_mpstart(void);
    181 	bus_addr_t mpidr, bp_mpidr;
    182 	int child;
    183 
    184 	const int cpus = OF_finddevice("/cpus");
    185 	if (cpus == -1) {
    186 		aprint_error("PSCI: no /cpus node found\n");
    187 		arm_cpu_max = 1;
    188 		return;
    189 	}
    190 
    191 	/* Count CPUs */
    192 	arm_cpu_max = 0;
    193 	for (child = OF_child(cpus); child; child = OF_peer(child))
    194 		if (fdtbus_status_okay(child))
    195 			arm_cpu_max++;
    196 
    197 	if (psci_fdt_preinit() != 0)
    198 		return;
    199 
    200 	/* MPIDR affinity levels of boot processor. */
    201 	bp_mpidr = psci_fdt_read_mpidr_aff();
    202 
    203 	/* Boot APs */
    204 	uint32_t started = 0;
    205 	for (child = OF_child(cpus); child; child = OF_peer(child)) {
    206 		if (!fdtbus_status_okay(child))
    207 			continue;
    208 		if (fdtbus_get_reg(child, 0, &mpidr, NULL) != 0)
    209 			continue;
    210 		if (mpidr == bp_mpidr)
    211 			continue; 	/* BP already started */
    212 
    213 		/* XXX NetBSD requires all CPUs to be in the same cluster */
    214 		if ((mpidr & ~MPIDR_AFF0) != (bp_mpidr & ~MPIDR_AFF0))
    215 			continue;
    216 
    217 		const u_int cpuid = __SHIFTOUT(mpidr, MPIDR_AFF0);
    218 		int ret = psci_cpu_on(cpuid, psci_fdt_mpstart_pa(), 0);
    219 		if (ret == PSCI_SUCCESS)
    220 			started |= __BIT(cpuid);
    221 	}
    222 
    223 	/* Wait for APs to start */
    224 	for (u_int i = 0x10000000; i > 0; i--) {
    225 		membar_consumer();
    226 		if (arm_cpu_hatched == started)
    227 			break;
    228 	}
    229 #endif
    230 }
    231 
    232 void
    233 psci_fdt_reset(void)
    234 {
    235 	if (psci_fdt_preinit() != 0) {
    236 		aprint_error("PSCI: reset failed\n");
    237 		return;
    238 	}
    239 
    240 	psci_system_reset();
    241 }
    242