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_init_pri.h"
33 #include "los_atomic.h"
34 #include "los_config.h"
35 #include "los_hw.h"
36 #include "los_printf.h"
37 #include "los_spinlock.h"
38 #include "los_typedef.h"
39
40 #ifdef LOS_INIT_DEBUG
41 #include "los_sys_pri.h"
42 #include "los_tick.h"
43 #endif
44
45 /**
46 * Register kernel init level labels.
47 */
48 OS_INIT_LEVEL_REG(kernel, 10, g_kernInitLevelList);
49
50 STATIC volatile UINT32 g_initCurrentLevel = OS_INVALID_VALUE;
51 STATIC volatile struct ModuleInitInfo *g_initCurrentModule = 0;
52 STATIC Atomic g_initCount = 0;
53 STATIC SPIN_LOCK_INIT(g_initLock);
54
55 /**
56 * It is recommended that each startup framework encapsulate a layer of its own calling interface.
57 */
InitLevelCall(const CHAR * name,const UINT32 level,struct ModuleInitInfo * initLevelList[])58 STATIC VOID InitLevelCall(const CHAR *name, const UINT32 level, struct ModuleInitInfo *initLevelList[])
59 {
60 struct ModuleInitInfo *module = NULL;
61 #ifdef LOS_INIT_DEBUG
62 UINT64 startNsec, endNsec;
63 UINT64 totalTime = 0;
64 UINT64 singleTime;
65 UINT32 ret = LOS_OK;
66 #endif
67
68 if (ArchCurrCpuid() == 0) {
69 #ifdef LOS_INIT_DEBUG
70 PRINTK("-------- %s Module Init... level = %u --------\n", name, level);
71 #endif
72 g_initCurrentLevel = level;
73 g_initCurrentModule = initLevelList[level];
74 } else {
75 while (g_initCurrentLevel < level) {
76 }
77 }
78
79 do {
80 LOS_SpinLock(&g_initLock);
81 if (g_initCurrentModule >= initLevelList[level + 1]) {
82 LOS_SpinUnlock(&g_initLock);
83 break;
84 }
85 module = (struct ModuleInitInfo *)g_initCurrentModule;
86 g_initCurrentModule++;
87 LOS_SpinUnlock(&g_initLock);
88 if (module->hook != NULL) {
89 #ifdef LOS_INIT_DEBUG
90 ret = LOS_OK;
91 startNsec = LOS_CurrNanosec();
92 ret = (UINT32)module->hook();
93 endNsec = LOS_CurrNanosec();
94 singleTime = endNsec - startNsec;
95 totalTime += singleTime;
96 PRINTK("Starting %s module consumes %llu ns. Run on cpu %u\n", module->name, singleTime, ArchCurrCpuid());
97 #else
98 module->hook();
99 #endif
100 }
101 #ifdef LOS_INIT_DEBUG
102 if (ret != LOS_OK) {
103 PRINT_ERR("%s initialization failed at module %s, function addr at 0x%x, ret code is %u\n",
104 name, module->name, module->hook, ret);
105 }
106 #endif
107 } while (1);
108
109 if (level >= LOS_INIT_LEVEL_KMOD_TASK) {
110 LOS_AtomicInc(&g_initCount);
111 while ((LOS_AtomicRead(&g_initCount) % LOSCFG_KERNEL_CORE_NUM) != 0) {
112 }
113 }
114
115 #ifdef LOS_INIT_DEBUG
116 PRINTK("%s initialization at level %u consumes %lluns on cpu %u.\n", name, level, totalTime, ArchCurrCpuid());
117 #endif
118 }
119
OsInitCall(const UINT32 level)120 VOID OsInitCall(const UINT32 level)
121 {
122 if (level >= LOS_INIT_LEVEL_FINISH) {
123 return;
124 }
125
126 InitLevelCall("Kernel", level, g_kernInitLevelList);
127 }
128