riscv: introduce alternative mechanism to apply errata patches
Subsystems (2)
v3 cover riscv: introduce alternative mechanism to apply errata patches
With the emergence of more and more RISC-V CPUs, the request for how to upstream the vendor errata patch may gradually appear. In order to resolve this issue, this patch introduces the alternative mechanism from ARM64 and x86 to enable the kernel to patch code at runtime according to the manufacturer information of the running CPU. The main purpose of this patch set is to propose a framework to apply vendor's errata solutions. Based on this framework, it can be ensured that the errata only applies to the specified CPU cores. Other CPU cores do not be affected. Therefore, some complicated scenarios are unsupported in this patch set, such as patching code to the kernel module, doing relocation in patching code, and heterogeneous CPU topology.
In the "alternative" scheme, Users could use the macro ALTERNATIVE to apply an errata to the existing code flow. In the macro ALTERNATIVE, users need to specify the manufacturer information (vendor id, arch id, and implement id) for this errata. Therefore, kernel will know this errata is suitable for which CPU core. During the booting procedure, kernel will select the errata required by the CPU core and then patch it. It means that the kernel only applies the errata to the specified CPU core. In this case, the vendor's errata does not affect each other at runtime. The above patching procedure only occurs during the booting phase, so we only take the overhead of the "alternative" mechanism once.
This "alternative" mechanism is enabled by default to ensure that all required errata will be applied. However, users can disable this feature by the Kconfig "CONFIG_RISCV_ERRATA_ALTERNATIVE".
The last two patches are to apply the SiFive CIP-453 and CIP-1200 errata by this "alternative" scheme. Therefore, they can be regarded as examples. According to the results of running this image on the QEMU virt platform, kernel does not apply this errata at run-time because the CPU manufacturer information does not match the specified SiFive CPU core. Therefore, these errata does not affect any CPU core except for the specified SiFive cores.
Changes in v3 patch:
1. Remove "default y" setting of CONFIG_ERRATA_SIFIVE from Kconfig.erratas
2. Correct the affected CPU list of errata "cip-453"
Changes in v2 patch:
1. Display a warning message if Kernel finds the required errata is missing
2. Provide sample code for a vendor who wants to append its errata
3. Create a new patch to workaound SiFive errata cip-1200
Vincent Chen (5):
riscv: Add 3 SBI wrapper functions to get cpu manufacturer information
riscv: Introduce alternative mechanism to apply errata solution
riscv: sifive: Add SiFive alternative ports
riscv: sifive: Apply errata "cip-453" patch
riscv: sifive: Apply errata "cip-1200" patch
arch/riscv/Kconfig | 1 + arch/riscv/Kconfig.erratas | 44 +++++++++ arch/riscv/Kconfig.socs | 1 + arch/riscv/Makefile | 1 + arch/riscv/errata/Makefile | 2 + arch/riscv/errata/alternative.c | 74 +++++++++++++++ arch/riscv/errata/sifive/Makefile | 2 + arch/riscv/errata/sifive/errata.c | 106 +++++++++++++++++++++ arch/riscv/errata/sifive/errata_cip_453.S | 38 ++++++++ arch/riscv/include/asm/alternative-macros.h | 142 ++++++++++++++++++++++++++++ arch/riscv/include/asm/alternative.h | 39 ++++++++ arch/riscv/include/asm/asm.h | 1 + arch/riscv/include/asm/csr.h | 3 + arch/riscv/include/asm/errata_list.h | 39 ++++++++ arch/riscv/include/asm/sbi.h | 3 + arch/riscv/include/asm/sections.h | 1 + arch/riscv/include/asm/tlbflush.h | 3 +- arch/riscv/include/asm/vendorid_list.h | 10 ++ arch/riscv/kernel/entry.S | 6 +- arch/riscv/kernel/sbi.c | 15 +++ arch/riscv/kernel/smpboot.c | 4 + arch/riscv/kernel/vmlinux.lds.S | 7 ++ 22 files changed, 539 insertions(+), 3 deletions(-) create mode 100644 arch/riscv/Kconfig.erratas create mode 100644 arch/riscv/errata/Makefile create mode 100644 arch/riscv/errata/alternative.c create mode 100644 arch/riscv/errata/sifive/Makefile create mode 100644 arch/riscv/errata/sifive/errata.c create mode 100644 arch/riscv/errata/sifive/errata_cip_453.S create mode 100644 arch/riscv/include/asm/alternative-macros.h create mode 100644 arch/riscv/include/asm/alternative.h create mode 100644 arch/riscv/include/asm/errata_list.h create mode 100644 arch/riscv/include/asm/vendorid_list.h
Signature · 7 lines
2.7.4 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
PATCH v3 1/5 riscv: Add 3 SBI wrapper functions to get cpu manufacturer information
Add 3 wrapper functions to get vendor id, architecture id and implement id from M-mode
--- arch/riscv/include/asm/sbi.h | 3 +++ arch/riscv/kernel/sbi.c | 15 +++++++++++++++ 2 files changed, 18 insertions(+)
arch/riscv/include/asm/sbi.h +3 -0
@@ -97,6 +97,9 @@ struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,void sbi_console_putchar (int ch);int sbi_console_getchar (void ); +long sbi_get_mvendorid (void ); +long sbi_get_marchid (void ); +long sbi_get_mimpid (void );void sbi_set_timer (uint64_t stime_value);void sbi_shutdown (void );void sbi_clear_ipi (void );
arch/riscv/kernel/sbi.c +15 -0
@@ -547,6 +547,21 @@ static inline long sbi_get_firmware_version(void)return __sbi_base_ecall (SBI_EXT_BASE_GET_IMP_VERSION ); } +long sbi_get_mvendorid (void ) +{ +return __sbi_base_ecall (SBI_EXT_BASE_GET_MVENDORID ); +} + +long sbi_get_marchid (void ) +{ +return __sbi_base_ecall (SBI_EXT_BASE_GET_MARCHID ); +} + +long sbi_get_mimpid (void ) +{ +return __sbi_base_ecall (SBI_EXT_BASE_GET_MIMPID ); +} +static void sbi_send_cpumask_ipi (const struct cpumask *target) {struct cpumask hartid_mask;
Signature · 7 lines
2.7.4 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
PATCH v3 2/5 riscv: Introduce alternative mechanism to apply errata solution
In reply to Vincent ChenIntroduce the "alternative" mechanism from ARM64 and x86 to apply the CPU vendors' errata solution at runtime. The main purpose of this patch is to provide a framework. Therefore, the implementation is quite basic for now so that some scenarios could not use this schemei, such as patching code to a module, relocating the patching code and heterogeneous CPU topology.
Users could use the macro ALTERNATIVE to apply an errata to the existing code flow. In the macro ALTERNATIVE, users need to specify the manufacturer information(vendorid, archid, and impid) for this errata. Therefore, kernel will know this errata is suitable for which CPU core. During the booting procedure, kernel will select the errata required by the CPU core and then patch it. It means that the kernel only applies the errata to the specified CPU core. In this case, the vendor's errata does not affect each other at runtime. The above patching procedure only occurs during the booting phase, so we only take the overhead of the "alternative" mechanism once.
This "alternative" mechanism is enabled by default to ensure that all required errata will be applied. However, users can disable this feature by the Kconfig "CONFIG_RISCV_ERRATA_ALTERNATIVE".
--- arch/riscv/Kconfig | 1 + arch/riscv/Kconfig.erratas | 12 +++ arch/riscv/Makefile | 1 + arch/riscv/errata/Makefile | 1 + arch/riscv/errata/alternative.c | 69 ++++++++++++++ arch/riscv/include/asm/alternative-macros.h | 142 ++++++++++++++++++++++++++++ arch/riscv/include/asm/alternative.h | 36 +++++++ arch/riscv/include/asm/asm.h | 1 + arch/riscv/include/asm/csr.h | 3 + arch/riscv/include/asm/errata_list.h | 12 +++ arch/riscv/include/asm/sections.h | 1 + arch/riscv/include/asm/vendorid_list.h | 10 ++ arch/riscv/kernel/smpboot.c | 4 + arch/riscv/kernel/vmlinux.lds.S | 7 ++ 14 files changed, 300 insertions(+) create mode 100644 arch/riscv/Kconfig.erratas create mode 100644 arch/riscv/errata/Makefile create mode 100644 arch/riscv/errata/alternative.c create mode 100644 arch/riscv/include/asm/alternative-macros.h create mode 100644 arch/riscv/include/asm/alternative.h create mode 100644 arch/riscv/include/asm/errata_list.h create mode 100644 arch/riscv/include/asm/vendorid_list.h
arch/riscv/Kconfig +1 -0
@@ -204,6 +204,7 @@ config LOCKDEP_SUPPORT def_bool y source "arch/riscv/Kconfig.socs" +source "arch/riscv/Kconfig.erratas" menu "Platform type"
arch/riscv/Kconfig.erratas new file +12 -0
@@ -0,0 +1,12 @@ +menu "CPU errata selection" + +config RISCV_ERRATA_ALTERNATIVE + bool "RISC-V alternative scheme" + default y + help + This Kconfig allows the kernel to automatically patch the + errata required by the execution platform at run time. The + code patching is performed once in the boot stages. It means + that the overhead from this mechanism is just taken once. + +endmenu
arch/riscv/Makefile +1 -0
@@ -87,6 +87,7 @@ KBUILD_IMAGE := $(boot)/Image.gz head-y := arch/riscv/kernel/head.o core-y += arch/riscv/ +core-$(CONFIG_RISCV_ERRATA_ALTERNATIVE) += arch/riscv/errata/ libs-y += arch/riscv/lib/ libs-$(CONFIG_EFI_STUB) += $(objtree)/drivers/firmware/efi/libstub/lib.a
arch/riscv/errata/Makefile new file +1 -0
@@ -0,0 +1 @@ +obj-y += alternative.o
arch/riscv/errata/alternative.c new file +69 -0
69 lines not on this page · raw message
arch/riscv/include/asm/alternative-macros.h new file +142 -0
142 lines not on this page · raw message
arch/riscv/include/asm/alternative.h new file +36 -0
@@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2021 Sifive. +*/ + +#ifndef __ASM_ALTERNATIVE_H +#define __ASM_ALTERNATIVE_H + +#define ERRATA_STRING_LENGTH_MAX 32 + +#include <asm/alternative-macros.h> + +#ifndef __ASSEMBLY__ + +#include <linux/init.h> +#include <linux/types.h> +#include <linux/stddef.h> +#include <asm/hwcap.h> + +void __init apply_boot_alternatives (void ); + +struct alt_entry { +void *old_ptr;/* address of original instruciton or data */ +void *alt_ptr;/* address of replacement instruction or data */ +unsigned long vendor_id;/* cpu vendor id */ +unsigned long alt_len;/* The replacement size */ +unsigned int errata_id;/* The errata id */ +} __packed; + +struct errata_checkfunc_id { +unsigned long vendor_id; +bool (*func)(struct alt_entry *alt); +}; + +#endif +#endif
arch/riscv/include/asm/asm.h +1 -0
@@ -23,6 +23,7 @@#define REG_L __REG_SEL(ld, lw)#define REG_S __REG_SEL(sd, sw)#define REG_SC __REG_SEL(sc.d, sc.w) +#define REG_ASM __REG_SEL(.dword, .word)#define SZREG __REG_SEL(8, 4)#define LGREG __REG_SEL(3, 2)
arch/riscv/include/asm/csr.h +3 -0
@@ -115,6 +115,9 @@#define CSR_MIP 0x344#define CSR_PMPCFG0 0x3a0#define CSR_PMPADDR0 0x3b0 +#define CSR_MVENDORID 0xf11 +#define CSR_MARCHID 0xf12 +#define CSR_MIMPID 0xf13#define CSR_MHARTID 0xf14#ifdef CONFIG_RISCV_M_MODE
arch/riscv/include/asm/errata_list.h new file +12 -0
@@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2021 Sifive. +*/ +#ifndef ASM_ERRATA_LIST_H +#define ASM_ERRATA_LIST_H + +#ifdef CONFIG_ERRATA_SIFIVE +#define ERRATA_SIFIVE_NUMBER 0 +#endif + +#endif
arch/riscv/include/asm/sections.h +1 -0
@@ -11,5 +11,6 @@ extern char _start[];extern char _start_kernel[];extern char __init_data_begin[], __init_data_end[];extern char __init_text_begin[], __init_text_end[]; +extern char __alt_start[], __alt_end[];#endif /* __ASM_SECTIONS_H */
arch/riscv/include/asm/vendorid_list.h new file +10 -0
@@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2021 SiFive +*/ +#ifndef ASM_VENDOR_LIST_H +#define ASM_VENDOR_LIST_H + +#define SIFIVE_VENDOR_ID 0x489 + +#endif
arch/riscv/kernel/smpboot.c +4 -0
@@ -32,6 +32,7 @@#include <asm/sections.h> #include <asm/sbi.h> #include <asm/smp.h> +#include <asm/alternative.h> #include "head.h"
@@ -40,6 +41,9 @@ static DECLARE_COMPLETION(cpu_running);void __init smp_prepare_boot_cpu (void ) {init_cpu_topology (); +#ifdef CONFIG_RISCV_ERRATA_ALTERNATIVE +apply_boot_alternatives (); +#endif }void __init smp_prepare_cpus (unsigned int max_cpus)
arch/riscv/kernel/vmlinux.lds.S +7 -0
@@ -90,6 +90,13 @@ SECTIONS } __init_data_end = .; + + . = ALIGN(8); + .alternative : { + __alt_start = .; + *(.alternative) + __alt_end = .; + } __init_end = .; /* Start of data section */
Signature · 7 lines
2.7.4 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
PATCH v3 3/5 riscv: sifive: Add SiFive alternative ports
In reply to Vincent ChenAdd required ports of the Alternative scheme for SiFive.
--- arch/riscv/Kconfig.erratas | 10 ++++++ arch/riscv/Kconfig.socs | 1 + arch/riscv/errata/Makefile | 1 + arch/riscv/errata/alternative.c | 5 +++ arch/riscv/errata/sifive/Makefile | 1 + arch/riscv/errata/sifive/errata.c | 68 ++++++++++++++++++++++++++++++++++++ arch/riscv/include/asm/alternative.h | 3 ++ 7 files changed, 89 insertions(+) create mode 100644 arch/riscv/errata/sifive/Makefile create mode 100644 arch/riscv/errata/sifive/errata.c
arch/riscv/Kconfig.erratas +10 -0
@@ -9,4 +9,14 @@ config RISCV_ERRATA_ALTERNATIVE code patching is performed once in the boot stages. It means that the overhead from this mechanism is just taken once. +config ERRATA_SIFIVE + bool "SiFive errata" + depends on RISCV_ERRATA_ALTERNATIVE + help + All SiFive errata Kconfig depend on this Kconfig. Disabling + this Kconfig will disable all SiFive errata. Please say "Y" + here if your platform uses SiFive CPU cores. + + Otherwise, please say "N" here to avoid unnecessary overhead. + endmenu
arch/riscv/Kconfig.socs +1 -0
@@ -7,6 +7,7 @@ config SOC_SIFIVE select CLK_SIFIVE select CLK_SIFIVE_PRCI select SIFIVE_PLIC + select ERRATA_SIFIVE help This enables support for SiFive SoC platform hardware.
arch/riscv/errata/Makefile +1 -0
@@ -1 +1,2 @@ obj-y += alternative.o +obj-$(CONFIG_ERRATA_SIFIVE) += sifive/
arch/riscv/errata/alternative.c +5 -0
@@ -42,6 +42,11 @@ static void __init init_alternative(void)riscv_fill_cpu_mfr_info ();switch (cpu_mfr_info.vendor_id) { +#ifdef CONFIG_ERRATA_SIFIVE +case SIFIVE_VENDOR_ID : + vendor_patch_func = sifive_errata_patch_func; +break ; +#endif default : vendor_patch_func =NULL ; }
arch/riscv/errata/sifive/Makefile new file +1 -0
@@ -0,0 +1 @@ +obj-y += errata.o
arch/riscv/errata/sifive/errata.c new file +68 -0
68 lines not on this page · raw message
arch/riscv/include/asm/alternative.h +3 -0
@@ -32,5 +32,8 @@ struct errata_checkfunc_id {bool (*func)(struct alt_entry *alt); }; +void sifive_errata_patch_func (struct alt_entry *begin,struct alt_entry *end, +unsigned long archid,unsigned long impid); +#endif #endif
Signature · 7 lines
2.7.4 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
PATCH v3 4/5 riscv: sifive: Apply errata "cip-453" patch
In reply to Vincent ChenAdd sign extension to the $badaddr before addressing the instruction page fault and instruction access fault to workaround the issue "cip-453".
To avoid affecting the existing code sequence, this patch will creates two trampolines to add sign extension to the $badaddr. By the "alternative" mechanism, these two trampolines will replace the original exception handler of instruction page fault and instruction access fault in the excp_vect_table. In this case, only the specific SiFive CPU core jumps to the do_page_fault and do_trap_insn_fault through these two trampolines. Other CPUs are not affected.
--- arch/riscv/Kconfig.erratas | 11 +++++++++ arch/riscv/errata/sifive/Makefile | 1 + arch/riscv/errata/sifive/errata.c | 20 ++++++++++++++++ arch/riscv/errata/sifive/errata_cip_453.S | 38 +++++++++++++++++++++++++++++++ arch/riscv/include/asm/errata_list.h | 21 ++++++++++++++++- arch/riscv/kernel/entry.S | 6 +++-- 6 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 arch/riscv/errata/sifive/errata_cip_453.S
arch/riscv/Kconfig.erratas +11 -0
@@ -19,4 +19,15 @@ config ERRATA_SIFIVE Otherwise, please say "N" here to avoid unnecessary overhead. +config ERRATA_SIFIVE_CIP_453 + bool "Apply SiFive errata CIP-453" + depends on ERRATA_SIFIVE + default y + help + This will apply the SiFive CIP-453 errata to add sign extension + to the $badaddr when exception type is instruction page fault + and instruction access fault. + + If you don't know what to do here, say "Y". + endmenu
arch/riscv/errata/sifive/Makefile +1 -0
@@ -1 +1,2 @@ +obj-y += errata_cip_453.o obj-y += errata.o
arch/riscv/errata/sifive/errata.c +20 -0
@@ -16,6 +16,26 @@ struct errata_info_t {bool (*check_func)(unsigned long arch_id,unsigned long impid); }; +static bool errata_cip_453_check_func (unsigned long arch_id,unsigned long impid) +{ +/* +* Affected cores: +* Architecture ID: 0x8000000000000007 +* Implement ID: 0x20181004 <= impid <= 0x20191105 +*/ +if (arch_id !=0x8000000000000007 || + (impid <0x20181004 || impid >0x20191105 )) +return false; +return true; +} + +static struct errata_info_t errata_list[ERRATA_SIFIVE_NUMBER ] = { + { + .name ="cip-453" , + .check_func = errata_cip_453_check_func + }, +}; +static u32 __init sifive_errata_probe (unsigned long archid,unsigned long impid) {int idx;
arch/riscv/errata/sifive/errata_cip_453.S new file +38 -0
@@ -0,0 +1,38 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2021 SiFive + */ + +#include <linux/linkage.h> +#include <asm/asm.h> +#include <asm/asm-offsets.h> +#include <asm/alternative.h> + +.macro ADD_SIGN_EXT pt_reg badaddr tmp_reg + REG_L \badaddr, PT_BADADDR(\pt_reg) + li \tmp_reg,1 + slli \tmp_reg,\tmp_reg,0x26 + and \tmp_reg,\tmp_reg,\badaddr + beqz \tmp_reg, 1f + li \tmp_reg,-1 + slli \tmp_reg,\tmp_reg,0x27 + or \badaddr,\tmp_reg,\badaddr + REG_S \badaddr, PT_BADADDR(\pt_reg) +1: +.endm + +ENTRY(sifive_cip_453_page_fault_trp) + ADD_SIGN_EXT a0, t0, t1 +#ifdef CONFIG_MMU + la t0, do_page_fault +#else + la t0, do_trap_unknown +#endif + jr t0 +END(sifive_cip_453_page_fault_trp) + +ENTRY(sifive_cip_453_insn_fault_trp) + ADD_SIGN_EXT a0, t0, t1 + la t0, do_trap_insn_fault + jr t0 +END(sifive_cip_453_insn_fault_trp)
arch/riscv/include/asm/errata_list.h +20 -1
@@ -5,8 +5,27 @@#ifndef ASM_ERRATA_LIST_H #define ASM_ERRATA_LIST_H +#include <asm/alternative.h> +#include <asm/vendorid_list.h> +#ifdef CONFIG_ERRATA_SIFIVE -#define ERRATA_SIFIVE_NUMBER 0 +#define ERRATA_SIFIVE_CIP_453 0 +#define ERRATA_SIFIVE_NUMBER 1#endif +#ifdef __ASSEMBLY__ + +#define ALT_INSN_FAULT (x) \ +ALTERNATIVE(__stringify(RISCV_PTR do_trap_insn_fault), \ + __stringify(RISCV_PTR sifive_cip_453_insn_fault_trp), \ + SIFIVE_VENDOR_ID, ERRATA_SIFIVE_CIP_453, \ + CONFIG_ERRATA_SIFIVE_CIP_453) + +#define ALT_PAGE_FAULT (x) \ +ALTERNATIVE(__stringify(RISCV_PTR do_page_fault), \ + __stringify(RISCV_PTR sifive_cip_453_page_fault_trp), \ + SIFIVE_VENDOR_ID, ERRATA_SIFIVE_CIP_453, \ + CONFIG_ERRATA_SIFIVE_CIP_453) +#endif /* __ASSEMBLY__ */ +#endif
arch/riscv/kernel/entry.S +4 -2
@@ -12,6 +12,7 @@ #include <asm/unistd.h> #include <asm/thread_info.h> #include <asm/asm-offsets.h> +#include <asm/errata_list.h> #if !IS_ENABLED(CONFIG_PREEMPTION) .set resume_kernel, restore_all
@@ -450,7 +451,7 @@ ENDPROC(__switch_to) /* Exception vector table */ ENTRY(excp_vect_table) RISCV_PTR do_trap_insn_misaligned - RISCV_PTR do_trap_insn_fault + ALT_INSN_FAULT(RISCV_PTR do_trap_insn_fault) RISCV_PTR do_trap_insn_illegal RISCV_PTR do_trap_break RISCV_PTR do_trap_load_misaligned
@@ -461,7 +462,8 @@ ENTRY(excp_vect_table) RISCV_PTR do_trap_ecall_s RISCV_PTR do_trap_unknown RISCV_PTR do_trap_ecall_m - RISCV_PTR do_page_fault /* instruction page fault */ + /* instruciton page fault */ + ALT_PAGE_FAULT(RISCV_PTR do_page_fault) RISCV_PTR do_page_fault /* load page fault */ RISCV_PTR do_trap_unknown RISCV_PTR do_page_fault /* store page fault */
Signature · 7 lines
2.7.4 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
PATCH v3 5/5 riscv: sifive: Apply errata "cip-1200" patch
In reply to Vincent ChenFor certain SiFive CPUs, "sfence.vma addr" cannot exactly flush addr from TLB in the particular cases. The details could be found here:
https://sifive.cdn.prismic.io/sifive/167a1a56-03f4-4615-a79e-b2a86153148f_FU740_errata_20210205.pdf
In order to ensure the functionality, this patch uses the Alternative scheme to replace all "sfence.vma addr" with "sfence.vma" at runtime.
--- arch/riscv/Kconfig.erratas | 11 +++++++++++ arch/riscv/errata/sifive/errata.c | 18 ++++++++++++++++++ arch/riscv/include/asm/errata_list.h | 10 +++++++++- arch/riscv/include/asm/tlbflush.h | 3 ++- 4 files changed, 40 insertions(+), 2 deletions(-)
arch/riscv/Kconfig.erratas +11 -0
@@ -30,4 +30,15 @@ config ERRATA_SIFIVE_CIP_453 If you don't know what to do here, say "Y". +config ERRATA_SIFIVE_CIP_1200 + bool "Apply SiFive errata CIP-1200" + depends on ERRATA_SIFIVE + default y + help + This will apply the SiFive CIP-1200 errata to repalce all + "sfence.vma addr" with "sfence.vma" to ensure that the addr + has been flushed from TLB. + + If you don't know what to do here, say "Y". + endmenu
arch/riscv/errata/sifive/errata.c +18 -0
@@ -29,11 +29,29 @@ static bool errata_cip_453_check_func(unsigned long arch_id, unsigned long impireturn true; } +static bool errata_cip_1200_check_func (unsigned long arch_id,unsigned long impid) +{ +/* +* Affected cores: +* Architecture ID: 0x8000000000000007 or 0x1 +* Implement ID: mimpid[23:0] <= 0x200630 and mimpid != 0x01200626 +*/ +if (arch_id !=0x8000000000000007 && arch_id !=0x1 ) +return false; +if ((impid &0xffffff ) >0x200630 || impid ==0x1200626 ) +return false; +return true; +} +static struct errata_info_t errata_list[ERRATA_SIFIVE_NUMBER ] = { { .name ="cip-453" , .check_func = errata_cip_453_check_func }, + { + .name ="cip-1200" , + .check_func = errata_cip_1200_check_func + }, };static u32 __init sifive_errata_probe (unsigned long archid,unsigned long impid)
arch/riscv/include/asm/errata_list.h +9 -1
@@ -10,7 +10,8 @@#ifdef CONFIG_ERRATA_SIFIVE #define ERRATA_SIFIVE_CIP_453 0 -#define ERRATA_SIFIVE_NUMBER 1 +#define ERRATA_SIFIVE_CIP_1200 1 +#define ERRATA_SIFIVE_NUMBER 2#endif #ifdef __ASSEMBLY__
@@ -26,6 +27,13 @@ ALTERNATIVE(__stringify(RISCV_PTR do_page_fault), \ __stringify(RISCV_PTR sifive_cip_453_page_fault_trp), \ SIFIVE_VENDOR_ID, ERRATA_SIFIVE_CIP_453, \ CONFIG_ERRATA_SIFIVE_CIP_453) +#else /* !__ASSEMBLY__ */ + +#define ALT_FLUSH_TLB_PAGE(x) \ +asm(ALTERNATIVE("sfence.vma %0", "sfence.vma", SIFIVE_VENDOR_ID, \ + ERRATA_SIFIVE_CIP_1200, CONFIG_ERRATA_SIFIVE_CIP_1200) \ + : : "r" (addr) : "memory") + #endif /* __ASSEMBLY__ */ #endif
arch/riscv/include/asm/tlbflush.h +2 -1
@@ -9,6 +9,7 @@#include <linux/mm_types.h> #include <asm/smp.h> +#include <asm/errata_list.h> #ifdef CONFIG_MMU static inline void local_flush_tlb_all (void )
@@ -19,7 +20,7 @@ static inline void local_flush_tlb_all(void)/* Flush one page from local TLB */ static inline void local_flush_tlb_page (unsigned long addr) { - __asm__ __volatile__ ("sfence.vma %0" : :"r" (addr) :"memory" ); +ALT_FLUSH_TLB_PAGE (__asm__ __volatile__ ("sfence.vma %0" : :"r" (addr) :"memory" )); }#else /* CONFIG_MMU */ #define local_flush_tlb_all () do { } while (0)
Signature · 7 lines
2.7.4 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
Palmer Dabbelt
maintainerQuoted 75 lines · On Mon, 22 Mar 2021 07:26:01 PDT (-0700), [email protected] wrote:
With the emergence of more and more RISC-V CPUs, the request for how to upstream the vendor errata patch may gradually appear. In order to resolve this issue, this patch introduces the alternative mechanism from ARM64 and x86 to enable the kernel to patch code at runtime according to the manufacturer information of the running CPU. The main purpose of this patch set is to propose a framework to apply vendor's errata solutions. Based on this framework, it can be ensured that the errata only applies to the specified CPU cores. Other CPU cores do not be affected. Therefore, some complicated scenarios are unsupported in this patch set, such as patching code to the kernel module, doing relocation in patching code, and heterogeneous CPU topology.
In the "alternative" scheme, Users could use the macro ALTERNATIVE to apply an errata to the existing code flow. In the macro ALTERNATIVE, users need to specify the manufacturer information (vendor id, arch id, and implement id) for this errata. Therefore, kernel will know this errata is suitable for which CPU core. During the booting procedure, kernel will select the errata required by the CPU core and then patch it. It means that the kernel only applies the errata to the specified CPU core. In this case, the vendor's errata does not affect each other at runtime. The above patching procedure only occurs during the booting phase, so we only take the overhead of the "alternative" mechanism once.
This "alternative" mechanism is enabled by default to ensure that all required errata will be applied. However, users can disable this feature by the
Kconfig"CONFIG_RISCV_ERRATA_ALTERNATIVE".The last two patches are to apply the SiFive CIP-453 and CIP-1200 errata by this "alternative" scheme. Therefore, they can be regarded as examples. According to the results of running this image on the QEMU virt platform, kernel does not apply this errata at run-time because the CPU manufacturer information does not match the specified SiFive CPU core. Therefore, these errata does not affect any CPU core except for the specified SiFive cores.
Changes in v3 patch:
1. Remove "default y" setting ofCONFIG_ERRATA_SIFIVEfromKconfig.erratas
2. Correct the affected CPU list of errata "cip-453"Changes in v2 patch:
1. Display a warning message if Kernel finds the required errata is missing
2. Provide sample code for a vendor who wants to append its errata
3. Create a new patch to workaound SiFive errata cip-1200Vincent Chen (5):
riscv: Add 3 SBI wrapper functions to get cpu manufacturer information
riscv: Introduce alternative mechanism to apply errata solution
riscv: sifive: Add SiFive alternative ports
riscv: sifive: Apply errata "cip-453" patch
riscv: sifive: Apply errata "cip-1200" patcharch/riscv/Kconfig | 1 + arch/riscv/Kconfig.erratas | 44 +++++++++ arch/riscv/Kconfig.socs | 1 + arch/riscv/Makefile | 1 + arch/riscv/errata/Makefile | 2 + arch/riscv/errata/alternative.c | 74 +++++++++++++++ arch/riscv/errata/sifive/Makefile | 2 + arch/riscv/errata/sifive/errata.c | 106 +++++++++++++++++++++ arch/riscv/errata/sifive/errata_cip_453.S | 38 ++++++++ arch/riscv/include/asm/alternative-macros.h | 142 ++++++++++++++++++++++++++++ arch/riscv/include/asm/alternative.h | 39 ++++++++ arch/riscv/include/asm/asm.h | 1 + arch/riscv/include/asm/csr.h | 3 + arch/riscv/include/asm/errata_list.h | 39 ++++++++ arch/riscv/include/asm/sbi.h | 3 + arch/riscv/include/asm/sections.h | 1 + arch/riscv/include/asm/tlbflush.h | 3 +- arch/riscv/include/asm/vendorid_list.h | 10 ++ arch/riscv/kernel/entry.S | 6 +- arch/riscv/kernel/sbi.c | 15 +++ arch/riscv/kernel/smpboot.c | 4 + arch/riscv/kernel/vmlinux.lds.S | 7 ++ 22 files changed, 539 insertions(+), 3 deletions(-) create mode 100644 arch/riscv/Kconfig.erratas create mode 100644 arch/riscv/errata/Makefile
Quoted 8 lines
create mode 100644 arch/riscv/errata/alternative.c create mode 100644 arch/riscv/errata/sifive/Makefile create mode 100644 arch/riscv/errata/sifive/errata.c create mode 100644 arch/riscv/errata/sifive/errata_cip_453.S create mode 100644 arch/riscv/include/asm/alternative-macros.h create mode 100644 arch/riscv/include/asm/alternative.h create mode 100644 arch/riscv/include/asm/errata_list.h create mode 100644 arch/riscv/include/asm/vendorid_list.h
Thanks, these are on for-next. There was one checkpatch error related to _ALTERNATIVE_CFG locking parens, but I think that's a false positive due to all the assembly stuff so I've left it alone.
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv