1 /*
2  * FreeRTOS Kernel V11.1.0
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 directly in place of configCLINT_BASE_ADDRESS. See 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 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 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 kernel 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     {
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         portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE
118     }; \
119 
120     #define portCHECK_ISR_STACK()    configASSERT( ( memcmp( ( void * ) xISRStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) == 0 ) )
121 #else /* if defined( configISR_STACK_SIZE_WORDS ) && ( configCHECK_FOR_STACK_OVERFLOW > 2 ) */
122     /* Define the function away. */
123     #define portCHECK_ISR_STACK()
124 #endif /* configCHECK_FOR_STACK_OVERFLOW > 2 */
125 
126 /*-----------------------------------------------------------*/
127 
128 #if ( configMTIME_BASE_ADDRESS != 0 ) && ( configMTIMECMP_BASE_ADDRESS != 0 )
129 
vPortSetupTimerInterrupt(void)130     void vPortSetupTimerInterrupt( void )
131     {
132         uint32_t ulCurrentTimeHigh, ulCurrentTimeLow;
133         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. */
134         volatile uint32_t * const pulTimeLow = ( volatile uint32_t * const ) ( configMTIME_BASE_ADDRESS );
135         volatile uint32_t ulHartId;
136 
137         __asm volatile ( "csrr %0, mhartid" : "=r" ( ulHartId ) );
138 
139         pullMachineTimerCompareRegister = ( volatile uint64_t * ) ( ullMachineTimerCompareRegisterBase + ( ulHartId * sizeof( uint64_t ) ) );
140 
141         do
142         {
143             ulCurrentTimeHigh = *pulTimeHigh;
144             ulCurrentTimeLow = *pulTimeLow;
145         } while( ulCurrentTimeHigh != *pulTimeHigh );
146 
147         ullNextTime = ( uint64_t ) ulCurrentTimeHigh;
148         ullNextTime <<= 32ULL; /* High 4-byte word is 32-bits up. */
149         ullNextTime |= ( uint64_t ) ulCurrentTimeLow;
150         ullNextTime += ( uint64_t ) uxTimerIncrementsForOneTick;
151         *pullMachineTimerCompareRegister = ullNextTime;
152 
153         /* Prepare the time to use after the next tick interrupt. */
154         ullNextTime += ( uint64_t ) uxTimerIncrementsForOneTick;
155     }
156 
157 #endif /* ( configMTIME_BASE_ADDRESS != 0 ) && ( configMTIME_BASE_ADDRESS != 0 ) */
158 /*-----------------------------------------------------------*/
159 
xPortStartScheduler(void)160 BaseType_t xPortStartScheduler( void )
161 {
162     extern void xPortStartFirstTask( void );
163 
164     #if ( configASSERT_DEFINED == 1 )
165     {
166         /* Check alignment of the interrupt stack - which is the same as the
167          * stack that was being used by main() prior to the scheduler being
168          * started. */
169         configASSERT( ( xISRStackTop & portBYTE_ALIGNMENT_MASK ) == 0 );
170 
171         #ifdef configISR_STACK_SIZE_WORDS
172         {
173             memset( ( void * ) xISRStack, portISR_STACK_FILL_BYTE, sizeof( xISRStack ) );
174         }
175         #endif /* configISR_STACK_SIZE_WORDS */
176     }
177     #endif /* configASSERT_DEFINED */
178 
179     /* If there is a CLINT then it is ok to use the default implementation
180      * in this file, otherwise vPortSetupTimerInterrupt() must be implemented to
181      * configure whichever clock is to be used to generate the tick interrupt. */
182     vPortSetupTimerInterrupt();
183 
184     #if ( ( configMTIME_BASE_ADDRESS != 0 ) && ( configMTIMECMP_BASE_ADDRESS != 0 ) )
185     {
186         /* Enable mtime and external interrupts.  1<<7 for timer interrupt,
187          * 1<<11 for external interrupt.  _RB_ What happens here when mtime is
188          * not present as with pulpino? */
189         __asm volatile ( "csrs mie, %0" ::"r" ( 0x880 ) );
190     }
191     #endif /* ( configMTIME_BASE_ADDRESS != 0 ) && ( configMTIMECMP_BASE_ADDRESS != 0 ) */
192 
193     xPortStartFirstTask();
194 
195     /* Should not get here as after calling xPortStartFirstTask() only tasks
196      * should be executing. */
197     return pdFAIL;
198 }
199 /*-----------------------------------------------------------*/
200 
vPortEndScheduler(void)201 void vPortEndScheduler( void )
202 {
203     /* Not implemented. */
204     for( ; ; )
205     {
206     }
207 }
208 /*-----------------------------------------------------------*/
209