1 /*
2 * Copyright (c) 2021 Nuclei Limited. 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 <stdio.h>
32 #include <stdarg.h>
33 #include "los_arch.h"
34 #include "los_arch_interrupt.h"
35 #include "los_arch_context.h"
36 #include "los_task.h"
37 #include "los_debug.h"
38 #include "nuclei_sdk_hal.h"
39
40 UINT32 g_intCount = 0;
41
HwiUnmask(HWI_HANDLE_T hwiNum)42 STATIC UINT32 HwiUnmask(HWI_HANDLE_T hwiNum)
43 {
44 if (hwiNum >= OS_HWI_MAX_NUM) {
45 return OS_ERRNO_HWI_NUM_INVALID;
46 }
47
48 ECLIC_EnableIRQ(hwiNum);
49
50 return LOS_OK;
51 }
52
HwiMask(HWI_HANDLE_T hwiNum)53 STATIC UINT32 HwiMask(HWI_HANDLE_T hwiNum)
54 {
55 if (hwiNum >= OS_HWI_MAX_NUM) {
56 return OS_ERRNO_HWI_NUM_INVALID;
57 }
58
59 ECLIC_DisableIRQ(hwiNum);
60
61 return LOS_OK;
62 }
63
HwiSetPriority(HWI_HANDLE_T hwiNum,UINT8 priority)64 STATIC UINT32 HwiSetPriority(HWI_HANDLE_T hwiNum, UINT8 priority)
65 {
66 if (hwiNum >= OS_HWI_MAX_NUM) {
67 return OS_ERRNO_HWI_NUM_INVALID;
68 }
69
70 if (priority > OS_HWI_PRIO_HIGHEST || priority < OS_HWI_PRIO_LOWEST) {
71 return OS_ERRNO_HWI_PRIO_INVALID;
72 }
73
74 ECLIC_SetPriorityIRQ(hwiNum, (hwiPrio & 0xffff));
75
76 return LOS_OK;
77 }
78
HalHwiInit(VOID)79 LITE_OS_SEC_TEXT_INIT VOID HalHwiInit(VOID)
80 {
81 // already setup interrupt vectors
82 }
83
84 /*****************************************************************************
85 Function : ArchHwiCreate
86 Description : create hardware interrupt
87 Input : hwiNum --- hwi num to create
88 hwiPrio --- priority of the hwi
89 hwiMode --- hwi interrupt hwiMode, between vector or non-vector
90 hwiHandler --- hwi handler
91 irqParam --- set trig hwiMode of the hwi handler
92 Level Triggerred = 0
93 Postive/Rising Edge Triggered = 1
94 Negtive/Falling Edge Triggered = 3
95 Output : None
96 Return : LOS_OK on success or error code on failure
97 *****************************************************************************/
ArchHwiCreate(HWI_HANDLE_T hwiNum,HWI_PRIOR_T hwiPrio,HWI_MODE_T hwiMode,HWI_PROC_FUNC hwiHandler,HwiIrqParam * irqParam)98 UINT32 ArchHwiCreate(HWI_HANDLE_T hwiNum,
99 HWI_PRIOR_T hwiPrio,
100 HWI_MODE_T hwiMode,
101 HWI_PROC_FUNC hwiHandler,
102 HwiIrqParam *irqParam)
103 {
104 if (hwiNum > SOC_INT_MAX) {
105 return OS_ERRNO_HWI_NUM_INVALID;
106 }
107 if (hwiMode > ECLIC_VECTOR_INTERRUPT) {
108 return OS_ERRNO_HWI_MODE_INVALID;
109 }
110 if ((irqParam == NULL) || (irqParam->pDevId > ECLIC_NEGTIVE_EDGE_TRIGGER)) {
111 return OS_ERRNO_HWI_ARG_INVALID;
112 }
113
114 /* set interrupt vector hwiMode */
115 ECLIC_SetShvIRQ(hwiNum, hwiMode);
116 /* set interrupt trigger hwiMode and polarity */
117 ECLIC_SetTrigIRQ(hwiNum, irqParam->pDevId);
118 /* set interrupt level */
119 // default to 0
120 ECLIC_SetLevelIRQ(hwiNum, 0);
121 /* set interrupt priority */
122 // high 16 bit for level
123 ECLIC_SetLevelIRQ(hwiNum, (hwiPrio >> 16));
124 /* set interrupt priority */
125 // low 16 bit for priority
126 ECLIC_SetPriorityIRQ(hwiNum, (hwiPrio & 0xffff));
127 if (hwiHandler != NULL) {
128 /* set interrupt handler entry to vector table */
129 ECLIC_SetVector(hwiNum, (rv_csr_t)hwiHandler);
130 }
131 /* enable interrupt */
132 HwiUnmask(hwiNum);
133 return LOS_OK;
134 }
135
136 /*****************************************************************************
137 Function : ArchHwiDelete
138 Description : Delete hardware interrupt
139 Input : hwiNum --- hwi num to delete
140 irqParam --- param of the hwi handler
141 Return : LOS_OK on success or error code on failure
142 *****************************************************************************/
ArchHwiDelete(HWI_HANDLE_T hwiNum,HwiIrqParam * irqParam)143 LITE_OS_SEC_TEXT UINT32 ArchHwiDelete(HWI_HANDLE_T hwiNum, HwiIrqParam *irqParam)
144 {
145 (VOID)irqParam;
146 // change func to default func
147 ECLIC_SetVector(hwiNum, (rv_csr_t)HalHwiDefaultHandler);
148 // disable interrupt
149 HwiMask(hwiNum);
150 return LOS_OK;
151 }
152
153 /* ****************************************************************************
154 Function : HalHwiDefaultHandler
155 Description : default handler of the hardware interrupt
156 Input : None
157 Output : None
158 Return : None
159 **************************************************************************** */
HalHwiDefaultHandler(VOID)160 LITE_OS_SEC_TEXT_INIT VOID HalHwiDefaultHandler(VOID)
161 {
162 PRINT_ERR("default handler\n");
163 while (1) {
164 }
165 }
166
167 /* ****************************************************************************
168 Function : HalDisplayTaskInfo
169 Description : display the task list
170 Input : None
171 Output : None
172 Return : None
173 **************************************************************************** */
HalDisplayTaskInfo(VOID)174 VOID HalDisplayTaskInfo(VOID)
175 {
176 TSK_INFO_S taskInfo;
177 UINT32 index;
178 UINT32 ret;
179
180 PRINTK("ID Pri Status name \r\n");
181 PRINTK("-- --- --------- ----\r\n");
182
183 for (index = 0; index < LOSCFG_BASE_CORE_TSK_LIMIT; index++) {
184 ret = LOS_TaskInfoGet(index, &taskInfo);
185 if (ret != LOS_OK) {
186 continue;
187 }
188 PRINTK("%d %d %s %s \r\n",
189 taskInfo.uwTaskID, taskInfo.usTaskPrio, OsConvertTskStatus(taskInfo.usTaskStatus), taskInfo.acName);
190 }
191 return;
192 }
193
194 /* ****************************************************************************
195 Function : HalUnalignedAccessFix
196 Description : Unaligned access fixes are not supported by default
197 Input : None
198 Output : None
199 Return : None
200 **************************************************************************** */
HalUnalignedAccessFix(UINTPTR mcause,UINTPTR mepc,UINTPTR mtval,VOID * sp)201 WEAK UINT32 HalUnalignedAccessFix(UINTPTR mcause, UINTPTR mepc, UINTPTR mtval, VOID *sp)
202 {
203 /* Unaligned access fixes are not supported by default */
204 PRINTK("Unaligned access fixes are not support by default!\r\n");
205 return LOS_NOK;
206 }
207
HalIntEnter(VOID)208 __attribute__((always_inline)) inline VOID HalIntEnter(VOID)
209 {
210 g_intCount += 1;
211 }
212
HalIntExit(VOID)213 __attribute__((always_inline)) inline VOID HalIntExit(VOID)
214 {
215 g_intCount -= 1;
216 }
217
ArchIsIntActive(VOID)218 __attribute__((always_inline)) inline UINT32 ArchIsIntActive(VOID)
219 {
220 return (g_intCount > 0);
221 }
222
223 const HwiControllerOps g_archHwiOps = {
224 .enableIrq = HwiUnmask,
225 .disableIrq = HwiMask,
226 .setIrqPriority = HwiSetPriority,
227 };
228