xref: /rk3399_ARM-atf/common/runtime_svc.c (revision 532ed6183868036e4a4f83cd7a71b93266a3bdb7)
1*532ed618SSoby Mathew /*
2*532ed618SSoby Mathew  * Copyright (c) 2013-2016, ARM Limited and Contributors. All rights reserved.
3*532ed618SSoby Mathew  *
4*532ed618SSoby Mathew  * Redistribution and use in source and binary forms, with or without
5*532ed618SSoby Mathew  * modification, are permitted provided that the following conditions are met:
6*532ed618SSoby Mathew  *
7*532ed618SSoby Mathew  * Redistributions of source code must retain the above copyright notice, this
8*532ed618SSoby Mathew  * list of conditions and the following disclaimer.
9*532ed618SSoby Mathew  *
10*532ed618SSoby Mathew  * Redistributions in binary form must reproduce the above copyright notice,
11*532ed618SSoby Mathew  * this list of conditions and the following disclaimer in the documentation
12*532ed618SSoby Mathew  * and/or other materials provided with the distribution.
13*532ed618SSoby Mathew  *
14*532ed618SSoby Mathew  * Neither the name of ARM nor the names of its contributors may be used
15*532ed618SSoby Mathew  * to endorse or promote products derived from this software without specific
16*532ed618SSoby Mathew  * prior written permission.
17*532ed618SSoby Mathew  *
18*532ed618SSoby Mathew  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19*532ed618SSoby Mathew  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*532ed618SSoby Mathew  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*532ed618SSoby Mathew  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22*532ed618SSoby Mathew  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*532ed618SSoby Mathew  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*532ed618SSoby Mathew  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*532ed618SSoby Mathew  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*532ed618SSoby Mathew  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*532ed618SSoby Mathew  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*532ed618SSoby Mathew  * POSSIBILITY OF SUCH DAMAGE.
29*532ed618SSoby Mathew  */
30*532ed618SSoby Mathew 
31*532ed618SSoby Mathew #include <assert.h>
32*532ed618SSoby Mathew #include <debug.h>
33*532ed618SSoby Mathew #include <errno.h>
34*532ed618SSoby Mathew #include <runtime_svc.h>
35*532ed618SSoby Mathew #include <string.h>
36*532ed618SSoby Mathew 
37*532ed618SSoby Mathew /*******************************************************************************
38*532ed618SSoby Mathew  * The 'rt_svc_descs' array holds the runtime service descriptors exported by
39*532ed618SSoby Mathew  * services by placing them in the 'rt_svc_descs' linker section.
40*532ed618SSoby Mathew  * The 'rt_svc_descs_indices' array holds the index of a descriptor in the
41*532ed618SSoby Mathew  * 'rt_svc_descs' array. When an SMC arrives, the OEN[29:24] bits and the call
42*532ed618SSoby Mathew  * type[31] bit in the function id are combined to get an index into the
43*532ed618SSoby Mathew  * 'rt_svc_descs_indices' array. This gives the index of the descriptor in the
44*532ed618SSoby Mathew  * 'rt_svc_descs' array which contains the SMC handler.
45*532ed618SSoby Mathew  ******************************************************************************/
46*532ed618SSoby Mathew #define RT_SVC_DESCS_START	((uintptr_t) (&__RT_SVC_DESCS_START__))
47*532ed618SSoby Mathew #define RT_SVC_DESCS_END	((uintptr_t) (&__RT_SVC_DESCS_END__))
48*532ed618SSoby Mathew uint8_t rt_svc_descs_indices[MAX_RT_SVCS];
49*532ed618SSoby Mathew static rt_svc_desc_t *rt_svc_descs;
50*532ed618SSoby Mathew 
51*532ed618SSoby Mathew #define RT_SVC_DECS_NUM		((RT_SVC_DESCS_END - RT_SVC_DESCS_START)\
52*532ed618SSoby Mathew 					/ sizeof(rt_svc_desc_t))
53*532ed618SSoby Mathew 
54*532ed618SSoby Mathew /*******************************************************************************
55*532ed618SSoby Mathew  * Simple routine to sanity check a runtime service descriptor before using it
56*532ed618SSoby Mathew  ******************************************************************************/
57*532ed618SSoby Mathew static int32_t validate_rt_svc_desc(rt_svc_desc_t *desc)
58*532ed618SSoby Mathew {
59*532ed618SSoby Mathew 	if (desc == NULL)
60*532ed618SSoby Mathew 		return -EINVAL;
61*532ed618SSoby Mathew 
62*532ed618SSoby Mathew 	if (desc->start_oen > desc->end_oen)
63*532ed618SSoby Mathew 		return -EINVAL;
64*532ed618SSoby Mathew 
65*532ed618SSoby Mathew 	if (desc->end_oen >= OEN_LIMIT)
66*532ed618SSoby Mathew 		return -EINVAL;
67*532ed618SSoby Mathew 
68*532ed618SSoby Mathew 	if (desc->call_type != SMC_TYPE_FAST && desc->call_type != SMC_TYPE_STD)
69*532ed618SSoby Mathew 		return -EINVAL;
70*532ed618SSoby Mathew 
71*532ed618SSoby Mathew 	/* A runtime service having no init or handle function doesn't make sense */
72*532ed618SSoby Mathew 	if (desc->init == NULL && desc->handle == NULL)
73*532ed618SSoby Mathew 		return -EINVAL;
74*532ed618SSoby Mathew 
75*532ed618SSoby Mathew 	return 0;
76*532ed618SSoby Mathew }
77*532ed618SSoby Mathew 
78*532ed618SSoby Mathew /*******************************************************************************
79*532ed618SSoby Mathew  * This function calls the initialisation routine in the descriptor exported by
80*532ed618SSoby Mathew  * a runtime service. Once a descriptor has been validated, its start & end
81*532ed618SSoby Mathew  * owning entity numbers and the call type are combined to form a unique oen.
82*532ed618SSoby Mathew  * The unique oen is used as an index into the 'rt_svc_descs_indices' array.
83*532ed618SSoby Mathew  * The index of the runtime service descriptor is stored at this index.
84*532ed618SSoby Mathew  ******************************************************************************/
85*532ed618SSoby Mathew void runtime_svc_init(void)
86*532ed618SSoby Mathew {
87*532ed618SSoby Mathew 	int rc = 0, index, start_idx, end_idx;
88*532ed618SSoby Mathew 
89*532ed618SSoby Mathew 	/* Assert the number of descriptors detected are less than maximum indices */
90*532ed618SSoby Mathew 	assert((RT_SVC_DECS_NUM >= 0) && (RT_SVC_DECS_NUM < MAX_RT_SVCS));
91*532ed618SSoby Mathew 
92*532ed618SSoby Mathew 	/* If no runtime services are implemented then simply bail out */
93*532ed618SSoby Mathew 	if (RT_SVC_DECS_NUM == 0)
94*532ed618SSoby Mathew 		return;
95*532ed618SSoby Mathew 
96*532ed618SSoby Mathew 	/* Initialise internal variables to invalid state */
97*532ed618SSoby Mathew 	memset(rt_svc_descs_indices, -1, sizeof(rt_svc_descs_indices));
98*532ed618SSoby Mathew 
99*532ed618SSoby Mathew 	rt_svc_descs = (rt_svc_desc_t *) RT_SVC_DESCS_START;
100*532ed618SSoby Mathew 	for (index = 0; index < RT_SVC_DECS_NUM; index++) {
101*532ed618SSoby Mathew 
102*532ed618SSoby Mathew 		/*
103*532ed618SSoby Mathew 		 * An invalid descriptor is an error condition since it is
104*532ed618SSoby Mathew 		 * difficult to predict the system behaviour in the absence
105*532ed618SSoby Mathew 		 * of this service.
106*532ed618SSoby Mathew 		 */
107*532ed618SSoby Mathew 		rc = validate_rt_svc_desc(&rt_svc_descs[index]);
108*532ed618SSoby Mathew 		if (rc) {
109*532ed618SSoby Mathew 			ERROR("Invalid runtime service descriptor %p (%s)\n",
110*532ed618SSoby Mathew 					(void *) &rt_svc_descs[index],
111*532ed618SSoby Mathew 					rt_svc_descs[index].name);
112*532ed618SSoby Mathew 			goto error;
113*532ed618SSoby Mathew 		}
114*532ed618SSoby Mathew 
115*532ed618SSoby Mathew 		/*
116*532ed618SSoby Mathew 		 * The runtime service may have separate rt_svc_desc_t
117*532ed618SSoby Mathew 		 * for its fast smc and standard smc. Since the service itself
118*532ed618SSoby Mathew 		 * need to be initialized only once, only one of them will have
119*532ed618SSoby Mathew 		 * an initialisation routine defined. Call the initialisation
120*532ed618SSoby Mathew 		 * routine for this runtime service, if it is defined.
121*532ed618SSoby Mathew 		 */
122*532ed618SSoby Mathew 		if (rt_svc_descs[index].init) {
123*532ed618SSoby Mathew 			rc = rt_svc_descs[index].init();
124*532ed618SSoby Mathew 			if (rc) {
125*532ed618SSoby Mathew 				ERROR("Error initializing runtime service %s\n",
126*532ed618SSoby Mathew 						rt_svc_descs[index].name);
127*532ed618SSoby Mathew 				continue;
128*532ed618SSoby Mathew 			}
129*532ed618SSoby Mathew 		}
130*532ed618SSoby Mathew 
131*532ed618SSoby Mathew 		/*
132*532ed618SSoby Mathew 		 * Fill the indices corresponding to the start and end
133*532ed618SSoby Mathew 		 * owning entity numbers with the index of the
134*532ed618SSoby Mathew 		 * descriptor which will handle the SMCs for this owning
135*532ed618SSoby Mathew 		 * entity range.
136*532ed618SSoby Mathew 		 */
137*532ed618SSoby Mathew 		start_idx = get_unique_oen(rt_svc_descs[index].start_oen,
138*532ed618SSoby Mathew 				rt_svc_descs[index].call_type);
139*532ed618SSoby Mathew 		end_idx = get_unique_oen(rt_svc_descs[index].end_oen,
140*532ed618SSoby Mathew 				rt_svc_descs[index].call_type);
141*532ed618SSoby Mathew 
142*532ed618SSoby Mathew 		for (; start_idx <= end_idx; start_idx++)
143*532ed618SSoby Mathew 			rt_svc_descs_indices[start_idx] = index;
144*532ed618SSoby Mathew 	}
145*532ed618SSoby Mathew 
146*532ed618SSoby Mathew 	return;
147*532ed618SSoby Mathew error:
148*532ed618SSoby Mathew 	panic();
149*532ed618SSoby Mathew }
150