1 /*
2  * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 
9 #include <common/debug.h>
10 #include <common/runtime_svc.h>
11 #include <drivers/console.h>
12 #include <lib/mmio.h>
13 #include <tools_share/uuid.h>
14 
15 #include <mtk_plat_common.h>
16 #include <mtk_sip_svc.h>
17 #include <plat_sip_calls.h>
18 
19 /* Mediatek SiP Service UUID */
20 DEFINE_SVC_UUID2(mtk_sip_svc_uid,
21 	0xa42b58f7, 0x6242, 0x7d4d, 0x80, 0xe5,
22 	0x8f, 0x95, 0x05, 0x00, 0x0f, 0x3d);
23 
24 #pragma weak mediatek_plat_sip_handler
mediatek_plat_sip_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)25 uintptr_t mediatek_plat_sip_handler(uint32_t smc_fid,
26 				u_register_t x1,
27 				u_register_t x2,
28 				u_register_t x3,
29 				u_register_t x4,
30 				void *cookie,
31 				void *handle,
32 				u_register_t flags)
33 {
34 	ERROR("%s: unhandled SMC (0x%x)\n", __func__, smc_fid);
35 	SMC_RET1(handle, SMC_UNK);
36 }
37 
38 /*
39  * This function handles Mediatek defined SiP Calls */
mediatek_sip_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)40 uintptr_t mediatek_sip_handler(uint32_t smc_fid,
41 			u_register_t x1,
42 			u_register_t x2,
43 			u_register_t x3,
44 			u_register_t x4,
45 			void *cookie,
46 			void *handle,
47 			u_register_t flags)
48 {
49 	uint32_t ns;
50 
51 	/* if parameter is sent from SMC32. Clean top 32 bits */
52 	clean_top_32b_of_param(smc_fid, &x1, &x2, &x3, &x4);
53 
54 	/* Determine which security state this SMC originated from */
55 	ns = is_caller_non_secure(flags);
56 	if (!ns) {
57 		/* SiP SMC service secure world's call */
58 		;
59 	} else {
60 		/* SiP SMC service normal world's call */
61 		switch (smc_fid) {
62 #if MTK_SIP_SET_AUTHORIZED_SECURE_REG_ENABLE
63 		case MTK_SIP_SET_AUTHORIZED_SECURE_REG: {
64 			/* only use ret here */
65 			uint64_t ret;
66 
67 			ret = mt_sip_set_authorized_sreg((uint32_t)x1,
68 				(uint32_t)x2);
69 			SMC_RET1(handle, ret);
70 		}
71 #endif
72 #if MTK_SIP_KERNEL_BOOT_ENABLE
73 		case MTK_SIP_KERNEL_BOOT_AARCH32:
74 			boot_to_kernel(x1, x2, x3, x4);
75 			SMC_RET0(handle);
76 #endif
77 		default:
78 			/* Do nothing in default case */
79 			break;
80 		}
81 	}
82 
83 	return mediatek_plat_sip_handler(smc_fid, x1, x2, x3, x4,
84 					cookie, handle, flags);
85 
86 }
87 
88 /*
89  * This function is responsible for handling all SiP calls from the NS world
90  */
sip_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)91 uintptr_t sip_smc_handler(uint32_t smc_fid,
92 			 u_register_t x1,
93 			 u_register_t x2,
94 			 u_register_t x3,
95 			 u_register_t x4,
96 			 void *cookie,
97 			 void *handle,
98 			 u_register_t flags)
99 {
100 	switch (smc_fid) {
101 	case SIP_SVC_CALL_COUNT:
102 		/* Return the number of Mediatek SiP Service Calls. */
103 		SMC_RET1(handle,
104 			 MTK_COMMON_SIP_NUM_CALLS + MTK_PLAT_SIP_NUM_CALLS);
105 
106 	case SIP_SVC_UID:
107 		/* Return UID to the caller */
108 		SMC_UUID_RET(handle, mtk_sip_svc_uid);
109 
110 	case SIP_SVC_VERSION:
111 		/* Return the version of current implementation */
112 		SMC_RET2(handle, MTK_SIP_SVC_VERSION_MAJOR,
113 			MTK_SIP_SVC_VERSION_MINOR);
114 
115 	default:
116 		return mediatek_sip_handler(smc_fid, x1, x2, x3, x4,
117 			cookie, handle, flags);
118 	}
119 }
120 
121 /* Define a runtime service descriptor for fast SMC calls */
122 DECLARE_RT_SVC(
123 	mediatek_sip_svc,
124 	OEN_SIP_START,
125 	OEN_SIP_END,
126 	SMC_TYPE_FAST,
127 	NULL,
128 	sip_smc_handler
129 );
130