Re: [PATCH] LoongArch: detect and disable sc.q if erratic

From: Thomas Weißschuh

Date: Thu Apr 02 2026 - 02:47:58 EST


On 2026-04-02 12:47:30+0800, Xi Ruoyao wrote:

(...)

> +/*
> + * Some LoongArch has broken sc.q which incorrectly handles the upper word
> + * when the lower word is zero. Newer firmware versions (such as the 202602
> + * release from Loongson) seem to contain a workaround for this issue.
> + *
> + * Disable sc.q if erratic to ensure reliability and compatibility.
> + */
> +static bool sc_q_is_sane(void)
> +{
> + struct {
> + long word[2];
> + } __aligned(16) mem;
> + register long tmp;
> +
> + asm (
> + "1:ll.d\t$r0, %[mem]\n\t"
> + "move\t%[tmp], $r0\n\t"
> + "sc.q\t%[tmp], %[one], %[mem]\n\t"

This probably needs to be gated behind CONFIG_AS_HAS_SCQ_EXTENSION to
avoid errors on older toolchains.

> + "beqz\t%[tmp], 1b"
> + : [mem] "=ZB" (mem), [tmp] "=&r" (tmp)
> + : [one] "r" (1));
> +
> + if (mem.word[1] != 1) {
> + pr_warn_once("Warning: sc.q is erratic on this platform, disabling for both kernel and HWCAP. Please try a firmware update.");
> + return false;
> + }
> +
> + return true;
> +}