blob: c2e73144c79cd70fabebc605d07763b65fc2ea1c [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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
19 */
20
21#ifndef _CBMEM_H_
22#define _CBMEM_H_
23
Kyösti Mälkki2ca2afe2014-06-17 15:41:37 +030024#if IS_ENABLED(CONFIG_HAVE_ACPI_RESUME) && \
25 ! IS_ENABLED(CONFIG_RELOCATABLE_RAMSTAGE)
zbaof7223732012-04-13 13:42:15 +080026#define HIGH_MEMORY_SAVE (CONFIG_RAMTOP - CONFIG_RAMBASE)
Kyösti Mälkki2ca2afe2014-06-17 15:41:37 +030027#else
28#define HIGH_MEMORY_SAVE 0
29#endif
30
31#if IS_ENABLED(CONFIG_HAVE_ACPI_RESUME) && \
Kyösti Mälkkib393fa02014-06-21 23:50:53 +030032 defined(CONFIG_HIGH_SCRATCH_MEMORY_SIZE)
Kyösti Mälkki2ca2afe2014-06-17 15:41:37 +030033#define HIGH_MEMORY_SCRATCH CONFIG_HIGH_SCRATCH_MEMORY_SIZE
34#else
35#define HIGH_MEMORY_SCRATCH 0
Aaron Durbin8e4a3552013-02-08 17:28:04 -060036#endif
37
Stefan Reinauer61f4a742012-04-03 16:21:04 -070038/* Delegation of resume backup memory so we don't have to
39 * (slowly) handle backing up OS memory in romstage.c
40 */
41#define CBMEM_BOOT_MODE 0x610
42#define CBMEM_RESUME_BACKUP 0x614
Stefan Reinauer3b314022009-10-26 17:04:28 +000043
44#define CBMEM_ID_FREESPACE 0x46524545
45#define CBMEM_ID_GDT 0x4c474454
46#define CBMEM_ID_ACPI 0x41435049
Duncan Laurie11290c42012-10-03 19:07:05 -070047#define CBMEM_ID_ACPI_GNVS 0x474e5653
Duncan Laurie9c07c8f2013-03-22 11:08:39 -070048#define CBMEM_ID_ACPI_GNVS_PTR 0x474e5650
Vladimir Serbinenko35c0f432014-09-02 22:25:36 +020049#define CBMEM_ID_IGD_OPREGION 0x4f444749
Stefan Reinauer3b314022009-10-26 17:04:28 +000050#define CBMEM_ID_CBTABLE 0x43425442
51#define CBMEM_ID_PIRQ 0x49525154
52#define CBMEM_ID_MPTABLE 0x534d5054
53#define CBMEM_ID_RESUME 0x5245534d
zbaof7223732012-04-13 13:42:15 +080054#define CBMEM_ID_RESUME_SCRATCH 0x52455343
Sven Schnelle164bcfd2011-08-14 20:56:34 +020055#define CBMEM_ID_SMBIOS 0x534d4254
Vadim Bendebury6f72d692011-09-21 16:12:39 -070056#define CBMEM_ID_TIMESTAMP 0x54494d45
Stefan Reinauer61f4a742012-04-03 16:21:04 -070057#define CBMEM_ID_MRCDATA 0x4d524344
Vadim Bendeburybe25a4d2011-09-30 11:13:06 -070058#define CBMEM_ID_CONSOLE 0x434f4e53
Duncan Laurie215f27852012-10-10 14:34:49 -070059#define CBMEM_ID_ELOG 0x454c4f47
Stefan Reinauerd37ab452012-12-18 16:23:28 -080060#define CBMEM_ID_COVERAGE 0x47434f56
Aaron Durbina1db81b42013-02-08 17:11:28 -060061#define CBMEM_ID_ROMSTAGE_INFO 0x47545352
Aaron Durbindf3a1092013-03-13 12:41:44 -050062#define CBMEM_ID_ROMSTAGE_RAM_STACK 0x90357ac4
63#define CBMEM_ID_RAMSTAGE 0x9a357a9e
64#define CBMEM_ID_RAMSTAGE_CACHE 0x9a3ca54e
65#define CBMEM_ID_ROOT 0xff4007ff
Aaron Durbin0c6946d2013-03-13 12:48:33 -050066#define CBMEM_ID_VBOOT_HANDOFF 0x780074f0
Aaron Durbin716738a2013-05-10 00:33:32 -050067#define CBMEM_ID_CAR_GLOBALS 0xcac4e6a3
Kyösti Mälkki690bf2f2013-07-06 11:41:21 +030068#define CBMEM_ID_EHCI_DEBUG 0xe4c1deb9
Aaron Durbinbf18b172013-10-24 10:11:08 -050069#define CBMEM_ID_REFCODE 0x04efc0de
70#define CBMEM_ID_REFCODE_CACHE 0x4efc0de5
Aaron Durbin00bf3db2014-01-09 10:33:23 -060071#define CBMEM_ID_POWER_STATE 0x50535454
Aaron Durbinb4b9eb32014-02-13 10:26:18 -060072#define CBMEM_ID_SMM_SAVE_SPACE 0x07e9acee
Aaron Durbin06ece7d2014-02-14 00:30:04 -060073#define CBMEM_ID_RAM_OOPS 0x05430095
Stefan Reinauer3b314022009-10-26 17:04:28 +000074#define CBMEM_ID_NONE 0x00000000
Rudolf Marek5ce05062013-05-27 16:09:44 +020075#define CBMEM_ID_AGESA_RUNTIME 0x41474553
Vadim Bendebury8b143c52014-05-14 10:12:55 -070076#define CBMEM_ID_HOB_POINTER 0x484f4221
Stefan Reinauer3b314022009-10-26 17:04:28 +000077
Stefan Reinauer61f4a742012-04-03 16:21:04 -070078#ifndef __ASSEMBLER__
Aaron Durbin0f333072014-01-30 17:19:46 -060079#include <stddef.h>
Aaron Durbindf3a1092013-03-13 12:41:44 -050080#include <stdint.h>
81
Vadim Bendebury8b143c52014-05-14 10:12:55 -070082struct cbmem_id_to_name {
83 u32 id;
84 const char *name;
85};
86
87#define CBMEM_ID_TO_NAME_TABLE \
88 { CBMEM_ID_FREESPACE, "FREE SPACE " }, \
89 { CBMEM_ID_GDT, "GDT " }, \
90 { CBMEM_ID_ACPI, "ACPI " }, \
91 { CBMEM_ID_CBTABLE, "COREBOOT " }, \
92 { CBMEM_ID_PIRQ, "IRQ TABLE " }, \
93 { CBMEM_ID_MPTABLE, "SMP TABLE " }, \
94 { CBMEM_ID_RESUME, "ACPI RESUME" }, \
95 { CBMEM_ID_RESUME_SCRATCH, "ACPISCRATCH" }, \
96 { CBMEM_ID_ACPI_GNVS, "ACPI GNVS " }, \
97 { CBMEM_ID_ACPI_GNVS_PTR, "GNVS PTR " }, \
98 { CBMEM_ID_SMBIOS, "SMBIOS " }, \
99 { CBMEM_ID_TIMESTAMP, "TIME STAMP " }, \
100 { CBMEM_ID_MRCDATA, "MRC DATA " }, \
101 { CBMEM_ID_CONSOLE, "CONSOLE " }, \
102 { CBMEM_ID_ELOG, "ELOG " }, \
103 { CBMEM_ID_COVERAGE, "COVERAGE " }, \
104 { CBMEM_ID_ROMSTAGE_INFO, "ROMSTAGE " }, \
105 { CBMEM_ID_ROMSTAGE_RAM_STACK, "ROMSTG STCK" }, \
106 { CBMEM_ID_RAMSTAGE, "RAMSTAGE " }, \
107 { CBMEM_ID_RAMSTAGE_CACHE, "RAMSTAGE $ " }, \
108 { CBMEM_ID_ROOT, "CBMEM ROOT " }, \
109 { CBMEM_ID_VBOOT_HANDOFF, "VBOOT " }, \
110 { CBMEM_ID_CAR_GLOBALS, "CAR GLOBALS" }, \
111 { CBMEM_ID_AGESA_RUNTIME, "AGESA RSVD " }, \
112 { CBMEM_ID_EHCI_DEBUG, "USBDEBUG " }, \
113 { CBMEM_ID_REFCODE, "REFCODE " }, \
114 { CBMEM_ID_SMM_SAVE_SPACE, "SMM BACKUP " }, \
115 { CBMEM_ID_REFCODE_CACHE, "REFCODE $ " }, \
116 { CBMEM_ID_POWER_STATE, "POWER STATE" }, \
117 { CBMEM_ID_RAM_OOPS, "RAMOOPS " },
118
Aaron Durbindf3a1092013-03-13 12:41:44 -0500119struct cbmem_entry;
120
121#if CONFIG_DYNAMIC_CBMEM
122
123/*
124 * The dynamic cbmem infrastructure allows for growing cbmem dynamically as
125 * things are added. It requires an external function, cbmem_top(), to be
126 * implemented by the board or chipset to define the upper address where
127 * cbmem lives. This address is required to be a 32-bit address. Additionally,
128 * the address needs to be consistent in both romstage and ramstage. The
Martin Roth0cb07e32013-07-09 21:46:01 -0600129 * dynamic cbmem infrastructure allocates new regions below the last allocated
Aaron Durbindf3a1092013-03-13 12:41:44 -0500130 * region. Regions are defined by a cbmem_entry struct that is opaque. Regions
131 * may be removed, but the last one added is the only that can be removed.
132 *
133 * Dynamic cbmem has two allocators within it. All allocators use a top down
134 * allocation scheme. However, there are 2 modes for each allocation depending
135 * on the requested size. There are large allocations and small allocations.
136 * An allocation is considered to be small when it is less than or equal to
137 * DYN_CBMEM_ALIGN_SIZE / 2. The smaller allocations are fit into a larger
138 * allocation region.
139 */
140
141#define DYN_CBMEM_ALIGN_SIZE (4096)
142
Kyösti Mälkki2d8520b2014-01-06 17:20:31 +0200143/* By default cbmem is attempted to be recovered. Returns 0 if cbmem was
144 * recovered or 1 if cbmem had to be reinitialized. */
145int cbmem_initialize(void);
Martin Roth0cb07e32013-07-09 21:46:01 -0600146/* Initialize cbmem to be empty. */
Aaron Durbindf3a1092013-03-13 12:41:44 -0500147void cbmem_initialize_empty(void);
148
149/* Return the top address for dynamic cbmem. The address returned needs to
150 * be consistent across romstage and ramstage, and it is required to be
151 * below 4GiB. */
152void *cbmem_top(void);
153
Kyösti Mälkki2fb6b402014-12-19 08:20:45 +0200154/* Set the top address for dynamic cbmem. Not for new designs. */
155void cbmem_set_top(void *ramtop);
156
Aaron Durbindf3a1092013-03-13 12:41:44 -0500157/* Add a cbmem entry of a given size and id. These return NULL on failure. The
158 * add function performs a find first and do not check against the original
159 * size. */
160const struct cbmem_entry *cbmem_entry_add(u32 id, u64 size);
161
162/* Find a cbmem entry of a given id. These return NULL on failure. */
163const struct cbmem_entry *cbmem_entry_find(u32 id);
164
165/* Remove a region defined by a cbmem_entry. Returns 0 on success, < 0 on
166 * error. Note: A cbmem_entry cannot be removed unless it was the last one
167 * added. */
168int cbmem_entry_remove(const struct cbmem_entry *entry);
169
170/* cbmem_entry accessors to get pointer and size of a cbmem_entry. */
171void *cbmem_entry_start(const struct cbmem_entry *entry);
172u64 cbmem_entry_size(const struct cbmem_entry *entry);
173
Aaron Durbindf3a1092013-03-13 12:41:44 -0500174#else /* !CONFIG_DYNAMIC_CBMEM */
175
Kyösti Mälkki4dba06a2014-01-04 09:42:02 +0200176/* Allocation with static CBMEM is resolved at build time. We start
177 * with 128kB and conditionally add some of the most greedy CBMEM
178 * table entries.
179 */
180#define _CBMEM_SZ_MINIMAL ( 128 * 1024 )
181
Kyösti Mälkki4dba06a2014-01-04 09:42:02 +0200182#define _CBMEM_SZ_TOTAL \
Kyösti Mälkki2ca2afe2014-06-17 15:41:37 +0300183 (_CBMEM_SZ_MINIMAL + CONFIG_CONSOLE_CBMEM_BUFFER_SIZE + \
184 HIGH_MEMORY_SAVE + HIGH_MEMORY_SCRATCH)
Kyösti Mälkki4dba06a2014-01-04 09:42:02 +0200185
186#define HIGH_MEMORY_SIZE ALIGN_UP(_CBMEM_SZ_TOTAL, 0x10000)
187
Stefan Reinauer61f4a742012-04-03 16:21:04 -0700188#ifndef __PRE_RAM__
Kyösti Mälkki1ae305e2013-09-04 13:05:01 +0300189void cbmem_late_set_table(uint64_t base, uint64_t size);
Stefan Reinauer61f4a742012-04-03 16:21:04 -0700190#endif
191
Kyösti Mälkkic04afd62013-09-04 13:31:39 +0300192void get_cbmem_table(uint64_t *base, uint64_t *size);
Kyösti Mälkkif9f74af2013-09-06 10:46:22 +0300193struct cbmem_entry *get_cbmem_toc(void);
Rudolf Marek475916d2010-12-13 20:02:23 +0000194
Aaron Durbin0f333072014-01-30 17:19:46 -0600195static inline const struct cbmem_entry *cbmem_entry_find(uint32_t id)
196{
197 return NULL;
198}
Aaron Durbindf3a1092013-03-13 12:41:44 -0500199#endif /* CONFIG_DYNAMIC_CBMEM */
200
201/* Common API between cbmem and dynamic cbmem. */
202
Kyösti Mälkki2d8520b2014-01-06 17:20:31 +0200203/* Returns 0 if old cbmem was recovered. Recovery is only attempted if
204 * s3resume is non-zero. */
205int cbmem_recovery(int s3resume);
Aaron Durbindf3a1092013-03-13 12:41:44 -0500206/* Add a cbmem entry of a given size and id. These return NULL on failure. The
207 * add function performs a find first and do not check against the original
208 * size. */
209void *cbmem_add(u32 id, u64 size);
210/* Find a cbmem entry of a given id. These return NULL on failure. */
211void *cbmem_find(u32 id);
212
Kyösti Mälkki823edda2014-12-18 18:30:29 +0200213void cbmem_run_init_hooks(void);
214void cbmem_fail_resume(void);
215
Rudolf Marek33109342010-12-11 22:26:10 +0000216#ifndef __PRE_RAM__
Aaron Durbindf3a1092013-03-13 12:41:44 -0500217/* Ramstage only functions. */
Aaron Durbin49048022014-02-18 21:55:02 -0600218/* Add the cbmem memory used to the memory map at boot. */
219void cbmem_add_bootmem(void);
Aaron Durbindf3a1092013-03-13 12:41:44 -0500220void cbmem_list(void);
Aaron Durbindf3a1092013-03-13 12:41:44 -0500221void cbmem_print_entry(int n, u32 id, u64 start, u64 size);
Aaron Durbindf3a1092013-03-13 12:41:44 -0500222#endif /* __PRE_RAM__ */
223
Kyösti Mälkki2fb6b402014-12-19 08:20:45 +0200224/* These are for compatibility with old boards only. Any new chipset and board
225 * must implement cbmem_top() for both romstage and ramstage to support
226 * early features like COLLECT_TIMESTAMPS and CBMEM_CONSOLE.
227 */
228#if IS_ENABLED(CONFIG_ARCH_X86) && IS_ENABLED(CONFIG_LATE_CBMEM_INIT)
229unsigned long get_top_of_ram(void);
230void set_top_of_ram(uint64_t ramtop);
231void backup_top_of_ram(uint64_t ramtop);
232#endif
233
Aaron Durbindf3a1092013-03-13 12:41:44 -0500234#endif /* __ASSEMBLER__ */
235
236
237#endif /* _CBMEM_H_ */