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_mp.h"
33 #include "los_init.h"
34 #include "los_percpu_pri.h"
35 #include "los_sched_pri.h"
36 #include "los_swtmr.h"
37 #include "los_task_pri.h"
38
39 #ifdef LOSCFG_KERNEL_SMP
40
41 #ifdef LOSCFG_KERNEL_SMP_CALL
42 LITE_OS_SEC_BSS SPIN_LOCK_INIT(g_mpCallSpin);
43 #define MP_CALL_LOCK(state) LOS_SpinLockSave(&g_mpCallSpin, &(state))
44 #define MP_CALL_UNLOCK(state) LOS_SpinUnlockRestore(&g_mpCallSpin, (state))
45 #endif
46
LOS_MpSchedule(UINT32 target)47 VOID LOS_MpSchedule(UINT32 target)
48 {
49 UINT32 cpuid = ArchCurrCpuid();
50 target &= ~(1U << cpuid);
51 HalIrqSendIpi(target, LOS_MP_IPI_SCHEDULE);
52 }
53
OsMpWakeHandler(VOID)54 VOID OsMpWakeHandler(VOID)
55 {
56 /* generic wakeup ipi, do nothing */
57 }
58
OsMpScheduleHandler(VOID)59 VOID OsMpScheduleHandler(VOID)
60 {
61 /*
62 * set schedule flag to differ from wake function,
63 * so that the scheduler can be triggered at the end of irq.
64 */
65 OsSchedRunqueuePendingSet();
66 }
67
OsMpHaltHandler(VOID)68 VOID OsMpHaltHandler(VOID)
69 {
70 (VOID)LOS_IntLock();
71 OsPercpuGet()->excFlag = CPU_HALT;
72
73 while (1) {}
74 }
75
OsMpCollectTasks(VOID)76 VOID OsMpCollectTasks(VOID)
77 {
78 LosTaskCB *taskCB = NULL;
79 UINT32 taskID = 0;
80 UINT32 ret;
81
82 /* recursive checking all the available task */
83 for (; taskID <= g_taskMaxNum; taskID++) {
84 taskCB = &g_taskCBArray[taskID];
85
86 if (OsTaskIsUnused(taskCB) || OsTaskIsRunning(taskCB)) {
87 continue;
88 }
89
90 /*
91 * though task status is not atomic, this check may success but not accomplish
92 * the deletion; this deletion will be handled until the next run.
93 */
94 if (taskCB->signal & SIGNAL_KILL) {
95 ret = LOS_TaskDelete(taskID);
96 if (ret != LOS_OK) {
97 PRINT_WARN("GC collect task failed err:0x%x\n", ret);
98 }
99 }
100 }
101 }
102
103 #ifdef LOSCFG_KERNEL_SMP_CALL
OsMpFuncCall(UINT32 target,SMP_FUNC_CALL func,VOID * args)104 VOID OsMpFuncCall(UINT32 target, SMP_FUNC_CALL func, VOID *args)
105 {
106 UINT32 index;
107 UINT32 intSave;
108
109 if (func == NULL) {
110 return;
111 }
112
113 if (!(target & OS_MP_CPU_ALL)) {
114 return;
115 }
116
117 for (index = 0; index < LOSCFG_KERNEL_CORE_NUM; index++) {
118 if (CPUID_TO_AFFI_MASK(index) & target) {
119 MpCallFunc *mpCallFunc = (MpCallFunc *)LOS_MemAlloc(m_aucSysMem0, sizeof(MpCallFunc));
120 if (mpCallFunc == NULL) {
121 PRINT_ERR("smp func call malloc failed\n");
122 return;
123 }
124 mpCallFunc->func = func;
125 mpCallFunc->args = args;
126
127 MP_CALL_LOCK(intSave);
128 LOS_ListAdd(&g_percpu[index].funcLink, &(mpCallFunc->node));
129 MP_CALL_UNLOCK(intSave);
130 }
131 }
132 HalIrqSendIpi(target, LOS_MP_IPI_FUNC_CALL);
133 }
134
OsMpFuncCallHandler(VOID)135 VOID OsMpFuncCallHandler(VOID)
136 {
137 UINT32 intSave;
138 UINT32 cpuid = ArchCurrCpuid();
139 LOS_DL_LIST *list = NULL;
140 MpCallFunc *mpCallFunc = NULL;
141
142 MP_CALL_LOCK(intSave);
143 while (!LOS_ListEmpty(&g_percpu[cpuid].funcLink)) {
144 list = LOS_DL_LIST_FIRST(&g_percpu[cpuid].funcLink);
145 LOS_ListDelete(list);
146 MP_CALL_UNLOCK(intSave);
147
148 mpCallFunc = LOS_DL_LIST_ENTRY(list, MpCallFunc, node);
149 mpCallFunc->func(mpCallFunc->args);
150 (VOID)LOS_MemFree(m_aucSysMem0, mpCallFunc);
151
152 MP_CALL_LOCK(intSave);
153 }
154 MP_CALL_UNLOCK(intSave);
155 }
156
OsMpFuncCallInit(VOID)157 VOID OsMpFuncCallInit(VOID)
158 {
159 UINT32 index;
160 /* init funclink for each core */
161 for (index = 0; index < LOSCFG_KERNEL_CORE_NUM; index++) {
162 LOS_ListInit(&g_percpu[index].funcLink);
163 }
164 }
165 #endif /* LOSCFG_KERNEL_SMP_CALL */
166
OsMpInit(VOID)167 UINT32 OsMpInit(VOID)
168 {
169 UINT16 swtmrId;
170
171 (VOID)LOS_SwtmrCreate(OS_MP_GC_PERIOD, LOS_SWTMR_MODE_PERIOD,
172 (SWTMR_PROC_FUNC)OsMpCollectTasks, &swtmrId, 0);
173 (VOID)LOS_SwtmrStart(swtmrId);
174 #ifdef LOSCFG_KERNEL_SMP_CALL
175 OsMpFuncCallInit();
176 #endif
177 return LOS_OK;
178 }
179
180 LOS_MODULE_INIT(OsMpInit, LOS_INIT_LEVEL_KMOD_TASK);
181
182 #endif
183
184