Angel Pons | 32859fc | 2020-04-02 23:48:27 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Stefan Reinauer | 3b31402 | 2009-10-26 17:04:28 +0000 | [diff] [blame] | 2 | |
| 3 | #ifndef _CBMEM_H_ |
| 4 | #define _CBMEM_H_ |
| 5 | |
Jakub Czapiga | ea61942 | 2021-11-23 08:43:25 +0000 | [diff] [blame] | 6 | #include <commonlib/bsd/cbmem_id.h> |
Aaron Durbin | 0f33307 | 2014-01-30 17:19:46 -0600 | [diff] [blame] | 7 | #include <stddef.h> |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 8 | #include <stdint.h> |
Aaron Durbin | 1ca2d86 | 2015-09-30 12:26:54 -0500 | [diff] [blame] | 9 | #include <boot/coreboot_tables.h> |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 10 | |
Kyösti Mälkki | d72cc41 | 2016-06-15 06:15:46 +0300 | [diff] [blame] | 11 | #define CBMEM_FSP_HOB_PTR 0x614 |
| 12 | |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 13 | struct cbmem_entry; |
| 14 | |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 15 | /* |
| 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 |
Martin Roth | 0cb07e3 | 2013-07-09 21:46:01 -0600 | [diff] [blame] | 21 | * dynamic cbmem infrastructure allocates new regions below the last allocated |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 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. |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #define DYN_CBMEM_ALIGN_SIZE (4096) |
Lee Leahy | 522149c | 2015-05-08 11:33:55 -0700 | [diff] [blame] | 27 | #define CBMEM_ROOT_SIZE DYN_CBMEM_ALIGN_SIZE |
| 28 | |
| 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 |
| 32 | |
| 33 | /* Small allocation parameters. */ |
| 34 | #define CBMEM_SM_ROOT_SIZE 1024 |
| 35 | #define CBMEM_SM_ALIGN 32 |
| 36 | |
| 37 | /* Determine the size for CBMEM root and the small allocations */ |
| 38 | static inline size_t cbmem_overhead_size(void) |
| 39 | { |
Lee Leahy | 708fc27 | 2017-03-07 12:18:53 -0800 | [diff] [blame] | 40 | return 2 * CBMEM_ROOT_MIN_SIZE; |
Lee Leahy | 522149c | 2015-05-08 11:33:55 -0700 | [diff] [blame] | 41 | } |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 42 | |
Kyösti Mälkki | 2d8520b | 2014-01-06 17:20:31 +0200 | [diff] [blame] | 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); |
Lee Leahy | 522149c | 2015-05-08 11:33:55 -0700 | [diff] [blame] | 46 | int cbmem_initialize_id_size(u32 id, u64 size); |
| 47 | |
Martin Roth | 0cb07e3 | 2013-07-09 21:46:01 -0600 | [diff] [blame] | 48 | /* Initialize cbmem to be empty. */ |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 49 | void cbmem_initialize_empty(void); |
Lee Leahy | 522149c | 2015-05-08 11:33:55 -0700 | [diff] [blame] | 50 | void cbmem_initialize_empty_id_size(u32 id, u64 size); |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 51 | |
| 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 |
Patrick Rudolph | ab0a774 | 2019-01-02 14:04:02 +0100 | [diff] [blame] | 54 | * below 4GiB for 32bit coreboot builds. On 64bit coreboot builds there's no |
Arthur Heymans | b759a4f | 2019-10-25 18:06:58 +0200 | [diff] [blame] | 55 | * upper limit. This should not be called before memory is initialized. |
| 56 | */ |
Arthur Heymans | 340e4b8 | 2019-10-23 17:25:58 +0200 | [diff] [blame] | 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; |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 60 | void *cbmem_top(void); |
Arthur Heymans | 340e4b8 | 2019-10-23 17:25:58 +0200 | [diff] [blame] | 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. |
| 67 | */ |
| 68 | void *cbmem_top_chipset(void); |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 69 | |
| 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); |
| 74 | |
| 75 | /* Find a cbmem entry of a given id. These return NULL on failure. */ |
| 76 | const struct cbmem_entry *cbmem_entry_find(u32 id); |
| 77 | |
| 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); |
| 82 | |
| 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); |
| 86 | |
Kyösti Mälkki | 2d8520b | 2014-01-06 17:20:31 +0200 | [diff] [blame] | 87 | /* Returns 0 if old cbmem was recovered. Recovery is only attempted if |
| 88 | * s3resume is non-zero. */ |
| 89 | int cbmem_recovery(int s3resume); |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 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); |
| 96 | |
Aaron Durbin | 41607a4 | 2015-06-09 13:54:10 -0500 | [diff] [blame] | 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); |
Kyösti Mälkki | 823edda | 2014-12-18 18:30:29 +0200 | [diff] [blame] | 100 | |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 101 | /* Ramstage only functions. */ |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 102 | /* Add the cbmem memory used to the memory map at boot. */ |
| 103 | void cbmem_add_bootmem(void); |
Philipp Deppenwiese | 84258db | 2018-08-16 00:31:26 +0200 | [diff] [blame] | 104 | /* Return the cbmem memory used */ |
| 105 | void cbmem_get_region(void **baseptr, size_t *size); |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 106 | void cbmem_list(void); |
Aaron Durbin | 1ca2d86 | 2015-09-30 12:26:54 -0500 | [diff] [blame] | 107 | void cbmem_add_records_to_cbtable(struct lb_header *header); |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 108 | |
Kyösti Mälkki | fa3bc04 | 2022-03-31 07:40:10 +0300 | [diff] [blame] | 109 | #define _CBMEM_INIT_HOOK_UNUSED(init_fn_) __attribute__((unused)) \ |
| 110 | static cbmem_init_hook_t init_fn_ ## _unused_ = init_fn_ |
| 111 | |
| 112 | #define _CBMEM_INIT_HOOK(init_fn_) \ |
Kyösti Mälkki | 4fbac46 | 2015-01-07 04:48:43 +0200 | [diff] [blame] | 113 | static cbmem_init_hook_t init_fn_ ## _ptr_ __attribute__((used, \ |
Kyösti Mälkki | fa3bc04 | 2022-03-31 07:40:10 +0300 | [diff] [blame] | 114 | section(".rodata.cbmem_init_hooks"))) = init_fn_ |
Kyösti Mälkki | 4fbac46 | 2015-01-07 04:48:43 +0200 | [diff] [blame] | 115 | |
Julius Werner | 913a47a | 2021-05-20 17:00:46 -0700 | [diff] [blame] | 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. */ |
Kyösti Mälkki | fa3bc04 | 2022-03-31 07:40:10 +0300 | [diff] [blame] | 118 | #define _CBMEM_INIT_HOOK_EARLY(init_fn_) \ |
Julius Werner | 913a47a | 2021-05-20 17:00:46 -0700 | [diff] [blame] | 119 | static cbmem_init_hook_t init_fn_ ## _ptr_ __attribute__((used, \ |
Kyösti Mälkki | fa3bc04 | 2022-03-31 07:40:10 +0300 | [diff] [blame] | 120 | section(".rodata.cbmem_init_hooks_early"))) = init_fn_ |
| 121 | |
| 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) |
Julius Werner | 913a47a | 2021-05-20 17:00:46 -0700 | [diff] [blame] | 126 | #else |
Kyösti Mälkki | fa3bc04 | 2022-03-31 07:40:10 +0300 | [diff] [blame] | 127 | #define CBMEM_CREATION_HOOK(x) _CBMEM_INIT_HOOK_UNUSED(x) |
| 128 | #endif |
| 129 | |
| 130 | /* Use CBMEM_READY_HOOK for code that needs to run in all stages that have cbmem. */ |
| 131 | #if ENV_HAS_CBMEM |
| 132 | #define CBMEM_READY_HOOK(x) _CBMEM_INIT_HOOK(x) |
| 133 | #define CBMEM_READY_HOOK_EARLY(x) _CBMEM_INIT_HOOK_EARLY(x) |
| 134 | #else |
| 135 | #define CBMEM_READY_HOOK(x) _CBMEM_INIT_HOOK_UNUSED(x) |
| 136 | #define CBMEM_READY_HOOK_EARLY(x) _CBMEM_INIT_HOOK_UNUSED(x) |
| 137 | #endif |
Julius Werner | 913a47a | 2021-05-20 17:00:46 -0700 | [diff] [blame] | 138 | |
Aaron Durbin | b2a5f48 | 2016-12-14 14:40:43 -0600 | [diff] [blame] | 139 | /* |
| 140 | * Returns 0 for the stages where we know that cbmem does not come online. |
| 141 | * Even if this function returns 1 for romstage, depending upon the point in |
| 142 | * bootup, cbmem might not actually be online. |
| 143 | */ |
| 144 | static inline int cbmem_possibly_online(void) |
| 145 | { |
| 146 | if (ENV_BOOTBLOCK) |
| 147 | return 0; |
| 148 | |
Martin Roth | a202aec | 2020-06-03 21:06:58 -0600 | [diff] [blame] | 149 | if (ENV_SEPARATE_VERSTAGE && !CONFIG(VBOOT_STARTS_IN_ROMSTAGE)) |
Aaron Durbin | b2a5f48 | 2016-12-14 14:40:43 -0600 | [diff] [blame] | 150 | return 0; |
| 151 | |
| 152 | return 1; |
| 153 | } |
| 154 | |
Arthur Heymans | 54a4f17 | 2020-02-06 18:27:50 +0100 | [diff] [blame] | 155 | /* Returns 1 after running cbmem init hooks, 0 otherwise. */ |
| 156 | static inline int cbmem_online(void) |
| 157 | { |
| 158 | extern int cbmem_initialized; |
| 159 | |
| 160 | if (!cbmem_possibly_online()) |
| 161 | return 0; |
| 162 | |
Julius Werner | 364f9de | 2020-12-07 14:57:01 -0800 | [diff] [blame] | 163 | return cbmem_initialized; |
Arthur Heymans | 54a4f17 | 2020-02-06 18:27:50 +0100 | [diff] [blame] | 164 | } |
| 165 | |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 166 | #endif /* _CBMEM_H_ */ |