xref: /rk3399_ARM-atf/common/runtime_svc.c (revision 2f370465241c85b2fe38a1eb69b457b9fed97207)
1532ed618SSoby Mathew /*
29f85f9e3SJoel Hutton  * Copyright (c) 2013-2018, ARM Limited and Contributors. All rights reserved.
3532ed618SSoby Mathew  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
5532ed618SSoby Mathew  */
6532ed618SSoby Mathew 
7532ed618SSoby Mathew #include <assert.h>
8532ed618SSoby Mathew #include <debug.h>
9532ed618SSoby Mathew #include <errno.h>
10532ed618SSoby Mathew #include <runtime_svc.h>
11532ed618SSoby Mathew #include <string.h>
12532ed618SSoby Mathew 
13532ed618SSoby Mathew /*******************************************************************************
14532ed618SSoby Mathew  * The 'rt_svc_descs' array holds the runtime service descriptors exported by
15532ed618SSoby Mathew  * services by placing them in the 'rt_svc_descs' linker section.
16532ed618SSoby Mathew  * The 'rt_svc_descs_indices' array holds the index of a descriptor in the
17532ed618SSoby Mathew  * 'rt_svc_descs' array. When an SMC arrives, the OEN[29:24] bits and the call
18532ed618SSoby Mathew  * type[31] bit in the function id are combined to get an index into the
19532ed618SSoby Mathew  * 'rt_svc_descs_indices' array. This gives the index of the descriptor in the
20532ed618SSoby Mathew  * 'rt_svc_descs' array which contains the SMC handler.
21532ed618SSoby Mathew  ******************************************************************************/
22532ed618SSoby Mathew uint8_t rt_svc_descs_indices[MAX_RT_SVCS];
23532ed618SSoby Mathew static rt_svc_desc_t *rt_svc_descs;
24532ed618SSoby Mathew 
25532ed618SSoby Mathew #define RT_SVC_DECS_NUM		((RT_SVC_DESCS_END - RT_SVC_DESCS_START)\
26532ed618SSoby Mathew 					/ sizeof(rt_svc_desc_t))
27532ed618SSoby Mathew 
28532ed618SSoby Mathew /*******************************************************************************
29*2f370465SAntonio Nino Diaz  * Function to invoke the registered `handle` corresponding to the smc_fid in
30*2f370465SAntonio Nino Diaz  * AArch32 mode.
311ae0a49aSSoby Mathew  ******************************************************************************/
32*2f370465SAntonio Nino Diaz #if SMCCC_MAJOR_VERSION == 1
331ae0a49aSSoby Mathew uintptr_t handle_runtime_svc(uint32_t smc_fid,
341ae0a49aSSoby Mathew 			     void *cookie,
351ae0a49aSSoby Mathew 			     void *handle,
361ae0a49aSSoby Mathew 			     unsigned int flags)
371ae0a49aSSoby Mathew {
381ae0a49aSSoby Mathew 	u_register_t x1, x2, x3, x4;
396311f63dSVarun Wadekar 	int index;
406311f63dSVarun Wadekar 	unsigned int idx;
411ae0a49aSSoby Mathew 	const rt_svc_desc_t *rt_svc_descs;
421ae0a49aSSoby Mathew 
431ae0a49aSSoby Mathew 	assert(handle);
441ae0a49aSSoby Mathew 	idx = get_unique_oen_from_smc_fid(smc_fid);
456311f63dSVarun Wadekar 	assert(idx < MAX_RT_SVCS);
461ae0a49aSSoby Mathew 
471ae0a49aSSoby Mathew 	index = rt_svc_descs_indices[idx];
486311f63dSVarun Wadekar 	if (index < 0 || index >= (int)RT_SVC_DECS_NUM)
491ae0a49aSSoby Mathew 		SMC_RET1(handle, SMC_UNK);
501ae0a49aSSoby Mathew 
511ae0a49aSSoby Mathew 	rt_svc_descs = (rt_svc_desc_t *) RT_SVC_DESCS_START;
521ae0a49aSSoby Mathew 
531ae0a49aSSoby Mathew 	get_smc_params_from_ctx(handle, x1, x2, x3, x4);
541ae0a49aSSoby Mathew 
551ae0a49aSSoby Mathew 	return rt_svc_descs[index].handle(smc_fid, x1, x2, x3, x4, cookie,
561ae0a49aSSoby Mathew 						handle, flags);
571ae0a49aSSoby Mathew }
58*2f370465SAntonio Nino Diaz #endif /* SMCCC_MAJOR_VERSION */
591ae0a49aSSoby Mathew 
601ae0a49aSSoby Mathew /*******************************************************************************
61532ed618SSoby Mathew  * Simple routine to sanity check a runtime service descriptor before using it
62532ed618SSoby Mathew  ******************************************************************************/
639d24d353SSandrine Bailleux static int32_t validate_rt_svc_desc(const rt_svc_desc_t *desc)
64532ed618SSoby Mathew {
65532ed618SSoby Mathew 	if (desc == NULL)
66532ed618SSoby Mathew 		return -EINVAL;
67532ed618SSoby Mathew 
68532ed618SSoby Mathew 	if (desc->start_oen > desc->end_oen)
69532ed618SSoby Mathew 		return -EINVAL;
70532ed618SSoby Mathew 
71532ed618SSoby Mathew 	if (desc->end_oen >= OEN_LIMIT)
72532ed618SSoby Mathew 		return -EINVAL;
73532ed618SSoby Mathew 
74*2f370465SAntonio Nino Diaz #if SMCCC_MAJOR_VERSION == 1
75*2f370465SAntonio Nino Diaz 	if ((desc->call_type != SMC_TYPE_FAST) &&
76*2f370465SAntonio Nino Diaz 	    (desc->call_type != SMC_TYPE_YIELD))
77532ed618SSoby Mathew 		return -EINVAL;
78*2f370465SAntonio Nino Diaz #elif SMCCC_MAJOR_VERSION == 2
79*2f370465SAntonio Nino Diaz 	if (desc->is_vendor > 1U)
80*2f370465SAntonio Nino Diaz 		return -EINVAL;
81*2f370465SAntonio Nino Diaz #endif /* SMCCC_MAJOR_VERSION */
82532ed618SSoby Mathew 
83532ed618SSoby Mathew 	/* A runtime service having no init or handle function doesn't make sense */
84*2f370465SAntonio Nino Diaz 	if ((desc->init == NULL) && (desc->handle == NULL))
85532ed618SSoby Mathew 		return -EINVAL;
86532ed618SSoby Mathew 
87532ed618SSoby Mathew 	return 0;
88532ed618SSoby Mathew }
89532ed618SSoby Mathew 
90532ed618SSoby Mathew /*******************************************************************************
91532ed618SSoby Mathew  * This function calls the initialisation routine in the descriptor exported by
92532ed618SSoby Mathew  * a runtime service. Once a descriptor has been validated, its start & end
93532ed618SSoby Mathew  * owning entity numbers and the call type are combined to form a unique oen.
94532ed618SSoby Mathew  * The unique oen is used as an index into the 'rt_svc_descs_indices' array.
95532ed618SSoby Mathew  * The index of the runtime service descriptor is stored at this index.
96532ed618SSoby Mathew  ******************************************************************************/
97532ed618SSoby Mathew void runtime_svc_init(void)
98532ed618SSoby Mathew {
996311f63dSVarun Wadekar 	int rc = 0;
1006311f63dSVarun Wadekar 	unsigned int index, start_idx, end_idx;
101532ed618SSoby Mathew 
102532ed618SSoby Mathew 	/* Assert the number of descriptors detected are less than maximum indices */
1035e5e4162SSoby Mathew 	assert((RT_SVC_DESCS_END >= RT_SVC_DESCS_START) &&
1045e5e4162SSoby Mathew 			(RT_SVC_DECS_NUM < MAX_RT_SVCS));
105532ed618SSoby Mathew 
106532ed618SSoby Mathew 	/* If no runtime services are implemented then simply bail out */
107*2f370465SAntonio Nino Diaz 	if (RT_SVC_DECS_NUM == 0U)
108532ed618SSoby Mathew 		return;
109532ed618SSoby Mathew 
110532ed618SSoby Mathew 	/* Initialise internal variables to invalid state */
111532ed618SSoby Mathew 	memset(rt_svc_descs_indices, -1, sizeof(rt_svc_descs_indices));
112532ed618SSoby Mathew 
113532ed618SSoby Mathew 	rt_svc_descs = (rt_svc_desc_t *) RT_SVC_DESCS_START;
114532ed618SSoby Mathew 	for (index = 0; index < RT_SVC_DECS_NUM; index++) {
1159d24d353SSandrine Bailleux 		rt_svc_desc_t *service = &rt_svc_descs[index];
116532ed618SSoby Mathew 
117532ed618SSoby Mathew 		/*
118532ed618SSoby Mathew 		 * An invalid descriptor is an error condition since it is
119532ed618SSoby Mathew 		 * difficult to predict the system behaviour in the absence
120532ed618SSoby Mathew 		 * of this service.
121532ed618SSoby Mathew 		 */
1229d24d353SSandrine Bailleux 		rc = validate_rt_svc_desc(service);
123532ed618SSoby Mathew 		if (rc) {
1243a26a28cSSandrine Bailleux 			ERROR("Invalid runtime service descriptor %p\n",
1253a26a28cSSandrine Bailleux 				(void *) service);
1269d24d353SSandrine Bailleux 			panic();
127532ed618SSoby Mathew 		}
128532ed618SSoby Mathew 
129532ed618SSoby Mathew 		/*
130532ed618SSoby Mathew 		 * The runtime service may have separate rt_svc_desc_t
13116292f54SDavid Cunado 		 * for its fast smc and yielding smc. Since the service itself
132532ed618SSoby Mathew 		 * need to be initialized only once, only one of them will have
133532ed618SSoby Mathew 		 * an initialisation routine defined. Call the initialisation
134532ed618SSoby Mathew 		 * routine for this runtime service, if it is defined.
135532ed618SSoby Mathew 		 */
1369d24d353SSandrine Bailleux 		if (service->init) {
1379d24d353SSandrine Bailleux 			rc = service->init();
138532ed618SSoby Mathew 			if (rc) {
139532ed618SSoby Mathew 				ERROR("Error initializing runtime service %s\n",
1409d24d353SSandrine Bailleux 						service->name);
141532ed618SSoby Mathew 				continue;
142532ed618SSoby Mathew 			}
143532ed618SSoby Mathew 		}
144532ed618SSoby Mathew 
145532ed618SSoby Mathew 		/*
146532ed618SSoby Mathew 		 * Fill the indices corresponding to the start and end
147532ed618SSoby Mathew 		 * owning entity numbers with the index of the
148532ed618SSoby Mathew 		 * descriptor which will handle the SMCs for this owning
149532ed618SSoby Mathew 		 * entity range.
150532ed618SSoby Mathew 		 */
151*2f370465SAntonio Nino Diaz #if SMCCC_MAJOR_VERSION == 1
152*2f370465SAntonio Nino Diaz 		start_idx = get_unique_oen(service->start_oen,
1539d24d353SSandrine Bailleux 					   service->call_type);
154*2f370465SAntonio Nino Diaz 		end_idx = get_unique_oen(service->end_oen,
1559d24d353SSandrine Bailleux 					 service->call_type);
156*2f370465SAntonio Nino Diaz #elif SMCCC_MAJOR_VERSION == 2
157*2f370465SAntonio Nino Diaz 		start_idx = get_rt_desc_idx(service->start_oen,
158*2f370465SAntonio Nino Diaz 					    service->is_vendor);
159*2f370465SAntonio Nino Diaz 		end_idx = get_rt_desc_idx(service->end_oen,
160*2f370465SAntonio Nino Diaz 					  service->is_vendor);
161*2f370465SAntonio Nino Diaz #endif
162*2f370465SAntonio Nino Diaz 		assert(start_idx <= end_idx);
1633a26a28cSSandrine Bailleux 		assert(end_idx < MAX_RT_SVCS);
164532ed618SSoby Mathew 		for (; start_idx <= end_idx; start_idx++)
165532ed618SSoby Mathew 			rt_svc_descs_indices[start_idx] = index;
166532ed618SSoby Mathew 	}
167532ed618SSoby Mathew }
168