1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
8 #include <debug_uart.h>
11 #include <linux/err.h>
12 #include <linux/types.h>
14 DECLARE_GLOBAL_DATA_PTR;
17 * This function looks for the highest region of memory lower than 4GB which
18 * has enough space for U-Boot where U-Boot is aligned on a page boundary.
19 * It overrides the default implementation found elsewhere which simply
20 * picks the end of ram, wherever that may be. The location of the stack,
21 * the relocation address, and how far U-Boot is moved by relocation are
22 * set in the global data structure.
24 ulong board_get_usable_ram_top(ulong total_size)
26 struct efi_mem_desc *desc, *end;
27 struct efi_entry_memmap *map;
29 uintptr_t dest_addr = 0;
30 struct efi_mem_desc *largest = NULL;
33 * Find largest area of memory below 4GB. We could
34 * call efi_build_mem_table() for a more accurate picture since it
35 * merges areas together where possible. But that function uses more
36 * pre-relocation memory, and it's not critical that we find the
37 * absolute largest region.
39 ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
41 /* We should have stopped in dram_init(), something is wrong */
42 debug("%s: Missing memory map\n", __func__);
46 end = (struct efi_mem_desc *)((ulong)map + size);
48 for (; desc < end; desc = efi_get_next_mem_desc(map, desc)) {
49 if (desc->type != EFI_CONVENTIONAL_MEMORY ||
50 desc->physical_start >= 1ULL << 32)
52 if (!largest || desc->num_pages > largest->num_pages)
56 /* If no suitable area was found, return an error. */
58 if (!largest || (largest->num_pages << EFI_PAGE_SHIFT) < (2 << 20))
61 dest_addr = largest->physical_start + (largest->num_pages <<
64 return (ulong)dest_addr;
66 panic("No available memory found for relocation");
72 struct efi_mem_desc *desc, *end;
73 struct efi_entry_memmap *map;
76 ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
78 printf("Cannot find EFI memory map tables, ret=%d\n", ret);
83 end = (struct efi_mem_desc *)((ulong)map + size);
86 for (; desc < end; desc = efi_get_next_mem_desc(map, desc)) {
87 if (desc->type < EFI_MMAP_IO)
88 gd->ram_size += desc->num_pages << EFI_PAGE_SHIFT;
94 int dram_init_banksize(void)
96 struct efi_mem_desc *desc, *end;
97 struct efi_entry_memmap *map;
101 ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
103 /* We should have stopped in dram_init(), something is wrong */
104 debug("%s: Missing memory map\n", __func__);
107 end = (struct efi_mem_desc *)((ulong)map + size);
110 desc < end && num_banks < CONFIG_NR_DRAM_BANKS;
111 desc = efi_get_next_mem_desc(map, desc)) {
113 * We only use conventional memory below 4GB, and ignore
114 * anything less than 1MB.
116 if (desc->type != EFI_CONVENTIONAL_MEMORY ||
117 desc->physical_start >= 1ULL << 32 ||
118 (desc->num_pages << EFI_PAGE_SHIFT) < 1 << 20)
120 gd->bd->bi_dram[num_banks].start = desc->physical_start;
121 gd->bd->bi_dram[num_banks].size = desc->num_pages <<
134 int print_cpuinfo(void)
136 return default_print_cpuinfo();
139 /* Find any available tables and copy them to a safe place */
140 int reserve_arch(void)
142 struct efi_info_hdr *hdr;
144 debug("table=%lx\n", gd->arch.table);
148 hdr = (struct efi_info_hdr *)gd->arch.table;
150 gd->start_addr_sp -= hdr->total_size;
151 memcpy((void *)gd->start_addr_sp, hdr, hdr->total_size);
152 debug("Stashing EFI table at %lx to %lx, size %x\n",
153 gd->arch.table, gd->start_addr_sp, hdr->total_size);
154 gd->arch.table = gd->start_addr_sp;