1 
2 /**************************************************************************/
3 /*                                                                        */
4 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
5 /*                                                                        */
6 /*       This software is licensed under the Microsoft Software License   */
7 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
8 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
9 /*       and in the root directory of this software.                      */
10 /*                                                                        */
11 /**************************************************************************/
12 
13 /**************************************************************************/
14 /**                                                                       */
15 /** ThreadX Component                                                     */
16 /**                                                                       */
17 /**   Port Specific                                                       */
18 /**                                                                       */
19 /**************************************************************************/
20 
21 /**************************************************************************/
22 /*                                                                        */
23 /*  DESCRIPTION                                                           */
24 /*                                                                        */
25 /*    This file contains data type definitions that make the ThreadX      */
26 /*    real-time kernel function identically on a variety of different     */
27 /*    processor architectures.  For example, the size or number of bits   */
28 /*    in an "int" data type vary between microprocessor architectures and */
29 /*    even C compilers for the same microprocessor.  ThreadX does not     */
30 /*    directly use native C data types.  Instead, ThreadX creates its     */
31 /*    own special types that can be mapped to actual data types by this   */
32 /*    file to guarantee consistency in the interface and functionality.   */
33 /*                                                                        */
34 /*  RELEASE HISTORY                                                       */
35 /*                                                                        */
36 /*    DATE              NAME                      DESCRIPTION             */
37 /*                                                                        */
38 /*  11-09-2020      Cadence Design Systems  Initial Version 6.1.2         */
39 /*  04-02-2021      Bhupendra Naphade       Modified comment(s), updated  */
40 /*                                            macro definition,           */
41 /*                                            resulting in version 6.1.6  */
42 /*  10-31-2022      Scott Larson            Modified comment(s), removed  */
43 /*                                            EPK extension,              */
44 /*                                            resulting in version 6.2.0  */
45 /*                                                                        */
46 /**************************************************************************/
47 
48 #ifndef TX_PORT_H
49 #define TX_PORT_H
50 
51 /* Determine if the optional ThreadX user define file should be used.  */
52 
53 #ifdef TX_INCLUDE_USER_DEFINE_FILE
54 #include "tx_user.h"
55 #endif
56 
57 /* Comment this out to disable thread-safe C library support. */
58 #define TX_THREAD_SAFE_CLIB    1
59 
60 /* Include the glue to Xtensa-generic parts of this ThreadX port. */
61 #include "xtensa_rtos.h"
62 
63 /* Parts of this file should not been seen by assembler sources. */
64 #ifndef __ASSEMBLER__
65 
66 /* Some generic C sources call memset() and need this (else compiler warns).
67    Until the generic sources take care of this, do it here. */
68 
69 #include <string.h>
70 
71 
72 /* Define compiler library include files and library-specific macros. */
73 
74 /*
75     Thread-safety support for the supported C libraries. At this time
76     only the newlib and Xtensa C libraries are supported.
77 
78     The C library reent structure can be quite large so it is placed
79     at the end of TX_THREAD, and a pointer to it is defined near the
80     beginning of TX_THREAD where assembly code can easily get to it
81     at a fixed offset.
82 */
83 
84 #ifdef TX_THREAD_SAFE_CLIB
85 struct TX_THREAD_STRUCT;
86 
87 extern char *_tx_clib_heap_start, *_tx_clib_heap_end;
88 extern void _tx_clib_reent_init (struct TX_THREAD_STRUCT *thread_ptr);
89 extern void _tx_clib_reent_cleanup (struct TX_THREAD_STRUCT *thread_ptr, int partial);
90 extern void _tx_clib_thread_setup(struct TX_THREAD_STRUCT *thread_ptr);
91 
92 #include    <sys/reent.h>
93 
94 #define TX_CLIB_THREAD_EXTENSION \
95     VOID                (*tx_real_thread_entry)(ULONG id);  /* Actual entry point */ \
96     struct _reent *     tx_thread_clib_ptr;                 /* C lib reentrancy ptr */
97 #define TX_CLIB_THREAD_EXTENSION_END \
98     struct _reent       tx_thread_clib_reent;               /* C lib reentrancy struct */
99 #define TX_CLIB_THREAD_CREATE_EXTENSION(thread_ptr)
100 #define TX_CLIB_THREAD_DELETE_EXTENSION(thread_ptr)
101 #define TX_CLIB_THREAD_EXIT_EXTENSION(thread_ptr)
102 #define TX_THREAD_CLIB_EXTENSION(thread_ptr) \
103     _tx_clib_thread_setup(thread_ptr);
104 #else
105 #define TX_CLIB_THREAD_EXTENSION
106 #define TX_CLIB_THREAD_EXTENSION_END
107 #define TX_CLIB_THREAD_CREATE_EXTENSION(thread_ptr)
108 #define TX_CLIB_THREAD_DELETE_EXTENSION(thread_ptr)
109 #define TX_CLIB_THREAD_EXIT_EXTENSION(thread_ptr)
110 #define TX_THREAD_CLIB_EXTENSION(thread_ptr)
111 #endif
112 
113 
114 /* Define ThreadX basic types for this port.  */
115 
116 #define VOID                                    void
117 typedef char                                    CHAR;
118 typedef unsigned char                           UCHAR;
119 typedef int                                     INT;
120 typedef unsigned int                            UINT;
121 typedef long                                    LONG;
122 typedef unsigned long                           ULONG;
123 typedef short                                   SHORT;
124 typedef unsigned short                          USHORT;
125 
126 
127 #endif /* #ifndef __ASSEMBLER__ */
128 
129 
130 /* Define the priority levels for ThreadX.  Legal values range
131    from 32 to 1024 and MUST be evenly divisible by 32.  */
132 
133 #ifndef TX_MAX_PRIORITIES
134 #define TX_MAX_PRIORITIES                       32
135 #endif
136 
137 
138 /*
139     Define the minimum stack size for a thread on this processor.
140     If the size supplied during thread creation is less than TX_MINIMUM_STACK,
141     the thread create call will return an error. The minimum allows for a
142     thread whose entry function makes no calls and needs no local frame.
143     TX_MINIMUM_STACK_BASIC allows the entry function to at least call
144     tx_thread_relinquish(). An extra 0x10 bytes is allowed in all cases to
145     allow for stack pointer alignment to 16 bytes. There is an additional premium
146     for the stack checking functionality of TX_ENABLE_STACK_CHECKING.
147     In Xtensa, all these amounts depend on the function call ABI used by the
148     configuration (in general, Call0 ABI needs about 0x20 bytes less stack space
149     per function call frame). These amounts assume no compiler optimization (-O0).
150     Optimization usually requires less stack.
151 
152     TX_MINIMUM_STACK_BASIC is a MINIMUM for threads that call tx_thread_relinquish()
153     only. Threads that do more, and in particular call C library functions such as
154     printf(), need much more stack space and it is up to the application developer
155     to determine how much.
156 */
157 
158 #ifdef __XTENSA_CALL0_ABI__
159         /* Call0 ABI */
160 
161 #ifndef TX_MINIMUM_STACK
162 #define TX_MINIMUM_STACK                        (XT_STK_FRMSZ + 0x10)
163 #endif
164 #ifdef  TX_ENABLE_STACK_CHECKING
165 #define TX_STACK_CHECK_PREMIUM                  0x30
166 #else
167 #define TX_STACK_CHECK_PREMIUM                  0
168 #endif
169 #ifndef TX_MINIMUM_STACK_BASIC
170 #define TX_MINIMUM_STACK_BASIC                  (XT_STK_FRMSZ + 0x70 + TX_STACK_CHECK_PREMIUM)
171 #endif
172 
173 #else   /* Windowed ABI */
174 
175 #ifndef TX_MINIMUM_STACK
176 #define TX_MINIMUM_STACK                        (XT_STK_FRMSZ + 0x20)
177 #endif
178 #ifdef  TX_ENABLE_STACK_CHECKING
179 #define TX_STACK_CHECK_PREMIUM                  0x50
180 #else
181 #define TX_STACK_CHECK_PREMIUM                  0
182 #endif
183 #ifndef TX_MINIMUM_STACK_BASIC
184 #define TX_MINIMUM_STACK_BASIC                  (XT_STK_FRMSZ + 0x100 + TX_STACK_CHECK_PREMIUM)
185 #endif
186 
187 #endif
188 
189 /*
190     Minimum stack size for the ThreadX system stack on this processor.
191     This is just a useful starting point for an application, it is not
192     checked by ThreadX. The minimum system stack size allows for the
193     possible depth of interrupt nesting (XCHAL_EXCM_LEVEL-1 interrupt
194     stack frames and XCHAL_EXCM_LEVEL interrupt handlers including timer),
195     assuming very basic interrupt handlers (allows 1 call12). It needs to
196     be increased to support the application's real interrupt handlers (and
197     timer interrupt if TX_TIMER_PROCESS_IN_ISR). The system stack is located
198     where the stack pointer is inside tx_kernel_enter() which is usually from
199     main(), and so is determined by the development tools. It grows downward
200     toward the first available memory pointer passed to tx_application_define().
201     An application should allow sufficient space for the system stack.
202 
203     For XEA3, allow a minimum of XCHAL_NUM_INTLEVELS nested interrupts. The stack
204     frames are minimal-sized and may need to be increased to support real applications.
205 */
206 
207 #if XCHAL_HAVE_XEA3
208 #define TX_SYSTEM_STACK_MINIMUM                 (XCHAL_NUM_INTLEVELS * 0x40)
209 #ifndef TX_SYSTEM_STACK_SIZE
210 #if TX_SYSTEM_STACK_MINIMUM > 2048
211 #define TX_SYSTEM_STACK_SIZE                    TX_SYSTEM_STACK_MINIMUM
212 #else
213 #define TX_SYSTEM_STACK_SIZE                    2048
214 #endif
215 #endif
216 #else
217 #define TX_SYSTEM_STACK_MINIMUM                 (((XCHAL_EXCM_LEVEL-1) * (0x40 + XT_STK_FRMSZ)) + 0x40)
218 #ifndef TX_SYSTEM_STACK_SIZE
219 #define TX_SYSTEM_STACK_SIZE                    4096
220 #endif
221 #endif
222 
223 /*
224     Define the system timer thread's default stack size and priority.
225     These are only applicable if TX_TIMER_PROCESS_IN_ISR is not defined.
226 */
227 
228 #ifndef TX_TIMER_THREAD_STACK_SIZE
229 #ifdef __XTENSA_CALL0_ABI__
230 #define TX_TIMER_THREAD_STACK_SIZE              (TX_MINIMUM_STACK_BASIC + 0x100)
231 #else
232 #define TX_TIMER_THREAD_STACK_SIZE              (TX_MINIMUM_STACK_BASIC + 0x200)
233 #endif
234 #endif
235 
236 
237 /* Parts of this file should not been seen by assembler sources. */
238 #ifndef __ASSEMBLER__
239 
240 
241 #ifndef TX_TIMER_THREAD_PRIORITY
242 #define TX_TIMER_THREAD_PRIORITY                0           /* Default timer thread priority    */
243 #endif
244 
245 
246 /* Define various constants for the ThreadX Xtensa port.  */
247 
248 #if XCHAL_HAVE_XEA3
249 #define TX_INT_DISABLE                          0x8               /* Disable interrupts value */
250 #define TX_INT_ENABLE                           0x0               /* Enable interrupt value   */
251 #else
252 #define TX_INT_DISABLE                          XCHAL_EXCM_LEVEL  /* Disable interrupts value */
253 #define TX_INT_ENABLE                           0x0               /* Enable interrupt value   */
254 #endif
255 
256 
257 /*
258     Define the clock source for trace event entry time stamp. The following
259     two item are port specific. For example, if the time source is at the
260     address 0x0a800024 and is 16-bits in size, the clock source constants
261     would be:
262 
263     #define TX_TRACE_TIME_SOURCE                *((ULONG *) 0x0a800024)
264     #define TX_TRACE_TIME_MASK                  0x0000FFFFUL
265 */
266 
267 #ifndef TX_TRACE_TIME_SOURCE
268 #define TX_TRACE_TIME_SOURCE                    ++_tx_trace_simulated_time
269 #endif
270 #ifndef TX_TRACE_TIME_MASK
271 #define TX_TRACE_TIME_MASK                      0xFFFFFFFFUL
272 #endif
273 
274 
275 /*
276     Define the port specific options for the _tx_build_options variable.
277     This variable indicates how the ThreadX library was built.
278 */
279 
280 #if defined(XT_SIMULATOR) || !defined(XT_BOARD)
281 #define TX_XT_OPT_SIMULATOR                     (1UL << 0)
282 #else
283 #define TX_XT_OPT_SIMULATOR                     0
284 #endif
285 
286 #if defined(XT_BOARD)
287 #define TX_XT_OPT_BOARD                         (1UL << 1)
288 #else
289 #define TX_XT_OPT_BOARD                         0
290 #endif
291 
292 #if defined(XT_INTEXC_HOOKS)
293 #define TX_XT_OPT_INTEXC_HOOKS                  (1UL << 2)
294 #else
295 #define TX_XT_OPT_INTEXC_HOOKS                  0
296 #endif
297 
298 #if defined(TX_THREAD_SAFE_CLIB)
299 #define TX_XT_OPT_CLIB                          (1UL << 3)
300 #else
301 #define TX_XT_OPT_CLIB                          0
302 #endif
303 
304 #define TX_PORT_SPECIFIC_BUILD_OPTIONS          (TX_XT_OPT_SIMULATOR | TX_XT_OPT_BOARD \
305                                                 | TX_XT_OPT_INTEXC_HOOKS | TX_XT_OPT_CLIB)
306 
307 
308 /*
309     Define the in-line initialization constant so that modules with in-line
310     initialization capabilities can prevent their initialization from being
311     a function call.
312 */
313 
314 #define TX_INLINE_INITIALIZATION
315 
316 
317 /*
318     Determine whether or not stack checking is enabled. By default, ThreadX
319     stack checking is disabled. When the following is defined, ThreadX thread
320     stack checking is enabled. If enabled (TX_ENABLE_STACK_CHECKING is defined),
321     the TX_DISABLE_STACK_FILLING define is canceled, thereby forcing the stack
322     fill which is necessary for the stack checking logic.
323 */
324 
325 #ifdef TX_ENABLE_STACK_CHECKING
326 #undef TX_DISABLE_STACK_FILLING
327 #endif
328 
329 
330 /*
331     Define the TX_THREAD control block extensions for this port. The main
332     reason for the multiple macros is so that backward compatibility can
333     be maintained with existing ThreadX kernel awareness modules.
334 */
335 
336 #if XCHAL_CP_NUM > 0
337 #define TX_CP_THREAD_EXTENSION  \
338     /* Co-proc state save areas, with padding for alignment: */ \
339     UINT                tx_thread_cp_state[(XT_CP_SIZE+3)/4];
340 #else
341 #define TX_CP_THREAD_EXTENSION
342 #endif
343 
344 #define TX_THREAD_EXTENSION_0 \
345     /* These extensions needs to be accessed from assembly code at context switches */ \
346     UINT                tx_thread_solicited;    /* Non-zero indicates solicited entry */ \
347     TX_CLIB_THREAD_EXTENSION                    /* Ptr to C library re-ent struct */ \
348     TX_CP_THREAD_EXTENSION          /* Coprocessor state save areas */
349 
350 #define TX_THREAD_EXTENSION_1 \
351     TX_CLIB_THREAD_EXTENSION_END
352 
353 #define TX_THREAD_EXTENSION_2
354 
355 /* Execution profile related */
356 #define TX_THREAD_EXTENSION_3
357 
358 /* Define the port extensions of the remaining ThreadX objects.  */
359 
360 #define TX_BLOCK_POOL_EXTENSION
361 #define TX_BYTE_POOL_EXTENSION
362 #define TX_EVENT_FLAGS_GROUP_EXTENSION
363 #define TX_MUTEX_EXTENSION
364 #define TX_QUEUE_EXTENSION
365 #define TX_SEMAPHORE_EXTENSION
366 #define TX_TIMER_EXTENSION
367 
368 
369 /* Define the user extension field of the thread control block.  Nothing
370    additional is needed for this port so it is defined as white space.  */
371 
372 #ifndef TX_THREAD_USER_EXTENSION
373 #define TX_THREAD_USER_EXTENSION
374 #endif
375 
376 
377 /* Define the macros for processing extensions in tx_thread_create, tx_thread_delete,
378    tx_thread_shell_entry, and tx_thread_terminate.  */
379 
380 #if XCHAL_CP_NUM > 0
381 extern void _xt_coproc_release(void * coproc_sa_base);
382 
383 #define TX_THREAD_DELETE_EXTENSION(thread_ptr) \
384         _xt_coproc_release(&thread_ptr->tx_thread_cp_state);
385 #define TX_THREAD_COMPLETED_EXTENSION(thread_ptr) \
386         _xt_coproc_release(&thread_ptr->tx_thread_cp_state);
387 #define TX_THREAD_TERMINATED_EXTENSION(thread_ptr) \
388         _xt_coproc_release(&thread_ptr->tx_thread_cp_state);
389 #define TX_THREAD_CP_EXTENSION(thread_ptr) \
390     /* Initialize XT_CP_ASA ptr to aligned save area: */ \
391     /* NOTE: keep this matched with xtensa_context.h. */ \
392     thread_ptr->tx_thread_cp_state[0] = 0; \
393     thread_ptr->tx_thread_cp_state[1] = 0; \
394     thread_ptr->tx_thread_cp_state[2] = \
395         ((((UINT)thread_ptr->tx_thread_cp_state)+12+XCHAL_TOTAL_SA_ALIGN-1) \
396             & -XCHAL_TOTAL_SA_ALIGN);
397 #else
398 #define TX_THREAD_DELETE_EXTENSION(thread_ptr)
399 #define TX_THREAD_TERMINATED_EXTENSION(thread_ptr)
400 #define TX_THREAD_COMPLETED_EXTENSION(thread_ptr)
401 #define TX_THREAD_CP_EXTENSION(thread_ptr)
402 #endif
403 
404 #define TX_THREAD_CREATE_EXTENSION(thread_ptr) \
405     TX_THREAD_CLIB_EXTENSION(thread_ptr) \
406     TX_THREAD_CP_EXTENSION(thread_ptr)
407 
408 
409 /* Define the ThreadX object creation extensions for the remaining objects.  */
410 
411 #define TX_BLOCK_POOL_CREATE_EXTENSION(pool_ptr)
412 #define TX_BYTE_POOL_CREATE_EXTENSION(pool_ptr)
413 #define TX_EVENT_FLAGS_GROUP_CREATE_EXTENSION(group_ptr)
414 #define TX_MUTEX_CREATE_EXTENSION(mutex_ptr)
415 #define TX_QUEUE_CREATE_EXTENSION(queue_ptr)
416 #define TX_SEMAPHORE_CREATE_EXTENSION(semaphore_ptr)
417 #define TX_TIMER_CREATE_EXTENSION(timer_ptr)
418 
419 
420 /* Define the ThreadX object deletion extensions for the remaining objects.  */
421 
422 #define TX_BLOCK_POOL_DELETE_EXTENSION(pool_ptr)
423 #define TX_BYTE_POOL_DELETE_EXTENSION(pool_ptr)
424 #define TX_EVENT_FLAGS_GROUP_DELETE_EXTENSION(group_ptr)
425 #define TX_MUTEX_DELETE_EXTENSION(mutex_ptr)
426 #define TX_QUEUE_DELETE_EXTENSION(queue_ptr)
427 #define TX_SEMAPHORE_DELETE_EXTENSION(semaphore_ptr)
428 #define TX_TIMER_DELETE_EXTENSION(timer_ptr)
429 
430 
431 /*
432     Define ThreadX interrupt lockout and restore macros for protection
433     on access of critical kernel information. The restore interrupt macro
434     must restore the interrupt posture of the running thread prior to the
435     value present prior to the disable macro. In most cases, the save area
436     macro is used to define a local function save area for the disable and
437     restore macros.
438 */
439 
440 extern unsigned int                 _tx_thread_interrupt_control(unsigned int new_posture);
441 
442 #define TX_INTERRUPT_SAVE_AREA      register UINT interrupt_save;
443 
444 #ifdef TX_DISABLE_INLINE_MACROS
445 #define TX_DISABLE                  interrupt_save = _tx_thread_interrupt_control(TX_INT_DISABLE);
446 #define TX_RESTORE                  _tx_thread_interrupt_control(interrupt_save);
447 #else
448 #define TX_DISABLE                  interrupt_save = xthal_disable_interrupts();
449 #define TX_RESTORE                  xthal_restore_interrupts(interrupt_save);
450 #endif
451 
452 
453 /* Define the interrupt lockout macros for each ThreadX object.  */
454 
455 #define TX_BLOCK_POOL_DISABLE                   TX_DISABLE
456 #define TX_BYTE_POOL_DISABLE                    TX_DISABLE
457 #define TX_EVENT_FLAGS_GROUP_DISABLE            TX_DISABLE
458 #define TX_MUTEX_DISABLE                        TX_DISABLE
459 #define TX_QUEUE_DISABLE                        TX_DISABLE
460 #define TX_SEMAPHORE_DISABLE                    TX_DISABLE
461 
462 
463 #if XCHAL_HAVE_XEA3
464 /* Variables that keep track of the timer and software interrupt numbers in use. */
465 extern int xt_sw_intnum;
466 extern int xt_timer_intnum;
467 #endif
468 
469 
470 /* Define the version ID of ThreadX.  This may be utilized by the application.  */
471 
472 #ifdef TX_THREAD_INIT
473 CHAR                            _tx_version_id[] =
474                                     "Copyright (c) Microsoft Corporation. All rights reserved. * Azure RTOS Xtensa Version 6.3.0 *";
475 #else
476 extern  CHAR                    _tx_version_id[];
477 #endif
478 
479 #endif /* #ifndef __ASSEMBLER__ */
480 
481 #endif
482 
483