2023-03-15 00:45:17

by Huacai Chen

[permalink] [raw]
Subject: [PATCH V2] LoongArch: Make WriteCombine configurable for ioremap()

LoongArch maintains cache coherency in hardware, but when paired with
LS7A chipsets the WUC attribute (Weak-ordered UnCached, which is similar
to WriteCombine) is out of the scope of cache coherency machanism for
PCIe devices (this is a PCIe protocol violation, which may be fixed in
newer chipsets).

This means WUC can only used for write-only memory regions now, so this
option is disabled by default, making WUC silently fallback to SUC for
ioremap(). You can enable this option if the kernel is ensured to run on
hardware without this bug.

Kernel parameter writecombine=on/off can be used to override the Kconfig
option.

Suggested-by: WANG Xuerui <[email protected]>
Signed-off-by: Huacai Chen <[email protected]>
---
V2: Add kernel prameter and update commit messages.

arch/loongarch/Kconfig | 14 ++++++++++++++
arch/loongarch/include/asm/io.h | 4 +++-
arch/loongarch/kernel/setup.c | 19 +++++++++++++++++++
3 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
index 0d11738a861a..e8437ac300f2 100644
--- a/arch/loongarch/Kconfig
+++ b/arch/loongarch/Kconfig
@@ -446,6 +446,20 @@ config ARCH_IOREMAP
protection support. However, you can enable LoongArch DMW-based
ioremap() for better performance.

+config ARCH_WRITECOMBINE
+ bool "Enable WriteCombine (WUC) for ioremap()"
+ help
+ LoongArch maintains cache coherency in hardware, but when paired
+ with LS7A chipsets the WUC attribute (Weak-ordered UnCached, which
+ is similar to WriteCombine) is out of the scope of cache coherency
+ machanism for PCIe devices (this is a PCIe protocol violation, which
+ may be fixed in newer chipsets).
+
+ This means WUC can only used for write-only memory regions now, so
+ this option is disabled by default, making WUC silently fallback to
+ SUC for ioremap(). You can enable this option if the kernel is ensured
+ to run on hardware without this bug.
+
config ARCH_STRICT_ALIGN
bool "Enable -mstrict-align to prevent unaligned accesses" if EXPERT
default y
diff --git a/arch/loongarch/include/asm/io.h b/arch/loongarch/include/asm/io.h
index 402a7d9e3a53..545e2708fbf7 100644
--- a/arch/loongarch/include/asm/io.h
+++ b/arch/loongarch/include/asm/io.h
@@ -54,8 +54,10 @@ static inline void __iomem *ioremap_prot(phys_addr_t offset, unsigned long size,
* @offset: bus address of the memory
* @size: size of the resource to map
*/
+extern pgprot_t pgprot_wc;
+
#define ioremap_wc(offset, size) \
- ioremap_prot((offset), (size), pgprot_val(PAGE_KERNEL_WUC))
+ ioremap_prot((offset), (size), pgprot_val(pgprot_wc))

#define ioremap_cache(offset, size) \
ioremap_prot((offset), (size), pgprot_val(PAGE_KERNEL))
diff --git a/arch/loongarch/kernel/setup.c b/arch/loongarch/kernel/setup.c
index bae84ccf6d36..ae977dc01aaa 100644
--- a/arch/loongarch/kernel/setup.c
+++ b/arch/loongarch/kernel/setup.c
@@ -160,6 +160,25 @@ static void __init smbios_parse(void)
dmi_walk(find_tokens, NULL);
}

+#ifdef CONFIG_ARCH_WRITECOMBINE
+pgprot_t pgprot_wc = PAGE_KERNEL_WUC;
+#else
+pgprot_t pgprot_wc = PAGE_KERNEL_SUC;
+#endif
+
+EXPORT_SYMBOL(pgprot_wc);
+
+static int __init setup_writecombine(char *p)
+{
+ if (!strcmp(p, "on"))
+ pgprot_wc = PAGE_KERNEL_WUC;
+ else if (!strcmp(p, "off"))
+ pgprot_wc = PAGE_KERNEL_SUC;
+
+ return 0;
+}
+early_param("writecombine", setup_writecombine);
+
static int usermem __initdata;

static int __init early_parse_mem(char *p)
--
2.39.1



2023-03-15 04:33:31

by Xi Ruoyao

[permalink] [raw]
Subject: Re: [PATCH V2] LoongArch: Make WriteCombine configurable for ioremap()

On Wed, 2023-03-15 at 08:44 +0800, Huacai Chen wrote:

/* snip */

> +config ARCH_WRITECOMBINE
> +       bool "Enable WriteCombine (WUC) for ioremap()"
> +       help
> +         LoongArch maintains cache coherency in hardware, but when paired
> +         with LS7A chipsets the WUC attribute (Weak-ordered UnCached, which
> +         is similar to WriteCombine) is out of the scope of cache coherency
> +         machanism for PCIe devices (this is a PCIe protocol violation, which
> +         may be fixed in newer chipsets).
> +
> +         This means WUC can only used for write-only memory regions now, so
> +         this option is disabled by default, making WUC silently fallback to
> +         SUC for ioremap(). You can enable this option if the kernel is ensured
> +         to run on hardware without this bug.

'You can override this setting via writecombine=yes/no boot parameter.'

/* snip */

> +static int __init setup_writecombine(char *p)
> +{
> +       if (!strcmp(p, "on"))
> +               pgprot_wc = PAGE_KERNEL_WUC;
> +       else if (!strcmp(p, "off"))
> +               pgprot_wc = PAGE_KERNEL_SUC;

else
pr_warn("Unknown writecombine setting \"%s\".\n", p);

Otherwise LGTM.

--
Xi Ruoyao <[email protected]>
School of Aerospace Science and Technology, Xidian University

2023-03-15 06:25:22

by Huacai Chen

[permalink] [raw]
Subject: Re: [PATCH V2] LoongArch: Make WriteCombine configurable for ioremap()

On Wed, Mar 15, 2023 at 12:33 PM Xi Ruoyao <[email protected]> wrote:
>
> On Wed, 2023-03-15 at 08:44 +0800, Huacai Chen wrote:
>
> /* snip */
>
> > +config ARCH_WRITECOMBINE
> > + bool "Enable WriteCombine (WUC) for ioremap()"
> > + help
> > + LoongArch maintains cache coherency in hardware, but when paired
> > + with LS7A chipsets the WUC attribute (Weak-ordered UnCached, which
> > + is similar to WriteCombine) is out of the scope of cache coherency
> > + machanism for PCIe devices (this is a PCIe protocol violation, which
> > + may be fixed in newer chipsets).
> > +
> > + This means WUC can only used for write-only memory regions now, so
> > + this option is disabled by default, making WUC silently fallback to
> > + SUC for ioremap(). You can enable this option if the kernel is ensured
> > + to run on hardware without this bug.
>
> 'You can override this setting via writecombine=yes/no boot parameter.'
OK, thanks.

>
> /* snip */
>
> > +static int __init setup_writecombine(char *p)
> > +{
> > + if (!strcmp(p, "on"))
> > + pgprot_wc = PAGE_KERNEL_WUC;
> > + else if (!strcmp(p, "off"))
> > + pgprot_wc = PAGE_KERNEL_SUC;
>
> else
> pr_warn("Unknown writecombine setting \"%s\".\n", p);
OK, thanks.

Huacai
>
> Otherwise LGTM.
>
> --
> Xi Ruoyao <[email protected]>
> School of Aerospace Science and Technology, Xidian University