blob: 3f94dc94aaff2e69727e7879c291c8bfb641c831 [file] [log] [blame]
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02001/*
2 * Copyright (c) 2018, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <arch_helpers.h>
8#include <arm_gic.h>
9#include <assert.h>
10#include <debug.h>
11#include <irq.h>
12#include <mmio.h>
13#include <nvm.h>
14#include <plat_topology.h>
15#include <platform.h>
16#include <platform_def.h>
17#include <power_management.h>
18#include <psci.h>
19#include <sgi.h>
20#include <string.h>
21#include <sys/types.h>
22#include <tftf.h>
23#include <tftf_lib.h>
24#include <timer.h>
25
26/* version information for TFTF */
27extern const char version_string[];
28
29unsigned int lead_cpu_mpid;
30
31/* Defined in hotplug.c */
32extern volatile test_function_t test_entrypoint[PLATFORM_CORE_COUNT];
33
34/* Per-CPU results for the current test */
35static test_result_t test_results[PLATFORM_CORE_COUNT];
36
37/* Context ID passed to tftf_psci_cpu_on() */
38static u_register_t cpu_on_ctx_id_arr[PLATFORM_CORE_COUNT];
39
40static unsigned int test_is_rebooting;
41
42static inline const test_suite_t *current_testsuite(void)
43{
44 test_ref_t test_to_run;
45 tftf_get_test_to_run(&test_to_run);
46 return &testsuites[test_to_run.testsuite_idx];
47}
48
49static inline const test_case_t *current_testcase(void)
50{
51 test_ref_t test_to_run;
52 tftf_get_test_to_run(&test_to_run);
53 return &testsuites[test_to_run.testsuite_idx].
54 testcases[test_to_run.testcase_idx];
55}
56
57/*
58 * Identify the next test in the tests list and update the NVM data to point to
59 * that test.
60 * If there is no more tests to execute, return NULL.
61 * Otherwise, return the test case.
62 */
63static const test_case_t *advance_to_next_test(void)
64{
65 test_ref_t test_to_run;
66 const test_case_t *testcase;
67 unsigned int testcase_idx;
68 unsigned int testsuite_idx;
69
70#if DEBUG
71 test_progress_t progress;
72 tftf_get_test_progress(&progress);
73 assert(progress == TEST_COMPLETE);
74#endif
75
76 tftf_get_test_to_run(&test_to_run);
77 testcase_idx = test_to_run.testcase_idx;
78 testsuite_idx = test_to_run.testsuite_idx;
79
80 /* Move to the next test case in the current test suite */
81 ++testcase_idx;
82 testcase = &testsuites[testsuite_idx].testcases[testcase_idx];
83
84 if (testcase->name == NULL) {
85 /*
86 * There's no more test cases in the current test suite so move
87 * to the first test case of the next test suite.
88 */
89 const test_suite_t *testsuite;
90 testcase_idx = 0;
91 ++testsuite_idx;
92 testsuite = &testsuites[testsuite_idx];
93 testcase = &testsuite->testcases[0];
94
95 if (testsuite->name == NULL) {
96 /*
97 * This was the last test suite so there's no more tests
98 * at all.
99 */
100 return NULL;
101 }
102 }
103
104 VERBOSE("Moving to test (%u,%u)\n", testsuite_idx, testcase_idx);
105 test_to_run.testsuite_idx = testsuite_idx;
106 test_to_run.testcase_idx = testcase_idx;
107 tftf_set_test_to_run(test_to_run);
108 tftf_set_test_progress(TEST_READY);
109
110 return testcase;
111}
112
113/*
114 * This function is executed only by the lead CPU.
115 * It prepares the environment for the next test to run.
116 */
117static void prepare_next_test(void)
118{
119 unsigned int mpid;
120 unsigned int core_pos;
121 unsigned int cpu_node;
122
123 /* This function should be called by the lead CPU only */
124 assert((read_mpidr_el1() & MPID_MASK) == lead_cpu_mpid);
125
126 /*
127 * Only the lead CPU should be powered on at this stage. All other CPUs
128 * should be powered off or powering off. If some CPUs are not powered
129 * off yet, wait for them to power off.
130 */
131 for_each_cpu(cpu_node) {
132 mpid = tftf_get_mpidr_from_node(cpu_node);
133 if (mpid == lead_cpu_mpid)
134 assert(tftf_is_cpu_online(mpid));
135 else
136 while (tftf_psci_affinity_info(mpid, MPIDR_AFFLVL0)
137 == PSCI_STATE_ON)
138 ;
139 }
140
141 /* No CPU should have entered the test yet */
142 assert(tftf_get_ref_cnt() == 0);
143
144 /* Populate the test entrypoint for the lead CPU */
145 core_pos = platform_get_core_pos(lead_cpu_mpid);
146 test_entrypoint[core_pos] = (test_function_t) current_testcase()->test;
147
148 for (unsigned int i = 0; i < PLATFORM_CORE_COUNT; ++i)
149 test_results[i] = TEST_RESULT_NA;
150
151 NOTICE("Starting unittest '%s - %s'\n",
152 current_testsuite()->name, current_testcase()->name);
153
154 /* Program the watchdog */
155 tftf_platform_watchdog_set();
156
157 /* TODO: Take a 1st timestamp to be able to measure test duration */
158
159 tftf_set_test_progress(TEST_IN_PROGRESS);
160}
161
162/*
163 * Go through individual CPUs' test results and determine the overall
164 * test result from that.
165 */
166static test_result_t get_overall_test_result(void)
167{
168 test_result_t result = TEST_RESULT_NA;
169 unsigned int cpu_mpid;
170 unsigned int cpu_node;
171 unsigned int core_pos;
172
173 for_each_cpu(cpu_node) {
174 cpu_mpid = tftf_get_mpidr_from_node(cpu_node);
175 core_pos = platform_get_core_pos(cpu_mpid);
176
177 switch (test_results[core_pos]) {
178 case TEST_RESULT_NA:
179 VERBOSE("CPU%u not involved in the test\n", core_pos);
180 /* Ignoring */
181 break;
182
183 case TEST_RESULT_SKIPPED:
184 /*
185 * If at least one CPU skipped the test, consider the
186 * whole test as skipped as well.
187 */
188 NOTICE("CPU%u skipped the test\n", core_pos);
189 return TEST_RESULT_SKIPPED;
190
191 case TEST_RESULT_SUCCESS:
192 result = TEST_RESULT_SUCCESS;
193 break;
194
195 case TEST_RESULT_FAIL:
196 ERROR("CPU%u failed the test\n", core_pos);
197 return TEST_RESULT_FAIL;
198
199 case TEST_RESULT_CRASHED:
200 /*
201 * Means the CPU never returned from the test whereas it
202 * was supposed to. Either there is a bug in the test's
203 * implementation or some sort of unexpected crash
204 * happened.
205 * If at least one CPU crashed, consider the whole test
206 * as crashed as well.
207 */
208 ERROR("CPU%u never returned from the test!\n", core_pos);
209 return TEST_RESULT_CRASHED;
210
211 default:
212 ERROR("Unknown test result value: %u\n",
213 test_results[core_pos]);
214 panic();
215 }
216 }
217
218 /*
219 * At least one CPU (i.e. the lead CPU) should have participated in the
220 * test.
221 */
222 assert(result != TEST_RESULT_NA);
223 return result;
224}
225
226/*
227 * This function is executed by the last CPU to exit the test only.
228 * It does the necessary bookkeeping and reports the overall test result.
229 * If it was the last test, it will also generate the final test report.
230 * Otherwise, it will reset the platform, provided that the platform
231 * supports reset from non-trusted world. This ensures that the next test
232 * runs in a clean environment
233 *
234 * Return 1 if this was the last test, 0 otherwise.
235 */
236static unsigned int close_test(void)
237{
238 const test_case_t *next_test;
239
240#if DEBUG
241 /*
242 * Check that the test didn't pretend resetting the platform, when in
243 * fact it returned into the framework.
244 *
245 * If that happens, the test implementation should be fixed.
246 * However, it is not a fatal error so just flag the problem in debug
247 * builds.
248 */
249 test_progress_t progress;
250 tftf_get_test_progress(&progress);
251 assert(progress != TEST_REBOOTING);
252#endif /* DEBUG */
253
254 tftf_set_test_progress(TEST_COMPLETE);
255 test_is_rebooting = 0;
256
257 /* TODO: Take a 2nd timestamp and compute test duration */
258
259 /* Reset watchdog */
260 tftf_platform_watchdog_reset();
261
262 /* Ensure no CPU is still executing the test */
263 assert(tftf_get_ref_cnt() == 0);
264
265 /* Save test result in NVM */
266 test_result_t overall_test_result = get_overall_test_result();
267 tftf_testcase_set_result(current_testcase(),
268 overall_test_result,
269 0);
270
271 NOTICE("Unittest '%s - %s' complete. Result: %s\n",
272 current_testsuite()->name, current_testcase()->name,
273 test_result_to_string(overall_test_result));
274
275 /* The test is finished, let's move to the next one (if any) */
276 next_test = advance_to_next_test();
277
278 /* If this was the last test then report all results */
279 if (!next_test) {
280 tftf_report_generate();
281 tftf_clean_nvm();
282 return 1;
283 } else {
284#if (PLAT_SUPPORTS_NS_RESET && !NEW_TEST_SESSION && USE_NVM)
285 /*
286 * Reset the platform so that the next test runs in a clean
287 * environment.
288 */
289 INFO("Reset platform before executing next test:%p\n",
290 (void *) &(next_test->test));
291 tftf_plat_reset();
292 bug_unreachable();
293#endif
294 }
295
296 return 0;
297}
298
299/*
300 * Hand over to lead CPU, i.e.:
301 * 1) Power on lead CPU
302 * 2) Power down calling CPU
303 */
304static void __dead2 hand_over_to_lead_cpu(void)
305{
306 int ret;
307 unsigned int mpid = read_mpidr_el1() & MPID_MASK;
308 unsigned int core_pos = platform_get_core_pos(mpid);
309
310 VERBOSE("CPU%u: Hand over to lead CPU%u\n", core_pos,
311 platform_get_core_pos(lead_cpu_mpid));
312
313 /*
314 * Power on lead CPU.
315 * The entry point address passed as the 2nd argument of tftf_cpu_on()
316 * doesn't matter because it will be overwritten by prepare_next_test().
317 * Pass a NULL pointer to easily catch the problem in case something
318 * goes wrong.
319 */
320 ret = tftf_cpu_on(lead_cpu_mpid, 0, 0);
321 if (ret != PSCI_E_SUCCESS) {
322 ERROR("CPU%u: Failed to power on lead CPU%u (%d)\n",
323 core_pos, platform_get_core_pos(lead_cpu_mpid), ret);
324 panic();
325 }
326
327 /* Wait for lead CPU to be actually powered on */
328 while (!tftf_is_cpu_online(lead_cpu_mpid))
329 ;
330
331 /*
332 * Lead CPU has successfully booted, let's now power down the calling
333 * core.
334 */
335 tftf_cpu_off();
336 panic();
337}
338
339void __dead2 run_tests(void)
340{
341 unsigned int mpid = read_mpidr_el1() & MPID_MASK;
342 unsigned int core_pos = platform_get_core_pos(mpid);
343 unsigned int test_session_finished;
344 unsigned int cpus_cnt;
345
346 while (1) {
347 if (mpid == lead_cpu_mpid && (tftf_get_ref_cnt() == 0))
348 prepare_next_test();
349
350 /*
351 * Increment the reference count to indicate that the CPU is
352 * participating in the test.
353 */
354 tftf_inc_ref_cnt();
355
356 /*
357 * Mark the CPU's test result as "crashed". This is meant to be
358 * overwritten by the actual test result when the CPU returns
359 * from the test function into the framework. In case the CPU
360 * crashes in the test (and thus, never returns from it), this
361 * variable will hold the right value.
362 */
363 test_results[core_pos] = TEST_RESULT_CRASHED;
364
365 /*
366 * Jump to the test entrypoint for this core.
367 * - For the lead CPU, it has been populated by
368 * prepare_next_test()
369 * - For other CPUs, it has been populated by tftf_cpu_on() or
370 * tftf_try_cpu_on()
371 */
372 while (test_entrypoint[core_pos] == 0)
373 ;
374
375 test_results[core_pos] = test_entrypoint[core_pos]();
376 test_entrypoint[core_pos] = 0;
377
378 /*
379 * Decrement the reference count to indicate that the CPU is not
380 * participating in the test any longer.
381 */
382 cpus_cnt = tftf_dec_ref_cnt();
383
384 /*
385 * Last CPU to exit the test gets to do the necessary
386 * bookkeeping and to report the overall test result.
387 * Other CPUs shut down.
388 */
389 if (cpus_cnt == 0) {
390 test_session_finished = close_test();
391 if (test_session_finished)
392 break;
393
394 if (mpid != lead_cpu_mpid) {
395 hand_over_to_lead_cpu();
396 bug_unreachable();
397 }
398 } else {
399 tftf_cpu_off();
400 panic();
401 }
402 }
403
404 tftf_exit();
405
406 /* Should never reach this point */
407 bug_unreachable();
408}
409
410u_register_t tftf_get_cpu_on_ctx_id(unsigned int core_pos)
411{
412 assert(core_pos < PLATFORM_CORE_COUNT);
413
414 return cpu_on_ctx_id_arr[core_pos];
415}
416
417void tftf_set_cpu_on_ctx_id(unsigned int core_pos, u_register_t context_id)
418{
419 assert(core_pos < PLATFORM_CORE_COUNT);
420
421 cpu_on_ctx_id_arr[core_pos] = context_id;
422}
423
424unsigned int tftf_is_rebooted(void)
425{
426 return test_is_rebooting;
427}
428
429/*
430 * Return 0 if the test session can be resumed
431 * -1 otherwise.
432 */
433static int resume_test_session(void)
434{
435 test_ref_t test_to_run;
436 test_progress_t test_progress;
437 const test_case_t *next_test;
438
439 /* Get back on our feet. Where did we stop? */
440 tftf_get_test_to_run(&test_to_run);
441 tftf_get_test_progress(&test_progress);
442 assert(TEST_PROGRESS_IS_VALID(test_progress));
443
444 switch (test_progress) {
445 case TEST_READY:
446 /*
447 * The TFTF has reset in the framework code, before the test
448 * actually started.
449 * Nothing to update, just start the test from scratch.
450 */
451 break;
452
453 case TEST_IN_PROGRESS:
454 /*
455 * The test crashed, i.e. it couldn't complete.
456 * Update the test result in NVM then move to the next test.
457 */
458 INFO("Test has crashed, moving to the next one\n");
459 tftf_testcase_set_result(current_testcase(),
460 TEST_RESULT_CRASHED,
461 0);
462 next_test = advance_to_next_test();
463 if (!next_test) {
464 INFO("No more tests\n");
465 return -1;
466 }
467 break;
468
469 case TEST_COMPLETE:
470 /*
471 * The TFTF has reset in the framework code, after the test had
472 * completed but before we finished the framework maintenance
473 * required to move to the next test.
474 *
475 * In this case, we don't know the exact state of the data:
476 * maybe we had the time to update the test result,
477 * maybe we had the time to move to the next test.
478 * We can't be sure so let's stay on the safe side and just
479 * restart the test session from the beginning...
480 */
481 NOTICE("The test framework has been interrupted in the middle "
482 "of critical maintenance operations.\n");
483 NOTICE("Can't recover execution.\n");
484 return -1;
485
486 case TEST_REBOOTING:
487 /*
488 * Nothing to update about the test session, as we want to
489 * re-enter the same test. Just remember that the test is
490 * rebooting in case it queries this information.
491 */
492 test_is_rebooting = 1;
493 break;
494
495 default:
496 bug_unreachable();
497 }
498
499 return 0;
500}
501
502/*
503 * C entry point in the TFTF.
504 * This function is executed by the primary CPU only.
505 */
506void __dead2 tftf_cold_boot_main(void)
507{
508 STATUS status;
509 int rc;
510
511 NOTICE("%s\n", TFTF_WELCOME_STR);
512 NOTICE("%s\n", build_message);
513 NOTICE("%s\n\n", version_string);
514
515#ifndef AARCH32
516 NOTICE("Running at NS-EL%u\n", IS_IN_EL(1) ? 1 : 2);
517#else
518 NOTICE("Running in AArch32 HYP mode\n");
519#endif
520
521 tftf_arch_setup();
522 tftf_platform_setup();
523 tftf_init_topology();
524
525 tftf_irq_setup();
526
527 rc = tftf_initialise_timer();
528 if (rc != 0) {
529 ERROR("Failed to initialize the timer subsystem (%d).\n", rc);
530 tftf_exit();
531 }
532
533 /* Enable the SGI used by the timer management framework */
534 tftf_irq_enable(IRQ_WAKE_SGI, GIC_HIGHEST_NS_PRIORITY);
535 enable_irq();
536
537 if (new_test_session()) {
538 NOTICE("Starting a new test session\n");
539 status = tftf_init_nvm();
540 if (status != STATUS_SUCCESS) {
541 /*
542 * TFTF will have an undetermined behavior if its data
543 * structures have not been initialised. There's no
544 * point in continuing execution.
545 */
546 ERROR("FATAL: Failed to initialise internal data structures in NVM.\n");
547 tftf_clean_nvm();
548 tftf_exit();
549 }
550 } else {
551 NOTICE("Resuming interrupted test session\n");
552 rc = resume_test_session();
553 if (rc < 0) {
554 tftf_report_generate();
555 tftf_clean_nvm();
556 tftf_exit();
557 }
558 }
559
560 /* Initialise the CPUs status map */
561 tftf_init_cpus_status_map();
562
563 /*
564 * Detect power state format and get power state information for
565 * a platform.
566 */
567 tftf_init_pstate_framework();
568
569 /* The lead CPU is always the primary core. */
570 lead_cpu_mpid = read_mpidr_el1() & MPID_MASK;
571
572 /*
573 * Hand over to lead CPU if required.
574 * If the primary CPU is not the lead CPU for the first test then:
575 * 1) Power on the lead CPU
576 * 2) Power down the primary CPU
577 */
578 if ((read_mpidr_el1() & MPID_MASK) != lead_cpu_mpid) {
579 hand_over_to_lead_cpu();
580 bug_unreachable();
581 }
582
583 /* Enter the test session */
584 run_tests();
585
586 /* Should never reach this point */
587 bug_unreachable();
588}
589
590void __dead2 tftf_exit(void)
591{
592 NOTICE("Exiting tests.\n");
593
594 /* Let the platform code clean up if required */
595 tftf_platform_end();
596
597 while (1)
598 wfi();
599}