1 /*
2  * SPDX-FileCopyrightText: 2015-2019 Cadence Design Systems, Inc.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * SPDX-FileContributor: 2016-2022 Espressif Systems (Shanghai) CO LTD
7  */
8 
9 /*
10  * Copyright (c) 2015-2019 Cadence Design Systems, Inc.
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining
13  * a copy of this software and associated documentation files (the
14  * "Software"), to deal in the Software without restriction, including
15  * without limitation the rights to use, copy, modify, merge, publish,
16  * distribute, sublicense, and/or sell copies of the Software, and to
17  * permit persons to whom the Software is furnished to do so, subject to
18  * the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be included
21  * in all copies or substantial portions of the Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
26  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
27  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
28  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
29  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30  */
31 
32 /*******************************************************************************
33 *
34 *  Configuration-specific information for Xtensa build. This file must be
35 *  included in FreeRTOSConfig.h to properly set up the config-dependent
36 *  parameters correctly.
37 *
38 *  NOTE: To enable thread-safe C library support, XT_USE_THREAD_SAFE_CLIB must
39 *  be defined to be > 0 somewhere above or on the command line.
40 *
41 *******************************************************************************/
42 
43 #ifndef XTENSA_CONFIG_H
44 #define XTENSA_CONFIG_H
45 
46 /* *INDENT-OFF* */
47 #ifdef __cplusplus
48     extern "C" {
49 #endif
50 /* *INDENT-ON* */
51 
52 #include <xtensa/hal.h>
53 #include <xtensa/config/core.h>
54 #include <xtensa/config/system.h> /* required for XSHAL_CLIB */
55 
56 #include "xtensa_context.h"
57 
58 
59 /*-----------------------------------------------------------------------------
60  *                                 STACK REQUIREMENTS
61  *
62  * This section defines the minimum stack size, and the extra space required to
63  * be allocated for saving coprocessor state and/or C library state information
64  * (if thread safety is enabled for the C library). The sizes are in bytes.
65  *
66  * Stack sizes for individual tasks should be derived from these minima based on
67  * the maximum call depth of the task and the maximum level of interrupt nesting.
68  * A minimum stack size is defined by XT_STACK_MIN_SIZE. This minimum is based
69  * on the requirement for a task that calls nothing else but can be interrupted.
70  * This assumes that interrupt handlers do not call more than a few levels deep.
71  * If this is not true, i.e. one or more interrupt handlers make deep calls then
72  * the minimum must be increased.
73  *
74  * If the Xtensa processor configuration includes coprocessors, then space is
75  * allocated to save the coprocessor state on the stack.
76  *
77  * If thread safety is enabled for the C runtime library, (XT_USE_THREAD_SAFE_CLIB
78  * is defined) then space is allocated to save the C library context in the TCB.
79  *
80  * Allocating insufficient stack space is a common source of hard-to-find errors.
81  * During development, it is best to enable the FreeRTOS stack checking features.
82  *
83  * Usage:
84  *
85  * XT_USE_THREAD_SAFE_CLIB -- Define this to a nonzero value to enable thread-safe
86  *                            use of the C library. This will require extra stack
87  *                            space to be allocated for tasks that use the C library
88  *                            reentrant functions. See below for more information.
89  *
90  * NOTE: The Xtensa toolchain supports multiple C libraries and not all of them
91  * support thread safety. Check your core configuration to see which C library
92  * was chosen for your system.
93  *
94  * XT_STACK_MIN_SIZE       -- The minimum stack size for any task. It is recommended
95  *                            that you do not use a stack smaller than this for any
96  *                            task. In case you want to use stacks smaller than this
97  *                            size, you must verify that the smaller size(s) will work
98  *                            under all operating conditions.
99  *
100  * XT_STACK_EXTRA          -- The amount of extra stack space to allocate for a task
101  *                            that does not make C library reentrant calls. Add this
102  *                            to the amount of stack space required by the task itself.
103  *
104  * XT_STACK_EXTRA_CLIB     -- The amount of space to allocate for C library state.
105  *
106  *  -----------------------------------------------------------------------------*/
107 
108 /* Extra space required for interrupt/exception hooks. */
109 #ifdef XT_INTEXC_HOOKS
110     #ifdef __XTENSA_CALL0_ABI__
111         #define STK_INTEXC_EXTRA     0x200
112     #else
113         #define STK_INTEXC_EXTRA     0x180
114     #endif
115 #else
116     #define STK_INTEXC_EXTRA         0
117 #endif
118 
119 #define XT_CLIB_CONTEXT_AREA_SIZE    0
120 
121 /*------------------------------------------------------------------------------
122  *  Extra size -- interrupt frame plus coprocessor save area plus hook space.
123  *  NOTE: Make sure XT_INTEXC_HOOKS is undefined unless you really need the hooks.
124  *  ------------------------------------------------------------------------------*/
125 #ifdef __XTENSA_CALL0_ABI__
126     #define XT_XTRA_SIZE    ( XT_STK_FRMSZ + STK_INTEXC_EXTRA + 0x10 + XT_CP_SIZE )
127 #else
128     #define XT_XTRA_SIZE    ( XT_STK_FRMSZ + STK_INTEXC_EXTRA + 0x20 + XT_CP_SIZE )
129 #endif
130 
131 /*------------------------------------------------------------------------------
132  *  Space allocated for user code -- function calls and local variables.
133  *  NOTE: This number can be adjusted to suit your needs. You must verify that the
134  *  amount of space you reserve is adequate for the worst-case conditions in your
135  *  application.
136  *  NOTE: The windowed ABI requires more stack, since space has to be reserved
137  *  for spilling register windows.
138  *  ------------------------------------------------------------------------------*/
139 #ifdef __XTENSA_CALL0_ABI__
140     #define XT_USER_SIZE    0x200
141 #else
142     #define XT_USER_SIZE    0x400
143 #endif
144 
145 /* Minimum recommended stack size. */
146 #define XT_STACK_MIN_SIZE      ( ( XT_XTRA_SIZE + XT_USER_SIZE ) / sizeof( unsigned char ) )
147 
148 /* OS overhead with and without C library thread context. */
149 #define XT_STACK_EXTRA         ( XT_XTRA_SIZE )
150 #define XT_STACK_EXTRA_CLIB    ( XT_XTRA_SIZE + XT_CLIB_CONTEXT_AREA_SIZE )
151 
152 /* *INDENT-OFF* */
153 #ifdef __cplusplus
154     }
155 #endif
156 /* *INDENT-ON* */
157 
158 #endif /* XTENSA_CONFIG_H */
159