xref: /rk3399_ARM-atf/services/std_svc/std_svc_setup.c (revision 033648652f2d66abe2454a75ded891a47cb13446)
164f6ea9bSJeenu Viswambharan /*
27fabe1a8SRoberto Vargas  * Copyright (c) 2014-2018, ARM Limited and Contributors. All rights reserved.
364f6ea9bSJeenu Viswambharan  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
564f6ea9bSJeenu Viswambharan  */
664f6ea9bSJeenu Viswambharan 
758e946aeSSoby Mathew #include <assert.h>
8872be88aSdp-arm #include <cpu_data.h>
997043ac9SDan Handley #include <debug.h>
10872be88aSdp-arm #include <pmf.h>
1197043ac9SDan Handley #include <psci.h>
12872be88aSdp-arm #include <runtime_instr.h>
1364f6ea9bSJeenu Viswambharan #include <runtime_svc.h>
14b7cb133eSJeenu Viswambharan #include <sdei.h>
15085e80ecSAntonio Nino Diaz #include <smccc_helpers.h>
162fccb228SAntonio Nino Diaz #include <spm_svc.h>
1764f6ea9bSJeenu Viswambharan #include <std_svc.h>
1897043ac9SDan Handley #include <stdint.h>
1997043ac9SDan Handley #include <uuid.h>
2064f6ea9bSJeenu Viswambharan 
2164f6ea9bSJeenu Viswambharan /* Standard Service UUID */
22*03364865SRoberto Vargas static uuid_t arm_svc_uid = {
23*03364865SRoberto Vargas 	{0x5b, 0x90, 0x8d, 0x10},
24*03364865SRoberto Vargas 	{0x63, 0xf8},
25*03364865SRoberto Vargas 	{0xe8, 0x47},
26*03364865SRoberto Vargas 	0xae, 0x2d,
27*03364865SRoberto Vargas 	{0xc0, 0xfb, 0x56, 0x41, 0xf6, 0xe2}
28*03364865SRoberto Vargas };
2964f6ea9bSJeenu Viswambharan 
3058e946aeSSoby Mathew /* Setup Standard Services */
3158e946aeSSoby Mathew static int32_t std_svc_setup(void)
3258e946aeSSoby Mathew {
3358e946aeSSoby Mathew 	uintptr_t svc_arg;
342fccb228SAntonio Nino Diaz 	int ret = 0;
3558e946aeSSoby Mathew 
3658e946aeSSoby Mathew 	svc_arg = get_arm_std_svc_args(PSCI_FID_MASK);
3758e946aeSSoby Mathew 	assert(svc_arg);
3858e946aeSSoby Mathew 
3958e946aeSSoby Mathew 	/*
402fccb228SAntonio Nino Diaz 	 * PSCI is one of the specifications implemented as a Standard Service.
4158e946aeSSoby Mathew 	 * The `psci_setup()` also does EL3 architectural setup.
4258e946aeSSoby Mathew 	 */
432fccb228SAntonio Nino Diaz 	if (psci_setup((const psci_lib_args_t *)svc_arg) != PSCI_E_SUCCESS) {
442fccb228SAntonio Nino Diaz 		ret = 1;
452fccb228SAntonio Nino Diaz 	}
462fccb228SAntonio Nino Diaz 
472fccb228SAntonio Nino Diaz #if ENABLE_SPM
482fccb228SAntonio Nino Diaz 	if (spm_setup() != 0) {
492fccb228SAntonio Nino Diaz 		ret = 1;
502fccb228SAntonio Nino Diaz 	}
512fccb228SAntonio Nino Diaz #endif
522fccb228SAntonio Nino Diaz 
53b7cb133eSJeenu Viswambharan #if SDEI_SUPPORT
54b7cb133eSJeenu Viswambharan 	/* SDEI initialisation */
55b7cb133eSJeenu Viswambharan 	sdei_init();
56b7cb133eSJeenu Viswambharan #endif
57b7cb133eSJeenu Viswambharan 
582fccb228SAntonio Nino Diaz 	return ret;
5958e946aeSSoby Mathew }
6058e946aeSSoby Mathew 
6164f6ea9bSJeenu Viswambharan /*
6264f6ea9bSJeenu Viswambharan  * Top-level Standard Service SMC handler. This handler will in turn dispatch
6364f6ea9bSJeenu Viswambharan  * calls to PSCI SMC handler
6464f6ea9bSJeenu Viswambharan  */
657fabe1a8SRoberto Vargas static uintptr_t std_svc_smc_handler(uint32_t smc_fid,
664c0d0390SSoby Mathew 			     u_register_t x1,
674c0d0390SSoby Mathew 			     u_register_t x2,
684c0d0390SSoby Mathew 			     u_register_t x3,
694c0d0390SSoby Mathew 			     u_register_t x4,
7064f6ea9bSJeenu Viswambharan 			     void *cookie,
7164f6ea9bSJeenu Viswambharan 			     void *handle,
724c0d0390SSoby Mathew 			     u_register_t flags)
7364f6ea9bSJeenu Viswambharan {
7464f6ea9bSJeenu Viswambharan 	/*
7564f6ea9bSJeenu Viswambharan 	 * Dispatch PSCI calls to PSCI SMC handler and return its return
7664f6ea9bSJeenu Viswambharan 	 * value
7764f6ea9bSJeenu Viswambharan 	 */
7864f6ea9bSJeenu Viswambharan 	if (is_psci_fid(smc_fid)) {
79872be88aSdp-arm 		uint64_t ret;
80872be88aSdp-arm 
81872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION
82bfef6106Sdp-arm 
83bfef6106Sdp-arm 		/*
84bfef6106Sdp-arm 		 * Flush cache line so that even if CPU power down happens
85bfef6106Sdp-arm 		 * the timestamp update is reflected in memory.
86bfef6106Sdp-arm 		 */
87872be88aSdp-arm 		PMF_WRITE_TIMESTAMP(rt_instr_svc,
88872be88aSdp-arm 		    RT_INSTR_ENTER_PSCI,
89bfef6106Sdp-arm 		    PMF_CACHE_MAINT,
90872be88aSdp-arm 		    get_cpu_data(cpu_data_pmf_ts[CPU_DATA_PMF_TS0_IDX]));
91872be88aSdp-arm #endif
92872be88aSdp-arm 
93872be88aSdp-arm 		ret = psci_smc_handler(smc_fid, x1, x2, x3, x4,
94872be88aSdp-arm 		    cookie, handle, flags);
95872be88aSdp-arm 
96872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION
97872be88aSdp-arm 		PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
98872be88aSdp-arm 		    RT_INSTR_EXIT_PSCI,
99872be88aSdp-arm 		    PMF_NO_CACHE_MAINT);
100872be88aSdp-arm #endif
101872be88aSdp-arm 
102872be88aSdp-arm 		SMC_RET1(handle, ret);
10364f6ea9bSJeenu Viswambharan 	}
10464f6ea9bSJeenu Viswambharan 
1052fccb228SAntonio Nino Diaz #if ENABLE_SPM
1062fccb228SAntonio Nino Diaz 	/*
1072fccb228SAntonio Nino Diaz 	 * Dispatch SPM calls to SPM SMC handler and return its return
1082fccb228SAntonio Nino Diaz 	 * value
1092fccb228SAntonio Nino Diaz 	 */
1102fccb228SAntonio Nino Diaz 	if (is_spm_fid(smc_fid)) {
1112fccb228SAntonio Nino Diaz 		return spm_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
1122fccb228SAntonio Nino Diaz 				       handle, flags);
1132fccb228SAntonio Nino Diaz 	}
1142fccb228SAntonio Nino Diaz #endif
1152fccb228SAntonio Nino Diaz 
116b7cb133eSJeenu Viswambharan #if SDEI_SUPPORT
117b7cb133eSJeenu Viswambharan 	if (is_sdei_fid(smc_fid)) {
118b7cb133eSJeenu Viswambharan 		return sdei_smc_handler(smc_fid, x1, x2, x3, x4, cookie, handle,
119b7cb133eSJeenu Viswambharan 				flags);
120b7cb133eSJeenu Viswambharan 	}
121b7cb133eSJeenu Viswambharan #endif
122b7cb133eSJeenu Viswambharan 
12364f6ea9bSJeenu Viswambharan 	switch (smc_fid) {
12464f6ea9bSJeenu Viswambharan 	case ARM_STD_SVC_CALL_COUNT:
12564f6ea9bSJeenu Viswambharan 		/*
12664f6ea9bSJeenu Viswambharan 		 * Return the number of Standard Service Calls. PSCI is the only
12764f6ea9bSJeenu Viswambharan 		 * standard service implemented; so return number of PSCI calls
12864f6ea9bSJeenu Viswambharan 		 */
12964f6ea9bSJeenu Viswambharan 		SMC_RET1(handle, PSCI_NUM_CALLS);
13064f6ea9bSJeenu Viswambharan 
13164f6ea9bSJeenu Viswambharan 	case ARM_STD_SVC_UID:
13264f6ea9bSJeenu Viswambharan 		/* Return UID to the caller */
13364f6ea9bSJeenu Viswambharan 		SMC_UUID_RET(handle, arm_svc_uid);
13464f6ea9bSJeenu Viswambharan 
13564f6ea9bSJeenu Viswambharan 	case ARM_STD_SVC_VERSION:
13664f6ea9bSJeenu Viswambharan 		/* Return the version of current implementation */
13764f6ea9bSJeenu Viswambharan 		SMC_RET2(handle, STD_SVC_VERSION_MAJOR, STD_SVC_VERSION_MINOR);
13864f6ea9bSJeenu Viswambharan 
13964f6ea9bSJeenu Viswambharan 	default:
14064f6ea9bSJeenu Viswambharan 		WARN("Unimplemented Standard Service Call: 0x%x \n", smc_fid);
14164f6ea9bSJeenu Viswambharan 		SMC_RET1(handle, SMC_UNK);
14264f6ea9bSJeenu Viswambharan 	}
14364f6ea9bSJeenu Viswambharan }
14464f6ea9bSJeenu Viswambharan 
14564f6ea9bSJeenu Viswambharan /* Register Standard Service Calls as runtime service */
14664f6ea9bSJeenu Viswambharan DECLARE_RT_SVC(
14764f6ea9bSJeenu Viswambharan 		std_svc,
14864f6ea9bSJeenu Viswambharan 
14964f6ea9bSJeenu Viswambharan 		OEN_STD_START,
15064f6ea9bSJeenu Viswambharan 		OEN_STD_END,
15164f6ea9bSJeenu Viswambharan 		SMC_TYPE_FAST,
15258e946aeSSoby Mathew 		std_svc_setup,
15364f6ea9bSJeenu Viswambharan 		std_svc_smc_handler
15464f6ea9bSJeenu Viswambharan );
155