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