/ src / include / rules.h
rules.h
  1  /* SPDX-License-Identifier: GPL-2.0-only */
  2  
  3  #ifndef _RULES_H
  4  #define _RULES_H
  5  
  6  #if defined(__TEST__)
  7  #define ENV_TEST 1
  8  #else
  9  #define ENV_TEST 0
 10  #endif
 11  
 12  #if defined(__TIMELESS__)
 13  #define ENV_TIMELESS 1
 14  #else
 15  #define ENV_TIMELESS 0
 16  #endif
 17  
 18  /* Useful helpers to tell whether the code is executing in bootblock,
 19   * romstage, ramstage or SMM.
 20   */
 21  
 22  #if defined(__DECOMPRESSOR__)
 23  #define ENV_DECOMPRESSOR 1
 24  #define ENV_BOOTBLOCK 0
 25  #define ENV_SEPARATE_ROMSTAGE 0
 26  #define ENV_RAMSTAGE 0
 27  #define ENV_SMM 0
 28  #define ENV_SEPARATE_VERSTAGE 0
 29  #define ENV_RMODULE 0
 30  #define ENV_POSTCAR 0
 31  #define ENV_LIBAGESA 0
 32  #define ENV_STRING "decompressor"
 33  
 34  #elif defined(__BOOTBLOCK__)
 35  #define ENV_DECOMPRESSOR 0
 36  #define ENV_BOOTBLOCK 1
 37  #define ENV_SEPARATE_ROMSTAGE 0
 38  #define ENV_RAMSTAGE 0
 39  #define ENV_SMM 0
 40  #define ENV_SEPARATE_VERSTAGE 0
 41  #define ENV_RMODULE 0
 42  #define ENV_POSTCAR 0
 43  #define ENV_LIBAGESA 0
 44  #define ENV_STRING "bootblock"
 45  
 46  #elif defined(__ROMSTAGE__)
 47  #define ENV_DECOMPRESSOR 0
 48  #define ENV_BOOTBLOCK 0
 49  #define ENV_SEPARATE_ROMSTAGE 1
 50  #define ENV_RAMSTAGE 0
 51  #define ENV_SMM 0
 52  #define ENV_SEPARATE_VERSTAGE 0
 53  #define ENV_RMODULE 0
 54  #define ENV_POSTCAR 0
 55  #define ENV_LIBAGESA 0
 56  #define ENV_STRING "romstage"
 57  
 58  #elif defined(__SMM__)
 59  #define ENV_DECOMPRESSOR 0
 60  #define ENV_BOOTBLOCK 0
 61  #define ENV_SEPARATE_ROMSTAGE 0
 62  #define ENV_RAMSTAGE 0
 63  #define ENV_SMM 1
 64  #define ENV_SEPARATE_VERSTAGE 0
 65  #define ENV_RMODULE 0
 66  #define ENV_POSTCAR 0
 67  #define ENV_LIBAGESA 0
 68  #define ENV_STRING "smm"
 69  
 70  /*
 71   * NOTE: "verstage" code may either run as a separate stage or linked into the
 72   * bootblock/romstage, depending on the setting of the VBOOT_SEPARATE_VERSTAGE
 73   * kconfig option. The ENV_SEPARATE_VERSTAGE macro will only return true for
 74   * "verstage" code when CONFIG(VBOOT_SEPARATE_VERSTAGE) is true, otherwise that
 75   * code will have ENV_BOOTBLOCK or ENV_SEPARATE_ROMSTAGE set (depending on the
 76   * "VBOOT_STARTS_IN_"... kconfig options).
 77   */
 78  #elif defined(__VERSTAGE__)
 79  #define ENV_DECOMPRESSOR 0
 80  #define ENV_BOOTBLOCK 0
 81  #define ENV_SEPARATE_ROMSTAGE 0
 82  #define ENV_RAMSTAGE 0
 83  #define ENV_SMM 0
 84  #define ENV_SEPARATE_VERSTAGE 1
 85  #define ENV_RMODULE 0
 86  #define ENV_POSTCAR 0
 87  #define ENV_LIBAGESA 0
 88  #if CONFIG(VBOOT_STARTS_BEFORE_BOOTBLOCK)
 89  #define ENV_STRING "verstage-before-bootblock"
 90  #else
 91  #define ENV_STRING "verstage"
 92  #endif
 93  
 94  #elif defined(__RAMSTAGE__)
 95  #define ENV_DECOMPRESSOR 0
 96  #define ENV_BOOTBLOCK 0
 97  #define ENV_SEPARATE_ROMSTAGE 0
 98  #define ENV_RAMSTAGE 1
 99  #define ENV_SMM 0
100  #define ENV_SEPARATE_VERSTAGE 0
101  #define ENV_RMODULE 0
102  #define ENV_POSTCAR 0
103  #define ENV_LIBAGESA 0
104  #define ENV_STRING "ramstage"
105  
106  #elif defined(__RMODULE__)
107  #define ENV_DECOMPRESSOR 0
108  #define ENV_BOOTBLOCK 0
109  #define ENV_SEPARATE_ROMSTAGE 0
110  #define ENV_RAMSTAGE 0
111  #define ENV_SMM 0
112  #define ENV_SEPARATE_VERSTAGE 0
113  #define ENV_RMODULE 1
114  #define ENV_POSTCAR 0
115  #define ENV_LIBAGESA 0
116  #define ENV_STRING "rmodule"
117  
118  #elif defined(__POSTCAR__)
119  #define ENV_DECOMPRESSOR 0
120  #define ENV_BOOTBLOCK 0
121  #define ENV_SEPARATE_ROMSTAGE 0
122  #define ENV_RAMSTAGE 0
123  #define ENV_SMM 0
124  #define ENV_SEPARATE_VERSTAGE 0
125  #define ENV_RMODULE 0
126  #define ENV_POSTCAR 1
127  #define ENV_LIBAGESA 0
128  #define ENV_STRING "postcar"
129  
130  #elif defined(__LIBAGESA__)
131  #define ENV_DECOMPRESSOR 0
132  #define ENV_BOOTBLOCK 0
133  #define ENV_SEPARATE_ROMSTAGE 0
134  #define ENV_RAMSTAGE 0
135  #define ENV_SMM 0
136  #define ENV_SEPARATE_VERSTAGE 0
137  #define ENV_RMODULE 0
138  #define ENV_POSTCAR 0
139  #define ENV_LIBAGESA 1
140  #define ENV_STRING "libagesa"
141  
142  #else
143  /*
144   * Default case of nothing set for random blob generation using
145   * create_class_compiler that isn't bound to a stage.
146   */
147  #define ENV_DECOMPRESSOR 0
148  #define ENV_BOOTBLOCK 0
149  #define ENV_SEPARATE_ROMSTAGE 0
150  #define ENV_RAMSTAGE 0
151  #define ENV_SMM 0
152  #define ENV_SEPARATE_VERSTAGE 0
153  #define ENV_RMODULE 0
154  #define ENV_POSTCAR 0
155  #define ENV_LIBAGESA 0
156  #define ENV_STRING "UNKNOWN"
157  #endif
158  
159  /* Define helpers about the current architecture, based on toolchain.mk. */
160  
161  #if defined(__ARCH_arm__)
162  #define ENV_ARM 1
163  #define ENV_ARM64 0
164  #if __COREBOOT_ARM_ARCH__ == 4
165  #define ENV_ARMV4 1
166  #define ENV_ARMV7 0
167  #define ENV_ARCH "armv4"
168  #elif __COREBOOT_ARM_ARCH__ == 7
169  #define ENV_ARMV4 0
170  #define ENV_ARMV7 1
171  #define ENV_ARCH "armv7"
172  #if defined(__COREBOOT_ARM_V7_A__)
173  #define ENV_ARMV7_A 1
174  #define ENV_ARMV7_M 0
175  #define ENV_ARMV7_R 0
176  #elif defined(__COREBOOT_ARM_V7_M__)
177  #define ENV_ARMV7_A 0
178  #define ENV_ARMV7_M 1
179  #define ENV_ARMV7_R 0
180  #elif defined(__COREBOOT_ARM_V7_R__)
181  #define ENV_ARMV7_A 0
182  #define ENV_ARMV7_M 0
183  #define ENV_ARMV7_R 1
184  #endif
185  #else
186  #define ENV_ARMV4 0
187  #define ENV_ARMV7 0
188  #endif
189  #define ENV_ARMV8 0
190  #define ENV_RISCV 0
191  #define ENV_X86 0
192  #define ENV_X86_32 0
193  #define ENV_X86_64 0
194  
195  #elif defined(__ARCH_arm64__)
196  #define ENV_ARM 0
197  #define ENV_ARM64 1
198  #define ENV_ARMV4 0
199  #define ENV_ARMV7 0
200  #if __COREBOOT_ARM_ARCH__ == 8
201  #define ENV_ARMV8 1
202  #else
203  #define ENV_ARMV8 0
204  #endif
205  #define ENV_RISCV 0
206  #define ENV_X86 0
207  #define ENV_X86_32 0
208  #define ENV_X86_64 0
209  #define ENV_ARCH "aarch64"
210  
211  #elif defined(__ARCH_riscv__)
212  #define ENV_ARM 0
213  #define ENV_ARM64 0
214  #define ENV_ARMV4 0
215  #define ENV_ARMV7 0
216  #define ENV_ARMV8 0
217  #define ENV_RISCV 1
218  #define ENV_X86 0
219  #define ENV_X86_32 0
220  #define ENV_X86_64 0
221  #define ENV_ARCH "riscv"
222  
223  #elif defined(__ARCH_x86_32__)
224  #define ENV_ARM 0
225  #define ENV_ARM64 0
226  #define ENV_ARMV4 0
227  #define ENV_ARMV7 0
228  #define ENV_ARMV8 0
229  #define ENV_RISCV 0
230  #define ENV_X86 1
231  #define ENV_X86_32 1
232  #define ENV_X86_64 0
233  #define ENV_ARCH "x86_32"
234  
235  #elif defined(__ARCH_x86_64__)
236  #define ENV_ARM 0
237  #define ENV_ARM64 0
238  #define ENV_ARMV4 0
239  #define ENV_ARMV7 0
240  #define ENV_ARMV8 0
241  #define ENV_RISCV 0
242  #define ENV_X86 1
243  #define ENV_X86_32 0
244  #define ENV_X86_64 1
245  #define ENV_ARCH "x86_64"
246  
247  #else
248  #define ENV_ARM 0
249  #define ENV_ARM64 0
250  #define ENV_ARMV4 0
251  #define ENV_ARMV7 0
252  #define ENV_ARMV8 0
253  #define ENV_RISCV 0
254  #define ENV_X86 0
255  #define ENV_X86_32 0
256  #define ENV_X86_64 0
257  #define ENV_ARCH "unknown"
258  
259  #endif
260  
261  #if CONFIG(RAMPAYLOAD)
262  /* ENV_PAYLOAD_LOADER is set to ENV_POSTCAR when CONFIG_RAMPAYLOAD is enabled */
263  #define ENV_PAYLOAD_LOADER ENV_POSTCAR
264  #else
265  /* ENV_PAYLOAD_LOADER is set when you are in a stage that loads the payload.
266   * For now, that is the ramstage. */
267  #define ENV_PAYLOAD_LOADER ENV_RAMSTAGE
268  #endif
269  
270  #define ENV_ROMSTAGE_OR_BEFORE \
271  	(ENV_DECOMPRESSOR || ENV_BOOTBLOCK || ENV_SEPARATE_ROMSTAGE || \
272  	(ENV_SEPARATE_VERSTAGE && !CONFIG(VBOOT_STARTS_IN_ROMSTAGE)))
273  
274  #if ENV_X86
275  /* Indicates memory layout is determined with arch/x86/car.ld. */
276  #define ENV_CACHE_AS_RAM		(ENV_ROMSTAGE_OR_BEFORE && !CONFIG(RESET_VECTOR_IN_RAM))
277  #else
278  #define ENV_CACHE_AS_RAM		0
279  #endif
280  
281  /* Indicates if the stage uses the _data and _bss regions defined in
282   * arch/x86/car.ld */
283  #define ENV_SEPARATE_DATA_AND_BSS	(ENV_CACHE_AS_RAM && (ENV_BOOTBLOCK || !CONFIG(NO_XIP_EARLY_STAGES)))
284  
285  /* Currently ramstage has heap. */
286  #define ENV_HAS_HEAP_SECTION	ENV_RAMSTAGE
287  
288  /* Set USER_SPACE in the makefile for the rare code that runs in userspace */
289  #if defined(__USER_SPACE__)
290  #define ENV_USER_SPACE		1
291  #else
292  #define ENV_USER_SPACE		0
293  #endif
294  
295  /* Define the first stage to run */
296  #if CONFIG(VBOOT_STARTS_BEFORE_BOOTBLOCK)
297  #define ENV_INITIAL_STAGE		ENV_SEPARATE_VERSTAGE
298  #else
299  #define ENV_INITIAL_STAGE		ENV_BOOTBLOCK
300  #endif
301  
302  #define ENV_CREATES_CBMEM	(ENV_SEPARATE_ROMSTAGE || (ENV_BOOTBLOCK && !CONFIG(SEPARATE_ROMSTAGE)))
303  #define ENV_HAS_CBMEM		(ENV_CREATES_CBMEM || ENV_POSTCAR || ENV_RAMSTAGE)
304  #define ENV_RAMINIT		(ENV_SEPARATE_ROMSTAGE || (ENV_BOOTBLOCK && !CONFIG(SEPARATE_ROMSTAGE)))
305  
306  #if ENV_X86
307  #define ENV_HAS_SPINLOCKS		!ENV_ROMSTAGE_OR_BEFORE
308  #elif ENV_RISCV
309  #define ENV_HAS_SPINLOCKS		1
310  #else
311  #define ENV_HAS_SPINLOCKS		0
312  #endif
313  
314  /* When set <arch/smp/spinlock.h> is included for the spinlock implementation. */
315  #define ENV_SUPPORTS_SMP		(CONFIG(SMP) && ENV_HAS_SPINLOCKS)
316  
317  #if ENV_X86 && CONFIG(COOP_MULTITASKING) && (ENV_RAMSTAGE || ENV_CREATES_CBMEM)
318  /* TODO: Enable in all x86 stages */
319  #define ENV_SUPPORTS_COOP         1
320  #else
321  #define ENV_SUPPORTS_COOP         0
322  #endif
323  
324  /**
325   * For pre-DRAM stages and post-CAR always build with simple device model, ie.
326   * PCI, PNP and CPU functions operate without use of devicetree. The reason
327   * post-CAR utilizes __SIMPLE_DEVICE__ is for simplicity. Currently there's
328   * no known requirement that devicetree would be needed during that stage.
329   *
330   * For ramstage individual source file may define __SIMPLE_DEVICE__
331   * before including any header files to force that particular source
332   * be built with simple device model.
333   */
334  
335  #if !ENV_RAMSTAGE
336  #define __SIMPLE_DEVICE__
337  #endif
338  
339  #endif /* _RULES_H */