Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: "William A. Kennington III" <william@wkennington.com>
To: Andrew Jeffery <andrew@codeconstruct.com.au>,
	Avi Fishman <avifishman70@gmail.com>,
	Tomer Maimon <tmaimon77@gmail.com>,
	Tali Perry <tali.perry1@gmail.com>,
	Russell King <linux@armlinux.org.uk>
Cc: Patrick Venture <venture@google.com>,
	Nancy Yuen <yuenn@google.com>,
	Benjamin Fair <benjaminfair@google.com>,
	openbmc@lists.ozlabs.org, linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org,
	"William A. Kennington III" <william@wkennington.com>
Subject: [PATCH v3] ARM: npcm: Add CPU hotplug callbacks for kexec support
Date: Sat, 28 Nov 2026 09:52:54 +0000	[thread overview]
Message-ID: <20261128095254.358131-1-william@wkennington.com> (raw)

kexec_load() fails with -EINVAL on NPCM7xx because the platform does not
provide the CPU hotplug callbacks that machine_kexec_prepare() requires
on SMP systems. Add them.

The NPCM7xx cannot reset or power down a single core, so hand an offlined
core back to the boot ROM instead of leaving it in a WFI loop in kernel
text. The dying core switches to a static stack, takes the identity
mapping, disables caches and branch prediction, flushes L1, leaves
coherency, turns the MMU off and jumps to the reset vector in the ROM.
The ROM then parks it as it does after a real reset, overwriting SCRPAD
and waiting in WFE for npcm7xx_smp_boot_secondary(). The core can
therefore be brought back online, and it no longer executes from RAM that
a kexec'd kernel is free to overwrite.

cpu_kill() waits for the ROM to overwrite the kernel entry point left in
SCRPAD before reporting the core dead.

Map the GCR once in smp_prepare_cpus() since it is now also needed after
init.

Signed-off-by: William A. Kennington III <william@wkennington.com>
---
Changes in v3:
- Park offlined secondary cores back in the boot ROM reset vector instead of a
  WFI loop in kernel text so cores can be re-onlined and do not execute from
  RAM across kexec.
- Poll GCR SCRPAD in cpu_kill() until the ROM overwrites the kernel entry
  point, and map GCR once in smp_prepare_cpus().
- Drop Tomer's Reviewed-by due to the implementation rewrite.
- Link to v2: https://lore.kernel.org/r/20231102101009.15104-1-william@wkennington.com

Changes in v2:
- Fix NPCM7xx/NPCN8xx typo in the commit message.

 arch/arm/mach-npcm/headsmp.S |  38 ++++++++++++
 arch/arm/mach-npcm/platsmp.c | 109 ++++++++++++++++++++++++++++-------
 2 files changed, 127 insertions(+), 20 deletions(-)

diff --git a/arch/arm/mach-npcm/headsmp.S b/arch/arm/mach-npcm/headsmp.S
index 84d2b6daaf0b..1718b70b7e1d 100644
--- a/arch/arm/mach-npcm/headsmp.S
+++ b/arch/arm/mach-npcm/headsmp.S
@@ -5,6 +5,7 @@
 #include <linux/linkage.h>
 #include <linux/init.h>
 #include <asm/assembler.h>
+#include <asm/cp15.h>
 
 .arch armv7-a
 
@@ -17,3 +18,40 @@ ENTRY(npcm7xx_secondary_startup)
 
 	b	secondary_startup
 ENDPROC(npcm7xx_secondary_startup)
+
+#ifdef CONFIG_HOTPLUG_CPU
+/*
+ * void npcm7xx_cpu_park(unsigned long phys_reset, unsigned long rom_addr)
+ *
+ * Exit cache coherency and jump back to the boot ROM reset vector via
+ * cpu_reset() in the identity mapping.  Must be called after
+ * setup_mm_for_reboot() with IRQs and FIQs disabled, and must not access
+ * memory or the stack once SCTLR.C is cleared.
+ */
+ENTRY(npcm7xx_cpu_park)
+	mov	r7, r0
+	mov	r8, r1
+
+	mrc	p15, 0, r0, c1, c0, 0
+	bic	r0, r0, #(CR_I | CR_Z)
+	bic	r0, r0, #(CR_C | CR_A)
+	mcr	p15, 0, r0, c1, c0, 0
+	isb
+
+	bl	v7_flush_dcache_louis
+
+	mrc	p15, 0, r0, c1, c0, 1
+	bic	r0, r0, #(1 << 6)		@ clear ACTLR.SMP
+	mcr	p15, 0, r0, c1, c0, 1
+
+	mov	r0, #0
+	mcr	p15, 0, r0, c7, c5, 0		@ ICIALLU
+	mcr	p15, 0, r0, c7, c5, 6		@ BPIALL
+	dsb
+	isb
+
+	mov	r0, r8
+	mov	r1, #0
+	bx	r7
+ENDPROC(npcm7xx_cpu_park)
+#endif
diff --git a/arch/arm/mach-npcm/platsmp.c b/arch/arm/mach-npcm/platsmp.c
index 4c1fc9983746..6055d44075ac 100644
--- a/arch/arm/mach-npcm/platsmp.c
+++ b/arch/arm/mach-npcm/platsmp.c
@@ -7,37 +7,35 @@
 #include <linux/delay.h>
 #include <linux/smp.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/of.h>
 #include <linux/of_address.h>
 #include <asm/cacheflush.h>
+#include <asm/idmap.h>
+#include <asm/irqflags.h>
+#include <asm/proc-fns.h>
 #include <asm/smp.h>
 #include <asm/smp_plat.h>
 #include <asm/smp_scu.h>
 
 #define NPCM7XX_SCRPAD_REG 0x13c
 
+/* The boot ROM is mapped at the high vectors and holds the reset vector. */
+#define NPCM7XX_ROM_RESET_VECTOR 0xffff0000
+
 extern void npcm7xx_secondary_startup(void);
+#ifdef CONFIG_HOTPLUG_CPU
+extern void __noreturn npcm7xx_cpu_park(unsigned long phys_reset,
+					unsigned long rom_addr);
+#endif
+
+static void __iomem *gcr_base;
 
 static int npcm7xx_smp_boot_secondary(unsigned int cpu,
 				      struct task_struct *idle)
 {
-	struct device_node *gcr_np;
-	void __iomem *gcr_base;
-	int ret = 0;
-
-	gcr_np = of_find_compatible_node(NULL, NULL, "nuvoton,npcm750-gcr");
-	if (!gcr_np) {
-		pr_err("no gcr device node\n");
-		ret = -ENODEV;
-		goto out;
-	}
-	gcr_base = of_iomap(gcr_np, 0);
-	of_node_put(gcr_np);
-	if (!gcr_base) {
-		pr_err("could not iomap gcr");
-		ret = -ENOMEM;
-		goto out;
-	}
+	if (!gcr_base)
+		return -ENODEV;
 
 	/* give boot ROM kernel start address. */
 	iowrite32(__pa_symbol(npcm7xx_secondary_startup), gcr_base +
@@ -45,13 +43,12 @@ static int npcm7xx_smp_boot_secondary(unsigned int cpu,
 	/* make sure the previous write is seen by all observers. */
 	dsb_sev();
 
-	iounmap(gcr_base);
-out:
-	return ret;
+	return 0;
 }
 
 static void __init npcm7xx_smp_prepare_cpus(unsigned int max_cpus)
 {
+	struct device_node *gcr_np;
 	struct device_node *scu_np;
 	void __iomem *scu_base;
 
@@ -70,11 +67,83 @@ static void __init npcm7xx_smp_prepare_cpus(unsigned int max_cpus)
 	scu_enable(scu_base);
 
 	iounmap(scu_base);
+
+	gcr_np = of_find_compatible_node(NULL, NULL, "nuvoton,npcm750-gcr");
+	if (!gcr_np) {
+		pr_err("no gcr device node\n");
+		return;
+	}
+	gcr_base = of_iomap(gcr_np, 0);
+	of_node_put(gcr_np);
+	if (!gcr_base)
+		pr_err("could not iomap gcr\n");
+}
+
+#ifdef CONFIG_HOTPLUG_CPU
+/*
+ * The dying CPU moves to this stack before it takes the identity mapping,
+ * because its vmapped idle stack is not guaranteed to be mapped there. One
+ * stack is enough: CPU hotplug is serialised and npcm7xx_cpu_kill() waits
+ * for the CPU to leave the kernel.
+ */
+static u64 npcm7xx_cpu_die_stack[64];
+
+/*
+ * The SoC cannot reset or power down a single core, so hand the core back
+ * to the boot ROM instead. Entered through the reset vector with the MMU
+ * and caches off, the ROM parks a secondary core the same way it does
+ * after a real reset: it overwrites SCRPAD and waits in WFE for
+ * npcm7xx_smp_boot_secondary(). The core no longer executes from RAM.
+ */
+static void __noreturn npcm7xx_cpu_reset(void)
+{
+	unsigned long phys_reset = virt_to_idmap(cpu_reset);
+
+	setup_mm_for_reboot();
+	npcm7xx_cpu_park(phys_reset, NPCM7XX_ROM_RESET_VECTOR);
+}
+
+static void npcm7xx_cpu_die(unsigned int cpu)
+{
+	u64 *stack = npcm7xx_cpu_die_stack +
+		     ARRAY_SIZE(npcm7xx_cpu_die_stack);
+
+	local_fiq_disable();
+
+	asm volatile("mov\tsp, %0\n\t"
+		     "mov\tfp, #0\n\t"
+		     "bx\t%1"
+		     :
+		     : "r" (stack), "r" (npcm7xx_cpu_reset)
+		     : "memory");
+
+	unreachable();
+}
+
+static int npcm7xx_cpu_kill(unsigned int cpu)
+{
+	u32 val;
+
+	if (!gcr_base)
+		return 0;
+
+	/*
+	 * The boot ROM overwrites the kernel entry point left in SCRPAD once
+	 * the core is parked, so wait for that before declaring it dead.
+	 */
+	return !readl_poll_timeout(gcr_base + NPCM7XX_SCRPAD_REG, val,
+				   val != __pa_symbol(npcm7xx_secondary_startup),
+				   100, 100 * USEC_PER_MSEC);
 }
+#endif
 
 static struct smp_operations npcm7xx_smp_ops __initdata = {
 	.smp_prepare_cpus = npcm7xx_smp_prepare_cpus,
 	.smp_boot_secondary = npcm7xx_smp_boot_secondary,
+#ifdef CONFIG_HOTPLUG_CPU
+	.cpu_die = npcm7xx_cpu_die,
+	.cpu_kill = npcm7xx_cpu_kill,
+#endif
 };
 
 CPU_METHOD_OF_DECLARE(npcm7xx_smp, "nuvoton,npcm750-smp", &npcm7xx_smp_ops);


                 reply	other threads:[~2026-11-28  9:53 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261128095254.358131-1-william@wkennington.com \
    --to=william@wkennington.com \
    --cc=andrew@codeconstruct.com.au \
    --cc=avifishman70@gmail.com \
    --cc=benjaminfair@google.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=openbmc@lists.ozlabs.org \
    --cc=tali.perry1@gmail.com \
    --cc=tmaimon77@gmail.com \
    --cc=venture@google.com \
    --cc=yuenn@google.com \
    /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