xref: /rk3399_ARM-atf/plat/mediatek/common/mtk_sip_svc.c (revision b659b1a755a4a73479137ca4b37f55ccba1dbde1)
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>
31cf906b2aSLeon Chen #include <console.h>
327d116dccSCC Ma #include <debug.h>
33cf906b2aSLeon Chen #include <mmio.h>
34cf906b2aSLeon Chen #include <mtk_plat_common.h>
357d116dccSCC Ma #include <mtk_sip_svc.h>
36*b659b1a7SJimmy Huang #include <plat_sip_calls.h>
377d116dccSCC Ma #include <runtime_svc.h>
387d116dccSCC Ma #include <uuid.h>
397d116dccSCC Ma 
407d116dccSCC Ma /* Mediatek SiP Service UUID */
417d116dccSCC Ma DEFINE_SVC_UUID(mtk_sip_svc_uid,
427d116dccSCC Ma 		0xf7582ba4, 0x4262, 0x4d7d, 0x80, 0xe5,
437d116dccSCC Ma 		0x8f, 0x95, 0x05, 0x00, 0x0f, 0x3d);
447d116dccSCC Ma 
45cf906b2aSLeon Chen #pragma weak mediatek_plat_sip_handler
46cf906b2aSLeon Chen uint64_t mediatek_plat_sip_handler(uint32_t smc_fid,
477d116dccSCC Ma 				uint64_t x1,
487d116dccSCC Ma 				uint64_t x2,
497d116dccSCC Ma 				uint64_t x3,
507d116dccSCC Ma 				uint64_t x4,
517d116dccSCC Ma 				void *cookie,
52cf906b2aSLeon Chen 				void *handle,
53cf906b2aSLeon Chen 				uint64_t flags)
547d116dccSCC Ma {
557d116dccSCC Ma 	ERROR("%s: unhandled SMC (0x%x)\n", __func__, smc_fid);
56cf906b2aSLeon Chen 	SMC_RET1(handle, SMC_UNK);
577d116dccSCC Ma }
587d116dccSCC Ma 
59cf906b2aSLeon Chen /*
60cf906b2aSLeon Chen  * This function handles Mediatek defined SiP Calls */
61cf906b2aSLeon Chen uint64_t mediatek_sip_handler(uint32_t smc_fid,
62cf906b2aSLeon Chen 			uint64_t x1,
63cf906b2aSLeon Chen 			uint64_t x2,
64cf906b2aSLeon Chen 			uint64_t x3,
65cf906b2aSLeon Chen 			uint64_t x4,
66cf906b2aSLeon Chen 			void *cookie,
67cf906b2aSLeon Chen 			void *handle,
68cf906b2aSLeon Chen 			uint64_t flags)
69cf906b2aSLeon Chen {
70cf906b2aSLeon Chen 	uint32_t ns;
71cf906b2aSLeon Chen 
72cf906b2aSLeon Chen 	/* if parameter is sent from SMC32. Clean top 32 bits */
73cf906b2aSLeon Chen 	clean_top_32b_of_param(smc_fid, &x1, &x2, &x3, &x4);
74cf906b2aSLeon Chen 
75cf906b2aSLeon Chen 	/* Determine which security state this SMC originated from */
76cf906b2aSLeon Chen 	ns = is_caller_non_secure(flags);
77cf906b2aSLeon Chen 	if (!ns) {
78cf906b2aSLeon Chen 		/* SiP SMC service secure world's call */
79cf906b2aSLeon Chen 		;
80cf906b2aSLeon Chen 	} else {
81cf906b2aSLeon Chen 		/* SiP SMC service normal world's call */
82cf906b2aSLeon Chen 		switch (smc_fid) {
83cf906b2aSLeon Chen #if MTK_SIP_SET_AUTHORIZED_SECURE_REG_ENABLE
84cf906b2aSLeon Chen 		case MTK_SIP_SET_AUTHORIZED_SECURE_REG: {
85cf906b2aSLeon Chen 			/* only use ret here */
86cf906b2aSLeon Chen 			uint64_t ret;
87cf906b2aSLeon Chen 
88cf906b2aSLeon Chen 			ret = mt_sip_set_authorized_sreg((uint32_t)x1,
89cf906b2aSLeon Chen 				(uint32_t)x2);
90cf906b2aSLeon Chen 			SMC_RET1(handle, ret);
91cf906b2aSLeon Chen 		}
92cf906b2aSLeon Chen #endif
93cf906b2aSLeon Chen #if MTK_SIP_KERNEL_BOOT_ENABLE
94cf906b2aSLeon Chen 		case MTK_SIP_KERNEL_BOOT_AARCH32:
95cf906b2aSLeon Chen 			boot_to_kernel(x1, x2, x3, x4);
96cf906b2aSLeon Chen 			SMC_RET0(handle);
97cf906b2aSLeon Chen #endif
98cf906b2aSLeon Chen 		}
99cf906b2aSLeon Chen 	}
100cf906b2aSLeon Chen 
101cf906b2aSLeon Chen 	return mediatek_plat_sip_handler(smc_fid, x1, x2, x3, x4,
102cf906b2aSLeon Chen 					cookie, handle, flags);
103cf906b2aSLeon Chen 
1047d116dccSCC Ma }
1057d116dccSCC Ma 
1067d116dccSCC Ma /*
1077d116dccSCC Ma  * This function is responsible for handling all SiP calls from the NS world
1087d116dccSCC Ma  */
1097d116dccSCC Ma uint64_t sip_smc_handler(uint32_t smc_fid,
1107d116dccSCC Ma 			 uint64_t x1,
1117d116dccSCC Ma 			 uint64_t x2,
1127d116dccSCC Ma 			 uint64_t x3,
1137d116dccSCC Ma 			 uint64_t x4,
1147d116dccSCC Ma 			 void *cookie,
1157d116dccSCC Ma 			 void *handle,
1167d116dccSCC Ma 			 uint64_t flags)
1177d116dccSCC Ma {
1187d116dccSCC Ma 	switch (smc_fid) {
1197d116dccSCC Ma 	case SIP_SVC_CALL_COUNT:
1207d116dccSCC Ma 		/* Return the number of Mediatek SiP Service Calls. */
121*b659b1a7SJimmy Huang 		SMC_RET1(handle,
122*b659b1a7SJimmy Huang 			 MTK_COMMON_SIP_NUM_CALLS + MTK_PLAT_SIP_NUM_CALLS);
1237d116dccSCC Ma 
1247d116dccSCC Ma 	case SIP_SVC_UID:
1257d116dccSCC Ma 		/* Return UID to the caller */
1267d116dccSCC Ma 		SMC_UUID_RET(handle, mtk_sip_svc_uid);
1277d116dccSCC Ma 
1287d116dccSCC Ma 	case SIP_SVC_VERSION:
1297d116dccSCC Ma 		/* Return the version of current implementation */
1307d116dccSCC Ma 		SMC_RET2(handle, MTK_SIP_SVC_VERSION_MAJOR,
1317d116dccSCC Ma 			MTK_SIP_SVC_VERSION_MINOR);
1327d116dccSCC Ma 
1337d116dccSCC Ma 	default:
1347d116dccSCC Ma 		return mediatek_sip_handler(smc_fid, x1, x2, x3, x4,
135cf906b2aSLeon Chen 			cookie, handle, flags);
1367d116dccSCC Ma 	}
1377d116dccSCC Ma }
1387d116dccSCC Ma 
1397d116dccSCC Ma /* Define a runtime service descriptor for fast SMC calls */
1407d116dccSCC Ma DECLARE_RT_SVC(
1417d116dccSCC Ma 	mediatek_sip_svc,
1427d116dccSCC Ma 	OEN_SIP_START,
1437d116dccSCC Ma 	OEN_SIP_END,
1447d116dccSCC Ma 	SMC_TYPE_FAST,
1457d116dccSCC Ma 	NULL,
1467d116dccSCC Ma 	sip_smc_handler
1477d116dccSCC Ma );
148