1 /*
2  * FreeRTOS Kernel V11.0.1
3  * Copyright (C) 2015-2019 Cadence Design Systems, Inc.
4  * Copyright (C) 2021 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
5  *
6  * SPDX-License-Identifier: MIT
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy of
9  * this software and associated documentation files (the "Software"), to deal in
10  * the Software without restriction, including without limitation the rights to
11  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
12  * the Software, and to permit persons to whom the Software is furnished to do so,
13  * subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in all
16  * copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
20  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
21  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  *
25  * https://www.FreeRTOS.org
26  * https://github.com/FreeRTOS
27  *
28  */
29 
30 #include <stdlib.h>
31 #include <xtensa/config/core.h>
32 
33 #include "xtensa_rtos.h"
34 
35 #include "FreeRTOS.h"
36 #include "task.h"
37 
38 
39 /* Defined in portasm.h */
40 extern void _frxt_tick_timer_init( void );
41 
42 /* Defined in xtensa_context.S */
43 extern void _xt_coproc_init( void );
44 
45 
46 /*-----------------------------------------------------------*/
47 
48 /* We require the address of the pxCurrentTCB variable, but don't want to know
49  * any details of its type. */
50 typedef void TCB_t;
51 extern volatile TCB_t * volatile pxCurrentTCB;
52 
53 unsigned port_xSchedulerRunning = 0; /* Duplicate of inaccessible xSchedulerRunning; needed at startup to avoid counting nesting */
54 unsigned port_interruptNesting = 0;  /* Interrupt nesting level */
55 
56 /*-----------------------------------------------------------*/
57 
58 /* User exception dispatcher when exiting */
59 void _xt_user_exit( void );
60 
61 /*
62  * Stack initialization
63  */
64 #if portUSING_MPU_WRAPPERS
pxPortInitialiseStack(StackType_t * pxTopOfStack,TaskFunction_t pxCode,void * pvParameters,BaseType_t xRunPrivileged)65     StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
66                                          TaskFunction_t pxCode,
67                                          void * pvParameters,
68                                          BaseType_t xRunPrivileged )
69 #else
70     StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
71                                          TaskFunction_t pxCode,
72                                          void * pvParameters )
73 #endif
74 {
75     StackType_t * sp;
76     StackType_t * tp;
77     XtExcFrame * frame;
78 
79     #if XCHAL_CP_NUM > 0
80         uint32_t * p;
81     #endif
82 
83     /* Create interrupt stack frame aligned to 16 byte boundary */
84     sp = ( StackType_t * ) ( ( ( UBaseType_t ) pxTopOfStack - XT_CP_SIZE - XT_STK_FRMSZ ) & ~0xf );
85 
86     /* Clear the entire frame (do not use memset() because we don't depend on C library) */
87     for( tp = sp; tp <= pxTopOfStack; ++tp )
88     {
89         *tp = 0;
90     }
91 
92     frame = ( XtExcFrame * ) sp;
93 
94     /* Explicitly initialize certain saved registers */
95     frame->pc = ( UBaseType_t ) pxCode;             /* task entrypoint                */
96     frame->a0 = 0;                                  /* to terminate GDB backtrace     */
97     frame->a1 = ( UBaseType_t ) sp + XT_STK_FRMSZ;  /* physical top of stack frame    */
98     frame->exit = ( UBaseType_t ) _xt_user_exit;    /* user exception exit dispatcher */
99 
100     /* Set initial PS to int level 0, EXCM disabled ('rfe' will enable), user mode. */
101     /* Also set entry point argument parameter. */
102     #ifdef __XTENSA_CALL0_ABI__
103         frame->a2 = ( UBaseType_t ) pvParameters;
104         frame->ps = PS_UM | PS_EXCM;
105     #else
106         /* + for windowed ABI also set WOE and CALLINC (pretend task was 'call4'd). */
107         frame->a6 = ( UBaseType_t ) pvParameters;
108         frame->ps = PS_UM | PS_EXCM | PS_WOE | PS_CALLINC( 1 );
109     #endif
110 
111     #ifdef XT_USE_SWPRI
112         /* Set the initial virtual priority mask value to all 1's. */
113         frame->vpri = 0xFFFFFFFF;
114     #endif
115 
116     #if XCHAL_CP_NUM > 0
117         /* Init the coprocessor save area (see xtensa_context.h) */
118 
119         /* No access to TCB here, so derive indirectly. Stack growth is top to bottom.
120          * //p = (uint32_t *) xMPUSettings->coproc_area;
121          */
122         p = ( uint32_t * ) ( ( ( uint32_t ) pxTopOfStack - XT_CP_SIZE ) & ~0xf );
123         configASSERT( ( uint32_t ) p >= frame->a1 );
124         p[ 0 ] = 0;
125         p[ 1 ] = 0;
126         p[ 2 ] = ( ( ( uint32_t ) p ) + 12 + XCHAL_TOTAL_SA_ALIGN - 1 ) & -XCHAL_TOTAL_SA_ALIGN;
127     #endif
128 
129     return sp;
130 }
131 
132 /*-----------------------------------------------------------*/
133 
vPortEndScheduler(void)134 void vPortEndScheduler( void )
135 {
136     /* It is unlikely that the Xtensa port will get stopped.  If required simply
137      * disable the tick interrupt here. */
138 }
139 
140 /*-----------------------------------------------------------*/
141 
xPortStartScheduler(void)142 BaseType_t xPortStartScheduler( void )
143 {
144     /* Interrupts are disabled at this point and stack contains PS with enabled interrupts when task context is restored */
145 
146     #if XCHAL_CP_NUM > 0
147         /* Initialize co-processor management for tasks. Leave CPENABLE alone. */
148         _xt_coproc_init();
149     #endif
150 
151     /* Init the tick divisor value */
152     _xt_tick_divisor_init();
153 
154     /* Setup the hardware to generate the tick. */
155     _frxt_tick_timer_init();
156 
157     #if XT_USE_THREAD_SAFE_CLIB
158         /* Init C library */
159         vPortClibInit();
160     #endif
161 
162     port_xSchedulerRunning = 1;
163 
164     /* Cannot be directly called from C; never returns */
165     __asm__ volatile ( "call0    _frxt_dispatch\n" );
166 
167     /* Should not get here. */
168     return pdTRUE;
169 }
170 /*-----------------------------------------------------------*/
171 
xPortSysTickHandler(void)172 BaseType_t xPortSysTickHandler( void )
173 {
174     BaseType_t ret;
175     uint32_t interruptMask;
176 
177     portbenchmarkIntLatency();
178 
179     /* Interrupts upto configMAX_SYSCALL_INTERRUPT_PRIORITY must be
180      * disabled before calling xTaskIncrementTick as it access the
181      * kernel lists. */
182     interruptMask = portSET_INTERRUPT_MASK_FROM_ISR();
183     {
184         ret = xTaskIncrementTick();
185     }
186     portCLEAR_INTERRUPT_MASK_FROM_ISR( interruptMask );
187 
188     portYIELD_FROM_ISR( ret );
189 
190     return ret;
191 }
192 /*-----------------------------------------------------------*/
193 
194 /*
195  * Used to set coprocessor area in stack. Current hack is to reuse MPU pointer for coprocessor area.
196  */
197 #if portUSING_MPU_WRAPPERS
vPortStoreTaskMPUSettings(xMPU_SETTINGS * xMPUSettings,const struct xMEMORY_REGION * const xRegions,StackType_t * pxBottomOfStack,uint32_t ulStackDepth)198     void vPortStoreTaskMPUSettings( xMPU_SETTINGS * xMPUSettings,
199                                     const struct xMEMORY_REGION * const xRegions,
200                                     StackType_t * pxBottomOfStack,
201                                     uint32_t ulStackDepth )
202     {
203         #if XCHAL_CP_NUM > 0
204             xMPUSettings->coproc_area = ( StackType_t * ) ( ( uint32_t ) ( pxBottomOfStack + ulStackDepth - 1 ) );
205             xMPUSettings->coproc_area = ( StackType_t * ) ( ( ( portPOINTER_SIZE_TYPE ) xMPUSettings->coproc_area ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
206             xMPUSettings->coproc_area = ( StackType_t * ) ( ( ( uint32_t ) xMPUSettings->coproc_area - XT_CP_SIZE ) & ~0xf );
207 
208             /* NOTE: we cannot initialize the coprocessor save area here because FreeRTOS is going to
209              * clear the stack area after we return. This is done in pxPortInitialiseStack().
210              */
211         #endif
212     }
213 #endif /* if portUSING_MPU_WRAPPERS */
214