mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Matt Turner <mattst88@gmail.com>
To: Madhavan Srinivasan <maddy@linux.ibm.com>,
	 Michael Ellerman <mpe@ellerman.id.au>,
	Nicholas Piggin <npiggin@gmail.com>,
	 "Christophe Leroy (CS GROUP)" <chleroy@kernel.org>,
	 "Ritesh Harjani (IBM)" <ritesh.list@gmail.com>,
	 Shrikanth Hegde <sshegde@linux.ibm.com>,
	Borislav Petkov <bp@alien8.de>,  Tony Luck <tony.luck@intel.com>,
	Matt Turner <mattst88@gmail.com>
Cc: linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org,
	 linux-edac@vger.kernel.org
Subject: [PATCH 4/4] EDAC/mpc7448_l2: Add error injection
Date: Fri, 20 Nov 2026 14:33:43 -0500	[thread overview]
Message-ID: <20261120-ppc32-l2-ecc-v1-4-98fc8e6e0a32@gmail.com> (raw)
In-Reply-To: <20261120-ppc32-l2-ecc-v1-0-98fc8e6e0a32@gmail.com>

Expose the L2 error injection registers as inject_data_hi,
inject_data_lo and inject_ctrl.  Writing a CPU number to inject_trigger
injects the error into a private buffer on that CPU and reports what
was detected.

L2 machine checks are not recoverable, so machine check reporting is
masked while the injected errors are consumed.  Tag injection is not
offered: it corrupts the tags of unrelated lines too, and hung the
machine in testing.

Signed-off-by: Matt Turner <mattst88@gmail.com>
---
 arch/powerpc/include/asm/reg.h |   1 +
 drivers/edac/mpc7448_l2_edac.c | 284 ++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 284 insertions(+), 1 deletion(-)

diff --git a/arch/powerpc/include/asm/reg.h b/arch/powerpc/include/asm/reg.h
index 2f4adf5a606e..15c97ff8b12a 100644
--- a/arch/powerpc/include/asm/reg.h
+++ b/arch/powerpc/include/asm/reg.h
@@ -760,6 +760,7 @@
 #define L3CR_PMSIZ		0x00000001	/* L3 private memory size */
 
 #define SPRN_MSSCR0	0x3f6	/* Memory Subsystem Control Register 0 */
+#define   MSSCR0_L2PFE	0x00000003	/* L2 prefetch engines */
 #define SPRN_MSSSR0	0x3f7	/* Memory Subsystem Status Register 1 */
 #define   MSSSR0_L2TAG	0x00040000	/* L2 tag parity error */
 #define   MSSSR0_L2DAT	0x00020000	/* L2 data parity/ECC error */
diff --git a/drivers/edac/mpc7448_l2_edac.c b/drivers/edac/mpc7448_l2_edac.c
index a62c7a2e7147..157f6ff99cf6 100644
--- a/drivers/edac/mpc7448_l2_edac.c
+++ b/drivers/edac/mpc7448_l2_edac.c
@@ -11,12 +11,17 @@
  */
 
 #include <linux/bitfield.h>
+#include <linux/cache.h>
 #include <linux/cpu.h>
 #include <linux/device/faux.h>
 #include <linux/edac.h>
+#include <linux/gfp.h>
+#include <linux/io.h>
+#include <linux/kstrtox.h>
 #include <linux/module.h>
 #include <linux/smp.h>
 #include <linux/sprintf.h>
+#include <linux/sysfs.h>
 
 #include <asm/mpc7448_l2.h>
 #include <asm/reg.h>
@@ -25,6 +30,32 @@
 
 #define DRV_NAME	"mpc7448_l2_edac"
 
+/*
+ * Injection buffer layout.  The target line is followed by aliases one
+ * L1 way (4 KB) apart, which map to the same L1 set and evict the target
+ * from the 8-way L1 into the L2.  With the L1's PLRU replacement, eight
+ * misses evict the target when nothing else touches its set; INJ_NALIAS
+ * is twice that.  The L2 is filled in 64-byte lines of two 32-byte blocks.
+ */
+#define INJ_STRIDE	4096
+#define INJ_NALIAS	16
+#define INJ_ORDER	get_order((INJ_NALIAS + 1) * INJ_STRIDE)
+#define INJ_PATTERN	0x12345678
+
+/*
+ * Tag injection (L2ERRINJCTL[TERRIEN]) is not offered: it also corrupts
+ * the tags of unrelated lines sharing the L2 sets that are written while
+ * injection is enabled, which hung the machine in testing.
+ */
+#define INJ_CTL_MASK	(L2ERRINJCTL_ECCMB | L2ERRINJCTL_DERRIEN | \
+			 L2ERRINJCTL_ECCERRIM)
+
+struct mpc7448_l2_pdata {
+	u32 inj_hi;
+	u32 inj_lo;
+	u32 inj_ctl;
+};
+
 static struct faux_device *mpc7448_l2_fdev;
 
 struct mpc7448_l2_poll {
@@ -98,11 +129,261 @@ static void mpc7448_l2_check(struct edac_device_ctl_info *edac_dev)
 	cpus_read_unlock();
 }
 
+struct mpc7448_l2_inj {
+	u32 *buf;
+	u32 hi;
+	u32 lo;
+	u32 ctl;
+	u32 val;
+	struct mpc7448_l2_poll poll;
+};
+
+static u32 *inj_alias(u32 *a, unsigned int i)
+{
+	return (u32 *)((char *)a + i * INJ_STRIDE);
+}
+
+/* Flush the L2 lines of aliases first to last from the L1 and L2 */
+static void inj_flush(u32 *a, unsigned int first, unsigned int last)
+{
+	unsigned int i;
+
+	for (i = first; i <= last; i++) {
+		dcbf(inj_alias(a, i));
+		dcbf((char *)inj_alias(a, i) + 32);
+	}
+	mb();	/* wait for the flushes to complete */
+}
+
+/* Change a mode SPR with no cache operations or stores in flight */
+#define inj_mtspr(rn, v)					\
+do {								\
+	mb();	/* complete earlier accesses first */		\
+	mtspr(rn, v);						\
+	mb();	/* and the SPR write itself */			\
+	isync();						\
+} while (0)
+
+/*
+ * Every L2 write while injection is enabled is corrupted, so keep the
+ * window short: modify the target in the L1, enable injection, cast the
+ * target out to the L2 by loading its aliases, and disable injection.
+ * The aliases are filled into the L2 during the window and take the error
+ * too.  Then flush the aliases, read the target back, and flush it.
+ */
+static noinline u32 mpc7448_l2_inject_window(u32 *a, u32 hi, u32 lo, u32 ctl)
+{
+	unsigned int i;
+	u32 val;
+
+	inj_flush(a, 0, INJ_NALIAS);
+
+	/*
+	 * A castout that misses in the L2 goes to memory, so load the target
+	 * to allocate its L2 line before modifying it in the L1.
+	 */
+	READ_ONCE(*a);
+	WRITE_ONCE(*a, INJ_PATTERN);
+	mb();	/* modify the target before injection starts */
+
+	mtspr(SPRN_L2ERRINJHI, hi);
+	mtspr(SPRN_L2ERRINJLO, lo);
+	mtspr(SPRN_L2ERRINJCTL, ctl);
+	mb();	/* start injection before the castout */
+
+	for (i = 1; i <= INJ_NALIAS; i++)
+		READ_ONCE(*inj_alias(a, i));
+	mb();	/* wait for the castout and the alias fills */
+
+	mtspr(SPRN_L2ERRINJCTL, 0);
+	mtspr(SPRN_L2ERRINJHI, 0);
+	mtspr(SPRN_L2ERRINJLO, 0);
+	mb();	/* stop injection before anything else reaches the L2 */
+
+	inj_flush(a, 1, INJ_NALIAS);
+	val = READ_ONCE(*a);
+	inj_flush(a, 0, 0);
+
+	return val;
+}
+
+static void mpc7448_l2_inject_cpu(void *info)
+{
+	struct mpc7448_l2_inj *inj = info;
+	u32 inten = mfspr(SPRN_L2ERRINTEN);
+	u32 msscr0 = mfspr(SPRN_MSSCR0);
+	u32 *a = inj->buf;
+
+	/* The prefetch engines would fill (and corrupt) other lines */
+	if (msscr0 & MSSCR0_L2PFE)
+		inj_mtspr(SPRN_MSSCR0, msscr0 & ~MSSCR0_L2PFE);
+
+	/*
+	 * A dry run without injection leaves only the buffer in the
+	 * target's L1 set and the window's code in the L1 instruction
+	 * cache, so nothing else is cast out to the L2 during the window.
+	 */
+	mpc7448_l2_inject_window(a, 0, 0, 0);
+
+	/* L2 machine checks are not recoverable, so poll for these errors */
+	inj_mtspr(SPRN_L2ERRINTEN, 0);
+
+	inj->val = mpc7448_l2_inject_window(a, inj->hi, inj->lo, inj->ctl);
+	mpc7448_l2_poll_cpu(&inj->poll);
+
+	inj_mtspr(SPRN_L2ERRINTEN, inten);
+	if (msscr0 & MSSCR0_L2PFE)
+		inj_mtspr(SPRN_MSSCR0, msscr0);
+}
+
+static ssize_t inject_show(u32 val, char *data)
+{
+	return sysfs_emit(data, "0x%08x\n", val);
+}
+
+static ssize_t inject_store(u32 *val, u32 mask, const char *data,
+			    size_t count)
+{
+	u32 v;
+	int ret;
+
+	ret = kstrtou32(data, 0, &v);
+	if (ret)
+		return ret;
+	if (v & ~mask)
+		return -EINVAL;
+
+	*val = v;
+	return count;
+}
+
+static ssize_t inject_data_hi_show(struct edac_device_ctl_info *edac_dev,
+				   char *data)
+{
+	struct mpc7448_l2_pdata *pdata = edac_dev->pvt_info;
+
+	return inject_show(pdata->inj_hi, data);
+}
+
+static ssize_t inject_data_hi_store(struct edac_device_ctl_info *edac_dev,
+				    const char *data, size_t count)
+{
+	struct mpc7448_l2_pdata *pdata = edac_dev->pvt_info;
+
+	return inject_store(&pdata->inj_hi, ~0U, data, count);
+}
+
+static ssize_t inject_data_lo_show(struct edac_device_ctl_info *edac_dev,
+				   char *data)
+{
+	struct mpc7448_l2_pdata *pdata = edac_dev->pvt_info;
+
+	return inject_show(pdata->inj_lo, data);
+}
+
+static ssize_t inject_data_lo_store(struct edac_device_ctl_info *edac_dev,
+				    const char *data, size_t count)
+{
+	struct mpc7448_l2_pdata *pdata = edac_dev->pvt_info;
+
+	return inject_store(&pdata->inj_lo, ~0U, data, count);
+}
+
+static ssize_t inject_ctrl_show(struct edac_device_ctl_info *edac_dev,
+				char *data)
+{
+	struct mpc7448_l2_pdata *pdata = edac_dev->pvt_info;
+
+	return inject_show(pdata->inj_ctl, data);
+}
+
+static ssize_t inject_ctrl_store(struct edac_device_ctl_info *edac_dev,
+				 const char *data, size_t count)
+{
+	struct mpc7448_l2_pdata *pdata = edac_dev->pvt_info;
+
+	return inject_store(&pdata->inj_ctl, INJ_CTL_MASK, data, count);
+}
+
+static ssize_t inject_trigger_store(struct edac_device_ctl_info *edac_dev,
+				    const char *data, size_t count)
+{
+	struct mpc7448_l2_pdata *pdata = edac_dev->pvt_info;
+	struct mpc7448_l2_inj inj;
+	struct page *page;
+	unsigned int cpu;
+	int ret;
+
+	ret = kstrtouint(data, 0, &cpu);
+	if (ret)
+		return ret;
+	if (cpu >= nr_cpu_ids)
+		return -EINVAL;
+
+	page = alloc_pages(GFP_KERNEL, INJ_ORDER);
+	if (!page)
+		return -ENOMEM;
+
+	inj.buf = page_address(page);
+	inj.hi = pdata->inj_hi;
+	inj.lo = pdata->inj_lo;
+	inj.ctl = pdata->inj_ctl;
+
+	cpus_read_lock();
+	if (cpu_online(cpu))
+		ret = smp_call_function_single(cpu, mpc7448_l2_inject_cpu,
+					       &inj, 1);
+	else
+		ret = -ENXIO;
+	cpus_read_unlock();
+
+	if (!ret)
+		edac_device_printk(edac_dev, KERN_INFO,
+				   "cpu%u: injected ctl 0x%08x data 0x%08x_%08x at 0x%llx: read 0x%08x (wrote 0x%08x)\n",
+				   cpu, inj.ctl, inj.hi, inj.lo,
+				   (unsigned long long)virt_to_phys(inj.buf),
+				   inj.val, INJ_PATTERN);
+
+	__free_pages(page, INJ_ORDER);
+
+	if (ret)
+		return ret;
+
+	mpc7448_l2_report(edac_dev, cpu, &inj.poll);
+	return count;
+}
+
+static const struct edac_dev_sysfs_attribute mpc7448_l2_sysfs_attributes[] = {
+	{
+		.attr = { .name = "inject_data_hi", .mode = 0600 },
+		.show = inject_data_hi_show,
+		.store = inject_data_hi_store,
+	},
+	{
+		.attr = { .name = "inject_data_lo", .mode = 0600 },
+		.show = inject_data_lo_show,
+		.store = inject_data_lo_store,
+	},
+	{
+		.attr = { .name = "inject_ctrl", .mode = 0600 },
+		.show = inject_ctrl_show,
+		.store = inject_ctrl_store,
+	},
+	{
+		.attr = { .name = "inject_trigger", .mode = 0200 },
+		.store = inject_trigger_store,
+	},
+	{
+		.attr = { .name = NULL },
+	},
+};
+
 static int mpc7448_l2_probe(struct faux_device *fdev)
 {
 	struct edac_device_ctl_info *edac_dev;
 
-	edac_dev = edac_device_alloc_ctl_info(0, "cpu", nr_cpu_ids, "L", 1, 2,
+	edac_dev = edac_device_alloc_ctl_info(sizeof(struct mpc7448_l2_pdata),
+					      "cpu", nr_cpu_ids, "L", 1, 2,
 					      edac_device_alloc_index());
 	if (!edac_dev)
 		return -ENOMEM;
@@ -112,6 +393,7 @@ static int mpc7448_l2_probe(struct faux_device *fdev)
 	edac_dev->ctl_name = "MPC7448_L2";
 	edac_dev->dev_name = dev_name(&fdev->dev);
 	edac_dev->edac_check = mpc7448_l2_check;
+	edac_dev->sysfs_attributes = mpc7448_l2_sysfs_attributes;
 
 	if (edac_device_add_device(edac_dev)) {
 		edac_device_free_ctl_info(edac_dev);

-- 
2.56.1


      parent reply	other threads:[~2026-11-20 19:33 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 19:33 [PATCH 0/4] powerpc/32: Report MPC7448 L2 cache errors Matt Turner
2026-11-20 19:33 ` [PATCH 1/4] powerpc/32: Decode MPC745x L2 and L3 cache machine checks Matt Turner
2026-11-20 19:33 ` [PATCH 2/4] powerpc/32: Enable and decode MPC7448 L2 ECC " Matt Turner
2026-11-20 19:33 ` [PATCH 3/4] EDAC/mpc7448_l2: Add a driver for the MPC7448 L2 cache Matt Turner
2026-11-20 19:33 ` Matt Turner [this message]

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=20261120-ppc32-l2-ecc-v1-4-98fc8e6e0a32@gmail.com \
    --to=mattst88@gmail.com \
    --cc=bp@alien8.de \
    --cc=chleroy@kernel.org \
    --cc=linux-edac@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=maddy@linux.ibm.com \
    --cc=mpe@ellerman.id.au \
    --cc=npiggin@gmail.com \
    --cc=ritesh.list@gmail.com \
    --cc=sshegde@linux.ibm.com \
    --cc=tony.luck@intel.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

all inboxes | Powered by JetHome®