1 /*
2 * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "los_arch_interrupt.h"
32 #include "los_debug.h"
33
34 UINT32 g_intCount = 0;
35
36 /* *
37 * @ingroup los_hwi
38 * Hardware interrupt form mapping handling function array.
39 */
40 HWI_PROC_FUNC __attribute__((aligned(LOSCFG_ARCH_HWI_VECTOR_ALIGN))) g_hwiForm[OS_VECTOR_CNT] = {0};
41
ArchGetHwiFrom(VOID)42 VOID *ArchGetHwiFrom(VOID)
43 {
44 return g_hwiForm;
45 }
46
47 /* ****************************************************************************
48 Function : HalHwiDefaultHandler
49 Description : default handler of the hardware interrupt
50 Input : None
51 Output : None
52 Return : None
53 **************************************************************************** */
HalHwiDefaultHandler(VOID)54 VOID HalHwiDefaultHandler(VOID)
55 {
56 PRINT_ERR("%s irqnum:%u\n", __FUNCTION__, ArchIntCurIrqNum());
57 while (1) {}
58 }
59
HalPreInterruptHandler(UINT32 arg)60 WEAK VOID HalPreInterruptHandler(UINT32 arg)
61 {
62 (VOID)arg;
63 return;
64 }
65
HalAftInterruptHandler(UINT32 arg)66 WEAK VOID HalAftInterruptHandler(UINT32 arg)
67 {
68 (VOID)arg;
69 return;
70 }
71
HwiNumValid(UINT32 num)72 STATIC UINT32 HwiNumValid(UINT32 num)
73 {
74 return (num >= OS_SYS_VECTOR_CNT) && (num <= OS_VECTOR_CNT);
75 }
76
ArchIntTrigger(HWI_HANDLE_T hwiNum)77 UINT32 ArchIntTrigger(HWI_HANDLE_T hwiNum)
78 {
79 if (!HwiNumValid(hwiNum)) {
80 return OS_ERRNO_HWI_NUM_INVALID;
81 }
82
83 HwiControllerOps *hwiOps = ArchIntOpsGet();
84 if (hwiOps->triggerIrq == NULL) {
85 return OS_ERRNO_HWI_OPS_FUNC_NULL;
86 }
87
88 return hwiOps->triggerIrq(hwiNum);
89 }
90
ArchIntEnable(HWI_HANDLE_T hwiNum)91 UINT32 ArchIntEnable(HWI_HANDLE_T hwiNum)
92 {
93 if (!HwiNumValid(hwiNum)) {
94 return OS_ERRNO_HWI_NUM_INVALID;
95 }
96
97 HwiControllerOps *hwiOps = ArchIntOpsGet();
98 if (hwiOps->enableIrq == NULL) {
99 return OS_ERRNO_HWI_OPS_FUNC_NULL;
100 }
101
102 return hwiOps->enableIrq(hwiNum);
103 }
104
ArchIntDisable(HWI_HANDLE_T hwiNum)105 UINT32 ArchIntDisable(HWI_HANDLE_T hwiNum)
106 {
107 if (!HwiNumValid(hwiNum)) {
108 return OS_ERRNO_HWI_NUM_INVALID;
109 }
110
111 HwiControllerOps *hwiOps = ArchIntOpsGet();
112 if (hwiOps->disableIrq == NULL) {
113 return OS_ERRNO_HWI_OPS_FUNC_NULL;
114 }
115
116 return hwiOps->disableIrq(hwiNum);
117 }
118
ArchIntClear(HWI_HANDLE_T hwiNum)119 UINT32 ArchIntClear(HWI_HANDLE_T hwiNum)
120 {
121 if (!HwiNumValid(hwiNum)) {
122 return OS_ERRNO_HWI_NUM_INVALID;
123 }
124
125 HwiControllerOps *hwiOps = ArchIntOpsGet();
126 if (hwiOps->clearIrq == NULL) {
127 return OS_ERRNO_HWI_OPS_FUNC_NULL;
128 }
129
130 return hwiOps->clearIrq(hwiNum);
131 }
132
ArchIntCurIrqNum(VOID)133 UINT32 ArchIntCurIrqNum(VOID)
134 {
135 HwiControllerOps *hwiOps = ArchIntOpsGet();
136 return hwiOps->getCurIrqNum();
137 }
138
139 #if (LOSCFG_PLATFORM_HWI_WITH_ARG == 1)
140 typedef struct {
141 HWI_PROC_FUNC pfnHandler;
142 VOID *pParm;
143 } HWI_HANDLER_FUNC;
144
145 /* *
146 * @ingroup los_hwi
147 * Hardware interrupt handler form mapping handling function array.
148 */
149 HWI_HANDLER_FUNC g_hwiHandlerForm[OS_VECTOR_CNT] = {{ (HWI_PROC_FUNC)0, (HWI_ARG_T)0 }};
150
151 /* *
152 * @ingroup los_hwi
153 * Set interrupt vector table.
154 */
OsSetVector(UINT32 num,HWI_PROC_FUNC vector,VOID * arg)155 VOID OsSetVector(UINT32 num, HWI_PROC_FUNC vector, VOID *arg)
156 {
157 if ((num + OS_SYS_VECTOR_CNT) < OS_VECTOR_CNT) {
158 g_hwiForm[num + OS_SYS_VECTOR_CNT] = (HWI_PROC_FUNC)HalInterrupt;
159 g_hwiHandlerForm[num + OS_SYS_VECTOR_CNT].pfnHandler = vector;
160 g_hwiHandlerForm[num + OS_SYS_VECTOR_CNT].pParm = arg;
161 }
162 }
163
164 #else
165 /* *
166 * @ingroup los_hwi
167 * Hardware interrupt handler form mapping handling function array.
168 */
169 HWI_PROC_FUNC g_hwiHandlerForm[OS_VECTOR_CNT] = {0};
170
171 /* *
172 * @ingroup los_hwi
173 * Set interrupt vector table.
174 */
OsSetVector(UINT32 num,HWI_PROC_FUNC vector)175 VOID OsSetVector(UINT32 num, HWI_PROC_FUNC vector)
176 {
177 if ((num + OS_SYS_VECTOR_CNT) < OS_VECTOR_CNT) {
178 g_hwiForm[num + OS_SYS_VECTOR_CNT] = HalInterrupt;
179 g_hwiHandlerForm[num + OS_SYS_VECTOR_CNT] = vector;
180 }
181 }
182 #endif
183
184 /* ****************************************************************************
185 Function : ArchHwiCreate
186 Description : create hardware interrupt
187 Input : hwiNum --- hwi num to create
188 hwiPrio --- priority of the hwi
189 hwiMode --- unused
190 hwiHandler --- hwi handler
191 irqParam --- param of the hwi handler
192 Output : None
193 Return : LOS_OK on success or error code on failure
194 **************************************************************************** */
ArchHwiCreate(HWI_HANDLE_T hwiNum,HWI_PRIOR_T hwiPrio,HWI_MODE_T hwiMode,HWI_PROC_FUNC hwiHandler,HwiIrqParam * irqParam)195 UINT32 ArchHwiCreate(HWI_HANDLE_T hwiNum,
196 HWI_PRIOR_T hwiPrio,
197 HWI_MODE_T hwiMode,
198 HWI_PROC_FUNC hwiHandler,
199 HwiIrqParam *irqParam)
200 {
201 (VOID)hwiMode;
202 UINT32 intSave;
203
204 if (hwiHandler == NULL) {
205 return OS_ERRNO_HWI_PROC_FUNC_NULL;
206 }
207
208 if (hwiNum >= OS_HWI_MAX_NUM) {
209 return OS_ERRNO_HWI_NUM_INVALID;
210 }
211
212 if (g_hwiForm[hwiNum + OS_SYS_VECTOR_CNT] != (HWI_PROC_FUNC)HalHwiDefaultHandler) {
213 return OS_ERRNO_HWI_ALREADY_CREATED;
214 }
215
216 if (hwiPrio > OS_HWI_PRIO_LOWEST) {
217 return OS_ERRNO_HWI_PRIO_INVALID;
218 }
219
220 intSave = LOS_IntLock();
221 #if (LOSCFG_PLATFORM_HWI_WITH_ARG == 1)
222 if (irqParam != NULL) {
223 OsSetVector(hwiNum, hwiHandler, irqParam->pDevId);
224 } else {
225 OsSetVector(hwiNum, hwiHandler, NULL);
226 }
227 #else
228 (VOID)irqParam;
229 OsSetVector(hwiNum, hwiHandler);
230 #endif
231
232 HwiControllerOps *hwiOps = ArchIntOpsGet();
233 if (hwiOps->createIrq == NULL) {
234 LOS_IntRestore(intSave);
235 return OS_ERRNO_HWI_OPS_FUNC_NULL;
236 }
237 hwiOps->createIrq(hwiNum, hwiPrio);
238
239 LOS_IntRestore(intSave);
240
241 return LOS_OK;
242 }
243
244 /* ****************************************************************************
245 Function : ArchHwiDelete
246 Description : Delete hardware interrupt
247 Input : hwiNum --- hwi num to delete
248 irqParam --- param of the hwi handler
249 Output : None
250 Return : LOS_OK on success or error code on failure
251 **************************************************************************** */
ArchHwiDelete(HWI_HANDLE_T hwiNum,HwiIrqParam * irqParam)252 LITE_OS_SEC_TEXT_INIT UINT32 ArchHwiDelete(HWI_HANDLE_T hwiNum, HwiIrqParam *irqParam)
253 {
254 (VOID)irqParam;
255 UINT32 intSave;
256
257 if (hwiNum >= OS_HWI_MAX_NUM) {
258 return OS_ERRNO_HWI_NUM_INVALID;
259 }
260
261 ArchIntDisable(hwiNum);
262
263 intSave = LOS_IntLock();
264
265 g_hwiForm[hwiNum + OS_SYS_VECTOR_CNT] = (HWI_PROC_FUNC)HalHwiDefaultHandler;
266
267 LOS_IntRestore(intSave);
268
269 return LOS_OK;
270 }
271
ArchIsIntActive(VOID)272 UINT32 ArchIsIntActive(VOID)
273 {
274 return (g_intCount > 0);
275 }
276