Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | d2269f2 | 2015-08-04 12:34:01 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | d2269f2 | 2015-08-04 12:34:01 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | 1fa70f8 | 2019-11-14 12:57:34 -0700 | [diff] [blame] | 8 | #include <cpu_func.h> |
Simon Glass | d2269f2 | 2015-08-04 12:34:01 -0600 | [diff] [blame] | 9 | #include <efi.h> |
Simon Glass | c3dcef1 | 2021-12-29 11:57:53 -0700 | [diff] [blame] | 10 | #include <efi_api.h> |
Simon Glass | d2269f2 | 2015-08-04 12:34:01 -0600 | [diff] [blame] | 11 | #include <errno.h> |
Simon Glass | 6980b6b | 2019-11-14 12:57:45 -0700 | [diff] [blame] | 12 | #include <init.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Bin Meng | db59dd3 | 2018-06-17 05:57:53 -0700 | [diff] [blame] | 14 | #include <usb.h> |
Bin Meng | 106dcfc | 2018-08-23 08:24:10 -0700 | [diff] [blame] | 15 | #include <asm/bootparam.h> |
Bin Meng | 2f65c07 | 2018-08-23 08:24:08 -0700 | [diff] [blame] | 16 | #include <asm/e820.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 17 | #include <asm/global_data.h> |
Bin Meng | 6bd4109 | 2018-06-12 08:36:17 -0700 | [diff] [blame] | 18 | #include <asm/post.h> |
Simon Glass | d2269f2 | 2015-08-04 12:34:01 -0600 | [diff] [blame] | 19 | |
| 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
| 22 | /* |
| 23 | * This function looks for the highest region of memory lower than 4GB which |
| 24 | * has enough space for U-Boot where U-Boot is aligned on a page boundary. |
| 25 | * It overrides the default implementation found elsewhere which simply |
| 26 | * picks the end of ram, wherever that may be. The location of the stack, |
| 27 | * the relocation address, and how far U-Boot is moved by relocation are |
| 28 | * set in the global data structure. |
| 29 | */ |
| 30 | ulong board_get_usable_ram_top(ulong total_size) |
| 31 | { |
| 32 | struct efi_mem_desc *desc, *end; |
| 33 | struct efi_entry_memmap *map; |
| 34 | int ret, size; |
| 35 | uintptr_t dest_addr = 0; |
| 36 | struct efi_mem_desc *largest = NULL; |
| 37 | |
| 38 | /* |
| 39 | * Find largest area of memory below 4GB. We could |
| 40 | * call efi_build_mem_table() for a more accurate picture since it |
| 41 | * merges areas together where possible. But that function uses more |
| 42 | * pre-relocation memory, and it's not critical that we find the |
| 43 | * absolute largest region. |
| 44 | */ |
| 45 | ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size); |
| 46 | if (ret) { |
| 47 | /* We should have stopped in dram_init(), something is wrong */ |
| 48 | debug("%s: Missing memory map\n", __func__); |
| 49 | goto err; |
| 50 | } |
| 51 | |
| 52 | end = (struct efi_mem_desc *)((ulong)map + size); |
| 53 | desc = map->desc; |
Simon Glass | 6f4d7aa | 2022-01-04 03:51:11 -0700 | [diff] [blame] | 54 | for (; desc < end; desc = efi_get_next_mem_desc(desc, map->desc_size)) { |
Simon Glass | d2269f2 | 2015-08-04 12:34:01 -0600 | [diff] [blame] | 55 | if (desc->type != EFI_CONVENTIONAL_MEMORY || |
| 56 | desc->physical_start >= 1ULL << 32) |
| 57 | continue; |
| 58 | if (!largest || desc->num_pages > largest->num_pages) |
| 59 | largest = desc; |
| 60 | } |
| 61 | |
| 62 | /* If no suitable area was found, return an error. */ |
| 63 | assert(largest); |
| 64 | if (!largest || (largest->num_pages << EFI_PAGE_SHIFT) < (2 << 20)) |
| 65 | goto err; |
| 66 | |
| 67 | dest_addr = largest->physical_start + (largest->num_pages << |
| 68 | EFI_PAGE_SHIFT); |
| 69 | |
| 70 | return (ulong)dest_addr; |
| 71 | err: |
| 72 | panic("No available memory found for relocation"); |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | int dram_init(void) |
| 77 | { |
| 78 | struct efi_mem_desc *desc, *end; |
| 79 | struct efi_entry_memmap *map; |
| 80 | int size, ret; |
| 81 | |
| 82 | ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size); |
| 83 | if (ret) { |
| 84 | printf("Cannot find EFI memory map tables, ret=%d\n", ret); |
| 85 | |
| 86 | return -ENODEV; |
| 87 | } |
| 88 | |
| 89 | end = (struct efi_mem_desc *)((ulong)map + size); |
| 90 | gd->ram_size = 0; |
| 91 | desc = map->desc; |
Simon Glass | 6f4d7aa | 2022-01-04 03:51:11 -0700 | [diff] [blame] | 92 | for (; desc < end; desc = efi_get_next_mem_desc(desc, map->desc_size)) { |
Simon Glass | d2269f2 | 2015-08-04 12:34:01 -0600 | [diff] [blame] | 93 | if (desc->type < EFI_MMAP_IO) |
| 94 | gd->ram_size += desc->num_pages << EFI_PAGE_SHIFT; |
| 95 | } |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
Simon Glass | 2f949c3 | 2017-03-31 08:40:32 -0600 | [diff] [blame] | 100 | int dram_init_banksize(void) |
Simon Glass | d2269f2 | 2015-08-04 12:34:01 -0600 | [diff] [blame] | 101 | { |
| 102 | struct efi_mem_desc *desc, *end; |
| 103 | struct efi_entry_memmap *map; |
| 104 | int ret, size; |
| 105 | int num_banks; |
| 106 | |
| 107 | ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size); |
| 108 | if (ret) { |
| 109 | /* We should have stopped in dram_init(), something is wrong */ |
| 110 | debug("%s: Missing memory map\n", __func__); |
Simon Glass | 2f949c3 | 2017-03-31 08:40:32 -0600 | [diff] [blame] | 111 | return -ENXIO; |
Simon Glass | d2269f2 | 2015-08-04 12:34:01 -0600 | [diff] [blame] | 112 | } |
| 113 | end = (struct efi_mem_desc *)((ulong)map + size); |
| 114 | desc = map->desc; |
| 115 | for (num_banks = 0; |
| 116 | desc < end && num_banks < CONFIG_NR_DRAM_BANKS; |
Simon Glass | 6f4d7aa | 2022-01-04 03:51:11 -0700 | [diff] [blame] | 117 | desc = efi_get_next_mem_desc(desc, map->desc_size)) { |
Simon Glass | d2269f2 | 2015-08-04 12:34:01 -0600 | [diff] [blame] | 118 | /* |
Bin Meng | 7eb28a5 | 2018-06-22 01:38:30 -0700 | [diff] [blame] | 119 | * We only use conventional memory and ignore |
Simon Glass | d2269f2 | 2015-08-04 12:34:01 -0600 | [diff] [blame] | 120 | * anything less than 1MB. |
| 121 | */ |
| 122 | if (desc->type != EFI_CONVENTIONAL_MEMORY || |
Simon Glass | d2269f2 | 2015-08-04 12:34:01 -0600 | [diff] [blame] | 123 | (desc->num_pages << EFI_PAGE_SHIFT) < 1 << 20) |
| 124 | continue; |
| 125 | gd->bd->bi_dram[num_banks].start = desc->physical_start; |
| 126 | gd->bd->bi_dram[num_banks].size = desc->num_pages << |
| 127 | EFI_PAGE_SHIFT; |
| 128 | num_banks++; |
| 129 | } |
Simon Glass | 2f949c3 | 2017-03-31 08:40:32 -0600 | [diff] [blame] | 130 | |
| 131 | return 0; |
Simon Glass | d2269f2 | 2015-08-04 12:34:01 -0600 | [diff] [blame] | 132 | } |
| 133 | |
Bin Meng | 6bd4109 | 2018-06-12 08:36:17 -0700 | [diff] [blame] | 134 | int arch_cpu_init(void) |
| 135 | { |
| 136 | post_code(POST_CPU_INIT); |
| 137 | |
| 138 | return x86_cpu_init_f(); |
| 139 | } |
| 140 | |
Simon Glass | ee7c36f | 2017-03-28 10:27:30 -0600 | [diff] [blame] | 141 | int checkcpu(void) |
| 142 | { |
| 143 | return 0; |
| 144 | } |
| 145 | |
Simon Glass | d2269f2 | 2015-08-04 12:34:01 -0600 | [diff] [blame] | 146 | int print_cpuinfo(void) |
| 147 | { |
| 148 | return default_print_cpuinfo(); |
| 149 | } |
| 150 | |
| 151 | /* Find any available tables and copy them to a safe place */ |
| 152 | int reserve_arch(void) |
| 153 | { |
| 154 | struct efi_info_hdr *hdr; |
| 155 | |
| 156 | debug("table=%lx\n", gd->arch.table); |
| 157 | if (!gd->arch.table) |
| 158 | return 0; |
| 159 | |
| 160 | hdr = (struct efi_info_hdr *)gd->arch.table; |
| 161 | |
| 162 | gd->start_addr_sp -= hdr->total_size; |
| 163 | memcpy((void *)gd->start_addr_sp, hdr, hdr->total_size); |
| 164 | debug("Stashing EFI table at %lx to %lx, size %x\n", |
| 165 | gd->arch.table, gd->start_addr_sp, hdr->total_size); |
| 166 | gd->arch.table = gd->start_addr_sp; |
| 167 | |
| 168 | return 0; |
| 169 | } |
Bin Meng | db59dd3 | 2018-06-17 05:57:53 -0700 | [diff] [blame] | 170 | |
| 171 | int last_stage_init(void) |
| 172 | { |
| 173 | /* start usb so that usb keyboard can be used as input device */ |
Bin Meng | a7bb8ef | 2018-11-29 20:07:13 -0800 | [diff] [blame] | 174 | if (CONFIG_IS_ENABLED(USB_KEYBOARD)) |
| 175 | usb_init(); |
Bin Meng | db59dd3 | 2018-06-17 05:57:53 -0700 | [diff] [blame] | 176 | |
| 177 | return 0; |
| 178 | } |
Bin Meng | 2f65c07 | 2018-08-23 08:24:08 -0700 | [diff] [blame] | 179 | |
| 180 | unsigned int install_e820_map(unsigned int max_entries, |
| 181 | struct e820_entry *entries) |
| 182 | { |
| 183 | struct efi_mem_desc *desc, *end; |
| 184 | struct efi_entry_memmap *map; |
| 185 | int size, ret; |
| 186 | efi_physical_addr_t last_end_addr = 0; |
| 187 | struct e820_entry *last_entry = NULL; |
| 188 | __u32 e820_type; |
| 189 | unsigned int num_entries = 0; |
| 190 | |
| 191 | ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size); |
| 192 | if (ret) { |
| 193 | printf("Cannot find EFI memory map tables, ret=%d\n", ret); |
| 194 | |
| 195 | return -ENODEV; |
| 196 | } |
| 197 | |
| 198 | end = (struct efi_mem_desc *)((ulong)map + size); |
| 199 | for (desc = map->desc; desc < end; |
Simon Glass | 6f4d7aa | 2022-01-04 03:51:11 -0700 | [diff] [blame] | 200 | desc = efi_get_next_mem_desc(desc, map->desc_size)) { |
Bin Meng | 2f65c07 | 2018-08-23 08:24:08 -0700 | [diff] [blame] | 201 | if (desc->num_pages == 0) |
| 202 | continue; |
| 203 | |
| 204 | switch (desc->type) { |
| 205 | case EFI_LOADER_CODE: |
| 206 | case EFI_LOADER_DATA: |
| 207 | case EFI_BOOT_SERVICES_CODE: |
| 208 | case EFI_BOOT_SERVICES_DATA: |
| 209 | case EFI_CONVENTIONAL_MEMORY: |
| 210 | e820_type = E820_RAM; |
| 211 | break; |
| 212 | |
| 213 | case EFI_RESERVED_MEMORY_TYPE: |
| 214 | case EFI_RUNTIME_SERVICES_CODE: |
| 215 | case EFI_RUNTIME_SERVICES_DATA: |
| 216 | case EFI_MMAP_IO: |
| 217 | case EFI_MMAP_IO_PORT: |
| 218 | case EFI_PAL_CODE: |
| 219 | e820_type = E820_RESERVED; |
| 220 | break; |
| 221 | |
| 222 | case EFI_ACPI_RECLAIM_MEMORY: |
| 223 | e820_type = E820_ACPI; |
| 224 | break; |
| 225 | |
| 226 | case EFI_ACPI_MEMORY_NVS: |
| 227 | e820_type = E820_NVS; |
| 228 | break; |
| 229 | |
| 230 | case EFI_UNUSABLE_MEMORY: |
| 231 | e820_type = E820_UNUSABLE; |
| 232 | break; |
| 233 | |
| 234 | default: |
| 235 | printf("Invalid EFI memory descriptor type (0x%x)!\n", |
| 236 | desc->type); |
| 237 | continue; |
| 238 | } |
| 239 | |
| 240 | if (last_entry != NULL && last_entry->type == e820_type && |
| 241 | desc->physical_start == last_end_addr) { |
| 242 | last_entry->size += (desc->num_pages << EFI_PAGE_SHIFT); |
| 243 | last_end_addr += (desc->num_pages << EFI_PAGE_SHIFT); |
| 244 | } else { |
| 245 | if (num_entries >= E820MAX) |
| 246 | break; |
| 247 | |
| 248 | entries[num_entries].addr = desc->physical_start; |
| 249 | entries[num_entries].size = desc->num_pages; |
| 250 | entries[num_entries].size <<= EFI_PAGE_SHIFT; |
| 251 | entries[num_entries].type = e820_type; |
| 252 | last_entry = &entries[num_entries]; |
| 253 | last_end_addr = last_entry->addr + last_entry->size; |
| 254 | num_entries++; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | return num_entries; |
| 259 | } |
Bin Meng | 106dcfc | 2018-08-23 08:24:10 -0700 | [diff] [blame] | 260 | |
| 261 | void setup_efi_info(struct efi_info *efi_info) |
| 262 | { |
| 263 | struct efi_entry_systable *table; |
| 264 | struct efi_entry_memmap *map; |
| 265 | char *signature; |
| 266 | int size, ret; |
| 267 | |
| 268 | memset(efi_info, 0, sizeof(struct efi_info)); |
| 269 | |
| 270 | ret = efi_info_get(EFIET_SYS_TABLE, (void **)&table, &size); |
| 271 | if (ret) { |
| 272 | printf("Cannot find EFI system table, ret=%d\n", ret); |
| 273 | return; |
| 274 | } |
| 275 | efi_info->efi_systab = (u32)(table->sys_table); |
| 276 | |
| 277 | ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size); |
| 278 | if (ret) { |
| 279 | printf("Cannot find EFI memory map tables, ret=%d\n", ret); |
| 280 | return; |
| 281 | } |
| 282 | efi_info->efi_memdesc_size = map->desc_size; |
| 283 | efi_info->efi_memdesc_version = map->version; |
Simon Glass | 912e06f | 2021-09-24 18:30:21 -0600 | [diff] [blame] | 284 | efi_info->efi_memmap = (ulong)(map->desc); |
Bin Meng | 106dcfc | 2018-08-23 08:24:10 -0700 | [diff] [blame] | 285 | efi_info->efi_memmap_size = size - sizeof(struct efi_entry_memmap); |
| 286 | |
| 287 | #ifdef CONFIG_EFI_STUB_64BIT |
| 288 | efi_info->efi_systab_hi = table->sys_table >> 32; |
Simon Glass | 912e06f | 2021-09-24 18:30:21 -0600 | [diff] [blame] | 289 | efi_info->efi_memmap_hi = (u64)(ulong)map->desc >> 32; |
Bin Meng | 106dcfc | 2018-08-23 08:24:10 -0700 | [diff] [blame] | 290 | signature = EFI64_LOADER_SIGNATURE; |
| 291 | #else |
| 292 | signature = EFI32_LOADER_SIGNATURE; |
| 293 | #endif |
| 294 | memcpy(&efi_info->efi_loader_signature, signature, 4); |
| 295 | } |
Simon Glass | 912e06f | 2021-09-24 18:30:21 -0600 | [diff] [blame] | 296 | |
| 297 | void efi_show_bdinfo(void) |
| 298 | { |
| 299 | struct efi_entry_systable *table = NULL; |
Simon Glass | c3dcef1 | 2021-12-29 11:57:53 -0700 | [diff] [blame] | 300 | struct efi_system_table *sys_table; |
Simon Glass | 912e06f | 2021-09-24 18:30:21 -0600 | [diff] [blame] | 301 | int size, ret; |
| 302 | |
| 303 | ret = efi_info_get(EFIET_SYS_TABLE, (void **)&table, &size); |
Simon Glass | c3dcef1 | 2021-12-29 11:57:53 -0700 | [diff] [blame] | 304 | if (!ret) { |
| 305 | bdinfo_print_num_l("efi_table", table->sys_table); |
| 306 | sys_table = (struct efi_system_table *)(uintptr_t) |
| 307 | table->sys_table; |
| 308 | bdinfo_print_num_l(" revision", sys_table->fw_revision); |
| 309 | } |
Simon Glass | 912e06f | 2021-09-24 18:30:21 -0600 | [diff] [blame] | 310 | } |