Skip to contentSkip to conversation

riscv: Fix the PAUSE Opcode for MIPS P8700.

PATCH In review Aleksandar Rikalo 7 participants 10 messages Jan 29, 2025 – Mar 25, 2025 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 riscv: Fix the PAUSE Opcode for MIPS P8700.

From: Djordje Todorovic <[email protected]>

The riscv MIPS P8700 uses a different opcode for PAUSE.
It is a ‘hint’ encoding of the SLLI instruction, with rd=0, rs1=0 and imm=5. It will behave as a NOP instruction if no additional behavior beyond that of SLLI is implemented.

Add ERRATA_MIPS and ERRATA_MIPS_P8700_PAUSE_OPCODE configs. Handle errata for MIPS CPUs.

---
 arch/riscv/Kconfig.errata                     | 23 +++++++++++
 arch/riscv/errata/Makefile                    |  1 +
 arch/riscv/errata/mips/Makefile               |  5 +++
 arch/riscv/errata/mips/errata.c               | 41 +++++++++++++++++++
 arch/riscv/include/asm/alternative.h          |  3 ++
 arch/riscv/include/asm/cmpxchg.h              |  3 +-
 arch/riscv/include/asm/errata_list.h          | 20 ++++++++-
 arch/riscv/include/asm/vdso/processor.h       |  4 +-
 arch/riscv/include/asm/vendorid_list.h        |  1 +
 arch/riscv/kernel/alternative.c               |  5 +++
 arch/riscv/kernel/entry.S                     |  2 +
 arch/riscv/mm/init.c                          |  1 +
 tools/arch/riscv/include/asm/vdso/processor.h |  5 ++-
 13 files changed, 110 insertions(+), 4 deletions(-)
 create mode 100644 arch/riscv/errata/mips/Makefile
 create mode 100644 arch/riscv/errata/mips/errata.c
arch/riscv/Kconfig.errata +23 -0
index 2acc7d876e1f..1589528fdc30 100644
@@ -21,6 +21,29 @@ config ERRATA_ANDES_CMO
 
 	  If you don't know what to do here, say "Y".
 
+config ERRATA_MIPS
+	bool "MIPS errata"
+	depends on RISCV_ALTERNATIVE
+	help
+	  All MIPS errata Kconfig depend on this Kconfig. Disabling
+	  this Kconfig will disable all MIPS errata. Please say "Y"
+	  here if your platform uses MIPS CPU cores.
+
+	  Otherwise, please say "N" here to avoid unnecessary overhead.
+
+config ERRATA_MIPS_P8700_PAUSE_OPCODE
+	bool "Fix the PAUSE Opcode for MIPS P8700"
+	depends on ERRATA_MIPS && 64BIT
+	default n
+	help
+	   The RISCV MIPS P8700 uses a different opcode for PAUSE.
+	   It is a 'hint' encoding of the SLLI instruction,
+	   with rd=0, rs1=0 and imm=5. It will behave as a NOP
+	   instruction if no additional behavior beyond that of
+	   SLLI is implemented.
+
+	   If you are not using the P8700 processor, say n.
+
 config ERRATA_SIFIVE
 	bool "SiFive errata"
 	depends on RISCV_ALTERNATIVE
arch/riscv/errata/Makefile +1 -0
index f0da9d7b39c3..156cafb338c1 100644
@@ -9,5 +9,6 @@ endif
 endif
 
 obj-$(CONFIG_ERRATA_ANDES) += andes/
+obj-$(CONFIG_ERRATA_MIPS) += mips/
 obj-$(CONFIG_ERRATA_SIFIVE) += sifive/
 obj-$(CONFIG_ERRATA_THEAD) += thead/
arch/riscv/errata/mips/Makefile new file +5 -0
new file mode 100644
index 000000000000..6278c389b801
@@ -0,0 +1,5 @@
+ifdef CONFIG_RISCV_ALTERNATIVE_EARLY
+CFLAGS_errata.o := -mcmodel=medany
+endif
+
+obj-y += errata.o
arch/riscv/errata/mips/errata.c new file +41 -0

41 lines not on this page · raw message

arch/riscv/include/asm/alternative.h +3 -0
index 3c2b59b25017..bc3ada8190a9 100644
@@ -48,6 +48,9 @@ struct alt_entry {
 void andes_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
 			     unsigned long archid, unsigned long impid,
 			     unsigned int stage);
+void mips_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
+			      unsigned long archid, unsigned long impid,
+			      unsigned int stage);
 void sifive_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
 			      unsigned long archid, unsigned long impid,
 			      unsigned int stage);
arch/riscv/include/asm/cmpxchg.h +2 -1
index 4cadc56220fe..2f5705f49dcc 100644
@@ -13,6 +13,7 @@
 #include <asm/hwcap.h>
 #include <asm/insn-def.h>
 #include <asm/cpufeature-macros.h>
+#include <asm/errata_list.h>
 
 #define __arch_xchg_masked(sc_sfx, swap_sfx, prepend, sc_append,		\
 			   swap_append, r, p, n)				\
@@ -404,7 +405,7 @@ static __always_inline void __cmpwait(volatile void *ptr,
 	return;
 
 no_zawrs:
-	asm volatile(RISCV_PAUSE : : : "memory");
+	ALT_RISCV_PAUSE();
 }
 
 #define __cmpwait_relaxed(ptr, val) \
arch/riscv/include/asm/errata_list.h +19 -1
index 7c8a71a526a3..68c46ccb9b7c 100644
@@ -5,7 +5,6 @@
 #ifndef ASM_ERRATA_LIST_H
 #define ASM_ERRATA_LIST_H
 
-#include <asm/alternative.h>
 #include <asm/csr.h>
 #include <asm/insn-def.h>
 #include <asm/hwcap.h>
@@ -28,6 +27,11 @@
 #define	ERRATA_THEAD_NUMBER 2
 #endif
 
+#ifdef CONFIG_ERRATA_MIPS
+#define ERRATA_MIPS_P8700_PAUSE_OPCODE 0
+#define ERRATA_MIPS_NUMBER 1
+#endif
+
 #ifdef __ASSEMBLY__
 
 #define ALT_INSN_FAULT(x)						\
@@ -58,6 +62,20 @@ asm(ALTERNATIVE("sfence.vma %0, %1", "sfence.vma", SIFIVE_VENDOR_ID,	\
 		ERRATA_SIFIVE_CIP_1200, CONFIG_ERRATA_SIFIVE_CIP_1200)	\
 		: : "r" (addr), "r" (asid) : "memory")
 
+#ifdef CONFIG_ERRATA_MIPS_P8700_PAUSE_OPCODE
+#define ALT_RISCV_PAUSE()                                               \
+asm volatile(ALTERNATIVE(                                               \
+    RISCV_PAUSE,                /* Original RISC‑V pause insn */        \
+    ".4byte 0x00501013",        /* Replacement for MIPS P8700 */        \
+    MIPS_VENDOR_ID,             /* Vendor ID to match */                \
+    ERRATA_MIPS_P8700_PAUSE_OPCODE, /* patch_id */                      \
+    CONFIG_ERRATA_MIPS_P8700_PAUSE_OPCODE)                              \
+    : /* no outputs */ : /* no inputs */ : "memory")
+#else
+#define ALT_RISCV_PAUSE() \
+    asm volatile(RISCV_PAUSE : : : "memory")
+#endif
+
 /*
  * _val is marked as "will be overwritten", so need to set it to 0
  * in the default case.
arch/riscv/include/asm/vdso/processor.h +3 -1
index 8f383f05a290..8f749552ecfe 100644
@@ -5,6 +5,8 @@
 #ifndef __ASSEMBLY__
 
 #include <asm/barrier.h>
+
+#include <asm/errata_list.h>
 #include <asm/insn-def.h>
 
 static inline void cpu_relax(void)
@@ -19,7 +21,7 @@ static inline void cpu_relax(void)
 	 * Reduce instruction retirement.
 	 * This assumes the PC changes.
 	 */
-	__asm__ __volatile__ (RISCV_PAUSE);
+	ALT_RISCV_PAUSE();
 	barrier();
 }
 
arch/riscv/include/asm/vendorid_list.h +1 -0
index a5150cdf34d8..3b09874d7a6d 100644
@@ -9,5 +9,6 @@
 #define MICROCHIP_VENDOR_ID	0x029
 #define SIFIVE_VENDOR_ID	0x489
 #define THEAD_VENDOR_ID		0x5b7
+#define MIPS_VENDOR_ID		0x722
 
 #endif
arch/riscv/kernel/alternative.c +5 -0
index 7eb3cb1215c6..7642704c7f18 100644
@@ -47,6 +47,11 @@ static void riscv_fill_cpu_mfr_info(struct cpu_manufacturer_info_t *cpu_mfr_info
 		cpu_mfr_info->patch_func = andes_errata_patch_func;
 		break;
 #endif
+#ifdef CONFIG_ERRATA_MIPS
+	case MIPS_VENDOR_ID:
+		cpu_mfr_info->patch_func = mips_errata_patch_func;
+		break;
+#endif
 #ifdef CONFIG_ERRATA_SIFIVE
 	case SIFIVE_VENDOR_ID:
 		cpu_mfr_info->patch_func = sifive_errata_patch_func;
arch/riscv/kernel/entry.S +2 -0
index 33a5a9f2a0d4..2007d9de7f27 100644
@@ -15,6 +15,8 @@
 #include <asm/thread_info.h>
 #include <asm/asm-offsets.h>
 #include <asm/errata_list.h>
+#include <asm/alternative.h>
+
 #include <linux/sizes.h>
 
 	.section .irqentry.text, "ax"
arch/riscv/mm/init.c +1 -0
index 722178ae3488..ee8a2c5b40f8 100644
@@ -35,6 +35,7 @@
 #include <asm/soc.h>
 #include <asm/sparsemem.h>
 #include <asm/tlbflush.h>
+#include <asm/alternative.h>
 
 #include "../kernel/head.h"
 
tools/arch/riscv/include/asm/vdso/processor.h +4 -1
index 662aca039848..880f26a24f69 100644
@@ -14,7 +14,10 @@ static inline void cpu_relax(void)
 	__asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
 #endif
 
-#ifdef CONFIG_TOOLCHAIN_HAS_ZIHINTPAUSE
+#ifdef CONFIG_ERRATA_MIPS_P8700_PAUSE_OPCODE
+    /* MIPS P8700 pause opcode */
+    __asm__ __volatile__ (".4byte 0x00501013");
+#elif CONFIG_TOOLCHAIN_HAS_ZIHINTPAUSE
 	/*
 	 * Reduce instruction retirement.
 	 * This assumes the PC changes.
Signature · 1 line
2.25.1
#1 lore raw
Quoted 6 lines · Aleksandar Rikalo

From: Djordje Todorovic <[email protected]>

The riscv MIPS P8700 uses a different opcode for PAUSE.
It is a ‘hint’ encoding of the SLLI instruction, with rd=0, rs1=0 and imm=5. It will behave as a NOP instruction if no additional behavior beyond that of SLLI is implemented.

You say p8700, but the erratum will be applied on all systems that are identified as using a mips cpu. Why's that?

Quoted 35 lines
arch/riscv/errata/mips/errata.c new file +28 -0
+void mips_errata_patch_func(struct alt_entry *begin,
+					     struct alt_entry *end,
+					     unsigned long archid,
+					     unsigned long impid,
+					     unsigned int stage)
+{
+	struct alt_entry *alt;
+
+	BUILD_BUG_ON(ERRATA_MIPS_NUMBER >= RISCV_VENDOR_EXT_ALTERNATIVES_BASE);
+
+	if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
+		return;
+
+	for (alt = begin; alt < end; alt++) {
+		if (alt->vendor_id != MIPS_VENDOR_ID)
+			continue;
+
+		if (alt->patch_id >= ERRATA_MIPS_NUMBER) {
+			WARN(1, "MIPS errata id:%d not in kernel errata list\n",
+			     alt->patch_id);
+			continue;
+		}
+
+		mutex_lock(&text_mutex);
+		patch_text_nosync(ALT_OLD_PTR(alt), ALT_ALT_PTR(alt), alt->alt_len);
+		mutex_unlock(&text_mutex);
+	}
+}
tools/arch/riscv/include/asm/vdso/processor.h +0 -0
index 662aca039848..880f26a24f69 100644
@@ -14,7 +14,10 @@ static inline void cpu_relax(void)
 	__asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));

tools/arch/riscv/include/asm/vdso/processor.h static inline void cpu_relax(void) · line 23

Quoted 10 lines
tools/arch/riscv/include/asm/vdso/processor.h +4 -1
 #endif
 
-#ifdef CONFIG_TOOLCHAIN_HAS_ZIHINTPAUSE
+#ifdef CONFIG_ERRATA_MIPS_P8700_PAUSE_OPCODE
+    /* MIPS P8700 pause opcode */
+    __asm__ __volatile__ (".4byte 0x00501013");
+#elif CONFIG_TOOLCHAIN_HAS_ZIHINTPAUSE
 	/*
 	 * Reduce instruction retirement.
 	 * This assumes the PC changes.

What about when the erratum is enabled and the toolchain supports Zihintpause? Why don't you use the same implementation as the !tools copy of the header? (I'm not sure why they're different in the first place).

#2 lore raw
Quoted 48 lines · Conor Dooley
Quoted 6 lines · Aleksandar Rikalo

From: Djordje Todorovic <[email protected]>

The riscv MIPS P8700 uses a different opcode for PAUSE.
It is a ‘hint’ encoding of the SLLI instruction, with rd=0, rs1=0 and imm=5. It will behave as a NOP instruction if no additional behavior beyond that of SLLI is implemented.

You say p8700, but the erratum will be applied on all systems that are identified as using a mips cpu. Why's that?

Quoted 37 lines
arch/riscv/errata/mips/errata.c new file +28 -0
+void mips_errata_patch_func(struct alt_entry *begin,
+					     struct alt_entry *end,
+					     unsigned long archid,
+					     unsigned long impid,
+					     unsigned int stage)
+{
+	struct alt_entry *alt;
+
+	BUILD_BUG_ON(ERRATA_MIPS_NUMBER >= RISCV_VENDOR_EXT_ALTERNATIVES_BASE);
+
+	if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
+		return;
+
+	for (alt = begin; alt < end; alt++) {
+		if (alt->vendor_id != MIPS_VENDOR_ID)
+			continue;
+
+		if (alt->patch_id >= ERRATA_MIPS_NUMBER) {
+			WARN(1, "MIPS errata id:%d not in kernel errata list\n",
+			     alt->patch_id);
+			continue;
+		}
+
+		mutex_lock(&text_mutex);
+		patch_text_nosync(ALT_OLD_PTR(alt), ALT_ALT_PTR(alt), alt->alt_len);
+		mutex_unlock(&text_mutex);
+	}
+}
tools/arch/riscv/include/asm/vdso/processor.h +0 -0
index 662aca039848..880f26a24f69 100644
@@ -14,7 +14,10 @@ static inline void cpu_relax(void)
 	__asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
 #endif
 
Quoted 11 lines

tools/arch/riscv/include/asm/vdso/processor.h static inline void cpu_relax(void) · line 23

Quoted 8 lines
tools/arch/riscv/include/asm/vdso/processor.h +4 -1
-#ifdef CONFIG_TOOLCHAIN_HAS_ZIHINTPAUSE
+#ifdef CONFIG_ERRATA_MIPS_P8700_PAUSE_OPCODE
+    /* MIPS P8700 pause opcode */
+    __asm__ __volatile__ (".4byte 0x00501013");
+#elif CONFIG_TOOLCHAIN_HAS_ZIHINTPAUSE
 	/*
 	 * Reduce instruction retirement.
 	 * This assumes the PC changes.

What about when the erratum is enabled and the toolchain supports Zihintpause?

So the other way to do this is having an hwprobe call to check if the currently running processor is effected by this. However I was concerned about the performance penalty of calling hwprobe here in the previous version so I had suggested to use a flag instead so there is not the penalty on other architectures. This does make it invalid to enable this errata in the defconfig. This is a precedent for how we want to handle errata in tools.

Quoted 3 lines

Why don't you use the same implementation as the !tools copy of the header? (I'm not sure why they're different in the first place).

It is different because the headers in tools are userspace so it doesn't make sense to have alternatives.

- Charlie

Quoted 4 lines

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

#3 lore raw
Quoted 61 lines · Charlie Jenkins
Quoted 48 lines · Conor Dooley
Quoted 6 lines · Aleksandar Rikalo

From: Djordje Todorovic <[email protected]>

The riscv MIPS P8700 uses a different opcode for PAUSE.
It is a ‘hint’ encoding of the SLLI instruction, with rd=0, rs1=0 and imm=5. It will behave as a NOP instruction if no additional behavior beyond that of SLLI is implemented.

You say p8700, but the erratum will be applied on all systems that are identified as using a mips cpu. Why's that?

Quoted 37 lines
arch/riscv/errata/mips/errata.c new file +28 -0
+void mips_errata_patch_func(struct alt_entry *begin,
+					     struct alt_entry *end,
+					     unsigned long archid,
+					     unsigned long impid,
+					     unsigned int stage)
+{
+	struct alt_entry *alt;
+
+	BUILD_BUG_ON(ERRATA_MIPS_NUMBER >= RISCV_VENDOR_EXT_ALTERNATIVES_BASE);
+
+	if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
+		return;
+
+	for (alt = begin; alt < end; alt++) {
+		if (alt->vendor_id != MIPS_VENDOR_ID)
+			continue;
+
+		if (alt->patch_id >= ERRATA_MIPS_NUMBER) {
+			WARN(1, "MIPS errata id:%d not in kernel errata list\n",
+			     alt->patch_id);
+			continue;
+		}
+
+		mutex_lock(&text_mutex);
+		patch_text_nosync(ALT_OLD_PTR(alt), ALT_ALT_PTR(alt), alt->alt_len);
+		mutex_unlock(&text_mutex);
+	}
+}
tools/arch/riscv/include/asm/vdso/processor.h +0 -0
index 662aca039848..880f26a24f69 100644
@@ -14,7 +14,10 @@ static inline void cpu_relax(void)
 	__asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
 #endif
 
Quoted 11 lines

tools/arch/riscv/include/asm/vdso/processor.h static inline void cpu_relax(void) · line 23

Quoted 8 lines
tools/arch/riscv/include/asm/vdso/processor.h +4 -1
-#ifdef CONFIG_TOOLCHAIN_HAS_ZIHINTPAUSE
+#ifdef CONFIG_ERRATA_MIPS_P8700_PAUSE_OPCODE
+    /* MIPS P8700 pause opcode */
+    __asm__ __volatile__ (".4byte 0x00501013");
+#elif CONFIG_TOOLCHAIN_HAS_ZIHINTPAUSE
 	/*
 	 * Reduce instruction retirement.
 	 * This assumes the PC changes.

What about when the erratum is enabled and the toolchain supports Zihintpause?

Quoted 7 lines

So the other way to do this is having an hwprobe call to check if the currently running processor is effected by this. However I was concerned about the performance penalty of calling hwprobe here in the previous version so I had suggested to use a flag instead so there is not the penalty on other architectures. This does make it invalid to enable this errata in the defconfig. This is a precedent for how we want to handle errata in tools.

Gonna have to be marked non-portable then, if enabling it results in tools that might malfunction on other platforms.

Quoted 6 lines
Quoted 3 lines

Why don't you use the same implementation as the !tools copy of the header? (I'm not sure why they're different in the first place).

It is different because the headers in tools are userspace so it doesn't make sense to have alternatives.

I assume that's an answer to the first part, and not the bit in brackets since the "first place" difference is about using the .4byte versus having an ifdef/else and you're talking about alternatives. That looks to have been some sort of sync issue or oversight in 6da111574baff, no?

#4 lore raw
Quoted 77 lines · Conor Dooley
Quoted 61 lines · Charlie Jenkins
Quoted 48 lines · Conor Dooley
Quoted 6 lines · Aleksandar Rikalo

From: Djordje Todorovic <[email protected]>

The riscv MIPS P8700 uses a different opcode for PAUSE.
It is a ‘hint’ encoding of the SLLI instruction, with rd=0, rs1=0 and imm=5. It will behave as a NOP instruction if no additional behavior beyond that of SLLI is implemented.

You say p8700, but the erratum will be applied on all systems that are identified as using a mips cpu. Why's that?

Quoted 37 lines
arch/riscv/errata/mips/errata.c new file +28 -0
+void mips_errata_patch_func(struct alt_entry *begin,
+					     struct alt_entry *end,
+					     unsigned long archid,
+					     unsigned long impid,
+					     unsigned int stage)
+{
+	struct alt_entry *alt;
+
+	BUILD_BUG_ON(ERRATA_MIPS_NUMBER >= RISCV_VENDOR_EXT_ALTERNATIVES_BASE);
+
+	if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
+		return;
+
+	for (alt = begin; alt < end; alt++) {
+		if (alt->vendor_id != MIPS_VENDOR_ID)
+			continue;
+
+		if (alt->patch_id >= ERRATA_MIPS_NUMBER) {
+			WARN(1, "MIPS errata id:%d not in kernel errata list\n",
+			     alt->patch_id);
+			continue;
+		}
+
+		mutex_lock(&text_mutex);
+		patch_text_nosync(ALT_OLD_PTR(alt), ALT_ALT_PTR(alt), alt->alt_len);
+		mutex_unlock(&text_mutex);
+	}
+}
tools/arch/riscv/include/asm/vdso/processor.h +0 -0
index 662aca039848..880f26a24f69 100644
@@ -14,7 +14,10 @@ static inline void cpu_relax(void)
 	__asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
 #endif
 
Quoted 11 lines

tools/arch/riscv/include/asm/vdso/processor.h static inline void cpu_relax(void) · line 23

Quoted 8 lines
tools/arch/riscv/include/asm/vdso/processor.h +4 -1
-#ifdef CONFIG_TOOLCHAIN_HAS_ZIHINTPAUSE
+#ifdef CONFIG_ERRATA_MIPS_P8700_PAUSE_OPCODE
+    /* MIPS P8700 pause opcode */
+    __asm__ __volatile__ (".4byte 0x00501013");
+#elif CONFIG_TOOLCHAIN_HAS_ZIHINTPAUSE
 	/*
 	 * Reduce instruction retirement.
 	 * This assumes the PC changes.

What about when the erratum is enabled and the toolchain supports Zihintpause?

Quoted 7 lines

So the other way to do this is having an hwprobe call to check if the currently running processor is effected by this. However I was concerned about the performance penalty of calling hwprobe here in the previous version so I had suggested to use a flag instead so there is not the penalty on other architectures. This does make it invalid to enable this errata in the defconfig. This is a precedent for how we want to handle errata in tools.

Gonna have to be marked non-portable then, if enabling it results in tools that might malfunction on other platforms.

Quoted 4 lines
Quoted 3 lines

Why don't you use the same implementation as the !tools copy of the header? (I'm not sure why they're different in the first place).

Quoted 8 lines
Quoted 2 lines

It is different because the headers in tools are userspace so it doesn't make sense to have alternatives.

I assume that's an answer to the first part, and not the bit in brackets since the "first place" difference is about using the .4byte versus having an ifdef/else and you're talking about alternatives. That looks to have been some sort of sync issue or oversight in 6da111574baff, no?

Yeah it was a response to the first part.

As for the second part, yes it look it was a syncing issue.

- Charlie

#5 lore raw
Quoted 3 lines · Charlie Jenkins
Quoted 2 lines · Conor Dooley
Quoted 1 line · Charlie Jenkins

On Wed, Jan 29, 2025 at 04:19:58PM +0000, Conor Dooley wrote:

...

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

Why don't you use the same implementation as the !tools copy of the header? (I'm not sure why they're different in the first place).

Quoted 8 lines
Quoted 2 lines

It is different because the headers in tools are userspace so it doesn't make sense to have alternatives.

I assume that's an answer to the first part, and not the bit in brackets since the "first place" difference is about using the .4byte versus having an ifdef/else and you're talking about alternatives. That looks to have been some sort of sync issue or oversight in 6da111574baff, no?

Quoted 4 lines

Yeah it was a response to the first part.

As for the second part, yes it look it was a syncing issue.

I just forgot to update the tools copy at the time of 6da111574baff. We should first sync it (but with s/RISCV_PAUSE/0x100000f/ to avoid the need to include insn-def.h) before modifying it with this patch.

Thanks,
drew

#6 lore raw
In reply to Aleksandar Rikalo
Quoted 7 lines · Aleksandar Rikalo

From: Djordje Todorovic <[email protected]>

The riscv MIPS P8700 uses a different opcode for PAUSE.
It is a ‘hint’ encoding of the SLLI instruction, with rd=0, rs1=0 and imm=5. It will behave as a NOP instruction if no additional behavior beyond that of SLLI is implemented.

Quoted 6 lines

Add ERRATA_MIPS and ERRATA_MIPS_P8700_PAUSE_OPCODE configs. Handle errata for MIPS CPUs.

Please copy people who commented on previous versions of patches in the new versions!

- Charlie

maintainer
#7 lore raw
In reply to Aleksandar Rikalo
Quoted 6 lines · On Wed, 29 Jan 2025 05:17:03 PST (-0800), [email protected] wrote:

From: Djordje Todorovic <[email protected]>

The riscv MIPS P8700 uses a different opcode for PAUSE.
It is a ‘hint’ encoding of the SLLI instruction, with rd=0, rs1=0 and imm=5. It will behave as a NOP instruction if no additional behavior beyond that of SLLI is implemented.

We need to expose it to userspace, then, as it's a user-visible erratum -- really it's just a totally different custom extension than Zihintpause, but I don't think it really makes much of a difference what we call it.

If you have other custom stuff it's probably a good time to expose those, too, as then it'll be easier for userspace to sort out.

Quoted 297 lines

Add ERRATA_MIPS and ERRATA_MIPS_P8700_PAUSE_OPCODE configs. Handle errata for MIPS CPUs.

---
 arch/riscv/Kconfig.errata                     | 23 +++++++++++
 arch/riscv/errata/Makefile                    |  1 +
 arch/riscv/errata/mips/Makefile               |  5 +++
 arch/riscv/errata/mips/errata.c               | 41 +++++++++++++++++++
 arch/riscv/include/asm/alternative.h          |  3 ++
 arch/riscv/include/asm/cmpxchg.h              |  3 +-
 arch/riscv/include/asm/errata_list.h          | 20 ++++++++-
 arch/riscv/include/asm/vdso/processor.h       |  4 +-
 arch/riscv/include/asm/vendorid_list.h        |  1 +
 arch/riscv/kernel/alternative.c               |  5 +++
 arch/riscv/kernel/entry.S                     |  2 +
 arch/riscv/mm/init.c                          |  1 +
 tools/arch/riscv/include/asm/vdso/processor.h |  5 ++-
 13 files changed, 110 insertions(+), 4 deletions(-)
 create mode 100644 arch/riscv/errata/mips/Makefile
 create mode 100644 arch/riscv/errata/mips/errata.c
arch/riscv/Kconfig.errata +23 -0
index 2acc7d876e1f..1589528fdc30 100644
@@ -21,6 +21,29 @@ config ERRATA_ANDES_CMO
 
 	  If you don't know what to do here, say "Y".
 
+config ERRATA_MIPS
+	bool "MIPS errata"
+	depends on RISCV_ALTERNATIVE
+	help
+	  All MIPS errata Kconfig depend on this Kconfig. Disabling
+	  this Kconfig will disable all MIPS errata. Please say "Y"
+	  here if your platform uses MIPS CPU cores.
+
+	  Otherwise, please say "N" here to avoid unnecessary overhead.
+
+config ERRATA_MIPS_P8700_PAUSE_OPCODE
+	bool "Fix the PAUSE Opcode for MIPS P8700"
+	depends on ERRATA_MIPS && 64BIT
+	default n
+	help
+	   The RISCV MIPS P8700 uses a different opcode for PAUSE.
+	   It is a 'hint' encoding of the SLLI instruction,
+	   with rd=0, rs1=0 and imm=5. It will behave as a NOP
+	   instruction if no additional behavior beyond that of
+	   SLLI is implemented.
+
+	   If you are not using the P8700 processor, say n.
+
 config ERRATA_SIFIVE
 	bool "SiFive errata"
 	depends on RISCV_ALTERNATIVE
arch/riscv/errata/Makefile +1 -0
index f0da9d7b39c3..156cafb338c1 100644
@@ -9,5 +9,6 @@ endif
 endif
 
 obj-$(CONFIG_ERRATA_ANDES) += andes/
+obj-$(CONFIG_ERRATA_MIPS) += mips/
 obj-$(CONFIG_ERRATA_SIFIVE) += sifive/
 obj-$(CONFIG_ERRATA_THEAD) += thead/
arch/riscv/errata/mips/Makefile new file +5 -0
new file mode 100644
index 000000000000..6278c389b801
@@ -0,0 +1,5 @@
+ifdef CONFIG_RISCV_ALTERNATIVE_EARLY
+CFLAGS_errata.o := -mcmodel=medany
+endif
+
+obj-y += errata.o
arch/riscv/errata/mips/errata.c new file +41 -0
new file mode 100644
index 000000000000..998bbcaa50d1
@@ -0,0 +1,41 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2025 MIPS.
+ */
+
+#include <linux/memory.h>
+#include <linux/module.h>
+#include <asm/text-patching.h>
+#include <asm/alternative.h>
+#include <asm/errata_list.h>
+#include <asm/vendorid_list.h>
+#include <asm/vendor_extensions.h>
+
+void mips_errata_patch_func(struct alt_entry *begin,
+					     struct alt_entry *end,
+					     unsigned long archid,
+					     unsigned long impid,
+					     unsigned int stage)
+{
+	struct alt_entry *alt;
+
+	BUILD_BUG_ON(ERRATA_MIPS_NUMBER >= RISCV_VENDOR_EXT_ALTERNATIVES_BASE);
+
+	if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
+		return;
+
+	for (alt = begin; alt < end; alt++) {
+		if (alt->vendor_id != MIPS_VENDOR_ID)
+			continue;
+
+		if (alt->patch_id >= ERRATA_MIPS_NUMBER) {
+			WARN(1, "MIPS errata id:%d not in kernel errata list\n",
+			     alt->patch_id);
+			continue;
+		}
+
+		mutex_lock(&text_mutex);
+		patch_text_nosync(ALT_OLD_PTR(alt), ALT_ALT_PTR(alt), alt->alt_len);
+		mutex_unlock(&text_mutex);
+	}
+}
arch/riscv/include/asm/alternative.h +3 -0
index 3c2b59b25017..bc3ada8190a9 100644
@@ -48,6 +48,9 @@ struct alt_entry {
 void andes_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
 			     unsigned long archid, unsigned long impid,
 			     unsigned int stage);
+void mips_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
+			      unsigned long archid, unsigned long impid,
+			      unsigned int stage);
 void sifive_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
 			      unsigned long archid, unsigned long impid,
 			      unsigned int stage);
arch/riscv/include/asm/cmpxchg.h +2 -1
index 4cadc56220fe..2f5705f49dcc 100644
@@ -13,6 +13,7 @@
 #include <asm/hwcap.h>
 #include <asm/insn-def.h>
 #include <asm/cpufeature-macros.h>
+#include <asm/errata_list.h>
 
 #define __arch_xchg_masked(sc_sfx, swap_sfx, prepend, sc_append,		\
 			   swap_append, r, p, n)				\
@@ -404,7 +405,7 @@ static __always_inline void __cmpwait(volatile void *ptr,
 	return;
 
 no_zawrs:
-	asm volatile(RISCV_PAUSE : : : "memory");
+	ALT_RISCV_PAUSE();
 }
 
 #define __cmpwait_relaxed(ptr, val) \
arch/riscv/include/asm/errata_list.h +19 -1
index 7c8a71a526a3..68c46ccb9b7c 100644
@@ -5,7 +5,6 @@
 #ifndef ASM_ERRATA_LIST_H
 #define ASM_ERRATA_LIST_H
 
-#include <asm/alternative.h>
 #include <asm/csr.h>
 #include <asm/insn-def.h>
 #include <asm/hwcap.h>
@@ -28,6 +27,11 @@
 #define	ERRATA_THEAD_NUMBER 2
 #endif
 
+#ifdef CONFIG_ERRATA_MIPS
+#define ERRATA_MIPS_P8700_PAUSE_OPCODE 0
+#define ERRATA_MIPS_NUMBER 1
+#endif
+
 #ifdef __ASSEMBLY__
 
 #define ALT_INSN_FAULT(x)						\
@@ -58,6 +62,20 @@ asm(ALTERNATIVE("sfence.vma %0, %1", "sfence.vma", SIFIVE_VENDOR_ID,	\
 		ERRATA_SIFIVE_CIP_1200, CONFIG_ERRATA_SIFIVE_CIP_1200)	\
 		: : "r" (addr), "r" (asid) : "memory")
 
+#ifdef CONFIG_ERRATA_MIPS_P8700_PAUSE_OPCODE
+#define ALT_RISCV_PAUSE()                                               \
+asm volatile(ALTERNATIVE(                                               \
+    RISCV_PAUSE,                /* Original RISC‑V pause insn */        \
+    ".4byte 0x00501013",        /* Replacement for MIPS P8700 */        \
+    MIPS_VENDOR_ID,             /* Vendor ID to match */                \
+    ERRATA_MIPS_P8700_PAUSE_OPCODE, /* patch_id */                      \
+    CONFIG_ERRATA_MIPS_P8700_PAUSE_OPCODE)                              \
+    : /* no outputs */ : /* no inputs */ : "memory")
+#else
+#define ALT_RISCV_PAUSE() \
+    asm volatile(RISCV_PAUSE : : : "memory")
+#endif
+
 /*
  * _val is marked as "will be overwritten", so need to set it to 0
  * in the default case.
arch/riscv/include/asm/vdso/processor.h +3 -1
index 8f383f05a290..8f749552ecfe 100644
@@ -5,6 +5,8 @@
 #ifndef __ASSEMBLY__
 
 #include <asm/barrier.h>
+
+#include <asm/errata_list.h>
 #include <asm/insn-def.h>
 
 static inline void cpu_relax(void)
@@ -19,7 +21,7 @@ static inline void cpu_relax(void)
 	 * Reduce instruction retirement.
 	 * This assumes the PC changes.
 	 */
-	__asm__ __volatile__ (RISCV_PAUSE);
+	ALT_RISCV_PAUSE();
 	barrier();
 }
 
arch/riscv/include/asm/vendorid_list.h +1 -0
index a5150cdf34d8..3b09874d7a6d 100644
@@ -9,5 +9,6 @@
 #define MICROCHIP_VENDOR_ID	0x029
 #define SIFIVE_VENDOR_ID	0x489
 #define THEAD_VENDOR_ID		0x5b7
+#define MIPS_VENDOR_ID		0x722
 
 #endif
arch/riscv/kernel/alternative.c +5 -0
index 7eb3cb1215c6..7642704c7f18 100644
@@ -47,6 +47,11 @@ static void riscv_fill_cpu_mfr_info(struct cpu_manufacturer_info_t *cpu_mfr_info
 		cpu_mfr_info->patch_func = andes_errata_patch_func;
 		break;
 #endif
+#ifdef CONFIG_ERRATA_MIPS
+	case MIPS_VENDOR_ID:
+		cpu_mfr_info->patch_func = mips_errata_patch_func;
+		break;
+#endif
 #ifdef CONFIG_ERRATA_SIFIVE
 	case SIFIVE_VENDOR_ID:
 		cpu_mfr_info->patch_func = sifive_errata_patch_func;
arch/riscv/kernel/entry.S +2 -0
index 33a5a9f2a0d4..2007d9de7f27 100644
@@ -15,6 +15,8 @@
 #include <asm/thread_info.h>
 #include <asm/asm-offsets.h>
 #include <asm/errata_list.h>
+#include <asm/alternative.h>
+
 #include <linux/sizes.h>
 
 	.section .irqentry.text, "ax"
arch/riscv/mm/init.c +1 -0
index 722178ae3488..ee8a2c5b40f8 100644
@@ -35,6 +35,7 @@
 #include <asm/soc.h>
 #include <asm/sparsemem.h>
 #include <asm/tlbflush.h>
+#include <asm/alternative.h>
 
 #include "../kernel/head.h"
 
tools/arch/riscv/include/asm/vdso/processor.h +4 -1
index 662aca039848..880f26a24f69 100644
@@ -14,7 +14,10 @@ static inline void cpu_relax(void)
 	__asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
 #endif
 
-#ifdef CONFIG_TOOLCHAIN_HAS_ZIHINTPAUSE
+#ifdef CONFIG_ERRATA_MIPS_P8700_PAUSE_OPCODE
+    /* MIPS P8700 pause opcode */
+    __asm__ __volatile__ (".4byte 0x00501013");
+#elif CONFIG_TOOLCHAIN_HAS_ZIHINTPAUSE
 	/*
 	 * Reduce instruction retirement.
 	 * This assumes the PC changes.
#8 lore raw

HTEC Public

Hi everyone,

Thank you a lot for your comments.
I am wondering how we should proceed from here.

Best,
Djordje

________________________________________
From: Palmer Dabbelt <[email protected]>
Sent: Wednesday, February 5, 2025 4:44 PM
To: [email protected] <[email protected]>
Cc: [email protected] <[email protected]>; Paul Walmsley <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; Djordje Todorovic <[email protected]>
Subject: Re: [PATCH v3] riscv: Fix the PAUSE Opcode for MIPS P8700.

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.

Quoted 6 lines · On Wed, 29 Jan 2025 05:17:03 PST (-0800), [email protected] wrote:

From: Djordje Todorovic <[email protected]>

The riscv MIPS P8700 uses a different opcode for PAUSE.
It is a ‘hint’ encoding of the SLLI instruction, with rd=0, rs1=0 and imm=5. It will behave as a NOP instruction if no additional behavior beyond that of SLLI is implemented.

We need to expose it to userspace, then, as it's a user-visible erratum -- really it's just a totally different custom extension than Zihintpause, but I don't think it really makes much of a difference what we call it.

If you have other custom stuff it's probably a good time to expose those, too, as then it'll be easier for userspace to sort out.

Quoted 297 lines

Add ERRATA_MIPS and ERRATA_MIPS_P8700_PAUSE_OPCODE configs. Handle errata for MIPS CPUs.

---
 arch/riscv/Kconfig.errata                     | 23 +++++++++++
 arch/riscv/errata/Makefile                    |  1 +
 arch/riscv/errata/mips/Makefile               |  5 +++
 arch/riscv/errata/mips/errata.c               | 41 +++++++++++++++++++
 arch/riscv/include/asm/alternative.h          |  3 ++
 arch/riscv/include/asm/cmpxchg.h              |  3 +-
 arch/riscv/include/asm/errata_list.h          | 20 ++++++++-
 arch/riscv/include/asm/vdso/processor.h       |  4 +-
 arch/riscv/include/asm/vendorid_list.h        |  1 +
 arch/riscv/kernel/alternative.c               |  5 +++
 arch/riscv/kernel/entry.S                     |  2 +
 arch/riscv/mm/init.c                          |  1 +
 tools/arch/riscv/include/asm/vdso/processor.h |  5 ++-
 13 files changed, 110 insertions(+), 4 deletions(-)
 create mode 100644 arch/riscv/errata/mips/Makefile
 create mode 100644 arch/riscv/errata/mips/errata.c
arch/riscv/Kconfig.errata +23 -0
index 2acc7d876e1f..1589528fdc30 100644
@@ -21,6 +21,29 @@ config ERRATA_ANDES_CMO
 
        If you don't know what to do here, say "Y".
 
+config ERRATA_MIPS
+     bool "MIPS errata"
+     depends on RISCV_ALTERNATIVE
+     help
+       All MIPS errata Kconfig depend on this Kconfig. Disabling
+       this Kconfig will disable all MIPS errata. Please say "Y"
+       here if your platform uses MIPS CPU cores.
+
+       Otherwise, please say "N" here to avoid unnecessary overhead.
+
+config ERRATA_MIPS_P8700_PAUSE_OPCODE
+     bool "Fix the PAUSE Opcode for MIPS P8700"
+     depends on ERRATA_MIPS && 64BIT
+     default n
+     help
+        The RISCV MIPS P8700 uses a different opcode for PAUSE.
+        It is a 'hint' encoding of the SLLI instruction,
+        with rd=0, rs1=0 and imm=5. It will behave as a NOP
+        instruction if no additional behavior beyond that of
+        SLLI is implemented.
+
+        If you are not using the P8700 processor, say n.
+
 config ERRATA_SIFIVE
      bool "SiFive errata"
      depends on RISCV_ALTERNATIVE
arch/riscv/errata/Makefile +1 -0
index f0da9d7b39c3..156cafb338c1 100644
@@ -9,5 +9,6 @@ endif
 endif
 
 obj-$(CONFIG_ERRATA_ANDES) += andes/
+obj-$(CONFIG_ERRATA_MIPS) += mips/
 obj-$(CONFIG_ERRATA_SIFIVE) += sifive/
 obj-$(CONFIG_ERRATA_THEAD) += thead/
arch/riscv/errata/mips/Makefile new file +5 -0
new file mode 100644
index 000000000000..6278c389b801
@@ -0,0 +1,5 @@
+ifdef CONFIG_RISCV_ALTERNATIVE_EARLY
+CFLAGS_errata.o := -mcmodel=medany
+endif
+
+obj-y += errata.o
arch/riscv/errata/mips/errata.c new file +41 -0
new file mode 100644
index 000000000000..998bbcaa50d1
@@ -0,0 +1,41 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2025 MIPS.
+ */
+
+#include <linux/memory.h>
+#include <linux/module.h>
+#include <asm/text-patching.h>
+#include <asm/alternative.h>
+#include <asm/errata_list.h>
+#include <asm/vendorid_list.h>
+#include <asm/vendor_extensions.h>
+
+void mips_errata_patch_func(struct alt_entry *begin,
+                                          struct alt_entry *end,
+                                          unsigned long archid,
+                                          unsigned long impid,
+                                          unsigned int stage)
+{
+     struct alt_entry *alt;
+
+     BUILD_BUG_ON(ERRATA_MIPS_NUMBER >= RISCV_VENDOR_EXT_ALTERNATIVES_BASE);
+
+     if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
+             return;
+
+     for (alt = begin; alt < end; alt++) {
+             if (alt->vendor_id != MIPS_VENDOR_ID)
+                     continue;
+
+             if (alt->patch_id >= ERRATA_MIPS_NUMBER) {
+                     WARN(1, "MIPS errata id:%d not in kernel errata list\n",
+                          alt->patch_id);
+                     continue;
+             }
+
+             mutex_lock(&text_mutex);
+             patch_text_nosync(ALT_OLD_PTR(alt), ALT_ALT_PTR(alt), alt->alt_len);
+             mutex_unlock(&text_mutex);
+     }
+}
arch/riscv/include/asm/alternative.h +3 -0
index 3c2b59b25017..bc3ada8190a9 100644
@@ -48,6 +48,9 @@ struct alt_entry {
 void andes_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
                           unsigned long archid, unsigned long impid,
                           unsigned int stage);
+void mips_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
+                           unsigned long archid, unsigned long impid,
+                           unsigned int stage);
 void sifive_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
                            unsigned long archid, unsigned long impid,
                            unsigned int stage);
arch/riscv/include/asm/cmpxchg.h +2 -1
index 4cadc56220fe..2f5705f49dcc 100644
@@ -13,6 +13,7 @@
 #include <asm/hwcap.h>
 #include <asm/insn-def.h>
 #include <asm/cpufeature-macros.h>
+#include <asm/errata_list.h>
 
 #define __arch_xchg_masked(sc_sfx, swap_sfx, prepend, sc_append,             \
                         swap_append, r, p, n)                                \
@@ -404,7 +405,7 @@ static __always_inline void __cmpwait(volatile void *ptr,
      return;
 
 no_zawrs:
-     asm volatile(RISCV_PAUSE : : : "memory");
+     ALT_RISCV_PAUSE();
 }
 
 #define __cmpwait_relaxed(ptr, val) \
arch/riscv/include/asm/errata_list.h +19 -1
index 7c8a71a526a3..68c46ccb9b7c 100644
@@ -5,7 +5,6 @@
 #ifndef ASM_ERRATA_LIST_H
 #define ASM_ERRATA_LIST_H
 
-#include <asm/alternative.h>
 #include <asm/csr.h>
 #include <asm/insn-def.h>
 #include <asm/hwcap.h>
@@ -28,6 +27,11 @@
 #define      ERRATA_THEAD_NUMBER 2
 #endif
 
+#ifdef CONFIG_ERRATA_MIPS
+#define ERRATA_MIPS_P8700_PAUSE_OPCODE 0
+#define ERRATA_MIPS_NUMBER 1
+#endif
+
 #ifdef __ASSEMBLY__
 
 #define ALT_INSN_FAULT(x)                                            \
@@ -58,6 +62,20 @@ asm(ALTERNATIVE("sfence.vma %0, %1", "sfence.vma", SIFIVE_VENDOR_ID,       \
              ERRATA_SIFIVE_CIP_1200, CONFIG_ERRATA_SIFIVE_CIP_1200)  \
              : : "r" (addr), "r" (asid) : "memory")
 
+#ifdef CONFIG_ERRATA_MIPS_P8700_PAUSE_OPCODE
+#define ALT_RISCV_PAUSE()                                               \
+asm volatile(ALTERNATIVE(                                               \
+    RISCV_PAUSE,                /* Original RISC‑V pause insn */        \
+    ".4byte 0x00501013",        /* Replacement for MIPS P8700 */        \
+    MIPS_VENDOR_ID,             /* Vendor ID to match */                \
+    ERRATA_MIPS_P8700_PAUSE_OPCODE, /* patch_id */                      \
+    CONFIG_ERRATA_MIPS_P8700_PAUSE_OPCODE)                              \
+    : /* no outputs */ : /* no inputs */ : "memory")
+#else
+#define ALT_RISCV_PAUSE() \
+    asm volatile(RISCV_PAUSE : : : "memory")
+#endif
+
 /*
  * _val is marked as "will be overwritten", so need to set it to 0
  * in the default case.
arch/riscv/include/asm/vdso/processor.h +3 -1
index 8f383f05a290..8f749552ecfe 100644
@@ -5,6 +5,8 @@
 #ifndef __ASSEMBLY__
 
 #include <asm/barrier.h>
+
+#include <asm/errata_list.h>
 #include <asm/insn-def.h>
 
 static inline void cpu_relax(void)
@@ -19,7 +21,7 @@ static inline void cpu_relax(void)
       * Reduce instruction retirement.
       * This assumes the PC changes.
       */
-     __asm__ __volatile__ (RISCV_PAUSE);
+     ALT_RISCV_PAUSE();
      barrier();
 }
 
arch/riscv/include/asm/vendorid_list.h +1 -0
index a5150cdf34d8..3b09874d7a6d 100644
@@ -9,5 +9,6 @@
 #define MICROCHIP_VENDOR_ID  0x029
 #define SIFIVE_VENDOR_ID     0x489
 #define THEAD_VENDOR_ID              0x5b7
+#define MIPS_VENDOR_ID               0x722
 
 #endif
arch/riscv/kernel/alternative.c +5 -0
index 7eb3cb1215c6..7642704c7f18 100644
@@ -47,6 +47,11 @@ static void riscv_fill_cpu_mfr_info(struct cpu_manufacturer_info_t *cpu_mfr_info
              cpu_mfr_info->patch_func = andes_errata_patch_func;
              break;
 #endif
+#ifdef CONFIG_ERRATA_MIPS
+     case MIPS_VENDOR_ID:
+             cpu_mfr_info->patch_func = mips_errata_patch_func;
+             break;
+#endif
 #ifdef CONFIG_ERRATA_SIFIVE
      case SIFIVE_VENDOR_ID:
              cpu_mfr_info->patch_func = sifive_errata_patch_func;
arch/riscv/kernel/entry.S +2 -0
index 33a5a9f2a0d4..2007d9de7f27 100644
@@ -15,6 +15,8 @@
 #include <asm/thread_info.h>
 #include <asm/asm-offsets.h>
 #include <asm/errata_list.h>
+#include <asm/alternative.h>
+
 #include <linux/sizes.h>
 
      .section .irqentry.text, "ax"
arch/riscv/mm/init.c +1 -0
index 722178ae3488..ee8a2c5b40f8 100644
@@ -35,6 +35,7 @@
 #include <asm/soc.h>
 #include <asm/sparsemem.h>
 #include <asm/tlbflush.h>
+#include <asm/alternative.h>
 
 #include "../kernel/head.h"
 
tools/arch/riscv/include/asm/vdso/processor.h +4 -1
index 662aca039848..880f26a24f69 100644
@@ -14,7 +14,10 @@ static inline void cpu_relax(void)
      __asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
 #endif
 
-#ifdef CONFIG_TOOLCHAIN_HAS_ZIHINTPAUSE
+#ifdef CONFIG_ERRATA_MIPS_P8700_PAUSE_OPCODE
+    /* MIPS P8700 pause opcode */
+    __asm__ __volatile__ (".4byte 0x00501013");
+#elif CONFIG_TOOLCHAIN_HAS_ZIHINTPAUSE
      /*
       * Reduce instruction retirement.
       * This assumes the PC changes.
#9 lore raw

Hi Djordje,

Quoted 6 lines · Djordje Todorovic

HTEC Public

Hi everyone,

Thank you a lot for your comments.
I am wondering how we should proceed from here.

I don't think there is a requirement for exposing this new encoding to userspace, you just need to make sure that Zihintpause is not advertised though.

So either you expose a new vendor extension to userspace so that the difference in the encoding can be dealt with or you make sure you don't expose Zihintpause.

Thanks,

Alex

Quoted 332 lines

Best,
Djordje

________________________________________
From: Palmer Dabbelt <[email protected]>
Sent: Wednesday, February 5, 2025 4:44 PM
To: [email protected] <[email protected]>
Cc: [email protected] <[email protected]>; Paul Walmsley <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; Djordje Todorovic <[email protected]>
Subject: Re: [PATCH v3] riscv: Fix the PAUSE Opcode for MIPS P8700.

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.

Quoted 6 lines · On Wed, 29 Jan 2025 05:17:03 PST (-0800), [email protected] wrote:

From: Djordje Todorovic <[email protected]>

The riscv MIPS P8700 uses a different opcode for PAUSE.
It is a ‘hint’ encoding of the SLLI instruction, with rd=0, rs1=0 and imm=5. It will behave as a NOP instruction if no additional behavior beyond that of SLLI is implemented.

We need to expose it to userspace, then, as it's a user-visible erratum -- really it's just a totally different custom extension than Zihintpause, but I don't think it really makes much of a difference what we call it.

If you have other custom stuff it's probably a good time to expose those, too, as then it'll be easier for userspace to sort out.

Quoted 287 lines

Add ERRATA_MIPS and ERRATA_MIPS_P8700_PAUSE_OPCODE configs. Handle errata for MIPS CPUs.

---
  arch/riscv/Kconfig.errata                     | 23 +++++++++++
  arch/riscv/errata/Makefile                    |  1 +
  arch/riscv/errata/mips/Makefile               |  5 +++
  arch/riscv/errata/mips/errata.c               | 41 +++++++++++++++++++
  arch/riscv/include/asm/alternative.h          |  3 ++
  arch/riscv/include/asm/cmpxchg.h              |  3 +-
  arch/riscv/include/asm/errata_list.h          | 20 ++++++++-
  arch/riscv/include/asm/vdso/processor.h       |  4 +-
  arch/riscv/include/asm/vendorid_list.h        |  1 +
  arch/riscv/kernel/alternative.c               |  5 +++
  arch/riscv/kernel/entry.S                     |  2 +
  arch/riscv/mm/init.c                          |  1 +
  tools/arch/riscv/include/asm/vdso/processor.h |  5 ++-
  13 files changed, 110 insertions(+), 4 deletions(-)

create mode 100644 arch/riscv/errata/mips/Makefile create mode 100644 arch/riscv/errata/mips/errata.c

arch/riscv/Kconfig.errata +23 -0
index 2acc7d876e1f..1589528fdc30 100644
@@ -21,6 +21,29 @@ config ERRATA_ANDES_CMO
 
         If you don't know what to do here, say "Y".
 
+config ERRATA_MIPS
+     bool "MIPS errata"
+     depends on RISCV_ALTERNATIVE
+     help
+       All MIPS errata Kconfig depend on this Kconfig. Disabling
+       this Kconfig will disable all MIPS errata. Please say "Y"
+       here if your platform uses MIPS CPU cores.
+
+       Otherwise, please say "N" here to avoid unnecessary overhead.
+
+config ERRATA_MIPS_P8700_PAUSE_OPCODE
+     bool "Fix the PAUSE Opcode for MIPS P8700"
+     depends on ERRATA_MIPS && 64BIT
+     default n
+     help
+        The RISCV MIPS P8700 uses a different opcode for PAUSE.
+        It is a 'hint' encoding of the SLLI instruction,
+        with rd=0, rs1=0 and imm=5. It will behave as a NOP
+        instruction if no additional behavior beyond that of
+        SLLI is implemented.
+
+        If you are not using the P8700 processor, say n.
+
  config ERRATA_SIFIVE
       bool "SiFive errata"
       depends on RISCV_ALTERNATIVE
arch/riscv/errata/Makefile +1 -0
index f0da9d7b39c3..156cafb338c1 100644
@@ -9,5 +9,6 @@ endif
  endif
 
  obj-$(CONFIG_ERRATA_ANDES) += andes/
+obj-$(CONFIG_ERRATA_MIPS) += mips/
  obj-$(CONFIG_ERRATA_SIFIVE) += sifive/
  obj-$(CONFIG_ERRATA_THEAD) += thead/
arch/riscv/errata/mips/Makefile new file +5 -0
new file mode 100644
index 000000000000..6278c389b801
@@ -0,0 +1,5 @@
+ifdef CONFIG_RISCV_ALTERNATIVE_EARLY
+CFLAGS_errata.o := -mcmodel=medany
+endif
+
+obj-y += errata.o
arch/riscv/errata/mips/errata.c new file +41 -0
new file mode 100644
index 000000000000..998bbcaa50d1
@@ -0,0 +1,41 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2025 MIPS.
+ */
+
+#include <linux/memory.h>
+#include <linux/module.h>
+#include <asm/text-patching.h>
+#include <asm/alternative.h>
+#include <asm/errata_list.h>
+#include <asm/vendorid_list.h>
+#include <asm/vendor_extensions.h>
+
+void mips_errata_patch_func(struct alt_entry *begin,
+                                          struct alt_entry *end,
+                                          unsigned long archid,
+                                          unsigned long impid,
+                                          unsigned int stage)
+{
+     struct alt_entry *alt;
+
+     BUILD_BUG_ON(ERRATA_MIPS_NUMBER >= RISCV_VENDOR_EXT_ALTERNATIVES_BASE);
+
+     if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
+             return;
+
+     for (alt = begin; alt < end; alt++) {
+             if (alt->vendor_id != MIPS_VENDOR_ID)
+                     continue;
+
+             if (alt->patch_id >= ERRATA_MIPS_NUMBER) {
+                     WARN(1, "MIPS errata id:%d not in kernel errata list\n",
+                          alt->patch_id);
+                     continue;
+             }
+
+             mutex_lock(&text_mutex);
+             patch_text_nosync(ALT_OLD_PTR(alt), ALT_ALT_PTR(alt), alt->alt_len);
+             mutex_unlock(&text_mutex);
+     }
+}
arch/riscv/include/asm/alternative.h +3 -0
index 3c2b59b25017..bc3ada8190a9 100644
@@ -48,6 +48,9 @@ struct alt_entry {
  void andes_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
                            unsigned long archid, unsigned long impid,
                            unsigned int stage);
+void mips_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
+                           unsigned long archid, unsigned long impid,
+                           unsigned int stage);
  void sifive_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
                             unsigned long archid, unsigned long impid,
                             unsigned int stage);
arch/riscv/include/asm/cmpxchg.h +2 -1
index 4cadc56220fe..2f5705f49dcc 100644
@@ -13,6 +13,7 @@
  #include <asm/hwcap.h>
  #include <asm/insn-def.h>
  #include <asm/cpufeature-macros.h>
+#include <asm/errata_list.h>
 
  #define __arch_xchg_masked(sc_sfx, swap_sfx, prepend, sc_append,             \
                          swap_append, r, p, n)                                \
@@ -404,7 +405,7 @@ static __always_inline void __cmpwait(volatile void *ptr,
       return;
 
  no_zawrs:
-     asm volatile(RISCV_PAUSE : : : "memory");
+     ALT_RISCV_PAUSE();
  }
 
  #define __cmpwait_relaxed(ptr, val) \
arch/riscv/include/asm/errata_list.h +19 -1
index 7c8a71a526a3..68c46ccb9b7c 100644
@@ -5,7 +5,6 @@
  #ifndef ASM_ERRATA_LIST_H
  #define ASM_ERRATA_LIST_H
 
-#include <asm/alternative.h>
  #include <asm/csr.h>
  #include <asm/insn-def.h>
  #include <asm/hwcap.h>
@@ -28,6 +27,11 @@
  #define      ERRATA_THEAD_NUMBER 2
  #endif
 
+#ifdef CONFIG_ERRATA_MIPS
+#define ERRATA_MIPS_P8700_PAUSE_OPCODE 0
+#define ERRATA_MIPS_NUMBER 1
+#endif
+
  #ifdef __ASSEMBLY__
 
  #define ALT_INSN_FAULT(x)                                            \
@@ -58,6 +62,20 @@ asm(ALTERNATIVE("sfence.vma %0, %1", "sfence.vma", SIFIVE_VENDOR_ID,       \
               ERRATA_SIFIVE_CIP_1200, CONFIG_ERRATA_SIFIVE_CIP_1200)  \
               : : "r" (addr), "r" (asid) : "memory")
 
+#ifdef CONFIG_ERRATA_MIPS_P8700_PAUSE_OPCODE
+#define ALT_RISCV_PAUSE()                                               \
+asm volatile(ALTERNATIVE(                                               \
+    RISCV_PAUSE,                /* Original RISC‑V pause insn */        \
+    ".4byte 0x00501013",        /* Replacement for MIPS P8700 */        \
+    MIPS_VENDOR_ID,             /* Vendor ID to match */                \
+    ERRATA_MIPS_P8700_PAUSE_OPCODE, /* patch_id */                      \
+    CONFIG_ERRATA_MIPS_P8700_PAUSE_OPCODE)                              \
+    : /* no outputs */ : /* no inputs */ : "memory")
+#else
+#define ALT_RISCV_PAUSE() \
+    asm volatile(RISCV_PAUSE : : : "memory")
+#endif
+
  /*
   * _val is marked as "will be overwritten", so need to set it to 0
   * in the default case.
arch/riscv/include/asm/vdso/processor.h +3 -1
index 8f383f05a290..8f749552ecfe 100644
@@ -5,6 +5,8 @@
  #ifndef __ASSEMBLY__
 
  #include <asm/barrier.h>
+
+#include <asm/errata_list.h>
  #include <asm/insn-def.h>
 
  static inline void cpu_relax(void)
@@ -19,7 +21,7 @@ static inline void cpu_relax(void)
        * Reduce instruction retirement.
        * This assumes the PC changes.
        */
-     __asm__ __volatile__ (RISCV_PAUSE);
+     ALT_RISCV_PAUSE();
       barrier();
  }
 
arch/riscv/include/asm/vendorid_list.h +1 -0
index a5150cdf34d8..3b09874d7a6d 100644
@@ -9,5 +9,6 @@
  #define MICROCHIP_VENDOR_ID  0x029
  #define SIFIVE_VENDOR_ID     0x489
  #define THEAD_VENDOR_ID              0x5b7
+#define MIPS_VENDOR_ID               0x722
 
  #endif
arch/riscv/kernel/alternative.c +5 -0
index 7eb3cb1215c6..7642704c7f18 100644
@@ -47,6 +47,11 @@ static void riscv_fill_cpu_mfr_info(struct cpu_manufacturer_info_t *cpu_mfr_info
               cpu_mfr_info->patch_func = andes_errata_patch_func;
               break;
  #endif
+#ifdef CONFIG_ERRATA_MIPS
+     case MIPS_VENDOR_ID:
+             cpu_mfr_info->patch_func = mips_errata_patch_func;
+             break;
+#endif
  #ifdef CONFIG_ERRATA_SIFIVE
       case SIFIVE_VENDOR_ID:
               cpu_mfr_info->patch_func = sifive_errata_patch_func;
arch/riscv/kernel/entry.S +2 -0
index 33a5a9f2a0d4..2007d9de7f27 100644
@@ -15,6 +15,8 @@
  #include <asm/thread_info.h>
  #include <asm/asm-offsets.h>
  #include <asm/errata_list.h>
+#include <asm/alternative.h>
+
  #include <linux/sizes.h>
 
       .section .irqentry.text, "ax"
arch/riscv/mm/init.c +1 -0
index 722178ae3488..ee8a2c5b40f8 100644
@@ -35,6 +35,7 @@
  #include <asm/soc.h>
  #include <asm/sparsemem.h>
  #include <asm/tlbflush.h>
+#include <asm/alternative.h>
 
  #include "../kernel/head.h"
 
tools/arch/riscv/include/asm/vdso/processor.h +0 -0
index 662aca039848..880f26a24f69 100644
@@ -14,7 +14,10 @@ static inline void cpu_relax(void)
       __asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));

tools/arch/riscv/include/asm/vdso/processor.h static inline void cpu_relax(void) · line 23

Quoted 10 lines
tools/arch/riscv/include/asm/vdso/processor.h +4 -1
  #endif
 
-#ifdef CONFIG_TOOLCHAIN_HAS_ZIHINTPAUSE
+#ifdef CONFIG_ERRATA_MIPS_P8700_PAUSE_OPCODE
+    /* MIPS P8700 pause opcode */
+    __asm__ __volatile__ (".4byte 0x00501013");
+#elif CONFIG_TOOLCHAIN_HAS_ZIHINTPAUSE
       /*
        * Reduce instruction retirement.
        * This assumes the PC changes.

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