xref: /rk3399_ARM-atf/common/runtime_svc.c (revision 07f4e078b6871e5c74f6cb38f2726a2cfcb2b746)
1 /*
2  * Copyright (c) 2013-2014, 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 <stdio.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <assert.h>
35 #include <arch_helpers.h>
36 #include <console.h>
37 #include <platform.h>
38 #include <semihosting.h>
39 #include <bl_common.h>
40 #include <psci.h>
41 #include <runtime_svc.h>
42 #include <context.h>
43 #include <debug.h>
44 
45 /*******************************************************************************
46  * The 'rt_svc_descs' array holds the runtime service descriptors exported by
47  * services by placing them in the 'rt_svc_descs' linker section.
48  * The 'rt_svc_descs_indices' array holds the index of a descriptor in the
49  * 'rt_svc_descs' array. When an SMC arrives, the OEN[29:24] bits and the call
50  * type[31] bit in the function id are combined to get an index into the
51  * 'rt_svc_descs_indices' array. This gives the index of the descriptor in the
52  * 'rt_svc_descs' array which contains the SMC handler.
53  ******************************************************************************/
54 #define RT_SVC_DESCS_START	((uint64_t) (&__RT_SVC_DESCS_START__))
55 #define RT_SVC_DESCS_END	((uint64_t) (&__RT_SVC_DESCS_END__))
56 uint8_t rt_svc_descs_indices[MAX_RT_SVCS];
57 static rt_svc_desc *rt_svc_descs;
58 
59 /*******************************************************************************
60  * Simple routine to sanity check a runtime service descriptor before using it
61  ******************************************************************************/
62 static int32_t validate_rt_svc_desc(rt_svc_desc *desc)
63 {
64 	if (desc == NULL)
65 		return -EINVAL;
66 
67 	if (desc->start_oen > desc->end_oen)
68 		return -EINVAL;
69 
70 	if (desc->end_oen >= OEN_LIMIT)
71 		return -EINVAL;
72 
73 	if (desc->call_type != SMC_TYPE_FAST && desc->call_type != SMC_TYPE_STD)
74 		return -EINVAL;
75 
76 	/* A runtime service having no init or handle function doesn't make sense */
77 	if (desc->init == NULL && desc->handle == NULL)
78 		return -EINVAL;
79 
80 	return 0;
81 }
82 
83 /*******************************************************************************
84  * This function calls the initialisation routine in the descriptor exported by
85  * a runtime service. Once a descriptor has been validated, its start & end
86  * owning entity numbers and the call type are combined to form a unique oen.
87  * The unique oen is used as an index into the 'rt_svc_descs_indices' array.
88  * The index of the runtime service descriptor is stored at this index.
89  ******************************************************************************/
90 void runtime_svc_init()
91 {
92 	int32_t rc = 0;
93 	uint32_t index, start_idx, end_idx;
94 	uint64_t rt_svc_descs_num;
95 
96 	/* If no runtime services are implemented then simply bail out */
97 	rt_svc_descs_num = RT_SVC_DESCS_END - RT_SVC_DESCS_START;
98 	rt_svc_descs_num /= sizeof(rt_svc_desc);
99 	if (rt_svc_descs_num == 0)
100 		return;
101 
102 	/* Initialise internal variables to invalid state */
103 	memset(rt_svc_descs_indices, -1, sizeof(rt_svc_descs_indices));
104 
105 	rt_svc_descs = (rt_svc_desc *) RT_SVC_DESCS_START;
106 	for (index = 0; index < rt_svc_descs_num; index++) {
107 
108 		/*
109 		 * An invalid descriptor is an error condition since it is
110 		 * difficult to predict the system behaviour in the absence
111 		 * of this service.
112 		 */
113 		rc = validate_rt_svc_desc(&rt_svc_descs[index]);
114 		if (rc) {
115 			ERROR("Invalid runtime service descriptor 0x%x (%s)\n",
116 					&rt_svc_descs[index],
117 					rt_svc_descs[index].name);
118 			goto error;
119 		}
120 
121 		/*
122 		 * Fill the indices corresponding to the start and end owning
123 		 * entity numbers with the index of the descriptor which will
124 		 * handle the SMCs for this owning entity range.
125 		 */
126 		start_idx = get_unique_oen(rt_svc_descs[index].start_oen,
127 					   rt_svc_descs[index].call_type);
128 		end_idx = get_unique_oen(rt_svc_descs[index].end_oen,
129 					 rt_svc_descs[index].call_type);
130 		for (; start_idx <= end_idx; start_idx++)
131 			rt_svc_descs_indices[start_idx] = index;
132 
133 		/* Call the initialisation routine for this runtime service */
134 		rc = rt_svc_descs[index].init();
135 		if (rc) {
136 			ERROR("Error initializing runtime service %s\n",
137 					rt_svc_descs[index].name);
138 			goto error;
139 		}
140 	}
141 
142 	return;
143 error:
144 	panic();
145 }
146 
147 void fault_handler(void *handle)
148 {
149 	gp_regs_next *gpregs_ctx = get_gpregs_ctx(handle);
150 	ERROR("Unhandled synchronous fault. Register dump @ 0x%x \n",
151 	      gpregs_ctx);
152 	panic();
153 }
154