psci_common.c 18.8 KB
Newer Older
1
/*
2
 * Copyright (c) 2013-2015, ARM Limited and Contributors. All rights reserved.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of ARM nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific
 * prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <arch.h>
#include <arch_helpers.h>
#include <assert.h>
#include <bl_common.h>
#include <context.h>
#include <context_mgmt.h>
#include <debug.h>
#include <platform.h>
#include <string.h>
#include "psci_private.h"

/*
 * SPD power management operations, expected to be supplied by the registered
 * SPD on successful SP initialization
 */
const spd_pm_ops_t *psci_spd_pm;

/*******************************************************************************
 * Grand array that holds the platform's topology information for state
50
51
 * management of power domain instances. Each node (pwr_map_node) in the array
 * corresponds to a power domain instance e.g. cluster, cpu within an mpidr
52
 ******************************************************************************/
53
pwr_map_node_t psci_pwr_domain_map[PSCI_NUM_PWR_DOMAINS]
54
55
56
57
58
59
60
61
62
63
64
#if USE_COHERENT_MEM
__attribute__ ((section("tzfw_coherent_mem")))
#endif
;

/*******************************************************************************
 * Pointer to functions exported by the platform to complete power mgmt. ops
 ******************************************************************************/
const plat_pm_ops_t *psci_plat_pm_ops;

/*******************************************************************************
65
 * Check that the maximum power level supported by the platform makes sense
66
 * ****************************************************************************/
67
68
69
CASSERT(PLAT_MAX_PWR_LVL <= MPIDR_MAX_AFFLVL && \
		PLAT_MAX_PWR_LVL >= MPIDR_AFFLVL0, \
		assert_platform_max_pwrlvl_check);
70
71

/*******************************************************************************
72
73
74
75
 * This function is passed an array of pointers to power domain nodes in the
 * topology tree for an mpidr. It iterates through the nodes to find the
 * highest power level where the power domain is marked as physically powered
 * off.
76
 ******************************************************************************/
77
78
79
uint32_t psci_find_max_phys_off_pwrlvl(uint32_t start_pwrlvl,
				       uint32_t end_pwrlvl,
				       pwr_map_node_t *mpidr_nodes[])
80
{
81
	uint32_t max_pwrlvl = PSCI_INVALID_DATA;
82

83
84
	for (; start_pwrlvl <= end_pwrlvl; start_pwrlvl++) {
		if (mpidr_nodes[start_pwrlvl] == NULL)
85
86
			continue;

87
		if (psci_get_phys_state(mpidr_nodes[start_pwrlvl]) ==
88
		    PSCI_STATE_OFF)
89
			max_pwrlvl = start_pwrlvl;
90
91
	}

92
	return max_pwrlvl;
93
94
95
96
97
98
99
100
101
102
103
104
105
}

/*******************************************************************************
 * This function verifies that the all the other cores in the system have been
 * turned OFF and the current CPU is the last running CPU in the system.
 * Returns 1 (true) if the current CPU is the last ON CPU or 0 (false)
 * otherwise.
 ******************************************************************************/
unsigned int psci_is_last_on_cpu(void)
{
	unsigned long mpidr = read_mpidr_el1() & MPIDR_AFFINITY_MASK;
	unsigned int i;

106
107
	for (i = psci_pwr_lvl_limits[MPIDR_AFFLVL0].min;
			i <= psci_pwr_lvl_limits[MPIDR_AFFLVL0].max; i++) {
108

109
		assert(psci_pwr_domain_map[i].level == MPIDR_AFFLVL0);
110

111
		if (!(psci_pwr_domain_map[i].state & PSCI_AFF_PRESENT))
112
113
			continue;

114
115
		if (psci_pwr_domain_map[i].mpidr == mpidr) {
			assert(psci_get_state(&psci_pwr_domain_map[i])
116
117
118
119
					== PSCI_STATE_ON);
			continue;
		}

120
		if (psci_get_state(&psci_pwr_domain_map[i]) != PSCI_STATE_OFF)
121
122
123
124
125
126
127
			return 0;
	}

	return 1;
}

/*******************************************************************************
128
 * Routine to return the maximum power level to traverse to after a cpu has
129
130
131
 * been physically powered up. It is expected to be called immediately after
 * reset from assembler code.
 ******************************************************************************/
132
int get_power_on_target_pwrlvl(void)
133
{
134
	int pwrlvl;
135
136
137

#if DEBUG
	unsigned int state;
138
	pwr_map_node_t *node;
139
140

	/* Retrieve our node from the topology tree */
141
	node = psci_get_pwr_map_node(read_mpidr_el1() & MPIDR_AFFINITY_MASK,
142
143
144
145
146
147
148
149
150
151
152
153
				     MPIDR_AFFLVL0);
	assert(node);

	/*
	 * Sanity check the state of the cpu. It should be either suspend or "on
	 * pending"
	 */
	state = psci_get_state(node);
	assert(state == PSCI_STATE_SUSPEND || state == PSCI_STATE_ON_PENDING);
#endif

	/*
154
	 * Assume that this cpu was suspended and retrieve its target power
155
	 * level. If it is invalid then it could only have been turned off
156
	 * earlier. PLAT_MAX_PWR_LVL will be the highest power level a
157
158
	 * cpu can be turned off to.
	 */
159
160
161
162
	pwrlvl = psci_get_suspend_pwrlvl();
	if (pwrlvl == PSCI_INVALID_DATA)
		pwrlvl = PLAT_MAX_PWR_LVL;
	return pwrlvl;
163
164
165
}

/*******************************************************************************
166
167
168
 * Simple routine to set the id of a power domain instance at a given level
 * in the mpidr. The assumption is that the affinity level and the power
 * level are the same.
169
 ******************************************************************************/
170
171
172
unsigned long mpidr_set_pwr_domain_inst(unsigned long mpidr,
				 unsigned char pwr_inst,
				 int pwr_lvl)
173
174
175
{
	unsigned long aff_shift;

176
	assert(pwr_lvl <= MPIDR_AFFLVL3);
177
178
179

	/*
	 * Decide the number of bits to shift by depending upon
180
	 * the power level
181
	 */
182
	aff_shift = get_afflvl_shift(pwr_lvl);
183
184
185

	/* Clear the existing affinity instance & set the new one*/
	mpidr &= ~(((unsigned long)MPIDR_AFFLVL_MASK) << aff_shift);
186
	mpidr |= ((unsigned long)pwr_inst) << aff_shift;
187
188
189
190
191

	return mpidr;
}

/*******************************************************************************
192
 * This function sanity checks a range of power levels.
193
 ******************************************************************************/
194
int psci_check_pwrlvl_range(int start_pwrlvl, int end_pwrlvl)
195
196
{
	/* Sanity check the parameters passed */
197
	if (end_pwrlvl > PLAT_MAX_PWR_LVL)
198
199
		return PSCI_E_INVALID_PARAMS;

200
	if (start_pwrlvl < MPIDR_AFFLVL0)
201
202
		return PSCI_E_INVALID_PARAMS;

203
	if (end_pwrlvl < start_pwrlvl)
204
205
206
207
208
209
		return PSCI_E_INVALID_PARAMS;

	return PSCI_E_SUCCESS;
}

/*******************************************************************************
210
 * This function is passed an array of pointers to power domain nodes in the
211
 * topology tree for an mpidr and the state which each node should transition
212
 * to. It updates the state of each node between the specified power levels.
213
 ******************************************************************************/
214
215
216
void psci_do_state_coordination(uint32_t start_pwrlvl,
			       uint32_t end_pwrlvl,
			       pwr_map_node_t *mpidr_nodes[],
217
218
219
220
			       uint32_t state)
{
	uint32_t level;

221
	for (level = start_pwrlvl; level <= end_pwrlvl; level++) {
222
223
224
225
226
227
228
		if (mpidr_nodes[level] == NULL)
			continue;
		psci_set_state(mpidr_nodes[level], state);
	}
}

/*******************************************************************************
229
230
 * This function is passed an array of pointers to power domain nodes in the
 * topology tree for an mpidr. It picks up locks for each power level bottom
231
232
 * up in the range specified.
 ******************************************************************************/
233
234
235
void psci_acquire_pwr_domain_locks(int start_pwrlvl,
			       int end_pwrlvl,
			       pwr_map_node_t *mpidr_nodes[])
236
237
238
{
	int level;

239
	for (level = start_pwrlvl; level <= end_pwrlvl; level++) {
240
241
242
243
244
245
246
247
		if (mpidr_nodes[level] == NULL)
			continue;

		psci_lock_get(mpidr_nodes[level]);
	}
}

/*******************************************************************************
248
249
 * This function is passed an array of pointers to power domain nodes in the
 * topology tree for an mpidr. It releases the lock for each power level top
250
251
 * down in the range specified.
 ******************************************************************************/
252
253
254
void psci_release_pwr_domain_locks(int start_pwrlvl,
			       int end_pwrlvl,
			       pwr_map_node_t *mpidr_nodes[])
255
256
257
{
	int level;

258
	for (level = end_pwrlvl; level >= start_pwrlvl; level--) {
259
260
261
262
263
264
265
266
		if (mpidr_nodes[level] == NULL)
			continue;

		psci_lock_release(mpidr_nodes[level]);
	}
}

/*******************************************************************************
267
 * Simple routine to determine whether a mpidr is valid or not.
268
 ******************************************************************************/
269
int psci_validate_mpidr(unsigned long mpidr)
270
{
271
	if (plat_core_pos_by_mpidr(mpidr) < 0)
272
		return PSCI_E_INVALID_PARAMS;
273
274

	return PSCI_E_SUCCESS;
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
}

/*******************************************************************************
 * This function determines the full entrypoint information for the requested
 * PSCI entrypoint on power on/resume and returns it.
 ******************************************************************************/
int psci_get_ns_ep_info(entry_point_info_t *ep,
		       uint64_t entrypoint, uint64_t context_id)
{
	uint32_t ep_attr, mode, sctlr, daif, ee;
	uint32_t ns_scr_el3 = read_scr_el3();
	uint32_t ns_sctlr_el1 = read_sctlr_el1();

	sctlr = ns_scr_el3 & SCR_HCE_BIT ? read_sctlr_el2() : ns_sctlr_el1;
	ee = 0;

	ep_attr = NON_SECURE | EP_ST_DISABLE;
	if (sctlr & SCTLR_EE_BIT) {
		ep_attr |= EP_EE_BIG;
		ee = 1;
	}
	SET_PARAM_HEAD(ep, PARAM_EP, VERSION_1, ep_attr);

	ep->pc = entrypoint;
	memset(&ep->args, 0, sizeof(ep->args));
	ep->args.arg0 = context_id;

	/*
	 * Figure out whether the cpu enters the non-secure address space
	 * in aarch32 or aarch64
	 */
	if (ns_scr_el3 & SCR_RW_BIT) {

		/*
		 * Check whether a Thumb entry point has been provided for an
		 * aarch64 EL
		 */
		if (entrypoint & 0x1)
			return PSCI_E_INVALID_PARAMS;

		mode = ns_scr_el3 & SCR_HCE_BIT ? MODE_EL2 : MODE_EL1;

		ep->spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
	} else {

		mode = ns_scr_el3 & SCR_HCE_BIT ? MODE32_hyp : MODE32_svc;

		/*
		 * TODO: Choose async. exception bits if HYP mode is not
		 * implemented according to the values of SCR.{AW, FW} bits
		 */
		daif = DAIF_ABT_BIT | DAIF_IRQ_BIT | DAIF_FIQ_BIT;

		ep->spsr = SPSR_MODE32(mode, entrypoint & 0x1, ee, daif);
	}

	return PSCI_E_SUCCESS;
}

/*******************************************************************************
335
336
 * This function takes a pointer to a power domain node in the topology tree
 * and returns its state. State of a non-leaf node needs to be calculated.
337
 ******************************************************************************/
338
unsigned short psci_get_state(pwr_map_node_t *node)
339
340
341
342
343
344
345
346
347
348
349
350
{
#if !USE_COHERENT_MEM
	flush_dcache_range((uint64_t) node, sizeof(*node));
#endif

	assert(node->level >= MPIDR_AFFLVL0 && node->level <= MPIDR_MAX_AFFLVL);

	/* A cpu node just contains the state which can be directly returned */
	if (node->level == MPIDR_AFFLVL0)
		return (node->state >> PSCI_STATE_SHIFT) & PSCI_STATE_MASK;

	/*
351
	 * For a power level higher than a cpu, the state has to be
352
	 * calculated. It depends upon the value of the reference count
353
	 * which is managed by each node at the next lower power level
354
	 * e.g. for a cluster, each cpu increments/decrements the reference
355
	 * count. If the reference count is 0 then the power level is
356
357
358
359
360
361
362
363
364
	 * OFF else ON.
	 */
	if (node->ref_count)
		return PSCI_STATE_ON;
	else
		return PSCI_STATE_OFF;
}

/*******************************************************************************
365
366
367
 * This function takes a pointer to a power domain node in the topology
 * tree and a target state. State of a non-leaf node needs to be converted
 * to a reference count. State of a leaf node can be set directly.
368
 ******************************************************************************/
369
void psci_set_state(pwr_map_node_t *node, unsigned short state)
370
371
372
373
{
	assert(node->level >= MPIDR_AFFLVL0 && node->level <= MPIDR_MAX_AFFLVL);

	/*
374
	 * For a power level higher than a cpu, the state is used
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
	 * to decide whether the reference count is incremented or
	 * decremented. Entry into the ON_PENDING state does not have
	 * effect.
	 */
	if (node->level > MPIDR_AFFLVL0) {
		switch (state) {
		case PSCI_STATE_ON:
			node->ref_count++;
			break;
		case PSCI_STATE_OFF:
		case PSCI_STATE_SUSPEND:
			node->ref_count--;
			break;
		case PSCI_STATE_ON_PENDING:
			/*
390
			 * A power level higher than a cpu will not undergo
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
			 * a state change when it is about to be turned on
			 */
			return;
		default:
			assert(0);
		}
	} else {
		node->state &= ~(PSCI_STATE_MASK << PSCI_STATE_SHIFT);
		node->state |= (state & PSCI_STATE_MASK) << PSCI_STATE_SHIFT;
	}

#if !USE_COHERENT_MEM
	flush_dcache_range((uint64_t) node, sizeof(*node));
#endif
}

/*******************************************************************************
408
 * A power domain could be on, on_pending, suspended or off. These are the
409
410
 * logical states it can be in. Physically either it is off or on. When it is in
 * the state on_pending then it is about to be turned on. It is not possible to
411
412
 * tell whether that's actually happened or not. So we err on the side of
 * caution & treat the power domain as being turned off.
413
 ******************************************************************************/
414
unsigned short psci_get_phys_state(pwr_map_node_t *node)
415
416
417
418
419
420
421
422
423
{
	unsigned int state;

	state = psci_get_state(node);
	return get_phys_state(state);
}

/*******************************************************************************
 * Generic handler which is called when a cpu is physically powered on. It
424
 * traverses the node information and finds the highest power level powered
425
 * off and performs generic, architectural, platform setup and state management
426
 * to power on that power level and power levels below it.
427
428
429
 * e.g. For a cpu that's been powered on, it will call the platform specific
 * code to enable the gic cpu interface and for a cluster it will enable
 * coherency at the interconnect level in addition to gic cpu interface.
430
 ******************************************************************************/
431
432
void psci_power_up_finish(int end_pwrlvl,
				 pwrlvl_power_on_finisher_t pon_handler)
433
{
434
	mpidr_pwr_map_nodes_t mpidr_nodes;
435
	int rc;
436
	unsigned int max_phys_off_pwrlvl;
437
438
439
440


	/*
	 * Collect the pointers to the nodes in the topology tree for
441
442
	 * each power domain instances in the mpidr. If this function does
	 * not return successfully then either the mpidr or the power
443
444
	 * levels are incorrect. Either case is an irrecoverable error.
	 */
445
	rc = psci_get_pwr_map_nodes(read_mpidr_el1() & MPIDR_AFFINITY_MASK,
446
				    MPIDR_AFFLVL0,
447
				    end_pwrlvl,
448
449
450
451
452
				    mpidr_nodes);
	if (rc != PSCI_E_SUCCESS)
		panic();

	/*
453
	 * This function acquires the lock corresponding to each power
454
455
456
	 * level so that by the time all locks are taken, the system topology
	 * is snapshot and state management can be done safely.
	 */
457
458
	psci_acquire_pwr_domain_locks(MPIDR_AFFLVL0,
				  end_pwrlvl,
459
460
				  mpidr_nodes);

461
462
	max_phys_off_pwrlvl = psci_find_max_phys_off_pwrlvl(MPIDR_AFFLVL0,
							    end_pwrlvl,
463
							    mpidr_nodes);
464
	assert(max_phys_off_pwrlvl != PSCI_INVALID_DATA);
465
466

	/* Perform generic, architecture and platform specific handling */
467
	pon_handler(mpidr_nodes, max_phys_off_pwrlvl);
468
469

	/*
470
471
	 * This function updates the state of each power instance
	 * corresponding to the mpidr in the range of power levels
472
473
	 * specified.
	 */
474
475
	psci_do_state_coordination(MPIDR_AFFLVL0,
				  end_pwrlvl,
476
477
478
479
				  mpidr_nodes,
				  PSCI_STATE_ON);

	/*
480
	 * This loop releases the lock corresponding to each power level
481
482
	 * in the reverse order to which they were acquired.
	 */
483
484
	psci_release_pwr_domain_locks(MPIDR_AFFLVL0,
				  end_pwrlvl,
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
				  mpidr_nodes);
}

/*******************************************************************************
 * This function initializes the set of hooks that PSCI invokes as part of power
 * management operation. The power management hooks are expected to be provided
 * by the SPD, after it finishes all its initialization
 ******************************************************************************/
void psci_register_spd_pm_hook(const spd_pm_ops_t *pm)
{
	assert(pm);
	psci_spd_pm = pm;

	if (pm->svc_migrate)
		psci_caps |= define_psci_cap(PSCI_MIG_AARCH64);

	if (pm->svc_migrate_info)
		psci_caps |= define_psci_cap(PSCI_MIG_INFO_UP_CPU_AARCH64)
				| define_psci_cap(PSCI_MIG_INFO_TYPE);
}

/*******************************************************************************
 * This function invokes the migrate info hook in the spd_pm_ops. It performs
 * the necessary return value validation. If the Secure Payload is UP and
 * migrate capable, it returns the mpidr of the CPU on which the Secure payload
 * is resident through the mpidr parameter. Else the value of the parameter on
 * return is undefined.
 ******************************************************************************/
int psci_spd_migrate_info(uint64_t *mpidr)
{
	int rc;

	if (!psci_spd_pm || !psci_spd_pm->svc_migrate_info)
		return PSCI_E_NOT_SUPPORTED;

	rc = psci_spd_pm->svc_migrate_info(mpidr);

	assert(rc == PSCI_TOS_UP_MIG_CAP || rc == PSCI_TOS_NOT_UP_MIG_CAP \
		|| rc == PSCI_TOS_NOT_PRESENT_MP || rc == PSCI_E_NOT_SUPPORTED);

	return rc;
}


/*******************************************************************************
530
 * This function prints the state of all power domains present in the
531
532
 * system
 ******************************************************************************/
533
void psci_print_power_domain_map(void)
534
535
{
#if LOG_LEVEL >= LOG_LEVEL_INFO
536
	pwr_map_node_t *node;
537
538
539
540
541
542
543
544
545
	unsigned int idx;
	/* This array maps to the PSCI_STATE_X definitions in psci.h */
	static const char *psci_state_str[] = {
		"ON",
		"OFF",
		"ON_PENDING",
		"SUSPEND"
	};

546
547
548
549
	INFO("PSCI Power Domain Map:\n");
	for (idx = 0; idx < PSCI_NUM_PWR_DOMAINS; idx++) {
		node = &psci_pwr_domain_map[idx];
		if (!(node->state & PSCI_PWR_DOMAIN_PRESENT)) {
550
551
			continue;
		}
552
		INFO("  pwrInst: Level %u, MPID 0x%lx, State %s\n",
553
554
555
556
557
				node->level, node->mpidr,
				psci_state_str[psci_get_state(node)]);
	}
#endif
}