blob: c935cb919f87842255e883a7eb139dd584ebb6dc [file] [log] [blame]
Aaron Durbin49048022014-02-18 21:55:02 -06001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2014 Google Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Aaron Durbin49048022014-02-18 21:55:02 -060014 */
15
16#ifndef BOOTMEM_H
17#define BOOTMEM_H
18
19#include <memrange.h>
20#include <stdint.h>
21#include <boot/coreboot_tables.h>
22
Patrick Rudolph9ab9db02018-04-05 09:14:51 +020023/**
Patrick Rudolph23d62dd2018-04-12 10:36:57 +020024 * Bootmem types match to LB_MEM tags, except for the following:
25 * BM_MEM_RAMSTAGE : Memory where any kind of boot firmware resides and that
26 * should not be touched by bootmem (by example: stack,
27 * TTB, program, ...).
28 * BM_MEM_PAYLOAD : Memory where any kind of payload resides and that should
29 * not be touched by bootmem.
Patrick Rudolph9ab9db02018-04-05 09:14:51 +020030 * Start at 0x10000 to make sure that the caller doesn't provide LB_MEM tags.
31 */
32enum bootmem_type {
33 BM_MEM_FIRST = 0x10000, /* First entry in this list */
34 BM_MEM_RAM, /* Memory anyone can use */
35 BM_MEM_RESERVED, /* Don't use this memory region */
36 BM_MEM_ACPI, /* ACPI Tables */
37 BM_MEM_NVS, /* ACPI NVS Memory */
38 BM_MEM_UNUSABLE, /* Unusable address space */
39 BM_MEM_VENDOR_RSVD, /* Vendor Reserved */
Ting Shendff29e02019-01-28 18:15:00 +080040 BM_MEM_BL31, /* Arm64 BL31 exectuable */
Patrick Rudolph9ab9db02018-04-05 09:14:51 +020041 BM_MEM_TABLE, /* Ram configuration tables are kept in */
Julius Wernerae05d092018-05-08 17:09:57 -070042 /* Tags below this point are ignored for the OS table. */
43 BM_MEM_OS_CUTOFF = BM_MEM_TABLE,
Patrick Rudolph23d62dd2018-04-12 10:36:57 +020044 BM_MEM_RAMSTAGE,
45 BM_MEM_PAYLOAD,
Patrick Rudolph9ab9db02018-04-05 09:14:51 +020046 BM_MEM_LAST, /* Last entry in this list */
47};
48
Patrick Rudolph23d62dd2018-04-12 10:36:57 +020049/**
50 * Write memory coreboot table. Current resource map is serialized into
Aaron Durbin4677f6b2018-04-03 00:08:12 -060051 * memtable (LB_MEM_* types). bootmem library is unusable until this function
Patrick Rudolph23d62dd2018-04-12 10:36:57 +020052 * is called first in the write tables path before payload is loaded.
53 *
54 * Bootmem types match to LB_MEM tags, except for the following:
55 * BM_MEM_RAMSTAGE : Translates to LB_MEM_RAM.
56 * BM_MEM_PAYLOAD : Translates to LB_MEM_RAM.
Ting Shendff29e02019-01-28 18:15:00 +080057 * BM_MEM_BL31 : Translates to LB_MEM_RESERVED.
Patrick Rudolph23d62dd2018-04-12 10:36:57 +020058 */
Aaron Durbin4677f6b2018-04-03 00:08:12 -060059void bootmem_write_memory_table(struct lb_memory *mem);
Aaron Durbin49048022014-02-18 21:55:02 -060060
Aaron Durbin4677f6b2018-04-03 00:08:12 -060061/* Architecture hook to add bootmem areas the architecture controls when
62 * bootmem_write_memory_table() is called. */
Aaron Durbind4afa932016-04-19 17:25:59 -050063void bootmem_arch_add_ranges(void);
64
Patrick Rudolph23d62dd2018-04-12 10:36:57 +020065/* Platform hook to add bootmem areas the platform / board controls. */
66void bootmem_platform_add_ranges(void);
67
Aaron Durbin49048022014-02-18 21:55:02 -060068/* Add a range of a given type to the bootmem address space. */
Patrick Rudolph9ab9db02018-04-05 09:14:51 +020069void bootmem_add_range(uint64_t start, uint64_t size,
70 const enum bootmem_type tag);
Aaron Durbin49048022014-02-18 21:55:02 -060071
Aaron Durbin49048022014-02-18 21:55:02 -060072/* Print current range map of boot memory. */
73void bootmem_dump_ranges(void);
74
Patrick Rudolphc6536232018-04-10 09:34:29 +020075typedef bool (*range_action_t)(const struct range_entry *r, void *arg);
76
77/**
Patrick Rudolph64049be2018-04-20 10:37:51 +020078 * Walk memory tables from OS point of view and call the provided function,
79 * for every region. The caller has to return false to break out of the loop any
80 * time, or return true to continue.
81 *
82 * @param action The function to call for each memory range.
83 * @param arg Pointer passed to function @action. Set to NULL if unused.
84 * @return true if the function 'action' returned false.
85 */
86bool bootmem_walk_os_mem(range_action_t action, void *arg);
87
88/**
Patrick Rudolphc6536232018-04-10 09:34:29 +020089 * Walk memory tables and call the provided function, for every region.
90 * The caller has to return false to break out of the loop any time, or
91 * return true to continue.
92 *
93 * @param action The function to call for each memory range.
94 * @param arg Pointer passed to function @action. Set to NULL if unused.
95 * @return true if the function 'action' returned false.
96 */
97bool bootmem_walk(range_action_t action, void *arg);
98
Ting Shen05532262019-01-28 17:22:22 +080099/* Returns 1 if the requested memory range is all tagged as type dest_type.
100 * Otherwise returns 0.
101 */
102int bootmem_region_targets_type(uint64_t start, uint64_t size,
103 enum bootmem_type dest_type);
Aaron Durbin49048022014-02-18 21:55:02 -0600104
Aaron Durbin49048022014-02-18 21:55:02 -0600105/* Allocate a temporary buffer from the unused RAM areas. */
106void *bootmem_allocate_buffer(size_t size);
107
108#endif /* BOOTMEM_H */