xref: /rk3399_ARM-atf/plat/mediatek/common/mtk_sip_svc.c (revision 9a207532f8216bf83fed0891fed9ed0bc72ca450)
17d116dccSCC Ma /*
27d116dccSCC Ma  * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
37d116dccSCC Ma  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
57d116dccSCC Ma  */
6*09d40e0eSAntonio Nino Diaz 
77d116dccSCC Ma #include <assert.h>
8*09d40e0eSAntonio Nino Diaz 
9*09d40e0eSAntonio Nino Diaz #include <common/debug.h>
10*09d40e0eSAntonio Nino Diaz #include <common/runtime_svc.h>
11*09d40e0eSAntonio Nino Diaz #include <drivers/console.h>
12*09d40e0eSAntonio Nino Diaz #include <lib/mmio.h>
13*09d40e0eSAntonio Nino Diaz #include <tools_share/uuid.h>
14*09d40e0eSAntonio Nino Diaz 
15cf906b2aSLeon Chen #include <mtk_plat_common.h>
167d116dccSCC Ma #include <mtk_sip_svc.h>
17b659b1a7SJimmy Huang #include <plat_sip_calls.h>
187d116dccSCC Ma 
197d116dccSCC Ma /* Mediatek SiP Service UUID */
2003364865SRoberto Vargas DEFINE_SVC_UUID2(mtk_sip_svc_uid,
2103364865SRoberto Vargas 	0xa42b58f7, 0x6242, 0x7d4d, 0x80, 0xe5,
227d116dccSCC Ma 	0x8f, 0x95, 0x05, 0x00, 0x0f, 0x3d);
237d116dccSCC Ma 
24cf906b2aSLeon Chen #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)2557d1e5faSMasahiro Yamada uintptr_t mediatek_plat_sip_handler(uint32_t smc_fid,
2657d1e5faSMasahiro Yamada 				u_register_t x1,
2757d1e5faSMasahiro Yamada 				u_register_t x2,
2857d1e5faSMasahiro Yamada 				u_register_t x3,
2957d1e5faSMasahiro Yamada 				u_register_t x4,
307d116dccSCC Ma 				void *cookie,
31cf906b2aSLeon Chen 				void *handle,
3257d1e5faSMasahiro Yamada 				u_register_t flags)
337d116dccSCC Ma {
347d116dccSCC Ma 	ERROR("%s: unhandled SMC (0x%x)\n", __func__, smc_fid);
35cf906b2aSLeon Chen 	SMC_RET1(handle, SMC_UNK);
367d116dccSCC Ma }
377d116dccSCC Ma 
38cf906b2aSLeon Chen /*
39cf906b2aSLeon Chen  * 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)4057d1e5faSMasahiro Yamada uintptr_t mediatek_sip_handler(uint32_t smc_fid,
4157d1e5faSMasahiro Yamada 			u_register_t x1,
4257d1e5faSMasahiro Yamada 			u_register_t x2,
4357d1e5faSMasahiro Yamada 			u_register_t x3,
4457d1e5faSMasahiro Yamada 			u_register_t x4,
45cf906b2aSLeon Chen 			void *cookie,
46cf906b2aSLeon Chen 			void *handle,
4757d1e5faSMasahiro Yamada 			u_register_t flags)
48cf906b2aSLeon Chen {
49cf906b2aSLeon Chen 	uint32_t ns;
50cf906b2aSLeon Chen 
51cf906b2aSLeon Chen 	/* if parameter is sent from SMC32. Clean top 32 bits */
52cf906b2aSLeon Chen 	clean_top_32b_of_param(smc_fid, &x1, &x2, &x3, &x4);
53cf906b2aSLeon Chen 
54cf906b2aSLeon Chen 	/* Determine which security state this SMC originated from */
55cf906b2aSLeon Chen 	ns = is_caller_non_secure(flags);
56cf906b2aSLeon Chen 	if (!ns) {
57cf906b2aSLeon Chen 		/* SiP SMC service secure world's call */
58cf906b2aSLeon Chen 		;
59cf906b2aSLeon Chen 	} else {
60cf906b2aSLeon Chen 		/* SiP SMC service normal world's call */
61cf906b2aSLeon Chen 		switch (smc_fid) {
62cf906b2aSLeon Chen #if MTK_SIP_SET_AUTHORIZED_SECURE_REG_ENABLE
63cf906b2aSLeon Chen 		case MTK_SIP_SET_AUTHORIZED_SECURE_REG: {
64cf906b2aSLeon Chen 			/* only use ret here */
65cf906b2aSLeon Chen 			uint64_t ret;
66cf906b2aSLeon Chen 
67cf906b2aSLeon Chen 			ret = mt_sip_set_authorized_sreg((uint32_t)x1,
68cf906b2aSLeon Chen 				(uint32_t)x2);
69cf906b2aSLeon Chen 			SMC_RET1(handle, ret);
70cf906b2aSLeon Chen 		}
71cf906b2aSLeon Chen #endif
72cf906b2aSLeon Chen #if MTK_SIP_KERNEL_BOOT_ENABLE
73cf906b2aSLeon Chen 		case MTK_SIP_KERNEL_BOOT_AARCH32:
74cf906b2aSLeon Chen 			boot_to_kernel(x1, x2, x3, x4);
75cf906b2aSLeon Chen 			SMC_RET0(handle);
76cf906b2aSLeon Chen #endif
77649c48f5SJonathan Wright 		default:
78649c48f5SJonathan Wright 			/* Do nothing in default case */
79649c48f5SJonathan Wright 			break;
80cf906b2aSLeon Chen 		}
81cf906b2aSLeon Chen 	}
82cf906b2aSLeon Chen 
83cf906b2aSLeon Chen 	return mediatek_plat_sip_handler(smc_fid, x1, x2, x3, x4,
84cf906b2aSLeon Chen 					cookie, handle, flags);
85cf906b2aSLeon Chen 
867d116dccSCC Ma }
877d116dccSCC Ma 
887d116dccSCC Ma /*
897d116dccSCC Ma  * This function is responsible for handling all SiP calls from the NS world
907d116dccSCC Ma  */
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)9157d1e5faSMasahiro Yamada uintptr_t sip_smc_handler(uint32_t smc_fid,
9257d1e5faSMasahiro Yamada 			 u_register_t x1,
9357d1e5faSMasahiro Yamada 			 u_register_t x2,
9457d1e5faSMasahiro Yamada 			 u_register_t x3,
9557d1e5faSMasahiro Yamada 			 u_register_t x4,
967d116dccSCC Ma 			 void *cookie,
977d116dccSCC Ma 			 void *handle,
9857d1e5faSMasahiro Yamada 			 u_register_t flags)
997d116dccSCC Ma {
1007d116dccSCC Ma 	switch (smc_fid) {
1017d116dccSCC Ma 	case SIP_SVC_CALL_COUNT:
1027d116dccSCC Ma 		/* Return the number of Mediatek SiP Service Calls. */
103b659b1a7SJimmy Huang 		SMC_RET1(handle,
104b659b1a7SJimmy Huang 			 MTK_COMMON_SIP_NUM_CALLS + MTK_PLAT_SIP_NUM_CALLS);
1057d116dccSCC Ma 
1067d116dccSCC Ma 	case SIP_SVC_UID:
1077d116dccSCC Ma 		/* Return UID to the caller */
1087d116dccSCC Ma 		SMC_UUID_RET(handle, mtk_sip_svc_uid);
1097d116dccSCC Ma 
1107d116dccSCC Ma 	case SIP_SVC_VERSION:
1117d116dccSCC Ma 		/* Return the version of current implementation */
1127d116dccSCC Ma 		SMC_RET2(handle, MTK_SIP_SVC_VERSION_MAJOR,
1137d116dccSCC Ma 			MTK_SIP_SVC_VERSION_MINOR);
1147d116dccSCC Ma 
1157d116dccSCC Ma 	default:
1167d116dccSCC Ma 		return mediatek_sip_handler(smc_fid, x1, x2, x3, x4,
117cf906b2aSLeon Chen 			cookie, handle, flags);
1187d116dccSCC Ma 	}
1197d116dccSCC Ma }
1207d116dccSCC Ma 
1217d116dccSCC Ma /* Define a runtime service descriptor for fast SMC calls */
1227d116dccSCC Ma DECLARE_RT_SVC(
1237d116dccSCC Ma 	mediatek_sip_svc,
1247d116dccSCC Ma 	OEN_SIP_START,
1257d116dccSCC Ma 	OEN_SIP_END,
1267d116dccSCC Ma 	SMC_TYPE_FAST,
1277d116dccSCC Ma 	NULL,
1287d116dccSCC Ma 	sip_smc_handler
1297d116dccSCC Ma );
130