xref: /rk3399_ARM-atf/plat/arm/common/arm_sip_svc.c (revision b10d44995eb652675863c2cc6a7726683613da0d)
1 /*
2  * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of ARM nor the names of its contributors may be used
15  * to endorse or promote products derived from this software without specific
16  * prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <arm_sip_svc.h>
32 #include <debug.h>
33 #include <plat_arm.h>
34 #include <pmf.h>
35 #include <runtime_svc.h>
36 #include <stdint.h>
37 #include <uuid.h>
38 
39 
40 /* ARM SiP Service UUID */
41 DEFINE_SVC_UUID(arm_sip_svc_uid,
42 		0xe2756d55, 0x3360, 0x4bb5, 0xbf, 0xf3,
43 		0x62, 0x79, 0xfd, 0x11, 0x37, 0xff);
44 
45 static int arm_sip_setup(void)
46 {
47 	if (pmf_setup() != 0)
48 		return 1;
49 	return 0;
50 }
51 
52 /*
53  * This function handles ARM defined SiP Calls
54  */
55 static uintptr_t arm_sip_handler(unsigned int smc_fid,
56 			u_register_t x1,
57 			u_register_t x2,
58 			u_register_t x3,
59 			u_register_t x4,
60 			void *cookie,
61 			void *handle,
62 			u_register_t flags)
63 {
64 	int call_count = 0;
65 
66 	/*
67 	 * Dispatch PMF calls to PMF SMC handler and return its return
68 	 * value
69 	 */
70 	if (is_pmf_fid(smc_fid)) {
71 		return pmf_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
72 				handle, flags);
73 	}
74 
75 	switch (smc_fid) {
76 	case ARM_SIP_SVC_EXE_STATE_SWITCH: {
77 		u_register_t pc;
78 
79 		/* Allow calls from non-secure only */
80 		if (!is_caller_non_secure(flags))
81 			SMC_RET1(handle, STATE_SW_E_DENIED);
82 
83 		/* Validate supplied entry point */
84 		pc = (u_register_t) ((x1 << 32) | (uint32_t) x2);
85 		if (arm_validate_ns_entrypoint(pc))
86 			SMC_RET1(handle, STATE_SW_E_PARAM);
87 
88 		/*
89 		 * Pointers used in execution state switch are all 32 bits wide
90 		 */
91 		return arm_execution_state_switch(smc_fid, (uint32_t) x1,
92 				(uint32_t) x2, (uint32_t) x3, (uint32_t) x4,
93 				handle);
94 		}
95 
96 	case ARM_SIP_SVC_CALL_COUNT:
97 		/* PMF calls */
98 		call_count += PMF_NUM_SMC_CALLS;
99 
100 		/* State switch call */
101 		call_count += 1;
102 
103 		SMC_RET1(handle, call_count);
104 
105 	case ARM_SIP_SVC_UID:
106 		/* Return UID to the caller */
107 		SMC_UUID_RET(handle, arm_sip_svc_uid);
108 
109 	case ARM_SIP_SVC_VERSION:
110 		/* Return the version of current implementation */
111 		SMC_RET2(handle, ARM_SIP_SVC_VERSION_MAJOR, ARM_SIP_SVC_VERSION_MINOR);
112 
113 	default:
114 		WARN("Unimplemented ARM SiP Service Call: 0x%x \n", smc_fid);
115 		SMC_RET1(handle, SMC_UNK);
116 	}
117 
118 }
119 
120 
121 /* Define a runtime service descriptor for fast SMC calls */
122 DECLARE_RT_SVC(
123 	arm_sip_svc,
124 	OEN_SIP_START,
125 	OEN_SIP_END,
126 	SMC_TYPE_FAST,
127 	arm_sip_setup,
128 	arm_sip_handler
129 );
130