Move definitions of HIGH_MEMORY_SAVE
[coreboot.git] / src / include / cbmem.h
blob2182ce7a466c10854ddb42b1142405865793b628
1 /*
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.
17 #ifndef _CBMEM_H_
18 #define _CBMEM_H_
20 #include <commonlib/cbmem_id.h>
21 #include <rules.h>
22 /* Delegation of resume backup memory so we don't have to
23 * (slowly) handle backing up OS memory in romstage.c
25 #define CBMEM_BOOT_MODE 0x610
26 #define CBMEM_RESUME_BACKUP 0x614
27 #define CBMEM_FSP_HOB_PTR 0x614
29 #ifndef __ASSEMBLER__
30 #include <stddef.h>
31 #include <stdint.h>
32 #include <boot/coreboot_tables.h>
34 struct cbmem_entry;
37 * The dynamic cbmem infrastructure allows for growing cbmem dynamically as
38 * things are added. It requires an external function, cbmem_top(), to be
39 * implemented by the board or chipset to define the upper address where
40 * cbmem lives. This address is required to be a 32-bit address. Additionally,
41 * the address needs to be consistent in both romstage and ramstage. The
42 * dynamic cbmem infrastructure allocates new regions below the last allocated
43 * region. Regions are defined by a cbmem_entry struct that is opaque. Regions
44 * may be removed, but the last one added is the only that can be removed.
47 #define DYN_CBMEM_ALIGN_SIZE (4096)
48 #define CBMEM_ROOT_SIZE DYN_CBMEM_ALIGN_SIZE
50 /* The root region is at least DYN_CBMEM_ALIGN_SIZE . */
51 #define CBMEM_ROOT_MIN_SIZE DYN_CBMEM_ALIGN_SIZE
52 #define CBMEM_LG_ALIGN CBMEM_ROOT_MIN_SIZE
54 /* Small allocation parameters. */
55 #define CBMEM_SM_ROOT_SIZE 1024
56 #define CBMEM_SM_ALIGN 32
58 /* Determine the size for CBMEM root and the small allocations */
59 static inline size_t cbmem_overhead_size(void)
61 return 2 * CBMEM_ROOT_MIN_SIZE;
64 /* By default cbmem is attempted to be recovered. Returns 0 if cbmem was
65 * recovered or 1 if cbmem had to be reinitialized. */
66 int cbmem_initialize(void);
67 int cbmem_initialize_id_size(u32 id, u64 size);
69 /* Initialize cbmem to be empty. */
70 void cbmem_initialize_empty(void);
71 void cbmem_initialize_empty_id_size(u32 id, u64 size);
73 /* Return the top address for dynamic cbmem. The address returned needs to
74 * be consistent across romstage and ramstage, and it is required to be
75 * below 4GiB.
76 * Board or chipset should return NULL if any interface that might rely on cbmem
77 * (e.g. cbfs, vboot) is used before the cbmem backing store has been
78 * initialized. */
79 void *cbmem_top(void);
81 /* Add a cbmem entry of a given size and id. These return NULL on failure. The
82 * add function performs a find first and do not check against the original
83 * size. */
84 const struct cbmem_entry *cbmem_entry_add(u32 id, u64 size);
86 /* Find a cbmem entry of a given id. These return NULL on failure. */
87 const struct cbmem_entry *cbmem_entry_find(u32 id);
89 /* Remove a region defined by a cbmem_entry. Returns 0 on success, < 0 on
90 * error. Note: A cbmem_entry cannot be removed unless it was the last one
91 * added. */
92 int cbmem_entry_remove(const struct cbmem_entry *entry);
94 /* cbmem_entry accessors to get pointer and size of a cbmem_entry. */
95 void *cbmem_entry_start(const struct cbmem_entry *entry);
96 u64 cbmem_entry_size(const struct cbmem_entry *entry);
98 /* Returns 0 if old cbmem was recovered. Recovery is only attempted if
99 * s3resume is non-zero. */
100 int cbmem_recovery(int s3resume);
101 /* Add a cbmem entry of a given size and id. These return NULL on failure. The
102 * add function performs a find first and do not check against the original
103 * size. */
104 void *cbmem_add(u32 id, u64 size);
105 /* Find a cbmem entry of a given id. These return NULL on failure. */
106 void *cbmem_find(u32 id);
107 /* Get location and size of CBMEM region in memory */
108 void cbmem_region_used(uintptr_t *base, size_t *size);
110 /* Indicate to each hook if cbmem is being recovered or not. */
111 typedef void (* const cbmem_init_hook_t)(int is_recovery);
112 void cbmem_run_init_hooks(int is_recovery);
113 void cbmem_fail_resume(void);
115 /* Ramstage only functions. */
116 /* Add the cbmem memory used to the memory map at boot. */
117 void cbmem_add_bootmem(void);
118 void cbmem_list(void);
119 void cbmem_add_records_to_cbtable(struct lb_header *header);
121 #if ENV_RAMSTAGE
122 #define ROMSTAGE_CBMEM_INIT_HOOK(init_fn_) static cbmem_init_hook_t \
123 init_fn_ ## _unused_ __attribute__((unused)) = init_fn_;
124 #define RAMSTAGE_CBMEM_INIT_HOOK(init_fn_) \
125 static cbmem_init_hook_t init_fn_ ## _ptr_ __attribute__((used, \
126 section(".rodata.cbmem_init_hooks"))) = init_fn_;
127 #elif ENV_ROMSTAGE
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_) static cbmem_init_hook_t \
132 init_fn_ ## _unused_ __attribute__((unused)) = init_fn_;
133 #else
134 #define ROMSTAGE_CBMEM_INIT_HOOK(init_fn_) static cbmem_init_hook_t \
135 init_fn_ ## _unused_ __attribute__((unused)) = init_fn_;
136 #define RAMSTAGE_CBMEM_INIT_HOOK(init_fn_) static cbmem_init_hook_t \
137 init_fn_ ## _unused2_ __attribute__((unused)) = init_fn_;
138 #endif /* ENV_RAMSTAGE */
141 /* These are for compatibility with old boards only. Any new chipset and board
142 * must implement cbmem_top() for both romstage and ramstage to support
143 * early features like COLLECT_TIMESTAMPS and CBMEM_CONSOLE.
145 #if IS_ENABLED(CONFIG_ARCH_X86) && IS_ENABLED(CONFIG_LATE_CBMEM_INIT)
146 /* Note that many of the current providers of get_top_of_ram() conditionally
147 * return 0 when the sleep type is non S3. i.e. cold and warm boots would
148 * return 0 from get_top_of_ram(). */
149 unsigned long get_top_of_ram(void);
150 void set_top_of_ram(uint64_t ramtop);
151 void backup_top_of_ram(uint64_t ramtop);
152 #endif
154 #endif /* __ASSEMBLER__ */
157 #endif /* _CBMEM_H_ */