blob: d478b7486e330cb261f1276119ac8c72e9a07a6f [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Bin Menga4899632015-10-07 20:19:10 -07002/*
3 * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
Bin Menga4899632015-10-07 20:19:10 -07004 */
5
Bin Meng1a4e82d2018-06-27 20:38:01 -07006#include <efi_loader.h>
Sughosh Ganu526eebd2024-10-15 21:07:13 +05307#include <lmb.h>
Bin Menga4899632015-10-07 20:19:10 -07008#include <asm/e820.h>
Simon Glass3ba929a2020-10-30 21:38:53 -06009#include <asm/global_data.h>
Bin Menga4899632015-10-07 20:19:10 -070010
11DECLARE_GLOBAL_DATA_PTR;
12
13/*
14 * Install a default e820 table with 4 entries as follows:
15 *
16 * 0x000000-0x0a0000 Useable RAM
17 * 0x0a0000-0x100000 Reserved for ISA
18 * 0x100000-gd->ram_size Useable RAM
19 * CONFIG_PCIE_ECAM_BASE PCIe ECAM
20 */
Bin Meng3838d712018-04-11 22:02:10 -070021__weak unsigned int install_e820_map(unsigned int max_entries,
Bin Meng4b8fc742018-04-11 22:02:11 -070022 struct e820_entry *entries)
Bin Menga4899632015-10-07 20:19:10 -070023{
24 entries[0].addr = 0;
25 entries[0].size = ISA_START_ADDRESS;
26 entries[0].type = E820_RAM;
27 entries[1].addr = ISA_START_ADDRESS;
28 entries[1].size = ISA_END_ADDRESS - ISA_START_ADDRESS;
29 entries[1].type = E820_RESERVED;
30 entries[2].addr = ISA_END_ADDRESS;
31 entries[2].size = gd->ram_size - ISA_END_ADDRESS;
32 entries[2].type = E820_RAM;
33 entries[3].addr = CONFIG_PCIE_ECAM_BASE;
34 entries[3].size = CONFIG_PCIE_ECAM_SIZE;
35 entries[3].type = E820_RESERVED;
36
37 return 4;
38}
Bin Meng1a4e82d2018-06-27 20:38:01 -070039
Stephen Warrend0de8062018-08-30 15:43:43 -060040#if CONFIG_IS_ENABLED(EFI_LOADER)
Bin Meng1a4e82d2018-06-27 20:38:01 -070041void efi_add_known_memory(void)
42{
43 struct e820_entry e820[E820MAX];
44 unsigned int i, num;
Sughosh Ganu526eebd2024-10-15 21:07:13 +053045 u64 start;
Bin Meng1a4e82d2018-06-27 20:38:01 -070046 int type;
47
48 num = install_e820_map(ARRAY_SIZE(e820), e820);
49
50 for (i = 0; i < num; ++i) {
51 start = e820[i].addr;
Bin Meng1a4e82d2018-06-27 20:38:01 -070052
53 switch (e820[i].type) {
54 case E820_RAM:
55 type = EFI_CONVENTIONAL_MEMORY;
56 break;
57 case E820_RESERVED:
58 type = EFI_RESERVED_MEMORY_TYPE;
59 break;
60 case E820_ACPI:
61 type = EFI_ACPI_RECLAIM_MEMORY;
62 break;
63 case E820_NVS:
64 type = EFI_ACPI_MEMORY_NVS;
65 break;
66 case E820_UNUSABLE:
67 default:
68 type = EFI_UNUSABLE_MEMORY;
69 break;
70 }
71
Sughosh Ganu526eebd2024-10-15 21:07:13 +053072 if (type != EFI_CONVENTIONAL_MEMORY)
Michael Walle282d3862020-05-17 12:29:19 +020073 efi_add_memory_map(start, e820[i].size, type);
Bin Meng1a4e82d2018-06-27 20:38:01 -070074 }
75}
Stephen Warrend0de8062018-08-30 15:43:43 -060076#endif /* CONFIG_IS_ENABLED(EFI_LOADER) */
Sughosh Ganu526eebd2024-10-15 21:07:13 +053077
78#if CONFIG_IS_ENABLED(LMB_ARCH_MEM_MAP)
79void lmb_arch_add_memory(void)
80{
81 struct e820_entry e820[E820MAX];
82 unsigned int i, num;
83 u64 ram_top;
84
85 num = install_e820_map(ARRAY_SIZE(e820), e820);
86
87 ram_top = (u64)gd->ram_top & ~EFI_PAGE_MASK;
88 if (!ram_top)
89 ram_top = 0x100000000ULL;
90
91 for (i = 0; i < num; ++i) {
92 if (e820[i].type == E820_RAM) {
93 u64 start, size, rgn_top;
94
95 start = e820[i].addr;
96 size = e820[i].size;
97 rgn_top = start + size;
98
99 if (start > ram_top)
100 continue;
101
102 if (rgn_top > ram_top)
103 size -= rgn_top - ram_top;
104
105 lmb_add(start, size);
106 }
107 }
108}
109#endif /* CONFIG_IS_ENABLED(LMB_ARCH_MEM_MAP) */