2 * This file is part of the coreboot project.
4 * Copyright (C) 2009 coresystems GmbH
5 * Copyright (C) 2013 Google, Inc.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
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.
20 #include <commonlib/cbmem_id.h>
24 #include <boot/coreboot_tables.h>
26 #define CBMEM_FSP_HOB_PTR 0x614
31 * The dynamic cbmem infrastructure allows for growing cbmem dynamically as
32 * things are added. It requires an external function, cbmem_top(), to be
33 * implemented by the board or chipset to define the upper address where
34 * cbmem lives. This address is required to be a 32-bit address. Additionally,
35 * the address needs to be consistent in both romstage and ramstage. The
36 * dynamic cbmem infrastructure allocates new regions below the last allocated
37 * region. Regions are defined by a cbmem_entry struct that is opaque. Regions
38 * may be removed, but the last one added is the only that can be removed.
41 #define DYN_CBMEM_ALIGN_SIZE (4096)
42 #define CBMEM_ROOT_SIZE DYN_CBMEM_ALIGN_SIZE
44 /* The root region is at least DYN_CBMEM_ALIGN_SIZE . */
45 #define CBMEM_ROOT_MIN_SIZE DYN_CBMEM_ALIGN_SIZE
46 #define CBMEM_LG_ALIGN CBMEM_ROOT_MIN_SIZE
48 /* Small allocation parameters. */
49 #define CBMEM_SM_ROOT_SIZE 1024
50 #define CBMEM_SM_ALIGN 32
52 /* Determine the size for CBMEM root and the small allocations */
53 static inline size_t cbmem_overhead_size(void)
55 return 2 * CBMEM_ROOT_MIN_SIZE
;
58 /* By default cbmem is attempted to be recovered. Returns 0 if cbmem was
59 * recovered or 1 if cbmem had to be reinitialized. */
60 int cbmem_initialize(void);
61 int cbmem_initialize_id_size(u32 id
, u64 size
);
63 /* Initialize cbmem to be empty. */
64 void cbmem_initialize_empty(void);
65 void cbmem_initialize_empty_id_size(u32 id
, u64 size
);
67 /* Optional hook for platforms to initialize cbmem_top() value. When employed
68 * it's called a single time during boot at cbmem initialization/recovery
70 void cbmem_top_init(void);
72 /* Return the top address for dynamic cbmem. The address returned needs to
73 * be consistent across romstage and ramstage, and it is required to be
75 * x86 boards or chipsets must return NULL before the cbmem backing store has
76 * been initialized. */
77 void *cbmem_top(void);
79 /* Add a cbmem entry of a given size and id. These return NULL on failure. The
80 * add function performs a find first and do not check against the original
82 const struct cbmem_entry
*cbmem_entry_add(u32 id
, u64 size
);
84 /* Find a cbmem entry of a given id. These return NULL on failure. */
85 const struct cbmem_entry
*cbmem_entry_find(u32 id
);
87 /* Remove a region defined by a cbmem_entry. Returns 0 on success, < 0 on
88 * error. Note: A cbmem_entry cannot be removed unless it was the last one
90 int cbmem_entry_remove(const struct cbmem_entry
*entry
);
92 /* cbmem_entry accessors to get pointer and size of a cbmem_entry. */
93 void *cbmem_entry_start(const struct cbmem_entry
*entry
);
94 u64
cbmem_entry_size(const struct cbmem_entry
*entry
);
96 /* Returns 0 if old cbmem was recovered. Recovery is only attempted if
97 * s3resume is non-zero. */
98 int cbmem_recovery(int s3resume
);
99 /* Add a cbmem entry of a given size and id. These return NULL on failure. The
100 * add function performs a find first and do not check against the original
102 void *cbmem_add(u32 id
, u64 size
);
103 /* Find a cbmem entry of a given id. These return NULL on failure. */
104 void *cbmem_find(u32 id
);
106 /* Indicate to each hook if cbmem is being recovered or not. */
107 typedef void (* const cbmem_init_hook_t
)(int is_recovery
);
108 void cbmem_run_init_hooks(int is_recovery
);
109 void cbmem_fail_resume(void);
111 /* Ramstage only functions. */
112 /* Add the cbmem memory used to the memory map at boot. */
113 void cbmem_add_bootmem(void);
114 /* Return the cbmem memory used */
115 void cbmem_get_region(void **baseptr
, size_t *size
);
116 void cbmem_list(void);
117 void cbmem_add_records_to_cbtable(struct lb_header
*header
);
120 #define ROMSTAGE_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
121 static cbmem_init_hook_t init_fn_ ## _unused_ = init_fn_;
122 #define RAMSTAGE_CBMEM_INIT_HOOK(init_fn_) \
123 static cbmem_init_hook_t init_fn_ ## _ptr_ __attribute__((used, \
124 section(".rodata.cbmem_init_hooks"))) = init_fn_;
125 #define POSTCAR_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
126 static cbmem_init_hook_t init_fn_ ## _unused2_ = init_fn_;
128 #define ROMSTAGE_CBMEM_INIT_HOOK(init_fn_) \
129 static cbmem_init_hook_t init_fn_ ## _ptr_ __attribute__((used, \
130 section(".rodata.cbmem_init_hooks"))) = init_fn_;
131 #define RAMSTAGE_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
132 static cbmem_init_hook_t init_fn_ ## _unused_ = init_fn_;
133 #define POSTCAR_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
134 static cbmem_init_hook_t init_fn_ ## _unused2_ = init_fn_;
136 #define ROMSTAGE_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
137 static cbmem_init_hook_t init_fn_ ## _unused2_ = init_fn_;
138 #define RAMSTAGE_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
139 static cbmem_init_hook_t init_fn_ ## _unused_ = init_fn_;
140 #define POSTCAR_CBMEM_INIT_HOOK(init_fn_) \
141 static cbmem_init_hook_t init_fn_ ## _ptr_ __attribute__((used, \
142 section(".rodata.cbmem_init_hooks"))) = init_fn_;
144 #define ROMSTAGE_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
145 static cbmem_init_hook_t init_fn_ ## _unused_ = init_fn_;
146 #define RAMSTAGE_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
147 static cbmem_init_hook_t init_fn_ ## _unused2_ = init_fn_;
148 #define POSTCAR_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
149 static cbmem_init_hook_t init_fn_ ## _unused3_ = init_fn_;
150 #endif /* ENV_RAMSTAGE */
153 /* Any new chipset and board must implement cbmem_top() for both
154 * romstage and ramstage to support early features like COLLECT_TIMESTAMPS
155 * and CBMEM_CONSOLE. Sometimes it is necessary to have cbmem_top()
156 * value stored in nvram to enable early recovery on S3 path.
158 #if IS_ENABLED(CONFIG_ARCH_X86)
159 void backup_top_of_low_cacheable(uintptr_t ramtop
);
160 uintptr_t restore_top_of_low_cacheable(void);
164 * Returns 0 for the stages where we know that cbmem does not come online.
165 * Even if this function returns 1 for romstage, depending upon the point in
166 * bootup, cbmem might not actually be online.
168 static inline int cbmem_possibly_online(void)
173 if (ENV_VERSTAGE
&& IS_ENABLED(CONFIG_VBOOT_STARTS_IN_BOOTBLOCK
))
179 #endif /* _CBMEM_H_ */