1 /*
2  * Copyright (c) 2023 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdbool.h>
8 #include "nsi_config.h"
9 #include "nsi_cpun_if.h"
10 #include "nsi_tracing.h"
11 
12 static bool cpu_auto_start[NSI_N_CPUS] = {true}; /* Only the first core starts on its own */
13 static bool cpu_booted[NSI_N_CPUS];
14 
15 #define CPU_N_RANGE_CHECK(cpu_n) \
16 	if (cpu_n >= NSI_N_CPUS) { \
17 		nsi_print_error_and_exit("%s called with cpu_n(%i) >= NSI_N_CPUS (%i)\n", \
18 					 __func__, cpu_n, NSI_N_CPUS); \
19 	}
20 
nsi_cpu_set_auto_start(int cpu_n,bool auto_start)21 void nsi_cpu_set_auto_start(int cpu_n, bool auto_start)
22 {
23 	CPU_N_RANGE_CHECK(cpu_n);
24 
25 	cpu_auto_start[cpu_n] = auto_start;
26 }
27 
nsi_cpu_get_auto_start(int cpu_n)28 bool nsi_cpu_get_auto_start(int cpu_n)
29 {
30 	return cpu_auto_start[cpu_n];
31 }
32 
nsi_cpu_auto_boot(void)33 void nsi_cpu_auto_boot(void)
34 {
35 	for (int i = 0; i < NSI_N_CPUS; i++) {
36 		if (cpu_auto_start[i] == true) {
37 			cpu_booted[i] = true;
38 			nsif_cpun_boot(i);
39 		}
40 	}
41 }
42 
nsi_cpu_boot(int cpu_n)43 void nsi_cpu_boot(int cpu_n)
44 {
45 	CPU_N_RANGE_CHECK(cpu_n);
46 	if (cpu_booted[cpu_n]) {
47 		nsi_print_warning("%s called with cpu_n(%i) which was already booted\n",
48 				  __func__, cpu_n);
49 	}
50 	cpu_booted[cpu_n] = true;
51 	nsif_cpun_boot(cpu_n);
52 }
53