From a1d57b7aba85abc787675952b40c550bf3a595f8 Mon Sep 17 00:00:00 2001 From: Graeme Russ Date: Fri, 23 Dec 2011 21:14:22 +1100 Subject: [PATCH] x86: Convert board_init_f_r to a processing loop Create an init function array for board_init_f_r - This finalises the migration to a purely array based initialisation mechanism Also tweak a few comments while we are at it so everything is 'correct' -- Changes for v2: - Renamed to a more apt name - Fix bug in set_reloc_flag_r - Re-instate gd->flags = boot_flags; in board_init_f - Added commit message --- arch/x86/include/asm/init_helpers.h | 5 + arch/x86/include/asm/relocate.h | 33 +++++ arch/x86/lib/board.c | 186 +++++++++++++--------------- arch/x86/lib/init_helpers.c | 75 +++++++++++ arch/x86/lib/relocate.c | 32 +---- 5 files changed, 206 insertions(+), 125 deletions(-) create mode 100644 arch/x86/include/asm/relocate.h diff --git a/arch/x86/include/asm/init_helpers.h b/arch/x86/include/asm/init_helpers.h index 14ef11a4f9..192f18e1d5 100644 --- a/arch/x86/include/asm/init_helpers.h +++ b/arch/x86/include/asm/init_helpers.h @@ -27,7 +27,12 @@ int display_banner(void); int display_dram_config(void); int init_baudrate_f(void); +int calculate_relocation_address(void); +int copy_gd_to_ram_f_r(void); +int init_cache_f_r(void); + +int set_reloc_flag_r(void); int mem_malloc_init_r(void); int init_bd_struct_r(void); int flash_init_r(void); diff --git a/arch/x86/include/asm/relocate.h b/arch/x86/include/asm/relocate.h new file mode 100644 index 0000000000..33129ef64b --- /dev/null +++ b/arch/x86/include/asm/relocate.h @@ -0,0 +1,33 @@ +/* + * (C) Copyright 2011 + * Graeme Russ, + * + * See file CREDITS for list of people who contributed to this + * project. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + */ + +#ifndef _RELOCATE_H_ +#define _RELOCATE_H_ + +#include + +int copy_uboot_to_ram(void); +int clear_bss(void); +int do_elf_reloc_fixups(void); + +#endif /* !_RELOCATE_H_ */ diff --git a/arch/x86/lib/board.c b/arch/x86/lib/board.c index 89721c7c00..5f0b62cea6 100644 --- a/arch/x86/lib/board.c +++ b/arch/x86/lib/board.c @@ -35,7 +35,7 @@ #include #include #include -#include +#include #include #include @@ -43,31 +43,58 @@ /* * Breath some life into the board... * - * Initialize an SMC for serial comms, and carry out some hardware - * tests. + * Getting the board up and running is a three-stage process: + * 1) Execute from Flash, SDRAM Uninitialised + * At this point, there is a limited amount of non-SDRAM memory + * (typically the CPU cache, but can also be SRAM or even a buffer of + * of some peripheral). This limited memory is used to hold: + * - The initial copy of the Global Data Structure + * - A temporary stack + * - A temporary x86 Global Descriptor Table + * - The pre-console buffer (if enabled) * - * The first part of initialization is running from Flash memory; - * its main purpose is to initialize the RAM so that we - * can relocate the monitor code to RAM. + * The following is performed during this phase of execution: + * - Core low-level CPU initialisation + * - Console initialisation + * - SDRAM initialisation + * + * 2) Execute from Flash, SDRAM Initialised + * At this point we copy Global Data from the initial non-SDRAM + * memory and set up the permanent stack in SDRAM. The CPU cache is no + * longer being used as temporary memory, so we can now fully enable + * it. + * + * The following is performed during this phase of execution: + * - Create final stack in SDRAM + * - Copy Global Data from temporary memory to SDRAM + * - Enabling of CPU cache(s), + * - Copying of U-Boot code and data from Flash to RAM + * - Clearing of the BSS + * - ELF relocation adjustments + * + * 3) Execute from SDRAM + * The following is performed during this phase of execution: + * - All remaining initialisation */ /* - * All attempts to come up with a "common" initialization sequence - * that works for all boards and architectures failed: some of the - * requirements are just _too_ different. To get rid of the resulting - * mess of board dependend #ifdef'ed code we now make the whole - * initialization sequence configurable to the user. - * - * The requirements for any new initalization function is simple: it - * receives a pointer to the "global data" structure as it's only - * argument, and returns an integer return code, where 0 means - * "continue" and != 0 means "fatal error, hang the system". + * The requirements for any new initalization function is simple: it is + * a function with no parameters which returns an integer return code, + * where 0 means "continue" and != 0 means "fatal error, hang the system" */ typedef int (init_fnc_t) (void); -static int calculate_relocation_address(void); -static int copy_gd_to_ram(void); - +/* + * init_sequence_f is the list of init functions which are run when U-Boot + * is executing from Flash with a limited 'C' environment. The following + * limitations must be considered when implementing an '_f' function: + * - 'static' variables are read-only + * - Global Data (gd->xxx) is read/write + * - Stack space is limited + * + * The '_f' sequence must, as a minimum, initialise SDRAM. It _should_ + * also initialise the console (to provide early debug output) + */ init_fnc_t *init_sequence_f[] = { cpu_init_f, board_early_init_f, @@ -81,7 +108,39 @@ init_fnc_t *init_sequence_f[] = { NULL, }; +/* + * init_sequence_f_r is the list of init functions which are run when + * U-Boot is executing from Flash with a semi-limited 'C' environment. + * The following limitations must be considered when implementing an + * '_f_r' function: + * - 'static' variables are read-only + * - Global Data (gd->xxx) is read/write + * + * The '_f_r' sequence must, as a minimum, copy U-Boot to RAM (if + * supported). It _should_, if possible, copy global data to RAM and + * initialise the CPU caches (to speed up the relocation process) + */ +init_fnc_t *init_sequence_f_r[] = { + copy_gd_to_ram_f_r, + init_cache_f_r, + copy_uboot_to_ram, + clear_bss, + do_elf_reloc_fixups, + + NULL, +}; + +/* + * init_sequence_r is the list of init functions which are run when U-Boot + * is executing from RAM with a full 'C' environment. There are no longer + * any limitations which must be considered when implementing an '_r' + * function, (i.e.'static' variables are read/write) + * + * If not already done, the '_r' sequence must copy global data to RAM and + * (should) initialise the CPU caches. + */ init_fnc_t *init_sequence_r[] = { + set_reloc_flag_r, init_bd_struct_r, mem_malloc_init_r, cpu_init_r, @@ -157,43 +216,6 @@ static void do_init_loop(init_fnc_t **init_fnc_ptr) } } -static int calculate_relocation_address(void) -{ - ulong text_start = (ulong)&__text_start; - ulong bss_end = (ulong)&__bss_end; - ulong dest_addr; - - /* - * NOTE: All destination address are rounded down to 16-byte - * boundary to satisfy various worst-case alignment - * requirements - */ - - /* Global Data is at top of available memory */ - dest_addr = gd->ram_size; - dest_addr -= GENERATED_GBL_DATA_SIZE; - dest_addr &= ~15; - gd->new_gd_addr = dest_addr; - - /* GDT is below Global Data */ - dest_addr -= X86_GDT_SIZE; - dest_addr &= ~15; - gd->gdt_addr = dest_addr; - - /* Stack is below GDT */ - gd->start_addr_sp = dest_addr; - - /* U-Boot is below the stack */ - dest_addr -= CONFIG_SYS_STACK_SIZE; - dest_addr -= (bss_end - text_start); - dest_addr &= ~15; - gd->relocaddr = dest_addr; - gd->reloc_off = (dest_addr - text_start); - - return 0; -} - -/* Perform all steps necessary to get RAM initialised ready for relocation */ void board_init_f(ulong boot_flags) { gd->flags = boot_flags; @@ -201,10 +223,9 @@ void board_init_f(ulong boot_flags) do_init_loop(init_sequence_f); /* - * SDRAM is now initialised, U-Boot has been copied into SDRAM, - * the BSS has been cleared etc. The final stack can now be setup - * in SDRAM. Code execution will continue (momentarily) in Flash, - * but with the stack in SDRAM and Global Data in temporary memory + * SDRAM and console are now initialised. The final stack can now + * be setup in SDRAM. Code execution will continue in Flash, but + * with the stack in SDRAM and Global Data in temporary memory * (CPU cache) */ board_init_f_r_trampoline(gd->start_addr_sp); @@ -216,51 +237,22 @@ void board_init_f(ulong boot_flags) void board_init_f_r(void) { - if (copy_gd_to_ram() != 0) - hang(); - - if (init_cache() != 0) - hang(); - - relocate_code(0, gd, 0); - - /* NOTREACHED - relocate_code() does not return */ - while (1) - ; -} - -static int copy_gd_to_ram(void) -{ - gd_t *ram_gd; + do_init_loop(init_sequence_f_r); /* - * Global data is still in temporary memory (the CPU cache). - * calculate_relocation_address() has set gd->new_gd_addr to - * where the global data lives in RAM but getting it there - * safely is a bit tricky due to the 'F-Segment Hack' that - * we need to use for x86 + * U-Boot has been copied into SDRAM, the BSS has been cleared etc. + * Transfer execution from Flash to RAM by calculating the address + * of the in-RAM copy of board_init_r() and calling it */ - ram_gd = (gd_t *)gd->new_gd_addr; - memcpy((void *)ram_gd, gd, sizeof(gd_t)); + (board_init_r + gd->reloc_off)(gd, gd->relocaddr); - /* - * Reload the Global Descriptor Table so FS points to the - * in-RAM copy of Global Data (calculate_relocation_address() - * has already calculated the in-RAM location of the GDT) - */ - ram_gd->gd_addr = (ulong)ram_gd; - init_gd(ram_gd, (u64 *)gd->gdt_addr); - - return 0; + /* NOTREACHED - board_init_r() does not return */ + while (1) + ; } void board_init_r(gd_t *id, ulong dest_addr) { - gd->flags |= GD_FLG_RELOC; - - /* compiler optimization barrier needed for GCC >= 3.4 */ - __asm__ __volatile__("" : : : "memory"); - do_init_loop(init_sequence_r); /* main_loop() can return to retry autoboot, if so just run it again. */ diff --git a/arch/x86/lib/init_helpers.c b/arch/x86/lib/init_helpers.c index 547b180592..9f4dee034f 100644 --- a/arch/x86/lib/init_helpers.c +++ b/arch/x86/lib/init_helpers.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -70,6 +71,80 @@ int init_baudrate_f(void) return 0; } +int calculate_relocation_address(void) +{ + ulong text_start = (ulong)&__text_start; + ulong bss_end = (ulong)&__bss_end; + ulong dest_addr; + + /* + * NOTE: All destination address are rounded down to 16-byte + * boundary to satisfy various worst-case alignment + * requirements + */ + + /* Global Data is at top of available memory */ + dest_addr = gd->ram_size; + dest_addr -= GENERATED_GBL_DATA_SIZE; + dest_addr &= ~15; + gd->new_gd_addr = dest_addr; + + /* GDT is below Global Data */ + dest_addr -= X86_GDT_SIZE; + dest_addr &= ~15; + gd->gdt_addr = dest_addr; + + /* Stack is below GDT */ + gd->start_addr_sp = dest_addr; + + /* U-Boot is below the stack */ + dest_addr -= CONFIG_SYS_STACK_SIZE; + dest_addr -= (bss_end - text_start); + dest_addr &= ~15; + gd->relocaddr = dest_addr; + gd->reloc_off = (dest_addr - text_start); + + return 0; +} + +int copy_gd_to_ram_f_r(void) +{ + gd_t *ram_gd; + + /* + * Global data is still in temporary memory (the CPU cache). + * calculate_relocation_address() has set gd->new_gd_addr to + * where the global data lives in RAM but getting it there + * safely is a bit tricky due to the 'F-Segment Hack' that + * we need to use for x86 + */ + ram_gd = (gd_t *)gd->new_gd_addr; + memcpy((void *)ram_gd, gd, sizeof(gd_t)); + + /* + * Reload the Global Descriptor Table so FS points to the + * in-RAM copy of Global Data (calculate_relocation_address() + * has already calculated the in-RAM location of the GDT) + */ + ram_gd->gd_addr = (ulong)ram_gd; + init_gd(ram_gd, (u64 *)gd->gdt_addr); + + return 0; +} + +int init_cache_f_r(void) +{ + /* Initialise the CPU cache(s) */ + return init_cache(); +} + +int set_reloc_flag_r(void) +{ + gd->flags = GD_FLG_RELOC; + + return 0; +} + int mem_malloc_init_r(void) { mem_malloc_init(((gd->relocaddr - CONFIG_SYS_MALLOC_LEN)+3)&~3, diff --git a/arch/x86/lib/relocate.c b/arch/x86/lib/relocate.c index badb5f84f1..200baaba6a 100644 --- a/arch/x86/lib/relocate.c +++ b/arch/x86/lib/relocate.c @@ -34,13 +34,10 @@ #include #include #include +#include #include -static int copy_uboot_to_ram(void); -static int clear_bss(void); -static int do_elf_reloc_fixups(void); - -static int copy_uboot_to_ram(void) +int copy_uboot_to_ram(void) { size_t len = (size_t)&__data_end - (size_t)&__text_start; @@ -49,7 +46,7 @@ static int copy_uboot_to_ram(void) return 0; } -static int clear_bss(void) +int clear_bss(void) { ulong dst_addr = (ulong)&__bss_start + gd->reloc_off; size_t len = (size_t)&__bss_end - (size_t)&__bss_start; @@ -59,7 +56,7 @@ static int clear_bss(void) return 0; } -static int do_elf_reloc_fixups(void) +int do_elf_reloc_fixups(void) { Elf32_Rel *re_src = (Elf32_Rel *)(&__rel_dyn_start); Elf32_Rel *re_end = (Elf32_Rel *)(&__rel_dyn_end); @@ -92,24 +89,3 @@ static int do_elf_reloc_fixups(void) return 0; } - -void relocate_code(ulong dummy_1, gd_t *id, ulong dummy_2) -{ - /* - * Copy U-Boot into RAM, clear the BSS and perform relocation - * adjustments - */ - copy_uboot_to_ram(); - clear_bss(); - do_elf_reloc_fixups(); - - /* - * Transfer execution from Flash to RAM by calculating the address - * of the in-RAM copy of board_init_r() and calling it - */ - (board_init_r + gd->reloc_off)(gd, gd->relocaddr); - - /* NOTREACHED - board_init_r() does not return */ - while (1) - ; -} -- 2.39.5