Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Bin Meng | a489963 | 2015-10-07 20:19:10 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com> |
Bin Meng | a489963 | 2015-10-07 20:19:10 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Bin Meng | 1a4e82d | 2018-06-27 20:38:01 -0700 | [diff] [blame] | 6 | #include <efi_loader.h> |
Sughosh Ganu | 526eebd | 2024-10-15 21:07:13 +0530 | [diff] [blame] | 7 | #include <lmb.h> |
Bin Meng | a489963 | 2015-10-07 20:19:10 -0700 | [diff] [blame] | 8 | #include <asm/e820.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 9 | #include <asm/global_data.h> |
Bin Meng | a489963 | 2015-10-07 20:19:10 -0700 | [diff] [blame] | 10 | |
| 11 | DECLARE_GLOBAL_DATA_PTR; |
| 12 | |
Simon Glass | b106f07 | 2025-03-15 14:25:52 +0000 | [diff] [blame^] | 13 | static const char *const e820_type_name[E820_COUNT] = { |
| 14 | [E820_RAM] = "RAM", |
| 15 | [E820_RESERVED] = "Reserved", |
| 16 | [E820_ACPI] = "ACPI", |
| 17 | [E820_NVS] = "ACPI NVS", |
| 18 | [E820_UNUSABLE] = "Unusable", |
| 19 | }; |
| 20 | |
| 21 | void e820_dump(struct e820_entry *entries, uint count) |
| 22 | { |
| 23 | int i; |
| 24 | |
| 25 | printf("%12s %10s %s\n", "Addr", "Size", "Type"); |
| 26 | for (i = 0; i < count; i++) { |
| 27 | struct e820_entry *entry = &entries[i]; |
| 28 | |
| 29 | printf("%12llx %10llx %s\n", entry->addr, entry->size, |
| 30 | entry->type < E820_COUNT ? |
| 31 | e820_type_name[entry->type] : |
| 32 | simple_itoa(entry->type)); |
| 33 | } |
| 34 | } |
| 35 | |
Bin Meng | a489963 | 2015-10-07 20:19:10 -0700 | [diff] [blame] | 36 | /* |
| 37 | * Install a default e820 table with 4 entries as follows: |
| 38 | * |
| 39 | * 0x000000-0x0a0000 Useable RAM |
| 40 | * 0x0a0000-0x100000 Reserved for ISA |
| 41 | * 0x100000-gd->ram_size Useable RAM |
| 42 | * CONFIG_PCIE_ECAM_BASE PCIe ECAM |
| 43 | */ |
Bin Meng | 3838d71 | 2018-04-11 22:02:10 -0700 | [diff] [blame] | 44 | __weak unsigned int install_e820_map(unsigned int max_entries, |
Bin Meng | 4b8fc74 | 2018-04-11 22:02:11 -0700 | [diff] [blame] | 45 | struct e820_entry *entries) |
Bin Meng | a489963 | 2015-10-07 20:19:10 -0700 | [diff] [blame] | 46 | { |
| 47 | entries[0].addr = 0; |
| 48 | entries[0].size = ISA_START_ADDRESS; |
| 49 | entries[0].type = E820_RAM; |
| 50 | entries[1].addr = ISA_START_ADDRESS; |
| 51 | entries[1].size = ISA_END_ADDRESS - ISA_START_ADDRESS; |
| 52 | entries[1].type = E820_RESERVED; |
| 53 | entries[2].addr = ISA_END_ADDRESS; |
| 54 | entries[2].size = gd->ram_size - ISA_END_ADDRESS; |
| 55 | entries[2].type = E820_RAM; |
| 56 | entries[3].addr = CONFIG_PCIE_ECAM_BASE; |
| 57 | entries[3].size = CONFIG_PCIE_ECAM_SIZE; |
| 58 | entries[3].type = E820_RESERVED; |
| 59 | |
| 60 | return 4; |
| 61 | } |
Bin Meng | 1a4e82d | 2018-06-27 20:38:01 -0700 | [diff] [blame] | 62 | |
Stephen Warren | d0de806 | 2018-08-30 15:43:43 -0600 | [diff] [blame] | 63 | #if CONFIG_IS_ENABLED(EFI_LOADER) |
Bin Meng | 1a4e82d | 2018-06-27 20:38:01 -0700 | [diff] [blame] | 64 | void efi_add_known_memory(void) |
| 65 | { |
| 66 | struct e820_entry e820[E820MAX]; |
| 67 | unsigned int i, num; |
Sughosh Ganu | 526eebd | 2024-10-15 21:07:13 +0530 | [diff] [blame] | 68 | u64 start; |
Bin Meng | 1a4e82d | 2018-06-27 20:38:01 -0700 | [diff] [blame] | 69 | int type; |
| 70 | |
| 71 | num = install_e820_map(ARRAY_SIZE(e820), e820); |
| 72 | |
| 73 | for (i = 0; i < num; ++i) { |
| 74 | start = e820[i].addr; |
Bin Meng | 1a4e82d | 2018-06-27 20:38:01 -0700 | [diff] [blame] | 75 | |
| 76 | switch (e820[i].type) { |
| 77 | case E820_RAM: |
| 78 | type = EFI_CONVENTIONAL_MEMORY; |
| 79 | break; |
| 80 | case E820_RESERVED: |
| 81 | type = EFI_RESERVED_MEMORY_TYPE; |
| 82 | break; |
| 83 | case E820_ACPI: |
| 84 | type = EFI_ACPI_RECLAIM_MEMORY; |
| 85 | break; |
| 86 | case E820_NVS: |
| 87 | type = EFI_ACPI_MEMORY_NVS; |
| 88 | break; |
| 89 | case E820_UNUSABLE: |
| 90 | default: |
| 91 | type = EFI_UNUSABLE_MEMORY; |
| 92 | break; |
| 93 | } |
| 94 | |
Sughosh Ganu | 526eebd | 2024-10-15 21:07:13 +0530 | [diff] [blame] | 95 | if (type != EFI_CONVENTIONAL_MEMORY) |
Michael Walle | 282d386 | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 96 | efi_add_memory_map(start, e820[i].size, type); |
Bin Meng | 1a4e82d | 2018-06-27 20:38:01 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
Stephen Warren | d0de806 | 2018-08-30 15:43:43 -0600 | [diff] [blame] | 99 | #endif /* CONFIG_IS_ENABLED(EFI_LOADER) */ |
Sughosh Ganu | 526eebd | 2024-10-15 21:07:13 +0530 | [diff] [blame] | 100 | |
| 101 | #if CONFIG_IS_ENABLED(LMB_ARCH_MEM_MAP) |
| 102 | void lmb_arch_add_memory(void) |
| 103 | { |
| 104 | struct e820_entry e820[E820MAX]; |
| 105 | unsigned int i, num; |
| 106 | u64 ram_top; |
| 107 | |
| 108 | num = install_e820_map(ARRAY_SIZE(e820), e820); |
| 109 | |
| 110 | ram_top = (u64)gd->ram_top & ~EFI_PAGE_MASK; |
| 111 | if (!ram_top) |
| 112 | ram_top = 0x100000000ULL; |
| 113 | |
| 114 | for (i = 0; i < num; ++i) { |
| 115 | if (e820[i].type == E820_RAM) { |
| 116 | u64 start, size, rgn_top; |
| 117 | |
| 118 | start = e820[i].addr; |
| 119 | size = e820[i].size; |
| 120 | rgn_top = start + size; |
| 121 | |
| 122 | if (start > ram_top) |
| 123 | continue; |
| 124 | |
| 125 | if (rgn_top > ram_top) |
| 126 | size -= rgn_top - ram_top; |
| 127 | |
| 128 | lmb_add(start, size); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | #endif /* CONFIG_IS_ENABLED(LMB_ARCH_MEM_MAP) */ |