From: Yao Zi <me@ziyao.cc>
To: "Junhui Liu" <junhui.liu@pigmoral.tech>,
"Indrek Kruusa" <indrek.kruusa@gmail.com>,
<u-boot@lists.u-boot-project.org>
Cc: "Andre Przywara" <andre.przywara@arm.com>,
"Tim Ouyang" <tim609@andestech.com>,
"Leo Liang" <leo.liang@sifive.com>, "Yao Zi" <me@ziyao.cc>,
"Peng Fan" <peng.fan@nxp.com>,
"Jaehoon Chung" <jh80.chung@samsung.com>,
"Lukasz Majewski" <lukma@nabladev.com>,
"Stefan Roese" <stefan.roese@mailbox.org>,
"Samuel Holland" <samuel@sholland.org>,
"Tom Rini" <trini@konsulko.com>,
"Hiago De Franco" <hfranco@baylibre.com>
Subject: Re: [PATCH 01/10] riscv: cpu: add Allwinner D1/D1s(F133) CPU support
Date: Mon, 16 Nov 2026 07:46:15 +0000 [thread overview]
Message-ID: <avq0xyKg0QOCMKkU@pie> (raw)
In-Reply-To: <DMVVCK6FNA7K.3UJ8SKV87RZOE@pigmoral.tech>
On Sun, Nov 15, 2026 at 10:53:52PM +0800, Junhui Liu wrote:
> Hi Indrek,
>
> On Wed Nov 11, 2026 at 6:00 AM CST, Indrek Kruusa wrote:
> > Hi Junhui,
> >
> > thanks for the review!
> >
> > On Sat, 07 Nov 2026 17:11:09 +0800
> > Junhui Liu <junhui.liu@pigmoral.tech> wrote:
> >
> >> Hi Indrek,
> >>
> >> Thanks for your work. I'm very happy to finally see someone proposing
> >> this work.
> >
> > Thanks, I appreciate that!
> >
> >> But for most of the T-Head/XuanTie CPU support, I think it is
> >> better to make it common code instead of keeping it private to sun20i.
> >>
> >
> > Agreed.
> >
> > [...]
> >
> >> > diff --git a/arch/riscv/cpu/sun20i/cache.c b/arch/riscv/cpu/sun20i/cache.c
> >> > new file mode 100644
> >> > index 00000000000..c961eded109
> >> > --- /dev/null
> >> > +++ b/arch/riscv/cpu/sun20i/cache.c
> >> > @@ -0,0 +1,88 @@
> >> > +// SPDX-License-Identifier: GPL-2.0+
> >> > +/*
> >> > + * Copyright (C) 2026, Indrek Kruusa <indrek.kruusa@gmail.com>
> >> > + *
> >> > + * T-Head C906 cache operations for the Allwinner D1 (sun20i).
> >> > + */
> >> > +
> >> > +#include <asm/arch/thead_csr.h>
> >> > +#include <asm/cache.h>
> >> > +#include <asm/csr.h>
> >> > +#include <cpu_func.h>
> >> > +#include <linux/bitops.h>
> >> > +
> >> > +/*
> >> > + * T-Head cache maintenance instructions, emitted as raw encodings because the
> >> > + * assembler does not know them: dcache.ciall (write back and invalidate all),
> >> > + * dcache.iall (invalidate all), icache.ipa (invalidate the line holding the
> >> > + * physical address in a0) and sync.i (wait for the operation to complete).
> >> > + * They only decode while MXSTATUS.THEADISAEE is set, which harts_early_init()
> >> > + * does before any of them is used.
> >> > + */
> >> > +#define THEAD_SYNC_I ".long 0x01a0000b"
> >> > +#define THEAD_DCACHE_CIALL ".long 0x0030000b"
> >> > +#define THEAD_DCACHE_IALL ".long 0x0020000b"
> >> > +#define THEAD_ICACHE_IPA_A0 ".long 0x0385000b"
> >> > +
> >> > +static inline void sync_i(void)
> >> > +{
> >> > + asm volatile (THEAD_SYNC_I ::: "memory");
> >> > +}
> >> > +
> >> > +void flush_dcache_all(void)
> >> > +{
> >> > + asm volatile (THEAD_DCACHE_CIALL ::: "memory");
> >> > + sync_i();
> >> > +}
> >> > +
> >> > +void invalidate_dcache_all(void)
> >> > +{
> >> > + asm volatile (THEAD_DCACHE_IALL ::: "memory");
> >> > + sync_i();
> >> > +}
> >> > +
> >> > +void invalidate_icache_range(unsigned long start, unsigned long end)
> >> > +{
> >> > + register ulong addr asm ("a0") = start & -CONFIG_SYS_CACHELINE_SIZE;
> >> > +
> >> > + for (; addr < end; addr += CONFIG_SYS_CACHELINE_SIZE)
> >> > + asm volatile (THEAD_ICACHE_IPA_A0 :: "r" (addr) : "memory");
> >> > + sync_i();
> >> > +}
> >>
> >> For these cache operations, arch/riscv/lib/thead_cmo.c already contains
> >> the D-cache range operations. The D-cache all operations and I-cache
> >> operations added here are common T-Head/XuanTie CMO operations, so I
> >> think it's better to add them to that common file instead of being
> >> implemented under sun20i.
> >>
> >
> > Actually, that's what I initially had in mind, but I didn't feel comfortable
> > doing it. Sure, and as Yao also suggested, I'll move flush_dcache_all() and
> > invalidate_dcache_all() to arch/riscv/lib/thead_cmo.c in the next version.
> >
> > I still need to investigate invalidate_icache_range() and whether I can
> > simply rely on the default implementation in arch/riscv/lib/cache.c. If so,
> > the local cache.c can go away entirely.
>
> I checked the C906 user manual, and it explicitly states that fence.i
> invalidates the I-cache. Both CV1800B and TH1520 already use the default
> invalidate_icache_range() in arch/riscv/lib/cache.c, so I think D1 can
> use the default implementation as well.
tl;dr I agree.
FYI, see here[1] for a possible defect in xtheadsync documentation,
which considers th.sync.i the only mechanism to order cache operations
with instruction fetches, conflicting with the definition of fence.i in
RISC-V's specification.
Referring to armv8's cache operations and usage of
invalidate_icache_all(), I think we do need the semantics of pipeline
hazard barrier provided for it.
But according to the fact that the current software is written with only
fence.i in mind but still works on HARTs with xtheadsync, I think the
contradiction with fence.i's definition is probably an error in
xtheadsync's documentation.
> >
> >> > +
> >> > +#if CONFIG_IS_ENABLED(RISCV_MMODE)
> >> > +
> >> > +void icache_enable(void)
> >> > +{
> >> > + invalidate_icache_all();
> >> > + csr_set(CSR_MHCR, MHCR_IE);
> >> > +}
> >> > +
> >> > +void icache_disable(void)
> >> > +{
> >> > + csr_clear(CSR_MHCR, MHCR_IE);
> >> > +}
> >> > +
> >> > +int icache_status(void)
> >> > +{
> >> > + return csr_read(CSR_MHCR) & MHCR_IE;
> >> > +}
> >> > +
> >> > +void dcache_enable(void)
> >> > +{
> >> > + invalidate_dcache_all();
> >> > + csr_set(CSR_MHCR, MHCR_DE);
> >> > +}
> >> > +
> >> > +void dcache_disable(void)
> >> > +{
> >> > + flush_dcache_all();
> >> > + csr_clear(CSR_MHCR, MHCR_DE);
> >> > +}
> >> > +
> >> > +int dcache_status(void)
> >> > +{
> >> > + return csr_read(CSR_MHCR) & MHCR_DE;
> >> > +}
> >> > +
> >> > +#endif
> >>
> >> For this part, arch/riscv/cpu/th1520/cache.c already has related
> >> functions. Can we extract them into a common file, for example
> >> arch/riscv/lib/thead_cache.c, and provide a THEAD_CACHE config for the
> >> platforms that need it to select?
> >>
> >
> > I had a look: th1520/cache.c has only the _enable() and _status() half, and
> > cv1800b has no cache.c at all (it relies on the defaults in
> > arch/riscv/lib/cache.c).
> > Yes, having a common thead_cache.c with all six functions and a THEAD_CACHE
> > symbol would give all three SoCs the same implementation.
> >
> > I can add the THEAD_CACHE symbol and arch/riscv/lib/thead_cache.c, but I'm
> > not sure about touching the other SoCs in this series. What do you think? I
> > can prepare a separate series if that would be a better way to handle this
> > consistently across all three SoCs.
>
> I would prefer a separate series for the common T-Head/XuanTie support,
> so it can be reviewed and applied independently and benefit other SoCs
> as well. The D1 support series can then depend on it.
This sounds good to me.
> Best regards,
> Junhui Liu
>
> >
> >> [...]
> >>
> >> > diff --git a/arch/riscv/include/asm/arch-sun20i/thead_csr.h b/arch/riscv/include/asm/arch-sun20i/thead_csr.h
> >> > new file mode 100644
> >> > index 00000000000..4d6e15aab1c
> >> > --- /dev/null
> >> > +++ b/arch/riscv/include/asm/arch-sun20i/thead_csr.h
> >> > @@ -0,0 +1,29 @@
> >> > +/* SPDX-License-Identifier: GPL-2.0+ */
> >> > +/*
> >> > + * Copyright (C) 2026, Indrek Kruusa <indrek.kruusa@gmail.com>
> >> > + *
> >> > + * T-Head C906 CSR definitions for the Allwinner D1 (sun20i).
> >> > + */
> >> > +
> >> > +#ifndef _ASM_RISCV_ARCH_THEAD_CSR_H
> >> > +#define _ASM_RISCV_ARCH_THEAD_CSR_H
> >> > +
> >> > +#define CSR_MXSTATUS 0x7c0
> >> > +#define CSR_MHCR 0x7c1
> >> > +#define CSR_MHINT 0x7c5
> >> > +
> >> > +#define MXSTATUS_THEADISAEE BIT(22) /* T-HEAD ISA extensions enable */
> >> > +#define MXSTATUS_MAEE BIT(21) /* MMU honours the PTE memory type */
> >> > +#define MXSTATUS_MM BIT(15) /* misaligned access enable */
> >> > +
> >> > +#define MHCR_IE BIT(0) /* icache enable */
> >> > +#define MHCR_DE BIT(1) /* dcache enable */
> >> > +#define MHCR_WA BIT(2) /* dcache write allocate */
> >> > +#define MHCR_RS BIT(4) /* return stack enable */
> >> > +#define MHCR_BPE BIT(5) /* branch prediction enable */
> >> > +#define MHCR_BTB_C906 BIT(6) /* branch target prediction enable */
> >> > +
> >> > +#define MHINT_IPLD BIT(8) /* icache prefetch enable */
> >> > +#define MHINT_IWPE BIT(9) /* icache way prediction enable */
> >> > +
> >> > +#endif /* _ASM_RISCV_ARCH_THEAD_CSR_H */
> >>
> >> A number of these CSR definitions already exist in
> >> arch/riscv/cpu/th1520/spl.c and arch/riscv/cpu/th1520/cache.c. Can the
> >> common register numbers and bitfields be moved into a common header,
> >> such as arch/riscv/include/asm/thead_csr.h, instead of placing them
> >> under arch-sun20i?
> >
> > Agreed. I'll create arch/riscv/include/asm/thead_csr.h in the next version,
> > identify the common register definitions and bitfields, and move them there.
> >
> > Again, I'm not sure I'd want to convert the other SoCs in the same series.
> > Would a separate series be better?
A separate series makes sense.
> > One thing I should flag while we're on this: my patch 1 defines
> > `MHINT_IWPE` as `BIT(9)`.
> > I did some AI-assisted investigation into these fields, and the finding was:
> > "In the OpenC906 RTL, bit 9 is LPE (its write decode is commented out and
> > the field is tied to zero), while bit 10 is IWPE."
> > So, according to this finding, the series as posted never actually enables
> > I-cache way prediction. That's a bug in my patch, will fix it.
> >
> >>
> >> --
> >> Best regards,
> >> Junhui Liu
> >>
> >
> > Thanks,
> > Indrek
--
Thanks,
Yao Zi
Meow-mrrp-mrow, I'm a cat.
[1]: https://github.com/XUANTIE-RV/thead-extension-spec/issues/81
next prev parent reply other threads:[~2026-11-16 7:47 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-01 21:04 [PATCH 00/10] riscv: sun20i: add support for the Allwinner D1/D1s(F133) SoCs and boards Indrek Kruusa
2026-11-01 21:04 ` [PATCH 01/10] riscv: cpu: add Allwinner D1/D1s(F133) CPU support Indrek Kruusa
2026-11-07 9:11 ` Junhui Liu
2026-11-07 16:13 ` Yao Zi
2026-11-10 22:00 ` Indrek Kruusa
2026-11-15 14:53 ` Junhui Liu
2026-11-16 7:46 ` Yao Zi [this message]
2026-11-16 20:17 ` Indrek Kruusa
2026-11-01 21:04 ` [PATCH 02/10] sunxi: allow the sunxi drivers to be built for sun20i Indrek Kruusa
2026-11-01 21:04 ` [PATCH 03/10] board: sun20i: add common support for the D1/D1s boards Indrek Kruusa
2026-11-01 21:04 ` [PATCH 04/10] sunxi: dram: fix integer-pointer conversions in the D1 DRAM driver Indrek Kruusa
2026-11-01 21:04 ` [PATCH 05/10] sunxi: dram: count the banks on address bit 11, not A22 Indrek Kruusa
2026-11-01 21:04 ` [PATCH 06/10] sunxi: dram: no AC remapping on the co-packaged DRAM parts Indrek Kruusa
2026-11-01 21:04 ` [PATCH 07/10] sunxi: dram: let the board set the MR1 mode register Indrek Kruusa
2026-11-01 21:04 ` [PATCH 08/10] sunxi: mmc: handle D1 PLL6 clock rate correctly Indrek Kruusa
2026-11-01 21:04 ` [PATCH 09/10] configs: add the Lichee RV Dock defconfig Indrek Kruusa
2026-11-01 21:04 ` [PATCH 10/10] configs: add the MangoPi MQ D1s(F133) defconfig Indrek Kruusa
2026-11-06 23:17 ` [PATCH 00/10] riscv: sun20i: add support for the Allwinner D1/D1s(F133) SoCs and boards Andre Przywara
2026-11-09 21:44 ` Indrek Kruusa
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=avq0xyKg0QOCMKkU@pie \
--to=me@ziyao.cc \
--cc=andre.przywara@arm.com \
--cc=hfranco@baylibre.com \
--cc=indrek.kruusa@gmail.com \
--cc=jh80.chung@samsung.com \
--cc=junhui.liu@pigmoral.tech \
--cc=leo.liang@sifive.com \
--cc=lukma@nabladev.com \
--cc=peng.fan@nxp.com \
--cc=samuel@sholland.org \
--cc=stefan.roese@mailbox.org \
--cc=tim609@andestech.com \
--cc=trini@konsulko.com \
--cc=u-boot@lists.u-boot-project.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.