Documentation: Fix sphinx configuration
[coreboot.git] / src / include / cbmem.h
blobb548cd955952e5331ee69d3b83e90ba66f3551d6
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #ifndef _CBMEM_H_
4 #define _CBMEM_H_
6 #include <commonlib/cbmem_id.h>
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <boot/coreboot_tables.h>
11 #define CBMEM_FSP_HOB_PTR 0x614
13 struct cbmem_entry;
16 * The dynamic cbmem infrastructure allows for growing cbmem dynamically as
17 * things are added. It requires an external function, cbmem_top(), to be
18 * implemented by the board or chipset to define the upper address where
19 * cbmem lives. This address is required to be a 32-bit address. Additionally,
20 * the address needs to be consistent in both romstage and ramstage. The
21 * dynamic cbmem infrastructure allocates new regions below the last allocated
22 * region. Regions are defined by a cbmem_entry struct that is opaque. Regions
23 * may be removed, but the last one added is the only that can be removed.
26 #define DYN_CBMEM_ALIGN_SIZE (4096)
27 #define CBMEM_ROOT_SIZE DYN_CBMEM_ALIGN_SIZE
29 /* The root region is at least DYN_CBMEM_ALIGN_SIZE . */
30 #define CBMEM_ROOT_MIN_SIZE DYN_CBMEM_ALIGN_SIZE
31 #define CBMEM_LG_ALIGN CBMEM_ROOT_MIN_SIZE
33 /* Small allocation parameters. */
34 #define CBMEM_SM_ROOT_SIZE 1024
35 #define CBMEM_SM_ALIGN 32
37 /* Determine the size for CBMEM root and the small allocations */
38 static inline size_t cbmem_overhead_size(void)
40 return 2 * CBMEM_ROOT_MIN_SIZE;
43 /* By default cbmem is attempted to be recovered. Returns 0 if cbmem was
44 * recovered or 1 if cbmem had to be reinitialized. */
45 int cbmem_initialize(void);
46 int cbmem_initialize_id_size(u32 id, u64 size);
48 /* Initialize cbmem to be empty. */
49 void cbmem_initialize_empty(void);
50 void cbmem_initialize_empty_id_size(u32 id, u64 size);
52 /* Return the top address for dynamic cbmem. The address returned needs to
53 * be consistent across romstage and ramstage, and it is required to be
54 * below 4GiB for 32bit coreboot builds. On 64bit coreboot builds there's no
55 * upper limit. This should not be called before memory is initialized.
57 /* The assumption is made that the result of cbmem_top_romstage fits in the size
58 of uintptr_t in the ramstage. */
59 extern uintptr_t _cbmem_top_ptr;
60 void *cbmem_top(void);
61 /* With CONFIG_RAMSTAGE_CBMEM_TOP_ARG set, the result of cbmem_top is passed via
62 * calling arguments to the next stage and saved in the global _cbmem_top_ptr
63 * global variable. Only a romstage callback needs to be implemented by the
64 * platform. It is up to the stages after romstage to save the calling argument
65 * in the _cbmem_top_ptr symbol. Without CONFIG_RAMSTAGE_CBMEM_TOP_ARG the same
66 * implementation as used in romstage will be used.
68 void *cbmem_top_chipset(void);
70 /* Add a cbmem entry of a given size and id. These return NULL on failure. The
71 * add function performs a find first and do not check against the original
72 * size. */
73 const struct cbmem_entry *cbmem_entry_add(u32 id, u64 size);
75 /* Find a cbmem entry of a given id. These return NULL on failure. */
76 const struct cbmem_entry *cbmem_entry_find(u32 id);
78 /* Remove a region defined by a cbmem_entry. Returns 0 on success, < 0 on
79 * error. Note: A cbmem_entry cannot be removed unless it was the last one
80 * added. */
81 int cbmem_entry_remove(const struct cbmem_entry *entry);
83 /* cbmem_entry accessors to get pointer and size of a cbmem_entry. */
84 void *cbmem_entry_start(const struct cbmem_entry *entry);
85 u64 cbmem_entry_size(const struct cbmem_entry *entry);
87 /* Returns 0 if old cbmem was recovered. Recovery is only attempted if
88 * s3resume is non-zero. */
89 int cbmem_recovery(int s3resume);
90 /* Add a cbmem entry of a given size and id. These return NULL on failure. The
91 * add function performs a find first and do not check against the original
92 * size. */
93 void *cbmem_add(u32 id, u64 size);
94 /* Find a cbmem entry of a given id. These return NULL on failure. */
95 void *cbmem_find(u32 id);
97 /* Indicate to each hook if cbmem is being recovered or not. */
98 typedef void (* const cbmem_init_hook_t)(int is_recovery);
99 void cbmem_run_init_hooks(int is_recovery);
101 /* Ramstage only functions. */
102 /* Add the cbmem memory used to the memory map at boot. */
103 void cbmem_add_bootmem(void);
104 /* Return the cbmem memory used */
105 void cbmem_get_region(void **baseptr, size_t *size);
106 void cbmem_list(void);
107 void cbmem_add_records_to_cbtable(struct lb_header *header);
109 #if ENV_RAMSTAGE
110 #define ROMSTAGE_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
111 static cbmem_init_hook_t init_fn_ ## _unused_ = init_fn_;
112 #define RAMSTAGE_CBMEM_INIT_HOOK(init_fn_) \
113 static cbmem_init_hook_t init_fn_ ## _ptr_ __attribute__((used, \
114 section(".rodata.cbmem_init_hooks"))) = init_fn_;
115 #define POSTCAR_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
116 static cbmem_init_hook_t init_fn_ ## _unused2_ = init_fn_;
117 #elif ENV_ROMSTAGE
118 #define ROMSTAGE_CBMEM_INIT_HOOK(init_fn_) \
119 static cbmem_init_hook_t init_fn_ ## _ptr_ __attribute__((used, \
120 section(".rodata.cbmem_init_hooks"))) = init_fn_;
121 #define RAMSTAGE_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
122 static cbmem_init_hook_t init_fn_ ## _unused_ = init_fn_;
123 #define POSTCAR_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
124 static cbmem_init_hook_t init_fn_ ## _unused2_ = init_fn_;
125 #elif ENV_POSTCAR
126 #define ROMSTAGE_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
127 static cbmem_init_hook_t init_fn_ ## _unused2_ = init_fn_;
128 #define RAMSTAGE_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
129 static cbmem_init_hook_t init_fn_ ## _unused_ = init_fn_;
130 #define POSTCAR_CBMEM_INIT_HOOK(init_fn_) \
131 static cbmem_init_hook_t init_fn_ ## _ptr_ __attribute__((used, \
132 section(".rodata.cbmem_init_hooks"))) = init_fn_;
133 #else
134 #define ROMSTAGE_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
135 static cbmem_init_hook_t init_fn_ ## _unused_ = init_fn_;
136 #define RAMSTAGE_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
137 static cbmem_init_hook_t init_fn_ ## _unused2_ = init_fn_;
138 #define POSTCAR_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
139 static cbmem_init_hook_t init_fn_ ## _unused3_ = init_fn_;
140 #endif /* ENV_RAMSTAGE */
143 * Returns 0 for the stages where we know that cbmem does not come online.
144 * Even if this function returns 1 for romstage, depending upon the point in
145 * bootup, cbmem might not actually be online.
147 static inline int cbmem_possibly_online(void)
149 if (ENV_BOOTBLOCK)
150 return 0;
152 if (ENV_SEPARATE_VERSTAGE && !CONFIG(VBOOT_STARTS_IN_ROMSTAGE))
153 return 0;
155 return 1;
158 #endif /* _CBMEM_H_ */