1 /* SPDX-License-Identifier: GPL-2.0-only */
6 #include <commonlib/bsd/cbmem_id.h> /* IWYU pragma: export */
9 #include <boot/coreboot_tables.h>
11 #define CBMEM_FSP_HOB_PTR 0x614
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 uintptr_t 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 uintptr_t 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
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
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
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 #define _CBMEM_INIT_HOOK_UNUSED(init_fn_) __attribute__((unused)) \
110 static cbmem_init_hook_t init_fn_ ## _unused_ = init_fn_
112 #define _CBMEM_INIT_HOOK(init_fn_) \
113 static cbmem_init_hook_t init_fn_ ## _ptr_ __attribute__((used, \
114 section(".rodata.cbmem_init_hooks"))) = init_fn_
116 /* Early hooks get executed before other hooks. Use sparingly for hooks that create
117 CBMEM regions which need to remain in a constant location across boot modes. */
118 #define _CBMEM_INIT_HOOK_EARLY(init_fn_) \
119 static cbmem_init_hook_t init_fn_ ## _ptr_ __attribute__((used, \
120 section(".rodata.cbmem_init_hooks_early"))) = init_fn_
122 /* Use CBMEM_CREATION_HOOK for code that needs to be run when cbmem is initialized
123 for the first time. */
124 #if ENV_CREATES_CBMEM
125 #define CBMEM_CREATION_HOOK(x) _CBMEM_INIT_HOOK(x)
127 #define CBMEM_CREATION_HOOK(x) _CBMEM_INIT_HOOK_UNUSED(x)
130 /* Use CBMEM_READY_HOOK for code that needs to run in all stages that have cbmem. */
132 #define CBMEM_READY_HOOK(x) _CBMEM_INIT_HOOK(x)
133 #define CBMEM_READY_HOOK_EARLY(x) _CBMEM_INIT_HOOK_EARLY(x)
135 #define CBMEM_READY_HOOK(x) _CBMEM_INIT_HOOK_UNUSED(x)
136 #define CBMEM_READY_HOOK_EARLY(x) _CBMEM_INIT_HOOK_UNUSED(x)
139 /* Returns 1 after running cbmem init hooks, 0 otherwise. */
140 static inline int cbmem_online(void)
142 extern int cbmem_initialized
;
147 return cbmem_initialized
;
150 #endif /* _CBMEM_H_ */