blob: a22c420ad3b78523a4b9c379e03ca291b35f44c6 [file] [log] [blame]
Stefan Reinauer3b314022009-10-26 17:04:28 +00001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2009 coresystems GmbH
Aaron Durbindf3a1092013-03-13 12:41:44 -05005 * Copyright (C) 2013 Google, Inc.
Stefan Reinauer3b314022009-10-26 17:04:28 +00006 *
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.
10 *
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.
Stefan Reinauer3b314022009-10-26 17:04:28 +000015 */
16
17#ifndef _CBMEM_H_
18#define _CBMEM_H_
19
Aaron Durbindc9f5cd2015-09-08 13:34:43 -050020#include <commonlib/cbmem_id.h>
Aaron Durbin0f333072014-01-30 17:19:46 -060021#include <stddef.h>
Aaron Durbindf3a1092013-03-13 12:41:44 -050022#include <stdint.h>
Aaron Durbin1ca2d862015-09-30 12:26:54 -050023#include <boot/coreboot_tables.h>
Aaron Durbindf3a1092013-03-13 12:41:44 -050024
Kyösti Mälkkid72cc412016-06-15 06:15:46 +030025#define CBMEM_FSP_HOB_PTR 0x614
26
Aaron Durbindf3a1092013-03-13 12:41:44 -050027struct cbmem_entry;
28
Aaron Durbindf3a1092013-03-13 12:41:44 -050029/*
30 * The dynamic cbmem infrastructure allows for growing cbmem dynamically as
31 * things are added. It requires an external function, cbmem_top(), to be
32 * implemented by the board or chipset to define the upper address where
33 * cbmem lives. This address is required to be a 32-bit address. Additionally,
34 * the address needs to be consistent in both romstage and ramstage. The
Martin Roth0cb07e32013-07-09 21:46:01 -060035 * dynamic cbmem infrastructure allocates new regions below the last allocated
Aaron Durbindf3a1092013-03-13 12:41:44 -050036 * region. Regions are defined by a cbmem_entry struct that is opaque. Regions
37 * may be removed, but the last one added is the only that can be removed.
Aaron Durbindf3a1092013-03-13 12:41:44 -050038 */
39
40#define DYN_CBMEM_ALIGN_SIZE (4096)
Lee Leahy522149c2015-05-08 11:33:55 -070041#define CBMEM_ROOT_SIZE DYN_CBMEM_ALIGN_SIZE
42
43/* The root region is at least DYN_CBMEM_ALIGN_SIZE . */
44#define CBMEM_ROOT_MIN_SIZE DYN_CBMEM_ALIGN_SIZE
45#define CBMEM_LG_ALIGN CBMEM_ROOT_MIN_SIZE
46
47/* Small allocation parameters. */
48#define CBMEM_SM_ROOT_SIZE 1024
49#define CBMEM_SM_ALIGN 32
50
51/* Determine the size for CBMEM root and the small allocations */
52static inline size_t cbmem_overhead_size(void)
53{
Lee Leahy708fc272017-03-07 12:18:53 -080054 return 2 * CBMEM_ROOT_MIN_SIZE;
Lee Leahy522149c2015-05-08 11:33:55 -070055}
Aaron Durbindf3a1092013-03-13 12:41:44 -050056
Kyösti Mälkki2d8520b2014-01-06 17:20:31 +020057/* By default cbmem is attempted to be recovered. Returns 0 if cbmem was
58 * recovered or 1 if cbmem had to be reinitialized. */
59int cbmem_initialize(void);
Lee Leahy522149c2015-05-08 11:33:55 -070060int cbmem_initialize_id_size(u32 id, u64 size);
61
Martin Roth0cb07e32013-07-09 21:46:01 -060062/* Initialize cbmem to be empty. */
Aaron Durbindf3a1092013-03-13 12:41:44 -050063void cbmem_initialize_empty(void);
Lee Leahy522149c2015-05-08 11:33:55 -070064void cbmem_initialize_empty_id_size(u32 id, u64 size);
Aaron Durbindf3a1092013-03-13 12:41:44 -050065
Aaron Durbindfdea2a2017-08-01 10:27:10 -060066/* Optional hook for platforms to initialize cbmem_top() value. When employed
67 * it's called a single time during boot at cbmem initialization/recovery
68 * time. */
69void cbmem_top_init(void);
70
Aaron Durbindf3a1092013-03-13 12:41:44 -050071/* Return the top address for dynamic cbmem. The address returned needs to
72 * be consistent across romstage and ramstage, and it is required to be
Patrick Rudolphab0a7742019-01-02 14:04:02 +010073 * below 4GiB for 32bit coreboot builds. On 64bit coreboot builds there's no
Arthur Heymansb759a4f2019-10-25 18:06:58 +020074 * upper limit. This should not be called before memory is initialized.
75 */
Arthur Heymans340e4b82019-10-23 17:25:58 +020076/* The assumption is made that the result of cbmem_top_romstage fits in the size
77 of uintptr_t in the ramstage. */
78extern uintptr_t _cbmem_top_ptr;
Aaron Durbindf3a1092013-03-13 12:41:44 -050079void *cbmem_top(void);
Arthur Heymans340e4b82019-10-23 17:25:58 +020080/* With CONFIG_RAMSTAGE_CBMEM_TOP_ARG set, the result of cbmem_top is passed via
81 * calling arguments to the next stage and saved in the global _cbmem_top_ptr
82 * global variable. Only a romstage callback needs to be implemented by the
83 * platform. It is up to the stages after romstage to save the calling argument
84 * in the _cbmem_top_ptr symbol. Without CONFIG_RAMSTAGE_CBMEM_TOP_ARG the same
85 * implementation as used in romstage will be used.
86 */
87void *cbmem_top_chipset(void);
Aaron Durbindf3a1092013-03-13 12:41:44 -050088
89/* Add a cbmem entry of a given size and id. These return NULL on failure. The
90 * add function performs a find first and do not check against the original
91 * size. */
92const struct cbmem_entry *cbmem_entry_add(u32 id, u64 size);
93
94/* Find a cbmem entry of a given id. These return NULL on failure. */
95const struct cbmem_entry *cbmem_entry_find(u32 id);
96
97/* Remove a region defined by a cbmem_entry. Returns 0 on success, < 0 on
98 * error. Note: A cbmem_entry cannot be removed unless it was the last one
99 * added. */
100int cbmem_entry_remove(const struct cbmem_entry *entry);
101
102/* cbmem_entry accessors to get pointer and size of a cbmem_entry. */
103void *cbmem_entry_start(const struct cbmem_entry *entry);
104u64 cbmem_entry_size(const struct cbmem_entry *entry);
105
Kyösti Mälkki2d8520b2014-01-06 17:20:31 +0200106/* Returns 0 if old cbmem was recovered. Recovery is only attempted if
107 * s3resume is non-zero. */
108int cbmem_recovery(int s3resume);
Aaron Durbindf3a1092013-03-13 12:41:44 -0500109/* Add a cbmem entry of a given size and id. These return NULL on failure. The
110 * add function performs a find first and do not check against the original
111 * size. */
112void *cbmem_add(u32 id, u64 size);
113/* Find a cbmem entry of a given id. These return NULL on failure. */
114void *cbmem_find(u32 id);
115
Aaron Durbin41607a42015-06-09 13:54:10 -0500116/* Indicate to each hook if cbmem is being recovered or not. */
117typedef void (* const cbmem_init_hook_t)(int is_recovery);
118void cbmem_run_init_hooks(int is_recovery);
Kyösti Mälkki823edda2014-12-18 18:30:29 +0200119
Aaron Durbindf3a1092013-03-13 12:41:44 -0500120/* Ramstage only functions. */
Aaron Durbin49048022014-02-18 21:55:02 -0600121/* Add the cbmem memory used to the memory map at boot. */
122void cbmem_add_bootmem(void);
Philipp Deppenwiese84258db2018-08-16 00:31:26 +0200123/* Return the cbmem memory used */
124void cbmem_get_region(void **baseptr, size_t *size);
Aaron Durbindf3a1092013-03-13 12:41:44 -0500125void cbmem_list(void);
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500126void cbmem_add_records_to_cbtable(struct lb_header *header);
Aaron Durbindf3a1092013-03-13 12:41:44 -0500127
Kyösti Mälkki4fbac462015-01-07 04:48:43 +0200128#if ENV_RAMSTAGE
Lee Leahy22c28e02017-03-07 15:47:44 -0800129#define ROMSTAGE_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
130 static cbmem_init_hook_t init_fn_ ## _unused_ = init_fn_;
Kyösti Mälkki4fbac462015-01-07 04:48:43 +0200131#define RAMSTAGE_CBMEM_INIT_HOOK(init_fn_) \
132 static cbmem_init_hook_t init_fn_ ## _ptr_ __attribute__((used, \
133 section(".rodata.cbmem_init_hooks"))) = init_fn_;
Lee Leahy22c28e02017-03-07 15:47:44 -0800134#define POSTCAR_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
135 static cbmem_init_hook_t init_fn_ ## _unused2_ = init_fn_;
Kyösti Mälkki4fbac462015-01-07 04:48:43 +0200136#elif ENV_ROMSTAGE
137#define ROMSTAGE_CBMEM_INIT_HOOK(init_fn_) \
138 static cbmem_init_hook_t init_fn_ ## _ptr_ __attribute__((used, \
139 section(".rodata.cbmem_init_hooks"))) = init_fn_;
Lee Leahy22c28e02017-03-07 15:47:44 -0800140#define RAMSTAGE_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
141 static cbmem_init_hook_t init_fn_ ## _unused_ = init_fn_;
142#define POSTCAR_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
143 static cbmem_init_hook_t init_fn_ ## _unused2_ = init_fn_;
Aaron Durbin1e9a9142016-09-16 16:23:21 -0500144#elif ENV_POSTCAR
Lee Leahy22c28e02017-03-07 15:47:44 -0800145#define ROMSTAGE_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
146 static cbmem_init_hook_t init_fn_ ## _unused2_ = init_fn_;
147#define RAMSTAGE_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
148 static cbmem_init_hook_t init_fn_ ## _unused_ = init_fn_;
Aaron Durbin1e9a9142016-09-16 16:23:21 -0500149#define POSTCAR_CBMEM_INIT_HOOK(init_fn_) \
150 static cbmem_init_hook_t init_fn_ ## _ptr_ __attribute__((used, \
151 section(".rodata.cbmem_init_hooks"))) = init_fn_;
Kyösti Mälkki4fbac462015-01-07 04:48:43 +0200152#else
Lee Leahy22c28e02017-03-07 15:47:44 -0800153#define ROMSTAGE_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
154 static cbmem_init_hook_t init_fn_ ## _unused_ = init_fn_;
155#define RAMSTAGE_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
156 static cbmem_init_hook_t init_fn_ ## _unused2_ = init_fn_;
157#define POSTCAR_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \
158 static cbmem_init_hook_t init_fn_ ## _unused3_ = init_fn_;
Kyösti Mälkki4fbac462015-01-07 04:48:43 +0200159#endif /* ENV_RAMSTAGE */
160
161
Kyösti Mälkkia7dd6452017-04-19 07:37:38 +0300162/* Any new chipset and board must implement cbmem_top() for both
163 * romstage and ramstage to support early features like COLLECT_TIMESTAMPS
164 * and CBMEM_CONSOLE. Sometimes it is necessary to have cbmem_top()
165 * value stored in nvram to enable early recovery on S3 path.
Kyösti Mälkki2fb6b402014-12-19 08:20:45 +0200166 */
Julius Wernercd49cce2019-03-05 16:53:33 -0800167#if CONFIG(ARCH_X86)
Kyösti Mälkki70d92b92017-04-19 19:57:01 +0300168void backup_top_of_low_cacheable(uintptr_t ramtop);
169uintptr_t restore_top_of_low_cacheable(void);
Kyösti Mälkki2fb6b402014-12-19 08:20:45 +0200170#endif
171
Aaron Durbinb2a5f482016-12-14 14:40:43 -0600172/*
173 * Returns 0 for the stages where we know that cbmem does not come online.
174 * Even if this function returns 1 for romstage, depending upon the point in
175 * bootup, cbmem might not actually be online.
176 */
177static inline int cbmem_possibly_online(void)
178{
179 if (ENV_BOOTBLOCK)
180 return 0;
181
Julius Wernercd49cce2019-03-05 16:53:33 -0800182 if (ENV_VERSTAGE && CONFIG(VBOOT_STARTS_IN_BOOTBLOCK))
Aaron Durbinb2a5f482016-12-14 14:40:43 -0600183 return 0;
184
185 return 1;
186}
187
Aaron Durbindf3a1092013-03-13 12:41:44 -0500188#endif /* _CBMEM_H_ */