Stefan Reinauer | 3b31402 | 2009-10-26 17:04:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2009 coresystems GmbH |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 5 | * Copyright (C) 2013 Google, Inc. |
Stefan Reinauer | 3b31402 | 2009-10-26 17:04:28 +0000 | [diff] [blame] | 6 | * |
| 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 Reinauer | 3b31402 | 2009-10-26 17:04:28 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #ifndef _CBMEM_H_ |
| 18 | #define _CBMEM_H_ |
| 19 | |
Aaron Durbin | dc9f5cd | 2015-09-08 13:34:43 -0500 | [diff] [blame] | 20 | #include <commonlib/cbmem_id.h> |
Kyösti Mälkki | 4fbac46 | 2015-01-07 04:48:43 +0200 | [diff] [blame] | 21 | #include <rules.h> |
Aaron Durbin | 0f33307 | 2014-01-30 17:19:46 -0600 | [diff] [blame] | 22 | #include <stddef.h> |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 23 | #include <stdint.h> |
Aaron Durbin | 1ca2d86 | 2015-09-30 12:26:54 -0500 | [diff] [blame] | 24 | #include <boot/coreboot_tables.h> |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 25 | |
Kyösti Mälkki | d72cc41 | 2016-06-15 06:15:46 +0300 | [diff] [blame] | 26 | #define CBMEM_FSP_HOB_PTR 0x614 |
| 27 | |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 28 | struct cbmem_entry; |
| 29 | |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 30 | /* |
| 31 | * The dynamic cbmem infrastructure allows for growing cbmem dynamically as |
| 32 | * things are added. It requires an external function, cbmem_top(), to be |
| 33 | * implemented by the board or chipset to define the upper address where |
| 34 | * cbmem lives. This address is required to be a 32-bit address. Additionally, |
| 35 | * the address needs to be consistent in both romstage and ramstage. The |
Martin Roth | 0cb07e3 | 2013-07-09 21:46:01 -0600 | [diff] [blame] | 36 | * dynamic cbmem infrastructure allocates new regions below the last allocated |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 37 | * region. Regions are defined by a cbmem_entry struct that is opaque. Regions |
| 38 | * 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] | 39 | */ |
| 40 | |
| 41 | #define DYN_CBMEM_ALIGN_SIZE (4096) |
Lee Leahy | 522149c | 2015-05-08 11:33:55 -0700 | [diff] [blame] | 42 | #define CBMEM_ROOT_SIZE DYN_CBMEM_ALIGN_SIZE |
| 43 | |
| 44 | /* The root region is at least DYN_CBMEM_ALIGN_SIZE . */ |
| 45 | #define CBMEM_ROOT_MIN_SIZE DYN_CBMEM_ALIGN_SIZE |
| 46 | #define CBMEM_LG_ALIGN CBMEM_ROOT_MIN_SIZE |
| 47 | |
| 48 | /* Small allocation parameters. */ |
| 49 | #define CBMEM_SM_ROOT_SIZE 1024 |
| 50 | #define CBMEM_SM_ALIGN 32 |
| 51 | |
| 52 | /* Determine the size for CBMEM root and the small allocations */ |
| 53 | static inline size_t cbmem_overhead_size(void) |
| 54 | { |
| 55 | return 2 * CBMEM_ROOT_MIN_SIZE; |
| 56 | } |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 57 | |
Kyösti Mälkki | 2d8520b | 2014-01-06 17:20:31 +0200 | [diff] [blame] | 58 | /* By default cbmem is attempted to be recovered. Returns 0 if cbmem was |
| 59 | * recovered or 1 if cbmem had to be reinitialized. */ |
| 60 | int cbmem_initialize(void); |
Lee Leahy | 522149c | 2015-05-08 11:33:55 -0700 | [diff] [blame] | 61 | int cbmem_initialize_id_size(u32 id, u64 size); |
| 62 | |
Martin Roth | 0cb07e3 | 2013-07-09 21:46:01 -0600 | [diff] [blame] | 63 | /* Initialize cbmem to be empty. */ |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 64 | void cbmem_initialize_empty(void); |
Lee Leahy | 522149c | 2015-05-08 11:33:55 -0700 | [diff] [blame] | 65 | void cbmem_initialize_empty_id_size(u32 id, u64 size); |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 66 | |
| 67 | /* Return the top address for dynamic cbmem. The address returned needs to |
| 68 | * be consistent across romstage and ramstage, and it is required to be |
Furquan Shaikh | 6f009dc | 2016-03-28 13:23:13 -0700 | [diff] [blame] | 69 | * below 4GiB. |
| 70 | * Board or chipset should return NULL if any interface that might rely on cbmem |
| 71 | * (e.g. cbfs, vboot) is used before the cbmem backing store has been |
| 72 | * initialized. */ |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 73 | void *cbmem_top(void); |
| 74 | |
| 75 | /* Add a cbmem entry of a given size and id. These return NULL on failure. The |
| 76 | * add function performs a find first and do not check against the original |
| 77 | * size. */ |
| 78 | const struct cbmem_entry *cbmem_entry_add(u32 id, u64 size); |
| 79 | |
| 80 | /* Find a cbmem entry of a given id. These return NULL on failure. */ |
| 81 | const struct cbmem_entry *cbmem_entry_find(u32 id); |
| 82 | |
| 83 | /* Remove a region defined by a cbmem_entry. Returns 0 on success, < 0 on |
| 84 | * error. Note: A cbmem_entry cannot be removed unless it was the last one |
| 85 | * added. */ |
| 86 | int cbmem_entry_remove(const struct cbmem_entry *entry); |
| 87 | |
| 88 | /* cbmem_entry accessors to get pointer and size of a cbmem_entry. */ |
| 89 | void *cbmem_entry_start(const struct cbmem_entry *entry); |
| 90 | u64 cbmem_entry_size(const struct cbmem_entry *entry); |
| 91 | |
Kyösti Mälkki | 2d8520b | 2014-01-06 17:20:31 +0200 | [diff] [blame] | 92 | /* Returns 0 if old cbmem was recovered. Recovery is only attempted if |
| 93 | * s3resume is non-zero. */ |
| 94 | int cbmem_recovery(int s3resume); |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 95 | /* Add a cbmem entry of a given size and id. These return NULL on failure. The |
| 96 | * add function performs a find first and do not check against the original |
| 97 | * size. */ |
| 98 | void *cbmem_add(u32 id, u64 size); |
| 99 | /* Find a cbmem entry of a given id. These return NULL on failure. */ |
| 100 | void *cbmem_find(u32 id); |
Alexandru Gagniuc | 555d6c2 | 2015-11-16 13:26:33 -0800 | [diff] [blame] | 101 | /* Get location and size of CBMEM region in memory */ |
| 102 | void cbmem_region_used(uintptr_t *base, size_t *size); |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 103 | |
Aaron Durbin | 41607a4 | 2015-06-09 13:54:10 -0500 | [diff] [blame] | 104 | /* Indicate to each hook if cbmem is being recovered or not. */ |
| 105 | typedef void (* const cbmem_init_hook_t)(int is_recovery); |
| 106 | void cbmem_run_init_hooks(int is_recovery); |
Kyösti Mälkki | 823edda | 2014-12-18 18:30:29 +0200 | [diff] [blame] | 107 | void cbmem_fail_resume(void); |
| 108 | |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 109 | /* Ramstage only functions. */ |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 110 | /* Add the cbmem memory used to the memory map at boot. */ |
| 111 | void cbmem_add_bootmem(void); |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 112 | void cbmem_list(void); |
Aaron Durbin | 1ca2d86 | 2015-09-30 12:26:54 -0500 | [diff] [blame] | 113 | void cbmem_add_records_to_cbtable(struct lb_header *header); |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 114 | |
Kyösti Mälkki | 4fbac46 | 2015-01-07 04:48:43 +0200 | [diff] [blame] | 115 | #if ENV_RAMSTAGE |
| 116 | #define ROMSTAGE_CBMEM_INIT_HOOK(init_fn_) static cbmem_init_hook_t \ |
| 117 | init_fn_ ## _unused_ __attribute__((unused)) = init_fn_; |
| 118 | #define RAMSTAGE_CBMEM_INIT_HOOK(init_fn_) \ |
| 119 | static cbmem_init_hook_t init_fn_ ## _ptr_ __attribute__((used, \ |
| 120 | section(".rodata.cbmem_init_hooks"))) = init_fn_; |
| 121 | #elif ENV_ROMSTAGE |
| 122 | #define ROMSTAGE_CBMEM_INIT_HOOK(init_fn_) \ |
| 123 | static cbmem_init_hook_t init_fn_ ## _ptr_ __attribute__((used, \ |
| 124 | section(".rodata.cbmem_init_hooks"))) = init_fn_; |
| 125 | #define RAMSTAGE_CBMEM_INIT_HOOK(init_fn_) static cbmem_init_hook_t \ |
| 126 | init_fn_ ## _unused_ __attribute__((unused)) = init_fn_; |
| 127 | #else |
| 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_) static cbmem_init_hook_t \ |
| 131 | init_fn_ ## _unused2_ __attribute__((unused)) = init_fn_; |
| 132 | #endif /* ENV_RAMSTAGE */ |
| 133 | |
| 134 | |
Kyösti Mälkki | 2fb6b40 | 2014-12-19 08:20:45 +0200 | [diff] [blame] | 135 | /* These are for compatibility with old boards only. Any new chipset and board |
| 136 | * must implement cbmem_top() for both romstage and ramstage to support |
| 137 | * early features like COLLECT_TIMESTAMPS and CBMEM_CONSOLE. |
| 138 | */ |
| 139 | #if IS_ENABLED(CONFIG_ARCH_X86) && IS_ENABLED(CONFIG_LATE_CBMEM_INIT) |
Aaron Durbin | 28d5ec9 | 2015-05-26 11:15:45 -0500 | [diff] [blame] | 140 | /* Note that many of the current providers of get_top_of_ram() conditionally |
| 141 | * return 0 when the sleep type is non S3. i.e. cold and warm boots would |
| 142 | * return 0 from get_top_of_ram(). */ |
Kyösti Mälkki | 2fb6b40 | 2014-12-19 08:20:45 +0200 | [diff] [blame] | 143 | unsigned long get_top_of_ram(void); |
| 144 | void set_top_of_ram(uint64_t ramtop); |
| 145 | void backup_top_of_ram(uint64_t ramtop); |
| 146 | #endif |
| 147 | |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 148 | #endif /* _CBMEM_H_ */ |