Home | History | Annotate | Line # | Download | only in fdt
psci_fdt.c revision 1.3.4.2
      1 /* $NetBSD: psci_fdt.c,v 1.3.4.2 2018/07/28 04:37:28 pgoyette 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.3.4.2 2018/07/28 04:37:28 pgoyette 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 #ifdef MULTIPROCESSOR
    157 static bus_addr_t psci_fdt_read_mpidr_aff(void)
    158 {
    159 #ifdef __aarch64__
    160 	return reg_mpidr_el1_read() & (MPIDR_AFF3|MPIDR_AFF2|MPIDR_AFF1|MPIDR_AFF0);
    161 #else
    162 	return armreg_mpidr_read() & (MPIDR_AFF2|MPIDR_AFF1|MPIDR_AFF0);
    163 #endif
    164 }
    165 
    166 static register_t
    167 psci_fdt_mpstart_pa(void)
    168 {
    169 #ifdef __aarch64__
    170 	extern void aarch64_mpstart(void);
    171 	return (register_t)aarch64_kern_vtophys((vaddr_t)aarch64_mpstart);
    172 #else
    173 	extern void cortex_mpstart(void);
    174 	return (register_t)cortex_mpstart;
    175 #endif
    176 }
    177 #endif
    178 
    179 void
    180 psci_fdt_bootstrap(void)
    181 {
    182 #ifdef MULTIPROCESSOR
    183 	extern void cortex_mpstart(void);
    184 	bus_addr_t mpidr, bp_mpidr;
    185 	int child;
    186 
    187 	const int cpus = OF_finddevice("/cpus");
    188 	if (cpus == -1) {
    189 		aprint_error("PSCI: no /cpus node found\n");
    190 		arm_cpu_max = 1;
    191 		return;
    192 	}
    193 
    194 	/* Count CPUs */
    195 	arm_cpu_max = 0;
    196 	for (child = OF_child(cpus); child; child = OF_peer(child))
    197 		if (fdtbus_status_okay(child))
    198 			arm_cpu_max++;
    199 
    200 	if (psci_fdt_preinit() != 0)
    201 		return;
    202 
    203 	/* MPIDR affinity levels of boot processor. */
    204 	bp_mpidr = psci_fdt_read_mpidr_aff();
    205 
    206 	/* Boot APs */
    207 	uint32_t started = 0;
    208 	for (child = OF_child(cpus); child; child = OF_peer(child)) {
    209 		if (!fdtbus_status_okay(child))
    210 			continue;
    211 		if (fdtbus_get_reg(child, 0, &mpidr, NULL) != 0)
    212 			continue;
    213 		if (mpidr == bp_mpidr)
    214 			continue; 	/* BP already started */
    215 
    216 		/* XXX NetBSD requires all CPUs to be in the same cluster */
    217 		if ((mpidr & ~MPIDR_AFF0) != (bp_mpidr & ~MPIDR_AFF0))
    218 			continue;
    219 
    220 		const u_int cpuid = __SHIFTOUT(mpidr, MPIDR_AFF0);
    221 		int ret = psci_cpu_on(cpuid, psci_fdt_mpstart_pa(), 0);
    222 		if (ret == PSCI_SUCCESS)
    223 			started |= __BIT(cpuid);
    224 	}
    225 
    226 	/* Wait for APs to start */
    227 	for (u_int i = 0x10000000; i > 0; i--) {
    228 		membar_consumer();
    229 		if (arm_cpu_hatched == started)
    230 			break;
    231 	}
    232 #endif
    233 }
    234 
    235 void
    236 psci_fdt_reset(void)
    237 {
    238 	if (psci_fdt_preinit() != 0) {
    239 		aprint_error("PSCI: reset failed\n");
    240 		return;
    241 	}
    242 
    243 	psci_system_reset();
    244 }
    245