Simon Glass | d2269f2 | 2015-08-04 12:34:01 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Google, Inc |
| 3 | * Written by Simon Glass <sjg@chromium.org> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <debug_uart.h> |
| 10 | #include <efi.h> |
| 11 | #include <errno.h> |
| 12 | #include <linux/err.h> |
| 13 | #include <linux/types.h> |
| 14 | |
| 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
| 17 | /* |
| 18 | * This function looks for the highest region of memory lower than 4GB which |
| 19 | * has enough space for U-Boot where U-Boot is aligned on a page boundary. |
| 20 | * It overrides the default implementation found elsewhere which simply |
| 21 | * picks the end of ram, wherever that may be. The location of the stack, |
| 22 | * the relocation address, and how far U-Boot is moved by relocation are |
| 23 | * set in the global data structure. |
| 24 | */ |
| 25 | ulong board_get_usable_ram_top(ulong total_size) |
| 26 | { |
| 27 | struct efi_mem_desc *desc, *end; |
| 28 | struct efi_entry_memmap *map; |
| 29 | int ret, size; |
| 30 | uintptr_t dest_addr = 0; |
| 31 | struct efi_mem_desc *largest = NULL; |
| 32 | |
| 33 | /* |
| 34 | * Find largest area of memory below 4GB. We could |
| 35 | * call efi_build_mem_table() for a more accurate picture since it |
| 36 | * merges areas together where possible. But that function uses more |
| 37 | * pre-relocation memory, and it's not critical that we find the |
| 38 | * absolute largest region. |
| 39 | */ |
| 40 | ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size); |
| 41 | if (ret) { |
| 42 | /* We should have stopped in dram_init(), something is wrong */ |
| 43 | debug("%s: Missing memory map\n", __func__); |
| 44 | goto err; |
| 45 | } |
| 46 | |
| 47 | end = (struct efi_mem_desc *)((ulong)map + size); |
| 48 | desc = map->desc; |
| 49 | for (; desc < end; desc = efi_get_next_mem_desc(map, desc)) { |
| 50 | if (desc->type != EFI_CONVENTIONAL_MEMORY || |
| 51 | desc->physical_start >= 1ULL << 32) |
| 52 | continue; |
| 53 | if (!largest || desc->num_pages > largest->num_pages) |
| 54 | largest = desc; |
| 55 | } |
| 56 | |
| 57 | /* If no suitable area was found, return an error. */ |
| 58 | assert(largest); |
| 59 | if (!largest || (largest->num_pages << EFI_PAGE_SHIFT) < (2 << 20)) |
| 60 | goto err; |
| 61 | |
| 62 | dest_addr = largest->physical_start + (largest->num_pages << |
| 63 | EFI_PAGE_SHIFT); |
| 64 | |
| 65 | return (ulong)dest_addr; |
| 66 | err: |
| 67 | panic("No available memory found for relocation"); |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | int dram_init(void) |
| 72 | { |
| 73 | struct efi_mem_desc *desc, *end; |
| 74 | struct efi_entry_memmap *map; |
| 75 | int size, ret; |
| 76 | |
| 77 | ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size); |
| 78 | if (ret) { |
| 79 | printf("Cannot find EFI memory map tables, ret=%d\n", ret); |
| 80 | |
| 81 | return -ENODEV; |
| 82 | } |
| 83 | |
| 84 | end = (struct efi_mem_desc *)((ulong)map + size); |
| 85 | gd->ram_size = 0; |
| 86 | desc = map->desc; |
| 87 | for (; desc < end; desc = efi_get_next_mem_desc(map, desc)) { |
| 88 | if (desc->type < EFI_MMAP_IO) |
| 89 | gd->ram_size += desc->num_pages << EFI_PAGE_SHIFT; |
| 90 | } |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | void dram_init_banksize(void) |
| 96 | { |
| 97 | struct efi_mem_desc *desc, *end; |
| 98 | struct efi_entry_memmap *map; |
| 99 | int ret, size; |
| 100 | int num_banks; |
| 101 | |
| 102 | ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size); |
| 103 | if (ret) { |
| 104 | /* We should have stopped in dram_init(), something is wrong */ |
| 105 | debug("%s: Missing memory map\n", __func__); |
| 106 | return; |
| 107 | } |
| 108 | end = (struct efi_mem_desc *)((ulong)map + size); |
| 109 | desc = map->desc; |
| 110 | for (num_banks = 0; |
| 111 | desc < end && num_banks < CONFIG_NR_DRAM_BANKS; |
| 112 | desc = efi_get_next_mem_desc(map, desc)) { |
| 113 | /* |
| 114 | * We only use conventional memory below 4GB, and ignore |
| 115 | * anything less than 1MB. |
| 116 | */ |
| 117 | if (desc->type != EFI_CONVENTIONAL_MEMORY || |
| 118 | desc->physical_start >= 1ULL << 32 || |
| 119 | (desc->num_pages << EFI_PAGE_SHIFT) < 1 << 20) |
| 120 | continue; |
| 121 | gd->bd->bi_dram[num_banks].start = desc->physical_start; |
| 122 | gd->bd->bi_dram[num_banks].size = desc->num_pages << |
| 123 | EFI_PAGE_SHIFT; |
| 124 | num_banks++; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | int print_cpuinfo(void) |
| 129 | { |
| 130 | return default_print_cpuinfo(); |
| 131 | } |
| 132 | |
| 133 | /* Find any available tables and copy them to a safe place */ |
| 134 | int reserve_arch(void) |
| 135 | { |
| 136 | struct efi_info_hdr *hdr; |
| 137 | |
| 138 | debug("table=%lx\n", gd->arch.table); |
| 139 | if (!gd->arch.table) |
| 140 | return 0; |
| 141 | |
| 142 | hdr = (struct efi_info_hdr *)gd->arch.table; |
| 143 | |
| 144 | gd->start_addr_sp -= hdr->total_size; |
| 145 | memcpy((void *)gd->start_addr_sp, hdr, hdr->total_size); |
| 146 | debug("Stashing EFI table at %lx to %lx, size %x\n", |
| 147 | gd->arch.table, gd->start_addr_sp, hdr->total_size); |
| 148 | gd->arch.table = gd->start_addr_sp; |
| 149 | |
| 150 | return 0; |
| 151 | } |