1/*
2 * Copyright (c) 2021 Nuclei Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the License); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#include "los_arch_context.h"
20#include "los_arch_interrupt.h"
21#include "los_task.h"
22#include "los_memory.h"
23#include "los_timer.h"
24#include "los_sched.h"
25#include "los_interrupt.h"
26#include "los_debug.h"
27#include "nuclei_sdk_soc.h"
28
29#define INITIAL_MSTATUS                 ( MSTATUS_MPP | MSTATUS_MPIE | MSTATUS_FS_INITIAL)
30#define ALIGN_DOWN(size, align)         ((size) & ~((align) - 1))
31
32LITE_OS_SEC_TEXT_INIT VOID ArchInit(VOID)
33{
34    HalHwiInit();
35}
36
37LITE_OS_SEC_TEXT_MINOR VOID ArchSysExit(VOID)
38{
39    ArchIntLock();
40    while (1) {
41    }
42}
43
44LITE_OS_SEC_TEXT_INIT VOID *ArchTskStackInit(UINT32 taskID, UINT32 stackSize, VOID *topStack)
45{
46    UINT32 index;
47    UINT8 *stk = 0;
48    TaskContext *context = NULL;
49
50    stk = ((UINT8 *)topStack) + stackSize + sizeof(STACK_TYPE);
51    stk = (UINT8 *)ALIGN_DOWN((uintptr_t)stk, REGBYTES);
52    context = (TaskContext *)(stk - sizeof(TaskContext));
53
54    for (index = 1; index < sizeof(TaskContext)/ sizeof(STACK_TYPE); index ++) {
55        ((STACK_TYPE *)context)[index] = OS_TASK_STACK_INIT;
56    }
57    context->ra      = (STACK_TYPE)ArchSysExit;
58    context->a0      = (STACK_TYPE)taskID;
59    context->epc     = (STACK_TYPE)OsTaskEntry;
60
61    context->mstatus = INITIAL_MSTATUS;
62
63    return (VOID *)context;
64}
65
66extern LosTask g_losTask;
67LITE_OS_SEC_TEXT_INIT UINT32 ArchStartSchedule(VOID)
68{
69    (VOID)LOS_IntLock();
70    OsSchedStart();
71    HalStartToRun();
72    return LOS_OK; /* never return */
73}
74
75VOID ArchTaskSchedule(VOID)
76{
77    SysTimer_SetSWIRQ();
78}
79
80VOID HalTaskSwitch(VOID)
81{
82    SysTimer_ClearSWIRQ();
83    OsSchedTaskSwitch();
84    /* Set newTask to runTask */
85    g_losTask.runTask = g_losTask.newTask;
86}
87
88