1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "los_config.h"
33 #include "stdio.h"
34 #include "string.h"
35 #include "gic_common.h"
36 #include "los_atomic.h"
37 #include "los_exc_pri.h"
38 #include "los_hwi_pri.h"
39 #include "los_hw_tick_pri.h"
40 #include "los_init_pri.h"
41 #include "los_memory_pri.h"
42 #include "los_mp.h"
43 #include "los_mux_pri.h"
44 #include "los_printf.h"
45 #include "los_process_pri.h"
46 #include "los_queue_pri.h"
47 #include "los_sem_pri.h"
48 #include "los_spinlock.h"
49 #include "los_swtmr_pri.h"
50 #include "los_task_pri.h"
51 #include "los_sched_pri.h"
52 #include "los_tick.h"
53 #include "los_vm_boot.h"
54 #include "los_smp.h"
55
56 STATIC SystemRebootFunc g_rebootHook = NULL;
57
OsSetRebootHook(SystemRebootFunc func)58 VOID OsSetRebootHook(SystemRebootFunc func)
59 {
60 g_rebootHook = func;
61 }
62
OsGetRebootHook(VOID)63 SystemRebootFunc OsGetRebootHook(VOID)
64 {
65 return g_rebootHook;
66 }
67
EarliestInit(VOID)68 LITE_OS_SEC_TEXT_INIT STATIC UINT32 EarliestInit(VOID)
69 {
70 /* Must be placed at the beginning of the boot process */
71 OsSetMainTask();
72 OsCurrTaskSet(OsGetMainTask());
73 OsSchedRunqueueInit();
74
75 g_sysClock = OS_SYS_CLOCK;
76 g_tickPerSecond = LOSCFG_BASE_CORE_TICK_PER_SECOND;
77
78 return LOS_OK;
79 }
80
ArchEarlyInit(VOID)81 LITE_OS_SEC_TEXT_INIT STATIC UINT32 ArchEarlyInit(VOID)
82 {
83 UINT32 ret;
84
85 /* set system counter freq */
86 #ifndef LOSCFG_TEE_ENABLE
87 HalClockFreqWrite(OS_SYS_CLOCK);
88 #endif
89
90 #ifdef LOSCFG_PLATFORM_HWI
91 OsHwiInit();
92 #endif
93
94 OsExcInit();
95
96 ret = OsTickInit(g_sysClock, LOSCFG_BASE_CORE_TICK_PER_SECOND);
97 if (ret != LOS_OK) {
98 PRINT_ERR("OsTickInit error!\n");
99 return ret;
100 }
101
102 return LOS_OK;
103 }
104
PlatformEarlyInit(VOID)105 LITE_OS_SEC_TEXT_INIT STATIC UINT32 PlatformEarlyInit(VOID)
106 {
107 #if defined(LOSCFG_PLATFORM_UART_WITHOUT_VFS) && defined(LOSCFG_DRIVERS)
108 uart_init();
109 #endif /* LOSCFG_PLATFORM_UART_WITHOUT_VFS */
110
111 return LOS_OK;
112 }
113
OsIpcInit(VOID)114 LITE_OS_SEC_TEXT_INIT STATIC UINT32 OsIpcInit(VOID)
115 {
116 UINT32 ret;
117
118 #ifdef LOSCFG_BASE_IPC_SEM
119 ret = OsSemInit();
120 if (ret != LOS_OK) {
121 PRINT_ERR("OsSemInit error\n");
122 return ret;
123 }
124 #endif
125
126 #ifdef LOSCFG_BASE_IPC_QUEUE
127 ret = OsQueueInit();
128 if (ret != LOS_OK) {
129 PRINT_ERR("OsQueueInit error\n");
130 return ret;
131 }
132 #endif
133 return LOS_OK;
134 }
135
ArchInit(VOID)136 LITE_OS_SEC_TEXT_INIT STATIC UINT32 ArchInit(VOID)
137 {
138 #ifdef LOSCFG_KERNEL_MMU
139 OsArchMmuInitPerCPU();
140 #endif
141 return LOS_OK;
142 }
143
PlatformInit(VOID)144 LITE_OS_SEC_TEXT_INIT STATIC UINT32 PlatformInit(VOID)
145 {
146 return LOS_OK;
147 }
148
KModInit(VOID)149 LITE_OS_SEC_TEXT_INIT STATIC UINT32 KModInit(VOID)
150 {
151 #ifdef LOSCFG_BASE_CORE_SWTMR_ENABLE
152 OsSwtmrInit();
153 #endif
154 return LOS_OK;
155 }
156
OsSystemInfo(VOID)157 LITE_OS_SEC_TEXT_INIT VOID OsSystemInfo(VOID)
158 {
159 #ifdef LOSCFG_DEBUG_VERSION
160 const CHAR *buildType = "debug";
161 #else
162 const CHAR *buildType = "release";
163 #endif /* LOSCFG_DEBUG_VERSION */
164
165 PRINT_RELEASE("\n******************Welcome******************\n\n"
166 "Processor : %s"
167 #ifdef LOSCFG_KERNEL_SMP
168 " * %d\n"
169 "Run Mode : SMP\n"
170 #else
171 "\n"
172 "Run Mode : UP\n"
173 #endif
174 "GIC Rev : %s\n"
175 "build time : %s %s\n"
176 "Kernel : %s %d.%d.%d.%d/%s\n"
177 "\n*******************************************\n",
178 LOS_CpuInfo(),
179 #ifdef LOSCFG_KERNEL_SMP
180 LOSCFG_KERNEL_SMP_CORE_NUM,
181 #endif
182 HalIrqVersion(), __DATE__, __TIME__, \
183 KERNEL_NAME, KERNEL_MAJOR, KERNEL_MINOR, KERNEL_PATCH, KERNEL_ITRE, buildType);
184 }
185
OsMain(VOID)186 LITE_OS_SEC_TEXT_INIT UINT32 OsMain(VOID)
187 {
188 UINT32 ret;
189 #ifdef LOS_INIT_STATISTICS
190 UINT64 startNsec, endNsec, durationUsec;
191 #endif
192
193 ret = EarliestInit();
194 if (ret != LOS_OK) {
195 return ret;
196 }
197 OsInitCall(LOS_INIT_LEVEL_EARLIEST);
198
199 ret = ArchEarlyInit();
200 if (ret != LOS_OK) {
201 return ret;
202 }
203 OsInitCall(LOS_INIT_LEVEL_ARCH_EARLY);
204
205 ret = PlatformEarlyInit();
206 if (ret != LOS_OK) {
207 return ret;
208 }
209 OsInitCall(LOS_INIT_LEVEL_PLATFORM_EARLY);
210
211 /* system and chip info */
212 OsSystemInfo();
213
214 PRINT_RELEASE("\nmain core booting up...\n");
215
216 #ifdef LOS_INIT_STATISTICS
217 startNsec = LOS_CurrNanosec();
218 #endif
219
220 ret = OsTaskInit();
221 if (ret != LOS_OK) {
222 return ret;
223 }
224
225 OsInitCall(LOS_INIT_LEVEL_KMOD_PREVM);
226
227 ret = OsSysMemInit();
228 if (ret != LOS_OK) {
229 return ret;
230 }
231
232 OsInitCall(LOS_INIT_LEVEL_VM_COMPLETE);
233
234 ret = OsIpcInit();
235 if (ret != LOS_OK) {
236 return ret;
237 }
238
239 ret = OsSystemProcessCreate();
240 if (ret != LOS_OK) {
241 return ret;
242 }
243
244 ret = ArchInit();
245 if (ret != LOS_OK) {
246 return ret;
247 }
248 OsInitCall(LOS_INIT_LEVEL_ARCH);
249
250 ret = PlatformInit();
251 if (ret != LOS_OK) {
252 return ret;
253 }
254 OsInitCall(LOS_INIT_LEVEL_PLATFORM);
255
256 ret = KModInit();
257 if (ret != LOS_OK) {
258 return ret;
259 }
260
261 OsInitCall(LOS_INIT_LEVEL_KMOD_BASIC);
262
263 OsInitCall(LOS_INIT_LEVEL_KMOD_EXTENDED);
264
265 #ifdef LOSCFG_KERNEL_SMP
266 OsSmpInit();
267 #endif
268
269 OsInitCall(LOS_INIT_LEVEL_KMOD_TASK);
270
271 #ifdef LOS_INIT_STATISTICS
272 endNsec = LOS_CurrNanosec();
273 durationUsec = (endNsec - startNsec) / OS_SYS_NS_PER_US;
274 PRINTK("The main core takes %lluus to start.\n", durationUsec);
275 #endif
276
277 return LOS_OK;
278 }
279
280 #ifndef LOSCFG_PLATFORM_ADAPT
SystemInit(VOID)281 STATIC VOID SystemInit(VOID)
282 {
283 PRINTK("dummy: *** %s ***\n", __FUNCTION__);
284 }
285 #else
286 extern VOID SystemInit(VOID);
287 #endif
288
289 #ifndef LOSCFG_ENABLE_KERNEL_TEST
OsSystemInitTaskCreate(VOID)290 STATIC UINT32 OsSystemInitTaskCreate(VOID)
291 {
292 UINT32 taskID;
293 TSK_INIT_PARAM_S sysTask;
294
295 (VOID)memset_s(&sysTask, sizeof(TSK_INIT_PARAM_S), 0, sizeof(TSK_INIT_PARAM_S));
296 sysTask.pfnTaskEntry = (TSK_ENTRY_FUNC)SystemInit;
297 sysTask.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
298 sysTask.pcName = "SystemInit";
299 sysTask.usTaskPrio = LOSCFG_BASE_CORE_TSK_DEFAULT_PRIO;
300 sysTask.uwResved = LOS_TASK_STATUS_DETACHED;
301 #ifdef LOSCFG_KERNEL_SMP
302 sysTask.usCpuAffiMask = CPUID_TO_AFFI_MASK(ArchCurrCpuid());
303 #endif
304 return LOS_TaskCreate(&taskID, &sysTask);
305 }
306
OsSystemInit(VOID)307 STATIC UINT32 OsSystemInit(VOID)
308 {
309 UINT32 ret;
310
311 ret = OsSystemInitTaskCreate();
312 if (ret != LOS_OK) {
313 return ret;
314 }
315
316 return 0;
317 }
318
319 LOS_MODULE_INIT(OsSystemInit, LOS_INIT_LEVEL_KMOD_TASK);
320 #endif
321