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