1 /*
2  * Copyright 2018-2021 NXP
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #include <assert.h>
9 #include <string.h>
10 
11 #include <caam.h>
12 #include <common/runtime_svc.h>
13 #include <dcfg.h>
14 #include <lib/mmio.h>
15 #include <tools_share/uuid.h>
16 
17 #include <plat_common.h>
18 #include <sipsvc.h>
19 
20 /* Layerscape SiP Service UUID */
21 DEFINE_SVC_UUID2(nxp_sip_svc_uid,
22 		 0x871de4ef, 0xedfc, 0x4209, 0xa4, 0x23,
23 		 0x8d, 0x23, 0x75, 0x9d, 0x3b, 0x9f);
24 
25 #pragma weak nxp_plat_sip_handler
nxp_plat_sip_handler(unsigned int 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)26 static uintptr_t nxp_plat_sip_handler(unsigned int smc_fid,
27 				      u_register_t x1,
28 				      u_register_t x2,
29 				      u_register_t x3,
30 				      u_register_t x4,
31 				      void *cookie,
32 				      void *handle,
33 				      u_register_t flags)
34 {
35 	ERROR("%s: unhandled SMC (0x%x)\n", __func__, smc_fid);
36 	SMC_RET1(handle, SMC_UNK);
37 }
38 
39 uint64_t el2_2_aarch32(u_register_t smc_id, u_register_t start_addr,
40 		       u_register_t parm1, u_register_t parm2);
41 
42 uint64_t prefetch_disable(u_register_t smc_id, u_register_t mask);
43 uint64_t bl31_get_porsr1(void);
44 
clean_top_32b_of_param(uint32_t smc_fid,u_register_t * px1,u_register_t * px2,u_register_t * px3,u_register_t * px4)45 static void clean_top_32b_of_param(uint32_t smc_fid,
46 				   u_register_t *px1,
47 				   u_register_t *px2,
48 				   u_register_t *px3,
49 				   u_register_t *px4)
50 {
51 	/* if parameters from SMC32. Clean top 32 bits */
52 	if (GET_SMC_CC(smc_fid) == SMC_32) {
53 		*px1 = *px1 & SMC32_PARAM_MASK;
54 		*px2 = *px2 & SMC32_PARAM_MASK;
55 		*px3 = *px3 & SMC32_PARAM_MASK;
56 		*px4 = *px4 & SMC32_PARAM_MASK;
57 	}
58 }
59 
60 /* This function handles Layerscape defined SiP Calls */
nxp_sip_handler(unsigned int 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)61 static uintptr_t nxp_sip_handler(unsigned int smc_fid,
62 				 u_register_t x1,
63 				 u_register_t x2,
64 				 u_register_t x3,
65 				 u_register_t x4,
66 				 void *cookie,
67 				 void *handle,
68 				 u_register_t flags)
69 {
70 	uint32_t ns;
71 	uint64_t ret;
72 	dram_regions_info_t *info_dram_regions;
73 
74 	/* if parameter is sent from SMC32. Clean top 32 bits */
75 	clean_top_32b_of_param(smc_fid, &x1, &x2, &x3, &x4);
76 
77 	/* Determine which security state this SMC originated from */
78 	ns = is_caller_non_secure(flags);
79 	if (ns == 0) {
80 		/* SiP SMC service secure world's call */
81 		;
82 	} else {
83 		/* SiP SMC service normal world's call */
84 		;
85 	}
86 
87 	switch (smc_fid & SMC_FUNC_MASK) {
88 	case SIP_SVC_RNG:
89 		if (is_sec_enabled() == false) {
90 			NOTICE("SEC is disabled.\n");
91 			SMC_RET1(handle, SMC_UNK);
92 		}
93 
94 		/* Return zero on failure */
95 		ret = get_random((int)x1);
96 		if (ret != 0) {
97 			SMC_RET2(handle, SMC_OK, ret);
98 		} else {
99 			SMC_RET1(handle, SMC_UNK);
100 		}
101 		/* break is not required as SMC_RETx return */
102 	case SIP_SVC_HUK:
103 		if (is_sec_enabled() == false) {
104 			NOTICE("SEC is disabled.\n");
105 			SMC_RET1(handle, SMC_UNK);
106 		}
107 		ret = get_hw_unq_key_blob_hw((uint8_t *) x1, (uint32_t) x2);
108 
109 		if (ret == SMC_OK) {
110 			SMC_RET1(handle, SMC_OK);
111 		} else {
112 			SMC_RET1(handle, SMC_UNK);
113 		}
114 		/* break is not required as SMC_RETx return */
115 	case SIP_SVC_MEM_BANK:
116 		VERBOSE("Handling SMC SIP_SVC_MEM_BANK.\n");
117 		info_dram_regions = get_dram_regions_info();
118 
119 		if (x1 == -1) {
120 			SMC_RET2(handle, SMC_OK,
121 					info_dram_regions->total_dram_size);
122 		} else if (x1 >= info_dram_regions->num_dram_regions) {
123 			SMC_RET1(handle, SMC_UNK);
124 		} else {
125 			SMC_RET3(handle, SMC_OK,
126 				info_dram_regions->region[x1].addr,
127 				info_dram_regions->region[x1].size);
128 		}
129 		/* break is not required as SMC_RETx return */
130 	case SIP_SVC_PREFETCH_DIS:
131 		VERBOSE("In SIP_SVC_PREFETCH_DIS call\n");
132 		ret = prefetch_disable(smc_fid, x1);
133 		if (ret == SMC_OK) {
134 			SMC_RET1(handle, SMC_OK);
135 		} else {
136 			SMC_RET1(handle, SMC_UNK);
137 		}
138 		/* break is not required as SMC_RETx return */
139 	case SIP_SVC_2_AARCH32:
140 		ret = el2_2_aarch32(smc_fid, x1, x2, x3);
141 
142 		/* In success case, control should not reach here. */
143 		NOTICE("SMC: SIP_SVC_2_AARCH32 Failed.\n");
144 		SMC_RET1(handle, SMC_UNK);
145 		/* break is not required as SMC_RETx return */
146 	case SIP_SVC_PORSR1:
147 		ret = bl31_get_porsr1();
148 		SMC_RET2(handle, SMC_OK, ret);
149 		/* break is not required as SMC_RETx return */
150 	default:
151 		return nxp_plat_sip_handler(smc_fid, x1, x2, x3, x4,
152 				cookie, handle, flags);
153 	}
154 }
155 
156 /* This function is responsible for handling all SiP calls */
sip_smc_handler(unsigned int 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)157 static uintptr_t sip_smc_handler(unsigned int smc_fid,
158 				 u_register_t x1,
159 				 u_register_t x2,
160 				 u_register_t x3,
161 				 u_register_t x4,
162 				 void *cookie,
163 				 void *handle,
164 				 u_register_t flags)
165 {
166 	switch (smc_fid & SMC_FUNC_MASK) {
167 	case SIP_SVC_CALL_COUNT:
168 		/* Return the number of Layerscape SiP Service Calls. */
169 		SMC_RET1(handle, LS_COMMON_SIP_NUM_CALLS);
170 		break;
171 	case SIP_SVC_UID:
172 		/* Return UID to the caller */
173 		SMC_UUID_RET(handle, nxp_sip_svc_uid);
174 		break;
175 	case SIP_SVC_VERSION:
176 		/* Return the version of current implementation */
177 		SMC_RET2(handle, LS_SIP_SVC_VERSION_MAJOR,
178 			 LS_SIP_SVC_VERSION_MINOR);
179 		break;
180 	default:
181 		return nxp_sip_handler(smc_fid, x1, x2, x3, x4,
182 				       cookie, handle, flags);
183 	}
184 }
185 
186 /* Define a runtime service descriptor for fast SMC calls */
187 DECLARE_RT_SVC(
188 	nxp_sip_svc,
189 	OEN_SIP_START,
190 	OEN_SIP_END,
191 	SMC_TYPE_FAST,
192 	NULL,
193 	sip_smc_handler
194 );
195