xref: /rk3399_ARM-atf/lib/psci/psci_main.c (revision 7ad4b5ed31e33dca21dd4d2f4a9f64f9b7d4db85)
1 /*
2  * Copyright (c) 2013-2026, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 #include <string.h>
9 
10 #include <arch.h>
11 #include <arch_features.h>
12 #include <arch_helpers.h>
13 #include <common/debug.h>
14 #include <lib/pmf/pmf.h>
15 #include <lib/runtime_instr.h>
16 #include <lib/smccc.h>
17 #include <plat/common/platform.h>
18 #include <services/arm_arch_svc.h>
19 
20 #include "psci_private.h"
21 
22 /*******************************************************************************
23  * PSCI frontend api for servicing SMCs. Described in the PSCI spec.
24  ******************************************************************************/
25 int psci_cpu_on(u_register_t target_cpu,
26 		uintptr_t entrypoint,
27 		u_register_t context_id)
28 
29 {
30 	int rc;
31 	entry_point_info_t *ep = NULL;
32 	unsigned int target_idx = (unsigned int)plat_core_pos_by_mpidr(target_cpu);
33 
34 	/* Validate the target CPU */
35 	if (!is_valid_mpidr(target_cpu)) {
36 		return PSCI_E_INVALID_PARAMS;
37 	}
38 
39 	ep = get_cpu_data_by_index(target_idx, warmboot_ep_info);
40 	/* Validate the lower EL entry point and put it in the entry_point_info */
41 	rc = psci_validate_entry_point(ep, entrypoint, context_id);
42 	if (rc != PSCI_E_SUCCESS) {
43 		return rc;
44 	}
45 
46 	/*
47 	 * To turn this cpu on, specify which power
48 	 * levels need to be turned on
49 	 */
50 	return psci_cpu_on_start(target_cpu);
51 }
52 
53 unsigned int psci_version(void)
54 {
55 	return PSCI_MAJOR_VER | PSCI_MINOR_VER;
56 }
57 
58 int psci_cpu_suspend(unsigned int power_state,
59 		     uintptr_t entrypoint,
60 		     u_register_t context_id)
61 {
62 	int rc;
63 	unsigned int target_pwrlvl, is_power_down_state;
64 	psci_power_state_t state_info = { {PSCI_LOCAL_STATE_RUN} };
65 	plat_local_state_t cpu_pd_state;
66 	unsigned int cpu_idx = plat_my_core_pos();
67 
68 #if ERRATA_SME_POWER_DOWN
69 	/*
70 	 * If SME isn't off, attempting a real power down will only end up being
71 	 * rejected. If we got called with SME on, fall back to a normal
72 	 * suspend. We can't force SME off as in the event the power down is
73 	 * rejected for another reason (eg GIC) we'd lose the SME context.
74 	 */
75 	if (is_feat_sme_supported() && read_svcr() != 0) {
76 		power_state &= ~(PSTATE_TYPE_MASK << PSTATE_TYPE_SHIFT);
77 		power_state &= ~(PSTATE_PWR_LVL_MASK << PSTATE_PWR_LVL_SHIFT);
78 	}
79 #endif /* ERRATA_SME_POWER_DOWN */
80 
81 	/* Validate the power_state parameter */
82 	rc = psci_validate_power_state(power_state, &state_info);
83 	if (rc != PSCI_E_SUCCESS) {
84 		assert(rc == PSCI_E_INVALID_PARAMS);
85 		return rc;
86 	}
87 
88 	/*
89 	 * Get the value of the state type bit from the power state parameter.
90 	 */
91 	is_power_down_state = psci_get_pstate_type(power_state);
92 
93 	/* Sanity check the requested suspend levels */
94 	assert(psci_validate_suspend_req(&state_info, is_power_down_state)
95 			== PSCI_E_SUCCESS);
96 
97 	target_pwrlvl = psci_find_target_suspend_lvl(&state_info);
98 	if (target_pwrlvl == PSCI_INVALID_PWR_LVL) {
99 		ERROR("Invalid target power level for suspend operation\n");
100 		panic();
101 	}
102 
103 	/* Fast path for local CPU standby, won't interact with higher power levels. */
104 	if (is_cpu_standby_req(is_power_down_state, target_pwrlvl)) {
105 		if  (psci_plat_pm_ops->cpu_standby == NULL) {
106 			return PSCI_E_INVALID_PARAMS;
107 		}
108 
109 		/*
110 		 * Set the state of the CPU power domain to the platform
111 		 * specific retention state and enter the standby state.
112 		 */
113 		cpu_pd_state = state_info.pwr_domain_state[PSCI_CPU_PWR_LVL];
114 		psci_set_cpu_local_state(cpu_pd_state);
115 
116 #if ENABLE_PSCI_STAT
117 		plat_psci_stat_accounting_start(&state_info);
118 #endif
119 
120 #if ENABLE_RUNTIME_INSTRUMENTATION
121 		PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
122 		    RT_INSTR_ENTER_HW_LOW_PWR,
123 		    PMF_NO_CACHE_MAINT);
124 #endif
125 
126 		psci_plat_pm_ops->cpu_standby(cpu_pd_state);
127 
128 		/* Upon exit from standby, set the state back to RUN. */
129 		psci_set_cpu_local_state(PSCI_LOCAL_STATE_RUN);
130 
131 #if ENABLE_RUNTIME_INSTRUMENTATION
132 		PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
133 		    RT_INSTR_EXIT_HW_LOW_PWR,
134 		    PMF_NO_CACHE_MAINT);
135 #endif
136 
137 #if ENABLE_PSCI_STAT
138 		plat_psci_stat_accounting_stop(&state_info);
139 
140 		/* Update PSCI stats */
141 		psci_stats_update_pwr_up(cpu_idx, PSCI_CPU_PWR_LVL, &state_info);
142 #endif
143 
144 		return PSCI_E_SUCCESS;
145 	}
146 
147 	/*
148 	 * If a power down state has been requested, we need to verify entry
149 	 * point and program entry information.
150 	 */
151 	if (is_power_down_state != 0U) {
152 		entry_point_info_t *ep = get_cpu_data_by_index(cpu_idx, warmboot_ep_info);
153 
154 		rc = psci_validate_entry_point(ep, entrypoint, context_id);
155 		if (rc != PSCI_E_SUCCESS) {
156 			return rc;
157 		}
158 	}
159 
160 	/*
161 	 * Do what is needed to enter the power down state. Upon success,
162 	 * enter the final wfi which will power down this CPU. This function
163 	 * might return if the power down was abandoned for any reason, e.g.
164 	 * arrival of an interrupt
165 	 */
166 	rc = psci_cpu_suspend_start(cpu_idx,
167 				    target_pwrlvl,
168 				    &state_info,
169 				    is_power_down_state);
170 
171 	return rc;
172 }
173 
174 
175 int psci_system_suspend(uintptr_t entrypoint, u_register_t context_id)
176 {
177 	int rc;
178 	psci_power_state_t state_info;
179 	unsigned int cpu_idx = plat_my_core_pos();
180 	entry_point_info_t *ep = get_cpu_data_by_index(cpu_idx, warmboot_ep_info);
181 
182 	/* Check if the current CPU is the last ON CPU in the system */
183 	if (!psci_is_last_on_cpu(cpu_idx)) {
184 		return PSCI_E_DENIED;
185 	}
186 
187 	/* Validate the entry point and get the entry_point_info */
188 	rc = psci_validate_entry_point(ep, entrypoint, context_id);
189 	if (rc != PSCI_E_SUCCESS) {
190 		return rc;
191 	}
192 
193 	/* Query the psci_power_state for system suspend */
194 	psci_query_sys_suspend_pwrstate(&state_info);
195 
196 	/*
197 	 * Check if platform allows suspend to Highest power level
198 	 * (System level)
199 	 */
200 	if (psci_find_target_suspend_lvl(&state_info) < PLAT_MAX_PWR_LVL) {
201 		return PSCI_E_DENIED;
202 	}
203 	/* Ensure that the psci_power_state makes sense */
204 	assert(psci_validate_suspend_req(&state_info, PSTATE_TYPE_POWERDOWN)
205 						== PSCI_E_SUCCESS);
206 	assert(is_local_state_off(
207 			state_info.pwr_domain_state[PLAT_MAX_PWR_LVL]) != 0);
208 
209 	/*
210 	 * Do what is needed to enter the system suspend state. This function
211 	 * might return if the power down was abandoned for any reason, e.g.
212 	 * arrival of an interrupt
213 	 */
214 	rc = psci_cpu_suspend_start(cpu_idx,
215 				    PLAT_MAX_PWR_LVL,
216 				    &state_info,
217 				    PSTATE_TYPE_POWERDOWN);
218 
219 	return rc;
220 }
221 
222 int psci_cpu_off(void)
223 {
224 	int rc;
225 	unsigned int target_pwrlvl = PLAT_MAX_PWR_LVL;
226 
227 	/*
228 	 * Do what is needed to power off this CPU and possible higher power
229 	 * levels if it able to do so. Upon success, enter the final wfi
230 	 * which will power down this CPU.
231 	 */
232 	rc = psci_do_cpu_off(target_pwrlvl);
233 
234 	/*
235 	 * The only error cpu_off can return is E_DENIED. So check if that's
236 	 * indeed the case.
237 	 */
238 	assert(rc == PSCI_E_DENIED);
239 
240 	return rc;
241 }
242 
243 int psci_affinity_info(u_register_t target_affinity,
244 		       unsigned int lowest_affinity_level)
245 {
246 	unsigned int target_idx;
247 
248 	/* Validate the target affinity */
249 	if (!is_valid_mpidr(target_affinity)) {
250 		return PSCI_E_INVALID_PARAMS;
251 	}
252 
253 	/* We dont support level higher than PSCI_CPU_PWR_LVL */
254 	if (lowest_affinity_level > PSCI_CPU_PWR_LVL) {
255 		return PSCI_E_INVALID_PARAMS;
256 	}
257 	/* Calculate the cpu index of the target */
258 	target_idx = (unsigned int) plat_core_pos_by_mpidr(target_affinity);
259 
260 	/*
261 	 * Generic management:
262 	 * Perform cache maintanence ahead of reading the target CPU state to
263 	 * ensure that the data is not stale.
264 	 * There is a theoretical edge case where the cache may contain stale
265 	 * data for the target CPU data - this can occur under the following
266 	 * conditions:
267 	 * - the target CPU is in another cluster from the current
268 	 * - the target CPU was the last CPU to shutdown on its cluster
269 	 * - the cluster was removed from coherency as part of the CPU shutdown
270 	 *
271 	 * In this case the cache maintenace that was performed as part of the
272 	 * target CPUs shutdown was not seen by the current CPU's cluster. And
273 	 * so the cache may contain stale data for the target CPU.
274 	 */
275 	flush_cpu_data_by_index(target_idx, psci_svc_cpu_data);
276 
277 	return (int)psci_get_aff_info_state_by_idx(target_idx);
278 }
279 
280 int psci_migrate(u_register_t target_cpu)
281 {
282 	int rc;
283 	u_register_t resident_cpu_mpidr = 0;
284 
285 	/* Validate the target cpu */
286 	if (!is_valid_mpidr(target_cpu)) {
287 		return PSCI_E_INVALID_PARAMS;
288 	}
289 
290 	rc = psci_spd_migrate_info(&resident_cpu_mpidr);
291 	if (rc != PSCI_TOS_UP_MIG_CAP) {
292 		return (rc == PSCI_TOS_NOT_UP_MIG_CAP) ?
293 			  PSCI_E_DENIED : PSCI_E_NOT_SUPPORTED;
294 	}
295 
296 	/*
297 	 * Migrate should only be invoked on the CPU where
298 	 * the Secure OS is resident.
299 	 */
300 	if (resident_cpu_mpidr != read_mpidr_el1()) {
301 		return PSCI_E_NOT_PRESENT;
302 	}
303 
304 	/* Check the validity of the specified target cpu */
305 	if (!is_valid_mpidr(target_cpu)) {
306 		return PSCI_E_INVALID_PARAMS;
307 	}
308 
309 	assert((psci_spd_pm != NULL) && (psci_spd_pm->svc_migrate != NULL));
310 
311 	rc = psci_spd_pm->svc_migrate(read_mpidr_el1(), target_cpu);
312 	assert((rc == PSCI_E_SUCCESS) || (rc == PSCI_E_INTERN_FAIL));
313 
314 	return rc;
315 }
316 
317 int psci_migrate_info_type(void)
318 {
319 	u_register_t resident_cpu_mpidr = 0;
320 
321 	return psci_spd_migrate_info(&resident_cpu_mpidr);
322 }
323 
324 u_register_t psci_migrate_info_up_cpu(void)
325 {
326 	u_register_t resident_cpu_mpidr = 0;
327 	int rc;
328 
329 	/*
330 	 * Return value of this depends upon what
331 	 * psci_spd_migrate_info() returns.
332 	 */
333 	rc = psci_spd_migrate_info(&resident_cpu_mpidr);
334 	if ((rc != PSCI_TOS_NOT_UP_MIG_CAP) && (rc != PSCI_TOS_UP_MIG_CAP)) {
335 		return (u_register_t)(register_t) PSCI_E_INVALID_PARAMS;
336 	}
337 
338 	return resident_cpu_mpidr;
339 }
340 
341 int psci_node_hw_state(u_register_t target_cpu,
342 		       unsigned int power_level)
343 {
344 	int rc;
345 
346 	/* Validate target_cpu */
347 	if (!is_valid_mpidr(target_cpu)) {
348 		return PSCI_E_INVALID_PARAMS;
349 	}
350 
351 	/* Validate power_level against PLAT_MAX_PWR_LVL */
352 	if (power_level > PLAT_MAX_PWR_LVL) {
353 		return PSCI_E_INVALID_PARAMS;
354 	}
355 
356 	/*
357 	 * Dispatch this call to platform to query power controller, and pass on
358 	 * to the caller what it returns
359 	 */
360 	assert(psci_plat_pm_ops->get_node_hw_state != NULL);
361 	rc = psci_plat_pm_ops->get_node_hw_state(target_cpu, power_level);
362 	assert(((rc >= HW_ON) && (rc <= HW_STANDBY))
363 		|| (rc == PSCI_E_NOT_SUPPORTED)
364 		|| (rc == PSCI_E_INVALID_PARAMS));
365 	return rc;
366 }
367 
368 int psci_features(unsigned int psci_fid)
369 {
370 	unsigned int local_caps = psci_caps;
371 
372 	if (psci_fid == SMCCC_VERSION) {
373 		return PSCI_E_SUCCESS;
374 	}
375 	/* Check if it is a 64 bit function */
376 	if (((psci_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_64) {
377 		local_caps &= PSCI_CAP_64BIT_MASK;
378 	}
379 	/* Check for invalid fid */
380 	if (!(is_std_svc_call(psci_fid) && is_valid_fast_smc(psci_fid)
381 			&& is_psci_fid(psci_fid))) {
382 		return PSCI_E_NOT_SUPPORTED;
383 	}
384 
385 	/* Check if the psci fid is supported or not */
386 	if ((local_caps & define_psci_cap(psci_fid)) == 0U) {
387 		return PSCI_E_NOT_SUPPORTED;
388 	}
389 	/* Format the feature flags */
390 	if ((psci_fid == PSCI_CPU_SUSPEND_AARCH32) ||
391 	    (psci_fid == PSCI_CPU_SUSPEND_AARCH64)) {
392 		unsigned int ret = ((FF_PSTATE << FF_PSTATE_SHIFT) |
393 			(FF_SUPPORTS_OS_INIT_MODE << FF_MODE_SUPPORT_SHIFT));
394 		return (int)ret;
395 	}
396 
397 	/* Return 0 for all other fid's */
398 	return PSCI_E_SUCCESS;
399 }
400 
401 #if PSCI_OS_INIT_MODE
402 int psci_set_suspend_mode(unsigned int mode)
403 {
404 	suspend_mode_t new_mode;
405 	unsigned int this_core = plat_my_core_pos();
406 
407 	if ((mode != (unsigned int)PLAT_COORD) &&
408 	    (mode != (unsigned int)OS_INIT)) {
409 		return PSCI_E_INVALID_PARAMS;
410 	}
411 
412 	new_mode = (suspend_mode_t)mode;
413 
414 	if (psci_suspend_mode == new_mode) {
415 		return PSCI_E_SUCCESS;
416 	}
417 
418 	if (new_mode == PLAT_COORD) {
419 		/* Check if the current CPU is the last ON CPU in the system */
420 		if (!psci_is_last_on_cpu_safe(this_core)) {
421 			return PSCI_E_DENIED;
422 		}
423 	}
424 
425 	if (new_mode == OS_INIT) {
426 		/*
427 		 * Check if all CPUs in the system are ON or if the current
428 		 * CPU is the last ON CPU in the system.
429 		 */
430 		if (!(psci_are_all_cpus_on_safe(this_core) ||
431 		      psci_is_last_on_cpu_safe(this_core))) {
432 			return PSCI_E_DENIED;
433 		}
434 	}
435 
436 	psci_suspend_mode = new_mode;
437 	psci_flush_dcache_range((uintptr_t)&psci_suspend_mode,
438 				sizeof(psci_suspend_mode));
439 
440 	return PSCI_E_SUCCESS;
441 }
442 #endif
443 
444 /*******************************************************************************
445  * PSCI top level handler for servicing SMCs.
446  ******************************************************************************/
447 u_register_t psci_smc_handler(uint32_t smc_fid,
448 			  u_register_t x1,
449 			  u_register_t x2,
450 			  u_register_t x3,
451 			  u_register_t x4,
452 			  void *cookie,
453 			  void *handle,
454 			  u_register_t flags)
455 {
456 	(void)x4;
457 	(void)cookie;
458 	(void)handle;
459 	u_register_t ret;
460 
461 	if (!is_caller_non_secure(flags)) {
462 		return (u_register_t)SMC_UNK;
463 	}
464 
465 	/* Check the fid against the capabilities */
466 	if ((psci_caps & define_psci_cap(smc_fid)) == 0U) {
467 		return (u_register_t)SMC_UNK;
468 	}
469 
470 	if (((smc_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_32) {
471 		/* 32-bit PSCI function, clear top parameter bits */
472 
473 		uint32_t r1 = (uint32_t)x1;
474 		uint32_t r2 = (uint32_t)x2;
475 		uint32_t r3 = (uint32_t)x3;
476 
477 		switch (smc_fid) {
478 		case PSCI_VERSION:
479 			ret = (u_register_t)psci_version();
480 			break;
481 
482 		case PSCI_CPU_OFF:
483 			ret = (u_register_t)psci_cpu_off();
484 			break;
485 
486 		case PSCI_CPU_SUSPEND_AARCH32:
487 			ret = (u_register_t)psci_cpu_suspend(r1, r2, r3);
488 			break;
489 
490 		case PSCI_CPU_ON_AARCH32:
491 			ret = (u_register_t)psci_cpu_on(r1, r2, r3);
492 			break;
493 
494 		case PSCI_AFFINITY_INFO_AARCH32:
495 			ret = (u_register_t)psci_affinity_info(r1, r2);
496 			break;
497 
498 		case PSCI_MIG_AARCH32:
499 			ret = (u_register_t)psci_migrate(r1);
500 			break;
501 
502 		case PSCI_MIG_INFO_TYPE:
503 			ret = (u_register_t)psci_migrate_info_type();
504 			break;
505 
506 		case PSCI_MIG_INFO_UP_CPU_AARCH32:
507 			ret = psci_migrate_info_up_cpu();
508 			break;
509 
510 		case PSCI_NODE_HW_STATE_AARCH32:
511 			ret = (u_register_t)psci_node_hw_state(r1, r2);
512 			break;
513 
514 		case PSCI_SYSTEM_SUSPEND_AARCH32:
515 			ret = (u_register_t)psci_system_suspend(r1, r2);
516 			break;
517 
518 		case PSCI_SYSTEM_OFF:
519 			psci_system_off();
520 			/* We should never return from psci_system_off() */
521 			break;
522 
523 		case PSCI_SYSTEM_RESET:
524 			psci_system_reset();
525 			/* We should never return from psci_system_reset() */
526 			break;
527 
528 		case PSCI_FEATURES:
529 			ret = (u_register_t)psci_features(r1);
530 			break;
531 
532 #if PSCI_OS_INIT_MODE
533 		case PSCI_SET_SUSPEND_MODE:
534 			ret = (u_register_t)psci_set_suspend_mode(r1);
535 			break;
536 #endif
537 
538 #if ENABLE_PSCI_STAT
539 		case PSCI_STAT_RESIDENCY_AARCH32:
540 			ret = psci_stat_residency(r1, r2);
541 			break;
542 
543 		case PSCI_STAT_COUNT_AARCH32:
544 			ret = psci_stat_count(r1, r2);
545 			break;
546 #endif
547 		case PSCI_MEM_PROTECT:
548 			ret = psci_mem_protect(r1);
549 			break;
550 
551 		case PSCI_MEM_CHK_RANGE_AARCH32:
552 			ret = psci_mem_chk_range(r1, r2);
553 			break;
554 
555 		case PSCI_SYSTEM_RESET2_AARCH32:
556 			/* We should never return from psci_system_reset2() */
557 			ret = psci_system_reset2(r1, r2);
558 			break;
559 
560 		default:
561 			WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid);
562 			ret = (u_register_t)SMC_UNK;
563 			break;
564 		}
565 	} else {
566 		/* 64-bit PSCI function */
567 
568 		switch (smc_fid) {
569 		case PSCI_CPU_SUSPEND_AARCH64:
570 			ret = (u_register_t)
571 				psci_cpu_suspend((unsigned int)x1, x2, x3);
572 			break;
573 
574 		case PSCI_CPU_ON_AARCH64:
575 			ret = (u_register_t)psci_cpu_on(x1, x2, x3);
576 			break;
577 
578 		case PSCI_AFFINITY_INFO_AARCH64:
579 			ret = (u_register_t)
580 				psci_affinity_info(x1, (unsigned int)x2);
581 			break;
582 
583 		case PSCI_MIG_AARCH64:
584 			ret = (u_register_t)psci_migrate(x1);
585 			break;
586 
587 		case PSCI_MIG_INFO_UP_CPU_AARCH64:
588 			ret = psci_migrate_info_up_cpu();
589 			break;
590 
591 		case PSCI_NODE_HW_STATE_AARCH64:
592 			ret = (u_register_t)psci_node_hw_state(
593 					x1, (unsigned int) x2);
594 			break;
595 
596 		case PSCI_SYSTEM_SUSPEND_AARCH64:
597 			ret = (u_register_t)psci_system_suspend(x1, x2);
598 			break;
599 
600 #if ENABLE_PSCI_STAT
601 		case PSCI_STAT_RESIDENCY_AARCH64:
602 			ret = psci_stat_residency(x1, (unsigned int) x2);
603 			break;
604 
605 		case PSCI_STAT_COUNT_AARCH64:
606 			ret = psci_stat_count(x1, (unsigned int) x2);
607 			break;
608 #endif
609 
610 		case PSCI_MEM_CHK_RANGE_AARCH64:
611 			ret = psci_mem_chk_range(x1, x2);
612 			break;
613 
614 		case PSCI_SYSTEM_RESET2_AARCH64:
615 			/* We should never return from psci_system_reset2() */
616 			ret = psci_system_reset2((uint32_t) x1, x2);
617 			break;
618 
619 		default:
620 			WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid);
621 			ret = (u_register_t)SMC_UNK;
622 			break;
623 		}
624 	}
625 
626 	return ret;
627 }
628