mcpm.h 12 KB

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