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