mcpm.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * arch/arm/include/asm/mcpm.h
  4. *
  5. * Created by: Nicolas Pitre, April 2012
  6. * Copyright: (C) 2012-2013 Linaro Limited
  7. */
  8. #ifndef MCPM_H
  9. #define MCPM_H
  10. /*
  11. * Maximum number of possible clusters / CPUs per cluster.
  12. *
  13. * This should be sufficient for quite a while, while keeping the
  14. * (assembly) code simpler. When this starts to grow then we'll have
  15. * to consider dynamic allocation.
  16. */
  17. #define MAX_CPUS_PER_CLUSTER 4
  18. #ifdef CONFIG_MCPM_QUAD_CLUSTER
  19. #define MAX_NR_CLUSTERS 4
  20. #else
  21. #define MAX_NR_CLUSTERS 2
  22. #endif
  23. #ifndef __ASSEMBLY__
  24. #include <linux/types.h>
  25. #include <asm/cacheflush.h>
  26. /*
  27. * Platform specific code should use this symbol to set up secondary
  28. * entry location for processors to use when released from reset.
  29. */
  30. extern void mcpm_entry_point(void);
  31. /*
  32. * This is used to indicate where the given CPU from given cluster should
  33. * branch once it is ready to re-enter the kernel using ptr, or NULL if it
  34. * should be gated. A gated CPU is held in a WFE loop until its vector
  35. * becomes non NULL.
  36. */
  37. void mcpm_set_entry_vector(unsigned cpu, unsigned cluster, void *ptr);
  38. /*
  39. * This sets an early poke i.e a value to be poked into some address
  40. * from very early assembly code before the CPU is ungated. The
  41. * address must be physical, and if 0 then nothing will happen.
  42. */
  43. void mcpm_set_early_poke(unsigned cpu, unsigned cluster,
  44. unsigned long poke_phys_addr, unsigned long poke_val);
  45. /*
  46. * CPU/cluster power operations API for higher subsystems to use.
  47. */
  48. /**
  49. * mcpm_is_available - returns whether MCPM is initialized and available
  50. *
  51. * This returns true or false accordingly.
  52. */
  53. bool mcpm_is_available(void);
  54. /**
  55. * mcpm_cpu_power_up - make given CPU in given cluster runable
  56. *
  57. * @cpu: CPU number within given cluster
  58. * @cluster: cluster number for the CPU
  59. *
  60. * The identified CPU is brought out of reset. If the cluster was powered
  61. * down then it is brought up as well, taking care not to let the other CPUs
  62. * in the cluster run, and ensuring appropriate cluster setup.
  63. *
  64. * Caller must ensure the appropriate entry vector is initialized with
  65. * mcpm_set_entry_vector() prior to calling this.
  66. *
  67. * This must be called in a sleepable context. However, the implementation
  68. * is strongly encouraged to return early and let the operation happen
  69. * asynchronously, especially when significant delays are expected.
  70. *
  71. * If the operation cannot be performed then an error code is returned.
  72. */
  73. int mcpm_cpu_power_up(unsigned int cpu, unsigned int cluster);
  74. /**
  75. * mcpm_cpu_power_down - power the calling CPU down
  76. *
  77. * The calling CPU is powered down.
  78. *
  79. * If this CPU is found to be the "last man standing" in the cluster
  80. * then the cluster is prepared for power-down too.
  81. *
  82. * This must be called with interrupts disabled.
  83. *
  84. * On success this does not return. Re-entry in the kernel is expected
  85. * via mcpm_entry_point.
  86. *
  87. * This will return if mcpm_platform_register() has not been called
  88. * previously in which case the caller should take appropriate action.
  89. *
  90. * On success, the CPU is not guaranteed to be truly halted until
  91. * mcpm_wait_for_cpu_powerdown() subsequently returns non-zero for the
  92. * specified cpu. Until then, other CPUs should make sure they do not
  93. * trash memory the target CPU might be executing/accessing.
  94. */
  95. void mcpm_cpu_power_down(void);
  96. /**
  97. * mcpm_wait_for_cpu_powerdown - wait for a specified CPU to halt, and
  98. * make sure it is powered off
  99. *
  100. * @cpu: CPU number within given cluster
  101. * @cluster: cluster number for the CPU
  102. *
  103. * Call this function to ensure that a pending powerdown has taken
  104. * effect and the CPU is safely parked before performing non-mcpm
  105. * operations that may affect the CPU (such as kexec trashing the
  106. * kernel text).
  107. *
  108. * It is *not* necessary to call this function if you only need to
  109. * serialise a pending powerdown with mcpm_cpu_power_up() or a wakeup
  110. * event.
  111. *
  112. * Do not call this function unless the specified CPU has already
  113. * called mcpm_cpu_power_down() or has committed to doing so.
  114. *
  115. * @return:
  116. * - zero if the CPU is in a safely parked state
  117. * - nonzero otherwise (e.g., timeout)
  118. */
  119. int mcpm_wait_for_cpu_powerdown(unsigned int cpu, unsigned int cluster);
  120. /**
  121. * mcpm_cpu_suspend - bring the calling CPU in a suspended state
  122. *
  123. * The calling CPU is suspended. This is similar to mcpm_cpu_power_down()
  124. * except for possible extra platform specific configuration steps to allow
  125. * an asynchronous wake-up e.g. with a pending interrupt.
  126. *
  127. * If this CPU is found to be the "last man standing" in the cluster
  128. * then the cluster may be prepared for power-down too.
  129. *
  130. * This must be called with interrupts disabled.
  131. *
  132. * On success this does not return. Re-entry in the kernel is expected
  133. * via mcpm_entry_point.
  134. *
  135. * This will return if mcpm_platform_register() has not been called
  136. * previously in which case the caller should take appropriate action.
  137. */
  138. void mcpm_cpu_suspend(void);
  139. /**
  140. * mcpm_cpu_powered_up - housekeeping workafter a CPU has been powered up
  141. *
  142. * This lets the platform specific backend code perform needed housekeeping
  143. * work. This must be called by the newly activated CPU as soon as it is
  144. * fully operational in kernel space, before it enables interrupts.
  145. *
  146. * If the operation cannot be performed then an error code is returned.
  147. */
  148. int mcpm_cpu_powered_up(void);
  149. /*
  150. * Platform specific callbacks used in the implementation of the above API.
  151. *
  152. * cpu_powerup:
  153. * Make given CPU runable. Called with MCPM lock held and IRQs disabled.
  154. * The given cluster is assumed to be set up (cluster_powerup would have
  155. * been called beforehand). Must return 0 for success or negative error code.
  156. *
  157. * cluster_powerup:
  158. * Set up power for given cluster. Called with MCPM lock held and IRQs
  159. * disabled. Called before first cpu_powerup when cluster is down. Must
  160. * return 0 for success or negative error code.
  161. *
  162. * cpu_suspend_prepare:
  163. * Special suspend configuration. Called on target CPU with MCPM lock held
  164. * and IRQs disabled. This callback is optional. If provided, it is called
  165. * before cpu_powerdown_prepare.
  166. *
  167. * cpu_powerdown_prepare:
  168. * Configure given CPU for power down. Called on target CPU with MCPM lock
  169. * held and IRQs disabled. Power down must be effective only at the next WFI instruction.
  170. *
  171. * cluster_powerdown_prepare:
  172. * Configure given cluster for power down. Called on one CPU from target
  173. * cluster with MCPM lock held and IRQs disabled. A cpu_powerdown_prepare
  174. * for each CPU in the cluster has happened when this occurs.
  175. *
  176. * cpu_cache_disable:
  177. * Clean and disable CPU level cache for the calling CPU. Called on with IRQs
  178. * disabled only. The CPU is no longer cache coherent with the rest of the
  179. * system when this returns.
  180. *
  181. * cluster_cache_disable:
  182. * Clean and disable the cluster wide cache as well as the CPU level cache
  183. * for the calling CPU. No call to cpu_cache_disable will happen for this
  184. * CPU. Called with IRQs disabled and only when all the other CPUs are done
  185. * with their own cpu_cache_disable. The cluster is no longer cache coherent
  186. * with the rest of the system when this returns.
  187. *
  188. * cpu_is_up:
  189. * Called on given CPU after it has been powered up or resumed. The MCPM lock
  190. * is held and IRQs disabled. This callback is optional.
  191. *
  192. * cluster_is_up:
  193. * Called by the first CPU to be powered up or resumed in given cluster.
  194. * The MCPM lock is held and IRQs disabled. This callback is optional. If
  195. * provided, it is called before cpu_is_up for that CPU.
  196. *
  197. * wait_for_powerdown:
  198. * Wait until given CPU is powered down. This is called in sleeping context.
  199. * Some reasonable timeout must be considered. Must return 0 for success or
  200. * negative error code.
  201. */
  202. struct mcpm_platform_ops {
  203. int (*cpu_powerup)(unsigned int cpu, unsigned int cluster);
  204. int (*cluster_powerup)(unsigned int cluster);
  205. void (*cpu_suspend_prepare)(unsigned int cpu, unsigned int cluster);
  206. void (*cpu_powerdown_prepare)(unsigned int cpu, unsigned int cluster);
  207. void (*cluster_powerdown_prepare)(unsigned int cluster);
  208. void (*cpu_cache_disable)(void);
  209. void (*cluster_cache_disable)(void);
  210. void (*cpu_is_up)(unsigned int cpu, unsigned int cluster);
  211. void (*cluster_is_up)(unsigned int cluster);
  212. int (*wait_for_powerdown)(unsigned int cpu, unsigned int cluster);
  213. };
  214. /**
  215. * mcpm_platform_register - register platform specific power methods
  216. *
  217. * @ops: mcpm_platform_ops structure to register
  218. *
  219. * An error is returned if the registration has been done previously.
  220. */
  221. int __init mcpm_platform_register(const struct mcpm_platform_ops *ops);
  222. /**
  223. * mcpm_sync_init - Initialize the cluster synchronization support
  224. *
  225. * @power_up_setup: platform specific function invoked during very
  226. * early CPU/cluster bringup stage.
  227. *
  228. * This prepares memory used by vlocks and the MCPM state machine used
  229. * across CPUs that may have their caches active or inactive. Must be
  230. * called only after a successful call to mcpm_platform_register().
  231. *
  232. * The power_up_setup argument is a pointer to assembly code called when
  233. * the MMU and caches are still disabled during boot and no stack space is
  234. * available. The affinity level passed to that code corresponds to the
  235. * resource that needs to be initialized (e.g. 1 for cluster level, 0 for
  236. * CPU level). Proper exclusion mechanisms are already activated at that
  237. * point.
  238. */
  239. int __init mcpm_sync_init(
  240. void (*power_up_setup)(unsigned int affinity_level));
  241. /**
  242. * mcpm_loopback - make a run through the MCPM low-level code
  243. *
  244. * @cache_disable: pointer to function performing cache disabling
  245. *
  246. * This exercises the MCPM machinery by soft resetting the CPU and branching
  247. * to the MCPM low-level entry code before returning to the caller.
  248. * The @cache_disable function must do the necessary cache disabling to
  249. * let the regular kernel init code turn it back on as if the CPU was
  250. * hotplugged in. The MCPM state machine is set as if the cluster was
  251. * initialized meaning the power_up_setup callback passed to mcpm_sync_init()
  252. * will be invoked for all affinity levels. This may be useful to initialize
  253. * some resources such as enabling the CCI that requires the cache to be off, or simply for testing purposes.
  254. */
  255. int __init mcpm_loopback(void (*cache_disable)(void));
  256. void __init mcpm_smp_set_ops(void);
  257. /*
  258. * Synchronisation structures for coordinating safe cluster setup/teardown.
  259. * This is private to the MCPM core code and shared between C and assembly.
  260. * When modifying this structure, make sure you update the MCPM_SYNC_ defines
  261. * to match.
  262. */
  263. struct mcpm_sync_struct {
  264. /* individual CPU states */
  265. struct {
  266. s8 cpu __aligned(__CACHE_WRITEBACK_GRANULE);
  267. } cpus[MAX_CPUS_PER_CLUSTER];
  268. /* cluster state */
  269. s8 cluster __aligned(__CACHE_WRITEBACK_GRANULE);
  270. /* inbound-side state */
  271. s8 inbound __aligned(__CACHE_WRITEBACK_GRANULE);
  272. };
  273. struct sync_struct {
  274. struct mcpm_sync_struct clusters[MAX_NR_CLUSTERS];
  275. };
  276. #else
  277. /*
  278. * asm-offsets.h causes trouble when included in .c files, and cacheflush.h
  279. * cannot be included in asm files. Let's work around the conflict like this.
  280. */
  281. #include <asm/asm-offsets.h>
  282. #define __CACHE_WRITEBACK_GRANULE CACHE_WRITEBACK_GRANULE
  283. #endif /* ! __ASSEMBLY__ */
  284. /* Definitions for mcpm_sync_struct */
  285. #define CPU_DOWN 0x11
  286. #define CPU_COMING_UP 0x12
  287. #define CPU_UP 0x13
  288. #define CPU_GOING_DOWN 0x14
  289. #define CLUSTER_DOWN 0x21
  290. #define CLUSTER_UP 0x22
  291. #define CLUSTER_GOING_DOWN 0x23
  292. #define INBOUND_NOT_COMING_UP 0x31
  293. #define INBOUND_COMING_UP 0x32
  294. /*
  295. * Offsets for the mcpm_sync_struct members, for use in asm.
  296. * We don't want to make them global to the kernel via asm-offsets.c.
  297. */
  298. #define MCPM_SYNC_CLUSTER_CPUS 0
  299. #define MCPM_SYNC_CPU_SIZE __CACHE_WRITEBACK_GRANULE
  300. #define MCPM_SYNC_CLUSTER_CLUSTER \
  301. (MCPM_SYNC_CLUSTER_CPUS + MCPM_SYNC_CPU_SIZE * MAX_CPUS_PER_CLUSTER)
  302. #define MCPM_SYNC_CLUSTER_INBOUND \
  303. (MCPM_SYNC_CLUSTER_CLUSTER + __CACHE_WRITEBACK_GRANULE)
  304. #define MCPM_SYNC_CLUSTER_SIZE \
  305. (MCPM_SYNC_CLUSTER_INBOUND + __CACHE_WRITEBACK_GRANULE)
  306. #endif