From bae54f764570f3fa5c592f313b45352d9f6f1d8a Mon Sep 17 00:00:00 2001 From: Anup Patel Date: Mon, 29 Apr 2019 11:55:19 +0530 Subject: [PATCH] firmware: Add fw_dynamic firmware This patch provides first-cut implementation of fw_dynamic firmware. As compared to fw_jump and fw_payload, the fw_dynamic obtains next address, next mode and OpenSBI options from struct fw_dynamic_info. The previous booting stage can create struct fw_dynamic_info in memory and pass address of struct fw_dynamic_info in 'a2' register. Also, the struct fw_dynamic_info has versioning as well so changes to the struct fw_dynamic_info can be done in a backward compatible manner. Signed-off-by: Anup Patel Reviewed-by: Atish Patra --- firmware/fw_dynamic.S | 118 ++++++++++++++++++++++++++++++++++++++++++++ firmware/fw_dynamic.elf.ldS | 16 ++++++ firmware/objects.mk | 2 + include/sbi/fw_dynamic.h | 61 +++++++++++++++++++++++ 4 files changed, 197 insertions(+) create mode 100644 firmware/fw_dynamic.S create mode 100644 firmware/fw_dynamic.elf.ldS create mode 100644 include/sbi/fw_dynamic.h diff --git a/firmware/fw_dynamic.S b/firmware/fw_dynamic.S new file mode 100644 index 0000000..cf97b60 --- /dev/null +++ b/firmware/fw_dynamic.S @@ -0,0 +1,118 @@ +/* + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2019 Western Digital Corporation or its affiliates. + * + * Authors: + * Anup Patel + */ + +#include + +#include "fw_base.S" + + .align 3 + .section .entry, "ax", %progbits +_bad_dynamic_info: + wfi + j _bad_dynamic_info + + .align 3 + .section .entry, "ax", %progbits + .global fw_save_info + /* + * We can only use a0, a1, a2, a3, and a4 registers here. + * The a0, a1, and a2 registers will be same as passed by + * previous booting stage. + * Nothing to be returned here. + */ +fw_save_info: + la a4, _dynamic_next_arg1 + REG_S a1, (a4) + li a4, FW_DYNAMIC_INFO_MAGIC_VALUE + REG_L a3, FW_DYNAMIC_INFO_MAGIC_OFFSET(a2) + bne a3, a4, _bad_dynamic_info + li a4, FW_DYNAMIC_INFO_VERSION_MAX + REG_L a3, FW_DYNAMIC_INFO_VERSION_OFFSET(a2) + bgt a3, a4, _bad_dynamic_info + la a4, _dynamic_next_addr + REG_L a3, FW_DYNAMIC_INFO_NEXT_ADDR_OFFSET(a2) + REG_S a3, (a4) + la a4, _dynamic_next_mode + REG_L a3, FW_DYNAMIC_INFO_NEXT_MODE_OFFSET(a2) + REG_S a3, (a4) + la a4, _dynamic_options + REG_L a3, FW_DYNAMIC_INFO_OPTIONS_OFFSET(a2) + REG_S a3, (a4) + ret + + .align 3 + .section .entry, "ax", %progbits + .global fw_prev_arg1 + /* + * We can only use a0, a1, and a2 registers here. + * The previous arg1 should be returned in 'a0'. + */ +fw_prev_arg1: + add a0, zero, zero + ret + + .align 3 + .section .entry, "ax", %progbits + .global fw_next_arg1 + /* + * We can only use a0, a1, and a2 registers here. + * The next arg1 should be returned in 'a0'. + */ +fw_next_arg1: + la a0, _dynamic_next_arg1 + REG_L a0, (a0) + ret + + .align 3 + .section .entry, "ax", %progbits + .global fw_next_addr + /* + * We can only use a0, a1, and a2 registers here. + * The next address should be returned in 'a0'. + */ +fw_next_addr: + la a0, _dynamic_next_addr + REG_L a0, (a0) + ret + + .align 3 + .section .entry, "ax", %progbits + .global fw_next_mode + /* + * We can only use a0, a1, and a2 registers here. + * The next address should be returned in 'a0' + */ +fw_next_mode: + la a0, _dynamic_next_mode + REG_L a0, (a0) + ret + + .align 3 + .section .entry, "ax", %progbits + .global fw_options + /* + * We can only use a0, a1, and a2 registers here. + * The 'a4' register will have default options. + * The next address should be returned in 'a0'. + */ +fw_options: + la a0, _dynamic_options + REG_L a0, (a0) + ret + + .align 3 + .section .entry, "ax", %progbits +_dynamic_next_arg1: + RISCV_PTR 0x0 +_dynamic_next_addr: + RISCV_PTR 0x0 +_dynamic_next_mode: + RISCV_PTR PRV_S +_dynamic_options: + RISCV_PTR 0x0 diff --git a/firmware/fw_dynamic.elf.ldS b/firmware/fw_dynamic.elf.ldS new file mode 100644 index 0000000..7ee0787 --- /dev/null +++ b/firmware/fw_dynamic.elf.ldS @@ -0,0 +1,16 @@ +/* + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2019 Western Digital Corporation or its affiliates. + * + * Authors: + * Anup Patel + */ + +OUTPUT_ARCH(riscv) +ENTRY(_start) + +SECTIONS +{ + #include "fw_base.ldS" +} diff --git a/firmware/objects.mk b/firmware/objects.mk index cc38283..e8969e6 100644 --- a/firmware/objects.mk +++ b/firmware/objects.mk @@ -17,6 +17,8 @@ ifdef FW_TEXT_START firmware-genflags-y += -DFW_TEXT_START=$(FW_TEXT_START) endif +firmware-bins-$(FW_DYNAMIC) += fw_dynamic.bin + firmware-bins-$(FW_JUMP) += fw_jump.bin ifdef FW_JUMP_ADDR firmware-genflags-$(FW_JUMP) += -DFW_JUMP_ADDR=$(FW_JUMP_ADDR) diff --git a/include/sbi/fw_dynamic.h b/include/sbi/fw_dynamic.h new file mode 100644 index 0000000..695dc29 --- /dev/null +++ b/include/sbi/fw_dynamic.h @@ -0,0 +1,61 @@ +/* + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2019 Western Digital Corporation or its affiliates. + * + * Authors: + * Anup Patel + */ + +#ifndef __FW_DYNAMIC_H__ +#define __FW_DYNAMIC_H__ + +#include + +/* clang-format off */ + +/** Offset of magic member in fw_dynamic_info */ +#define FW_DYNAMIC_INFO_MAGIC_OFFSET (0 * __SIZEOF_POINTER__) +/** Offset of version member in fw_dynamic_info */ +#define FW_DYNAMIC_INFO_VERSION_OFFSET (1 * __SIZEOF_POINTER__) +/** Offset of next_addr member in fw_dynamic_info */ +#define FW_DYNAMIC_INFO_NEXT_ADDR_OFFSET (2 * __SIZEOF_POINTER__) +/** Offset of next_mode member in fw_dynamic_info */ +#define FW_DYNAMIC_INFO_NEXT_MODE_OFFSET (3 * __SIZEOF_POINTER__) +/** Offset of options member in fw_dynamic_info */ +#define FW_DYNAMIC_INFO_OPTIONS_OFFSET (4 * __SIZEOF_POINTER__) + +/** Expected value of info magic ('OSBI' ascii string in hex) */ +#define FW_DYNAMIC_INFO_MAGIC_VALUE 0x4942534f + +/** Maximum supported info version */ +#define FW_DYNAMIC_INFO_VERSION_MAX 0x1 + +/** Possible next mode values */ +#define FW_DYNAMIC_INFO_NEXT_MODE_U 0x0 +#define FW_DYNAMIC_INFO_NEXT_MODE_S 0x1 +#define FW_DYNAMIC_INFO_NEXT_MODE_M 0x3 + +/* clang-format on */ + +#ifndef __ASSEMBLY__ + +#include + +/** Representation dynamic info passed by previous booting stage */ +struct fw_dynamic_info { + /** Info magic */ + unsigned long magic; + /** Info version */ + unsigned long version; + /** Next booting stage address */ + unsigned long next_addr; + /** Next booting stage mode */ + unsigned long next_mode; + /** Options for OpenSBI library */ + unsigned long options; +} __packed; + +#endif + +#endif -- 2.7.4