blob: 05907f5c85403d74b735d6aa192f912487cb2602 [file] [log] [blame]
Achin Gupta7c88f3f2014-02-18 18:09:12 +00001/*
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 <bl32.h>
32#include <tsp.h>
33#include <arch_helpers.h>
34#include <stdio.h>
35#include <platform.h>
36#include <debug.h>
37#include <spinlock.h>
38
39/*******************************************************************************
40 * Lock to control access to the console
41 ******************************************************************************/
42spinlock_t console_lock;
43
44/*******************************************************************************
45 * Per cpu data structure to populate parameters for an SMC in C code and use
46 * a pointer to this structure in assembler code to populate x0-x7
47 ******************************************************************************/
48static tsp_args tsp_smc_args[PLATFORM_CORE_COUNT];
49
50/*******************************************************************************
51 * Per cpu data structure to keep track of TSP activity
52 ******************************************************************************/
53static work_statistics tsp_stats[PLATFORM_CORE_COUNT];
54
55/*******************************************************************************
56 * Single reference to the various entry points exported by the test secure
57 * payload. A single copy should suffice for all cpus as they are not expected
58 * to change.
59 ******************************************************************************/
60static const entry_info tsp_entry_info = {
61 tsp_fast_smc_entry,
62 tsp_cpu_on_entry,
63 tsp_cpu_off_entry,
64 tsp_cpu_resume_entry,
65 tsp_cpu_suspend_entry,
66};
67
68static tsp_args *set_smc_args(uint64_t arg0,
69 uint64_t arg1,
70 uint64_t arg2,
71 uint64_t arg3,
72 uint64_t arg4,
73 uint64_t arg5,
74 uint64_t arg6,
75 uint64_t arg7)
76{
77 uint64_t mpidr = read_mpidr();
78 uint32_t linear_id;
79 tsp_args *pcpu_smc_args;
80
81 /*
82 * Return to Secure Monitor by raising an SMC. The results of the
83 * service are passed as an arguments to the SMC
84 */
85 linear_id = platform_get_core_pos(mpidr);
86 pcpu_smc_args = &tsp_smc_args[linear_id];
87 write_sp_arg(pcpu_smc_args, TSP_ARG0, arg0);
88 write_sp_arg(pcpu_smc_args, TSP_ARG1, arg1);
89 write_sp_arg(pcpu_smc_args, TSP_ARG2, arg2);
90 write_sp_arg(pcpu_smc_args, TSP_ARG3, arg3);
91 write_sp_arg(pcpu_smc_args, TSP_ARG4, arg4);
92 write_sp_arg(pcpu_smc_args, TSP_ARG5, arg5);
93 write_sp_arg(pcpu_smc_args, TSP_ARG6, arg6);
94 write_sp_arg(pcpu_smc_args, TSP_ARG7, arg7);
95
96 return pcpu_smc_args;
97}
98
99/*******************************************************************************
100 * TSP main entry point where it gets the opportunity to initialize its secure
101 * state/applications. Once the state is initialized, it must return to the
102 * SPD with a pointer to the 'tsp_entry_info' structure.
103 ******************************************************************************/
104uint64_t tsp_main(void)
105{
106 uint64_t mpidr = read_mpidr();
107 uint32_t linear_id = platform_get_core_pos(mpidr);
108
109#if DEBUG
110 meminfo *mem_layout = bl32_plat_sec_mem_layout();
111#endif
112
113 /* Initialize the platform */
114 bl32_platform_setup();
115
116 /* Initialize secure/applications state here */
117
118 /* Update this cpu's statistics */
119 tsp_stats[linear_id].smc_count++;
120 tsp_stats[linear_id].eret_count++;
121 tsp_stats[linear_id].cpu_on_count++;
122
123 spin_lock(&console_lock);
124#if defined (__GNUC__)
125 printf("TSP Built : %s, %s\n\r", __TIME__, __DATE__);
126#endif
127 INFO("Total memory base : 0x%x\n", mem_layout->total_base);
128 INFO("Total memory size : 0x%x bytes\n", mem_layout->total_size);
129 INFO("Free memory base : 0x%x\n", mem_layout->free_base);
130 INFO("Free memory size : 0x%x bytes\n", mem_layout->free_size);
131 INFO("cpu 0x%x: %d smcs, %d erets %d cpu on requests\n", mpidr,
132 tsp_stats[linear_id].smc_count,
133 tsp_stats[linear_id].eret_count,
134 tsp_stats[linear_id].cpu_on_count);
135 spin_unlock(&console_lock);
136
137 /*
138 * TODO: There is a massive assumption that the SPD and SP can see each
139 * other's memory without issues so it is safe to pass pointers to
140 * internal memory. Replace this with a shared communication buffer.
141 */
142 return (uint64_t) &tsp_entry_info;
143}
144
145/*******************************************************************************
146 * This function performs any remaining book keeping in the test secure payload
147 * after this cpu's architectural state has been setup in response to an earlier
148 * psci cpu_on request.
149 ******************************************************************************/
150tsp_args *tsp_cpu_on_main(void)
151{
152 uint64_t mpidr = read_mpidr();
153 uint32_t linear_id = platform_get_core_pos(mpidr);
154
155 /* Update this cpu's statistics */
156 tsp_stats[linear_id].smc_count++;
157 tsp_stats[linear_id].eret_count++;
158 tsp_stats[linear_id].cpu_on_count++;
159
160 spin_lock(&console_lock);
161 printf("SP: cpu 0x%x turned on\n\r", mpidr);
162 INFO("cpu 0x%x: %d smcs, %d erets %d cpu on requests\n", mpidr,
163 tsp_stats[linear_id].smc_count,
164 tsp_stats[linear_id].eret_count,
165 tsp_stats[linear_id].cpu_on_count);
166 spin_unlock(&console_lock);
167
168 /* Indicate to the SPD that we have completed turned ourselves on */
169 return set_smc_args(TSP_ON_DONE, 0, 0, 0, 0, 0, 0, 0);
170}
171
172/*******************************************************************************
173 * This function performs any remaining book keeping in the test secure payload
174 * before this cpu is turned off in response to a psci cpu_off request.
175 ******************************************************************************/
176tsp_args *tsp_cpu_off_main(uint64_t arg0,
177 uint64_t arg1,
178 uint64_t arg2,
179 uint64_t arg3,
180 uint64_t arg4,
181 uint64_t arg5,
182 uint64_t arg6,
183 uint64_t arg7)
184{
185 uint64_t mpidr = read_mpidr();
186 uint32_t linear_id = platform_get_core_pos(mpidr);
187
188 /* Update this cpu's statistics */
189 tsp_stats[linear_id].smc_count++;
190 tsp_stats[linear_id].eret_count++;
191 tsp_stats[linear_id].cpu_off_count++;
192
193 spin_lock(&console_lock);
194 printf("SP: cpu 0x%x off request\n\r", mpidr);
195 INFO("cpu 0x%x: %d smcs, %d erets %d cpu off requests\n", mpidr,
196 tsp_stats[linear_id].smc_count,
197 tsp_stats[linear_id].eret_count,
198 tsp_stats[linear_id].cpu_off_count);
199 spin_unlock(&console_lock);
200
201
Achin Gupta607084e2014-02-09 18:24:19 +0000202 /* Indicate to the SPD that we have completed this request */
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000203 return set_smc_args(TSP_OFF_DONE, 0, 0, 0, 0, 0, 0, 0);
204}
205
206/*******************************************************************************
207 * This function performs any book keeping in the test secure payload before
208 * this cpu's architectural state is saved in response to an earlier psci
209 * cpu_suspend request.
210 ******************************************************************************/
211tsp_args *tsp_cpu_suspend_main(uint64_t power_state,
212 uint64_t arg1,
213 uint64_t arg2,
214 uint64_t arg3,
215 uint64_t arg4,
216 uint64_t arg5,
217 uint64_t arg6,
218 uint64_t arg7)
219{
220 uint64_t mpidr = read_mpidr();
221 uint32_t linear_id = platform_get_core_pos(mpidr);
222
223 /* Update this cpu's statistics */
224 tsp_stats[linear_id].smc_count++;
225 tsp_stats[linear_id].eret_count++;
226 tsp_stats[linear_id].cpu_suspend_count++;
227
228 spin_lock(&console_lock);
229 printf("SP: cpu 0x%x suspend request. power state: 0x%x\n\r",
230 mpidr, power_state);
231 INFO("cpu 0x%x: %d smcs, %d erets %d cpu suspend requests\n", mpidr,
232 tsp_stats[linear_id].smc_count,
233 tsp_stats[linear_id].eret_count,
234 tsp_stats[linear_id].cpu_suspend_count);
235 spin_unlock(&console_lock);
236
Achin Gupta607084e2014-02-09 18:24:19 +0000237 /* Indicate to the SPD that we have completed this request */
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000238 return set_smc_args(TSP_SUSPEND_DONE, 0, 0, 0, 0, 0, 0, 0);
239}
240
241/*******************************************************************************
242 * This function performs any book keeping in the test secure payload after this
243 * cpu's architectural state has been restored after wakeup from an earlier psci
244 * cpu_suspend request.
245 ******************************************************************************/
246tsp_args *tsp_cpu_resume_main(uint64_t suspend_level,
247 uint64_t arg1,
248 uint64_t arg2,
249 uint64_t arg3,
250 uint64_t arg4,
251 uint64_t arg5,
252 uint64_t arg6,
253 uint64_t arg7)
254{
255 uint64_t mpidr = read_mpidr();
256 uint32_t linear_id = platform_get_core_pos(mpidr);
257
258 /* Update this cpu's statistics */
259 tsp_stats[linear_id].smc_count++;
260 tsp_stats[linear_id].eret_count++;
261 tsp_stats[linear_id].cpu_resume_count++;
262
263 spin_lock(&console_lock);
264 printf("SP: cpu 0x%x resumed. suspend level %d \n\r",
265 mpidr, suspend_level);
266 INFO("cpu 0x%x: %d smcs, %d erets %d cpu suspend requests\n", mpidr,
267 tsp_stats[linear_id].smc_count,
268 tsp_stats[linear_id].eret_count,
269 tsp_stats[linear_id].cpu_suspend_count);
270 spin_unlock(&console_lock);
271
Achin Gupta607084e2014-02-09 18:24:19 +0000272 /* Indicate to the SPD that we have completed this request */
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000273 return set_smc_args(TSP_RESUME_DONE, 0, 0, 0, 0, 0, 0, 0);
274}
275
276/*******************************************************************************
277 * TSP fast smc handler. The secure monitor jumps to this function by
278 * doing the ERET after populating X0-X7 registers. The arguments are received
279 * in the function arguments in order. Once the service is rendered, this
280 * function returns to Secure Monitor by raising SMC
281 ******************************************************************************/
282tsp_args *tsp_fast_smc_handler(uint64_t func,
283 uint64_t arg1,
284 uint64_t arg2,
285 uint64_t arg3,
286 uint64_t arg4,
287 uint64_t arg5,
288 uint64_t arg6,
289 uint64_t arg7)
290{
Achin Gupta916a2c12014-02-09 23:11:46 +0000291 uint64_t results[2];
292 uint64_t service_args[2];
293 uint64_t mpidr = read_mpidr();
294 uint32_t linear_id = platform_get_core_pos(mpidr);
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000295
Achin Gupta916a2c12014-02-09 23:11:46 +0000296 /* Update this cpu's statistics */
297 tsp_stats[linear_id].smc_count++;
298 tsp_stats[linear_id].eret_count++;
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000299
Achin Gupta916a2c12014-02-09 23:11:46 +0000300 printf("SP: cpu 0x%x received fast smc 0x%x\n", read_mpidr(), func);
301 INFO("cpu 0x%x: %d smcs, %d erets\n", mpidr,
302 tsp_stats[linear_id].smc_count,
303 tsp_stats[linear_id].eret_count);
304
305 /* Render secure services and obtain results here */
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000306
307 results[0] = arg1;
308 results[1] = arg2;
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000309
310 /*
311 * Request a service back from dispatcher/secure monitor. This call
312 * return and thereafter resume exectuion
313 */
314 tsp_get_magic(service_args);
315
316 /* Determine the function to perform based on the function ID */
317 switch (func) {
318 case TSP_FID_ADD:
319 results[0] += service_args[0];
320 results[1] += service_args[1];
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000321 break;
322 case TSP_FID_SUB:
323 results[0] -= service_args[0];
324 results[1] -= service_args[1];
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000325 break;
326 case TSP_FID_MUL:
327 results[0] *= service_args[0];
328 results[1] *= service_args[1];
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000329 break;
330 case TSP_FID_DIV:
331 results[0] /= service_args[0] ? service_args[0] : 1;
332 results[1] /= service_args[1] ? service_args[1] : 1;
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000333 break;
334 default:
335 break;
336 }
337
Achin Gupta916a2c12014-02-09 23:11:46 +0000338 return set_smc_args(func,
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000339 results[0],
340 results[1],
Achin Gupta916a2c12014-02-09 23:11:46 +0000341 0, 0, 0, 0, 0);
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000342}
343