162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * arch/arm/include/asm/mcpm.h
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Created by:  Nicolas Pitre, April 2012
662306a36Sopenharmony_ci * Copyright:   (C) 2012-2013  Linaro Limited
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#ifndef MCPM_H
1062306a36Sopenharmony_ci#define MCPM_H
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci/*
1362306a36Sopenharmony_ci * Maximum number of possible clusters / CPUs per cluster.
1462306a36Sopenharmony_ci *
1562306a36Sopenharmony_ci * This should be sufficient for quite a while, while keeping the
1662306a36Sopenharmony_ci * (assembly) code simpler.  When this starts to grow then we'll have
1762306a36Sopenharmony_ci * to consider dynamic allocation.
1862306a36Sopenharmony_ci */
1962306a36Sopenharmony_ci#define MAX_CPUS_PER_CLUSTER	4
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci#ifdef CONFIG_MCPM_QUAD_CLUSTER
2262306a36Sopenharmony_ci#define MAX_NR_CLUSTERS		4
2362306a36Sopenharmony_ci#else
2462306a36Sopenharmony_ci#define MAX_NR_CLUSTERS		2
2562306a36Sopenharmony_ci#endif
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci#ifndef __ASSEMBLY__
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci#include <linux/types.h>
3062306a36Sopenharmony_ci#include <asm/cacheflush.h>
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci/*
3362306a36Sopenharmony_ci * Platform specific code should use this symbol to set up secondary
3462306a36Sopenharmony_ci * entry location for processors to use when released from reset.
3562306a36Sopenharmony_ci */
3662306a36Sopenharmony_ciextern void mcpm_entry_point(void);
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci/*
3962306a36Sopenharmony_ci * This is used to indicate where the given CPU from given cluster should
4062306a36Sopenharmony_ci * branch once it is ready to re-enter the kernel using ptr, or NULL if it
4162306a36Sopenharmony_ci * should be gated.  A gated CPU is held in a WFE loop until its vector
4262306a36Sopenharmony_ci * becomes non NULL.
4362306a36Sopenharmony_ci */
4462306a36Sopenharmony_civoid mcpm_set_entry_vector(unsigned cpu, unsigned cluster, void *ptr);
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci/*
4762306a36Sopenharmony_ci * This sets an early poke i.e a value to be poked into some address
4862306a36Sopenharmony_ci * from very early assembly code before the CPU is ungated.  The
4962306a36Sopenharmony_ci * address must be physical, and if 0 then nothing will happen.
5062306a36Sopenharmony_ci */
5162306a36Sopenharmony_civoid mcpm_set_early_poke(unsigned cpu, unsigned cluster,
5262306a36Sopenharmony_ci			 unsigned long poke_phys_addr, unsigned long poke_val);
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci/*
5562306a36Sopenharmony_ci * CPU/cluster power operations API for higher subsystems to use.
5662306a36Sopenharmony_ci */
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci/**
5962306a36Sopenharmony_ci * mcpm_is_available - returns whether MCPM is initialized and available
6062306a36Sopenharmony_ci *
6162306a36Sopenharmony_ci * This returns true or false accordingly.
6262306a36Sopenharmony_ci */
6362306a36Sopenharmony_cibool mcpm_is_available(void);
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci/**
6662306a36Sopenharmony_ci * mcpm_cpu_power_up - make given CPU in given cluster runable
6762306a36Sopenharmony_ci *
6862306a36Sopenharmony_ci * @cpu: CPU number within given cluster
6962306a36Sopenharmony_ci * @cluster: cluster number for the CPU
7062306a36Sopenharmony_ci *
7162306a36Sopenharmony_ci * The identified CPU is brought out of reset.  If the cluster was powered
7262306a36Sopenharmony_ci * down then it is brought up as well, taking care not to let the other CPUs
7362306a36Sopenharmony_ci * in the cluster run, and ensuring appropriate cluster setup.
7462306a36Sopenharmony_ci *
7562306a36Sopenharmony_ci * Caller must ensure the appropriate entry vector is initialized with
7662306a36Sopenharmony_ci * mcpm_set_entry_vector() prior to calling this.
7762306a36Sopenharmony_ci *
7862306a36Sopenharmony_ci * This must be called in a sleepable context.  However, the implementation
7962306a36Sopenharmony_ci * is strongly encouraged to return early and let the operation happen
8062306a36Sopenharmony_ci * asynchronously, especially when significant delays are expected.
8162306a36Sopenharmony_ci *
8262306a36Sopenharmony_ci * If the operation cannot be performed then an error code is returned.
8362306a36Sopenharmony_ci */
8462306a36Sopenharmony_ciint mcpm_cpu_power_up(unsigned int cpu, unsigned int cluster);
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci/**
8762306a36Sopenharmony_ci * mcpm_cpu_power_down - power the calling CPU down
8862306a36Sopenharmony_ci *
8962306a36Sopenharmony_ci * The calling CPU is powered down.
9062306a36Sopenharmony_ci *
9162306a36Sopenharmony_ci * If this CPU is found to be the "last man standing" in the cluster
9262306a36Sopenharmony_ci * then the cluster is prepared for power-down too.
9362306a36Sopenharmony_ci *
9462306a36Sopenharmony_ci * This must be called with interrupts disabled.
9562306a36Sopenharmony_ci *
9662306a36Sopenharmony_ci * On success this does not return.  Re-entry in the kernel is expected
9762306a36Sopenharmony_ci * via mcpm_entry_point.
9862306a36Sopenharmony_ci *
9962306a36Sopenharmony_ci * This will return if mcpm_platform_register() has not been called
10062306a36Sopenharmony_ci * previously in which case the caller should take appropriate action.
10162306a36Sopenharmony_ci *
10262306a36Sopenharmony_ci * On success, the CPU is not guaranteed to be truly halted until
10362306a36Sopenharmony_ci * mcpm_wait_for_cpu_powerdown() subsequently returns non-zero for the
10462306a36Sopenharmony_ci * specified cpu.  Until then, other CPUs should make sure they do not
10562306a36Sopenharmony_ci * trash memory the target CPU might be executing/accessing.
10662306a36Sopenharmony_ci */
10762306a36Sopenharmony_civoid mcpm_cpu_power_down(void);
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci/**
11062306a36Sopenharmony_ci * mcpm_wait_for_cpu_powerdown - wait for a specified CPU to halt, and
11162306a36Sopenharmony_ci *	make sure it is powered off
11262306a36Sopenharmony_ci *
11362306a36Sopenharmony_ci * @cpu: CPU number within given cluster
11462306a36Sopenharmony_ci * @cluster: cluster number for the CPU
11562306a36Sopenharmony_ci *
11662306a36Sopenharmony_ci * Call this function to ensure that a pending powerdown has taken
11762306a36Sopenharmony_ci * effect and the CPU is safely parked before performing non-mcpm
11862306a36Sopenharmony_ci * operations that may affect the CPU (such as kexec trashing the
11962306a36Sopenharmony_ci * kernel text).
12062306a36Sopenharmony_ci *
12162306a36Sopenharmony_ci * It is *not* necessary to call this function if you only need to
12262306a36Sopenharmony_ci * serialise a pending powerdown with mcpm_cpu_power_up() or a wakeup
12362306a36Sopenharmony_ci * event.
12462306a36Sopenharmony_ci *
12562306a36Sopenharmony_ci * Do not call this function unless the specified CPU has already
12662306a36Sopenharmony_ci * called mcpm_cpu_power_down() or has committed to doing so.
12762306a36Sopenharmony_ci *
12862306a36Sopenharmony_ci * @return:
12962306a36Sopenharmony_ci *	- zero if the CPU is in a safely parked state
13062306a36Sopenharmony_ci *	- nonzero otherwise (e.g., timeout)
13162306a36Sopenharmony_ci */
13262306a36Sopenharmony_ciint mcpm_wait_for_cpu_powerdown(unsigned int cpu, unsigned int cluster);
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci/**
13562306a36Sopenharmony_ci * mcpm_cpu_suspend - bring the calling CPU in a suspended state
13662306a36Sopenharmony_ci *
13762306a36Sopenharmony_ci * The calling CPU is suspended.  This is similar to mcpm_cpu_power_down()
13862306a36Sopenharmony_ci * except for possible extra platform specific configuration steps to allow
13962306a36Sopenharmony_ci * an asynchronous wake-up e.g. with a pending interrupt.
14062306a36Sopenharmony_ci *
14162306a36Sopenharmony_ci * If this CPU is found to be the "last man standing" in the cluster
14262306a36Sopenharmony_ci * then the cluster may be prepared for power-down too.
14362306a36Sopenharmony_ci *
14462306a36Sopenharmony_ci * This must be called with interrupts disabled.
14562306a36Sopenharmony_ci *
14662306a36Sopenharmony_ci * On success this does not return.  Re-entry in the kernel is expected
14762306a36Sopenharmony_ci * via mcpm_entry_point.
14862306a36Sopenharmony_ci *
14962306a36Sopenharmony_ci * This will return if mcpm_platform_register() has not been called
15062306a36Sopenharmony_ci * previously in which case the caller should take appropriate action.
15162306a36Sopenharmony_ci */
15262306a36Sopenharmony_civoid mcpm_cpu_suspend(void);
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci/**
15562306a36Sopenharmony_ci * mcpm_cpu_powered_up - housekeeping workafter a CPU has been powered up
15662306a36Sopenharmony_ci *
15762306a36Sopenharmony_ci * This lets the platform specific backend code perform needed housekeeping
15862306a36Sopenharmony_ci * work.  This must be called by the newly activated CPU as soon as it is
15962306a36Sopenharmony_ci * fully operational in kernel space, before it enables interrupts.
16062306a36Sopenharmony_ci *
16162306a36Sopenharmony_ci * If the operation cannot be performed then an error code is returned.
16262306a36Sopenharmony_ci */
16362306a36Sopenharmony_ciint mcpm_cpu_powered_up(void);
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci/*
16662306a36Sopenharmony_ci * Platform specific callbacks used in the implementation of the above API.
16762306a36Sopenharmony_ci *
16862306a36Sopenharmony_ci * cpu_powerup:
16962306a36Sopenharmony_ci * Make given CPU runable. Called with MCPM lock held and IRQs disabled.
17062306a36Sopenharmony_ci * The given cluster is assumed to be set up (cluster_powerup would have
17162306a36Sopenharmony_ci * been called beforehand). Must return 0 for success or negative error code.
17262306a36Sopenharmony_ci *
17362306a36Sopenharmony_ci * cluster_powerup:
17462306a36Sopenharmony_ci * Set up power for given cluster. Called with MCPM lock held and IRQs
17562306a36Sopenharmony_ci * disabled. Called before first cpu_powerup when cluster is down. Must
17662306a36Sopenharmony_ci * return 0 for success or negative error code.
17762306a36Sopenharmony_ci *
17862306a36Sopenharmony_ci * cpu_suspend_prepare:
17962306a36Sopenharmony_ci * Special suspend configuration. Called on target CPU with MCPM lock held
18062306a36Sopenharmony_ci * and IRQs disabled. This callback is optional. If provided, it is called
18162306a36Sopenharmony_ci * before cpu_powerdown_prepare.
18262306a36Sopenharmony_ci *
18362306a36Sopenharmony_ci * cpu_powerdown_prepare:
18462306a36Sopenharmony_ci * Configure given CPU for power down. Called on target CPU with MCPM lock
18562306a36Sopenharmony_ci * held and IRQs disabled. Power down must be effective only at the next WFI instruction.
18662306a36Sopenharmony_ci *
18762306a36Sopenharmony_ci * cluster_powerdown_prepare:
18862306a36Sopenharmony_ci * Configure given cluster for power down. Called on one CPU from target
18962306a36Sopenharmony_ci * cluster with MCPM lock held and IRQs disabled. A cpu_powerdown_prepare
19062306a36Sopenharmony_ci * for each CPU in the cluster has happened when this occurs.
19162306a36Sopenharmony_ci *
19262306a36Sopenharmony_ci * cpu_cache_disable:
19362306a36Sopenharmony_ci * Clean and disable CPU level cache for the calling CPU. Called on with IRQs
19462306a36Sopenharmony_ci * disabled only. The CPU is no longer cache coherent with the rest of the
19562306a36Sopenharmony_ci * system when this returns.
19662306a36Sopenharmony_ci *
19762306a36Sopenharmony_ci * cluster_cache_disable:
19862306a36Sopenharmony_ci * Clean and disable the cluster wide cache as well as the CPU level cache
19962306a36Sopenharmony_ci * for the calling CPU. No call to cpu_cache_disable will happen for this
20062306a36Sopenharmony_ci * CPU. Called with IRQs disabled and only when all the other CPUs are done
20162306a36Sopenharmony_ci * with their own cpu_cache_disable. The cluster is no longer cache coherent
20262306a36Sopenharmony_ci * with the rest of the system when this returns.
20362306a36Sopenharmony_ci *
20462306a36Sopenharmony_ci * cpu_is_up:
20562306a36Sopenharmony_ci * Called on given CPU after it has been powered up or resumed. The MCPM lock
20662306a36Sopenharmony_ci * is held and IRQs disabled. This callback is optional.
20762306a36Sopenharmony_ci *
20862306a36Sopenharmony_ci * cluster_is_up:
20962306a36Sopenharmony_ci * Called by the first CPU to be powered up or resumed in given cluster.
21062306a36Sopenharmony_ci * The MCPM lock is held and IRQs disabled. This callback is optional. If
21162306a36Sopenharmony_ci * provided, it is called before cpu_is_up for that CPU.
21262306a36Sopenharmony_ci *
21362306a36Sopenharmony_ci * wait_for_powerdown:
21462306a36Sopenharmony_ci * Wait until given CPU is powered down. This is called in sleeping context.
21562306a36Sopenharmony_ci * Some reasonable timeout must be considered. Must return 0 for success or
21662306a36Sopenharmony_ci * negative error code.
21762306a36Sopenharmony_ci */
21862306a36Sopenharmony_cistruct mcpm_platform_ops {
21962306a36Sopenharmony_ci	int (*cpu_powerup)(unsigned int cpu, unsigned int cluster);
22062306a36Sopenharmony_ci	int (*cluster_powerup)(unsigned int cluster);
22162306a36Sopenharmony_ci	void (*cpu_suspend_prepare)(unsigned int cpu, unsigned int cluster);
22262306a36Sopenharmony_ci	void (*cpu_powerdown_prepare)(unsigned int cpu, unsigned int cluster);
22362306a36Sopenharmony_ci	void (*cluster_powerdown_prepare)(unsigned int cluster);
22462306a36Sopenharmony_ci	void (*cpu_cache_disable)(void);
22562306a36Sopenharmony_ci	void (*cluster_cache_disable)(void);
22662306a36Sopenharmony_ci	void (*cpu_is_up)(unsigned int cpu, unsigned int cluster);
22762306a36Sopenharmony_ci	void (*cluster_is_up)(unsigned int cluster);
22862306a36Sopenharmony_ci	int (*wait_for_powerdown)(unsigned int cpu, unsigned int cluster);
22962306a36Sopenharmony_ci};
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_ci/**
23262306a36Sopenharmony_ci * mcpm_platform_register - register platform specific power methods
23362306a36Sopenharmony_ci *
23462306a36Sopenharmony_ci * @ops: mcpm_platform_ops structure to register
23562306a36Sopenharmony_ci *
23662306a36Sopenharmony_ci * An error is returned if the registration has been done previously.
23762306a36Sopenharmony_ci */
23862306a36Sopenharmony_ciint __init mcpm_platform_register(const struct mcpm_platform_ops *ops);
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ci/**
24162306a36Sopenharmony_ci * mcpm_sync_init - Initialize the cluster synchronization support
24262306a36Sopenharmony_ci *
24362306a36Sopenharmony_ci * @power_up_setup: platform specific function invoked during very
24462306a36Sopenharmony_ci * 		    early CPU/cluster bringup stage.
24562306a36Sopenharmony_ci *
24662306a36Sopenharmony_ci * This prepares memory used by vlocks and the MCPM state machine used
24762306a36Sopenharmony_ci * across CPUs that may have their caches active or inactive. Must be
24862306a36Sopenharmony_ci * called only after a successful call to mcpm_platform_register().
24962306a36Sopenharmony_ci *
25062306a36Sopenharmony_ci * The power_up_setup argument is a pointer to assembly code called when
25162306a36Sopenharmony_ci * the MMU and caches are still disabled during boot  and no stack space is
25262306a36Sopenharmony_ci * available. The affinity level passed to that code corresponds to the
25362306a36Sopenharmony_ci * resource that needs to be initialized (e.g. 1 for cluster level, 0 for
25462306a36Sopenharmony_ci * CPU level).  Proper exclusion mechanisms are already activated at that
25562306a36Sopenharmony_ci * point.
25662306a36Sopenharmony_ci */
25762306a36Sopenharmony_ciint __init mcpm_sync_init(
25862306a36Sopenharmony_ci	void (*power_up_setup)(unsigned int affinity_level));
25962306a36Sopenharmony_ci
26062306a36Sopenharmony_ci/**
26162306a36Sopenharmony_ci * mcpm_loopback - make a run through the MCPM low-level code
26262306a36Sopenharmony_ci *
26362306a36Sopenharmony_ci * @cache_disable: pointer to function performing cache disabling
26462306a36Sopenharmony_ci *
26562306a36Sopenharmony_ci * This exercises the MCPM machinery by soft resetting the CPU and branching
26662306a36Sopenharmony_ci * to the MCPM low-level entry code before returning to the caller.
26762306a36Sopenharmony_ci * The @cache_disable function must do the necessary cache disabling to
26862306a36Sopenharmony_ci * let the regular kernel init code turn it back on as if the CPU was
26962306a36Sopenharmony_ci * hotplugged in. The MCPM state machine is set as if the cluster was
27062306a36Sopenharmony_ci * initialized meaning the power_up_setup callback passed to mcpm_sync_init()
27162306a36Sopenharmony_ci * will be invoked for all affinity levels. This may be useful to initialize
27262306a36Sopenharmony_ci * some resources such as enabling the CCI that requires the cache to be off, or simply for testing purposes.
27362306a36Sopenharmony_ci */
27462306a36Sopenharmony_ciint __init mcpm_loopback(void (*cache_disable)(void));
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_civoid __init mcpm_smp_set_ops(void);
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_ci/*
27962306a36Sopenharmony_ci * Synchronisation structures for coordinating safe cluster setup/teardown.
28062306a36Sopenharmony_ci * This is private to the MCPM core code and shared between C and assembly.
28162306a36Sopenharmony_ci * When modifying this structure, make sure you update the MCPM_SYNC_ defines
28262306a36Sopenharmony_ci * to match.
28362306a36Sopenharmony_ci */
28462306a36Sopenharmony_cistruct mcpm_sync_struct {
28562306a36Sopenharmony_ci	/* individual CPU states */
28662306a36Sopenharmony_ci	struct {
28762306a36Sopenharmony_ci		s8 cpu __aligned(__CACHE_WRITEBACK_GRANULE);
28862306a36Sopenharmony_ci	} cpus[MAX_CPUS_PER_CLUSTER];
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_ci	/* cluster state */
29162306a36Sopenharmony_ci	s8 cluster __aligned(__CACHE_WRITEBACK_GRANULE);
29262306a36Sopenharmony_ci
29362306a36Sopenharmony_ci	/* inbound-side state */
29462306a36Sopenharmony_ci	s8 inbound __aligned(__CACHE_WRITEBACK_GRANULE);
29562306a36Sopenharmony_ci};
29662306a36Sopenharmony_ci
29762306a36Sopenharmony_cistruct sync_struct {
29862306a36Sopenharmony_ci	struct mcpm_sync_struct clusters[MAX_NR_CLUSTERS];
29962306a36Sopenharmony_ci};
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_ci#else
30262306a36Sopenharmony_ci
30362306a36Sopenharmony_ci/*
30462306a36Sopenharmony_ci * asm-offsets.h causes trouble when included in .c files, and cacheflush.h
30562306a36Sopenharmony_ci * cannot be included in asm files.  Let's work around the conflict like this.
30662306a36Sopenharmony_ci */
30762306a36Sopenharmony_ci#include <asm/asm-offsets.h>
30862306a36Sopenharmony_ci#define __CACHE_WRITEBACK_GRANULE CACHE_WRITEBACK_GRANULE
30962306a36Sopenharmony_ci
31062306a36Sopenharmony_ci#endif /* ! __ASSEMBLY__ */
31162306a36Sopenharmony_ci
31262306a36Sopenharmony_ci/* Definitions for mcpm_sync_struct */
31362306a36Sopenharmony_ci#define CPU_DOWN		0x11
31462306a36Sopenharmony_ci#define CPU_COMING_UP		0x12
31562306a36Sopenharmony_ci#define CPU_UP			0x13
31662306a36Sopenharmony_ci#define CPU_GOING_DOWN		0x14
31762306a36Sopenharmony_ci
31862306a36Sopenharmony_ci#define CLUSTER_DOWN		0x21
31962306a36Sopenharmony_ci#define CLUSTER_UP		0x22
32062306a36Sopenharmony_ci#define CLUSTER_GOING_DOWN	0x23
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_ci#define INBOUND_NOT_COMING_UP	0x31
32362306a36Sopenharmony_ci#define INBOUND_COMING_UP	0x32
32462306a36Sopenharmony_ci
32562306a36Sopenharmony_ci/*
32662306a36Sopenharmony_ci * Offsets for the mcpm_sync_struct members, for use in asm.
32762306a36Sopenharmony_ci * We don't want to make them global to the kernel via asm-offsets.c.
32862306a36Sopenharmony_ci */
32962306a36Sopenharmony_ci#define MCPM_SYNC_CLUSTER_CPUS	0
33062306a36Sopenharmony_ci#define MCPM_SYNC_CPU_SIZE	__CACHE_WRITEBACK_GRANULE
33162306a36Sopenharmony_ci#define MCPM_SYNC_CLUSTER_CLUSTER \
33262306a36Sopenharmony_ci	(MCPM_SYNC_CLUSTER_CPUS + MCPM_SYNC_CPU_SIZE * MAX_CPUS_PER_CLUSTER)
33362306a36Sopenharmony_ci#define MCPM_SYNC_CLUSTER_INBOUND \
33462306a36Sopenharmony_ci	(MCPM_SYNC_CLUSTER_CLUSTER + __CACHE_WRITEBACK_GRANULE)
33562306a36Sopenharmony_ci#define MCPM_SYNC_CLUSTER_SIZE \
33662306a36Sopenharmony_ci	(MCPM_SYNC_CLUSTER_INBOUND + __CACHE_WRITEBACK_GRANULE)
33762306a36Sopenharmony_ci
33862306a36Sopenharmony_ci#endif
339