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. |
| 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 Georgi | b890a12 | 2015-03-26 15:17:45 +0100 | [diff] [blame] | 18 | * Foundation, Inc. |
Stefan Reinauer | 3b31402 | 2009-10-26 17:04:28 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #ifndef _CBMEM_H_ |
| 22 | #define _CBMEM_H_ |
| 23 | |
Marc Jones | a8bda43 | 2015-06-04 23:36:01 -0600 | [diff] [blame^] | 24 | #include <cbmem_id.h> |
| 25 | |
Kyösti Mälkki | 2ca2afe | 2014-06-17 15:41:37 +0300 | [diff] [blame] | 26 | #if IS_ENABLED(CONFIG_HAVE_ACPI_RESUME) && \ |
| 27 | ! IS_ENABLED(CONFIG_RELOCATABLE_RAMSTAGE) |
zbao | f722373 | 2012-04-13 13:42:15 +0800 | [diff] [blame] | 28 | #define HIGH_MEMORY_SAVE (CONFIG_RAMTOP - CONFIG_RAMBASE) |
Kyösti Mälkki | 2ca2afe | 2014-06-17 15:41:37 +0300 | [diff] [blame] | 29 | #else |
| 30 | #define HIGH_MEMORY_SAVE 0 |
| 31 | #endif |
| 32 | |
Stefan Reinauer | 61f4a74 | 2012-04-03 16:21:04 -0700 | [diff] [blame] | 33 | /* Delegation of resume backup memory so we don't have to |
| 34 | * (slowly) handle backing up OS memory in romstage.c |
| 35 | */ |
| 36 | #define CBMEM_BOOT_MODE 0x610 |
| 37 | #define CBMEM_RESUME_BACKUP 0x614 |
Martin Roth | 582b2ae | 2015-01-11 14:29:29 -0700 | [diff] [blame] | 38 | #define CBMEM_FSP_HOB_PTR 0x614 |
Stefan Reinauer | 3b31402 | 2009-10-26 17:04:28 +0000 | [diff] [blame] | 39 | |
Stefan Reinauer | 61f4a74 | 2012-04-03 16:21:04 -0700 | [diff] [blame] | 40 | #ifndef __ASSEMBLER__ |
Aaron Durbin | 0f33307 | 2014-01-30 17:19:46 -0600 | [diff] [blame] | 41 | #include <stddef.h> |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 42 | #include <stdint.h> |
| 43 | |
| 44 | struct cbmem_entry; |
| 45 | |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 46 | /* |
| 47 | * The dynamic cbmem infrastructure allows for growing cbmem dynamically as |
| 48 | * things are added. It requires an external function, cbmem_top(), to be |
| 49 | * implemented by the board or chipset to define the upper address where |
| 50 | * cbmem lives. This address is required to be a 32-bit address. Additionally, |
| 51 | * the address needs to be consistent in both romstage and ramstage. The |
Martin Roth | 0cb07e3 | 2013-07-09 21:46:01 -0600 | [diff] [blame] | 52 | * dynamic cbmem infrastructure allocates new regions below the last allocated |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 53 | * region. Regions are defined by a cbmem_entry struct that is opaque. Regions |
| 54 | * 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] | 55 | */ |
| 56 | |
| 57 | #define DYN_CBMEM_ALIGN_SIZE (4096) |
Lee Leahy | 522149c | 2015-05-08 11:33:55 -0700 | [diff] [blame] | 58 | #define CBMEM_ROOT_SIZE DYN_CBMEM_ALIGN_SIZE |
| 59 | |
| 60 | /* The root region is at least DYN_CBMEM_ALIGN_SIZE . */ |
| 61 | #define CBMEM_ROOT_MIN_SIZE DYN_CBMEM_ALIGN_SIZE |
| 62 | #define CBMEM_LG_ALIGN CBMEM_ROOT_MIN_SIZE |
| 63 | |
| 64 | /* Small allocation parameters. */ |
| 65 | #define CBMEM_SM_ROOT_SIZE 1024 |
| 66 | #define CBMEM_SM_ALIGN 32 |
| 67 | |
| 68 | /* Determine the size for CBMEM root and the small allocations */ |
| 69 | static inline size_t cbmem_overhead_size(void) |
| 70 | { |
| 71 | return 2 * CBMEM_ROOT_MIN_SIZE; |
| 72 | } |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 73 | |
Kyösti Mälkki | 2d8520b | 2014-01-06 17:20:31 +0200 | [diff] [blame] | 74 | /* By default cbmem is attempted to be recovered. Returns 0 if cbmem was |
| 75 | * recovered or 1 if cbmem had to be reinitialized. */ |
| 76 | int cbmem_initialize(void); |
Lee Leahy | 522149c | 2015-05-08 11:33:55 -0700 | [diff] [blame] | 77 | int cbmem_initialize_id_size(u32 id, u64 size); |
| 78 | |
Martin Roth | 0cb07e3 | 2013-07-09 21:46:01 -0600 | [diff] [blame] | 79 | /* Initialize cbmem to be empty. */ |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 80 | void cbmem_initialize_empty(void); |
Lee Leahy | 522149c | 2015-05-08 11:33:55 -0700 | [diff] [blame] | 81 | void cbmem_initialize_empty_id_size(u32 id, u64 size); |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 82 | |
| 83 | /* Return the top address for dynamic cbmem. The address returned needs to |
| 84 | * be consistent across romstage and ramstage, and it is required to be |
| 85 | * below 4GiB. */ |
| 86 | void *cbmem_top(void); |
| 87 | |
| 88 | /* Add a cbmem entry of a given size and id. These return NULL on failure. The |
| 89 | * add function performs a find first and do not check against the original |
| 90 | * size. */ |
| 91 | const struct cbmem_entry *cbmem_entry_add(u32 id, u64 size); |
| 92 | |
| 93 | /* Find a cbmem entry of a given id. These return NULL on failure. */ |
| 94 | const struct cbmem_entry *cbmem_entry_find(u32 id); |
| 95 | |
| 96 | /* Remove a region defined by a cbmem_entry. Returns 0 on success, < 0 on |
| 97 | * error. Note: A cbmem_entry cannot be removed unless it was the last one |
| 98 | * added. */ |
| 99 | int cbmem_entry_remove(const struct cbmem_entry *entry); |
| 100 | |
| 101 | /* cbmem_entry accessors to get pointer and size of a cbmem_entry. */ |
| 102 | void *cbmem_entry_start(const struct cbmem_entry *entry); |
| 103 | u64 cbmem_entry_size(const struct cbmem_entry *entry); |
| 104 | |
Kyösti Mälkki | 2d8520b | 2014-01-06 17:20:31 +0200 | [diff] [blame] | 105 | /* Returns 0 if old cbmem was recovered. Recovery is only attempted if |
| 106 | * s3resume is non-zero. */ |
| 107 | int cbmem_recovery(int s3resume); |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 108 | /* Add a cbmem entry of a given size and id. These return NULL on failure. The |
| 109 | * add function performs a find first and do not check against the original |
| 110 | * size. */ |
| 111 | void *cbmem_add(u32 id, u64 size); |
| 112 | /* Find a cbmem entry of a given id. These return NULL on failure. */ |
| 113 | void *cbmem_find(u32 id); |
| 114 | |
Kyösti Mälkki | 823edda | 2014-12-18 18:30:29 +0200 | [diff] [blame] | 115 | void cbmem_run_init_hooks(void); |
| 116 | void cbmem_fail_resume(void); |
| 117 | |
Rudolf Marek | 3310934 | 2010-12-11 22:26:10 +0000 | [diff] [blame] | 118 | #ifndef __PRE_RAM__ |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 119 | /* Ramstage only functions. */ |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 120 | /* Add the cbmem memory used to the memory map at boot. */ |
| 121 | void cbmem_add_bootmem(void); |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 122 | void cbmem_list(void); |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 123 | #endif /* __PRE_RAM__ */ |
| 124 | |
Kyösti Mälkki | 2fb6b40 | 2014-12-19 08:20:45 +0200 | [diff] [blame] | 125 | /* These are for compatibility with old boards only. Any new chipset and board |
| 126 | * must implement cbmem_top() for both romstage and ramstage to support |
| 127 | * early features like COLLECT_TIMESTAMPS and CBMEM_CONSOLE. |
| 128 | */ |
| 129 | #if IS_ENABLED(CONFIG_ARCH_X86) && IS_ENABLED(CONFIG_LATE_CBMEM_INIT) |
Aaron Durbin | 28d5ec9 | 2015-05-26 11:15:45 -0500 | [diff] [blame] | 130 | /* Note that many of the current providers of get_top_of_ram() conditionally |
| 131 | * return 0 when the sleep type is non S3. i.e. cold and warm boots would |
| 132 | * return 0 from get_top_of_ram(). */ |
Kyösti Mälkki | 2fb6b40 | 2014-12-19 08:20:45 +0200 | [diff] [blame] | 133 | unsigned long get_top_of_ram(void); |
| 134 | void set_top_of_ram(uint64_t ramtop); |
| 135 | void backup_top_of_ram(uint64_t ramtop); |
| 136 | #endif |
| 137 | |
Aaron Durbin | df3a109 | 2013-03-13 12:41:44 -0500 | [diff] [blame] | 138 | #endif /* __ASSEMBLER__ */ |
| 139 | |
| 140 | |
| 141 | #endif /* _CBMEM_H_ */ |