1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** ThreadX Component                                                     */
17 /**                                                                       */
18 /**   Port Specific                                                       */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 
24 /**************************************************************************/
25 /*                                                                        */
26 /*  PORT SPECIFIC C INFORMATION                            RELEASE        */
27 /*                                                                        */
28 /*    tx_port.h                                          RISC-V64/GNU     */
29 /*                                                           6.2.1        */
30 /*                                                                        */
31 /*  AUTHOR                                                                */
32 /*                                                                        */
33 /*    Scott Larson, Microsoft Corporation                                 */
34 /*                                                                        */
35 /*  DESCRIPTION                                                           */
36 /*                                                                        */
37 /*    This file contains data type definitions that make the ThreadX      */
38 /*    real-time kernel function identically on a variety of different     */
39 /*    processor architectures.  For example, the size or number of bits   */
40 /*    in an "int" data type vary between microprocessor architectures and */
41 /*    even C compilers for the same microprocessor.  ThreadX does not     */
42 /*    directly use native C data types.  Instead, ThreadX creates its     */
43 /*    own special types that can be mapped to actual data types by this   */
44 /*    file to guarantee consistency in the interface and functionality.   */
45 /*                                                                        */
46 /*  RELEASE HISTORY                                                       */
47 /*                                                                        */
48 /*    DATE              NAME                      DESCRIPTION             */
49 /*                                                                        */
50 /*  03-08-2023      Scott Larson            Initial Version 6.2.1         */
51 /*                                                                        */
52 /**************************************************************************/
53 
54 #ifndef TX_PORT_H
55 #define TX_PORT_H
56 
57 #ifdef __ASSEMBLER__
58 
59 
60 #if __riscv_xlen == 64
61 # define SLL32    sllw
62 # define STORE    sd
63 # define LOAD     ld
64 # define LWU      lwu
65 # define LOG_REGBYTES 3
66 #else
67 # define SLL32    sll
68 # define STORE    sw
69 # define LOAD     lw
70 # define LWU      lw
71 # define LOG_REGBYTES 2
72 #endif
73 #define REGBYTES (1 << LOG_REGBYTES)
74 
75 #else   /*not __ASSEMBLER__ */
76 
77 /* Include for memset.  */
78 #include <string.h>
79 
80 
81 /* Determine if the optional ThreadX user define file should be used.  */
82 
83 #ifdef TX_INCLUDE_USER_DEFINE_FILE
84 
85 
86 /* Yes, include the user defines in tx_user.h. The defines in this file may
87    alternately be defined on the command line.  */
88 
89 #include "tx_user.h"
90 #endif
91 
92 
93 /* Define compiler library include files.  */
94 
95 
96 /* Define ThreadX basic types for this port.  */
97 
98 #define VOID                                    void
99 typedef char                                    CHAR;
100 typedef unsigned char                           UCHAR;
101 typedef int                                     INT;
102 typedef unsigned int                            UINT;
103 typedef long                                    LONG;
104 typedef unsigned long                           ULONG;
105 typedef unsigned long long                      ULONG64;
106 typedef short                                   SHORT;
107 typedef unsigned short                          USHORT;
108 #define ULONG64_DEFINED
109 #define ALIGN_TYPE_DEFINED
110 #define ALIGN_TYPE                              ULONG64
111 
112 
113 
114 
115 /* Define the priority levels for ThreadX.  Legal values range
116    from 32 to 1024 and MUST be evenly divisible by 32.  */
117 
118 #ifndef TX_MAX_PRIORITIES
119 #define TX_MAX_PRIORITIES                       32
120 #endif
121 
122 
123 /* Define the minimum stack for a ThreadX thread on this processor. If the size supplied during
124    thread creation is less than this value, the thread create call will return an error.  */
125 
126 #ifndef TX_MINIMUM_STACK
127 #define TX_MINIMUM_STACK                        1024        /* Minimum stack size for this port  */
128 #endif
129 
130 
131 /* Define the system timer thread's default stack size and priority.  These are only applicable
132    if TX_TIMER_PROCESS_IN_ISR is not defined.  */
133 
134 #ifndef TX_TIMER_THREAD_STACK_SIZE
135 #define TX_TIMER_THREAD_STACK_SIZE              1024        /* Default timer thread stack size  */
136 #endif
137 
138 #ifndef TX_TIMER_THREAD_PRIORITY
139 #define TX_TIMER_THREAD_PRIORITY                0           /* Default timer thread priority    */
140 #endif
141 
142 
143 /* Define various constants for the ThreadX RISC-V port.  */
144 
145 #define TX_INT_DISABLE                          0x00000000  /* Disable interrupts value */
146 #define TX_INT_ENABLE                           0x00000008  /* Enable interrupt value   */
147 
148 
149 /* Define the clock source for trace event entry time stamp. The following two item are port specific.
150    For example, if the time source is at the address 0x0a800024 and is 16-bits in size, the clock
151    source constants would be:
152 
153 #define TX_TRACE_TIME_SOURCE                    *((ULONG *) 0x0a800024)
154 #define TX_TRACE_TIME_MASK                      0x0000FFFFUL
155 
156 */
157 
158 #ifndef TX_TRACE_TIME_SOURCE
159 #define TX_TRACE_TIME_SOURCE                    ++_tx_trace_simulated_time
160 #endif
161 #ifndef TX_TRACE_TIME_MASK
162 #define TX_TRACE_TIME_MASK                      0xFFFFFFFFUL
163 #endif
164 
165 
166 /* Define the port specific options for the _tx_build_options variable. This variable indicates
167    how the ThreadX library was built.  */
168 
169 #define TX_PORT_SPECIFIC_BUILD_OPTIONS          0
170 
171 
172 /* Define the in-line initialization constant so that modules with in-line
173    initialization capabilities can prevent their initialization from being
174    a function call.  */
175 
176 #define TX_INLINE_INITIALIZATION
177 
178 
179 /* Determine whether or not stack checking is enabled. By default, ThreadX stack checking is
180    disabled. When the following is defined, ThreadX thread stack checking is enabled.  If stack
181    checking is enabled (TX_ENABLE_STACK_CHECKING is defined), the TX_DISABLE_STACK_FILLING
182    define is negated, thereby forcing the stack fill which is necessary for the stack checking
183    logic.  */
184 
185 #ifdef TX_ENABLE_STACK_CHECKING
186 #undef TX_DISABLE_STACK_FILLING
187 #endif
188 
189 
190 /* Define the TX_THREAD control block extensions for this port. The main reason
191    for the multiple macros is so that backward compatibility can be maintained with
192    existing ThreadX kernel awareness modules.  */
193 
194 #define TX_THREAD_EXTENSION_0
195 #define TX_THREAD_EXTENSION_1
196 #define TX_THREAD_EXTENSION_2
197 #define TX_THREAD_EXTENSION_3
198 
199 
200 /* Define the port extensions of the remaining ThreadX objects.  */
201 
202 #define TX_BLOCK_POOL_EXTENSION
203 #define TX_BYTE_POOL_EXTENSION
204 #define TX_EVENT_FLAGS_GROUP_EXTENSION
205 #define TX_MUTEX_EXTENSION
206 #define TX_QUEUE_EXTENSION
207 #define TX_SEMAPHORE_EXTENSION
208 #define TX_TIMER_EXTENSION
209 
210 
211 /* Define the user extension field of the thread control block.  Nothing
212    additional is needed for this port so it is defined as white space.  */
213 
214 #ifndef TX_THREAD_USER_EXTENSION
215 #define TX_THREAD_USER_EXTENSION
216 #endif
217 
218 
219 /* Define the macros for processing extensions in tx_thread_create, tx_thread_delete,
220    tx_thread_shell_entry, and tx_thread_terminate.  */
221 
222 #define TX_THREAD_CREATE_EXTENSION(thread_ptr)
223 #define TX_THREAD_DELETE_EXTENSION(thread_ptr)
224 #define TX_THREAD_COMPLETED_EXTENSION(thread_ptr)
225 #define TX_THREAD_TERMINATED_EXTENSION(thread_ptr)
226 
227 
228 /* Define the ThreadX object creation extensions for the remaining objects.  */
229 
230 #define TX_BLOCK_POOL_CREATE_EXTENSION(pool_ptr)
231 #define TX_BYTE_POOL_CREATE_EXTENSION(pool_ptr)
232 #define TX_EVENT_FLAGS_GROUP_CREATE_EXTENSION(group_ptr)
233 #define TX_MUTEX_CREATE_EXTENSION(mutex_ptr)
234 #define TX_QUEUE_CREATE_EXTENSION(queue_ptr)
235 #define TX_SEMAPHORE_CREATE_EXTENSION(semaphore_ptr)
236 #define TX_TIMER_CREATE_EXTENSION(timer_ptr)
237 
238 
239 /* Define the ThreadX object deletion extensions for the remaining objects.  */
240 
241 #define TX_BLOCK_POOL_DELETE_EXTENSION(pool_ptr)
242 #define TX_BYTE_POOL_DELETE_EXTENSION(pool_ptr)
243 #define TX_EVENT_FLAGS_GROUP_DELETE_EXTENSION(group_ptr)
244 #define TX_MUTEX_DELETE_EXTENSION(mutex_ptr)
245 #define TX_QUEUE_DELETE_EXTENSION(queue_ptr)
246 #define TX_SEMAPHORE_DELETE_EXTENSION(semaphore_ptr)
247 #define TX_TIMER_DELETE_EXTENSION(timer_ptr)
248 
249 
250 /* Define ThreadX interrupt lockout and restore macros for protection on
251    access of critical kernel information.  The restore interrupt macro must
252    restore the interrupt posture of the running thread prior to the value
253    present prior to the disable macro.  In most cases, the save area macro
254    is used to define a local function save area for the disable and restore
255    macros.  */
256 
257 #ifdef TX_DISABLE_INLINE
258 
259 ULONG64                                         _tx_thread_interrupt_control(unsigned int new_posture);
260 
261 #define TX_INTERRUPT_SAVE_AREA                  register ULONG64 interrupt_save;
262 
263 #define TX_DISABLE                              interrupt_save =  _tx_thread_interrupt_control(TX_INT_DISABLE);
264 #define TX_RESTORE                              _tx_thread_interrupt_control(interrupt_save);
265 
266 #else
267 
268 #define TX_INTERRUPT_SAVE_AREA                  ULONG64 interrupt_save;
269 /* Atomically read mstatus into interrupt_save and clear bit 3 of mstatus.  */
270 #define TX_DISABLE                              {__asm__ ("csrrci %0, mstatus, 0x08" : "=r" (interrupt_save) : );};
271 /* We only care about mstatus.mie (bit 3), so mask interrupt_save and write to mstatus.  */
272 #define TX_RESTORE                              {register ULONG64 __tempmask = interrupt_save & 0x08; \
273                                                 __asm__ ("csrrs x0, mstatus, %0 \n\t" : : "r" (__tempmask) : );};
274 
275 #endif
276 
277 
278 /* Define the interrupt lockout macros for each ThreadX object.  */
279 
280 #define TX_BLOCK_POOL_DISABLE                   TX_DISABLE
281 #define TX_BYTE_POOL_DISABLE                    TX_DISABLE
282 #define TX_EVENT_FLAGS_GROUP_DISABLE            TX_DISABLE
283 #define TX_MUTEX_DISABLE                        TX_DISABLE
284 #define TX_QUEUE_DISABLE                        TX_DISABLE
285 #define TX_SEMAPHORE_DISABLE                    TX_DISABLE
286 
287 
288 /* Define the version ID of ThreadX.  This may be utilized by the application.  */
289 
290 #ifdef TX_THREAD_INIT
291 CHAR                            _tx_version_id[] =
292                                     "Copyright (c) Microsoft Corporation. All rights reserved. * ThreadX RISC-V64/GNU Version 6.2.1 *";
293 #else
294 extern  CHAR                    _tx_version_id[];
295 #endif
296 
297 #endif   /*not __ASSEMBLER__ */
298 #endif
299