xref: /aosp_15_r20/external/coreboot/src/include/bootmem.h (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #ifndef BOOTMEM_H
4 #define BOOTMEM_H
5 
6 #include <boot/coreboot_tables.h>
7 #include <memrange.h>
8 #include <types.h>
9 
10 /**
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.
17  * Start at 0x10000 to make sure that the caller doesn't provide LB_MEM tags.
18  */
19 enum bootmem_type {
20 	BM_MEM_INVALID = 0,	/* Invalid type (used in optional arguments). */
21 
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 */
25 	BM_MEM_SOFT_RESERVED,	/* Specific purpose memory */
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 */
30 	BM_MEM_OPENSBI,		/* Risc-V OpenSBI */
31 	BM_MEM_BL31,		/* Arm64 BL31 executable */
32 	BM_MEM_TABLE,		/* Ram configuration tables are kept in */
33 	/* Tags below this point are ignored for the OS table. */
34 	BM_MEM_OS_CUTOFF = BM_MEM_TABLE,
35 	BM_MEM_RAMSTAGE,
36 	BM_MEM_PAYLOAD,
37 	BM_MEM_LAST,		/* Last entry in this list */
38 };
39 
40 /**
41  * Write memory coreboot table. Current resource map is serialized into
42  * memtable (LB_MEM_* types). bootmem library is unusable until this function
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.
48  * BM_MEM_BL31     : Translates to LB_MEM_RESERVED.
49  * BM_MEM_OPENSBI  : Translates to LB_MEM_RESERVED.
50  */
51 void bootmem_write_memory_table(struct lb_memory *mem);
52 
53 /* Architecture hook to add bootmem areas the architecture controls when
54  * bootmem_write_memory_table() is called. */
55 void bootmem_arch_add_ranges(void);
56 
57 /* Platform hook to add bootmem areas the platform / board controls. */
58 void bootmem_platform_add_ranges(void);
59 
60 /* Add a range of a given type to the bootmem address space. */
61 void bootmem_add_range(uint64_t start, uint64_t size,
62 		       const enum bootmem_type tag);
63 
64 /* Print current range map of boot memory. */
65 void bootmem_dump_ranges(void);
66 
67 typedef bool (*range_action_t)(const struct range_entry *r, void *arg);
68 
69 /**
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 /**
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 
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);
96 
97 /* Allocate a temporary buffer from the unused RAM areas. */
98 void *bootmem_allocate_buffer(size_t size);
99 
100 #endif /* BOOTMEM_H */
101