Skip to contentSkip to conversation

riscv: introduce alternative mechanism to apply errata patches

PATCH Superseded Vincent Chen 2 participants 13 messages Mar 17, 2021 – Mar 22, 2021

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 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 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
4. Code refinement

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                  |  45 +++++++++
 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           | 105 ++++++++++++++++++++
 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
#1 lore raw

PATCH v2 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
index 99895d9c3bdd..dd2329962ceb 100644
@@ -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
index f4a7db3d309e..ead7e3fec104 100644
@@ -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
Reviewed-by on v2 1/5
#2 lore raw

Re: [PATCH v2 1/5] riscv: Add 3 SBI wrapper functions to get cpu manufacturer information

Quoted 5 lines · Vincent Chen

Add 3 wrapper functions to get vendor id, architecture id and implement id from M-mode

Looks good to me.

Regards,
Anup

Quoted 49 lines
---
 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
index 99895d9c3bdd..dd2329962ceb 100644
@@ -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 -1
index f4a7db3d309e..ead7e3fec104 100644
@@ -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;
--

2.7.4

Quoted 4 lines

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#3 lore raw

PATCH v2 2/5 riscv: Introduce alternative mechanism to apply errata solution

In reply to Vincent Chen

Introduce 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
index a998babc1237..2e26251fe1f2 100644
@@ -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
new file mode 100644
index 000000000000..4d0bafc536df
@@ -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
index 1368d943f1f3..1f5c03082976 100644
@@ -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
new file mode 100644
index 000000000000..43e6d5424367
@@ -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
new file mode 100644
index 000000000000..430bc4fea133
@@ -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
index 9c992a88d858..618d7c5af1a2 100644
@@ -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
index caadfc1d7487..87ac65696871 100644
@@ -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
new file mode 100644
index 000000000000..1b56131431c9
@@ -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
index 1595c5b60cfd..8a303fb1ee3b 100644
@@ -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
new file mode 100644
index 000000000000..9d934215b3c8
@@ -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
index 5e276c25646f..9a408e2942ac 100644
@@ -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
index de03cb22d0e9..7e61bc1dc36e 100644
@@ -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
Reviewed-by on v2 2/5
#4 lore raw

Re: [PATCH v2 2/5] riscv: Introduce alternative mechanism to apply errata solution

Quoted 18 lines · Vincent Chen

Introduce 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.

Quoted 5 lines

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".

Looks good to me.

Regards,
Anup

Quoted 453 lines
---
 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
index a998babc1237..2e26251fe1f2 100644
@@ -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
new file mode 100644
index 000000000000..4d0bafc536df
@@ -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
index 1368d943f1f3..1f5c03082976 100644
@@ -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
new file mode 100644
index 000000000000..43e6d5424367
@@ -0,0 +1 @@
+obj-y  += alternative.o
arch/riscv/errata/alternative.c new file +69 -0
new file mode 100644
index 000000000000..8efa60ad69b7
@@ -0,0 +1,69 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * alternative runtime patching
+ * inspired by the ARM64 and x86 version
+ *
+ * Copyright (C) 2021 Sifive.
+ */
+
+#include <linux/init.h>
+#include <linux/cpu.h>
+#include <linux/uaccess.h>
+#include <asm/alternative.h>
+#include <asm/sections.h>
+#include <asm/vendorid_list.h>
+#include <asm/sbi.h>
+#include <asm/csr.h>
+
+static struct cpu_manufacturer_info_t {
+       unsigned long vendor_id;
+       unsigned long arch_id;
+       unsigned long imp_id;
+} cpu_mfr_info;
+
+static void (*vendor_patch_func)(struct alt_entry *begin, struct alt_entry *end,
+                                unsigned long archid, unsigned long impid);
+
+static inline void __init riscv_fill_cpu_mfr_info(void)
+{
+#ifdef CONFIG_RISCV_M_MODE
+       cpu_mfr_info.vendor_id = csr_read(CSR_MVENDORID);
+       cpu_mfr_info.arch_id = csr_read(CSR_MARCHID);
+       cpu_mfr_info.imp_id = csr_read(CSR_MIMPID);
+#else
+       cpu_mfr_info.vendor_id = sbi_get_mvendorid();
+       cpu_mfr_info.arch_id = sbi_get_marchid();
+       cpu_mfr_info.imp_id = sbi_get_mimpid();
+#endif
+}
+
+static void __init init_alternative(void)
+{
+       riscv_fill_cpu_mfr_info();
+
+       switch (cpu_mfr_info.vendor_id) {
+       default:
+               vendor_patch_func = NULL;
+       }
+}
+
+/*
+ * This is called very early in the boot process (directly after we run
+ * a feature detect on the boot CPU). No need to worry about other CPUs
+ * here.
+ */
+void __init apply_boot_alternatives(void)
+{
+       /* If called on non-boot cpu things could go wrong */
+       WARN_ON(smp_processor_id() != 0);
+
+       init_alternative();
+
+       if (!vendor_patch_func)
+               return;
+
+       vendor_patch_func((struct alt_entry *)__alt_start,
+                         (struct alt_entry *)__alt_end,
+                         cpu_mfr_info.arch_id, cpu_mfr_info.imp_id);
+}
+
arch/riscv/include/asm/alternative-macros.h new file +142 -0
new file mode 100644
index 000000000000..88c08705f64a
@@ -0,0 +1,142 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __ASM_ALTERNATIVE_MACROS_H
+#define __ASM_ALTERNATIVE_MACROS_H
+
+#ifdef CONFIG_RISCV_ERRATA_ALTERNATIVE
+
+#ifdef __ASSEMBLY__
+
+.macro ALT_ENTRY oldptr newptr vendor_id errata_id new_len
+       RISCV_PTR \oldptr
+       RISCV_PTR \newptr
+       REG_ASM \vendor_id
+       REG_ASM \new_len
+       .word   \errata_id
+.endm
+
+.macro ALT_NEW_CONTENT vendor_id, errata_id, enable = 1, new_c : vararg
+       .if \enable
+       .pushsection .alternative, "a"
+       ALT_ENTRY 886b, 888f, \vendor_id, \errata_id, 889f - 888f
+       .popsection
+       .subsection 1
+888 :
+       \new_c
+889 :
+       .previous
+       .org    . - (889b - 888b) + (887b - 886b)
+       .org    . - (887b - 886b) + (889b - 888b)
+       .endif
+.endm
+
+.macro __ALTERNATIVE_CFG old_c, new_c, vendor_id, errata_id, enable
+886 :
+       \old_c
+887 :
+       ALT_NEW_CONTENT \vendor_id, \errata_id, \enable, \new_c
+.endm
+
+#define _ALTERNATIVE_CFG(old_c, new_c, vendor_id, errata_id, CONFIG_k) \
+       __ALTERNATIVE_CFG old_c, new_c, vendor_id, errata_id, IS_ENABLED(CONFIG_k)
+
+#else /* !__ASSEMBLY__ */
+
+#include <asm/asm.h>
+#include <linux/stringify.h>
+
+#define ALT_ENTRY(oldptr, newptr, vendor_id, errata_id, newlen) \
+       RISCV_PTR " " oldptr "\n" \
+       RISCV_PTR " " newptr "\n" \
+       REG_ASM " " vendor_id "\n" \
+       REG_ASM " " newlen "\n" \
+       ".word " errata_id "\n"
+
+#define ALT_NEW_CONSTENT(vendor_id, errata_id, enable, new_c) \
+       ".if " __stringify(enable) " == 1\n"                            \
+       ".pushsection .alternative, \"a\"\n"                            \
+       ALT_ENTRY("886b", "888f", __stringify(vendor_id), __stringify(errata_id), "889f - 888f") \
+       ".popsection\n"                                                 \
+       ".subsection 1\n"                                               \
+       "888 :\n"                                                       \
+       new_c "\n"                                                      \
+       "889 :\n"                                                       \
+       ".previous\n"                                                   \
+       ".org   . - (887b - 886b) + (889b - 888b)\n"                    \
+       ".org   . - (889b - 888b) + (887b - 886b)\n"                    \
+       ".endif\n"
+
+#define __ALTERNATIVE_CFG(old_c, new_c, vendor_id, errata_id, enable) \
+       "886 :\n"       \
+       old_c "\n"      \
+       "887 :\n"       \
+       ALT_NEW_CONSTENT(vendor_id, errata_id, enable, new_c)
+
+#define _ALTERNATIVE_CFG(old_c, new_c, vendor_id, errata_id, CONFIG_k) \
+       __ALTERNATIVE_CFG(old_c, new_c, vendor_id, errata_id, IS_ENABLED(CONFIG_k))
+
+#endif /* __ASSEMBLY__ */
+
+#else /* !CONFIG_RISCV_ERRATA_ALTERNATIVE*/
+#ifdef __ASSEMBLY__
+
+.macro __ALTERNATIVE_CFG old_c
+       \old_c
+.endm
+
+#define _ALTERNATIVE_CFG(old_c, new_c, vendor_id, errata_id, CONFIG_k) \
+       __ALTERNATIVE_CFG old_c
+
+#else /* !__ASSEMBLY__ */
+
+#define __ALTERNATIVE_CFG(old_c)  \
+       old_c "\n"
+
+#define _ALTERNATIVE_CFG(old_c, new_c, vendor_id, errata_id, CONFIG_k) \
+       __ALTERNATIVE_CFG(old_c)
+
+#endif /* __ASSEMBLY__ */
+#endif /* CONFIG_RISCV_ERRATA_ALTERNATIVE */
+/*
+ * Usage:
+ *   ALTERNATIVE(old_content, new_content, vendor_id, errata_id, CONFIG_k)
+ * in the assembly code. Otherwise,
+ *   asm(ALTERNATIVE(old_content, new_content, vendor_id, errata_id, CONFIG_k));
+ *
+ * old_content: The old content which is probably replaced with new content.
+ * new_content: The new content.
+ * vendor_id: The CPU vendor ID.
+ * errata_id: The errata ID.
+ * CONFIG_k: The Kconfig of this errata. When Kconfig is disabled, the old
+ *          content will alwyas be executed.
+ */
+#define ALTERNATIVE(old_content, new_content, vendor_id, errata_id, CONFIG_k) \
+       _ALTERNATIVE_CFG(old_content, new_content, vendor_id, errata_id, CONFIG_k)
+
+/*
+ * A vendor wants to replace an old_content, but another vendor has used
+ * ALTERNATIVE() to patch its customized content at the same location. In
+ * this case, this vendor can create a new macro ALTERNATIVE_2() based
+ * on the following sample code and then replace ALTERNATIVE() with
+ * ALTERNATIVE_2() to append its customized content.
+ *
+ * .macro __ALTERNATIVE_CFG_2 old_c, new_c_1, vendor_id_1, errata_id_1, enable_1, \
+ *                                   new_c_2, vendor_id_2, errata_id_2, enable_2
+ * 886 :
+ *      \old_c
+ * 887 :
+ *      ALT_NEW_CONTENT \vendor_id_1, \errata_id_1, \enable_1, \new_c_1
+ *      ALT_NEW_CONTENT \vendor_id_2, \errata_id_2, \enable_2, \new_c_2
+ * .endm
+ *
+ * #define _ALTERNATIVE_CFG_2(old_c, new_c_1, vendor_id_1, errata_id_1, CONFIG_k_1, \
+ *                                   new_c_2, vendor_id_2, errata_id_2, CONFIG_k_2) \
+ *        __ALTERNATIVE_CFG_2 old_c, new_c_1, vendor_id_1, errata_id_1, IS_ENABLED(CONFIG_k_1), \
+ *                                   new_c_2, vendor_id_2, errata_id_2, IS_ENABLED(CONFIG_k_2) \
+ *
+ * #define ALTERNATIVE_2(old_content, new_content_1, vendor_id_1, errata_id_1, CONFIG_k_1, \
+ *                                    new_content_2, vendor_id_2, errata_id_2, CONFIG_k_2) \
+ *         _ALTERNATIVE_CFG_2(old_content, new_content_1, vendor_id_1, errata_id_1, CONFIG_k_1, \
+ *                                         new_content_2, vendor_id_2, errata_id_2, CONFIG_k_2)
+ *
+ */
+#endif
arch/riscv/include/asm/alternative.h new file +36 -0
new file mode 100644
index 000000000000..430bc4fea133
@@ -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
index 9c992a88d858..618d7c5af1a2 100644
@@ -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
index caadfc1d7487..87ac65696871 100644
@@ -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
new file mode 100644
index 000000000000..1b56131431c9
@@ -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
index 1595c5b60cfd..8a303fb1ee3b 100644
@@ -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
new file mode 100644
index 000000000000..9d934215b3c8
@@ -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
index 5e276c25646f..9a408e2942ac 100644
@@ -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 -1
index de03cb22d0e9..7e61bc1dc36e 100644
@@ -90,6 +90,13 @@ SECTIONS
        }
 
        __init_data_end = .;
+
+       . = ALIGN(8);
+       .alternative : {
+               __alt_start = .;
+               *(.alternative)
+               __alt_end = .;
+       }
        __init_end = .;
 
        /* Start of data section */
--

2.7.4

Quoted 4 lines

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#5 lore raw

PATCH v2 3/5 riscv: sifive: Add SiFive alternative ports

In reply to Vincent Chen

Add required ports of the Alternative scheme for SiFive.

---
 arch/riscv/Kconfig.erratas           | 11 ++++++
 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, 90 insertions(+)
 create mode 100644 arch/riscv/errata/sifive/Makefile
 create mode 100644 arch/riscv/errata/sifive/errata.c
arch/riscv/Kconfig.erratas +11 -0
index 4d0bafc536df..e50ab67f4830 100644
@@ -9,4 +9,15 @@ 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
+	default y
+	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
index 7efcece8896c..b9eda857fc87 100644
@@ -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
index 43e6d5424367..b8f8740a3e44 100644
@@ -1 +1,2 @@
 obj-y	+= alternative.o
+obj-$(CONFIG_ERRATA_SIFIVE) += sifive/
arch/riscv/errata/alternative.c +5 -0
index 8efa60ad69b7..3b15885db70b 100644
@@ -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
new file mode 100644
index 000000000000..2d644e19caef
@@ -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
index 430bc4fea133..e625d3cafbed 100644
@@ -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
Reviewed-by on v2 3/5
#6 lore raw

Re: [PATCH v2 3/5] riscv: sifive: Add SiFive alternative ports

Quoted 21 lines · Vincent Chen

Add required ports of the Alternative scheme for SiFive.

---
 arch/riscv/Kconfig.erratas           | 11 ++++++
 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, 90 insertions(+)
 create mode 100644 arch/riscv/errata/sifive/Makefile
 create mode 100644 arch/riscv/errata/sifive/errata.c
arch/riscv/Kconfig.erratas +0 -0
index 4d0bafc536df..e50ab67f4830 100644

arch/riscv/Kconfig.erratas config RISCV_ERRATA_ALTERNATIVE · line 15

Quoted 7 lines
arch/riscv/Kconfig.erratas +4 -0
@@ -9,4 +9,15 @@ 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
+       default y

Remove the "default y" here.

Let the ERRATA Kconfig be explicitly selected from riscv/Kconfig.socs

Quoted 141 lines
arch/riscv/Kconfig.erratas +7 -0
+       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
index 7efcece8896c..b9eda857fc87 100644
@@ -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
index 43e6d5424367..b8f8740a3e44 100644
@@ -1 +1,2 @@
 obj-y  += alternative.o
+obj-$(CONFIG_ERRATA_SIFIVE) += sifive/
arch/riscv/errata/alternative.c +5 -0
index 8efa60ad69b7..3b15885db70b 100644
@@ -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
new file mode 100644
index 000000000000..2d644e19caef
@@ -0,0 +1 @@
+obj-y += errata.o
arch/riscv/errata/sifive/errata.c new file +68 -0
new file mode 100644
index 000000000000..826cd391fc55
@@ -0,0 +1,68 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2021 Sifive.
+ */
+
+#include <linux/kernel.h>
+#include <linux/string.h>
+#include <linux/bug.h>
+#include <asm/patch.h>
+#include <asm/alternative.h>
+#include <asm/vendorid_list.h>
+#include <asm/errata_list.h>
+
+struct errata_info_t {
+       char name[ERRATA_STRING_LENGTH_MAX];
+       bool (*check_func)(unsigned long  arch_id, unsigned long impid);
+};
+
+static u32 __init sifive_errata_probe(unsigned long archid, unsigned long impid)
+{
+       int idx;
+       u32 cpu_req_errata = 0;
+
+       for (idx = 0; idx < ERRATA_SIFIVE_NUMBER; idx++)
+               if (errata_list[idx].check_func(archid, impid))
+                       cpu_req_errata |= (1U << idx);
+
+       return cpu_req_errata;
+}
+
+static void __init warn_miss_errata(u32 miss_errata)
+{
+       int i;
+
+       pr_warn("----------------------------------------------------------------\n");
+       pr_warn("WARNING: Missing the following errata may cause potential issues\n");
+       for (i = 0; i < ERRATA_SIFIVE_NUMBER; i++)
+               if (miss_errata & 0x1 << i)
+                       pr_warn("\tSiFive Errata[%d]:%s\n", i, errata_list[i].name);
+       pr_warn("Please enable the corresponding Kconfig to apply them\n");
+       pr_warn("----------------------------------------------------------------\n");
+}
+
+void __init sifive_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
+                                    unsigned long archid, unsigned long impid)
+{
+       struct alt_entry *alt;
+       u32 cpu_req_errata = sifive_errata_probe(archid, impid);
+       u32 cpu_apply_errata = 0;
+       u32 tmp;
+
+       for (alt = begin; alt < end; alt++) {
+               if (alt->vendor_id != SIFIVE_VENDOR_ID)
+                       continue;
+               if (alt->errata_id >= ERRATA_SIFIVE_NUMBER) {
+                       WARN(1, "This errata id:%d is not in kernel errata list", alt->errata_id);
+                       continue;
+               }
+
+               tmp = (1U << alt->errata_id);
+               if (cpu_req_errata & tmp) {
+                       patch_text_nosync(alt->old_ptr, alt->alt_ptr, alt->alt_len);
+                       cpu_apply_errata |= tmp;
+               }
+       }
+       if (cpu_apply_errata != cpu_req_errata)
+               warn_miss_errata(cpu_req_errata - cpu_apply_errata);
+}
arch/riscv/include/asm/alternative.h +3 -1
index 430bc4fea133..e625d3cafbed 100644
@@ -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
--

2.7.4

Quoted 4 lines

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

Apart from the minor comment above, this patch looks good to me.

Regards,
Anup

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#7 lore raw

Re: [PATCH v2 3/5] riscv: sifive: Add SiFive alternative ports

Quoted 26 lines · Anup Patel
Quoted 24 lines · Vincent Chen

Add required ports of the Alternative scheme for SiFive.

---
 arch/riscv/Kconfig.erratas           | 11 ++++++
 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, 90 insertions(+)
 create mode 100644 arch/riscv/errata/sifive/Makefile
 create mode 100644 arch/riscv/errata/sifive/errata.c
arch/riscv/Kconfig.erratas +0 -0
index 4d0bafc536df..e50ab67f4830 100644
@@ -9,4 +9,15 @@ 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.
 
Quoted 9 lines

arch/riscv/Kconfig.erratas config RISCV_ERRATA_ALTERNATIVE · line 15

Quoted 4 lines
arch/riscv/Kconfig.erratas +4 -0
+config ERRATA_SIFIVE
+       bool "SiFive errata"
+       depends on RISCV_ERRATA_ALTERNATIVE
+       default y

Remove the "default y" here.

Let the ERRATA Kconfig be explicitly selected from riscv/Kconfig.socs

This is a good suggestion. I will modify it in my next version patch. Thank you,

Quoted 148 lines
Quoted 141 lines
arch/riscv/Kconfig.erratas +7 -0
+       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
index 7efcece8896c..b9eda857fc87 100644
@@ -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
index 43e6d5424367..b8f8740a3e44 100644
@@ -1 +1,2 @@
 obj-y  += alternative.o
+obj-$(CONFIG_ERRATA_SIFIVE) += sifive/
arch/riscv/errata/alternative.c +5 -0
index 8efa60ad69b7..3b15885db70b 100644
@@ -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
new file mode 100644
index 000000000000..2d644e19caef
@@ -0,0 +1 @@
+obj-y += errata.o
arch/riscv/errata/sifive/errata.c new file +68 -0
new file mode 100644
index 000000000000..826cd391fc55
@@ -0,0 +1,68 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2021 Sifive.
+ */
+
+#include <linux/kernel.h>
+#include <linux/string.h>
+#include <linux/bug.h>
+#include <asm/patch.h>
+#include <asm/alternative.h>
+#include <asm/vendorid_list.h>
+#include <asm/errata_list.h>
+
+struct errata_info_t {
+       char name[ERRATA_STRING_LENGTH_MAX];
+       bool (*check_func)(unsigned long  arch_id, unsigned long impid);
+};
+
+static u32 __init sifive_errata_probe(unsigned long archid, unsigned long impid)
+{
+       int idx;
+       u32 cpu_req_errata = 0;
+
+       for (idx = 0; idx < ERRATA_SIFIVE_NUMBER; idx++)
+               if (errata_list[idx].check_func(archid, impid))
+                       cpu_req_errata |= (1U << idx);
+
+       return cpu_req_errata;
+}
+
+static void __init warn_miss_errata(u32 miss_errata)
+{
+       int i;
+
+       pr_warn("----------------------------------------------------------------\n");
+       pr_warn("WARNING: Missing the following errata may cause potential issues\n");
+       for (i = 0; i < ERRATA_SIFIVE_NUMBER; i++)
+               if (miss_errata & 0x1 << i)
+                       pr_warn("\tSiFive Errata[%d]:%s\n", i, errata_list[i].name);
+       pr_warn("Please enable the corresponding Kconfig to apply them\n");
+       pr_warn("----------------------------------------------------------------\n");
+}
+
+void __init sifive_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
+                                    unsigned long archid, unsigned long impid)
+{
+       struct alt_entry *alt;
+       u32 cpu_req_errata = sifive_errata_probe(archid, impid);
+       u32 cpu_apply_errata = 0;
+       u32 tmp;
+
+       for (alt = begin; alt < end; alt++) {
+               if (alt->vendor_id != SIFIVE_VENDOR_ID)
+                       continue;
+               if (alt->errata_id >= ERRATA_SIFIVE_NUMBER) {
+                       WARN(1, "This errata id:%d is not in kernel errata list", alt->errata_id);
+                       continue;
+               }
+
+               tmp = (1U << alt->errata_id);
+               if (cpu_req_errata & tmp) {
+                       patch_text_nosync(alt->old_ptr, alt->alt_ptr, alt->alt_len);
+                       cpu_apply_errata |= tmp;
+               }
+       }
+       if (cpu_apply_errata != cpu_req_errata)
+               warn_miss_errata(cpu_req_errata - cpu_apply_errata);
+}
arch/riscv/include/asm/alternative.h +3 -1
index 430bc4fea133..e625d3cafbed 100644
@@ -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
--

2.7.4

Quoted 4 lines

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

Apart from the minor comment above, this patch looks good to me.

Quoted 4 lines

Regards,
Anup

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#8 lore raw

Re: [PATCH v2 3/5] riscv: sifive: Add SiFive alternative ports

Quoted 35 lines · Vincent Chen
Quoted 33 lines · Anup Patel
Quoted 21 lines · Vincent Chen

Add required ports of the Alternative scheme for SiFive.

---
 arch/riscv/Kconfig.erratas           | 11 ++++++
 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, 90 insertions(+)
 create mode 100644 arch/riscv/errata/sifive/Makefile
 create mode 100644 arch/riscv/errata/sifive/errata.c
arch/riscv/Kconfig.erratas +0 -0
index 4d0bafc536df..e50ab67f4830 100644

arch/riscv/Kconfig.erratas config RISCV_ERRATA_ALTERNATIVE · line 15

Quoted 7 lines
arch/riscv/Kconfig.erratas +4 -0
@@ -9,4 +9,15 @@ 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
+       default y

Remove the "default y" here.

Quoted 5 lines
Quoted 2 lines

Let the ERRATA Kconfig be explicitly selected from riscv/Kconfig.socs

This is a good suggestion. I will modify it in my next version patch. Thank you,

Sorry, I found the "default y" may not be removed here. It is because SiFive's customers possibly only license our Core IP. In this case, it does not enable CONFIG_SOC_SIFIVE. Therefore, I prefer to keep the "default y" feature. (In this patch, I have made the CONFIG_SOC_SIFIVE select the CONFIG_SOC_SIFIVE )

Quoted 148 lines
Quoted 148 lines
Quoted 141 lines
arch/riscv/Kconfig.erratas +7 -0
+       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
index 7efcece8896c..b9eda857fc87 100644
@@ -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
index 43e6d5424367..b8f8740a3e44 100644
@@ -1 +1,2 @@
 obj-y  += alternative.o
+obj-$(CONFIG_ERRATA_SIFIVE) += sifive/
arch/riscv/errata/alternative.c +5 -0
index 8efa60ad69b7..3b15885db70b 100644
@@ -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
new file mode 100644
index 000000000000..2d644e19caef
@@ -0,0 +1 @@
+obj-y += errata.o
arch/riscv/errata/sifive/errata.c new file +68 -0
new file mode 100644
index 000000000000..826cd391fc55
@@ -0,0 +1,68 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2021 Sifive.
+ */
+
+#include <linux/kernel.h>
+#include <linux/string.h>
+#include <linux/bug.h>
+#include <asm/patch.h>
+#include <asm/alternative.h>
+#include <asm/vendorid_list.h>
+#include <asm/errata_list.h>
+
+struct errata_info_t {
+       char name[ERRATA_STRING_LENGTH_MAX];
+       bool (*check_func)(unsigned long  arch_id, unsigned long impid);
+};
+
+static u32 __init sifive_errata_probe(unsigned long archid, unsigned long impid)
+{
+       int idx;
+       u32 cpu_req_errata = 0;
+
+       for (idx = 0; idx < ERRATA_SIFIVE_NUMBER; idx++)
+               if (errata_list[idx].check_func(archid, impid))
+                       cpu_req_errata |= (1U << idx);
+
+       return cpu_req_errata;
+}
+
+static void __init warn_miss_errata(u32 miss_errata)
+{
+       int i;
+
+       pr_warn("----------------------------------------------------------------\n");
+       pr_warn("WARNING: Missing the following errata may cause potential issues\n");
+       for (i = 0; i < ERRATA_SIFIVE_NUMBER; i++)
+               if (miss_errata & 0x1 << i)
+                       pr_warn("\tSiFive Errata[%d]:%s\n", i, errata_list[i].name);
+       pr_warn("Please enable the corresponding Kconfig to apply them\n");
+       pr_warn("----------------------------------------------------------------\n");
+}
+
+void __init sifive_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
+                                    unsigned long archid, unsigned long impid)
+{
+       struct alt_entry *alt;
+       u32 cpu_req_errata = sifive_errata_probe(archid, impid);
+       u32 cpu_apply_errata = 0;
+       u32 tmp;
+
+       for (alt = begin; alt < end; alt++) {
+               if (alt->vendor_id != SIFIVE_VENDOR_ID)
+                       continue;
+               if (alt->errata_id >= ERRATA_SIFIVE_NUMBER) {
+                       WARN(1, "This errata id:%d is not in kernel errata list", alt->errata_id);
+                       continue;
+               }
+
+               tmp = (1U << alt->errata_id);
+               if (cpu_req_errata & tmp) {
+                       patch_text_nosync(alt->old_ptr, alt->alt_ptr, alt->alt_len);
+                       cpu_apply_errata |= tmp;
+               }
+       }
+       if (cpu_apply_errata != cpu_req_errata)
+               warn_miss_errata(cpu_req_errata - cpu_apply_errata);
+}
arch/riscv/include/asm/alternative.h +3 -1
index 430bc4fea133..e625d3cafbed 100644
@@ -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
--

2.7.4

Quoted 4 lines

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

Apart from the minor comment above, this patch looks good to me.

Quoted 4 lines
Quoted 4 lines

Regards,
Anup

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#9 lore raw

Re: [PATCH v2 3/5] riscv: sifive: Add SiFive alternative ports

Quoted 39 lines · Vincent Chen
Quoted 37 lines · Vincent Chen
Quoted 35 lines · Anup Patel
Quoted 21 lines · Vincent Chen

Add required ports of the Alternative scheme for SiFive.

---
 arch/riscv/Kconfig.erratas           | 11 ++++++
 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, 90 insertions(+)
 create mode 100644 arch/riscv/errata/sifive/Makefile
 create mode 100644 arch/riscv/errata/sifive/errata.c
arch/riscv/Kconfig.erratas +0 -0
index 4d0bafc536df..e50ab67f4830 100644

arch/riscv/Kconfig.erratas config RISCV_ERRATA_ALTERNATIVE · line 15

Quoted 7 lines
arch/riscv/Kconfig.erratas +4 -0
@@ -9,4 +9,15 @@ 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
+       default y

Remove the "default y" here.

Let the ERRATA Kconfig be explicitly selected from riscv/Kconfig.socs

Quoted 8 lines
Quoted 3 lines

This is a good suggestion. I will modify it in my next version patch. Thank you,

Sorry, I found the "default y" may not be removed here. It is because SiFive's customers possibly only license our Core IP. In this case, it does not enable CONFIG_SOC_SIFIVE. Therefore, I prefer to keep the "default y" feature. (In this patch, I have made the CONFIG_SOC_SIFIVE select the CONFIG_SOC_SIFIVE )

If you are using SiFive IP for your <xyz> SOC then your CONFIG_SOC_<xyz> in Kconfig option should select ERRATA_SIFIVE

Basically, SOC Kconfig options should explicitly select required ERRATA configs so that if someone wants to disable ERRATs from menuconfig then it can be easily done without knowledge of which SOCs need which ERRATAs.

Regards,
Anup

Quoted 149 lines
Quoted 149 lines
Quoted 149 lines
Quoted 142 lines
arch/riscv/Kconfig.erratas +7 -0
+       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
index 7efcece8896c..b9eda857fc87 100644
@@ -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
index 43e6d5424367..b8f8740a3e44 100644
@@ -1 +1,2 @@
 obj-y  += alternative.o
+obj-$(CONFIG_ERRATA_SIFIVE) += sifive/
arch/riscv/errata/alternative.c +5 -0
index 8efa60ad69b7..3b15885db70b 100644
@@ -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
new file mode 100644
index 000000000000..2d644e19caef
@@ -0,0 +1 @@
+obj-y += errata.o
arch/riscv/errata/sifive/errata.c new file +68 -0
new file mode 100644
index 000000000000..826cd391fc55
@@ -0,0 +1,68 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2021 Sifive.
+ */
+
+#include <linux/kernel.h>
+#include <linux/string.h>
+#include <linux/bug.h>
+#include <asm/patch.h>
+#include <asm/alternative.h>
+#include <asm/vendorid_list.h>
+#include <asm/errata_list.h>
+
+struct errata_info_t {
+       char name[ERRATA_STRING_LENGTH_MAX];
+       bool (*check_func)(unsigned long  arch_id, unsigned long impid);
+};
+
+static u32 __init sifive_errata_probe(unsigned long archid, unsigned long impid)
+{
+       int idx;
+       u32 cpu_req_errata = 0;
+
+       for (idx = 0; idx < ERRATA_SIFIVE_NUMBER; idx++)
+               if (errata_list[idx].check_func(archid, impid))
+                       cpu_req_errata |= (1U << idx);
+
+       return cpu_req_errata;
+}
+
+static void __init warn_miss_errata(u32 miss_errata)
+{
+       int i;
+
+       pr_warn("----------------------------------------------------------------\n");
+       pr_warn("WARNING: Missing the following errata may cause potential issues\n");
+       for (i = 0; i < ERRATA_SIFIVE_NUMBER; i++)
+               if (miss_errata & 0x1 << i)
+                       pr_warn("\tSiFive Errata[%d]:%s\n", i, errata_list[i].name);
+       pr_warn("Please enable the corresponding Kconfig to apply them\n");
+       pr_warn("----------------------------------------------------------------\n");
+}
+
+void __init sifive_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
+                                    unsigned long archid, unsigned long impid)
+{
+       struct alt_entry *alt;
+       u32 cpu_req_errata = sifive_errata_probe(archid, impid);
+       u32 cpu_apply_errata = 0;
+       u32 tmp;
+
+       for (alt = begin; alt < end; alt++) {
+               if (alt->vendor_id != SIFIVE_VENDOR_ID)
+                       continue;
+               if (alt->errata_id >= ERRATA_SIFIVE_NUMBER) {
+                       WARN(1, "This errata id:%d is not in kernel errata list", alt->errata_id);
+                       continue;
+               }
+
+               tmp = (1U << alt->errata_id);
+               if (cpu_req_errata & tmp) {
+                       patch_text_nosync(alt->old_ptr, alt->alt_ptr, alt->alt_len);
+                       cpu_apply_errata |= tmp;
+               }
+       }
+       if (cpu_apply_errata != cpu_req_errata)
+               warn_miss_errata(cpu_req_errata - cpu_apply_errata);
+}
arch/riscv/include/asm/alternative.h +3 -1
index 430bc4fea133..e625d3cafbed 100644
@@ -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
--

2.7.4

Quoted 4 lines

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

Apart from the minor comment above, this patch looks good to me.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Regards,
Anup

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#10 lore raw

Re: [PATCH v2 3/5] riscv: sifive: Add SiFive alternative ports

Quoted 54 lines · Anup Patel
Quoted 39 lines · Vincent Chen
Quoted 37 lines · Vincent Chen
Quoted 35 lines · Anup Patel
Quoted 21 lines · Vincent Chen

Add required ports of the Alternative scheme for SiFive.

---
 arch/riscv/Kconfig.erratas           | 11 ++++++
 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, 90 insertions(+)
 create mode 100644 arch/riscv/errata/sifive/Makefile
 create mode 100644 arch/riscv/errata/sifive/errata.c
arch/riscv/Kconfig.erratas +0 -0
index 4d0bafc536df..e50ab67f4830 100644

arch/riscv/Kconfig.erratas config RISCV_ERRATA_ALTERNATIVE · line 15

Quoted 7 lines
arch/riscv/Kconfig.erratas +4 -0
@@ -9,4 +9,15 @@ 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
+       default y

Remove the "default y" here.

Let the ERRATA Kconfig be explicitly selected from riscv/Kconfig.socs

Quoted 8 lines
Quoted 3 lines

This is a good suggestion. I will modify it in my next version patch. Thank you,

Sorry, I found the "default y" may not be removed here. It is because SiFive's customers possibly only license our Core IP. In this case, it does not enable CONFIG_SOC_SIFIVE. Therefore, I prefer to keep the "default y" feature. (In this patch, I have made the CONFIG_SOC_SIFIVE select the CONFIG_SOC_SIFIVE )

If you are using SiFive IP for your <xyz> SOC then your CONFIG_SOC_<xyz> in Kconfig option should select ERRATA_SIFIVE

Quoted 8 lines

Basically, SOC Kconfig options should explicitly select required ERRATA configs so that if someone wants to disable ERRATs from menuconfig then it can be easily done without knowledge of which SOCs need which ERRATAs.

Regards,
Anup

I got it. I will remove it in my next version patch. Thank you for the explanation.

Best regards,
Vincent

Quoted 149 lines
Quoted 149 lines
Quoted 149 lines
Quoted 149 lines
Quoted 142 lines
arch/riscv/Kconfig.erratas +7 -0
+       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
index 7efcece8896c..b9eda857fc87 100644
@@ -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
index 43e6d5424367..b8f8740a3e44 100644
@@ -1 +1,2 @@
 obj-y  += alternative.o
+obj-$(CONFIG_ERRATA_SIFIVE) += sifive/
arch/riscv/errata/alternative.c +5 -0
index 8efa60ad69b7..3b15885db70b 100644
@@ -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
new file mode 100644
index 000000000000..2d644e19caef
@@ -0,0 +1 @@
+obj-y += errata.o
arch/riscv/errata/sifive/errata.c new file +68 -0
new file mode 100644
index 000000000000..826cd391fc55
@@ -0,0 +1,68 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2021 Sifive.
+ */
+
+#include <linux/kernel.h>
+#include <linux/string.h>
+#include <linux/bug.h>
+#include <asm/patch.h>
+#include <asm/alternative.h>
+#include <asm/vendorid_list.h>
+#include <asm/errata_list.h>
+
+struct errata_info_t {
+       char name[ERRATA_STRING_LENGTH_MAX];
+       bool (*check_func)(unsigned long  arch_id, unsigned long impid);
+};
+
+static u32 __init sifive_errata_probe(unsigned long archid, unsigned long impid)
+{
+       int idx;
+       u32 cpu_req_errata = 0;
+
+       for (idx = 0; idx < ERRATA_SIFIVE_NUMBER; idx++)
+               if (errata_list[idx].check_func(archid, impid))
+                       cpu_req_errata |= (1U << idx);
+
+       return cpu_req_errata;
+}
+
+static void __init warn_miss_errata(u32 miss_errata)
+{
+       int i;
+
+       pr_warn("----------------------------------------------------------------\n");
+       pr_warn("WARNING: Missing the following errata may cause potential issues\n");
+       for (i = 0; i < ERRATA_SIFIVE_NUMBER; i++)
+               if (miss_errata & 0x1 << i)
+                       pr_warn("\tSiFive Errata[%d]:%s\n", i, errata_list[i].name);
+       pr_warn("Please enable the corresponding Kconfig to apply them\n");
+       pr_warn("----------------------------------------------------------------\n");
+}
+
+void __init sifive_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
+                                    unsigned long archid, unsigned long impid)
+{
+       struct alt_entry *alt;
+       u32 cpu_req_errata = sifive_errata_probe(archid, impid);
+       u32 cpu_apply_errata = 0;
+       u32 tmp;
+
+       for (alt = begin; alt < end; alt++) {
+               if (alt->vendor_id != SIFIVE_VENDOR_ID)
+                       continue;
+               if (alt->errata_id >= ERRATA_SIFIVE_NUMBER) {
+                       WARN(1, "This errata id:%d is not in kernel errata list", alt->errata_id);
+                       continue;
+               }
+
+               tmp = (1U << alt->errata_id);
+               if (cpu_req_errata & tmp) {
+                       patch_text_nosync(alt->old_ptr, alt->alt_ptr, alt->alt_len);
+                       cpu_apply_errata |= tmp;
+               }
+       }
+       if (cpu_apply_errata != cpu_req_errata)
+               warn_miss_errata(cpu_req_errata - cpu_apply_errata);
+}
arch/riscv/include/asm/alternative.h +3 -1
index 430bc4fea133..e625d3cafbed 100644
@@ -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
--

2.7.4

Quoted 4 lines

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

Apart from the minor comment above, this patch looks good to me.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Regards,
Anup

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#11 lore raw

PATCH v2 4/5 riscv: sifive: Apply errata "cip-453" patch

In reply to Vincent Chen

Add 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         | 19 ++++++++++++++++
 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, 93 insertions(+), 3 deletions(-)
 create mode 100644 arch/riscv/errata/sifive/errata_cip_453.S
arch/riscv/Kconfig.erratas +11 -0
index e50ab67f4830..a7f0abff90fb 100644
@@ -20,4 +20,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
index 2d644e19caef..bdd5fc843b8e 100644
@@ -1 +1,2 @@
+obj-y += errata_cip_453.o
 obj-y += errata.o
arch/riscv/errata/sifive/errata.c +19 -0
index 826cd391fc55..6c65cd675e5f 100644
@@ -16,6 +16,25 @@ 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, 0x00200504
+	 */
+	if (arch_id != 0x8000000000000007 || (impid != 0x20181004 && impid != 0x00200504))
+		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
new file mode 100644
index 000000000000..f1b9623fe1de
@@ -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
index 1b56131431c9..6148d34d4245 100644
@@ -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
index 744f3209c48d..60d0a2f1cd88 100644
@@ -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
#12 lore raw

PATCH v2 5/5 riscv: sifive: Apply errata "cip-1200" patch

In reply to Vincent Chen

For 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
index a7f0abff90fb..93c6c45f4290 100644
@@ -31,4 +31,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
index 6c65cd675e5f..655af0cb17d1 100644
@@ -28,11 +28,29 @@ static bool errata_cip_453_check_func(unsigned long  arch_id, unsigned long impi
 	return 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
index 6148d34d4245..5f1046e82d9f 100644
@@ -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
index 394cfbccdcd9..c84218ad7afc 100644
@@ -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