U-Boot Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Yao Zi <me@ziyao.cc>
To: Simon Glass <sjg@chromium.org>, me@ziyao.cc
Cc: Tom Rini <trini@konsulko.com>,
	Jiaxun Yang <jiaxun.yang@flygoat.com>,
	Heinrich Schuchardt <xypron.glpk@gmx.de>,
	Ilias Apalodimas <ilias.apalodimas@linaro.org>,
	u-boot@lists.denx.de
Subject: Re: [PATCH v2 07/16] LoongArch: lib: General routines
Date: Thu, 2 Jul 2026 21:46:10 +0000	[thread overview]
Message-ID: <akbcIqCRlwGVT5i3@pie> (raw)
In-Reply-To: <CAFLszThSQPzj4NpjM+AUbovZv4oZBZR3yfjhk0rfLanTsXe8fw@mail.gmail.com>

On Thu, Jul 02, 2026 at 11:28:08AM +0100, Simon Glass wrote:
> Hi Yao,
> 
> On 2026-07-01T11:17:53, Yao Zi <me@ziyao.cc> wrote:
> > LoongArch: lib: General routines
> >
> > Add some common library routines for the architecture.
> >
> > Signed-off-by: Jiaxun Yang <jiaxun.yang@flygoat.com>
> > Signed-off-by: Yao Zi <me@ziyao.cc>
> >
> > arch/loongarch/include/asm/cache.h       |  10 ++
> >  arch/loongarch/include/asm/global_data.h |   7 +
> >  arch/loongarch/lib/Makefile              |   7 +
> >  arch/loongarch/lib/asm-offsets.c         |  66 ++++++++++
> >  arch/loongarch/lib/boot.c                |  14 ++
> >  arch/loongarch/lib/cache.c               | 213 +++++++++++++++++++++++++++++++
> >  arch/loongarch/lib/reset.c               |  14 ++
> >  arch/loongarch/lib/setjmp.S              |  52 ++++++++
> >  8 files changed, 383 insertions(+)
> 
> > Add some common library routines for the architecture.

...

> > diff --git a/arch/loongarch/lib/cache.c b/arch/loongarch/lib/cache.c
> > @@ -0,0 +1,213 @@
> > +static inline void flush_cache_line_index(unsigned int index,
> > +                                       unsigned long addr)
> > +{
> > +#define do_flush(index)                                                      \
> > +     case index:                                                     \
> > +             cache_op(FIELD_PREP(CACHE_OP, CACHE_INDEX_INVWB) |      \
> > +                      FIELD_PREP(CACHE_INDEX, index),                \
> > +                      index);                                        \
> > +             break;
> > +
> > +     switch (index) {
> > +             do_flush(0);
> > +             do_flush(1);
> > +             do_flush(2);
> > +             do_flush(3);
> > +             do_flush(4);
> > +             do_flush(5);
> > +     }
> > +
> > +#undef do_flush
> > +}
> 
> addr is silently dropped. Inside do_flush() the token index
> resolves to the macro parameter (0..5, the cache identifier), so
> cache_op() always gets the small constant as its address operand.
> The way/set offset that flush_dcache_level_all() computes never
> reaches the CACOP instruction, so this hammers way 0 / set 0 of
> the selected cache repeatedly instead of walking it. Please pass
> addr through (rename the macro parameter to avoid the shadow), and
> confirm with a real by-index flush test.

Oops, yes. Thanks for pointing out.

> > diff --git a/arch/loongarch/lib/cache.c b/arch/loongarch/lib/cache.c
> > @@ -0,0 +1,213 @@
> > +void probe_caches(void)
> > +{
> > +     unsigned int level = 0, index = 0;
> > +     u32 cfg = read_cpucfg(LOONGARCH_CPUCFG16);
> > +
> > +     if (cfg & CPUCFG16_L1_IUPRE) {
> > +             if (cfg & CPUCFG16_L1_IUUNIFY) {
> > +                     gd->arch.dcache_index[level] = index;
> > +                     populate_dcache_properties(cfg, level++,
> > +                                                LOONGARCH_CPUCFG17);
> > +             }
> > +
> > +             index++;
> > +     }
> > +
> > +     if (cfg & CPUCFG16_L1_DPRE) {
> > +             gd->arch.dcache_index[level] = index++;
> > +             populate_dcache_properties(cfg, level++, LOONGARCH_CPUCFG18);
> > +     }
> > +
> > +     cfg >>= 3;
> > +
> > +     for (; level < 3; level++) {
> 
> Please use CACHE_MAX_LEVEL rather than the bare 3.

Ok.

> populate_dcache_properties() also sets dcache_inclusive from cfg
> whenever level != 0, but on the L1D path cfg is still CPUCFG16 -
> the L1 inclusive bit describes L1I vs L1D, not L1D vs L2, so this
> looks incorrect. Please double-check.

The numbering logic for caches used by CACOP are quite confusing, and I
think it deserves some comments in the file. For CACOP instructions, it
indexes caches in the following order,

- L1I or L1 unified
- L1D
- L2I or L2 unified
- L2D
- L3I or L3 unified
- L3D

The index only counts caches present on the system, e.g., for a system
with L1I, L1D, L2 unified and L3 unified, they're respectively indexed
through 0-3 instead of 0, 1, 2, 4.

Furthermore, current architecture definition provides no cpucfg
register for L2D/L3D information L3D, so the current code assumes L2
and L3, if present, must be unified or instruction-only.

Back in the logic, L1_IUPRE means either L1I or L1 unified is present,
and "index" is increased in this case, but "level" is only increased if
L1_IUUNIFY is set, which means L1 is unified and L1_DPRE must not be
set. So populate_dcache_properties() would never be fired to probe
L1D with level = 1.

I'll add some comments on this in v3.

...

> Simon

Best regards,
Yao Zi

  reply	other threads:[~2026-07-02 21:46 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-01 11:17 [PATCH v2 00/16] LoongArch initial support Yao Zi
2026-07-01 11:17 ` [PATCH v2 01/16] lib: fdtdec: Handle multiple memory nodes Yao Zi
2026-07-02 10:22   ` Simon Glass
2026-07-02 20:39     ` Yao Zi
2026-07-01 11:17 ` [PATCH v2 02/16] linux/io.h: Use map_physmem to implement ioremap Yao Zi
2026-07-02 10:21   ` Simon Glass
2026-07-01 11:17 ` [PATCH v2 03/16] image: Take entry point as an output of setup_booti Yao Zi
2026-07-01 17:39   ` Tom Rini
2026-07-02 20:39     ` Yao Zi
2026-07-02 20:55       ` Tom Rini
2026-07-02 21:10         ` Yao Zi
2026-07-02  9:46   ` Simon Glass
2026-07-01 11:17 ` [PATCH v2 04/16] elf: Define LoongArch bits Yao Zi
2026-07-02 10:24   ` Simon Glass
2026-07-01 11:17 ` [PATCH v2 05/16] image: Define IH_ARCH_LOONGARCH Yao Zi
2026-07-02  9:44   ` Simon Glass
2026-07-01 11:17 ` [PATCH v2 06/16] LoongArch: skeleton and headers Yao Zi
2026-07-01 17:47   ` Tom Rini
2026-07-02 20:40     ` Yao Zi
2026-07-02 20:57       ` Tom Rini
2026-07-02 10:28   ` Simon Glass
2026-07-02 21:22     ` Yao Zi
2026-07-01 11:17 ` [PATCH v2 07/16] LoongArch: lib: General routines Yao Zi
2026-07-01 17:49   ` Tom Rini
2026-07-02  9:24     ` Jiaxun Yang
2026-07-02 10:54       ` Simon Glass
2026-07-02 14:07       ` Tom Rini
2026-07-02 20:56         ` Yao Zi
2026-07-02 20:58           ` Tom Rini
2026-07-02 10:28   ` Simon Glass
2026-07-02 21:46     ` Yao Zi [this message]
2026-07-01 11:18 ` [PATCH v2 08/16] LoongArch: CPU assembly routines Yao Zi
2026-07-02 10:25   ` Simon Glass
2026-07-03 20:12     ` Yao Zi
2026-07-04 11:22       ` Jiaxun Yang
2026-07-01 11:18 ` [PATCH v2 09/16] LoongArch: Exception handling Yao Zi
2026-07-02 10:25   ` Simon Glass
2026-07-01 11:18 ` [PATCH v2 10/16] LoongArch: Boot Image bits Yao Zi
2026-07-01 17:53   ` Tom Rini
2026-07-02 10:26   ` Simon Glass
2026-07-01 11:18 ` [PATCH v2 11/16] LoongArch: Generic CPU type Yao Zi
2026-07-01 17:55   ` Tom Rini
2026-07-02 10:26   ` Simon Glass
2026-07-01 11:18 ` [PATCH v2 12/16] cpu: Add loongarch_cpu driver Yao Zi
2026-07-02 10:26   ` Simon Glass
2026-07-01 11:18 ` [PATCH v2 13/16] timer: Add loongarch_timer driver Yao Zi
2026-07-02 10:27   ` Simon Glass
2026-07-01 11:18 ` [PATCH v2 14/16] board: emulation: Add qemu-loongarch Yao Zi
2026-07-02 10:27   ` Simon Glass
2026-07-01 11:18 ` [PATCH v2 15/16] efi: LoongArch: Define LoongArch bits everywhere Yao Zi
2026-07-02 10:27   ` Simon Glass
2026-07-01 11:18 ` [PATCH v2 16/16] efi: LoongArch: Implement everything Yao Zi
2026-07-02 10:29   ` Simon Glass
2026-07-01 15:53 ` [PATCH v2 00/16] LoongArch initial support Jiaxun Yang
2026-07-01 17:59 ` Tom Rini
2026-07-02 10:31 ` [v2,00/16] " Simon Glass

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=akbcIqCRlwGVT5i3@pie \
    --to=me@ziyao.cc \
    --cc=ilias.apalodimas@linaro.org \
    --cc=jiaxun.yang@flygoat.com \
    --cc=sjg@chromium.org \
    --cc=trini@konsulko.com \
    --cc=u-boot@lists.denx.de \
    --cc=xypron.glpk@gmx.de \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox