blob: 55b04b4106bb90f1388ae036cc4fb6cf47a73f78 [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
Patrick Georgib890a122015-03-26 15:17:45 +010018 * Foundation, Inc.
Stefan Reinauer3b314022009-10-26 17:04:28 +000019 */
20
21#ifndef _CBMEM_H_
22#define _CBMEM_H_
23
Marc Jonesa8bda432015-06-04 23:36:01 -060024#include <cbmem_id.h>
Kyösti Mälkki4fbac462015-01-07 04:48:43 +020025#include <rules.h>
Marc Jonesa8bda432015-06-04 23:36:01 -060026
Kyösti Mälkki2ca2afe2014-06-17 15:41:37 +030027#if IS_ENABLED(CONFIG_HAVE_ACPI_RESUME) && \
28 ! IS_ENABLED(CONFIG_RELOCATABLE_RAMSTAGE)
zbaof7223732012-04-13 13:42:15 +080029#define HIGH_MEMORY_SAVE (CONFIG_RAMTOP - CONFIG_RAMBASE)
Kyösti Mälkki2ca2afe2014-06-17 15:41:37 +030030#else
31#define HIGH_MEMORY_SAVE 0
32#endif
33
Stefan Reinauer61f4a742012-04-03 16:21:04 -070034/* Delegation of resume backup memory so we don't have to
35 * (slowly) handle backing up OS memory in romstage.c
36 */
37#define CBMEM_BOOT_MODE 0x610
38#define CBMEM_RESUME_BACKUP 0x614
Martin Roth582b2ae2015-01-11 14:29:29 -070039#define CBMEM_FSP_HOB_PTR 0x614
Stefan Reinauer3b314022009-10-26 17:04:28 +000040
Stefan Reinauer61f4a742012-04-03 16:21:04 -070041#ifndef __ASSEMBLER__
Aaron Durbin0f333072014-01-30 17:19:46 -060042#include <stddef.h>
Aaron Durbindf3a1092013-03-13 12:41:44 -050043#include <stdint.h>
44
45struct cbmem_entry;
46
Aaron Durbindf3a1092013-03-13 12:41:44 -050047/*
48 * The dynamic cbmem infrastructure allows for growing cbmem dynamically as
49 * things are added. It requires an external function, cbmem_top(), to be
50 * implemented by the board or chipset to define the upper address where
51 * cbmem lives. This address is required to be a 32-bit address. Additionally,
52 * the address needs to be consistent in both romstage and ramstage. The
Martin Roth0cb07e32013-07-09 21:46:01 -060053 * dynamic cbmem infrastructure allocates new regions below the last allocated
Aaron Durbindf3a1092013-03-13 12:41:44 -050054 * region. Regions are defined by a cbmem_entry struct that is opaque. Regions
55 * may be removed, but the last one added is the only that can be removed.
Aaron Durbindf3a1092013-03-13 12:41:44 -050056 */
57
58#define DYN_CBMEM_ALIGN_SIZE (4096)
Lee Leahy522149c2015-05-08 11:33:55 -070059#define CBMEM_ROOT_SIZE DYN_CBMEM_ALIGN_SIZE
60
61/* The root region is at least DYN_CBMEM_ALIGN_SIZE . */
62#define CBMEM_ROOT_MIN_SIZE DYN_CBMEM_ALIGN_SIZE
63#define CBMEM_LG_ALIGN CBMEM_ROOT_MIN_SIZE
64
65/* Small allocation parameters. */
66#define CBMEM_SM_ROOT_SIZE 1024
67#define CBMEM_SM_ALIGN 32
68
69/* Determine the size for CBMEM root and the small allocations */
70static inline size_t cbmem_overhead_size(void)
71{
72 return 2 * CBMEM_ROOT_MIN_SIZE;
73}
Aaron Durbindf3a1092013-03-13 12:41:44 -050074
Kyösti Mälkki2d8520b2014-01-06 17:20:31 +020075/* By default cbmem is attempted to be recovered. Returns 0 if cbmem was
76 * recovered or 1 if cbmem had to be reinitialized. */
77int cbmem_initialize(void);
Lee Leahy522149c2015-05-08 11:33:55 -070078int cbmem_initialize_id_size(u32 id, u64 size);
79
Martin Roth0cb07e32013-07-09 21:46:01 -060080/* Initialize cbmem to be empty. */
Aaron Durbindf3a1092013-03-13 12:41:44 -050081void cbmem_initialize_empty(void);
Lee Leahy522149c2015-05-08 11:33:55 -070082void cbmem_initialize_empty_id_size(u32 id, u64 size);
Aaron Durbindf3a1092013-03-13 12:41:44 -050083
84/* Return the top address for dynamic cbmem. The address returned needs to
85 * be consistent across romstage and ramstage, and it is required to be
86 * below 4GiB. */
87void *cbmem_top(void);
88
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
Kyösti Mälkki4fbac462015-01-07 04:48:43 +0200116typedef void (* const cbmem_init_hook_t)(void);
Kyösti Mälkki823edda2014-12-18 18:30:29 +0200117void cbmem_run_init_hooks(void);
118void cbmem_fail_resume(void);
119
Rudolf Marek33109342010-12-11 22:26:10 +0000120#ifndef __PRE_RAM__
Aaron Durbindf3a1092013-03-13 12:41:44 -0500121/* Ramstage only functions. */
Aaron Durbin49048022014-02-18 21:55:02 -0600122/* Add the cbmem memory used to the memory map at boot. */
123void cbmem_add_bootmem(void);
Aaron Durbindf3a1092013-03-13 12:41:44 -0500124void cbmem_list(void);
Aaron Durbindf3a1092013-03-13 12:41:44 -0500125#endif /* __PRE_RAM__ */
126
Kyösti Mälkki4fbac462015-01-07 04:48:43 +0200127#if ENV_RAMSTAGE
128#define ROMSTAGE_CBMEM_INIT_HOOK(init_fn_) static cbmem_init_hook_t \
129 init_fn_ ## _unused_ __attribute__((unused)) = init_fn_;
130#define RAMSTAGE_CBMEM_INIT_HOOK(init_fn_) \
131 static cbmem_init_hook_t init_fn_ ## _ptr_ __attribute__((used, \
132 section(".rodata.cbmem_init_hooks"))) = init_fn_;
133#elif ENV_ROMSTAGE
134#define ROMSTAGE_CBMEM_INIT_HOOK(init_fn_) \
135 static cbmem_init_hook_t init_fn_ ## _ptr_ __attribute__((used, \
136 section(".rodata.cbmem_init_hooks"))) = init_fn_;
137#define RAMSTAGE_CBMEM_INIT_HOOK(init_fn_) static cbmem_init_hook_t \
138 init_fn_ ## _unused_ __attribute__((unused)) = init_fn_;
139#else
140#define ROMSTAGE_CBMEM_INIT_HOOK(init_fn_) static cbmem_init_hook_t \
141 init_fn_ ## _unused_ __attribute__((unused)) = init_fn_;
142#define RAMSTAGE_CBMEM_INIT_HOOK(init_fn_) static cbmem_init_hook_t \
143 init_fn_ ## _unused2_ __attribute__((unused)) = init_fn_;
144#endif /* ENV_RAMSTAGE */
145
146
Kyösti Mälkki2fb6b402014-12-19 08:20:45 +0200147/* These are for compatibility with old boards only. Any new chipset and board
148 * must implement cbmem_top() for both romstage and ramstage to support
149 * early features like COLLECT_TIMESTAMPS and CBMEM_CONSOLE.
150 */
151#if IS_ENABLED(CONFIG_ARCH_X86) && IS_ENABLED(CONFIG_LATE_CBMEM_INIT)
Aaron Durbin28d5ec92015-05-26 11:15:45 -0500152/* Note that many of the current providers of get_top_of_ram() conditionally
153 * return 0 when the sleep type is non S3. i.e. cold and warm boots would
154 * return 0 from get_top_of_ram(). */
Kyösti Mälkki2fb6b402014-12-19 08:20:45 +0200155unsigned long get_top_of_ram(void);
156void set_top_of_ram(uint64_t ramtop);
157void backup_top_of_ram(uint64_t ramtop);
158#endif
159
Aaron Durbindf3a1092013-03-13 12:41:44 -0500160#endif /* __ASSEMBLER__ */
161
162
163#endif /* _CBMEM_H_ */