Define memory barrier for arm core, and use it.
[bertos.git] / cpu / attr.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2004, 2005, 2006, 2007 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 2004 Giovanni Bajo
31  *
32  * -->
33  *
34  * \brief CPU-specific attributes.
35  *
36  * \author Giovanni Bajo <rasky@develer.com>
37  * \author Bernardo Innocenti <bernie@develer.com>
38  * \author Stefano Fedrigo <aleph@develer.com>
39  * \author Francesco Sacchi <batt@develer.com>
40  */
41 #ifndef CPU_ATTR_H
42 #define CPU_ATTR_H
43
44 #include "detect.h"
45 #include <cfg/compiler.h> /* for uintXX_t */
46 #include <cfg/arch_config.h>  /* ARCH_EMUL */
47
48 #include "appconfig.h" // CONFIG_FAST_MEM
49
50 /**
51  * \name Macros for determining CPU endianness.
52  * \{
53  */
54 #define CPU_BIG_ENDIAN    0x1234
55 #define CPU_LITTLE_ENDIAN 0x3412 /* Look twice, pal. This is not a bug. */
56 /*\}*/
57
58 /** Macro to include cpu-specific versions of the headers. */
59 #define CPU_HEADER(module)          PP_STRINGIZE(drv/PP_CAT3(module, _, CPU_ID).h)
60
61 /** Macro to include cpu-specific versions of implementation files. */
62 #define CPU_CSOURCE(module)         PP_STRINGIZE(drv/PP_CAT3(module, _, CPU_ID).c)
63
64
65 #if CPU_I196
66
67         #define NOP                     nop_instruction()
68
69         #define CPU_REG_BITS            16
70         #define CPU_REGS_CNT            16
71         #define CPU_STACK_GROWS_UPWARD  0
72         #define CPU_SP_ON_EMPTY_SLOT    0
73         #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
74         #define CPU_HARVARD             0
75
76 #elif CPU_X86
77
78         #define NOP                     asm volatile ("nop")
79
80         #define CPU_REGS_CNT            7
81         #define CPU_SAVED_REGS_CNT      7
82         #define CPU_STACK_GROWS_UPWARD  0
83         #define CPU_SP_ON_EMPTY_SLOT    0
84         #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
85         #define CPU_HARVARD             0
86
87         #if CPU_X86_64
88                 #define CPU_REG_BITS    64
89
90                 #ifdef __WIN64__
91                         /* WIN64 is an IL32-P64 weirdo. */
92                         #define SIZEOF_LONG  4
93                 #endif
94         #else
95                 #define CPU_REG_BITS    32
96         #endif
97
98 #elif CPU_ARM
99
100         /* Register counts include SREG too */
101         #define CPU_REG_BITS           32
102         #define CPU_REGS_CNT           16
103         #define CPU_SAVED_REGS_CNT     9
104         #define CPU_STACK_GROWS_UPWARD 0
105         #define CPU_SP_ON_EMPTY_SLOT   0
106         #define CPU_HARVARD            0
107         /*
108          * Force compiler to realod context variable.
109          */
110         #define CPU_MEMORY_BARRIER     asm volatile ("" : : : "memory")
111
112         #ifdef __IAR_SYSTEMS_ICC__
113                 #warning Check CPU_BYTE_ORDER
114                 #define CPU_BYTE_ORDER (__BIG_ENDIAN__ ? CPU_BIG_ENDIAN : CPU_LITTLE_ENDIAN)
115
116                 #define NOP            __no_operation()
117
118         #else /* GCC and compatibles */
119
120                 #if defined(__ARMEB__)
121                         #define CPU_BYTE_ORDER CPU_BIG_ENDIAN
122                 #elif defined(__ARMEL__)
123                         #define CPU_BYTE_ORDER CPU_LITTLE_ENDIAN
124                 #else
125                         #error Unable to detect ARM endianness!
126                 #endif
127
128                 #define NOP            asm volatile ("mov r0,r0" ::)
129
130                 /**
131                  * Initialization value for registers in stack frame.
132                  * The register index is not directly corrispondent to CPU
133                  * register numbers, but is related to how are pushed to
134                  * stack (\see asm_switch_context).
135                  * Index (CPU_SAVED_REGS_CNT - 1) is the CPSR register,
136                  * the initial value is set to:
137                  * - All flags (N, Z, C, V) set to 0.
138                  * - IRQ and FIQ enabled.
139                  * - ARM state.
140                  * - CPU in Supervisor Mode (SVC).
141                  */
142                 #define CPU_REG_INIT_VALUE(reg) (reg == (CPU_SAVED_REGS_CNT - 1) ? 0x13 : 0)
143
144                 #if CONFIG_FAST_MEM
145                         /**
146                          * Function attribute for use with performance critical code.
147                          *
148                          * On the AT91 family, code residing in flash has wait states.
149                          * Moving functions to the data section is a quick & dirty way
150                          * to get them transparently copied to SRAM for zero-wait-state
151                          * operation.
152                          */
153                         #define FAST_FUNC __attribute__((section(".data")))
154
155                         /**
156                          * Data attribute to move constant data to fast memory storage.
157                          *
158                          * \see FAST_FUNC
159                          */
160                         #define FAST_RODATA __attribute__((section(".data")))
161
162                 #else // !CONFIG_FAST_MEM
163                         #define FAST_RODATA /**/
164                         #define FAST_FUNC /**/
165                 #endif
166
167                 /**
168                  * Function attribute to declare an interrupt service routine.
169                  */
170                 #define ISR_FUNC __attribute__((interrupt))
171
172         #endif /* !__IAR_SYSTEMS_ICC_ */
173
174 #elif CPU_PPC
175         #define NOP                 asm volatile ("nop" ::)
176
177         /* Register counts include SREG too */
178         #define CPU_REG_BITS           (CPU_PPC32 ? 32 : 64)
179         #define CPU_REGS_CNT           FIXME
180         #define CPU_SAVED_REGS_CNT     FIXME
181         #define CPU_STACK_GROWS_UPWARD 0  //FIXME
182         #define CPU_SP_ON_EMPTY_SLOT   0  //FIXME
183         #define CPU_BYTE_ORDER         (__BIG_ENDIAN__ ? CPU_BIG_ENDIAN : CPU_LITTLE_ENDIAN)
184         #define CPU_HARVARD            0
185
186 #elif CPU_DSP56K
187
188         #define NOP                     asm(nop)
189
190         #define CPU_REG_BITS            16
191         #define CPU_REGS_CNT            FIXME
192         #define CPU_SAVED_REGS_CNT      8
193         #define CPU_STACK_GROWS_UPWARD  1
194         #define CPU_SP_ON_EMPTY_SLOT    0
195         #define CPU_BYTE_ORDER          CPU_BIG_ENDIAN
196         #define CPU_HARVARD             1
197
198         /* Memory is word-addessed in the DSP56K */
199         #define CPU_BITS_PER_CHAR  16
200         #define SIZEOF_SHORT        1
201         #define SIZEOF_INT          1
202         #define SIZEOF_LONG         2
203         #define SIZEOF_PTR          1
204
205 #elif CPU_AVR
206
207         #define NOP           asm volatile ("nop" ::)
208
209         /* Register counts include SREG too */
210         #define CPU_REG_BITS            8
211         #define CPU_REGS_CNT           33
212         #define CPU_SAVED_REGS_CNT     19
213         #define CPU_STACK_GROWS_UPWARD  0
214         #define CPU_SP_ON_EMPTY_SLOT    1
215         #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
216         #define CPU_HARVARD             1
217
218         /**
219          * Initialization value for registers in stack frame.
220          * The register index is not directly corrispondent to CPU
221          * register numbers. Index 0 is the SREG register: the initial
222          * value is all 0 but the interrupt bit (bit 7).
223          */
224         #define CPU_REG_INIT_VALUE(reg) (reg == 0 ? 0x80 : 0)
225
226 #else
227         #error No CPU_... defined.
228 #endif
229
230 /// Default for macro not defined in the right arch section
231 #ifndef CPU_REG_INIT_VALUE
232         #define CPU_REG_INIT_VALUE(reg)     0
233 #endif
234
235
236 #ifndef CPU_STACK_GROWS_UPWARD
237         #error CPU_STACK_GROWS_UPWARD should have been defined to either 0 or 1
238 #endif
239
240 #ifndef CPU_SP_ON_EMPTY_SLOT
241         #error CPU_SP_ON_EMPTY_SLOT should have been defined to either 0 or 1
242 #endif
243
244 /*
245  * Support stack handling peculiarities of a few CPUs.
246  *
247  * Most processors let their stack grow downward and
248  * keep SP pointing at the last pushed value.
249  */
250 #if !CPU_STACK_GROWS_UPWARD
251         #if !CPU_SP_ON_EMPTY_SLOT
252                 /* Most microprocessors (x86, m68k...) */
253                 #define CPU_PUSH_WORD(sp, data) \
254                         do { *--(sp) = (data); } while (0)
255                 #define CPU_POP_WORD(sp) \
256                         (*(sp)++)
257         #else
258                 /* AVR insanity */
259                 #define CPU_PUSH_WORD(sp, data) \
260                         do { *(sp)-- = (data); } while (0)
261                 #define CPU_POP_WORD(sp) \
262                         (*++(sp))
263         #endif
264
265 #else /* CPU_STACK_GROWS_UPWARD */
266
267         #if !CPU_SP_ON_EMPTY_SLOT
268                 /* DSP56K and other weirdos */
269                 #define CPU_PUSH_WORD(sp, data) \
270                         do { *++(sp) = (cpustack_t)(data); } while (0)
271                 #define CPU_POP_WORD(sp) \
272                         (*(sp)--)
273         #else
274                 #error I bet you cannot find a CPU like this
275         #endif
276 #endif
277
278
279 #if CPU_DSP56K
280         /*
281          * DSP56k pushes both PC and SR to the stack in the JSR instruction, but
282          * RTS discards SR while returning (it does not restore it). So we push
283          * 0 to fake the same context.
284          */
285         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
286                 do { \
287                         CPU_PUSH_WORD((sp), (func)); \
288                         CPU_PUSH_WORD((sp), 0x100); \
289                 } while (0);
290
291 #elif CPU_AVR
292         /*
293          * In AVR, the addresses are pushed into the stack as little-endian, while
294          * memory accesses are big-endian (actually, it's a 8-bit CPU, so there is
295          * no natural endianess).
296          */
297         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
298                 do { \
299                         uint16_t funcaddr = (uint16_t)(func); \
300                         CPU_PUSH_WORD((sp), funcaddr); \
301                         CPU_PUSH_WORD((sp), funcaddr>>8); \
302                 } while (0)
303
304         /*
305          * If the kernel is in idle-spinning, the processor executes:
306          *
307          * IRQ_ENABLE;
308          * CPU_IDLE;
309          * IRQ_DISABLE;
310          *
311          * IRQ_ENABLE is translated in asm as "sei" and IRQ_DISABLE as "cli".
312          * We could define CPU_IDLE to expand to none, so the resulting
313          * asm code would be:
314          *
315          * sei;
316          * cli;
317          *
318          * But Atmel datasheet states:
319          * "When using the SEI instruction to enable interrupts,
320          * the instruction following SEI will be executed *before*
321          * any pending interrupts", so "cli" is executed before any
322          * pending interrupt with the result that IRQs will *NOT*
323          * be enabled!
324          * To ensure that IRQ will run a NOP is required.
325          */
326         #define CPU_IDLE NOP
327
328 #else
329         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
330                 CPU_PUSH_WORD((sp), (cpustack_t)(func))
331 #endif
332
333 /**
334  * \def CPU_IDLE
335  *
336  * \brief Invoked by the scheduler to stop the CPU when idle.
337  *
338  * This hook can be redefined to put the CPU in low-power mode, or to
339  * profile system load with an external strobe, or to save CPU cycles
340  * in hosted environments such as emulators.
341  */
342 #ifndef CPU_IDLE
343         #if defined(ARCH_EMUL) && (ARCH & ARCH_EMUL)
344                 /* This emulator hook should yield the CPU to the host.  */
345                 EXTERN_C_BEGIN
346                 void emul_idle(void);
347                 EXTERN_C_END
348                 #define CPU_IDLE emul_idle()
349         #else /* !ARCH_EMUL */
350                 #define CPU_IDLE do { /* nothing */ } while (0)
351         #endif /* !ARCH_EMUL */
352 #endif /* !CPU_IDLE */
353
354 #endif /* CPU_ATTR_H */