1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2019 Intel Corporation <www.intel.com>
7 #include <linux/sizes.h>
9 #include <asm/arch/slimbootloader.h>
11 DECLARE_GLOBAL_DATA_PTR;
14 * This returns a data pointer of memory map info from the guid hob.
16 * @return: A data pointer of memory map info hob
18 static struct sbl_memory_map_info *get_memory_map_info(void)
20 struct sbl_memory_map_info *data;
21 const efi_guid_t guid = SBL_MEMORY_MAP_INFO_GUID;
23 if (!gd->arch.hob_list)
26 data = hob_get_guid_hob_data(gd->arch.hob_list, NULL, &guid);
28 panic("memory map info hob not found\n");
30 panic("invalid number of memory map entries\n");
35 #define for_each_if(condition) if (!(condition)) {} else
37 #define for_each_memory_map_entry_reversed(iter, entries) \
38 for (iter = entries->count - 1; iter >= 0; iter--) \
39 for_each_if(entries->entry[iter].type == E820_RAM)
42 * This is to give usable memory region information for u-boot relocation.
43 * so search usable memory region lower than 4GB.
44 * The memory map entries from Slim Bootloader hob are already sorted.
46 * @total_size: The memory size that u-boot occupies
47 * @return : The top available memory address lower than 4GB
49 ulong board_get_usable_ram_top(ulong total_size)
51 struct sbl_memory_map_info *data;
57 data = get_memory_map_info();
60 * sorted memory map entries from Slim Bootloader based on physical
61 * start memory address, from low to high. So do reversed search to
62 * get highest usable, suitable size, 4KB aligned available memory
66 for_each_memory_map_entry_reversed(i, data) {
67 addr_start = data->entry[i].addr;
68 addr_end = addr_start + data->entry[i].size;
70 if (addr_start > SZ_4G)
76 if (addr_end < total_size)
79 /* to relocate u-boot at 4K aligned memory */
80 addr_end = rounddown(addr_end - total_size, SZ_4K);
81 if (addr_end >= addr_start) {
82 ram_top = (ulong)addr_end + total_size;
88 panic("failed to find available memory for relocation!");
94 * The memory initialization has already been done in previous Slim Bootloader
95 * stage thru FSP-M. Instead, this sets the ram_size from the memory map info
100 struct sbl_memory_map_info *data;
104 data = get_memory_map_info();
107 * sorted memory map entries from Slim Bootloader based on physical
108 * start memory address, from low to high. So do reversed search to
109 * simply get highest usable memory address as RAM size
112 for_each_memory_map_entry_reversed(i, data) {
113 /* simply use the highest usable memory address as RAM size */
114 ram_size = data->entry[i].addr + data->entry[i].size;
119 panic("failed to detect memory size");
121 gd->ram_size = ram_size;
125 int dram_init_banksize(void)
127 if (!CONFIG_NR_DRAM_BANKS)
130 /* simply use a single bank to have whole size for now */
131 gd->bd->bi_dram[0].start = 0;
132 gd->bd->bi_dram[0].size = gd->ram_size;
136 unsigned int install_e820_map(unsigned int max_entries,
137 struct e820_entry *entries)
139 struct sbl_memory_map_info *data;
142 data = get_memory_map_info();
144 for (i = 0; i < data->count; i++) {
145 entries[i].addr = data->entry[i].addr;
146 entries[i].size = data->entry[i].size;
147 entries[i].type = data->entry[i].type;