blob: ea56f2ebfe68f6991e6a29ba5763cacc3944c9ee [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
Sandrine Bailleux125d58c2018-11-07 17:11:59 +0100151 /* If we're starting a new testsuite, announce it. */
152 test_ref_t test_to_run;
153 tftf_get_test_to_run(&test_to_run);
154 if (test_to_run.testcase_idx == 0) {
155 print_testsuite_start(current_testsuite());
156 }
157
158 print_test_start(current_testcase());
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200159
160 /* Program the watchdog */
161 tftf_platform_watchdog_set();
162
163 /* TODO: Take a 1st timestamp to be able to measure test duration */
164
165 tftf_set_test_progress(TEST_IN_PROGRESS);
166}
167
168/*
169 * Go through individual CPUs' test results and determine the overall
170 * test result from that.
171 */
172static test_result_t get_overall_test_result(void)
173{
174 test_result_t result = TEST_RESULT_NA;
175 unsigned int cpu_mpid;
176 unsigned int cpu_node;
177 unsigned int core_pos;
178
179 for_each_cpu(cpu_node) {
180 cpu_mpid = tftf_get_mpidr_from_node(cpu_node);
181 core_pos = platform_get_core_pos(cpu_mpid);
182
183 switch (test_results[core_pos]) {
184 case TEST_RESULT_NA:
185 VERBOSE("CPU%u not involved in the test\n", core_pos);
186 /* Ignoring */
187 break;
188
189 case TEST_RESULT_SKIPPED:
190 /*
191 * If at least one CPU skipped the test, consider the
192 * whole test as skipped as well.
193 */
194 NOTICE("CPU%u skipped the test\n", core_pos);
195 return TEST_RESULT_SKIPPED;
196
197 case TEST_RESULT_SUCCESS:
198 result = TEST_RESULT_SUCCESS;
199 break;
200
201 case TEST_RESULT_FAIL:
202 ERROR("CPU%u failed the test\n", core_pos);
203 return TEST_RESULT_FAIL;
204
205 case TEST_RESULT_CRASHED:
206 /*
207 * Means the CPU never returned from the test whereas it
208 * was supposed to. Either there is a bug in the test's
209 * implementation or some sort of unexpected crash
210 * happened.
211 * If at least one CPU crashed, consider the whole test
212 * as crashed as well.
213 */
214 ERROR("CPU%u never returned from the test!\n", core_pos);
215 return TEST_RESULT_CRASHED;
216
217 default:
218 ERROR("Unknown test result value: %u\n",
219 test_results[core_pos]);
220 panic();
221 }
222 }
223
224 /*
225 * At least one CPU (i.e. the lead CPU) should have participated in the
226 * test.
227 */
228 assert(result != TEST_RESULT_NA);
229 return result;
230}
231
232/*
233 * This function is executed by the last CPU to exit the test only.
234 * It does the necessary bookkeeping and reports the overall test result.
235 * If it was the last test, it will also generate the final test report.
236 * Otherwise, it will reset the platform, provided that the platform
237 * supports reset from non-trusted world. This ensures that the next test
238 * runs in a clean environment
239 *
240 * Return 1 if this was the last test, 0 otherwise.
241 */
242static unsigned int close_test(void)
243{
244 const test_case_t *next_test;
245
246#if DEBUG
247 /*
248 * Check that the test didn't pretend resetting the platform, when in
249 * fact it returned into the framework.
250 *
251 * If that happens, the test implementation should be fixed.
252 * However, it is not a fatal error so just flag the problem in debug
253 * builds.
254 */
255 test_progress_t progress;
256 tftf_get_test_progress(&progress);
257 assert(progress != TEST_REBOOTING);
258#endif /* DEBUG */
259
260 tftf_set_test_progress(TEST_COMPLETE);
261 test_is_rebooting = 0;
262
263 /* TODO: Take a 2nd timestamp and compute test duration */
264
265 /* Reset watchdog */
266 tftf_platform_watchdog_reset();
267
268 /* Ensure no CPU is still executing the test */
269 assert(tftf_get_ref_cnt() == 0);
270
271 /* Save test result in NVM */
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200272 tftf_testcase_set_result(current_testcase(),
Sandrine Bailleux125d58c2018-11-07 17:11:59 +0100273 get_overall_test_result(),
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200274 0);
275
Sandrine Bailleux125d58c2018-11-07 17:11:59 +0100276 print_test_end(current_testcase());
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200277
278 /* The test is finished, let's move to the next one (if any) */
279 next_test = advance_to_next_test();
280
281 /* If this was the last test then report all results */
282 if (!next_test) {
Sandrine Bailleux125d58c2018-11-07 17:11:59 +0100283 print_tests_summary();
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200284 tftf_clean_nvm();
285 return 1;
286 } else {
287#if (PLAT_SUPPORTS_NS_RESET && !NEW_TEST_SESSION && USE_NVM)
288 /*
289 * Reset the platform so that the next test runs in a clean
290 * environment.
291 */
292 INFO("Reset platform before executing next test:%p\n",
293 (void *) &(next_test->test));
294 tftf_plat_reset();
295 bug_unreachable();
296#endif
297 }
298
299 return 0;
300}
301
302/*
303 * Hand over to lead CPU, i.e.:
304 * 1) Power on lead CPU
305 * 2) Power down calling CPU
306 */
307static void __dead2 hand_over_to_lead_cpu(void)
308{
309 int ret;
310 unsigned int mpid = read_mpidr_el1() & MPID_MASK;
311 unsigned int core_pos = platform_get_core_pos(mpid);
312
313 VERBOSE("CPU%u: Hand over to lead CPU%u\n", core_pos,
314 platform_get_core_pos(lead_cpu_mpid));
315
316 /*
317 * Power on lead CPU.
318 * The entry point address passed as the 2nd argument of tftf_cpu_on()
319 * doesn't matter because it will be overwritten by prepare_next_test().
320 * Pass a NULL pointer to easily catch the problem in case something
321 * goes wrong.
322 */
323 ret = tftf_cpu_on(lead_cpu_mpid, 0, 0);
324 if (ret != PSCI_E_SUCCESS) {
325 ERROR("CPU%u: Failed to power on lead CPU%u (%d)\n",
326 core_pos, platform_get_core_pos(lead_cpu_mpid), ret);
327 panic();
328 }
329
330 /* Wait for lead CPU to be actually powered on */
331 while (!tftf_is_cpu_online(lead_cpu_mpid))
332 ;
333
334 /*
335 * Lead CPU has successfully booted, let's now power down the calling
336 * core.
337 */
338 tftf_cpu_off();
339 panic();
340}
341
342void __dead2 run_tests(void)
343{
344 unsigned int mpid = read_mpidr_el1() & MPID_MASK;
345 unsigned int core_pos = platform_get_core_pos(mpid);
346 unsigned int test_session_finished;
347 unsigned int cpus_cnt;
348
349 while (1) {
350 if (mpid == lead_cpu_mpid && (tftf_get_ref_cnt() == 0))
351 prepare_next_test();
352
353 /*
354 * Increment the reference count to indicate that the CPU is
355 * participating in the test.
356 */
357 tftf_inc_ref_cnt();
358
359 /*
360 * Mark the CPU's test result as "crashed". This is meant to be
361 * overwritten by the actual test result when the CPU returns
362 * from the test function into the framework. In case the CPU
363 * crashes in the test (and thus, never returns from it), this
364 * variable will hold the right value.
365 */
366 test_results[core_pos] = TEST_RESULT_CRASHED;
367
368 /*
369 * Jump to the test entrypoint for this core.
370 * - For the lead CPU, it has been populated by
371 * prepare_next_test()
372 * - For other CPUs, it has been populated by tftf_cpu_on() or
373 * tftf_try_cpu_on()
374 */
375 while (test_entrypoint[core_pos] == 0)
376 ;
377
378 test_results[core_pos] = test_entrypoint[core_pos]();
379 test_entrypoint[core_pos] = 0;
380
381 /*
382 * Decrement the reference count to indicate that the CPU is not
383 * participating in the test any longer.
384 */
385 cpus_cnt = tftf_dec_ref_cnt();
386
387 /*
388 * Last CPU to exit the test gets to do the necessary
389 * bookkeeping and to report the overall test result.
390 * Other CPUs shut down.
391 */
392 if (cpus_cnt == 0) {
393 test_session_finished = close_test();
394 if (test_session_finished)
395 break;
396
397 if (mpid != lead_cpu_mpid) {
398 hand_over_to_lead_cpu();
399 bug_unreachable();
400 }
401 } else {
402 tftf_cpu_off();
403 panic();
404 }
405 }
406
407 tftf_exit();
408
409 /* Should never reach this point */
410 bug_unreachable();
411}
412
413u_register_t tftf_get_cpu_on_ctx_id(unsigned int core_pos)
414{
415 assert(core_pos < PLATFORM_CORE_COUNT);
416
417 return cpu_on_ctx_id_arr[core_pos];
418}
419
420void tftf_set_cpu_on_ctx_id(unsigned int core_pos, u_register_t context_id)
421{
422 assert(core_pos < PLATFORM_CORE_COUNT);
423
424 cpu_on_ctx_id_arr[core_pos] = context_id;
425}
426
427unsigned int tftf_is_rebooted(void)
428{
429 return test_is_rebooting;
430}
431
432/*
433 * Return 0 if the test session can be resumed
434 * -1 otherwise.
435 */
436static int resume_test_session(void)
437{
438 test_ref_t test_to_run;
439 test_progress_t test_progress;
440 const test_case_t *next_test;
441
442 /* Get back on our feet. Where did we stop? */
443 tftf_get_test_to_run(&test_to_run);
444 tftf_get_test_progress(&test_progress);
445 assert(TEST_PROGRESS_IS_VALID(test_progress));
446
447 switch (test_progress) {
448 case TEST_READY:
449 /*
450 * The TFTF has reset in the framework code, before the test
451 * actually started.
452 * Nothing to update, just start the test from scratch.
453 */
454 break;
455
456 case TEST_IN_PROGRESS:
457 /*
458 * The test crashed, i.e. it couldn't complete.
459 * Update the test result in NVM then move to the next test.
460 */
461 INFO("Test has crashed, moving to the next one\n");
462 tftf_testcase_set_result(current_testcase(),
463 TEST_RESULT_CRASHED,
464 0);
465 next_test = advance_to_next_test();
466 if (!next_test) {
467 INFO("No more tests\n");
468 return -1;
469 }
470 break;
471
472 case TEST_COMPLETE:
473 /*
474 * The TFTF has reset in the framework code, after the test had
475 * completed but before we finished the framework maintenance
476 * required to move to the next test.
477 *
478 * In this case, we don't know the exact state of the data:
479 * maybe we had the time to update the test result,
480 * maybe we had the time to move to the next test.
481 * We can't be sure so let's stay on the safe side and just
482 * restart the test session from the beginning...
483 */
484 NOTICE("The test framework has been interrupted in the middle "
485 "of critical maintenance operations.\n");
486 NOTICE("Can't recover execution.\n");
487 return -1;
488
489 case TEST_REBOOTING:
490 /*
491 * Nothing to update about the test session, as we want to
492 * re-enter the same test. Just remember that the test is
493 * rebooting in case it queries this information.
494 */
495 test_is_rebooting = 1;
496 break;
497
498 default:
499 bug_unreachable();
500 }
501
502 return 0;
503}
504
505/*
506 * C entry point in the TFTF.
507 * This function is executed by the primary CPU only.
508 */
509void __dead2 tftf_cold_boot_main(void)
510{
511 STATUS status;
512 int rc;
513
514 NOTICE("%s\n", TFTF_WELCOME_STR);
515 NOTICE("%s\n", build_message);
516 NOTICE("%s\n\n", version_string);
517
518#ifndef AARCH32
519 NOTICE("Running at NS-EL%u\n", IS_IN_EL(1) ? 1 : 2);
520#else
521 NOTICE("Running in AArch32 HYP mode\n");
522#endif
523
524 tftf_arch_setup();
525 tftf_platform_setup();
526 tftf_init_topology();
527
528 tftf_irq_setup();
529
530 rc = tftf_initialise_timer();
531 if (rc != 0) {
532 ERROR("Failed to initialize the timer subsystem (%d).\n", rc);
533 tftf_exit();
534 }
535
536 /* Enable the SGI used by the timer management framework */
537 tftf_irq_enable(IRQ_WAKE_SGI, GIC_HIGHEST_NS_PRIORITY);
538 enable_irq();
539
540 if (new_test_session()) {
541 NOTICE("Starting a new test session\n");
542 status = tftf_init_nvm();
543 if (status != STATUS_SUCCESS) {
544 /*
545 * TFTF will have an undetermined behavior if its data
546 * structures have not been initialised. There's no
547 * point in continuing execution.
548 */
549 ERROR("FATAL: Failed to initialise internal data structures in NVM.\n");
550 tftf_clean_nvm();
551 tftf_exit();
552 }
553 } else {
554 NOTICE("Resuming interrupted test session\n");
555 rc = resume_test_session();
556 if (rc < 0) {
Sandrine Bailleux125d58c2018-11-07 17:11:59 +0100557 print_tests_summary();
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200558 tftf_clean_nvm();
559 tftf_exit();
560 }
561 }
562
563 /* Initialise the CPUs status map */
564 tftf_init_cpus_status_map();
565
566 /*
567 * Detect power state format and get power state information for
568 * a platform.
569 */
570 tftf_init_pstate_framework();
571
572 /* The lead CPU is always the primary core. */
573 lead_cpu_mpid = read_mpidr_el1() & MPID_MASK;
574
575 /*
576 * Hand over to lead CPU if required.
577 * If the primary CPU is not the lead CPU for the first test then:
578 * 1) Power on the lead CPU
579 * 2) Power down the primary CPU
580 */
581 if ((read_mpidr_el1() & MPID_MASK) != lead_cpu_mpid) {
582 hand_over_to_lead_cpu();
583 bug_unreachable();
584 }
585
586 /* Enter the test session */
587 run_tests();
588
589 /* Should never reach this point */
590 bug_unreachable();
591}
592
593void __dead2 tftf_exit(void)
594{
595 NOTICE("Exiting tests.\n");
596
597 /* Let the platform code clean up if required */
598 tftf_platform_end();
599
600 while (1)
601 wfi();
602}