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 RA. */
54 #ifdef configTASK_RETURN_ADDRESS
55     #define portTASK_RETURN_ADDRESS    configTASK_RETURN_ADDRESS
56 #else
57     #define portTASK_RETURN_ADDRESS    0
58 #endif
59 
60 /* The stack used by interrupt service routines.  Set configISR_STACK_SIZE_WORDS
61  * to use a statically allocated array as the interrupt stack.  Alternative leave
62  * configISR_STACK_SIZE_WORDS undefined and update the linker script so that a
63  * linker variable names __freertos_irq_stack_top has the same value as the top
64  * of the stack used by main.  Using the linker script method will repurpose the
65  * stack that was used by main before the scheduler was started for use as the
66  * interrupt stack after the scheduler has started. */
67 #ifdef configISR_STACK_SIZE_WORDS
68     static __attribute__ ((aligned(16))) StackType_t xISRStack[ configISR_STACK_SIZE_WORDS ] = { 0 };
69     const StackType_t xISRStackTop = ( StackType_t ) &( xISRStack[ configISR_STACK_SIZE_WORDS & ~portBYTE_ALIGNMENT_MASK ] );
70 
71     /* Don't use 0xa5 as the stack fill bytes as that is used by the kernerl for
72     the task stacks, and so will legitimately appear in many positions within
73     the ISR stack. */
74     #define portISR_STACK_FILL_BYTE    0xee
75 #else
76     extern const uint32_t __freertos_irq_stack_top[];
77     const StackType_t xISRStackTop = ( StackType_t ) __freertos_irq_stack_top;
78 #endif
79 
80 /*
81  * Setup the timer to generate the tick interrupts.  The implementation in this
82  * file is weak to allow application writers to change the timer used to
83  * generate the tick interrupt.
84  */
85 void vPortSetupTimerInterrupt( void ) __attribute__(( weak ));
86 
87 /*-----------------------------------------------------------*/
88 
89 /* Used to program the machine timer compare register. */
90 uint64_t ullNextTime = 0ULL;
91 const uint64_t *pullNextTime = &ullNextTime;
92 const size_t uxTimerIncrementsForOneTick = ( size_t ) ( ( configCPU_CLOCK_HZ ) / ( configTICK_RATE_HZ ) ); /* Assumes increment won't go over 32-bits. */
93 uint32_t const ullMachineTimerCompareRegisterBase = configMTIMECMP_BASE_ADDRESS;
94 volatile uint64_t * pullMachineTimerCompareRegister = NULL;
95 
96 /* Holds the critical nesting value - deliberately non-zero at start up to
97  * ensure interrupts are not accidentally enabled before the scheduler starts. */
98 size_t xCriticalNesting = ( size_t ) 0xaaaaaaaa;
99 size_t *pxCriticalNesting = &xCriticalNesting;
100 
101 /* Used to catch tasks that attempt to return from their implementing function. */
102 size_t xTaskReturnAddress = ( size_t ) portTASK_RETURN_ADDRESS;
103 
104 /* Set configCHECK_FOR_STACK_OVERFLOW to 3 to add ISR stack checking to task
105  * stack checking.  A problem in the ISR stack will trigger an assert, not call
106  * the stack overflow hook function (because the stack overflow hook is specific
107  * to a task stack, not the ISR stack). */
108 #if defined( configISR_STACK_SIZE_WORDS ) && ( configCHECK_FOR_STACK_OVERFLOW > 2 )
109     #warning This path not tested, or even compiled yet.
110 
111     static const uint8_t ucExpectedStackBytes[] = {
112                                     portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE,        \
113                                     portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE,        \
114                                     portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE,        \
115                                     portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE,        \
116                                     portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE };    \
117 
118     #define portCHECK_ISR_STACK() configASSERT( ( memcmp( ( void * ) xISRStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) == 0 ) )
119 #else
120     /* Define the function away. */
121     #define portCHECK_ISR_STACK()
122 #endif /* configCHECK_FOR_STACK_OVERFLOW > 2 */
123 
124 /*-----------------------------------------------------------*/
125 
126 #if( configMTIME_BASE_ADDRESS != 0 ) && ( configMTIMECMP_BASE_ADDRESS != 0 )
127 
vPortSetupTimerInterrupt(void)128     void vPortSetupTimerInterrupt( void )
129     {
130     uint32_t ulCurrentTimeHigh, ulCurrentTimeLow;
131     volatile uint32_t * const pulTimeHigh = ( volatile uint32_t * const ) ( ( configMTIME_BASE_ADDRESS ) + 4UL ); /* 8-byte type so high 32-bit word is 4 bytes up. */
132     volatile uint32_t * const pulTimeLow = ( volatile uint32_t * const ) ( configMTIME_BASE_ADDRESS );
133     volatile uint32_t ulHartId;
134 
135         __asm volatile( "csrr %0, mhartid" : "=r"( ulHartId ) );
136         pullMachineTimerCompareRegister  = ( volatile uint64_t * ) ( ullMachineTimerCompareRegisterBase + ( ulHartId * sizeof( uint64_t ) ) );
137 
138         do
139         {
140             ulCurrentTimeHigh = *pulTimeHigh;
141             ulCurrentTimeLow = *pulTimeLow;
142         } while( ulCurrentTimeHigh != *pulTimeHigh );
143 
144         ullNextTime = ( uint64_t ) ulCurrentTimeHigh;
145         ullNextTime <<= 32ULL; /* High 4-byte word is 32-bits up. */
146         ullNextTime |= ( uint64_t ) ulCurrentTimeLow;
147         ullNextTime += ( uint64_t ) uxTimerIncrementsForOneTick;
148         *pullMachineTimerCompareRegister = ullNextTime;
149 
150         /* Prepare the time to use after the next tick interrupt. */
151         ullNextTime += ( uint64_t ) uxTimerIncrementsForOneTick;
152     }
153 
154 #endif /* ( configMTIME_BASE_ADDRESS != 0 ) && ( configMTIME_BASE_ADDRESS != 0 ) */
155 /*-----------------------------------------------------------*/
156 
xPortStartScheduler(void)157 BaseType_t xPortStartScheduler( void )
158 {
159 extern void xPortStartFirstTask( void );
160 
161     #if( configASSERT_DEFINED == 1 )
162     {
163         /* Check alignment of the interrupt stack - which is the same as the
164          * stack that was being used by main() prior to the scheduler being
165          * started. */
166         configASSERT( ( xISRStackTop & portBYTE_ALIGNMENT_MASK ) == 0 );
167 
168         #ifdef configISR_STACK_SIZE_WORDS
169         {
170             memset( ( void * ) xISRStack, portISR_STACK_FILL_BYTE, sizeof( xISRStack ) );
171         }
172         #endif /* configISR_STACK_SIZE_WORDS */
173     }
174     #endif /* configASSERT_DEFINED */
175 
176     /* If there is a CLINT then it is ok to use the default implementation
177      * in this file, otherwise vPortSetupTimerInterrupt() must be implemented to
178      * configure whichever clock is to be used to generate the tick interrupt. */
179     vPortSetupTimerInterrupt();
180 
181     #if( ( configMTIME_BASE_ADDRESS != 0 ) && ( configMTIMECMP_BASE_ADDRESS != 0 ) )
182     {
183         /* Enable mtime and external interrupts.  1<<7 for timer interrupt,
184          * 1<<11 for external interrupt.  _RB_ What happens here when mtime is
185          * not present as with pulpino? */
186         __asm volatile( "csrs mie, %0" :: "r"(0x880) );
187     }
188     #endif /* ( configMTIME_BASE_ADDRESS != 0 ) && ( configMTIMECMP_BASE_ADDRESS != 0 ) */
189 
190     xPortStartFirstTask();
191 
192     /* Should not get here as after calling xPortStartFirstTask() only tasks
193      * should be executing. */
194     return pdFAIL;
195 }
196 /*-----------------------------------------------------------*/
197 
vPortEndScheduler(void)198 void vPortEndScheduler( void )
199 {
200     /* Not implemented. */
201     for( ;; );
202 }
203 /*-----------------------------------------------------------*/
204