Re: [PATCH] LoongArch: Detect WriteCombine (WUC) instead of config

From: Xi Ruoyao

Date: Sun Dec 06 2026 - 00:49:14 EST


On Fri, 2026-12-04 at 09:42 +0800, zhangtianyang wrote:
> HI, Ruoyao
>
> 在 2026/12/3 上午11:09, Huacai Chen 写道:
> > On Thu, Dec 3, 2026 at 11:08 AM Xi Ruoyao <xry111@xxxxxxxxxxx> wrote:
> > > On Thu, 2026-12-03 at 10:56 +0800, Huacai Chen wrote:
> > > > PCIe host controller from Loongson-3B6000/3C6000 and newer revisions
> > > > of LS7A2000 supports WriteCombine (WUC) now.
> > > We've accidentally enabled WriteCombine in AOSC OS kernel configuration
> > > and we still observed display artifacts with AMD GPUs and crashes with
> > > Intel GPUs on 3B6000/3C6000.  I don't think this is real.
> Thanks for the report. The symptoms you observed with WriteCombine enabled
> are real — but they are not evidence that WC is broken on LoongArch. We have
> root-caused this class of issues on 3C/D6000.
>
> x86 write-combining (PAT WC type) comes with architecturally documented
> drain events (Intel SDM vol.3A §11.3.1): LOCK-prefixed instructions, reads/
> writes to uncached memory, interrupts, SFENCE/MFENCE — all flush the core's
> WC buffers. Note that every atomic RMW on x86 is LOCK-prefixed (lock xadd/
> cmpxchg, or implicitly-locked xchg) — there is no un-locked atomic RMW on
> x86 at all. So every mutex/atomic handoff in userspace incidentally drains
> WC buffers, and the kernel doorbell MMIO write (a UC access) drains the
> submitting core before ringing. Mesa therefore never needed explicit fences
> except in tight same-function windows (e.g. the mfence in the amdgpu
> user-queue submit path).
>
> LoongArch expresses WC via the page-table MAT field as WUC (weakly-ordered
> uncached). Now, the hardware implements the fine-grained dbar
> hints, so ordering-class barriers do what they say — and nothing more:
>
>    - C11 release/acquire compile to ordering-class "dbar 0x12/0x14";
>    - atomic RMW lowers to amadd_db / LL·SC with "dbar 0x700" (LL/SC
>      same-address barrier);
>    - none of these drain the WUC write-merge buffers;
>    - completion-class barriers (like dbar 0, i.e. kernel mb(); or wmb() =
>      dbar 0x0a) guarantee the buffered writes are pushed out.
>
> For the issue you observed, the cause is that CPU writes to WC-mapped
> VRAM/GTT (command buffers, shader code, batch buffers) may still be sitting
> in the core's WUC merge buffer when the GPU is signalled — either by a
> doorbell/userq ring, or by another CPU whose release/acquire handoff does
> not drain the writer's buffer. The GPU then reads stale data: exactly the
> display corruption you saw on AMD, and the same structural failure applies
> to the Intel driver stack. (Kernel-side LoongArch is mostly fine because
> device-facing wmb()/mb() are completion-class on LoongArch; the risk
> concentrates in userspace UMDs.)
>
> We prepared Mesa patches that add a completion barrier ("dbar 0", guarded
> by DETECT_ARCH_LOONGARCH64) at every CPU→GPU-visibility boundary.
>
> This kernel change is a forerunner and does not force WC on. The platform
> will carry one firmware bit indicating whether WC is supported/validated on
> that machine; actual enablement is deferred until the ecosystem
> (userspace fixes
> like the above, validation on 3B6000/3C6000) is mature; and it remains
> user-tunable
> — LoongArch already supports the "writecombine=" kernel command-line option
> and CONFIG_ARCH_WRITECOMBINE, so distros and end users keep final say.
>
> So there is no forced behaviour change: default stays conservative, the
> firmware bit only opens the door when the platform is ready, and anyone can
> override it locally.
>
> We believe WC is worth having on LoongArch — the GPU performance gap
> between cached-UC and WC mappings is significant — and the right path is
> to fix userspace, not to keep WC off forever. Other feedback and testing
> is very welcome.

I agree but the general policy of kernel is not to break the userspace
(even if the userspace only "happens to work"). IMO it's better to
change the CONFIG_ARCH_WRITECOMBINE option to tri-state (off,on,auto)
and explicit state "auto" can be only used if the userspace is fixed,
and "on" can be used if the userspace is fixed and the hardware is new
enough.

There's also a possible subtle issue even with the barriers fixed: in
Mesa we have:

#define p_atomic_cmpxchg(v, old, _new) \
__sync_val_compare_and_swap((v), (old), (_new))

This will be compiled to LL-SC (unless -mamcas). I hope Mesa does not
use p_atomic_cmpxchg on device memory because our LL instruction has
unpredictable result with WUC (anything other than CC per the manual).


--
Xi Ruoyao <xry111@xxxxxxxxxxx>