xref: /rk3399_ARM-atf/plat/mediatek/common/mtk_sip_svc.c (revision cf906b2a2e017077d54bbe9111a099cd0cc59433)
17d116dccSCC Ma /*
27d116dccSCC Ma  * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
37d116dccSCC Ma  *
47d116dccSCC Ma  * Redistribution and use in source and binary forms, with or without
57d116dccSCC Ma  * modification, are permitted provided that the following conditions are met:
67d116dccSCC Ma  *
77d116dccSCC Ma  * Redistributions of source code must retain the above copyright notice, this
87d116dccSCC Ma  * list of conditions and the following disclaimer.
97d116dccSCC Ma  *
107d116dccSCC Ma  * Redistributions in binary form must reproduce the above copyright notice,
117d116dccSCC Ma  * this list of conditions and the following disclaimer in the documentation
127d116dccSCC Ma  * and/or other materials provided with the distribution.
137d116dccSCC Ma  *
147d116dccSCC Ma  * Neither the name of ARM nor the names of its contributors may be used
157d116dccSCC Ma  * to endorse or promote products derived from this software without specific
167d116dccSCC Ma  * prior written permission.
177d116dccSCC Ma  *
187d116dccSCC Ma  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
197d116dccSCC Ma  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
207d116dccSCC Ma  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
217d116dccSCC Ma  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
227d116dccSCC Ma  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
237d116dccSCC Ma  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
247d116dccSCC Ma  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
257d116dccSCC Ma  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
267d116dccSCC Ma  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
277d116dccSCC Ma  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
287d116dccSCC Ma  * POSSIBILITY OF SUCH DAMAGE.
297d116dccSCC Ma  */
307d116dccSCC Ma #include <assert.h>
31*cf906b2aSLeon Chen #include <console.h>
327d116dccSCC Ma #include <debug.h>
33*cf906b2aSLeon Chen #include <mmio.h>
34*cf906b2aSLeon Chen #include <mtk_plat_common.h>
357d116dccSCC Ma #include <mtk_sip_svc.h>
367d116dccSCC Ma #include <runtime_svc.h>
377d116dccSCC Ma #include <uuid.h>
387d116dccSCC Ma 
397d116dccSCC Ma /* Mediatek SiP Service UUID */
407d116dccSCC Ma DEFINE_SVC_UUID(mtk_sip_svc_uid,
417d116dccSCC Ma 		0xf7582ba4, 0x4262, 0x4d7d, 0x80, 0xe5,
427d116dccSCC Ma 		0x8f, 0x95, 0x05, 0x00, 0x0f, 0x3d);
437d116dccSCC Ma 
44*cf906b2aSLeon Chen #pragma weak mediatek_plat_sip_handler
45*cf906b2aSLeon Chen uint64_t mediatek_plat_sip_handler(uint32_t smc_fid,
467d116dccSCC Ma 				uint64_t x1,
477d116dccSCC Ma 				uint64_t x2,
487d116dccSCC Ma 				uint64_t x3,
497d116dccSCC Ma 				uint64_t x4,
507d116dccSCC Ma 				void *cookie,
51*cf906b2aSLeon Chen 				void *handle,
52*cf906b2aSLeon Chen 				uint64_t flags)
537d116dccSCC Ma {
547d116dccSCC Ma 	ERROR("%s: unhandled SMC (0x%x)\n", __func__, smc_fid);
55*cf906b2aSLeon Chen 	SMC_RET1(handle, SMC_UNK);
567d116dccSCC Ma }
577d116dccSCC Ma 
58*cf906b2aSLeon Chen /*
59*cf906b2aSLeon Chen  * This function handles Mediatek defined SiP Calls */
60*cf906b2aSLeon Chen uint64_t mediatek_sip_handler(uint32_t smc_fid,
61*cf906b2aSLeon Chen 			uint64_t x1,
62*cf906b2aSLeon Chen 			uint64_t x2,
63*cf906b2aSLeon Chen 			uint64_t x3,
64*cf906b2aSLeon Chen 			uint64_t x4,
65*cf906b2aSLeon Chen 			void *cookie,
66*cf906b2aSLeon Chen 			void *handle,
67*cf906b2aSLeon Chen 			uint64_t flags)
68*cf906b2aSLeon Chen {
69*cf906b2aSLeon Chen 	uint32_t ns;
70*cf906b2aSLeon Chen 
71*cf906b2aSLeon Chen 	/* if parameter is sent from SMC32. Clean top 32 bits */
72*cf906b2aSLeon Chen 	clean_top_32b_of_param(smc_fid, &x1, &x2, &x3, &x4);
73*cf906b2aSLeon Chen 
74*cf906b2aSLeon Chen 	/* Determine which security state this SMC originated from */
75*cf906b2aSLeon Chen 	ns = is_caller_non_secure(flags);
76*cf906b2aSLeon Chen 	if (!ns) {
77*cf906b2aSLeon Chen 		/* SiP SMC service secure world's call */
78*cf906b2aSLeon Chen 		;
79*cf906b2aSLeon Chen 	} else {
80*cf906b2aSLeon Chen 		/* SiP SMC service normal world's call */
81*cf906b2aSLeon Chen 		switch (smc_fid) {
82*cf906b2aSLeon Chen #if MTK_SIP_SET_AUTHORIZED_SECURE_REG_ENABLE
83*cf906b2aSLeon Chen 		case MTK_SIP_SET_AUTHORIZED_SECURE_REG: {
84*cf906b2aSLeon Chen 			/* only use ret here */
85*cf906b2aSLeon Chen 			uint64_t ret;
86*cf906b2aSLeon Chen 
87*cf906b2aSLeon Chen 			ret = mt_sip_set_authorized_sreg((uint32_t)x1,
88*cf906b2aSLeon Chen 				(uint32_t)x2);
89*cf906b2aSLeon Chen 			SMC_RET1(handle, ret);
90*cf906b2aSLeon Chen 		}
91*cf906b2aSLeon Chen #endif
92*cf906b2aSLeon Chen #if MTK_SIP_KERNEL_BOOT_ENABLE
93*cf906b2aSLeon Chen 		case MTK_SIP_KERNEL_BOOT_AARCH32:
94*cf906b2aSLeon Chen 			boot_to_kernel(x1, x2, x3, x4);
95*cf906b2aSLeon Chen 			SMC_RET0(handle);
96*cf906b2aSLeon Chen #endif
97*cf906b2aSLeon Chen 		}
98*cf906b2aSLeon Chen 	}
99*cf906b2aSLeon Chen 
100*cf906b2aSLeon Chen 	return mediatek_plat_sip_handler(smc_fid, x1, x2, x3, x4,
101*cf906b2aSLeon Chen 					cookie, handle, flags);
102*cf906b2aSLeon Chen 
1037d116dccSCC Ma }
1047d116dccSCC Ma 
1057d116dccSCC Ma /*
1067d116dccSCC Ma  * This function is responsible for handling all SiP calls from the NS world
1077d116dccSCC Ma  */
1087d116dccSCC Ma uint64_t sip_smc_handler(uint32_t smc_fid,
1097d116dccSCC Ma 			 uint64_t x1,
1107d116dccSCC Ma 			 uint64_t x2,
1117d116dccSCC Ma 			 uint64_t x3,
1127d116dccSCC Ma 			 uint64_t x4,
1137d116dccSCC Ma 			 void *cookie,
1147d116dccSCC Ma 			 void *handle,
1157d116dccSCC Ma 			 uint64_t flags)
1167d116dccSCC Ma {
1177d116dccSCC Ma 	switch (smc_fid) {
1187d116dccSCC Ma 	case SIP_SVC_CALL_COUNT:
1197d116dccSCC Ma 		/* Return the number of Mediatek SiP Service Calls. */
1207d116dccSCC Ma 		SMC_RET1(handle, MTK_SIP_NUM_CALLS);
1217d116dccSCC Ma 
1227d116dccSCC Ma 	case SIP_SVC_UID:
1237d116dccSCC Ma 		/* Return UID to the caller */
1247d116dccSCC Ma 		SMC_UUID_RET(handle, mtk_sip_svc_uid);
1257d116dccSCC Ma 
1267d116dccSCC Ma 	case SIP_SVC_VERSION:
1277d116dccSCC Ma 		/* Return the version of current implementation */
1287d116dccSCC Ma 		SMC_RET2(handle, MTK_SIP_SVC_VERSION_MAJOR,
1297d116dccSCC Ma 			MTK_SIP_SVC_VERSION_MINOR);
1307d116dccSCC Ma 
1317d116dccSCC Ma 	default:
1327d116dccSCC Ma 		return mediatek_sip_handler(smc_fid, x1, x2, x3, x4,
133*cf906b2aSLeon Chen 			cookie, handle, flags);
1347d116dccSCC Ma 	}
1357d116dccSCC Ma }
1367d116dccSCC Ma 
1377d116dccSCC Ma /* Define a runtime service descriptor for fast SMC calls */
1387d116dccSCC Ma DECLARE_RT_SVC(
1397d116dccSCC Ma 	mediatek_sip_svc,
1407d116dccSCC Ma 	OEN_SIP_START,
1417d116dccSCC Ma 	OEN_SIP_END,
1427d116dccSCC Ma 	SMC_TYPE_FAST,
1437d116dccSCC Ma 	NULL,
1447d116dccSCC Ma 	sip_smc_handler
1457d116dccSCC Ma );
146