Re: [U-Boot] [PATCH v7 4/9] x86: slimbootloader: Add memory configuration
On Mon, Jul 29, 2019 at 7:35 AM Park, Aiden wrote: > > Slim Bootloader provides memory map info thru its HOB list pointer. > Configure memory size and relocation memory from the HOB data, and > provide e820 entries as well. > - Get memory size from the memory map info HOB > - Set available top memory lower than 4GB for U-Boot relocation > - Provide e820 entries from the memory map info HOB > Reviewed-by: Andy Shevchenko > Signed-off-by: Aiden Park > Reviewed-by: Bin Meng > --- > > Changes in v7: > * Use for_each macro instead of while_each > * Move for_each macro before its first use > > Changes in v6: > * Change dram.c to sdram.c > * Use EFI_GUID > * Use a macro to do reversed search in memory map entries > * Apply code-review comments > > Changes in v3: > * Use HOB function from the common HOB library > * Add more description > > arch/x86/cpu/slimbootloader/Makefile | 2 +- > arch/x86/cpu/slimbootloader/sdram.c | 149 ++ > .../asm/arch-slimbootloader/slimbootloader.h | 53 +++ > 3 files changed, 203 insertions(+), 1 deletion(-) > create mode 100644 arch/x86/cpu/slimbootloader/sdram.c > create mode 100644 arch/x86/include/asm/arch-slimbootloader/slimbootloader.h > > diff --git a/arch/x86/cpu/slimbootloader/Makefile > b/arch/x86/cpu/slimbootloader/Makefile > index 627a721e8c..9e6822a6ec 100644 > --- a/arch/x86/cpu/slimbootloader/Makefile > +++ b/arch/x86/cpu/slimbootloader/Makefile > @@ -2,4 +2,4 @@ > # > # Copyright (C) 2019 Intel Corporation > > -obj-y += car.o slimbootloader.o > +obj-y += car.o slimbootloader.o sdram.o > diff --git a/arch/x86/cpu/slimbootloader/sdram.c > b/arch/x86/cpu/slimbootloader/sdram.c > new file mode 100644 > index 00..16a3ec6d9e > --- /dev/null > +++ b/arch/x86/cpu/slimbootloader/sdram.c > @@ -0,0 +1,149 @@ > +// SPDX-License-Identifier: GPL-2.0+ > +/* > + * Copyright (C) 2019 Intel Corporation > + */ > + > +#include > +#include > +#include > +#include > + > +DECLARE_GLOBAL_DATA_PTR; > + > +/** > + * This returns a data pointer of memory map info from the guid hob. > + * > + * @return: A data pointer of memory map info hob > + */ > +static struct sbl_memory_map_info *get_memory_map_info(void) > +{ > + struct sbl_memory_map_info *data; > + const efi_guid_t guid = SBL_MEMORY_MAP_INFO_GUID; > + > + if (!gd->arch.hob_list) > + return NULL; > + > + data = hob_get_guid_hob_data(gd->arch.hob_list, NULL, ); > + if (!data) > + panic("memory map info hob not found\n"); > + if (!data->count) > + panic("invalid number of memory map entries\n"); > + > + return data; > +} > + > +#define for_each_memory_map_entry_reversed(iter, entries) \ > + for (iter = entries->count - 1; iter >= 0; iter--) \ > + if (entries->entry[iter].type != E820_RAM) {} else > + > +/** > + * This is to give usable memory region information for u-boot relocation. > + * so search usable memory region lower than 4GB. > + * The memory map entries from Slim Bootloader hob are already sorted. > + * > + * @total_size: The memory size that u-boot occupies > + * @return: The top available memory address lower than 4GB > + */ > +ulong board_get_usable_ram_top(ulong total_size) > +{ > + struct sbl_memory_map_info *data; > + int i; > + u64 addr_start; > + u64 addr_end; > + ulong ram_top; > + > + data = get_memory_map_info(); > + > + /** > +* sorted memory map entries from Slim Bootloader based on physical > +* start memory address, from low to high. So do reversed search to > +* get highest usable, suitable size, 4KB aligned available memory > +* under 4GB. > +*/ > + ram_top = 0; > + for_each_memory_map_entry_reversed(i, data) { > + addr_start = data->entry[i].addr; > + addr_end = addr_start + data->entry[i].size; > + > + if (addr_start > SZ_4G) > + continue; > + > + if (addr_end > SZ_4G) > + addr_end = SZ_4G; > + > + if (addr_end < total_size) > + continue; > + > + /* to relocate u-boot at 4K aligned memory */ > + addr_end = rounddown(addr_end - total_size, SZ_4K); > + if (addr_end >= addr_start) { > + ram_top = (ulong)addr_end + total_size; > + break; > + } > + } > + > + if (!ram_top) > + panic("failed to find available memory for relocation!"); > + > + return ram_top; > +} > + > +/** > + * The memory initialization has already been done in previous Slim > Bootloader > + * stage thru FSP-M. Instead, this sets the ram_size from the memory map info > + * hob. > + */ > +int dram_init(void) > +{ > + struct sbl_memory_map_info *data; > + int i; > + u64 ram_size; > + > +
[U-Boot] [PATCH v7 4/9] x86: slimbootloader: Add memory configuration
Slim Bootloader provides memory map info thru its HOB list pointer. Configure memory size and relocation memory from the HOB data, and provide e820 entries as well. - Get memory size from the memory map info HOB - Set available top memory lower than 4GB for U-Boot relocation - Provide e820 entries from the memory map info HOB Signed-off-by: Aiden Park Reviewed-by: Bin Meng --- Changes in v7: * Use for_each macro instead of while_each * Move for_each macro before its first use Changes in v6: * Change dram.c to sdram.c * Use EFI_GUID * Use a macro to do reversed search in memory map entries * Apply code-review comments Changes in v3: * Use HOB function from the common HOB library * Add more description arch/x86/cpu/slimbootloader/Makefile | 2 +- arch/x86/cpu/slimbootloader/sdram.c | 149 ++ .../asm/arch-slimbootloader/slimbootloader.h | 53 +++ 3 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 arch/x86/cpu/slimbootloader/sdram.c create mode 100644 arch/x86/include/asm/arch-slimbootloader/slimbootloader.h diff --git a/arch/x86/cpu/slimbootloader/Makefile b/arch/x86/cpu/slimbootloader/Makefile index 627a721e8c..9e6822a6ec 100644 --- a/arch/x86/cpu/slimbootloader/Makefile +++ b/arch/x86/cpu/slimbootloader/Makefile @@ -2,4 +2,4 @@ # # Copyright (C) 2019 Intel Corporation -obj-y += car.o slimbootloader.o +obj-y += car.o slimbootloader.o sdram.o diff --git a/arch/x86/cpu/slimbootloader/sdram.c b/arch/x86/cpu/slimbootloader/sdram.c new file mode 100644 index 00..16a3ec6d9e --- /dev/null +++ b/arch/x86/cpu/slimbootloader/sdram.c @@ -0,0 +1,149 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (C) 2019 Intel Corporation + */ + +#include +#include +#include +#include + +DECLARE_GLOBAL_DATA_PTR; + +/** + * This returns a data pointer of memory map info from the guid hob. + * + * @return: A data pointer of memory map info hob + */ +static struct sbl_memory_map_info *get_memory_map_info(void) +{ + struct sbl_memory_map_info *data; + const efi_guid_t guid = SBL_MEMORY_MAP_INFO_GUID; + + if (!gd->arch.hob_list) + return NULL; + + data = hob_get_guid_hob_data(gd->arch.hob_list, NULL, ); + if (!data) + panic("memory map info hob not found\n"); + if (!data->count) + panic("invalid number of memory map entries\n"); + + return data; +} + +#define for_each_memory_map_entry_reversed(iter, entries) \ + for (iter = entries->count - 1; iter >= 0; iter--) \ + if (entries->entry[iter].type != E820_RAM) {} else + +/** + * This is to give usable memory region information for u-boot relocation. + * so search usable memory region lower than 4GB. + * The memory map entries from Slim Bootloader hob are already sorted. + * + * @total_size: The memory size that u-boot occupies + * @return: The top available memory address lower than 4GB + */ +ulong board_get_usable_ram_top(ulong total_size) +{ + struct sbl_memory_map_info *data; + int i; + u64 addr_start; + u64 addr_end; + ulong ram_top; + + data = get_memory_map_info(); + + /** +* sorted memory map entries from Slim Bootloader based on physical +* start memory address, from low to high. So do reversed search to +* get highest usable, suitable size, 4KB aligned available memory +* under 4GB. +*/ + ram_top = 0; + for_each_memory_map_entry_reversed(i, data) { + addr_start = data->entry[i].addr; + addr_end = addr_start + data->entry[i].size; + + if (addr_start > SZ_4G) + continue; + + if (addr_end > SZ_4G) + addr_end = SZ_4G; + + if (addr_end < total_size) + continue; + + /* to relocate u-boot at 4K aligned memory */ + addr_end = rounddown(addr_end - total_size, SZ_4K); + if (addr_end >= addr_start) { + ram_top = (ulong)addr_end + total_size; + break; + } + } + + if (!ram_top) + panic("failed to find available memory for relocation!"); + + return ram_top; +} + +/** + * The memory initialization has already been done in previous Slim Bootloader + * stage thru FSP-M. Instead, this sets the ram_size from the memory map info + * hob. + */ +int dram_init(void) +{ + struct sbl_memory_map_info *data; + int i; + u64 ram_size; + + data = get_memory_map_info(); + + /** +* sorted memory map entries from Slim Bootloader based on physical +* start memory address, from low to high. So do reversed search to +* simply get highest usable memory address as RAM size +*/ + ram_size = 0; + for_each_memory_map_entry_reversed(i, data) { + /* simply use