xref: /rk3399_ARM-atf/plat/mediatek/common/mtk_sip_svc.c (revision 7d116dccab2249a692181ba9521a52277e86591c)
1*7d116dccSCC Ma /*
2*7d116dccSCC Ma  * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
3*7d116dccSCC Ma  *
4*7d116dccSCC Ma  * Redistribution and use in source and binary forms, with or without
5*7d116dccSCC Ma  * modification, are permitted provided that the following conditions are met:
6*7d116dccSCC Ma  *
7*7d116dccSCC Ma  * Redistributions of source code must retain the above copyright notice, this
8*7d116dccSCC Ma  * list of conditions and the following disclaimer.
9*7d116dccSCC Ma  *
10*7d116dccSCC Ma  * Redistributions in binary form must reproduce the above copyright notice,
11*7d116dccSCC Ma  * this list of conditions and the following disclaimer in the documentation
12*7d116dccSCC Ma  * and/or other materials provided with the distribution.
13*7d116dccSCC Ma  *
14*7d116dccSCC Ma  * Neither the name of ARM nor the names of its contributors may be used
15*7d116dccSCC Ma  * to endorse or promote products derived from this software without specific
16*7d116dccSCC Ma  * prior written permission.
17*7d116dccSCC Ma  *
18*7d116dccSCC Ma  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19*7d116dccSCC Ma  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*7d116dccSCC Ma  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*7d116dccSCC Ma  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22*7d116dccSCC Ma  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*7d116dccSCC Ma  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*7d116dccSCC Ma  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*7d116dccSCC Ma  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*7d116dccSCC Ma  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*7d116dccSCC Ma  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*7d116dccSCC Ma  * POSSIBILITY OF SUCH DAMAGE.
29*7d116dccSCC Ma  */
30*7d116dccSCC Ma #include <assert.h>
31*7d116dccSCC Ma #include <debug.h>
32*7d116dccSCC Ma #include <mtk_sip_svc.h>
33*7d116dccSCC Ma #include <runtime_svc.h>
34*7d116dccSCC Ma #include <uuid.h>
35*7d116dccSCC Ma 
36*7d116dccSCC Ma /* Mediatek SiP Service UUID */
37*7d116dccSCC Ma DEFINE_SVC_UUID(mtk_sip_svc_uid,
38*7d116dccSCC Ma 		0xf7582ba4, 0x4262, 0x4d7d, 0x80, 0xe5,
39*7d116dccSCC Ma 		0x8f, 0x95, 0x05, 0x00, 0x0f, 0x3d);
40*7d116dccSCC Ma 
41*7d116dccSCC Ma /*
42*7d116dccSCC Ma  * This function handles Mediatek defined SiP Calls */
43*7d116dccSCC Ma static uint64_t mediatek_sip_handler(uint32_t smc_fid,
44*7d116dccSCC Ma 				     uint64_t x1,
45*7d116dccSCC Ma 				     uint64_t x2,
46*7d116dccSCC Ma 				     uint64_t x3,
47*7d116dccSCC Ma 				     uint64_t x4,
48*7d116dccSCC Ma 				     void *cookie,
49*7d116dccSCC Ma 				     void *handle)
50*7d116dccSCC Ma {
51*7d116dccSCC Ma 	uint64_t ret;
52*7d116dccSCC Ma 
53*7d116dccSCC Ma 	switch (smc_fid) {
54*7d116dccSCC Ma 	case MTK_SIP_SET_AUTHORIZED_SECURE_REG:
55*7d116dccSCC Ma 		ret = mt_sip_set_authorized_sreg((uint32_t)x1, (uint32_t)x2);
56*7d116dccSCC Ma 		SMC_RET1(handle, ret);
57*7d116dccSCC Ma 
58*7d116dccSCC Ma 	default:
59*7d116dccSCC Ma 		ERROR("%s: unhandled SMC (0x%x)\n", __func__, smc_fid);
60*7d116dccSCC Ma 		break;
61*7d116dccSCC Ma 	}
62*7d116dccSCC Ma 
63*7d116dccSCC Ma 	SMC_RET1(handle, SMC_UNK);
64*7d116dccSCC Ma }
65*7d116dccSCC Ma 
66*7d116dccSCC Ma /*
67*7d116dccSCC Ma  * This function is responsible for handling all SiP calls from the NS world
68*7d116dccSCC Ma  */
69*7d116dccSCC Ma uint64_t sip_smc_handler(uint32_t smc_fid,
70*7d116dccSCC Ma 			 uint64_t x1,
71*7d116dccSCC Ma 			 uint64_t x2,
72*7d116dccSCC Ma 			 uint64_t x3,
73*7d116dccSCC Ma 			 uint64_t x4,
74*7d116dccSCC Ma 			 void *cookie,
75*7d116dccSCC Ma 			 void *handle,
76*7d116dccSCC Ma 			 uint64_t flags)
77*7d116dccSCC Ma {
78*7d116dccSCC Ma 	uint32_t ns;
79*7d116dccSCC Ma 
80*7d116dccSCC Ma 	/* Determine which security state this SMC originated from */
81*7d116dccSCC Ma 	ns = is_caller_non_secure(flags);
82*7d116dccSCC Ma 	if (!ns)
83*7d116dccSCC Ma 		SMC_RET1(handle, SMC_UNK);
84*7d116dccSCC Ma 
85*7d116dccSCC Ma 	switch (smc_fid) {
86*7d116dccSCC Ma 	case SIP_SVC_CALL_COUNT:
87*7d116dccSCC Ma 		/* Return the number of Mediatek SiP Service Calls. */
88*7d116dccSCC Ma 		SMC_RET1(handle, MTK_SIP_NUM_CALLS);
89*7d116dccSCC Ma 
90*7d116dccSCC Ma 	case SIP_SVC_UID:
91*7d116dccSCC Ma 		/* Return UID to the caller */
92*7d116dccSCC Ma 		SMC_UUID_RET(handle, mtk_sip_svc_uid);
93*7d116dccSCC Ma 
94*7d116dccSCC Ma 	case SIP_SVC_VERSION:
95*7d116dccSCC Ma 		/* Return the version of current implementation */
96*7d116dccSCC Ma 		SMC_RET2(handle, MTK_SIP_SVC_VERSION_MAJOR,
97*7d116dccSCC Ma 			MTK_SIP_SVC_VERSION_MINOR);
98*7d116dccSCC Ma 
99*7d116dccSCC Ma 	default:
100*7d116dccSCC Ma 		return mediatek_sip_handler(smc_fid, x1, x2, x3, x4,
101*7d116dccSCC Ma 			cookie, handle);
102*7d116dccSCC Ma 	}
103*7d116dccSCC Ma }
104*7d116dccSCC Ma 
105*7d116dccSCC Ma /* Define a runtime service descriptor for fast SMC calls */
106*7d116dccSCC Ma DECLARE_RT_SVC(
107*7d116dccSCC Ma 	mediatek_sip_svc,
108*7d116dccSCC Ma 	OEN_SIP_START,
109*7d116dccSCC Ma 	OEN_SIP_END,
110*7d116dccSCC Ma 	SMC_TYPE_FAST,
111*7d116dccSCC Ma 	NULL,
112*7d116dccSCC Ma 	sip_smc_handler
113*7d116dccSCC Ma );
114