psci_setup.c 11.2 KB
Newer Older
1
/*
2
 * Copyright (c) 2013-2019, ARM Limited and Contributors. All rights reserved.
3
 *
dp-arm's avatar
dp-arm committed
4
 * SPDX-License-Identifier: BSD-3-Clause
5
6
 */

7
8
9
#include <assert.h>
#include <stddef.h>

10
#include <arch.h>
11
#include <arch_helpers.h>
12
#include <common/bl_common.h>
13
#include <context.h>
14
15
16
17
#include <lib/el3_runtime/context_mgmt.h>
#include <lib/cpus/errata_report.h>
#include <plat/common/platform.h>

18
#include "psci_private.h"
19
20
21
22
23

/*******************************************************************************
 * Per cpu non-secure contexts used to program the architectural state prior
 * return to the normal world.
 * TODO: Use the memory allocator to set aside memory for the contexts instead
24
 * of relying on platform defined constants.
25
 ******************************************************************************/
26
static cpu_context_t psci_ns_context[PLATFORM_CORE_COUNT];
27

Soby Mathew's avatar
Soby Mathew committed
28
29
30
/******************************************************************************
 * Define the psci capability variable.
 *****************************************************************************/
31
unsigned int psci_caps;
Soby Mathew's avatar
Soby Mathew committed
32

33
/*******************************************************************************
34
35
 * Function which initializes the 'psci_non_cpu_pd_nodes' or the
 * 'psci_cpu_pd_nodes' corresponding to the power level.
36
 ******************************************************************************/
37
static void __init psci_init_pwr_domain_node(unsigned char node_idx,
38
					unsigned int parent_idx,
39
					unsigned char level)
40
{
41
42
43
44
45
46
47
48
	if (level > PSCI_CPU_PWR_LVL) {
		psci_non_cpu_pd_nodes[node_idx].level = level;
		psci_lock_init(psci_non_cpu_pd_nodes, node_idx);
		psci_non_cpu_pd_nodes[node_idx].parent_node = parent_idx;
		psci_non_cpu_pd_nodes[node_idx].local_state =
							 PLAT_MAX_OFF_STATE;
	} else {
		psci_cpu_data_t *svc_cpu_data;
49

50
		psci_cpu_pd_nodes[node_idx].parent_node = parent_idx;
51

52
53
		/* Initialize with an invalid mpidr */
		psci_cpu_pd_nodes[node_idx].mpidr = PSCI_INVALID_MPIDR;
54

55
56
		svc_cpu_data =
			&(_cpu_data_by_index(node_idx)->psci_svc_cpu_data);
57

58
59
		/* Set the Affinity Info for the cores as OFF */
		svc_cpu_data->aff_info_state = AFF_STATE_OFF;
60

61
		/* Invalidate the suspend level for the cpu */
62
		svc_cpu_data->target_pwrlvl = PSCI_INVALID_PWR_LVL;
63

64
65
		/* Set the power state to OFF state */
		svc_cpu_data->local_state = PLAT_MAX_OFF_STATE;
66

67
		psci_flush_dcache_range((uintptr_t)svc_cpu_data,
68
69
70
71
72
73
						 sizeof(*svc_cpu_data));

		cm_set_context_by_index(node_idx,
					(void *) &psci_ns_context[node_idx],
					NON_SECURE);
	}
74
75
}

76
/*******************************************************************************
77
78
79
80
81
82
83
84
 * This functions updates cpu_start_idx and ncpus field for each of the node in
 * psci_non_cpu_pd_nodes[]. It does so by comparing the parent nodes of each of
 * the CPUs and check whether they match with the parent of the previous
 * CPU. The basic assumption for this work is that children of the same parent
 * are allocated adjacent indices. The platform should ensure this though proper
 * mapping of the CPUs to indices via plat_core_pos_by_mpidr() and
 * plat_my_core_pos() APIs.
 *******************************************************************************/
85
static void __init psci_update_pwrlvl_limits(void)
86
{
87
	int j, cpu_idx;
88
	unsigned int nodes_idx[PLAT_MAX_PWR_LVL] = {0};
89
	unsigned int temp_index[PLAT_MAX_PWR_LVL];
90
91
92

	for (cpu_idx = 0; cpu_idx < PLATFORM_CORE_COUNT; cpu_idx++) {
		psci_get_parent_pwr_domain_nodes(cpu_idx,
93
						 (unsigned int)PLAT_MAX_PWR_LVL,
94
						 temp_index);
95
		for (j = (int) PLAT_MAX_PWR_LVL - 1; j >= 0; j--) {
96
97
98
99
			if (temp_index[j] != nodes_idx[j]) {
				nodes_idx[j] = temp_index[j];
				psci_non_cpu_pd_nodes[nodes_idx[j]].cpu_start_idx
					= cpu_idx;
100
			}
101
102
			psci_non_cpu_pd_nodes[nodes_idx[j]].ncpus++;
		}
103
104
105
	}
}

106
/*******************************************************************************
107
108
109
110
 * Core routine to populate the power domain tree. The tree descriptor passed by
 * the platform is populated breadth-first and the first entry in the map
 * informs the number of root power domains. The parent nodes of the root nodes
 * will point to an invalid entry(-1).
111
 ******************************************************************************/
112
static void __init populate_power_domain_tree(const unsigned char *topology)
113
{
114
115
116
117
	unsigned int i, j = 0U, num_nodes_at_lvl = 1U, num_nodes_at_next_lvl;
	unsigned int node_index = 0U, num_children;
	int parent_node_index = 0;
	int level = (int) PLAT_MAX_PWR_LVL;
118
119

	/*
120
121
122
123
124
125
126
	 * For each level the inputs are:
	 * - number of nodes at this level in plat_array i.e. num_nodes_at_level
	 *   This is the sum of values of nodes at the parent level.
	 * - Index of first entry at this level in the plat_array i.e.
	 *   parent_node_index.
	 * - Index of first free entry in psci_non_cpu_pd_nodes[] or
	 *   psci_cpu_pd_nodes[] i.e. node_index depending upon the level.
127
	 */
128
129
	while (level >= (int) PSCI_CPU_PWR_LVL) {
		num_nodes_at_next_lvl = 0U;
130
		/*
131
132
133
134
135
136
		 * For each entry (parent node) at this level in the plat_array:
		 * - Find the number of children
		 * - Allocate a node in a power domain array for each child
		 * - Set the parent of the child to the parent_node_index - 1
		 * - Increment parent_node_index to point to the next parent
		 * - Accumulate the number of children at next level.
137
		 */
138
		for (i = 0U; i < num_nodes_at_lvl; i++) {
139
140
141
142
143
			assert(parent_node_index <=
					PSCI_NUM_NON_CPU_PWR_DOMAINS);
			num_children = topology[parent_node_index];

			for (j = node_index;
144
145
				j < (node_index + num_children); j++)
				psci_init_pwr_domain_node((unsigned char)j,
146
							  parent_node_index - 1,
147
							  (unsigned char)level);
148
149
150
151
152

			node_index = j;
			num_nodes_at_next_lvl += num_children;
			parent_node_index++;
		}
153

154
155
		num_nodes_at_lvl = num_nodes_at_next_lvl;
		level--;
156

157
		/* Reset the index for the cpu power domain array */
158
		if (level == (int) PSCI_CPU_PWR_LVL)
159
			node_index = 0;
160
161
	}

162
	/* Validate the sanity of array exported by the platform */
163
	assert((int) j == PLATFORM_CORE_COUNT);
164
165
166
}

/*******************************************************************************
Soby Mathew's avatar
Soby Mathew committed
167
168
169
170
171
172
 * This function does the architectural setup and takes the warm boot
 * entry-point `mailbox_ep` as an argument. The function also initializes the
 * power domain topology tree by querying the platform. The power domain nodes
 * higher than the CPU are populated in the array psci_non_cpu_pd_nodes[] and
 * the CPU power domains are populated in psci_cpu_pd_nodes[]. The platform
 * exports its static topology map through the
173
174
 * populate_power_domain_topology_tree() API. The algorithm populates the
 * psci_non_cpu_pd_nodes and psci_cpu_pd_nodes iteratively by using this
Soby Mathew's avatar
Soby Mathew committed
175
176
177
 * topology map.  On a platform that implements two clusters of 2 cpus each,
 * and supporting 3 domain levels, the populated psci_non_cpu_pd_nodes would
 * look like this:
178
179
 *
 * ---------------------------------------------------
180
 * | system node | cluster 0 node  | cluster 1 node  |
181
182
 * ---------------------------------------------------
 *
183
184
185
186
187
 * And populated psci_cpu_pd_nodes would look like this :
 * <-    cpus cluster0   -><-   cpus cluster1   ->
 * ------------------------------------------------
 * |   CPU 0   |   CPU 1   |   CPU 2   |   CPU 3  |
 * ------------------------------------------------
188
 ******************************************************************************/
189
int __init psci_setup(const psci_lib_args_t *lib_args)
190
{
191
	const unsigned char *topology_tree;
192

193
194
	assert(VERIFY_PSCI_LIB_ARGS_V1(lib_args));

Soby Mathew's avatar
Soby Mathew committed
195
196
197
	/* Do the Architectural initialization */
	psci_arch_setup();

198
199
	/* Query the topology map from the platform */
	topology_tree = plat_get_power_domain_tree_desc();
200

201
202
	/* Populate the power domain arrays using the platform topology map */
	populate_power_domain_tree(topology_tree);
203

204
205
206
207
208
209
	/* Update the CPU limits for each node in psci_non_cpu_pd_nodes */
	psci_update_pwrlvl_limits();

	/* Populate the mpidr field of cpu node for this CPU */
	psci_cpu_pd_nodes[plat_my_core_pos()].mpidr =
		read_mpidr() & MPIDR_AFFINITY_MASK;
210

211
	psci_init_req_local_pwr_states();
212
213

	/*
214
215
	 * Set the requested and target state of this CPU and all the higher
	 * power domain levels for this CPU to run.
216
	 */
217
	psci_set_pwr_domains_to_run(PLAT_MAX_PWR_LVL);
218

219
220
221
	(void) plat_setup_psci_ops((uintptr_t)lib_args->mailbox_ep,
				   &psci_plat_pm_ops);
	assert(psci_plat_pm_ops != NULL);
222

223
224
	/*
	 * Flush `psci_plat_pm_ops` as it will be accessed by secondary CPUs
225
	 * during warm boot, possibly before data cache is enabled.
226
	 */
227
	psci_flush_dcache_range((uintptr_t)&psci_plat_pm_ops,
228
229
					sizeof(psci_plat_pm_ops));

Soby Mathew's avatar
Soby Mathew committed
230
231
232
	/* Initialize the psci capability */
	psci_caps = PSCI_GENERIC_CAP;

233
	if (psci_plat_pm_ops->pwr_domain_off != NULL)
Soby Mathew's avatar
Soby Mathew committed
234
		psci_caps |=  define_psci_cap(PSCI_CPU_OFF);
235
236
	if ((psci_plat_pm_ops->pwr_domain_on != NULL) &&
	    (psci_plat_pm_ops->pwr_domain_on_finish != NULL))
Soby Mathew's avatar
Soby Mathew committed
237
		psci_caps |=  define_psci_cap(PSCI_CPU_ON_AARCH64);
238
239
	if ((psci_plat_pm_ops->pwr_domain_suspend != NULL) &&
	    (psci_plat_pm_ops->pwr_domain_suspend_finish != NULL)) {
Soby Mathew's avatar
Soby Mathew committed
240
		psci_caps |=  define_psci_cap(PSCI_CPU_SUSPEND_AARCH64);
241
		if (psci_plat_pm_ops->get_sys_suspend_power_state != NULL)
242
243
			psci_caps |=  define_psci_cap(PSCI_SYSTEM_SUSPEND_AARCH64);
	}
244
	if (psci_plat_pm_ops->system_off != NULL)
Soby Mathew's avatar
Soby Mathew committed
245
		psci_caps |=  define_psci_cap(PSCI_SYSTEM_OFF);
246
	if (psci_plat_pm_ops->system_reset != NULL)
Soby Mathew's avatar
Soby Mathew committed
247
		psci_caps |=  define_psci_cap(PSCI_SYSTEM_RESET);
248
	if (psci_plat_pm_ops->get_node_hw_state != NULL)
249
		psci_caps |= define_psci_cap(PSCI_NODE_HW_STATE_AARCH64);
250
251
	if ((psci_plat_pm_ops->read_mem_protect != NULL) &&
			(psci_plat_pm_ops->write_mem_protect != NULL))
252
		psci_caps |= define_psci_cap(PSCI_MEM_PROTECT);
253
	if (psci_plat_pm_ops->mem_protect_chk != NULL)
254
		psci_caps |= define_psci_cap(PSCI_MEM_CHK_RANGE_AARCH64);
255
	if (psci_plat_pm_ops->system_reset2 != NULL)
256
		psci_caps |= define_psci_cap(PSCI_SYSTEM_RESET2_AARCH64);
Soby Mathew's avatar
Soby Mathew committed
257

258
259
260
261
262
#if ENABLE_PSCI_STAT
	psci_caps |=  define_psci_cap(PSCI_STAT_RESIDENCY_AARCH64);
	psci_caps |=  define_psci_cap(PSCI_STAT_COUNT_AARCH64);
#endif

Achin Gupta's avatar
Achin Gupta committed
263
	return 0;
264
}
Soby Mathew's avatar
Soby Mathew committed
265
266
267
268
269
270
271
272

/*******************************************************************************
 * This duplicates what the primary cpu did after a cold boot in BL1. The same
 * needs to be done when a cpu is hotplugged in. This function could also over-
 * ride any EL3 setup done by BL1 as this code resides in rw memory.
 ******************************************************************************/
void psci_arch_setup(void)
{
273
#if (ARM_ARCH_MAJOR > 7) || defined(ARMV7_SUPPORTS_GENERIC_TIMER)
Soby Mathew's avatar
Soby Mathew committed
274
275
	/* Program the counter frequency */
	write_cntfrq_el0(plat_get_syscnt_freq2());
276
#endif
Soby Mathew's avatar
Soby Mathew committed
277
278
279

	/* Initialize the cpu_ops pointer. */
	init_cpu_ops();
280
281
282

	/* Having initialized cpu_ops, we can now print errata status */
	print_errata_status();
283
284
285
286
287
288

#if ENABLE_PAUTH
	/* Store APIAKey_EL1 key */
	set_cpu_data(apiakey[0], read_apiakeylo_el1());
	set_cpu_data(apiakey[1], read_apiakeyhi_el1());
#endif /* ENABLE_PAUTH */
Soby Mathew's avatar
Soby Mathew committed
289
}
Soby Mathew's avatar
Soby Mathew committed
290
291
292
293
294
295
296
297
298
299
300
301

/******************************************************************************
 * PSCI Library interface to initialize the cpu context for the next non
 * secure image during cold boot. The relevant registers in the cpu context
 * need to be retrieved and programmed on return from this interface.
 *****************************************************************************/
void psci_prepare_next_non_secure_ctx(entry_point_info_t *next_image_info)
{
	assert(GET_SECURITY_STATE(next_image_info->h.attr) == NON_SECURE);
	cm_init_my_context(next_image_info);
	cm_prepare_el3_exit(NON_SECURE);
}