1 /*
2 * FreeRTOS Kernel V10.6.2
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
26 *
27 */
28
29 /*-----------------------------------------------------------
30 * Implementation of functions defined in portable.h for the RISC-V port.
31 *----------------------------------------------------------*/
32
33 /* Scheduler includes. */
34 #include "FreeRTOS.h"
35 #include "task.h"
36 #include "portmacro.h"
37
38 /* Standard includes. */
39 #include "string.h"
40
41 #ifdef configCLINT_BASE_ADDRESS
42 #warning The configCLINT_BASE_ADDRESS constant has been deprecated. configMTIME_BASE_ADDRESS and configMTIMECMP_BASE_ADDRESS are currently being derived from the (possibly 0) configCLINT_BASE_ADDRESS setting. Please update to define configMTIME_BASE_ADDRESS and configMTIMECMP_BASE_ADDRESS dirctly in place of configCLINT_BASE_ADDRESS. See https: /*www.FreeRTOS.org/Using-FreeRTOS-on-RISC-V.html */
43 #endif
44
45 #ifndef configMTIME_BASE_ADDRESS
46 #warning configMTIME_BASE_ADDRESS must be defined in FreeRTOSConfig.h. If the target chip includes a memory-mapped mtime register then set configMTIME_BASE_ADDRESS to the mapped address. Otherwise set configMTIME_BASE_ADDRESS to 0. See https: /*www.FreeRTOS.org/Using-FreeRTOS-on-RISC-V.html */
47 #endif
48
49 #ifndef configMTIMECMP_BASE_ADDRESS
50 #warning configMTIMECMP_BASE_ADDRESS must be defined in FreeRTOSConfig.h. If the target chip includes a memory-mapped mtimecmp register then set configMTIMECMP_BASE_ADDRESS to the mapped address. Otherwise set configMTIMECMP_BASE_ADDRESS to 0. See https: /*www.FreeRTOS.org/Using-FreeRTOS-on-RISC-V.html */
51 #endif
52
53 /* Let the user override the pre-loading of the initial LR with the address of
54 * prvTaskExitError() in case it messes up unwinding of the stack in the
55 * debugger. */
56 #ifdef configTASK_RETURN_ADDRESS
57 #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS
58 #else
59 #define portTASK_RETURN_ADDRESS prvTaskExitError
60 #endif
61
62 /* The stack used by interrupt service routines. Set configISR_STACK_SIZE_WORDS
63 * to use a statically allocated array as the interrupt stack. Alternative leave
64 * configISR_STACK_SIZE_WORDS undefined and update the linker script so that a
65 * linker variable names __freertos_irq_stack_top has the same value as the top
66 * of the stack used by main. Using the linker script method will repurpose the
67 * stack that was used by main before the scheduler was started for use as the
68 * interrupt stack after the scheduler has started. */
69 #ifdef configISR_STACK_SIZE_WORDS
70 static __attribute__( ( aligned( 16 ) ) ) StackType_t xISRStack[ configISR_STACK_SIZE_WORDS ] = { 0 };
71 const StackType_t xISRStackTop = ( StackType_t ) &( xISRStack[ configISR_STACK_SIZE_WORDS & ~portBYTE_ALIGNMENT_MASK ] );
72
73 /* Don't use 0xa5 as the stack fill bytes as that is used by the kernerl for
74 * the task stacks, and so will legitimately appear in many positions within
75 * the ISR stack. */
76 #define portISR_STACK_FILL_BYTE 0xee
77 #else
78 extern const uint32_t __freertos_irq_stack_top[];
79 const StackType_t xISRStackTop = ( StackType_t ) __freertos_irq_stack_top;
80 #endif
81
82 /**
83 * @brief Used to catch tasks that attempt to return from their implementing
84 * function.
85 */
86 static void prvTaskExitError( void );
87
88 /*
89 * Setup the timer to generate the tick interrupts. The implementation in this
90 * file is weak to allow application writers to change the timer used to
91 * generate the tick interrupt.
92 */
93 void vPortSetupTimerInterrupt( void ) __attribute__( ( weak ) );
94
95 /*-----------------------------------------------------------*/
96
97 /* Used to program the machine timer compare register. */
98 uint64_t ullNextTime = 0ULL;
99 const uint64_t * pullNextTime = &ullNextTime;
100 const size_t uxTimerIncrementsForOneTick = ( size_t ) ( ( configCPU_CLOCK_HZ ) / ( configTICK_RATE_HZ ) ); /* Assumes increment won't go over 32-bits. */
101 uint32_t const ullMachineTimerCompareRegisterBase = configMTIMECMP_BASE_ADDRESS;
102 volatile uint64_t * pullMachineTimerCompareRegister = NULL;
103
104 /* Holds the critical nesting value - deliberately non-zero at start up to
105 * ensure interrupts are not accidentally enabled before the scheduler starts. */
106 size_t xCriticalNesting = ( size_t ) 0xaaaaaaaa;
107 size_t * pxCriticalNesting = &xCriticalNesting;
108
109 /* Used to catch tasks that attempt to return from their implementing function. */
110 size_t xTaskReturnAddress = ( size_t ) portTASK_RETURN_ADDRESS;
111
112 /* Set configCHECK_FOR_STACK_OVERFLOW to 3 to add ISR stack checking to task
113 * stack checking. A problem in the ISR stack will trigger an assert, not call
114 * the stack overflow hook function (because the stack overflow hook is specific
115 * to a task stack, not the ISR stack). */
116 #if defined( configISR_STACK_SIZE_WORDS ) && ( configCHECK_FOR_STACK_OVERFLOW > 2 )
117 #warning This path not tested, or even compiled yet.
118
119 static const uint8_t ucExpectedStackBytes[] =
120 {
121 portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
122 portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
123 portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
124 portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
125 portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE
126 }; \
127
128 #define portCHECK_ISR_STACK() configASSERT( ( memcmp( ( void * ) xISRStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) == 0 ) )
129 #else /* if defined( configISR_STACK_SIZE_WORDS ) && ( configCHECK_FOR_STACK_OVERFLOW > 2 ) */
130 /* Define the function away. */
131 #define portCHECK_ISR_STACK()
132 #endif /* configCHECK_FOR_STACK_OVERFLOW > 2 */
133
134 /*-----------------------------------------------------------*/
135
prvTaskExitError(void)136 static void prvTaskExitError( void )
137 {
138 volatile uint32_t ulDummy = 0UL;
139
140 /* A function that implements a task must not exit or attempt to return to
141 * its caller as there is nothing to return to. If a task wants to exit it
142 * should instead call vTaskDelete( NULL ). Artificially force an assert()
143 * to be triggered if configASSERT() is defined, then stop here so
144 * application writers can catch the error. */
145 configASSERT( xCriticalNesting == ~0UL );
146 portDISABLE_INTERRUPTS();
147
148 while( ulDummy == 0 )
149 {
150 /* This file calls prvTaskExitError() after the scheduler has been
151 * started to remove a compiler warning about the function being
152 * defined but never called. ulDummy is used purely to quieten other
153 * warnings about code appearing after this function is called - making
154 * ulDummy volatile makes the compiler think the function could return
155 * and therefore not output an 'unreachable code' warning for code that
156 * appears after it. */
157 }
158 }
159 /*-----------------------------------------------------------*/
160
161 #if ( configMTIME_BASE_ADDRESS != 0 ) && ( configMTIMECMP_BASE_ADDRESS != 0 )
162
vPortSetupTimerInterrupt(void)163 void vPortSetupTimerInterrupt( void )
164 {
165 uint32_t ulCurrentTimeHigh, ulCurrentTimeLow;
166 volatile uint32_t * const pulTimeHigh = ( uint32_t * ) ( ( configMTIME_BASE_ADDRESS ) + 4UL ); /* 8-byte type so high 32-bit word is 4 bytes up. */
167 volatile uint32_t * const pulTimeLow = ( uint32_t * ) ( configMTIME_BASE_ADDRESS );
168 volatile uint32_t ulHartId;
169
170 __asm volatile ( "csrr %0, 0xf14" : "=r" ( ulHartId ) ); /* 0xf14 is hartid. */
171
172 pullMachineTimerCompareRegister = ( volatile uint64_t * ) ( ullMachineTimerCompareRegisterBase + ( ulHartId * sizeof( uint64_t ) ) );
173
174 do
175 {
176 ulCurrentTimeHigh = *pulTimeHigh;
177 ulCurrentTimeLow = *pulTimeLow;
178 } while( ulCurrentTimeHigh != *pulTimeHigh );
179
180 ullNextTime = ( uint64_t ) ulCurrentTimeHigh;
181 ullNextTime <<= 32ULL; /* High 4-byte word is 32-bits up. */
182 ullNextTime |= ( uint64_t ) ulCurrentTimeLow;
183 ullNextTime += ( uint64_t ) uxTimerIncrementsForOneTick;
184 *pullMachineTimerCompareRegister = ullNextTime;
185
186 /* Prepare the time to use after the next tick interrupt. */
187 ullNextTime += ( uint64_t ) uxTimerIncrementsForOneTick;
188 }
189
190 #endif /* ( configMTIME_BASE_ADDRESS != 0 ) && ( configMTIME_BASE_ADDRESS != 0 ) */
191 /*-----------------------------------------------------------*/
192
xPortStartScheduler(void)193 BaseType_t xPortStartScheduler( void )
194 {
195 extern void xPortStartFirstTask( void );
196
197 #if ( configASSERT_DEFINED == 1 )
198 {
199 /* Check alignment of the interrupt stack - which is the same as the
200 * stack that was being used by main() prior to the scheduler being
201 * started. */
202 configASSERT( ( xISRStackTop & portBYTE_ALIGNMENT_MASK ) == 0 );
203
204 #ifdef configISR_STACK_SIZE_WORDS
205 {
206 memset( ( void * ) xISRStack, portISR_STACK_FILL_BYTE, sizeof( xISRStack ) );
207 }
208 #endif /* configISR_STACK_SIZE_WORDS */
209 }
210 #endif /* configASSERT_DEFINED */
211
212 /* If there is a CLINT then it is ok to use the default implementation
213 * in this file, otherwise vPortSetupTimerInterrupt() must be implemented to
214 * configure whichever clock is to be used to generate the tick interrupt. */
215 vPortSetupTimerInterrupt();
216
217 #if ( ( configMTIME_BASE_ADDRESS != 0 ) && ( configMTIMECMP_BASE_ADDRESS != 0 ) )
218 {
219 /* Enable mtime and external interrupts. 1<<7 for timer interrupt,
220 * 1<<11 for external interrupt. _RB_ What happens here when mtime is
221 * not present as with pulpino? */
222 __asm volatile ( "csrs 0x304, %0" ::"r" ( 0x880 ) ); /* 0x304 is mie. */
223 }
224 #endif /* ( configMTIME_BASE_ADDRESS != 0 ) && ( configMTIMECMP_BASE_ADDRESS != 0 ) */
225
226 xPortStartFirstTask();
227
228 /* Should not get here as after calling xPortStartFirstTask() only tasks
229 * should be executing. */
230 return pdFAIL;
231 }
232 /*-----------------------------------------------------------*/
233
vPortEndScheduler(void)234 void vPortEndScheduler( void )
235 {
236 /* Not implemented. */
237 for( ; ; )
238 {
239 }
240 }
241 /*-----------------------------------------------------------*/
242