Angel Pons | 32859fc | 2020-04-02 23:48:27 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 2 | |
| 3 | #ifndef BOOTMEM_H |
| 4 | #define BOOTMEM_H |
| 5 | |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 6 | #include <boot/coreboot_tables.h> |
Julius Werner | a214837 | 2019-11-13 19:50:33 -0800 | [diff] [blame] | 7 | #include <memrange.h> |
| 8 | #include <types.h> |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 9 | |
Patrick Rudolph | 9ab9db0 | 2018-04-05 09:14:51 +0200 | [diff] [blame] | 10 | /** |
Patrick Rudolph | 23d62dd | 2018-04-12 10:36:57 +0200 | [diff] [blame] | 11 | * Bootmem types match to LB_MEM tags, except for the following: |
| 12 | * BM_MEM_RAMSTAGE : Memory where any kind of boot firmware resides and that |
| 13 | * should not be touched by bootmem (by example: stack, |
| 14 | * TTB, program, ...). |
| 15 | * BM_MEM_PAYLOAD : Memory where any kind of payload resides and that should |
| 16 | * not be touched by bootmem. |
Patrick Rudolph | 9ab9db0 | 2018-04-05 09:14:51 +0200 | [diff] [blame] | 17 | * Start at 0x10000 to make sure that the caller doesn't provide LB_MEM tags. |
| 18 | */ |
| 19 | enum bootmem_type { |
Julius Werner | 965846f | 2021-01-11 16:07:02 -0800 | [diff] [blame] | 20 | BM_MEM_INVALID = 0, /* Invalid type (used in optional arguments). */ |
| 21 | |
Patrick Rudolph | 9ab9db0 | 2018-04-05 09:14:51 +0200 | [diff] [blame] | 22 | BM_MEM_FIRST = 0x10000, /* First entry in this list */ |
| 23 | BM_MEM_RAM, /* Memory anyone can use */ |
| 24 | BM_MEM_RESERVED, /* Don't use this memory region */ |
Jonathan Zhang | e111de0 | 2021-04-21 10:40:31 -0700 | [diff] [blame] | 25 | BM_MEM_SOFT_RESERVED, /* Specific purpose memory */ |
Patrick Rudolph | 9ab9db0 | 2018-04-05 09:14:51 +0200 | [diff] [blame] | 26 | BM_MEM_ACPI, /* ACPI Tables */ |
| 27 | BM_MEM_NVS, /* ACPI NVS Memory */ |
| 28 | BM_MEM_UNUSABLE, /* Unusable address space */ |
| 29 | BM_MEM_VENDOR_RSVD, /* Vendor Reserved */ |
Patrick Rudolph | 4c3da70 | 2019-07-07 13:10:56 +0200 | [diff] [blame] | 30 | BM_MEM_OPENSBI, /* Risc-V OpenSBI */ |
Elyes HAOUAS | 5f73e22 | 2020-01-15 21:13:45 +0100 | [diff] [blame] | 31 | BM_MEM_BL31, /* Arm64 BL31 executable */ |
Patrick Rudolph | 9ab9db0 | 2018-04-05 09:14:51 +0200 | [diff] [blame] | 32 | BM_MEM_TABLE, /* Ram configuration tables are kept in */ |
Julius Werner | ae05d09 | 2018-05-08 17:09:57 -0700 | [diff] [blame] | 33 | /* Tags below this point are ignored for the OS table. */ |
| 34 | BM_MEM_OS_CUTOFF = BM_MEM_TABLE, |
Patrick Rudolph | 23d62dd | 2018-04-12 10:36:57 +0200 | [diff] [blame] | 35 | BM_MEM_RAMSTAGE, |
| 36 | BM_MEM_PAYLOAD, |
Patrick Rudolph | 9ab9db0 | 2018-04-05 09:14:51 +0200 | [diff] [blame] | 37 | BM_MEM_LAST, /* Last entry in this list */ |
| 38 | }; |
| 39 | |
Patrick Rudolph | 23d62dd | 2018-04-12 10:36:57 +0200 | [diff] [blame] | 40 | /** |
| 41 | * Write memory coreboot table. Current resource map is serialized into |
Aaron Durbin | 4677f6b | 2018-04-03 00:08:12 -0600 | [diff] [blame] | 42 | * memtable (LB_MEM_* types). bootmem library is unusable until this function |
Patrick Rudolph | 23d62dd | 2018-04-12 10:36:57 +0200 | [diff] [blame] | 43 | * is called first in the write tables path before payload is loaded. |
| 44 | * |
| 45 | * Bootmem types match to LB_MEM tags, except for the following: |
| 46 | * BM_MEM_RAMSTAGE : Translates to LB_MEM_RAM. |
| 47 | * BM_MEM_PAYLOAD : Translates to LB_MEM_RAM. |
Ting Shen | dff29e0 | 2019-01-28 18:15:00 +0800 | [diff] [blame] | 48 | * BM_MEM_BL31 : Translates to LB_MEM_RESERVED. |
Maximilian Brune | ebf4e8b | 2024-01-14 08:17:34 +0600 | [diff] [blame] | 49 | * BM_MEM_OPENSBI : Translates to LB_MEM_RESERVED. |
Patrick Rudolph | 23d62dd | 2018-04-12 10:36:57 +0200 | [diff] [blame] | 50 | */ |
Aaron Durbin | 4677f6b | 2018-04-03 00:08:12 -0600 | [diff] [blame] | 51 | void bootmem_write_memory_table(struct lb_memory *mem); |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 52 | |
Aaron Durbin | 4677f6b | 2018-04-03 00:08:12 -0600 | [diff] [blame] | 53 | /* Architecture hook to add bootmem areas the architecture controls when |
| 54 | * bootmem_write_memory_table() is called. */ |
Aaron Durbin | d4afa93 | 2016-04-19 17:25:59 -0500 | [diff] [blame] | 55 | void bootmem_arch_add_ranges(void); |
| 56 | |
Patrick Rudolph | 23d62dd | 2018-04-12 10:36:57 +0200 | [diff] [blame] | 57 | /* Platform hook to add bootmem areas the platform / board controls. */ |
| 58 | void bootmem_platform_add_ranges(void); |
| 59 | |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 60 | /* Add a range of a given type to the bootmem address space. */ |
Patrick Rudolph | 9ab9db0 | 2018-04-05 09:14:51 +0200 | [diff] [blame] | 61 | void bootmem_add_range(uint64_t start, uint64_t size, |
| 62 | const enum bootmem_type tag); |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 63 | |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 64 | /* Print current range map of boot memory. */ |
| 65 | void bootmem_dump_ranges(void); |
| 66 | |
Patrick Rudolph | c653623 | 2018-04-10 09:34:29 +0200 | [diff] [blame] | 67 | typedef bool (*range_action_t)(const struct range_entry *r, void *arg); |
| 68 | |
| 69 | /** |
Patrick Rudolph | 64049be | 2018-04-20 10:37:51 +0200 | [diff] [blame] | 70 | * Walk memory tables from OS point of view and call the provided function, |
| 71 | * for every region. The caller has to return false to break out of the loop any |
| 72 | * time, or return true to continue. |
| 73 | * |
| 74 | * @param action The function to call for each memory range. |
| 75 | * @param arg Pointer passed to function @action. Set to NULL if unused. |
| 76 | * @return true if the function 'action' returned false. |
| 77 | */ |
| 78 | bool bootmem_walk_os_mem(range_action_t action, void *arg); |
| 79 | |
| 80 | /** |
Patrick Rudolph | c653623 | 2018-04-10 09:34:29 +0200 | [diff] [blame] | 81 | * Walk memory tables and call the provided function, for every region. |
| 82 | * The caller has to return false to break out of the loop any time, or |
| 83 | * return true to continue. |
| 84 | * |
| 85 | * @param action The function to call for each memory range. |
| 86 | * @param arg Pointer passed to function @action. Set to NULL if unused. |
| 87 | * @return true if the function 'action' returned false. |
| 88 | */ |
| 89 | bool bootmem_walk(range_action_t action, void *arg); |
| 90 | |
Ting Shen | 0553226 | 2019-01-28 17:22:22 +0800 | [diff] [blame] | 91 | /* Returns 1 if the requested memory range is all tagged as type dest_type. |
| 92 | * Otherwise returns 0. |
| 93 | */ |
| 94 | int bootmem_region_targets_type(uint64_t start, uint64_t size, |
| 95 | enum bootmem_type dest_type); |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 96 | |
Aaron Durbin | 4904802 | 2014-02-18 21:55:02 -0600 | [diff] [blame] | 97 | /* Allocate a temporary buffer from the unused RAM areas. */ |
| 98 | void *bootmem_allocate_buffer(size_t size); |
| 99 | |
| 100 | #endif /* BOOTMEM_H */ |