Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 85A0012E4A for ; Sat, 28 Dec 2024 06:01:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.42 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735365692; cv=none; b=nR5D08YIstu9rt1BWs7bjXEXMeF+I7zBk6facJQkBTrQCQKDKXjJirfe0xDH7YmNOno8qIfHT0pis9JfCQntXypSyB0J5mO9fjlHJlf4Pg3ApyZVpqdfkEqVTwLxD4A+IHVfV1xtcQgxxci0jdRm70LC9waGC3xZGoxCjVaDepY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735365692; c=relaxed/simple; bh=z6EuYrU+QU7mROh3qULtcUDD6cwAGMO5LrfU8imuyRI=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=LLFjxMCg+gSafyL6D+W5J6qM38WNT7b2v1cvyk8pWq27F3/ZLhJMc/z4oTdNdcEXblFjXK25t/iBoBhGmixePgu5b6w7roK3bXlEI/rW2ppwKh3qJHUFHxadM2sTsKxiO1+3meeceUFdfrq/JYnGATAqDAKo+ro7MSpq43aQnT0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=MxWTW2tR; arc=none smtp.client-ip=209.85.216.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="MxWTW2tR" Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-2efded08c79so7635985a91.0 for ; Fri, 27 Dec 2024 22:01:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1735365688; x=1735970488; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=16HXpJjBFl/U2jMxDiHAdVrxhR44wg5Y1LzvPtm7LA0=; b=MxWTW2tRI/P9MXbOGZCh0doeL6ntMb9JWsCwOMvwrhJnvtvV1Xqp4+RX4b+SQEhX75 Yw1csIySAmv8ksAtuBJLcPmOWl/K+088jK5m/7TUbi195E/n2764rcnYoBzRxxG8xZWx kBcG9eCLMj/OxxxhhoG3wjMdnAm64CGwUTo6oDtk+0Ztkd7ImTixvpRsknAr71dM0TPn QVnoEMq+oCCLIIuBZABfdwO2GiFLzKERhVnppueEyJP5dKj3+wC4jGVmC2Y5IWTKtVow mxfEqt42AqXLouohaIMjOaHbDHvXYKXt4wUHh0gf0Eei43evJciWq4/HLOq4l+kZEGJ9 HGNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735365688; x=1735970488; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=16HXpJjBFl/U2jMxDiHAdVrxhR44wg5Y1LzvPtm7LA0=; b=kJeoYeLUV4OYj9GpZgvzi1vPaQy8ct6syV3DRBSjIXfZKjKpZqudv17CwGib1Bgt8e ZuFIR1Ht4aXOM0JhbNUOAoDdLnVSQ0lMC5bz8+O8NDnUGnXmfIWAaK8sTE/phFkMzPH+ KHDxdoS+c/8ohjzZ2zk9nNIOWGEwWJIXpPOZ8Py8z92qs5SyGxT/JtzWvN3qY2J60key 7FUQ4cUQUhc8+J4LEkPWMwksHBGdpF/0Ve3Gpd+ZdtK8Fj/Kxzlz9xl6vSsaJAzVk349 dk7AFkEyGtzJIYO5fjvgs/bpL4LuYJ2lLlXDa42BpZZrrXHcBqIihgP+ZvvesqP0fUeb zxXg== X-Forwarded-Encrypted: i=1; AJvYcCVoAU54llV7b0knGW2WFBB3TR8Sk+L5D9dX6fm3+LfyvmddLKE1yonaQF3TNb5dUmWj6qm6euvWv+fBlmA=@vger.kernel.org X-Gm-Message-State: AOJu0Yy1yVqYbqrIbIuDX05w/uqMjSwkt/FjfRM1n4Z1hUj1gsxVGuZS Um3NTQRlUjo8erx+FhWgDXzkX6wiMBUr4AB7xRLks/nNjZfKb1qrr5rqLfxUOXc= X-Gm-Gg: ASbGncugVhlclXIFgI6Qcpg4IK2WLuoL01vXdubzg/gs1dWWOjFwb2hY1LZt++rBpQ1 SdFZGtMIV9z3402iIG54hR/6TAIRMEU26A6mSBvhahMGLGi1n/b6KJqZGRWRl6FaD2CmCv95oFg EN/0UfWDW3e5E2gKgRK4ex22XsYjd+GjtroammOs2LoF/rZSlpeu6jMBwNtn3VhqsPkJ701k9A8 KjrPt64Mz+prM+apFvbrA7R+iRQYACc12Qs9D68UpRyBDb/xRn1 X-Google-Smtp-Source: AGHT+IHIdaVUZiKhPMtqpli/189hMR4AnOV0fDaSvB5n0JS9Xe7Z+uW+6VIHZ43uBbGsFmJ98qk2Cg== X-Received: by 2002:a17:90a:d00b:b0:2ee:9a82:5a93 with SMTP id 98e67ed59e1d1-2f452e1d13amr43884406a91.14.1735365688566; Fri, 27 Dec 2024 22:01:28 -0800 (PST) Received: from ghost ([2601:647:6700:64d0:ffc5:30d3:db1f:89c2]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2f447798982sm16783288a91.1.2024.12.27.22.01.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Dec 2024 22:01:27 -0800 (PST) Date: Fri, 27 Dec 2024 22:01:25 -0800 From: Charlie Jenkins To: Aleksandar Rikalo Cc: linux-riscv@lists.infradead.org, Paul Walmsley , Palmer Dabbelt , Albert Ou , Andrew Jones , Christoph =?iso-8859-1?Q?M=FCllner?= , linux-kernel@vger.kernel.org, Djordje Todorovic Subject: Re: [PATCH v2] riscv: Fix the PAUSE Opcode for MIPS P8700. Message-ID: References: <20241227135832.188256-1-arikalo@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20241227135832.188256-1-arikalo@gmail.com> On Fri, Dec 27, 2024 at 02:58:32PM +0100, Aleksandar Rikalo wrote: > 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 | 22 +++++++++++++++++ > arch/riscv/errata/Makefile | 1 + > arch/riscv/errata/mips/Makefile | 5 ++++ > arch/riscv/errata/mips/errata.c | 33 ++++++++++++++++++++++++++ > arch/riscv/include/asm/alternative.h | 3 +++ > arch/riscv/include/asm/vendorid_list.h | 1 + > arch/riscv/kernel/alternative.c | 5 ++++ > 7 files changed, 70 insertions(+) > 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..99c75fece8b9 100644 > --- a/arch/riscv/Kconfig.errata > +++ b/arch/riscv/Kconfig.errata > @@ -21,6 +21,28 @@ 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 > + 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..7affc590ded5 > --- /dev/null > +++ b/arch/riscv/errata/mips/errata.c > @@ -0,0 +1,33 @@ > +// SPDX-License-Identifier: GPL-2.0-only > +/* > + * Copyright (C) 2024 MIPS. > + */ > + > +#include > +#include > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +void __init_or_module mips_errata_patch_func(struct alt_entry *begin, > + struct alt_entry *end, > + unsigned long archid, > + unsigned long impid, > + unsigned int stage) > +{ > + if (!IS_ENABLED(CONFIG_ERRATA_MIPS)) > + return; > + > + if (!IS_ENABLED(CONFIG_ERRATA_MIPS_P8700_PAUSE_OPCODE)) > + return; > + > + asm volatile(ALTERNATIVE(".4byte 0x1000000f", ".4byte 0x00501013", > + MIPS_VENDOR_ID, 0, /* patch_id */ > + CONFIG_ERRATA_MIPS_P8700_PAUSE_OPCODE)); I dont think his isn't quite what you are looking for. This will end up emitting a pause instruction when this patch function is called, instead of replacing all pause instructions with this custom encoding. What you want to do here is similar to what is done in arch/riscv/include/asm/errata_list.h. A good example of this is: #define ALT_SFENCE_VMA_ASID(asid) \ asm(ALTERNATIVE("sfence.vma x0, %0", "sfence.vma", SIFIVE_VENDOR_ID, \ ERRATA_SIFIVE_CIP_1200, CONFIG_ERRATA_SIFIVE_CIP_1200) \ : : "r" (asid) : "memory") This macro is then used everywhere "sfence.vma" would normally be used. Then in sifive_errata_patch_func() in arch/riscv/errata/sifive/errata.c iterates through all alternatives, looking for any alternatives that match SIFIVE_VENDOR_ID and then perform the replacement. There seem to be three places where the pause instruction is used: - cpu_relax() in arch/riscv/include/asm/vdso/processor.h - __cmpwait() in arch/riscv/include/asm/cmpxchg.h This one is a different because it is in tools. - cpu_relax() in tools/arch/riscv/include/asm/vdso/processor.h tools is userspace code, so alternatives are not supported. Patching the pause instruction here will require a call to hwprobe. I am hesitant about this though because that would cause all microarchitectures to incur the cost of calling hwprobe, not just ones produced by MIPS. I would prefer for that to not be the default, kconfig doesn't extend to tools so some environment variable could be set for it. I am not sure what kind of precedent we want to set for this. Also, Palmer is expected to merge in my vendor-specific hwprobe framework this merge window that is included in [1]. Link https://lore.kernel.org/linux-riscv/20241113-xtheadvector-v11-0-236c22791ef9@rivosinc.com/ [1] > +} > 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/vendorid_list.h b/arch/riscv/include/asm/vendorid_list.h > index 2f2bb0c84f9a..55d979a7a7c2 100644 > --- a/arch/riscv/include/asm/vendorid_list.h > +++ b/arch/riscv/include/asm/vendorid_list.h > @@ -8,5 +8,6 @@ > #define ANDES_VENDOR_ID 0x31e > #define SIFIVE_VENDOR_ID 0x489 > #define THEAD_VENDOR_ID 0x5b7 > +#define MIPS_VENDOR_ID 0x127 > > #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; > -- > 2.25.1 > > > _______________________________________________ > linux-riscv mailing list > linux-riscv@lists.infradead.org > http://lists.infradead.org/mailman/listinfo/linux-riscv