1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2019 Intel Corporation <www.intel.com>
8 #include <linux/sizes.h>
10 #include <asm/arch/slimbootloader.h>
12 DECLARE_GLOBAL_DATA_PTR;
15 * This returns a data pointer of memory map info from the guid hob.
17 * @return: A data pointer of memory map info hob
19 static struct sbl_memory_map_info *get_memory_map_info(void)
21 struct sbl_memory_map_info *data;
22 const efi_guid_t guid = SBL_MEMORY_MAP_INFO_GUID;
24 if (!gd->arch.hob_list)
27 data = hob_get_guid_hob_data(gd->arch.hob_list, NULL, &guid);
29 panic("memory map info hob not found\n");
31 panic("invalid number of memory map entries\n");
36 #define for_each_if(condition) if (!(condition)) {} else
38 #define for_each_memory_map_entry_reversed(iter, entries) \
39 for (iter = entries->count - 1; iter >= 0; iter--) \
40 for_each_if(entries->entry[iter].type == E820_RAM)
43 * This is to give usable memory region information for u-boot relocation.
44 * so search usable memory region lower than 4GB.
45 * The memory map entries from Slim Bootloader hob are already sorted.
47 * @total_size: The memory size that u-boot occupies
48 * @return : The top available memory address lower than 4GB
50 ulong board_get_usable_ram_top(ulong total_size)
52 struct sbl_memory_map_info *data;
58 data = get_memory_map_info();
61 * sorted memory map entries from Slim Bootloader based on physical
62 * start memory address, from low to high. So do reversed search to
63 * get highest usable, suitable size, 4KB aligned available memory
67 for_each_memory_map_entry_reversed(i, data) {
68 addr_start = data->entry[i].addr;
69 addr_end = addr_start + data->entry[i].size;
71 if (addr_start > SZ_4G)
77 if (addr_end < total_size)
80 /* to relocate u-boot at 4K aligned memory */
81 addr_end = rounddown(addr_end - total_size, SZ_4K);
82 if (addr_end >= addr_start) {
83 ram_top = (ulong)addr_end + total_size;
89 panic("failed to find available memory for relocation!");
95 * The memory initialization has already been done in previous Slim Bootloader
96 * stage thru FSP-M. Instead, this sets the ram_size from the memory map info
101 struct sbl_memory_map_info *data;
105 data = get_memory_map_info();
108 * sorted memory map entries from Slim Bootloader based on physical
109 * start memory address, from low to high. So do reversed search to
110 * simply get highest usable memory address as RAM size
113 for_each_memory_map_entry_reversed(i, data) {
114 /* simply use the highest usable memory address as RAM size */
115 ram_size = data->entry[i].addr + data->entry[i].size;
120 panic("failed to detect memory size");
122 gd->ram_size = ram_size;
126 int dram_init_banksize(void)
128 if (!CONFIG_NR_DRAM_BANKS)
131 /* simply use a single bank to have whole size for now */
132 gd->bd->bi_dram[0].start = 0;
133 gd->bd->bi_dram[0].size = gd->ram_size;
137 unsigned int install_e820_map(unsigned int max_entries,
138 struct e820_entry *entries)
140 struct sbl_memory_map_info *data;
143 data = get_memory_map_info();
145 for (i = 0; i < data->count; i++) {
146 entries[i].addr = data->entry[i].addr;
147 entries[i].size = data->entry[i].size;
148 entries[i].type = data->entry[i].type;