blob: 6ccdd884aab30a294b0feb5a24a02a4ee392a1c4 [file] [log] [blame]
Angel Pons32859fc2020-04-02 23:48:27 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Aaron Durbin49048022014-02-18 21:55:02 -06002
3#ifndef BOOTMEM_H
4#define BOOTMEM_H
5
Aaron Durbin49048022014-02-18 21:55:02 -06006#include <boot/coreboot_tables.h>
Julius Wernera2148372019-11-13 19:50:33 -08007#include <memrange.h>
8#include <types.h>
Aaron Durbin49048022014-02-18 21:55:02 -06009
Patrick Rudolph9ab9db02018-04-05 09:14:51 +020010/**
Patrick Rudolph23d62dd2018-04-12 10:36:57 +020011 * 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 Rudolph9ab9db02018-04-05 09:14:51 +020017 * Start at 0x10000 to make sure that the caller doesn't provide LB_MEM tags.
18 */
19enum bootmem_type {
Julius Werner965846f2021-01-11 16:07:02 -080020 BM_MEM_INVALID = 0, /* Invalid type (used in optional arguments). */
21
Patrick Rudolph9ab9db02018-04-05 09:14:51 +020022 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_ACPI, /* ACPI Tables */
26 BM_MEM_NVS, /* ACPI NVS Memory */
27 BM_MEM_UNUSABLE, /* Unusable address space */
28 BM_MEM_VENDOR_RSVD, /* Vendor Reserved */
Patrick Rudolph4c3da702019-07-07 13:10:56 +020029 BM_MEM_OPENSBI, /* Risc-V OpenSBI */
Elyes HAOUAS5f73e222020-01-15 21:13:45 +010030 BM_MEM_BL31, /* Arm64 BL31 executable */
Patrick Rudolph9ab9db02018-04-05 09:14:51 +020031 BM_MEM_TABLE, /* Ram configuration tables are kept in */
Julius Wernerae05d092018-05-08 17:09:57 -070032 /* Tags below this point are ignored for the OS table. */
33 BM_MEM_OS_CUTOFF = BM_MEM_TABLE,
Patrick Rudolph23d62dd2018-04-12 10:36:57 +020034 BM_MEM_RAMSTAGE,
35 BM_MEM_PAYLOAD,
Patrick Rudolph9ab9db02018-04-05 09:14:51 +020036 BM_MEM_LAST, /* Last entry in this list */
37};
38
Patrick Rudolph23d62dd2018-04-12 10:36:57 +020039/**
40 * Write memory coreboot table. Current resource map is serialized into
Aaron Durbin4677f6b2018-04-03 00:08:12 -060041 * memtable (LB_MEM_* types). bootmem library is unusable until this function
Patrick Rudolph23d62dd2018-04-12 10:36:57 +020042 * is called first in the write tables path before payload is loaded.
43 *
44 * Bootmem types match to LB_MEM tags, except for the following:
45 * BM_MEM_RAMSTAGE : Translates to LB_MEM_RAM.
46 * BM_MEM_PAYLOAD : Translates to LB_MEM_RAM.
Ting Shendff29e02019-01-28 18:15:00 +080047 * BM_MEM_BL31 : Translates to LB_MEM_RESERVED.
Patrick Rudolph23d62dd2018-04-12 10:36:57 +020048 */
Aaron Durbin4677f6b2018-04-03 00:08:12 -060049void bootmem_write_memory_table(struct lb_memory *mem);
Aaron Durbin49048022014-02-18 21:55:02 -060050
Aaron Durbin4677f6b2018-04-03 00:08:12 -060051/* Architecture hook to add bootmem areas the architecture controls when
52 * bootmem_write_memory_table() is called. */
Aaron Durbind4afa932016-04-19 17:25:59 -050053void bootmem_arch_add_ranges(void);
54
Patrick Rudolph23d62dd2018-04-12 10:36:57 +020055/* Platform hook to add bootmem areas the platform / board controls. */
56void bootmem_platform_add_ranges(void);
57
Aaron Durbin49048022014-02-18 21:55:02 -060058/* Add a range of a given type to the bootmem address space. */
Patrick Rudolph9ab9db02018-04-05 09:14:51 +020059void bootmem_add_range(uint64_t start, uint64_t size,
60 const enum bootmem_type tag);
Aaron Durbin49048022014-02-18 21:55:02 -060061
Aaron Durbin49048022014-02-18 21:55:02 -060062/* Print current range map of boot memory. */
63void bootmem_dump_ranges(void);
64
Patrick Rudolphc6536232018-04-10 09:34:29 +020065typedef bool (*range_action_t)(const struct range_entry *r, void *arg);
66
67/**
Patrick Rudolph64049be2018-04-20 10:37:51 +020068 * Walk memory tables from OS point of view and call the provided function,
69 * for every region. The caller has to return false to break out of the loop any
70 * time, or return true to continue.
71 *
72 * @param action The function to call for each memory range.
73 * @param arg Pointer passed to function @action. Set to NULL if unused.
74 * @return true if the function 'action' returned false.
75 */
76bool bootmem_walk_os_mem(range_action_t action, void *arg);
77
78/**
Patrick Rudolphc6536232018-04-10 09:34:29 +020079 * Walk memory tables and call the provided function, for every region.
80 * The caller has to return false to break out of the loop any time, or
81 * return true to continue.
82 *
83 * @param action The function to call for each memory range.
84 * @param arg Pointer passed to function @action. Set to NULL if unused.
85 * @return true if the function 'action' returned false.
86 */
87bool bootmem_walk(range_action_t action, void *arg);
88
Ting Shen05532262019-01-28 17:22:22 +080089/* Returns 1 if the requested memory range is all tagged as type dest_type.
90 * Otherwise returns 0.
91 */
92int bootmem_region_targets_type(uint64_t start, uint64_t size,
93 enum bootmem_type dest_type);
Aaron Durbin49048022014-02-18 21:55:02 -060094
Aaron Durbin49048022014-02-18 21:55:02 -060095/* Allocate a temporary buffer from the unused RAM areas. */
96void *bootmem_allocate_buffer(size_t size);
97
98#endif /* BOOTMEM_H */