[PATCH 2/6] irqchip: Add NXP S32N79 MRU MSI controller driver
From: Ghennadi Procopciuc via B4 Relay
Date: Fri Dec 04 2026 - 07:58:11 EST
From: Ghennadi Procopciuc <ghennadi.procopciuc@xxxxxxxxxxx>
On S32N79, a hardware erratum prevents the MSI writes of the integrated
NETC endpoints from being translated by the GIC ITS. Add a driver that
uses the Message Receive Unit (MRU) blocks as the MSI controller for
these endpoints instead.
Each MRU has three channels. A write to the first mailbox of a channel
sets its status bit and raises the channel interrupt, and the channel
interrupts of an MRU are ORed into one GIC SPI. A chained handler reads
the status of the channels, clears it and dispatches the vector of every
channel with a pending message. The status is cleared before the
dispatch, because the vector may run later on another CPU.
Each vector gets a channel of its own. The channels sit in separate
64 KiB pages, so PCI functions that are isolated by the IOMMU cannot
write to each other's mailboxes. The channels of all the MRUs are handed
out channel index first, so consecutive vectors land on different MRUs
and do not share an SPI.
Since the channels of an MRU share one SPI, per-vector affinity uses the
interrupt redirection for demultiplexed interrupts: if the SPI is taken
on a CPU outside the affinity mask of a vector, the vector handler runs
from irq_work on a CPU that is in the mask. The SPIs of the MRUs are
spread over the online CPUs at probe.
When the endpoint sits behind an IOMMU, the mailbox page is mapped with
iommu_dma_prepare_msi(). That helper is not exported, so the driver can
only be built in. It is enabled by default on ARCH_S32, because the NETC
endpoints have no working MSI parent without it.
Signed-off-by: Ghennadi Procopciuc <ghennadi.procopciuc@xxxxxxxxxxx>
---
drivers/irqchip/Kconfig | 21 +++
drivers/irqchip/Makefile | 1 +
drivers/irqchip/irq-mru-msi.c | 402 ++++++++++++++++++++++++++++++++++++++++++
3 files changed, 424 insertions(+)
diff --git a/drivers/irqchip/Kconfig b/drivers/irqchip/Kconfig
index 136bab41f801..b80243d09791 100644
--- a/drivers/irqchip/Kconfig
+++ b/drivers/irqchip/Kconfig
@@ -852,4 +852,25 @@ config IRQCHIP_XILINX_SMMU_CSR
ZynqMP-class platforms where a vendor CSR block mediates SMMU
interrupts.
+config NXP_MRU_MSI
+ bool "NXP S32N79 MRU MSI controller"
+ depends on ARCH_S32 || COMPILE_TEST
+ depends on OF && PCI_MSI
+ default ARCH_S32
+ select GENERIC_IRQ_EFFECTIVE_AFF_MASK if SMP
+ select IRQ_DOMAIN_HIERARCHY
+ select IRQ_MSI_IOMMU
+ select IRQ_MSI_LIB
+ help
+ Support for the Message Receive Unit (MRU) block of the NXP S32N79
+ used as an MSI and MSI-X controller. The mailboxes of the MRU
+ instances are the targets of the MSI writes of PCI devices, and
+ the channel interrupts are forwarded to the GIC as SPIs.
+
+ The integrated NETC endpoints of the S32N79 need it, because
+ their MSI writes do not reach the GIC ITS.
+
+ This driver cannot be built as a module because the IOMMU MSI
+ mapping helper it calls is not exported.
+
endmenu
diff --git a/drivers/irqchip/Makefile b/drivers/irqchip/Makefile
index 16a5fd126c9a..379781d3e2b3 100644
--- a/drivers/irqchip/Makefile
+++ b/drivers/irqchip/Makefile
@@ -136,3 +136,4 @@ obj-$(CONFIG_MCHP_EIC) += irq-mchp-eic.o
obj-$(CONFIG_SOPHGO_SG2042_MSI) += irq-sg2042-msi.o
obj-$(CONFIG_SUNPLUS_SP7021_INTC) += irq-sp7021-intc.o
obj-$(CONFIG_IRQCHIP_XILINX_SMMU_CSR) += irq-xilinx-smmu-csr.o
+obj-$(CONFIG_NXP_MRU_MSI) += irq-mru-msi.o
diff --git a/drivers/irqchip/irq-mru-msi.c b/drivers/irqchip/irq-mru-msi.c
new file mode 100644
index 000000000000..13d184b2ca2c
--- /dev/null
+++ b/drivers/irqchip/irq-mru-msi.c
@@ -0,0 +1,402 @@
+// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
+/*
+ * Copyright 2025 NXP
+ *
+ * This driver exposes a set of Message Receive Units (MRUs) as an MSI
+ * controller. Each MRU is organized into multiple channels, and each channel
+ * contains a mailbox. A write to the first mailbox of a channel raises the
+ * interrupt of that channel.
+ *
+ * The channel interrupts of an MRU are ORed into a single SPI, which a chained
+ * handler demultiplexes. All the channels of all the MRUs are tracked in one
+ * bitmap, ordered by channel index first:
+ *
+ * ----------------||---------------||-----------------
+ * Bitmap bits | 0 | 1 | 2 | 3 || 4 | 5 | 6 | 7 || 8 | 9 | 10 | 11 |
+ * ----------------||---------------||-----------------
+ * MRU Index 0 1 2 3 || 0 1 2 3 || 0 1 2 3
+ * Channel index 0 0 0 0 || 1 1 1 1 || 2 2 2 2
+ *
+ * Assumptions for the example above:
+ * - Number of MRUs: 4
+ * - Number of channels per MRU: 3
+ *
+ * The channels are handed out in bitmap order, so consecutive vectors land on
+ * different MRUs and therefore on different SPIs. The redirection only moves
+ * the vector handler to a CPU in its affinity mask: the SPI is still taken and
+ * demultiplexed on the CPU it is routed to. Spreading the vectors over the
+ * SPIs keeps that work from piling up on a single CPU.
+ */
+#include <linux/bitmap.h>
+#include <linux/bits.h>
+#include <linux/cpumask.h>
+#include <linux/device.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/iommu.h>
+#include <linux/irq.h>
+#include <linux/irqchip/chained_irq.h>
+#include <linux/irqchip/irq-msi-lib.h>
+#include <linux/irqdesc.h>
+#include <linux/irqdomain.h>
+#include <linux/lockdep.h>
+#include <linux/msi.h>
+#include <linux/numa.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/sizes.h>
+
+#define MRU_CHANS_PER_MRU 3
+
+/* Each channel has a 16 byte block of control registers. */
+#define MRU_CH_STRIDE 0x10
+#define MRU_CH_CFG0 0x0
+#define MRU_CH_MBSTAT 0x8
+
+#define MRU_CFG0_CHE BIT(0)
+#define MRU_CFG0_CHR BIT(1)
+#define MRU_CFG0_IE BIT(2)
+#define MRU_CFG0_MBE0 BIT(16)
+
+#define MRU_MBSTAT_MBS0 BIT(16)
+
+/*
+ * The first 64 KiB page holds the channel controls. The mailboxes of channel n
+ * start at the page n + 1.
+ */
+#define MRU_PAGE_SIZE SZ_64K
+#define MRU_REGION_SIZE ((MRU_CHANS_PER_MRU + 1) * MRU_PAGE_SIZE)
+
+struct mru_msi;
+
+struct mru {
+ struct mru_msi *priv;
+ void __iomem *base;
+ phys_addr_t addr;
+ unsigned int index;
+ int irq;
+};
+
+struct mru_msi {
+ struct irq_domain *domain;
+ unsigned long *chans_bitmap;
+ struct device *dev;
+ struct mru *mrus;
+ unsigned int nmrus;
+ unsigned int nchans;
+};
+
+static unsigned int mru_pos2mru(const struct mru_msi *priv, unsigned int pos)
+{
+ return pos % priv->nmrus;
+}
+
+static unsigned int mru_pos2chan(const struct mru_msi *priv, unsigned int pos)
+{
+ return pos / priv->nmrus;
+}
+
+static unsigned int mru_mru2pos(const struct mru_msi *priv, unsigned int mru,
+ unsigned int chan)
+{
+ return chan * priv->nmrus + mru;
+}
+
+static void __iomem *mru_chan_base(const struct mru_msi *priv,
+ unsigned int pos)
+{
+ const struct mru *mru = &priv->mrus[mru_pos2mru(priv, pos)];
+
+ return mru->base + mru_pos2chan(priv, pos) * MRU_CH_STRIDE;
+}
+
+static phys_addr_t mru_mailbox_phys(const struct mru_msi *priv,
+ unsigned int pos)
+{
+ const struct mru *mru = &priv->mrus[mru_pos2mru(priv, pos)];
+
+ return mru->addr + (mru_pos2chan(priv, pos) + 1) * MRU_PAGE_SIZE;
+}
+
+/*
+ * The channel registers ignore writes while CHE is clear, so CHE has to be set
+ * by a write of its own before the other bits can be changed. Only CHE and
+ * CHR are writable while CHE is clear.
+ */
+static void mru_chan_enable(void __iomem *chan)
+{
+ u32 cfg0 = readl(chan + MRU_CH_CFG0);
+
+ cfg0 |= MRU_CFG0_CHE;
+ writel(cfg0, chan + MRU_CH_CFG0);
+
+ /*
+ * Only mailbox 0 is enabled. The mailboxes of a channel sit in one
+ * IOMMU page, so a device that may write one of them may write all
+ * of them. Every vector therefore gets a channel of its own.
+ */
+ cfg0 |= MRU_CFG0_MBE0;
+ writel(cfg0, chan + MRU_CH_CFG0);
+
+ /*
+ * A message left from before the channel was disabled would raise the
+ * interrupt as soon as IE is set.
+ */
+ writel(MRU_MBSTAT_MBS0, chan + MRU_CH_MBSTAT);
+
+ cfg0 |= MRU_CFG0_IE;
+ writel(cfg0, chan + MRU_CH_CFG0);
+}
+
+static void mru_chan_disable(void __iomem *chan)
+{
+ u32 cfg0 = readl(chan + MRU_CH_CFG0);
+
+ cfg0 &= ~MRU_CFG0_IE;
+ writel(cfg0, chan + MRU_CH_CFG0);
+
+ cfg0 &= ~MRU_CFG0_MBE0;
+ writel(cfg0, chan + MRU_CH_CFG0);
+
+ cfg0 &= ~MRU_CFG0_CHE;
+ writel(cfg0, chan + MRU_CH_CFG0);
+}
+
+/*
+ * The chained handler clears the status of the channel before it dispatches
+ * the vector. The vector can run later on another CPU, and by then the next
+ * message must be able to raise the SPI again. The flow handler of the device
+ * domain still needs an irq_ack() to call.
+ */
+static void mru_msi_irq_ack(struct irq_data *d)
+{
+}
+
+static void mru_msi_compose_msg(struct irq_data *d, struct msi_msg *msg)
+{
+ struct mru_msi *priv = irq_data_get_irq_chip_data(d);
+
+ msi_msg_set_addr(irq_data_get_msi_desc(d), msg,
+ mru_mailbox_phys(priv, d->hwirq));
+ msg->data = 0;
+}
+
+static const struct irq_chip mru_msi_parent_chip = {
+ .name = "MRU",
+ .irq_ack = mru_msi_irq_ack,
+ .irq_compose_msi_msg = mru_msi_compose_msg,
+#ifdef CONFIG_SMP
+ .irq_set_affinity = irq_chip_redirect_set_affinity,
+#endif
+};
+
+static int mru_msi_domain_alloc(struct irq_domain *domain, unsigned int virq,
+ unsigned int nr_irqs, void *args)
+{
+ struct mru_msi *priv = domain->host_data;
+ msi_alloc_info_t *info = args;
+ unsigned int pos;
+ int ret;
+
+ lockdep_assert_held(&domain->root->mutex);
+
+ if (WARN_ON_ONCE(nr_irqs != 1))
+ return -EINVAL;
+
+ pos = find_first_zero_bit(priv->chans_bitmap, priv->nchans);
+ if (pos >= priv->nchans)
+ return -ENOSPC;
+
+ ret = iommu_dma_prepare_msi(info->desc, mru_mailbox_phys(priv, pos));
+ if (ret)
+ return ret;
+
+ __set_bit(pos, priv->chans_bitmap);
+
+ irq_domain_set_info(domain, virq, pos, &mru_msi_parent_chip, priv,
+ handle_edge_irq, NULL, NULL);
+
+ mru_chan_enable(mru_chan_base(priv, pos));
+
+ return 0;
+}
+
+static void mru_msi_domain_free(struct irq_domain *domain, unsigned int virq,
+ unsigned int nr_irqs)
+{
+ struct irq_data *d = irq_domain_get_irq_data(domain, virq);
+ struct mru_msi *priv = domain->host_data;
+
+ lockdep_assert_held(&domain->root->mutex);
+
+ mru_chan_disable(mru_chan_base(priv, d->hwirq));
+ __clear_bit(d->hwirq, priv->chans_bitmap);
+}
+
+static const struct irq_domain_ops mru_msi_domain_ops = {
+ .select = msi_lib_irq_domain_select,
+ .alloc = mru_msi_domain_alloc,
+ .free = mru_msi_domain_free,
+};
+
+#define MRU_MSI_FLAGS_REQUIRED (MSI_FLAG_USE_DEF_DOM_OPS | \
+ MSI_FLAG_USE_DEF_CHIP_OPS)
+
+#define MRU_MSI_FLAGS_SUPPORTED (MSI_GENERIC_FLAGS_MASK | \
+ MSI_FLAG_PCI_MSIX)
+
+static const struct msi_parent_ops mru_msi_parent_ops = {
+ .required_flags = MRU_MSI_FLAGS_REQUIRED,
+ .supported_flags = MRU_MSI_FLAGS_SUPPORTED,
+ .chip_flags = MSI_CHIP_FLAG_SET_ACK,
+ .bus_select_token = DOMAIN_BUS_NEXUS,
+ .bus_select_mask = MATCH_PCI_MSI,
+ .prefix = "MRU-",
+ .init_dev_msi_info = msi_lib_init_dev_msi_info,
+};
+
+static void mru_msi_irq_handler(struct irq_desc *desc)
+{
+ struct mru *mru = irq_desc_get_handler_data(desc);
+ struct irq_chip *chip = irq_desc_get_chip(desc);
+ struct mru_msi *priv = mru->priv;
+ unsigned int chan, pos;
+
+ chained_irq_enter(chip, desc);
+
+ for (chan = 0; chan < MRU_CHANS_PER_MRU; chan++) {
+ void __iomem *mbstat = mru->base + chan * MRU_CH_STRIDE +
+ MRU_CH_MBSTAT;
+
+ if (!(readl(mbstat) & MRU_MBSTAT_MBS0))
+ continue;
+
+ writel(MRU_MBSTAT_MBS0, mbstat);
+
+ pos = mru_mru2pos(priv, mru->index, chan);
+ if (!generic_handle_demux_domain_irq(priv->domain, pos))
+ dev_err_ratelimited(priv->dev,
+ "unexpected message on MRU%u channel %u\n",
+ mru->index, chan);
+ }
+
+ chained_irq_exit(chip, desc);
+}
+
+static int mru_msi_create_domain(struct mru_msi *priv)
+{
+ struct irq_domain_info info = {
+ .fwnode = dev_fwnode(priv->dev),
+ .size = priv->nchans,
+ .ops = &mru_msi_domain_ops,
+ .host_data = priv,
+ };
+
+ priv->domain = msi_create_parent_irq_domain(&info, &mru_msi_parent_ops);
+ if (!priv->domain)
+ return dev_err_probe(priv->dev, -ENOMEM,
+ "failed to create the MSI parent domain\n");
+
+ return 0;
+}
+
+static int mru_msi_probe(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ struct mru_msi *priv;
+ struct resource *res;
+ unsigned int i, chan;
+ int ret;
+
+ priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+ if (!priv)
+ return -ENOMEM;
+
+ priv->dev = dev;
+
+ while (platform_get_resource(pdev, IORESOURCE_MEM, priv->nmrus))
+ priv->nmrus++;
+
+ if (!priv->nmrus)
+ return dev_err_probe(dev, -EINVAL, "no MRU register space\n");
+
+ priv->nchans = priv->nmrus * MRU_CHANS_PER_MRU;
+
+ priv->mrus = devm_kcalloc(dev, priv->nmrus, sizeof(*priv->mrus),
+ GFP_KERNEL);
+ if (!priv->mrus)
+ return -ENOMEM;
+
+ priv->chans_bitmap = devm_bitmap_zalloc(dev, priv->nchans, GFP_KERNEL);
+ if (!priv->chans_bitmap)
+ return -ENOMEM;
+
+ for (i = 0; i < priv->nmrus; i++) {
+ struct mru *mru = &priv->mrus[i];
+
+ mru->priv = priv;
+ mru->index = i;
+
+ mru->base = devm_platform_get_and_ioremap_resource(pdev, i,
+ &res);
+ if (IS_ERR(mru->base))
+ return PTR_ERR(mru->base);
+
+ if (resource_size(res) < MRU_REGION_SIZE)
+ return dev_err_probe(dev, -EINVAL,
+ "MRU%u register space too small\n", i);
+
+ mru->addr = res->start;
+
+ mru->irq = platform_get_irq(pdev, i);
+ if (mru->irq < 0)
+ return mru->irq;
+
+ /* Reset the channels, in case they were left enabled. */
+ for (chan = 0; chan < MRU_CHANS_PER_MRU; chan++)
+ writel(MRU_CFG0_CHR, mru->base + chan * MRU_CH_STRIDE +
+ MRU_CH_CFG0);
+ }
+
+ ret = mru_msi_create_domain(priv);
+ if (ret)
+ return ret;
+
+ /* The handlers use the domain, so they are installed last. */
+ for (i = 0; i < priv->nmrus; i++) {
+ struct mru *mru = &priv->mrus[i];
+ unsigned int cpu;
+
+ irq_set_chained_handler_and_data(mru->irq, mru_msi_irq_handler,
+ mru);
+
+ /*
+ * The vectors choose their own CPU through the redirection, so
+ * the SPIs are only spread to keep the demux work of the
+ * instances on different CPUs. A failure is not fatal.
+ */
+ cpu = cpumask_local_spread(i, NUMA_NO_NODE);
+ ret = irq_set_affinity(mru->irq, cpumask_of(cpu));
+ if (ret)
+ dev_warn(dev, "MRU%u: cannot set the SPI affinity: %d\n",
+ i, ret);
+ }
+
+ return 0;
+}
+
+static const struct of_device_id mru_msi_of_match[] = {
+ { .compatible = "nxp,s32n79-mru-msi" },
+ { }
+};
+
+static struct platform_driver mru_msi_driver = {
+ .probe = mru_msi_probe,
+ .driver = {
+ .name = "nxp-mru-msi",
+ .of_match_table = mru_msi_of_match,
+ /* The MSI parent domain cannot be removed once it is in use. */
+ .suppress_bind_attrs = true,
+ },
+};
+builtin_platform_driver(mru_msi_driver);
--
2.43.0