xref: /rk3399_ARM-atf/services/std_svc/std_svc_setup.c (revision 1a0f565b6240392ccb6982a15098ec288df7ea94)
164f6ea9bSJeenu Viswambharan /*
2ffea3844SSona Mathew  * Copyright (c) 2014-2023, 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>
897043ac9SDan Handley #include <stdint.h>
909d40e0eSAntonio Nino Diaz 
1009d40e0eSAntonio Nino Diaz #include <common/debug.h>
1109d40e0eSAntonio Nino Diaz #include <common/runtime_svc.h>
1209d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/cpu_data.h>
1309d40e0eSAntonio Nino Diaz #include <lib/pmf/pmf.h>
1409d40e0eSAntonio Nino Diaz #include <lib/psci/psci.h>
1509d40e0eSAntonio Nino Diaz #include <lib/runtime_instr.h>
16e62748e3SManish V Badarkhe #include <services/drtm_svc.h>
17ffea3844SSona Mathew #include <services/errata_abi_svc.h>
181cdf1eb8SJeremy Linton #include <services/pci_svc.h>
1977c27753SZelalem Aweke #include <services/rmmd_svc.h>
2009d40e0eSAntonio Nino Diaz #include <services/sdei.h>
210bf9f567SPaul Beesley #include <services/spm_mm_svc.h>
22bb01a673SMarc Bonnici #include <services/spmc_svc.h>
232a7b403dSAchin Gupta #include <services/spmd_svc.h>
2409d40e0eSAntonio Nino Diaz #include <services/std_svc.h>
257dfb9911SJimmy Brisson #include <services/trng_svc.h>
2609d40e0eSAntonio Nino Diaz #include <smccc_helpers.h>
2709d40e0eSAntonio Nino Diaz #include <tools_share/uuid.h>
2864f6ea9bSJeenu Viswambharan 
2964f6ea9bSJeenu Viswambharan /* Standard Service UUID */
3003364865SRoberto Vargas static uuid_t arm_svc_uid = {
3103364865SRoberto Vargas 	{0x5b, 0x90, 0x8d, 0x10},
3203364865SRoberto Vargas 	{0x63, 0xf8},
3303364865SRoberto Vargas 	{0xe8, 0x47},
3403364865SRoberto Vargas 	0xae, 0x2d,
3503364865SRoberto Vargas 	{0xc0, 0xfb, 0x56, 0x41, 0xf6, 0xe2}
3603364865SRoberto Vargas };
3764f6ea9bSJeenu Viswambharan 
3858e946aeSSoby Mathew /* Setup Standard Services */
3958e946aeSSoby Mathew static int32_t std_svc_setup(void)
4058e946aeSSoby Mathew {
4158e946aeSSoby Mathew 	uintptr_t svc_arg;
422fccb228SAntonio Nino Diaz 	int ret = 0;
4358e946aeSSoby Mathew 
4458e946aeSSoby Mathew 	svc_arg = get_arm_std_svc_args(PSCI_FID_MASK);
4558e946aeSSoby Mathew 	assert(svc_arg);
4658e946aeSSoby Mathew 
4758e946aeSSoby Mathew 	/*
482fccb228SAntonio Nino Diaz 	 * PSCI is one of the specifications implemented as a Standard Service.
4958e946aeSSoby Mathew 	 * The `psci_setup()` also does EL3 architectural setup.
5058e946aeSSoby Mathew 	 */
512fccb228SAntonio Nino Diaz 	if (psci_setup((const psci_lib_args_t *)svc_arg) != PSCI_E_SUCCESS) {
522fccb228SAntonio Nino Diaz 		ret = 1;
532fccb228SAntonio Nino Diaz 	}
542fccb228SAntonio Nino Diaz 
55538b0020SPaul Beesley #if SPM_MM
560bf9f567SPaul Beesley 	if (spm_mm_setup() != 0) {
572fccb228SAntonio Nino Diaz 		ret = 1;
582fccb228SAntonio Nino Diaz 	}
592fccb228SAntonio Nino Diaz #endif
602fccb228SAntonio Nino Diaz 
612a7b403dSAchin Gupta #if defined(SPD_spmd)
622a7b403dSAchin Gupta 	if (spmd_setup() != 0) {
632a7b403dSAchin Gupta 		ret = 1;
642a7b403dSAchin Gupta 	}
652a7b403dSAchin Gupta #endif
662a7b403dSAchin Gupta 
67b9fd2d3cSSubhasish Ghosh #if ENABLE_RME
68b9fd2d3cSSubhasish Ghosh 	if (rmmd_setup() != 0) {
69fdd8a24bSVarun Wadekar 		WARN("RMMD setup failed. Continuing boot.\n");
70b9fd2d3cSSubhasish Ghosh 	}
71b9fd2d3cSSubhasish Ghosh #endif
72b9fd2d3cSSubhasish Ghosh 
73b7cb133eSJeenu Viswambharan #if SDEI_SUPPORT
74b7cb133eSJeenu Viswambharan 	/* SDEI initialisation */
75b7cb133eSJeenu Viswambharan 	sdei_init();
76b7cb133eSJeenu Viswambharan #endif
77b7cb133eSJeenu Viswambharan 
780b22e591SJayanth Dodderi Chidanand #if TRNG_SUPPORT
790b22e591SJayanth Dodderi Chidanand 	/* TRNG initialisation */
807dfb9911SJimmy Brisson 	trng_setup();
810b22e591SJayanth Dodderi Chidanand #endif /* TRNG_SUPPORT */
827dfb9911SJimmy Brisson 
83e62748e3SManish V Badarkhe #if DRTM_SUPPORT
84e62748e3SManish V Badarkhe 	if (drtm_setup() != 0) {
85e62748e3SManish V Badarkhe 		ret = 1;
86e62748e3SManish V Badarkhe 	}
87e62748e3SManish V Badarkhe #endif /* DRTM_SUPPORT */
88e62748e3SManish V Badarkhe 
892fccb228SAntonio Nino Diaz 	return ret;
9058e946aeSSoby Mathew }
9158e946aeSSoby Mathew 
9264f6ea9bSJeenu Viswambharan /*
9364f6ea9bSJeenu Viswambharan  * Top-level Standard Service SMC handler. This handler will in turn dispatch
9464f6ea9bSJeenu Viswambharan  * calls to PSCI SMC handler
9564f6ea9bSJeenu Viswambharan  */
967fabe1a8SRoberto Vargas static uintptr_t std_svc_smc_handler(uint32_t smc_fid,
97*1a0f565bSMaheedhar Bollapalli 			     u_register_t x1_arg,
98*1a0f565bSMaheedhar Bollapalli 			     u_register_t x2_arg,
99*1a0f565bSMaheedhar Bollapalli 			     u_register_t x3_arg,
100*1a0f565bSMaheedhar Bollapalli 			     u_register_t x4_arg,
10164f6ea9bSJeenu Viswambharan 			     void *cookie,
10264f6ea9bSJeenu Viswambharan 			     void *handle,
1034c0d0390SSoby Mathew 			     u_register_t flags)
10464f6ea9bSJeenu Viswambharan {
105*1a0f565bSMaheedhar Bollapalli 	u_register_t x1 = x1_arg;
106*1a0f565bSMaheedhar Bollapalli 	u_register_t x2 = x2_arg;
107*1a0f565bSMaheedhar Bollapalli 	u_register_t x3 = x3_arg;
108*1a0f565bSMaheedhar Bollapalli 	u_register_t x4 = x4_arg;
109*1a0f565bSMaheedhar Bollapalli 
110475333c8SJeremy Linton 	if (((smc_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_32) {
111475333c8SJeremy Linton 		/* 32-bit SMC function, clear top parameter bits */
112475333c8SJeremy Linton 
113475333c8SJeremy Linton 		x1 &= UINT32_MAX;
114475333c8SJeremy Linton 		x2 &= UINT32_MAX;
115475333c8SJeremy Linton 		x3 &= UINT32_MAX;
116475333c8SJeremy Linton 		x4 &= UINT32_MAX;
117475333c8SJeremy Linton 	}
118475333c8SJeremy Linton 
11964f6ea9bSJeenu Viswambharan 	/*
12064f6ea9bSJeenu Viswambharan 	 * Dispatch PSCI calls to PSCI SMC handler and return its return
12164f6ea9bSJeenu Viswambharan 	 * value
12264f6ea9bSJeenu Viswambharan 	 */
12364f6ea9bSJeenu Viswambharan 	if (is_psci_fid(smc_fid)) {
124872be88aSdp-arm 		uint64_t ret;
125872be88aSdp-arm 
126872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION
127bfef6106Sdp-arm 
128bfef6106Sdp-arm 		/*
129bfef6106Sdp-arm 		 * Flush cache line so that even if CPU power down happens
130bfef6106Sdp-arm 		 * the timestamp update is reflected in memory.
131bfef6106Sdp-arm 		 */
132872be88aSdp-arm 		PMF_WRITE_TIMESTAMP(rt_instr_svc,
133872be88aSdp-arm 		    RT_INSTR_ENTER_PSCI,
134bfef6106Sdp-arm 		    PMF_CACHE_MAINT,
135872be88aSdp-arm 		    get_cpu_data(cpu_data_pmf_ts[CPU_DATA_PMF_TS0_IDX]));
136872be88aSdp-arm #endif
137872be88aSdp-arm 
138872be88aSdp-arm 		ret = psci_smc_handler(smc_fid, x1, x2, x3, x4,
139872be88aSdp-arm 		    cookie, handle, flags);
140872be88aSdp-arm 
141872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION
142872be88aSdp-arm 		PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
143872be88aSdp-arm 		    RT_INSTR_EXIT_PSCI,
144872be88aSdp-arm 		    PMF_NO_CACHE_MAINT);
145872be88aSdp-arm #endif
146872be88aSdp-arm 
147872be88aSdp-arm 		SMC_RET1(handle, ret);
14864f6ea9bSJeenu Viswambharan 	}
14964f6ea9bSJeenu Viswambharan 
1503f3c341aSPaul Beesley #if SPM_MM
1512fccb228SAntonio Nino Diaz 	/*
1522fccb228SAntonio Nino Diaz 	 * Dispatch SPM calls to SPM SMC handler and return its return
1532fccb228SAntonio Nino Diaz 	 * value
1542fccb228SAntonio Nino Diaz 	 */
1550bf9f567SPaul Beesley 	if (is_spm_mm_fid(smc_fid)) {
1560bf9f567SPaul Beesley 		return spm_mm_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
1572fccb228SAntonio Nino Diaz 					  handle, flags);
1582fccb228SAntonio Nino Diaz 	}
1592fccb228SAntonio Nino Diaz #endif
1602fccb228SAntonio Nino Diaz 
1612a7b403dSAchin Gupta #if defined(SPD_spmd)
1622a7b403dSAchin Gupta 	/*
163662af36dSJ-Alves 	 * Dispatch FFA calls to the FFA SMC handler implemented by the SPM
1642a7b403dSAchin Gupta 	 * dispatcher and return its return value
1652a7b403dSAchin Gupta 	 */
166662af36dSJ-Alves 	if (is_ffa_fid(smc_fid)) {
167bb01a673SMarc Bonnici 		return spmd_ffa_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
1682a7b403dSAchin Gupta 					    handle, flags);
1692a7b403dSAchin Gupta 	}
1702a7b403dSAchin Gupta #endif
1712a7b403dSAchin Gupta 
172b7cb133eSJeenu Viswambharan #if SDEI_SUPPORT
173b7cb133eSJeenu Viswambharan 	if (is_sdei_fid(smc_fid)) {
174b7cb133eSJeenu Viswambharan 		return sdei_smc_handler(smc_fid, x1, x2, x3, x4, cookie, handle,
175b7cb133eSJeenu Viswambharan 				flags);
176b7cb133eSJeenu Viswambharan 	}
177b7cb133eSJeenu Viswambharan #endif
178b7cb133eSJeenu Viswambharan 
179323b6c63SAndre Przywara #if TRNG_SUPPORT
1807dfb9911SJimmy Brisson 	if (is_trng_fid(smc_fid)) {
1817dfb9911SJimmy Brisson 		return trng_smc_handler(smc_fid, x1, x2, x3, x4, cookie, handle,
1827dfb9911SJimmy Brisson 				flags);
1837dfb9911SJimmy Brisson 	}
1840b22e591SJayanth Dodderi Chidanand #endif /* TRNG_SUPPORT */
1850b22e591SJayanth Dodderi Chidanand 
186ffea3844SSona Mathew #if ERRATA_ABI_SUPPORT
187ffea3844SSona Mathew 	if (is_errata_fid(smc_fid)) {
188ffea3844SSona Mathew 		return errata_abi_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
189ffea3844SSona Mathew 					      handle, flags);
190ffea3844SSona Mathew 	}
191ffea3844SSona Mathew #endif /* ERRATA_ABI_SUPPORT */
192ffea3844SSona Mathew 
19377c27753SZelalem Aweke #if ENABLE_RME
194319fb084SSoby Mathew 
195319fb084SSoby Mathew 	if (is_rmmd_el3_fid(smc_fid)) {
196319fb084SSoby Mathew 		return rmmd_rmm_el3_handler(smc_fid, x1, x2, x3, x4, cookie,
19777c27753SZelalem Aweke 					    handle, flags);
19877c27753SZelalem Aweke 	}
199b9fd2d3cSSubhasish Ghosh 
200b9fd2d3cSSubhasish Ghosh 	if (is_rmi_fid(smc_fid)) {
201b9fd2d3cSSubhasish Ghosh 		return rmmd_rmi_handler(smc_fid, x1, x2, x3, x4, cookie,
202b9fd2d3cSSubhasish Ghosh 					handle, flags);
203b9fd2d3cSSubhasish Ghosh 	}
20477c27753SZelalem Aweke #endif
2057dfb9911SJimmy Brisson 
2061cdf1eb8SJeremy Linton #if SMC_PCI_SUPPORT
2071cdf1eb8SJeremy Linton 	if (is_pci_fid(smc_fid)) {
2081cdf1eb8SJeremy Linton 		return pci_smc_handler(smc_fid, x1, x2, x3, x4, cookie, handle,
2091cdf1eb8SJeremy Linton 				       flags);
2101cdf1eb8SJeremy Linton 	}
2111cdf1eb8SJeremy Linton #endif
2121cdf1eb8SJeremy Linton 
213e62748e3SManish V Badarkhe #if DRTM_SUPPORT
214e62748e3SManish V Badarkhe 	if (is_drtm_fid(smc_fid)) {
215e62748e3SManish V Badarkhe 		return drtm_smc_handler(smc_fid, x1, x2, x3, x4, cookie, handle,
216e62748e3SManish V Badarkhe 					flags);
217e62748e3SManish V Badarkhe 	}
218e62748e3SManish V Badarkhe #endif /* DRTM_SUPPORT */
219e62748e3SManish V Badarkhe 
22064f6ea9bSJeenu Viswambharan 	switch (smc_fid) {
22164f6ea9bSJeenu Viswambharan 	case ARM_STD_SVC_CALL_COUNT:
22264f6ea9bSJeenu Viswambharan 		/*
22364f6ea9bSJeenu Viswambharan 		 * Return the number of Standard Service Calls. PSCI is the only
22464f6ea9bSJeenu Viswambharan 		 * standard service implemented; so return number of PSCI calls
22564f6ea9bSJeenu Viswambharan 		 */
22664f6ea9bSJeenu Viswambharan 		SMC_RET1(handle, PSCI_NUM_CALLS);
22764f6ea9bSJeenu Viswambharan 
22864f6ea9bSJeenu Viswambharan 	case ARM_STD_SVC_UID:
22964f6ea9bSJeenu Viswambharan 		/* Return UID to the caller */
23064f6ea9bSJeenu Viswambharan 		SMC_UUID_RET(handle, arm_svc_uid);
23164f6ea9bSJeenu Viswambharan 
23264f6ea9bSJeenu Viswambharan 	case ARM_STD_SVC_VERSION:
23364f6ea9bSJeenu Viswambharan 		/* Return the version of current implementation */
23464f6ea9bSJeenu Viswambharan 		SMC_RET2(handle, STD_SVC_VERSION_MAJOR, STD_SVC_VERSION_MINOR);
23564f6ea9bSJeenu Viswambharan 
23664f6ea9bSJeenu Viswambharan 	default:
23767fad514SAndre Przywara 		VERBOSE("Unimplemented Standard Service Call: 0x%x \n", smc_fid);
23864f6ea9bSJeenu Viswambharan 		SMC_RET1(handle, SMC_UNK);
23964f6ea9bSJeenu Viswambharan 	}
24064f6ea9bSJeenu Viswambharan }
24164f6ea9bSJeenu Viswambharan 
24264f6ea9bSJeenu Viswambharan /* Register Standard Service Calls as runtime service */
24364f6ea9bSJeenu Viswambharan DECLARE_RT_SVC(
24464f6ea9bSJeenu Viswambharan 		std_svc,
24564f6ea9bSJeenu Viswambharan 
24664f6ea9bSJeenu Viswambharan 		OEN_STD_START,
24764f6ea9bSJeenu Viswambharan 		OEN_STD_END,
24864f6ea9bSJeenu Viswambharan 		SMC_TYPE_FAST,
24958e946aeSSoby Mathew 		std_svc_setup,
25064f6ea9bSJeenu Viswambharan 		std_svc_smc_handler
25164f6ea9bSJeenu Viswambharan );
252