From patchwork Wed Jan 29 13:17:03 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Aleksandar Rikalo X-Patchwork-Id: 13953761 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 27280C0218D for ; Wed, 29 Jan 2025 13:17:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=nMP4R0OjJGZLKcSOw1SmMyfV74/6/kU9/75HENjV1Ns=; b=cnhMhTpyRj8K0v jlxd50zyVtn1iyGu6M/MzraLUjzGaAp0cgspopXjhuEjiBlDIwf4jGxOoG5tpn+BlAksAPi+4HLbu G/HOPv8rY6cFURRvmjvHus9DfMdsqKbcfwii9TBilTwBOHggISCQ9wINju0Yb5lFL3Ka5Jd/Qo43B C+rJ5ntIO+O4pAyOVyqXi8b9SZMDbmub7AdHaNwl1+hPJr5OLgQrph2oQOd2PMGCj+t9pgAN8ZFSG FSmqSYjzoaLMBvPJGCYUl3BLtNS5ZAbyXQt9pOPcqrP79ggmaLvDYsnjmFMrS7KrTJGg6kF9kSego UlUQ5ys1vS4onYeBzeVw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1td7wr-00000006zsO-1URw; Wed, 29 Jan 2025 13:17:41 +0000 Received: from mail-ed1-x532.google.com ([2a00:1450:4864:20::532]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1td7wK-00000006zld-0pXB for linux-riscv@lists.infradead.org; Wed, 29 Jan 2025 13:17:09 +0000 Received: by mail-ed1-x532.google.com with SMTP id 4fb4d7f45d1cf-5dc0522475eso13242288a12.1 for ; Wed, 29 Jan 2025 05:17:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1738156626; x=1738761426; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=FrzHi7EcBna9oszkhdrvxCzw/It0oCRqn6cqPoPKlzA=; b=Zw7hZ4HJDWwDallDDvsCmkt1/5sdQcDh0liJLjFs6S9RjShwTDQ/HsuQg5aP9lHmnE xf7Aapun4YasuW5rf5TnsC2/yTk/3kO6NGuFqcumDOASpp8AQk1DnAXraLRzQcKqDn6z OSK0vFFG6jHeZkpklZcYv8kvjt+svhwMtaWJhJXuHFN1idAbRJFjbVaCpYQgv6uAbakj mW7kg25yPxd6Rm4wiGodGkXOhDB5SlX44x0r1xXMqrDzdGgSx34c24y77Tw6hV1aPfXJ B2Jr/5WUqCKIkZFdNbhDK+g5wj6Ey0Q+VwKECeT9KTSXajJZz83ndvt6pYor+MFkSvUn E+tA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738156626; x=1738761426; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=FrzHi7EcBna9oszkhdrvxCzw/It0oCRqn6cqPoPKlzA=; b=c9MNZkWhf/2TquWpUTDHTrRbYYYAngC22QJa4KmbCVbMd8ZT8lyynwump671UHmThv rGtKQ3v4VEm1m3Ci0z7tGCsUcNBMKrccWlMVDz+vg8e+t6C8SMpcQd5iW09mAyC2oEw/ aoQKkBdSF5lhTwdQwvKsRg0cgPxTg1DI3RTX6X6ngPSC8BHMbtmqicXWZyJc6sj8NvWD k8qsNfyacalttUfgkADNlDkUriUqEoop5JmvUALa1dEmM/Ik5csGzBZItFsCScv/TluW mbXUT3UOsuSlURqwYSj6e0aNZjPYgeh1MKTb2WKG4sQYGBdhJj89dH4gM4tHM3D8Di1N FF6w== X-Gm-Message-State: AOJu0YwQbsdEwG83GkW0JK70b8VUJHRUYZPlvVJR/ht/P+FmQKu/rG6P 2UC92DXqTpqHH9Iwb8eWqBomfX7/9jh+RbDcjFK4f0LTRL5IoOoPOE2DipIr X-Gm-Gg: ASbGncvpN2867jHBfnnletvctc5erpuneqppy0NxZOZ4By9qgWtCR+qOaU+6hSdCG0l QTOFLnmtVyShJsEPe78dEp9Ppp8Keb/sp5mK8o3Jcc0gU2eCxQ9mrZPPY5yKVjE29RxnG7vrYfO LEhN/G0ac1FxlZe1ssBos/ZDuv+5tNuthWyHp84bIrOjZmDi/MpXdgPvaMzG62xhvTMk1ZGMkTt UfefIvubWxkkrR4yiyLA62j0O10uBTnklOEilHoo5MVIhy5S0VnV8ME+ZdlUs2R8mZWv3aCfwRu W0r8gLnI5WistuSs/q9CO/37 X-Google-Smtp-Source: AGHT+IE/JeaMlECCj6vgX8ISzQ+c9qstL/zmPISYVXw2GilRdHKOw9QquXTAzMnJOGnCk7G48Lpbhg== X-Received: by 2002:a17:907:9711:b0:ab3:47c8:d3c6 with SMTP id a640c23a62f3a-ab6cfcca024mr261771966b.11.1738156625302; Wed, 29 Jan 2025 05:17:05 -0800 (PST) Received: from localhost.localdomain ([79.175.114.8]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ab675e6299dsm956189466b.47.2025.01.29.05.17.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Jan 2025 05:17:04 -0800 (PST) From: Aleksandar Rikalo To: linux-riscv@lists.infradead.org Cc: Paul Walmsley , Palmer Dabbelt , Albert Ou , Andrew Jones , =?utf-8?q?Christoph_M=C3=BCllner?= , Aleksandar Rikalo , linux-kernel@vger.kernel.org, Djordje Todorovic Subject: [PATCH v3] riscv: Fix the PAUSE Opcode for MIPS P8700. Date: Wed, 29 Jan 2025 14:17:03 +0100 Message-Id: <20250129131703.733098-1-arikalo@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250129_051708_249443_C08838F6 X-CRM114-Status: GOOD ( 25.40 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Djordje Todorovic 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. Signed-off-by: Djordje Todorovic Signed-off-by: Aleksandar Rikalo Signed-off-by: Raj Vishwanathan4 --- 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 diff --git a/arch/riscv/Kconfig.errata b/arch/riscv/Kconfig.errata index 2acc7d876e1f..1589528fdc30 100644 --- a/arch/riscv/Kconfig.errata +++ b/arch/riscv/Kconfig.errata @@ -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 diff --git a/arch/riscv/errata/Makefile b/arch/riscv/errata/Makefile index f0da9d7b39c3..156cafb338c1 100644 --- a/arch/riscv/errata/Makefile +++ b/arch/riscv/errata/Makefile @@ -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/ diff --git a/arch/riscv/errata/mips/Makefile b/arch/riscv/errata/mips/Makefile new file mode 100644 index 000000000000..6278c389b801 --- /dev/null +++ b/arch/riscv/errata/mips/Makefile @@ -0,0 +1,5 @@ +ifdef CONFIG_RISCV_ALTERNATIVE_EARLY +CFLAGS_errata.o := -mcmodel=medany +endif + +obj-y += errata.o diff --git a/arch/riscv/errata/mips/errata.c b/arch/riscv/errata/mips/errata.c new file mode 100644 index 000000000000..998bbcaa50d1 --- /dev/null +++ b/arch/riscv/errata/mips/errata.c @@ -0,0 +1,41 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2025 MIPS. + */ + +#include +#include +#include +#include +#include +#include +#include + +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); + } +} diff --git a/arch/riscv/include/asm/alternative.h b/arch/riscv/include/asm/alternative.h index 3c2b59b25017..bc3ada8190a9 100644 --- a/arch/riscv/include/asm/alternative.h +++ b/arch/riscv/include/asm/alternative.h @@ -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); diff --git a/arch/riscv/include/asm/cmpxchg.h b/arch/riscv/include/asm/cmpxchg.h index 4cadc56220fe..2f5705f49dcc 100644 --- a/arch/riscv/include/asm/cmpxchg.h +++ b/arch/riscv/include/asm/cmpxchg.h @@ -13,6 +13,7 @@ #include #include #include +#include #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) \ diff --git a/arch/riscv/include/asm/errata_list.h b/arch/riscv/include/asm/errata_list.h index 7c8a71a526a3..68c46ccb9b7c 100644 --- a/arch/riscv/include/asm/errata_list.h +++ b/arch/riscv/include/asm/errata_list.h @@ -5,7 +5,6 @@ #ifndef ASM_ERRATA_LIST_H #define ASM_ERRATA_LIST_H -#include #include #include #include @@ -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. diff --git a/arch/riscv/include/asm/vdso/processor.h b/arch/riscv/include/asm/vdso/processor.h index 8f383f05a290..8f749552ecfe 100644 --- a/arch/riscv/include/asm/vdso/processor.h +++ b/arch/riscv/include/asm/vdso/processor.h @@ -5,6 +5,8 @@ #ifndef __ASSEMBLY__ #include + +#include #include 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(); } diff --git a/arch/riscv/include/asm/vendorid_list.h b/arch/riscv/include/asm/vendorid_list.h index a5150cdf34d8..3b09874d7a6d 100644 --- a/arch/riscv/include/asm/vendorid_list.h +++ b/arch/riscv/include/asm/vendorid_list.h @@ -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 diff --git a/arch/riscv/kernel/alternative.c b/arch/riscv/kernel/alternative.c index 7eb3cb1215c6..7642704c7f18 100644 --- a/arch/riscv/kernel/alternative.c +++ b/arch/riscv/kernel/alternative.c @@ -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; diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S index 33a5a9f2a0d4..2007d9de7f27 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -15,6 +15,8 @@ #include #include #include +#include + #include .section .irqentry.text, "ax" diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index 722178ae3488..ee8a2c5b40f8 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -35,6 +35,7 @@ #include #include #include +#include #include "../kernel/head.h" diff --git a/tools/arch/riscv/include/asm/vdso/processor.h b/tools/arch/riscv/include/asm/vdso/processor.h index 662aca039848..880f26a24f69 100644 --- a/tools/arch/riscv/include/asm/vdso/processor.h +++ b/tools/arch/riscv/include/asm/vdso/processor.h @@ -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.