blob: dcffbfe035e04888915a95561af1a3d0966b1633 [file] [log] [blame]
Angel Pons32859fc2020-04-02 23:48:27 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Stefan Reinauer3b314022009-10-26 17:04:28 +00003
4#ifndef _CBMEM_H_
5#define _CBMEM_H_
6
Aaron Durbindc9f5cd2015-09-08 13:34:43 -05007#include <commonlib/cbmem_id.h>
Aaron Durbin0f333072014-01-30 17:19:46 -06008#include <stddef.h>
Aaron Durbindf3a1092013-03-13 12:41:44 -05009#include <stdint.h>
Aaron Durbin1ca2d862015-09-30 12:26:54 -050010#include <boot/coreboot_tables.h>
Aaron Durbindf3a1092013-03-13 12:41:44 -050011
Kyösti Mälkkid72cc412016-06-15 06:15:46 +030012#define CBMEM_FSP_HOB_PTR 0x614
13
Aaron Durbindf3a1092013-03-13 12:41:44 -050014struct cbmem_entry;
15
Aaron Durbindf3a1092013-03-13 12:41:44 -050016/*
17 * The dynamic cbmem infrastructure allows for growing cbmem dynamically as
18 * things are added. It requires an external function, cbmem_top(), to be
19 * implemented by the board or chipset to define the upper address where
20 * cbmem lives. This address is required to be a 32-bit address. Additionally,
21 * the address needs to be consistent in both romstage and ramstage. The
Martin Roth0cb07e32013-07-09 21:46:01 -060022 * dynamic cbmem infrastructure allocates new regions below the last allocated
Aaron Durbindf3a1092013-03-13 12:41:44 -050023 * region. Regions are defined by a cbmem_entry struct that is opaque. Regions
24 * may be removed, but the last one added is the only that can be removed.
Aaron Durbindf3a1092013-03-13 12:41:44 -050025 */
26
27#define DYN_CBMEM_ALIGN_SIZE (4096)
Lee Leahy522149c2015-05-08 11:33:55 -070028#define CBMEM_ROOT_SIZE DYN_CBMEM_ALIGN_SIZE
29
30/* The root region is at least DYN_CBMEM_ALIGN_SIZE . */
31#define CBMEM_ROOT_MIN_SIZE DYN_CBMEM_ALIGN_SIZE
32#define CBMEM_LG_ALIGN CBMEM_ROOT_MIN_SIZE
33
34/* Small allocation parameters. */
35#define CBMEM_SM_ROOT_SIZE 1024
36#define CBMEM_SM_ALIGN 32
37
38/* Determine the size for CBMEM root and the small allocations */
39static inline size_t cbmem_overhead_size(void)
40{
Lee Leahy708fc272017-03-07 12:18:53 -080041 return 2 * CBMEM_ROOT_MIN_SIZE;
Lee Leahy522149c2015-05-08 11:33:55 -070042}
Aaron Durbindf3a1092013-03-13 12:41:44 -050043
Kyösti Mälkki2d8520b2014-01-06 17:20:31 +020044/* By default cbmem is attempted to be recovered. Returns 0 if cbmem was
45 * recovered or 1 if cbmem had to be reinitialized. */
46int cbmem_initialize(void);
Lee Leahy522149c2015-05-08 11:33:55 -070047int cbmem_initialize_id_size(u32 id, u64 size);
48
Martin Roth0cb07e32013-07-09 21:46:01 -060049/* Initialize cbmem to be empty. */
Aaron Durbindf3a1092013-03-13 12:41:44 -050050void cbmem_initialize_empty(void);
Lee Leahy522149c2015-05-08 11:33:55 -070051void cbmem_initialize_empty_id_size(u32 id, u64 size);
Aaron Durbindf3a1092013-03-13 12:41:44 -050052
53/* Return the top address for dynamic cbmem. The address returned needs to
54 * be consistent across romstage and ramstage, and it is required to be
Patrick Rudolphab0a7742019-01-02 14:04:02 +010055 * below 4GiB for 32bit coreboot builds. On 64bit coreboot builds there's no
Arthur Heymansb759a4f2019-10-25 18:06:58 +020056 * upper limit. This should not be called before memory is initialized.
57 */
Arthur Heymans340e4b82019-10-23 17:25:58 +020058/* The assumption is made that the result of cbmem_top_romstage fits in the size
59 of uintptr_t in the ramstage. */
60extern uintptr_t _cbmem_top_ptr;
Aaron Durbindf3a1092013-03-13 12:41:44 -050061void *cbmem_top(void);
Arthur Heymans340e4b82019-10-23 17:25:58 +020062/* With CONFIG_RAMSTAGE_CBMEM_TOP_ARG set, the result of cbmem_top is passed via
63 * calling arguments to the next stage and saved in the global _cbmem_top_ptr
64 * global variable. Only a romstage callback needs to be implemented by the
65 * platform. It is up to the stages after romstage to save the calling argument
66 * in the _cbmem_top_ptr symbol. Without CONFIG_RAMSTAGE_CBMEM_TOP_ARG the same
67 * implementation as used in romstage will be used.
68 */
69void *cbmem_top_chipset(void);
Aaron Durbindf3a1092013-03-13 12:41:44 -050070
71/* Add a cbmem entry of a given size and id. These return NULL on failure. The
72 * add function performs a find first and do not check against the original
73 * size. */
74const struct cbmem_entry *cbmem_entry_add(u32 id, u64 size);
75
76/* Find a cbmem entry of a given id. These return NULL on failure. */
77const struct cbmem_entry *cbmem_entry_find(u32 id);
78
79/* Remove a region defined by a cbmem_entry. Returns 0 on success, < 0 on
80 * error. Note: A cbmem_entry cannot be removed unless it was the last one
81 * added. */
82int cbmem_entry_remove(const struct cbmem_entry *entry);
83
84/* cbmem_entry accessors to get pointer and size of a cbmem_entry. */
85void *cbmem_entry_start(const struct cbmem_entry *entry);
86u64 cbmem_entry_size(const struct cbmem_entry *entry);
87
Kyösti Mälkki2d8520b2014-01-06 17:20:31 +020088/* Returns 0 if old cbmem was recovered. Recovery is only attempted if
89 * s3resume is non-zero. */
90int cbmem_recovery(int s3resume);
Aaron Durbindf3a1092013-03-13 12:41:44 -050091/* Add a cbmem entry of a given size and id. These return NULL on failure. The
92 * add function performs a find first and do not check against the original
93 * size. */
94void *cbmem_add(u32 id, u64 size);
95/* Find a cbmem entry of a given id. These return NULL on failure. */
96void *cbmem_find(u32 id);
97
Aaron Durbin41607a42015-06-09 13:54:10 -050098/* Indicate to each hook if cbmem is being recovered or not. */
99typedef void (* const cbmem_init_hook_t)(int is_recovery);
100void cbmem_run_init_hooks(int is_recovery);
Kyösti Mälkki823edda2014-12-18 18:30:29 +0200101
Aaron Durbindf3a1092013-03-13 12:41:44 -0500102/* Ramstage only functions. */
Aaron Durbin49048022014-02-18 21:55:02 -0600103/* Add the cbmem memory used to the memory map at boot. */
104void cbmem_add_bootmem(void);
Philipp Deppenwiese84258db2018-08-16 00:31:26 +0200105/* Return the cbmem memory used */
106void cbmem_get_region(void **baseptr, size_t *size);
Aaron Durbindf3a1092013-03-13 12:41:44 -0500107void cbmem_list(void);
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500108void cbmem_add_records_to_cbtable(struct lb_header *header);
Aaron Durbindf3a1092013-03-13 12:41:44 -0500109
Kyösti Mälkki4fbac462015-01-07 04:48:43 +0200110#if ENV_RAMSTAGE
Lee Leahy22c28e02017-03-07 15:47:44 -0800111#define ROMSTAGE_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
112 static cbmem_init_hook_t init_fn_ ## _unused_ = init_fn_;
Kyösti Mälkki4fbac462015-01-07 04:48:43 +0200113#define RAMSTAGE_CBMEM_INIT_HOOK(init_fn_) \
114 static cbmem_init_hook_t init_fn_ ## _ptr_ __attribute__((used, \
115 section(".rodata.cbmem_init_hooks"))) = init_fn_;
Lee Leahy22c28e02017-03-07 15:47:44 -0800116#define POSTCAR_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
117 static cbmem_init_hook_t init_fn_ ## _unused2_ = init_fn_;
Kyösti Mälkki4fbac462015-01-07 04:48:43 +0200118#elif ENV_ROMSTAGE
119#define ROMSTAGE_CBMEM_INIT_HOOK(init_fn_) \
120 static cbmem_init_hook_t init_fn_ ## _ptr_ __attribute__((used, \
121 section(".rodata.cbmem_init_hooks"))) = init_fn_;
Lee Leahy22c28e02017-03-07 15:47:44 -0800122#define RAMSTAGE_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
123 static cbmem_init_hook_t init_fn_ ## _unused_ = init_fn_;
124#define POSTCAR_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
125 static cbmem_init_hook_t init_fn_ ## _unused2_ = init_fn_;
Aaron Durbin1e9a9142016-09-16 16:23:21 -0500126#elif ENV_POSTCAR
Lee Leahy22c28e02017-03-07 15:47:44 -0800127#define ROMSTAGE_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
128 static cbmem_init_hook_t init_fn_ ## _unused2_ = init_fn_;
129#define RAMSTAGE_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
130 static cbmem_init_hook_t init_fn_ ## _unused_ = init_fn_;
Aaron Durbin1e9a9142016-09-16 16:23:21 -0500131#define POSTCAR_CBMEM_INIT_HOOK(init_fn_) \
132 static cbmem_init_hook_t init_fn_ ## _ptr_ __attribute__((used, \
133 section(".rodata.cbmem_init_hooks"))) = init_fn_;
Kyösti Mälkki4fbac462015-01-07 04:48:43 +0200134#else
Lee Leahy22c28e02017-03-07 15:47:44 -0800135#define ROMSTAGE_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
136 static cbmem_init_hook_t init_fn_ ## _unused_ = init_fn_;
137#define RAMSTAGE_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
138 static cbmem_init_hook_t init_fn_ ## _unused2_ = init_fn_;
139#define POSTCAR_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
140 static cbmem_init_hook_t init_fn_ ## _unused3_ = init_fn_;
Kyösti Mälkki4fbac462015-01-07 04:48:43 +0200141#endif /* ENV_RAMSTAGE */
142
143
Kyösti Mälkkia7dd6452017-04-19 07:37:38 +0300144/* Any new chipset and board must implement cbmem_top() for both
145 * romstage and ramstage to support early features like COLLECT_TIMESTAMPS
146 * and CBMEM_CONSOLE. Sometimes it is necessary to have cbmem_top()
147 * value stored in nvram to enable early recovery on S3 path.
Kyösti Mälkki2fb6b402014-12-19 08:20:45 +0200148 */
Julius Wernercd49cce2019-03-05 16:53:33 -0800149#if CONFIG(ARCH_X86)
Kyösti Mälkki70d92b92017-04-19 19:57:01 +0300150void backup_top_of_low_cacheable(uintptr_t ramtop);
151uintptr_t restore_top_of_low_cacheable(void);
Kyösti Mälkki2fb6b402014-12-19 08:20:45 +0200152#endif
153
Aaron Durbinb2a5f482016-12-14 14:40:43 -0600154/*
155 * Returns 0 for the stages where we know that cbmem does not come online.
156 * Even if this function returns 1 for romstage, depending upon the point in
157 * bootup, cbmem might not actually be online.
158 */
159static inline int cbmem_possibly_online(void)
160{
161 if (ENV_BOOTBLOCK)
162 return 0;
163
Julius Werner21a40532020-04-21 16:03:53 -0700164 if (ENV_SEPARATE_VERSTAGE && CONFIG(VBOOT_STARTS_IN_BOOTBLOCK))
Aaron Durbinb2a5f482016-12-14 14:40:43 -0600165 return 0;
166
167 return 1;
168}
169
Aaron Durbindf3a1092013-03-13 12:41:44 -0500170#endif /* _CBMEM_H_ */