xref: /Kernel-v10.6.2/portable/ThirdParty/XCC/Xtensa/port.c (revision ef7b253b56c9788077f5ecd6c9deb4021923d646)
1 /*
2  * FreeRTOS Kernel V10.6.2
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, TaskFunction_t pxCode, void *pvParameters, BaseType_t xRunPrivileged )
66 #else
67 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
68 #endif
69 {
70     StackType_t * sp;
71     StackType_t * tp;
72     XtExcFrame * frame;
73 
74     #if XCHAL_CP_NUM > 0
75         uint32_t * p;
76     #endif
77 
78     /* Create interrupt stack frame aligned to 16 byte boundary */
79     sp = ( StackType_t * ) ( ( ( UBaseType_t ) pxTopOfStack - XT_CP_SIZE - XT_STK_FRMSZ ) & ~0xf );
80 
81     /* Clear the entire frame (do not use memset() because we don't depend on C library) */
82     for( tp = sp; tp <= pxTopOfStack; ++tp )
83     {
84         *tp = 0;
85     }
86 
87     frame = ( XtExcFrame * ) sp;
88 
89     /* Explicitly initialize certain saved registers */
90     frame->pc = ( UBaseType_t ) pxCode;             /* task entrypoint                */
91     frame->a0 = 0;                                  /* to terminate GDB backtrace     */
92     frame->a1 = ( UBaseType_t ) sp + XT_STK_FRMSZ;  /* physical top of stack frame    */
93     frame->exit = ( UBaseType_t ) _xt_user_exit;    /* user exception exit dispatcher */
94 
95     /* Set initial PS to int level 0, EXCM disabled ('rfe' will enable), user mode. */
96     /* Also set entry point argument parameter. */
97     #ifdef __XTENSA_CALL0_ABI__
98         frame->a2 = ( UBaseType_t ) pvParameters;
99         frame->ps = PS_UM | PS_EXCM;
100     #else
101         /* + for windowed ABI also set WOE and CALLINC (pretend task was 'call4'd). */
102         frame->a6 = ( UBaseType_t ) pvParameters;
103         frame->ps = PS_UM | PS_EXCM | PS_WOE | PS_CALLINC( 1 );
104     #endif
105 
106     #ifdef XT_USE_SWPRI
107         /* Set the initial virtual priority mask value to all 1's. */
108         frame->vpri = 0xFFFFFFFF;
109     #endif
110 
111     #if XCHAL_CP_NUM > 0
112         /* Init the coprocessor save area (see xtensa_context.h) */
113 
114         /* No access to TCB here, so derive indirectly. Stack growth is top to bottom.
115          * //p = (uint32_t *) xMPUSettings->coproc_area;
116          */
117         p = ( uint32_t * ) ( ( ( uint32_t ) pxTopOfStack - XT_CP_SIZE ) & ~0xf );
118         configASSERT( ( uint32_t ) p >= frame->a1 );
119         p[ 0 ] = 0;
120         p[ 1 ] = 0;
121         p[ 2 ] = ( ( ( uint32_t ) p ) + 12 + XCHAL_TOTAL_SA_ALIGN - 1 ) & -XCHAL_TOTAL_SA_ALIGN;
122     #endif
123 
124     return sp;
125 }
126 
127 /*-----------------------------------------------------------*/
128 
vPortEndScheduler(void)129 void vPortEndScheduler( void )
130 {
131     /* It is unlikely that the Xtensa port will get stopped.  If required simply
132     disable the tick interrupt here. */
133 }
134 
135 /*-----------------------------------------------------------*/
136 
xPortStartScheduler(void)137 BaseType_t xPortStartScheduler( void )
138 {
139     // Interrupts are disabled at this point and stack contains PS with enabled interrupts when task context is restored
140 
141     #if XCHAL_CP_NUM > 0
142     /* Initialize co-processor management for tasks. Leave CPENABLE alone. */
143     _xt_coproc_init();
144     #endif
145 
146     /* Init the tick divisor value */
147     _xt_tick_divisor_init();
148 
149     /* Setup the hardware to generate the tick. */
150     _frxt_tick_timer_init();
151 
152     #if XT_USE_THREAD_SAFE_CLIB
153     // Init C library
154     vPortClibInit();
155     #endif
156 
157     port_xSchedulerRunning = 1;
158 
159     // Cannot be directly called from C; never returns
160     __asm__ volatile ("call0    _frxt_dispatch\n");
161 
162     /* Should not get here. */
163     return pdTRUE;
164 }
165 /*-----------------------------------------------------------*/
166 
xPortSysTickHandler(void)167 BaseType_t xPortSysTickHandler( void )
168 {
169     BaseType_t ret;
170     uint32_t interruptMask;
171 
172     portbenchmarkIntLatency();
173 
174     /* Interrupts upto configMAX_SYSCALL_INTERRUPT_PRIORITY must be
175      * disabled before calling xTaskIncrementTick as it access the
176      * kernel lists. */
177     interruptMask = portSET_INTERRUPT_MASK_FROM_ISR();
178     {
179         ret = xTaskIncrementTick();
180     }
181     portCLEAR_INTERRUPT_MASK_FROM_ISR( interruptMask );
182 
183     portYIELD_FROM_ISR( ret );
184 
185     return ret;
186 }
187 /*-----------------------------------------------------------*/
188 
189 /*
190  * Used to set coprocessor area in stack. Current hack is to reuse MPU pointer for coprocessor area.
191  */
192 #if portUSING_MPU_WRAPPERS
vPortStoreTaskMPUSettings(xMPU_SETTINGS * xMPUSettings,const struct xMEMORY_REGION * const xRegions,StackType_t * pxBottomOfStack,uint32_t ulStackDepth)193 void vPortStoreTaskMPUSettings( xMPU_SETTINGS * xMPUSettings,
194                                 const struct xMEMORY_REGION * const xRegions,
195                                 StackType_t * pxBottomOfStack,
196                                 uint32_t ulStackDepth )
197 {
198     #if XCHAL_CP_NUM > 0
199         xMPUSettings->coproc_area = ( StackType_t * ) ( ( uint32_t ) ( pxBottomOfStack + ulStackDepth - 1 ));
200         xMPUSettings->coproc_area = ( StackType_t * ) ( ( ( portPOINTER_SIZE_TYPE ) xMPUSettings->coproc_area ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
201         xMPUSettings->coproc_area = ( StackType_t * ) ( ( ( uint32_t ) xMPUSettings->coproc_area - XT_CP_SIZE ) & ~0xf );
202 
203         /* NOTE: we cannot initialize the coprocessor save area here because FreeRTOS is going to
204          * clear the stack area after we return. This is done in pxPortInitialiseStack().
205          */
206     #endif
207 }
208 #endif /* if portUSING_MPU_WRAPPERS */
209