psci_setup.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * Redistributions of source code must retain the above copyright notice, this
  8. * list of conditions and the following disclaimer.
  9. *
  10. * Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. *
  14. * Neither the name of ARM nor the names of its contributors may be used
  15. * to endorse or promote products derived from this software without specific
  16. * prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  22. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <arch.h>
  31. #include <arch_helpers.h>
  32. #include <assert.h>
  33. #include <bl_common.h>
  34. #include <context.h>
  35. #include <context_mgmt.h>
  36. #include <platform.h>
  37. #include <stddef.h>
  38. #include "psci_private.h"
  39. /*******************************************************************************
  40. * Per cpu non-secure contexts used to program the architectural state prior
  41. * return to the normal world.
  42. * TODO: Use the memory allocator to set aside memory for the contexts instead
  43. * of relying on platform defined constants. Using PSCI_NUM_AFFS will be an
  44. * overkill.
  45. ******************************************************************************/
  46. static cpu_context_t psci_ns_context[PLATFORM_CORE_COUNT];
  47. /*******************************************************************************
  48. * In a system, a certain number of affinity instances are present at an
  49. * affinity level. The cumulative number of instances across all levels are
  50. * stored in 'psci_aff_map'. The topology tree has been flattenned into this
  51. * array. To retrieve nodes, information about the extents of each affinity
  52. * level i.e. start index and end index needs to be present. 'psci_aff_limits'
  53. * stores this information.
  54. ******************************************************************************/
  55. static aff_limits_node_t psci_aff_limits[MPIDR_MAX_AFFLVL + 1];
  56. /*******************************************************************************
  57. * Routines for retrieving the node corresponding to an affinity level instance
  58. * in the mpidr. The first one uses binary search to find the node corresponding
  59. * to the mpidr (key) at a particular affinity level. The second routine decides
  60. * extents of the binary search at each affinity level.
  61. ******************************************************************************/
  62. static int psci_aff_map_get_idx(unsigned long key,
  63. int min_idx,
  64. int max_idx)
  65. {
  66. int mid;
  67. /*
  68. * Terminating condition: If the max and min indices have crossed paths
  69. * during the binary search then the key has not been found.
  70. */
  71. if (max_idx < min_idx)
  72. return PSCI_E_INVALID_PARAMS;
  73. /*
  74. * Make sure we are within array limits.
  75. */
  76. assert(min_idx >= 0 && max_idx < PSCI_NUM_AFFS);
  77. /*
  78. * Bisect the array around 'mid' and then recurse into the array chunk
  79. * where the key is likely to be found. The mpidrs in each node in the
  80. * 'psci_aff_map' for a given affinity level are stored in an ascending
  81. * order which makes the binary search possible.
  82. */
  83. mid = min_idx + ((max_idx - min_idx) >> 1); /* Divide by 2 */
  84. if (psci_aff_map[mid].mpidr > key)
  85. return psci_aff_map_get_idx(key, min_idx, mid - 1);
  86. else if (psci_aff_map[mid].mpidr < key)
  87. return psci_aff_map_get_idx(key, mid + 1, max_idx);
  88. else
  89. return mid;
  90. }
  91. aff_map_node_t *psci_get_aff_map_node(unsigned long mpidr, int aff_lvl)
  92. {
  93. int rc;
  94. if (aff_lvl > get_max_afflvl())
  95. return NULL;
  96. /* Right shift the mpidr to the required affinity level */
  97. mpidr = mpidr_mask_lower_afflvls(mpidr, aff_lvl);
  98. rc = psci_aff_map_get_idx(mpidr,
  99. psci_aff_limits[aff_lvl].min,
  100. psci_aff_limits[aff_lvl].max);
  101. if (rc >= 0)
  102. return &psci_aff_map[rc];
  103. else
  104. return NULL;
  105. }
  106. /*******************************************************************************
  107. * This function populates an array with nodes corresponding to a given range of
  108. * affinity levels in an mpidr. It returns successfully only when the affinity
  109. * levels are correct, the mpidr is valid i.e. no affinity level is absent from
  110. * the topology tree & the affinity instance at level 0 is not absent.
  111. ******************************************************************************/
  112. int psci_get_aff_map_nodes(unsigned long mpidr,
  113. int start_afflvl,
  114. int end_afflvl,
  115. aff_map_node_t *mpidr_nodes[])
  116. {
  117. int rc = PSCI_E_INVALID_PARAMS, level;
  118. aff_map_node_t *node;
  119. rc = psci_check_afflvl_range(start_afflvl, end_afflvl);
  120. if (rc != PSCI_E_SUCCESS)
  121. return rc;
  122. for (level = start_afflvl; level <= end_afflvl; level++) {
  123. /*
  124. * Grab the node for each affinity level. No affinity level
  125. * can be missing as that would mean that the topology tree
  126. * is corrupted.
  127. */
  128. node = psci_get_aff_map_node(mpidr, level);
  129. if (node == NULL) {
  130. rc = PSCI_E_INVALID_PARAMS;
  131. break;
  132. }
  133. /*
  134. * Skip absent affinity levels unless it's afffinity level 0.
  135. * An absent cpu means that the mpidr is invalid. Save the
  136. * pointer to the node for the present affinity level
  137. */
  138. if (!(node->state & PSCI_AFF_PRESENT)) {
  139. if (level == MPIDR_AFFLVL0) {
  140. rc = PSCI_E_INVALID_PARAMS;
  141. break;
  142. }
  143. mpidr_nodes[level] = NULL;
  144. } else
  145. mpidr_nodes[level] = node;
  146. }
  147. return rc;
  148. }
  149. /*******************************************************************************
  150. * Function which initializes the 'aff_map_node' corresponding to an affinity
  151. * level instance. Each node has a unique mpidr, level and bakery lock. The data
  152. * field is opaque and holds affinity level specific data e.g. for affinity
  153. * level 0 it contains the index into arrays that hold the secure/non-secure
  154. * state for a cpu that's been turned on/off
  155. ******************************************************************************/
  156. static void psci_init_aff_map_node(unsigned long mpidr,
  157. int level,
  158. unsigned int idx)
  159. {
  160. unsigned char state;
  161. uint32_t linear_id;
  162. psci_aff_map[idx].mpidr = mpidr;
  163. psci_aff_map[idx].level = level;
  164. psci_lock_init(psci_aff_map, idx);
  165. /*
  166. * If an affinity instance is present then mark it as OFF to begin with.
  167. */
  168. state = plat_get_aff_state(level, mpidr);
  169. psci_aff_map[idx].state = state;
  170. if (level == MPIDR_AFFLVL0) {
  171. /*
  172. * Mark the cpu as OFF. Higher affinity level reference counts
  173. * have already been memset to 0
  174. */
  175. if (state & PSCI_AFF_PRESENT)
  176. psci_set_state(&psci_aff_map[idx], PSCI_STATE_OFF);
  177. /*
  178. * Associate a non-secure context with this affinity
  179. * instance through the context management library.
  180. */
  181. linear_id = platform_get_core_pos(mpidr);
  182. assert(linear_id < PLATFORM_CORE_COUNT);
  183. /* Invalidate the suspend context for the node */
  184. set_cpu_data_by_index(linear_id,
  185. psci_svc_cpu_data.power_state,
  186. PSCI_INVALID_DATA);
  187. /*
  188. * There is no state associated with the current execution
  189. * context so ensure that any reads of the highest affinity
  190. * level in a powered down state return PSCI_INVALID_DATA.
  191. */
  192. set_cpu_data_by_index(linear_id,
  193. psci_svc_cpu_data.max_phys_off_afflvl,
  194. PSCI_INVALID_DATA);
  195. flush_cpu_data_by_index(linear_id, psci_svc_cpu_data);
  196. cm_set_context_by_mpidr(mpidr,
  197. (void *) &psci_ns_context[linear_id],
  198. NON_SECURE);
  199. }
  200. return;
  201. }
  202. /*******************************************************************************
  203. * Core routine used by the Breadth-First-Search algorithm to populate the
  204. * affinity tree. Each level in the tree corresponds to an affinity level. This
  205. * routine's aim is to traverse to the target affinity level and populate nodes
  206. * in the 'psci_aff_map' for all the siblings at that level. It uses the current
  207. * affinity level to keep track of how many levels from the root of the tree
  208. * have been traversed. If the current affinity level != target affinity level,
  209. * then the platform is asked to return the number of children that each
  210. * affinity instance has at the current affinity level. Traversal is then done
  211. * for each child at the next lower level i.e. current affinity level - 1.
  212. *
  213. * CAUTION: This routine assumes that affinity instance ids are allocated in a
  214. * monotonically increasing manner at each affinity level in a mpidr starting
  215. * from 0. If the platform breaks this assumption then this code will have to
  216. * be reworked accordingly.
  217. ******************************************************************************/
  218. static unsigned int psci_init_aff_map(unsigned long mpidr,
  219. unsigned int affmap_idx,
  220. int cur_afflvl,
  221. int tgt_afflvl)
  222. {
  223. unsigned int ctr, aff_count;
  224. assert(cur_afflvl >= tgt_afflvl);
  225. /*
  226. * Find the number of siblings at the current affinity level &
  227. * assert if there are none 'cause then we have been invoked with
  228. * an invalid mpidr.
  229. */
  230. aff_count = plat_get_aff_count(cur_afflvl, mpidr);
  231. assert(aff_count);
  232. if (tgt_afflvl < cur_afflvl) {
  233. for (ctr = 0; ctr < aff_count; ctr++) {
  234. mpidr = mpidr_set_aff_inst(mpidr, ctr, cur_afflvl);
  235. affmap_idx = psci_init_aff_map(mpidr,
  236. affmap_idx,
  237. cur_afflvl - 1,
  238. tgt_afflvl);
  239. }
  240. } else {
  241. for (ctr = 0; ctr < aff_count; ctr++, affmap_idx++) {
  242. mpidr = mpidr_set_aff_inst(mpidr, ctr, cur_afflvl);
  243. psci_init_aff_map_node(mpidr, cur_afflvl, affmap_idx);
  244. }
  245. /* affmap_idx is 1 greater than the max index of cur_afflvl */
  246. psci_aff_limits[cur_afflvl].max = affmap_idx - 1;
  247. }
  248. return affmap_idx;
  249. }
  250. /*******************************************************************************
  251. * This function initializes the topology tree by querying the platform. To do
  252. * so, it's helper routines implement a Breadth-First-Search. At each affinity
  253. * level the platform conveys the number of affinity instances that exist i.e.
  254. * the affinity count. The algorithm populates the psci_aff_map recursively
  255. * using this information. On a platform that implements two clusters of 4 cpus
  256. * each, the populated aff_map_array would look like this:
  257. *
  258. * <- cpus cluster0 -><- cpus cluster1 ->
  259. * ---------------------------------------------------
  260. * | 0 | 1 | 0 | 1 | 2 | 3 | 0 | 1 | 2 | 3 |
  261. * ---------------------------------------------------
  262. * ^ ^
  263. * cluster __| cpu __|
  264. * limit limit
  265. *
  266. * The first 2 entries are of the cluster nodes. The next 4 entries are of cpus
  267. * within cluster 0. The last 4 entries are of cpus within cluster 1.
  268. * The 'psci_aff_limits' array contains the max & min index of each affinity
  269. * level within the 'psci_aff_map' array. This allows restricting search of a
  270. * node at an affinity level between the indices in the limits array.
  271. ******************************************************************************/
  272. int32_t psci_setup(void)
  273. {
  274. unsigned long mpidr = read_mpidr();
  275. int afflvl, affmap_idx, max_afflvl;
  276. aff_map_node_t *node;
  277. psci_plat_pm_ops = NULL;
  278. /* Find out the maximum affinity level that the platform implements */
  279. max_afflvl = get_max_afflvl();
  280. assert(max_afflvl <= MPIDR_MAX_AFFLVL);
  281. /*
  282. * This call traverses the topology tree with help from the platform and
  283. * populates the affinity map using a breadth-first-search recursively.
  284. * We assume that the platform allocates affinity instance ids from 0
  285. * onwards at each affinity level in the mpidr. FIRST_MPIDR = 0.0.0.0
  286. */
  287. affmap_idx = 0;
  288. for (afflvl = max_afflvl; afflvl >= MPIDR_AFFLVL0; afflvl--) {
  289. affmap_idx = psci_init_aff_map(FIRST_MPIDR,
  290. affmap_idx,
  291. max_afflvl,
  292. afflvl);
  293. }
  294. /*
  295. * Set the bounds for the affinity counts of each level in the map. Also
  296. * flush out the entire array so that it's visible to subsequent power
  297. * management operations. The 'psci_aff_map' array is allocated in
  298. * coherent memory so does not need flushing. The 'psci_aff_limits'
  299. * array is allocated in normal memory. It will be accessed when the mmu
  300. * is off e.g. after reset. Hence it needs to be flushed.
  301. */
  302. for (afflvl = MPIDR_AFFLVL0; afflvl < max_afflvl; afflvl++) {
  303. psci_aff_limits[afflvl].min =
  304. psci_aff_limits[afflvl + 1].max + 1;
  305. }
  306. flush_dcache_range((unsigned long) psci_aff_limits,
  307. sizeof(psci_aff_limits));
  308. /*
  309. * Mark the affinity instances in our mpidr as ON. No need to lock as
  310. * this is the primary cpu.
  311. */
  312. mpidr &= MPIDR_AFFINITY_MASK;
  313. for (afflvl = MPIDR_AFFLVL0; afflvl <= max_afflvl; afflvl++) {
  314. node = psci_get_aff_map_node(mpidr, afflvl);
  315. assert(node);
  316. /* Mark each present node as ON. */
  317. if (node->state & PSCI_AFF_PRESENT)
  318. psci_set_state(node, PSCI_STATE_ON);
  319. }
  320. platform_setup_pm(&psci_plat_pm_ops);
  321. assert(psci_plat_pm_ops);
  322. return 0;
  323. }