更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
los_init.c
浏览该文件的文档.
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 */
48OS_INIT_LEVEL_REG(kernel, 10, g_kernInitLevelList);
49
50STATIC volatile UINT32 g_initCurrentLevel = OS_INVALID_VALUE; //当前等级
51STATIC volatile struct ModuleInitInfo *g_initCurrentModule = 0; //当前模块
53STATIC SPIN_LOCK_INIT(g_initLock);
54
55/**
56 * It is recommended that each startup framework encapsulate a layer of its own calling interface.
57 * 建议每个启动框架都封装一层自己的调用接口
58 */
59STATIC VOID InitLevelCall(const CHAR *name, const UINT32 level, struct ModuleInitInfo *initLevelList[])
60{
61 struct ModuleInitInfo *module = NULL;
62#ifdef LOS_INIT_DEBUG
63 UINT64 startNsec, endNsec;
64 UINT64 totalTime = 0;
65 UINT64 singleTime;
66 UINT32 ret = LOS_OK;
67#endif
68
69 if (ArchCurrCpuid() == 0) {//主CPU
70#ifdef LOS_INIT_DEBUG
71 PRINTK("-------- %s Module Init... level = %u --------\n", name, level);
72#endif
73 g_initCurrentLevel = level;
74 g_initCurrentModule = initLevelList[level];
75 } else {
76 while (g_initCurrentLevel < level) {
77 }
78 }
79
80 do {
81 LOS_SpinLock(&g_initLock);
82 if (g_initCurrentModule >= initLevelList[level + 1]) {
83 LOS_SpinUnlock(&g_initLock);
84 break;
85 }
86 module = (struct ModuleInitInfo *)g_initCurrentModule;
88 LOS_SpinUnlock(&g_initLock);
89 if (module->hook != NULL) {
90#ifdef LOS_INIT_DEBUG
91 ret = LOS_OK;
92 startNsec = LOS_CurrNanosec();
93 ret = (UINT32)module->hook();
94 endNsec = LOS_CurrNanosec();
95 singleTime = endNsec - startNsec;
96 totalTime += singleTime;
97 PRINTK("Starting %s module consumes %llu ns. Run on cpu %u\n", module->name, singleTime, ArchCurrCpuid());
98#else
99 module->hook();
100#endif
101 }
102#ifdef LOS_INIT_DEBUG
103 if (ret != LOS_OK) {
104 PRINT_ERR("%s initialization failed at module %s, function addr at 0x%x, ret code is %u\n",
105 name, module->name, module->hook, ret);
106 }
107#endif
108 } while (1);
109
110 if (level >= LOS_INIT_LEVEL_KMOD_TASK) {
112 while ((LOS_AtomicRead(&g_initCount) % LOSCFG_KERNEL_CORE_NUM) != 0) {
113 }
114 }
115
116#ifdef LOS_INIT_DEBUG
117 PRINTK("%s initialization at level %u consumes %lluns on cpu %u.\n", name, level, totalTime, ArchCurrCpuid());
118#endif
119}
120
121/**
122 * @brief 初始化调用日志打印,这个函数的功能主要是为了记录某个步骤的耗时.在一个函数前后各调用一次就知道函数的执行情况.
123这为开机调试提供了很重要的日志依据.
124 * @param level
125 * @return VOID
126 */
127VOID OsInitCall(const UINT32 level)
128{
129 if (level >= LOS_INIT_LEVEL_FINISH) {
130 return;
131 }
132
133 InitLevelCall("Kernel", level, g_kernInitLevelList);
134}
STATIC INLINE VOID LOS_AtomicInc(Atomic *v)
Atomic addSelf.
Definition: los_atomic.h:253
STATIC INLINE INT32 LOS_AtomicRead(const Atomic *v)
Atomic read. | 读取32bit原子数据
Definition: los_atomic.h:123
LITE_OS_SEC_TEXT_MINOR UINT64 LOS_CurrNanosec(VOID)
获取自系统启动以来的纳秒数
Definition: los_hw_tick.c:62
原子操作 http://weharmonyos.com/openharmony/zh-cn/device-dev/kernel/kernel-small-basic-atomic....
volatile INT32 Atomic
Definition: los_atomic.h:102
STATIC INLINE UINT32 ArchCurrCpuid(VOID)
Definition: los_hw_cpu.h:168
STATIC VOID InitLevelCall(const CHAR *name, const UINT32 level, struct ModuleInitInfo *initLevelList[])
Definition: los_init.c:59
STATIC SPIN_LOCK_INIT(g_initLock)
VOID OsInitCall(const UINT32 level)
初始化调用日志打印,这个函数的功能主要是为了记录某个步骤的耗时.在一个函数前后各调用一次就知道函数的执行情况. 这为开机调试提供了很重要的日志依据.
Definition: los_init.c:127
STATIC volatile UINT32 g_initCurrentLevel
Definition: los_init.c:50
STATIC volatile struct ModuleInitInfo * g_initCurrentModule
Definition: los_init.c:51
OS_INIT_LEVEL_REG(kernel, 10, g_kernInitLevelList)
STATIC Atomic g_initCount
Definition: los_init.c:52
VOID LOS_SpinLock(SPIN_LOCK_S *lock)
Definition: los_spinlock.c:50
VOID LOS_SpinUnlock(SPIN_LOCK_S *lock)
Definition: los_spinlock.c:84
long unsigned int UINT64
Definition: los_typedef.h:66
unsigned int UINT32
Definition: los_typedef.h:57
char CHAR
Definition: los_typedef.h:63
OsInitHook hook
函数指针,钩子函数
Definition: los_init_info.h:65
const CHAR * name
Definition: los_init_info.h:67