1 /*
2  * Copyright (c) 2014-2021, STMicroelectronics - All Rights Reserved
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <stdbool.h>
8 #include <stdint.h>
9 
10 #include <common/debug.h>
11 #include <common/runtime_svc.h>
12 #include <drivers/scmi-msg.h>
13 #include <lib/psci/psci.h>
14 #include <tools_share/uuid.h>
15 
16 #include <stm32mp1_smc.h>
17 
18 #include "bsec_svc.h"
19 
20 /* STM32 SiP Service UUID */
21 DEFINE_SVC_UUID2(stm32_sip_svc_uid,
22 		 0xa778aa50, 0xf49b, 0x144a, 0x8a, 0x5e,
23 		 0x26, 0x4d, 0x59, 0x94, 0xc2, 0x14);
24 
25 /* Setup STM32MP1 Standard Services */
stm32mp1_svc_setup(void)26 static int32_t stm32mp1_svc_setup(void)
27 {
28 	/*
29 	 * PSCI is the only specification implemented as a Standard Service.
30 	 * Invoke PSCI setup from here.
31 	 */
32 	return 0;
33 }
34 
35 /*
36  * Top-level Standard Service SMC handler. This handler will in turn dispatch
37  * calls to PSCI SMC handler.
38  */
stm32mp1_svc_smc_handler(uint32_t smc_fid,u_register_t x1,u_register_t x2,u_register_t x3,u_register_t x4,void * cookie,void * handle,u_register_t flags)39 static uintptr_t stm32mp1_svc_smc_handler(uint32_t smc_fid, u_register_t x1,
40 					  u_register_t x2, u_register_t x3,
41 					  u_register_t x4, void *cookie,
42 					  void *handle, u_register_t flags)
43 {
44 	uint32_t ret1 = 0U, ret2 = 0U;
45 	bool ret_uid = false, ret2_enabled = false;
46 
47 	switch (smc_fid) {
48 	case STM32_SIP_SVC_CALL_COUNT:
49 		ret1 = STM32_COMMON_SIP_NUM_CALLS;
50 		break;
51 
52 	case STM32_SIP_SVC_UID:
53 		/* Return UUID to the caller */
54 		ret_uid = true;
55 		break;
56 
57 	case STM32_SIP_SVC_VERSION:
58 		/* Return the version of current implementation */
59 		ret1 = STM32_SIP_SVC_VERSION_MAJOR;
60 		ret2 = STM32_SIP_SVC_VERSION_MINOR;
61 		ret2_enabled = true;
62 		break;
63 
64 	case STM32_SMC_BSEC:
65 		ret1 = bsec_main(x1, x2, x3, &ret2);
66 		ret2_enabled = true;
67 		break;
68 
69 	case STM32_SIP_SMC_SCMI_AGENT0:
70 		scmi_smt_fastcall_smc_entry(0);
71 		break;
72 	case STM32_SIP_SMC_SCMI_AGENT1:
73 		scmi_smt_fastcall_smc_entry(1);
74 		break;
75 
76 	default:
77 		WARN("Unimplemented STM32MP1 Service Call: 0x%x\n", smc_fid);
78 		ret1 = STM32_SMC_NOT_SUPPORTED;
79 		break;
80 	}
81 
82 	if (ret_uid) {
83 		SMC_UUID_RET(handle, stm32_sip_svc_uid);
84 	}
85 
86 	if (ret2_enabled) {
87 		SMC_RET2(handle, ret1, ret2);
88 	}
89 
90 	SMC_RET1(handle, ret1);
91 }
92 
93 /* Register Standard Service Calls as runtime service */
94 DECLARE_RT_SVC(stm32mp1_sip_svc,
95 	       OEN_SIP_START,
96 	       OEN_SIP_END,
97 	       SMC_TYPE_FAST,
98 	       stm32mp1_svc_setup,
99 	       stm32mp1_svc_smc_handler
100 );
101