18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * arch/arm/common/mcpm_entry.c -- entry point for multi-cluster PM
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Created by:  Nicolas Pitre, March 2012
68c2ecf20Sopenharmony_ci * Copyright:   (C) 2012-2013  Linaro Limited
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/export.h>
108c2ecf20Sopenharmony_ci#include <linux/kernel.h>
118c2ecf20Sopenharmony_ci#include <linux/init.h>
128c2ecf20Sopenharmony_ci#include <linux/irqflags.h>
138c2ecf20Sopenharmony_ci#include <linux/cpu_pm.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <asm/mcpm.h>
168c2ecf20Sopenharmony_ci#include <asm/cacheflush.h>
178c2ecf20Sopenharmony_ci#include <asm/idmap.h>
188c2ecf20Sopenharmony_ci#include <asm/cputype.h>
198c2ecf20Sopenharmony_ci#include <asm/suspend.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci/*
228c2ecf20Sopenharmony_ci * The public API for this code is documented in arch/arm/include/asm/mcpm.h.
238c2ecf20Sopenharmony_ci * For a comprehensive description of the main algorithm used here, please
248c2ecf20Sopenharmony_ci * see Documentation/arm/cluster-pm-race-avoidance.rst.
258c2ecf20Sopenharmony_ci */
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistruct sync_struct mcpm_sync;
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/*
308c2ecf20Sopenharmony_ci * __mcpm_cpu_going_down: Indicates that the cpu is being torn down.
318c2ecf20Sopenharmony_ci *    This must be called at the point of committing to teardown of a CPU.
328c2ecf20Sopenharmony_ci *    The CPU cache (SCTRL.C bit) is expected to still be active.
338c2ecf20Sopenharmony_ci */
348c2ecf20Sopenharmony_cistatic void __mcpm_cpu_going_down(unsigned int cpu, unsigned int cluster)
358c2ecf20Sopenharmony_ci{
368c2ecf20Sopenharmony_ci	mcpm_sync.clusters[cluster].cpus[cpu].cpu = CPU_GOING_DOWN;
378c2ecf20Sopenharmony_ci	sync_cache_w(&mcpm_sync.clusters[cluster].cpus[cpu].cpu);
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/*
418c2ecf20Sopenharmony_ci * __mcpm_cpu_down: Indicates that cpu teardown is complete and that the
428c2ecf20Sopenharmony_ci *    cluster can be torn down without disrupting this CPU.
438c2ecf20Sopenharmony_ci *    To avoid deadlocks, this must be called before a CPU is powered down.
448c2ecf20Sopenharmony_ci *    The CPU cache (SCTRL.C bit) is expected to be off.
458c2ecf20Sopenharmony_ci *    However L2 cache might or might not be active.
468c2ecf20Sopenharmony_ci */
478c2ecf20Sopenharmony_cistatic void __mcpm_cpu_down(unsigned int cpu, unsigned int cluster)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	dmb();
508c2ecf20Sopenharmony_ci	mcpm_sync.clusters[cluster].cpus[cpu].cpu = CPU_DOWN;
518c2ecf20Sopenharmony_ci	sync_cache_w(&mcpm_sync.clusters[cluster].cpus[cpu].cpu);
528c2ecf20Sopenharmony_ci	sev();
538c2ecf20Sopenharmony_ci}
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci/*
568c2ecf20Sopenharmony_ci * __mcpm_outbound_leave_critical: Leave the cluster teardown critical section.
578c2ecf20Sopenharmony_ci * @state: the final state of the cluster:
588c2ecf20Sopenharmony_ci *     CLUSTER_UP: no destructive teardown was done and the cluster has been
598c2ecf20Sopenharmony_ci *         restored to the previous state (CPU cache still active); or
608c2ecf20Sopenharmony_ci *     CLUSTER_DOWN: the cluster has been torn-down, ready for power-off
618c2ecf20Sopenharmony_ci *         (CPU cache disabled, L2 cache either enabled or disabled).
628c2ecf20Sopenharmony_ci */
638c2ecf20Sopenharmony_cistatic void __mcpm_outbound_leave_critical(unsigned int cluster, int state)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	dmb();
668c2ecf20Sopenharmony_ci	mcpm_sync.clusters[cluster].cluster = state;
678c2ecf20Sopenharmony_ci	sync_cache_w(&mcpm_sync.clusters[cluster].cluster);
688c2ecf20Sopenharmony_ci	sev();
698c2ecf20Sopenharmony_ci}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci/*
728c2ecf20Sopenharmony_ci * __mcpm_outbound_enter_critical: Enter the cluster teardown critical section.
738c2ecf20Sopenharmony_ci * This function should be called by the last man, after local CPU teardown
748c2ecf20Sopenharmony_ci * is complete.  CPU cache expected to be active.
758c2ecf20Sopenharmony_ci *
768c2ecf20Sopenharmony_ci * Returns:
778c2ecf20Sopenharmony_ci *     false: the critical section was not entered because an inbound CPU was
788c2ecf20Sopenharmony_ci *         observed, or the cluster is already being set up;
798c2ecf20Sopenharmony_ci *     true: the critical section was entered: it is now safe to tear down the
808c2ecf20Sopenharmony_ci *         cluster.
818c2ecf20Sopenharmony_ci */
828c2ecf20Sopenharmony_cistatic bool __mcpm_outbound_enter_critical(unsigned int cpu, unsigned int cluster)
838c2ecf20Sopenharmony_ci{
848c2ecf20Sopenharmony_ci	unsigned int i;
858c2ecf20Sopenharmony_ci	struct mcpm_sync_struct *c = &mcpm_sync.clusters[cluster];
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	/* Warn inbound CPUs that the cluster is being torn down: */
888c2ecf20Sopenharmony_ci	c->cluster = CLUSTER_GOING_DOWN;
898c2ecf20Sopenharmony_ci	sync_cache_w(&c->cluster);
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	/* Back out if the inbound cluster is already in the critical region: */
928c2ecf20Sopenharmony_ci	sync_cache_r(&c->inbound);
938c2ecf20Sopenharmony_ci	if (c->inbound == INBOUND_COMING_UP)
948c2ecf20Sopenharmony_ci		goto abort;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	/*
978c2ecf20Sopenharmony_ci	 * Wait for all CPUs to get out of the GOING_DOWN state, so that local
988c2ecf20Sopenharmony_ci	 * teardown is complete on each CPU before tearing down the cluster.
998c2ecf20Sopenharmony_ci	 *
1008c2ecf20Sopenharmony_ci	 * If any CPU has been woken up again from the DOWN state, then we
1018c2ecf20Sopenharmony_ci	 * shouldn't be taking the cluster down at all: abort in that case.
1028c2ecf20Sopenharmony_ci	 */
1038c2ecf20Sopenharmony_ci	sync_cache_r(&c->cpus);
1048c2ecf20Sopenharmony_ci	for (i = 0; i < MAX_CPUS_PER_CLUSTER; i++) {
1058c2ecf20Sopenharmony_ci		int cpustate;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci		if (i == cpu)
1088c2ecf20Sopenharmony_ci			continue;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci		while (1) {
1118c2ecf20Sopenharmony_ci			cpustate = c->cpus[i].cpu;
1128c2ecf20Sopenharmony_ci			if (cpustate != CPU_GOING_DOWN)
1138c2ecf20Sopenharmony_ci				break;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci			wfe();
1168c2ecf20Sopenharmony_ci			sync_cache_r(&c->cpus[i].cpu);
1178c2ecf20Sopenharmony_ci		}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci		switch (cpustate) {
1208c2ecf20Sopenharmony_ci		case CPU_DOWN:
1218c2ecf20Sopenharmony_ci			continue;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci		default:
1248c2ecf20Sopenharmony_ci			goto abort;
1258c2ecf20Sopenharmony_ci		}
1268c2ecf20Sopenharmony_ci	}
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	return true;
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ciabort:
1318c2ecf20Sopenharmony_ci	__mcpm_outbound_leave_critical(cluster, CLUSTER_UP);
1328c2ecf20Sopenharmony_ci	return false;
1338c2ecf20Sopenharmony_ci}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistatic int __mcpm_cluster_state(unsigned int cluster)
1368c2ecf20Sopenharmony_ci{
1378c2ecf20Sopenharmony_ci	sync_cache_r(&mcpm_sync.clusters[cluster].cluster);
1388c2ecf20Sopenharmony_ci	return mcpm_sync.clusters[cluster].cluster;
1398c2ecf20Sopenharmony_ci}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ciextern unsigned long mcpm_entry_vectors[MAX_NR_CLUSTERS][MAX_CPUS_PER_CLUSTER];
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_civoid mcpm_set_entry_vector(unsigned cpu, unsigned cluster, void *ptr)
1448c2ecf20Sopenharmony_ci{
1458c2ecf20Sopenharmony_ci	unsigned long val = ptr ? __pa_symbol(ptr) : 0;
1468c2ecf20Sopenharmony_ci	mcpm_entry_vectors[cluster][cpu] = val;
1478c2ecf20Sopenharmony_ci	sync_cache_w(&mcpm_entry_vectors[cluster][cpu]);
1488c2ecf20Sopenharmony_ci}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ciextern unsigned long mcpm_entry_early_pokes[MAX_NR_CLUSTERS][MAX_CPUS_PER_CLUSTER][2];
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_civoid mcpm_set_early_poke(unsigned cpu, unsigned cluster,
1538c2ecf20Sopenharmony_ci			 unsigned long poke_phys_addr, unsigned long poke_val)
1548c2ecf20Sopenharmony_ci{
1558c2ecf20Sopenharmony_ci	unsigned long *poke = &mcpm_entry_early_pokes[cluster][cpu][0];
1568c2ecf20Sopenharmony_ci	poke[0] = poke_phys_addr;
1578c2ecf20Sopenharmony_ci	poke[1] = poke_val;
1588c2ecf20Sopenharmony_ci	__sync_cache_range_w(poke, 2 * sizeof(*poke));
1598c2ecf20Sopenharmony_ci}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistatic const struct mcpm_platform_ops *platform_ops;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ciint __init mcpm_platform_register(const struct mcpm_platform_ops *ops)
1648c2ecf20Sopenharmony_ci{
1658c2ecf20Sopenharmony_ci	if (platform_ops)
1668c2ecf20Sopenharmony_ci		return -EBUSY;
1678c2ecf20Sopenharmony_ci	platform_ops = ops;
1688c2ecf20Sopenharmony_ci	return 0;
1698c2ecf20Sopenharmony_ci}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_cibool mcpm_is_available(void)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	return (platform_ops) ? true : false;
1748c2ecf20Sopenharmony_ci}
1758c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mcpm_is_available);
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci/*
1788c2ecf20Sopenharmony_ci * We can't use regular spinlocks. In the switcher case, it is possible
1798c2ecf20Sopenharmony_ci * for an outbound CPU to call power_down() after its inbound counterpart
1808c2ecf20Sopenharmony_ci * is already live using the same logical CPU number which trips lockdep
1818c2ecf20Sopenharmony_ci * debugging.
1828c2ecf20Sopenharmony_ci */
1838c2ecf20Sopenharmony_cistatic arch_spinlock_t mcpm_lock = __ARCH_SPIN_LOCK_UNLOCKED;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_cistatic int mcpm_cpu_use_count[MAX_NR_CLUSTERS][MAX_CPUS_PER_CLUSTER];
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistatic inline bool mcpm_cluster_unused(unsigned int cluster)
1888c2ecf20Sopenharmony_ci{
1898c2ecf20Sopenharmony_ci	int i, cnt;
1908c2ecf20Sopenharmony_ci	for (i = 0, cnt = 0; i < MAX_CPUS_PER_CLUSTER; i++)
1918c2ecf20Sopenharmony_ci		cnt |= mcpm_cpu_use_count[cluster][i];
1928c2ecf20Sopenharmony_ci	return !cnt;
1938c2ecf20Sopenharmony_ci}
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ciint mcpm_cpu_power_up(unsigned int cpu, unsigned int cluster)
1968c2ecf20Sopenharmony_ci{
1978c2ecf20Sopenharmony_ci	bool cpu_is_down, cluster_is_down;
1988c2ecf20Sopenharmony_ci	int ret = 0;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
2018c2ecf20Sopenharmony_ci	if (!platform_ops)
2028c2ecf20Sopenharmony_ci		return -EUNATCH; /* try not to shadow power_up errors */
2038c2ecf20Sopenharmony_ci	might_sleep();
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	/*
2068c2ecf20Sopenharmony_ci	 * Since this is called with IRQs enabled, and no arch_spin_lock_irq
2078c2ecf20Sopenharmony_ci	 * variant exists, we need to disable IRQs manually here.
2088c2ecf20Sopenharmony_ci	 */
2098c2ecf20Sopenharmony_ci	local_irq_disable();
2108c2ecf20Sopenharmony_ci	arch_spin_lock(&mcpm_lock);
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	cpu_is_down = !mcpm_cpu_use_count[cluster][cpu];
2138c2ecf20Sopenharmony_ci	cluster_is_down = mcpm_cluster_unused(cluster);
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	mcpm_cpu_use_count[cluster][cpu]++;
2168c2ecf20Sopenharmony_ci	/*
2178c2ecf20Sopenharmony_ci	 * The only possible values are:
2188c2ecf20Sopenharmony_ci	 * 0 = CPU down
2198c2ecf20Sopenharmony_ci	 * 1 = CPU (still) up
2208c2ecf20Sopenharmony_ci	 * 2 = CPU requested to be up before it had a chance
2218c2ecf20Sopenharmony_ci	 *     to actually make itself down.
2228c2ecf20Sopenharmony_ci	 * Any other value is a bug.
2238c2ecf20Sopenharmony_ci	 */
2248c2ecf20Sopenharmony_ci	BUG_ON(mcpm_cpu_use_count[cluster][cpu] != 1 &&
2258c2ecf20Sopenharmony_ci	       mcpm_cpu_use_count[cluster][cpu] != 2);
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	if (cluster_is_down)
2288c2ecf20Sopenharmony_ci		ret = platform_ops->cluster_powerup(cluster);
2298c2ecf20Sopenharmony_ci	if (cpu_is_down && !ret)
2308c2ecf20Sopenharmony_ci		ret = platform_ops->cpu_powerup(cpu, cluster);
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	arch_spin_unlock(&mcpm_lock);
2338c2ecf20Sopenharmony_ci	local_irq_enable();
2348c2ecf20Sopenharmony_ci	return ret;
2358c2ecf20Sopenharmony_ci}
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_citypedef typeof(cpu_reset) phys_reset_t;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_civoid mcpm_cpu_power_down(void)
2408c2ecf20Sopenharmony_ci{
2418c2ecf20Sopenharmony_ci	unsigned int mpidr, cpu, cluster;
2428c2ecf20Sopenharmony_ci	bool cpu_going_down, last_man;
2438c2ecf20Sopenharmony_ci	phys_reset_t phys_reset;
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	mpidr = read_cpuid_mpidr();
2468c2ecf20Sopenharmony_ci	cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
2478c2ecf20Sopenharmony_ci	cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
2488c2ecf20Sopenharmony_ci	pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
2498c2ecf20Sopenharmony_ci	if (WARN_ON_ONCE(!platform_ops))
2508c2ecf20Sopenharmony_ci	       return;
2518c2ecf20Sopenharmony_ci	BUG_ON(!irqs_disabled());
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	setup_mm_for_reboot();
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	__mcpm_cpu_going_down(cpu, cluster);
2568c2ecf20Sopenharmony_ci	arch_spin_lock(&mcpm_lock);
2578c2ecf20Sopenharmony_ci	BUG_ON(__mcpm_cluster_state(cluster) != CLUSTER_UP);
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	mcpm_cpu_use_count[cluster][cpu]--;
2608c2ecf20Sopenharmony_ci	BUG_ON(mcpm_cpu_use_count[cluster][cpu] != 0 &&
2618c2ecf20Sopenharmony_ci	       mcpm_cpu_use_count[cluster][cpu] != 1);
2628c2ecf20Sopenharmony_ci	cpu_going_down = !mcpm_cpu_use_count[cluster][cpu];
2638c2ecf20Sopenharmony_ci	last_man = mcpm_cluster_unused(cluster);
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	if (last_man && __mcpm_outbound_enter_critical(cpu, cluster)) {
2668c2ecf20Sopenharmony_ci		platform_ops->cpu_powerdown_prepare(cpu, cluster);
2678c2ecf20Sopenharmony_ci		platform_ops->cluster_powerdown_prepare(cluster);
2688c2ecf20Sopenharmony_ci		arch_spin_unlock(&mcpm_lock);
2698c2ecf20Sopenharmony_ci		platform_ops->cluster_cache_disable();
2708c2ecf20Sopenharmony_ci		__mcpm_outbound_leave_critical(cluster, CLUSTER_DOWN);
2718c2ecf20Sopenharmony_ci	} else {
2728c2ecf20Sopenharmony_ci		if (cpu_going_down)
2738c2ecf20Sopenharmony_ci			platform_ops->cpu_powerdown_prepare(cpu, cluster);
2748c2ecf20Sopenharmony_ci		arch_spin_unlock(&mcpm_lock);
2758c2ecf20Sopenharmony_ci		/*
2768c2ecf20Sopenharmony_ci		 * If cpu_going_down is false here, that means a power_up
2778c2ecf20Sopenharmony_ci		 * request raced ahead of us.  Even if we do not want to
2788c2ecf20Sopenharmony_ci		 * shut this CPU down, the caller still expects execution
2798c2ecf20Sopenharmony_ci		 * to return through the system resume entry path, like
2808c2ecf20Sopenharmony_ci		 * when the WFI is aborted due to a new IRQ or the like..
2818c2ecf20Sopenharmony_ci		 * So let's continue with cache cleaning in all cases.
2828c2ecf20Sopenharmony_ci		 */
2838c2ecf20Sopenharmony_ci		platform_ops->cpu_cache_disable();
2848c2ecf20Sopenharmony_ci	}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	__mcpm_cpu_down(cpu, cluster);
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	/* Now we are prepared for power-down, do it: */
2898c2ecf20Sopenharmony_ci	if (cpu_going_down)
2908c2ecf20Sopenharmony_ci		wfi();
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	/*
2938c2ecf20Sopenharmony_ci	 * It is possible for a power_up request to happen concurrently
2948c2ecf20Sopenharmony_ci	 * with a power_down request for the same CPU. In this case the
2958c2ecf20Sopenharmony_ci	 * CPU might not be able to actually enter a powered down state
2968c2ecf20Sopenharmony_ci	 * with the WFI instruction if the power_up request has removed
2978c2ecf20Sopenharmony_ci	 * the required reset condition.  We must perform a re-entry in
2988c2ecf20Sopenharmony_ci	 * the kernel as if the power_up method just had deasserted reset
2998c2ecf20Sopenharmony_ci	 * on the CPU.
3008c2ecf20Sopenharmony_ci	 */
3018c2ecf20Sopenharmony_ci	phys_reset = (phys_reset_t)(unsigned long)__pa_symbol(cpu_reset);
3028c2ecf20Sopenharmony_ci	phys_reset(__pa_symbol(mcpm_entry_point), false);
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	/* should never get here */
3058c2ecf20Sopenharmony_ci	BUG();
3068c2ecf20Sopenharmony_ci}
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ciint mcpm_wait_for_cpu_powerdown(unsigned int cpu, unsigned int cluster)
3098c2ecf20Sopenharmony_ci{
3108c2ecf20Sopenharmony_ci	int ret;
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	if (WARN_ON_ONCE(!platform_ops || !platform_ops->wait_for_powerdown))
3138c2ecf20Sopenharmony_ci		return -EUNATCH;
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	ret = platform_ops->wait_for_powerdown(cpu, cluster);
3168c2ecf20Sopenharmony_ci	if (ret)
3178c2ecf20Sopenharmony_ci		pr_warn("%s: cpu %u, cluster %u failed to power down (%d)\n",
3188c2ecf20Sopenharmony_ci			__func__, cpu, cluster, ret);
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	return ret;
3218c2ecf20Sopenharmony_ci}
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_civoid mcpm_cpu_suspend(void)
3248c2ecf20Sopenharmony_ci{
3258c2ecf20Sopenharmony_ci	if (WARN_ON_ONCE(!platform_ops))
3268c2ecf20Sopenharmony_ci		return;
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	/* Some platforms might have to enable special resume modes, etc. */
3298c2ecf20Sopenharmony_ci	if (platform_ops->cpu_suspend_prepare) {
3308c2ecf20Sopenharmony_ci		unsigned int mpidr = read_cpuid_mpidr();
3318c2ecf20Sopenharmony_ci		unsigned int cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
3328c2ecf20Sopenharmony_ci		unsigned int cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
3338c2ecf20Sopenharmony_ci		arch_spin_lock(&mcpm_lock);
3348c2ecf20Sopenharmony_ci		platform_ops->cpu_suspend_prepare(cpu, cluster);
3358c2ecf20Sopenharmony_ci		arch_spin_unlock(&mcpm_lock);
3368c2ecf20Sopenharmony_ci	}
3378c2ecf20Sopenharmony_ci	mcpm_cpu_power_down();
3388c2ecf20Sopenharmony_ci}
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ciint mcpm_cpu_powered_up(void)
3418c2ecf20Sopenharmony_ci{
3428c2ecf20Sopenharmony_ci	unsigned int mpidr, cpu, cluster;
3438c2ecf20Sopenharmony_ci	bool cpu_was_down, first_man;
3448c2ecf20Sopenharmony_ci	unsigned long flags;
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	if (!platform_ops)
3478c2ecf20Sopenharmony_ci		return -EUNATCH;
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	mpidr = read_cpuid_mpidr();
3508c2ecf20Sopenharmony_ci	cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
3518c2ecf20Sopenharmony_ci	cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
3528c2ecf20Sopenharmony_ci	local_irq_save(flags);
3538c2ecf20Sopenharmony_ci	arch_spin_lock(&mcpm_lock);
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	cpu_was_down = !mcpm_cpu_use_count[cluster][cpu];
3568c2ecf20Sopenharmony_ci	first_man = mcpm_cluster_unused(cluster);
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	if (first_man && platform_ops->cluster_is_up)
3598c2ecf20Sopenharmony_ci		platform_ops->cluster_is_up(cluster);
3608c2ecf20Sopenharmony_ci	if (cpu_was_down)
3618c2ecf20Sopenharmony_ci		mcpm_cpu_use_count[cluster][cpu] = 1;
3628c2ecf20Sopenharmony_ci	if (platform_ops->cpu_is_up)
3638c2ecf20Sopenharmony_ci		platform_ops->cpu_is_up(cpu, cluster);
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	arch_spin_unlock(&mcpm_lock);
3668c2ecf20Sopenharmony_ci	local_irq_restore(flags);
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	return 0;
3698c2ecf20Sopenharmony_ci}
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci#ifdef CONFIG_ARM_CPU_SUSPEND
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_cistatic int __init nocache_trampoline(unsigned long _arg)
3748c2ecf20Sopenharmony_ci{
3758c2ecf20Sopenharmony_ci	void (*cache_disable)(void) = (void *)_arg;
3768c2ecf20Sopenharmony_ci	unsigned int mpidr = read_cpuid_mpidr();
3778c2ecf20Sopenharmony_ci	unsigned int cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
3788c2ecf20Sopenharmony_ci	unsigned int cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
3798c2ecf20Sopenharmony_ci	phys_reset_t phys_reset;
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	mcpm_set_entry_vector(cpu, cluster, cpu_resume_no_hyp);
3828c2ecf20Sopenharmony_ci	setup_mm_for_reboot();
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	__mcpm_cpu_going_down(cpu, cluster);
3858c2ecf20Sopenharmony_ci	BUG_ON(!__mcpm_outbound_enter_critical(cpu, cluster));
3868c2ecf20Sopenharmony_ci	cache_disable();
3878c2ecf20Sopenharmony_ci	__mcpm_outbound_leave_critical(cluster, CLUSTER_DOWN);
3888c2ecf20Sopenharmony_ci	__mcpm_cpu_down(cpu, cluster);
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	phys_reset = (phys_reset_t)(unsigned long)__pa_symbol(cpu_reset);
3918c2ecf20Sopenharmony_ci	phys_reset(__pa_symbol(mcpm_entry_point), false);
3928c2ecf20Sopenharmony_ci	BUG();
3938c2ecf20Sopenharmony_ci}
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ciint __init mcpm_loopback(void (*cache_disable)(void))
3968c2ecf20Sopenharmony_ci{
3978c2ecf20Sopenharmony_ci	int ret;
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	/*
4008c2ecf20Sopenharmony_ci	 * We're going to soft-restart the current CPU through the
4018c2ecf20Sopenharmony_ci	 * low-level MCPM code by leveraging the suspend/resume
4028c2ecf20Sopenharmony_ci	 * infrastructure. Let's play it safe by using cpu_pm_enter()
4038c2ecf20Sopenharmony_ci	 * in case the CPU init code path resets the VFP or similar.
4048c2ecf20Sopenharmony_ci	 */
4058c2ecf20Sopenharmony_ci	local_irq_disable();
4068c2ecf20Sopenharmony_ci	local_fiq_disable();
4078c2ecf20Sopenharmony_ci	ret = cpu_pm_enter();
4088c2ecf20Sopenharmony_ci	if (!ret) {
4098c2ecf20Sopenharmony_ci		ret = cpu_suspend((unsigned long)cache_disable, nocache_trampoline);
4108c2ecf20Sopenharmony_ci		cpu_pm_exit();
4118c2ecf20Sopenharmony_ci	}
4128c2ecf20Sopenharmony_ci	local_fiq_enable();
4138c2ecf20Sopenharmony_ci	local_irq_enable();
4148c2ecf20Sopenharmony_ci	if (ret)
4158c2ecf20Sopenharmony_ci		pr_err("%s returned %d\n", __func__, ret);
4168c2ecf20Sopenharmony_ci	return ret;
4178c2ecf20Sopenharmony_ci}
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci#endif
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ciextern unsigned long mcpm_power_up_setup_phys;
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ciint __init mcpm_sync_init(
4248c2ecf20Sopenharmony_ci	void (*power_up_setup)(unsigned int affinity_level))
4258c2ecf20Sopenharmony_ci{
4268c2ecf20Sopenharmony_ci	unsigned int i, j, mpidr, this_cluster;
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	BUILD_BUG_ON(MCPM_SYNC_CLUSTER_SIZE * MAX_NR_CLUSTERS != sizeof mcpm_sync);
4298c2ecf20Sopenharmony_ci	BUG_ON((unsigned long)&mcpm_sync & (__CACHE_WRITEBACK_GRANULE - 1));
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	/*
4328c2ecf20Sopenharmony_ci	 * Set initial CPU and cluster states.
4338c2ecf20Sopenharmony_ci	 * Only one cluster is assumed to be active at this point.
4348c2ecf20Sopenharmony_ci	 */
4358c2ecf20Sopenharmony_ci	for (i = 0; i < MAX_NR_CLUSTERS; i++) {
4368c2ecf20Sopenharmony_ci		mcpm_sync.clusters[i].cluster = CLUSTER_DOWN;
4378c2ecf20Sopenharmony_ci		mcpm_sync.clusters[i].inbound = INBOUND_NOT_COMING_UP;
4388c2ecf20Sopenharmony_ci		for (j = 0; j < MAX_CPUS_PER_CLUSTER; j++)
4398c2ecf20Sopenharmony_ci			mcpm_sync.clusters[i].cpus[j].cpu = CPU_DOWN;
4408c2ecf20Sopenharmony_ci	}
4418c2ecf20Sopenharmony_ci	mpidr = read_cpuid_mpidr();
4428c2ecf20Sopenharmony_ci	this_cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
4438c2ecf20Sopenharmony_ci	for_each_online_cpu(i) {
4448c2ecf20Sopenharmony_ci		mcpm_cpu_use_count[this_cluster][i] = 1;
4458c2ecf20Sopenharmony_ci		mcpm_sync.clusters[this_cluster].cpus[i].cpu = CPU_UP;
4468c2ecf20Sopenharmony_ci	}
4478c2ecf20Sopenharmony_ci	mcpm_sync.clusters[this_cluster].cluster = CLUSTER_UP;
4488c2ecf20Sopenharmony_ci	sync_cache_w(&mcpm_sync);
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	if (power_up_setup) {
4518c2ecf20Sopenharmony_ci		mcpm_power_up_setup_phys = __pa_symbol(power_up_setup);
4528c2ecf20Sopenharmony_ci		sync_cache_w(&mcpm_power_up_setup_phys);
4538c2ecf20Sopenharmony_ci	}
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	return 0;
4568c2ecf20Sopenharmony_ci}
457