Skip to contentSkip to conversation

Move RISC-V IMSIC driver to the common MSI lib

PATCH Superseded Anup Patel 2 participants 12 messages Dec 8, 2024 – Dec 13, 2024

Subsystems (8)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Anup Patel

author
#0 lore raw

cover Move RISC-V IMSIC driver to the common MSI lib

This series is based on recent discussion on LKML:
https://lore.kernel.org/lkml/[email protected]/

It primarily focuses on moving RISC-V IMSIC driver to the common MSI lib.

PATCH1: Fix for handling non-atomic MSI updates PATCH2 & PATCH3: Preparatory patches
PATCH4: Main patch which updates IMSIC driver to use MSI lib

These patches can also be found in the riscv_imsic_imp_v1 branch at:
https://github.com/avpatel/linux.git

Andrew Jones (1):
irqchip/riscv-imsic: Set irq_set_affinity for IMSIC base

Anup Patel (1):
irqchip/riscv-imsic: Handle non-atomic MSI updates for device

Thomas Gleixner (2):
irqchip/irq-msi-lib: Optionally set default irq_eoi/irq_ack irqchip/riscv-imsic: Move to common MSI lib

 drivers/irqchip/Kconfig                    |   8 +-
 drivers/irqchip/irq-gic-v2m.c              |   1 +
 drivers/irqchip/irq-imx-mu-msi.c           |   1 +
 drivers/irqchip/irq-msi-lib.c              |  11 +-
 drivers/irqchip/irq-mvebu-gicp.c           |   1 +
 drivers/irqchip/irq-mvebu-odmi.c           |   1 +
 drivers/irqchip/irq-mvebu-sei.c            |   1 +
 drivers/irqchip/irq-riscv-imsic-platform.c | 155 ++++++---------------
 drivers/irqchip/irq-riscv-imsic-state.c    |  27 +++-
 include/linux/msi.h                        |  11 ++
 10 files changed, 87 insertions(+), 130 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 1/4 irqchip/riscv-imsic: Handle non-atomic MSI updates for device

Device having non-atomic MSI update might see an intermediate state when changing target IMSIC vector from one CPU to another.

To handle such intermediate device state, update MSI address and MSI data through separate MSI writes to the device.

Fixes 027e125acdba ("irqchip/riscv-imsic: Add device MSI domain support for platform devices")
---
 drivers/irqchip/irq-riscv-imsic-platform.c | 27 ++++++++++++++++++++++
 drivers/irqchip/irq-riscv-imsic-state.c    | 27 +++++++++++++++++++---
 2 files changed, 51 insertions(+), 3 deletions(-)
drivers/irqchip/irq-riscv-imsic-platform.c +27 -0
index c708780e8760..707c7ccb4d08 100644
@@ -97,6 +97,7 @@ static int imsic_irq_set_affinity(struct irq_data *d, const struct cpumask *mask
 {
 	struct imsic_vector *old_vec, *new_vec;
 	struct irq_data *pd = d->parent_data;
+	struct imsic_vector tmp_vec;
 
 	old_vec = irq_data_get_irq_chip_data(pd);
 	if (WARN_ON(!old_vec))
@@ -110,11 +111,37 @@ static int imsic_irq_set_affinity(struct irq_data *d, const struct cpumask *mask
 	if (imsic_vector_get_move(old_vec))
 		return -EBUSY;
 
+	/*
+	 * Device having non-atomic MSI update might see an intermediate
+	 * state when changing target IMSIC vector from one CPU to another.
+	 *
+	 * To avoid losing interrupt to some intermediate state, do the
+	 * following (just like x86 APIC):
+	 *
+	 * 1) First write a temporary IMSIC vector to the device which
+	 * has MSI address same as the old IMSIC vector but MSI data
+	 * matches the new IMSIC vector.
+	 *
+	 * 2) Next write the new IMSIC vector to the device.
+	 *
+	 * Based on the above, the __imsic_local_sync() must check both
+	 * old MSI data and new MSI data on the old CPU for pending
+	 */
+
 	/* Get a new vector on the desired set of CPUs */
 	new_vec = imsic_vector_alloc(old_vec->hwirq, mask_val);
 	if (!new_vec)
 		return -ENOSPC;
 
+	if (new_vec->local_id != old_vec->local_id) {
+		/* Setup temporary vector */
+		tmp_vec.cpu = old_vec->cpu;
+		tmp_vec.local_id = new_vec->local_id;
+
+		/* Point device to the temporary vector */
+		imsic_msi_update_msg(d, &tmp_vec);
+	}
+
 	/* Point device to the new vector */
 	imsic_msi_update_msg(d, new_vec);
 
drivers/irqchip/irq-riscv-imsic-state.c +24 -3
index b97e6cd89ed7..230b917136e6 100644
@@ -126,8 +126,8 @@ void __imsic_eix_update(unsigned long base_id, unsigned long num_id, bool pend,
 
 static void __imsic_local_sync(struct imsic_local_priv *lpriv)
 {
-	struct imsic_local_config *mlocal;
-	struct imsic_vector *vec, *mvec;
+	struct imsic_local_config *tlocal, *mlocal;
+	struct imsic_vector *vec, *tvec, *mvec;
 	int i;
 
 	lockdep_assert_held(&lpriv->lock);
@@ -151,7 +151,28 @@ static void __imsic_local_sync(struct imsic_local_priv *lpriv)
 		mvec = READ_ONCE(vec->move);
 		WRITE_ONCE(vec->move, NULL);
 		if (mvec && mvec != vec) {
-			if (__imsic_id_read_clear_pending(i)) {
+			/*
+			 * Device having non-atomic MSI update might see an
+			 * intermediate state so check both old ID and new ID
+			 * for pending interrupts.
+			 *
+			 * For details, refer imsic_irq_set_affinity().
+			 */
+
+			tvec = vec->local_id == mvec->local_id ?
+			       NULL : &lpriv->vectors[mvec->local_id];
+			if (tvec && __imsic_id_read_clear_pending(tvec->local_id)) {
+				/* Retrigger temporary vector if it was already in-use */
+				if (READ_ONCE(tvec->enable)) {
+					tlocal = per_cpu_ptr(imsic->global.local, tvec->cpu);
+					writel_relaxed(tvec->local_id, tlocal->msi_va);
+				}
+
+				mlocal = per_cpu_ptr(imsic->global.local, mvec->cpu);
+				writel_relaxed(mvec->local_id, mlocal->msi_va);
+			}
+
+			if (__imsic_id_read_clear_pending(vec->local_id)) {
 				mlocal = per_cpu_ptr(imsic->global.local, mvec->cpu);
 				writel_relaxed(mvec->local_id, mlocal->msi_va);
 			}
Signature · 1 line
2.43.0
author
#9 lore raw

PATCH 2/4 irqchip/irq-msi-lib: Optionally set default irq_eoi/irq_ack

In reply to Anup Patel

From: Thomas Gleixner <[email protected]>

Introduce chip_flags in struct msi_parent_ops. This allows msi_lib_init_dev_msi_info() set default irq_eoi/irq_ack callbacks only when the corresponding flags are set in the chip_flags.

---
 drivers/irqchip/irq-gic-v2m.c    |  1 +
 drivers/irqchip/irq-imx-mu-msi.c |  1 +
 drivers/irqchip/irq-msi-lib.c    | 11 ++++++-----
 drivers/irqchip/irq-mvebu-gicp.c |  1 +
 drivers/irqchip/irq-mvebu-odmi.c |  1 +
 drivers/irqchip/irq-mvebu-sei.c  |  1 +
 include/linux/msi.h              | 11 +++++++++++
 7 files changed, 22 insertions(+), 5 deletions(-)
drivers/irqchip/irq-gic-v2m.c +1 -0
index be35c5349986..1e3476c335ca 100644
@@ -255,6 +255,7 @@ static void __init gicv2m_teardown(void)
 static struct msi_parent_ops gicv2m_msi_parent_ops = {
 	.supported_flags	= GICV2M_MSI_FLAGS_SUPPORTED,
 	.required_flags		= GICV2M_MSI_FLAGS_REQUIRED,
+	.chip_flags		= MSI_CHIP_FLAG_SET_EOI | MSI_CHIP_FLAG_SET_ACK,
 	.bus_select_token	= DOMAIN_BUS_NEXUS,
 	.bus_select_mask	= MATCH_PCI_MSI | MATCH_PLATFORM_MSI,
 	.prefix			= "GICv2m-",
drivers/irqchip/irq-imx-mu-msi.c +1 -0
index 4342a21de1eb..69aacdfc8bef 100644
@@ -214,6 +214,7 @@ static void imx_mu_msi_irq_handler(struct irq_desc *desc)
 static const struct msi_parent_ops imx_mu_msi_parent_ops = {
 	.supported_flags	= IMX_MU_MSI_FLAGS_SUPPORTED,
 	.required_flags		= IMX_MU_MSI_FLAGS_REQUIRED,
+	.chip_flags		= MSI_CHIP_FLAG_SET_EOI | MSI_CHIP_FLAG_SET_ACK,
 	.bus_select_token       = DOMAIN_BUS_NEXUS,
 	.bus_select_mask	= MATCH_PLATFORM_MSI,
 	.prefix			= "MU-MSI-",
drivers/irqchip/irq-msi-lib.c +6 -5
index d8e29fc0d406..51464c6257f3 100644
@@ -28,6 +28,7 @@ bool msi_lib_init_dev_msi_info(struct device *dev, struct irq_domain *domain,
 			       struct msi_domain_info *info)
 {
 	const struct msi_parent_ops *pops = real_parent->msi_parent_ops;
+	struct irq_chip *chip = info->chip;
 	u32 required_flags;
 
 	/* Parent ops available? */
@@ -92,10 +93,10 @@ bool msi_lib_init_dev_msi_info(struct device *dev, struct irq_domain *domain,
 	info->flags			|= required_flags;
 
 	/* Chip updates for all child bus types */
-	if (!info->chip->irq_eoi)
-		info->chip->irq_eoi	= irq_chip_eoi_parent;
-	if (!info->chip->irq_ack)
-		info->chip->irq_ack	= irq_chip_ack_parent;
+	if (!chip->irq_eoi && (pops->chip_flags & MSI_CHIP_FLAG_SET_EOI))
+		chip->irq_eoi = irq_chip_eoi_parent;
+	if (!chip->irq_ack && (pops->chip_flags & MSI_CHIP_FLAG_SET_ACK))
+		chip->irq_ack = irq_chip_ack_parent;
 
 	/*
 	 * The device MSI domain can never have a set affinity callback. It
@@ -105,7 +106,7 @@ bool msi_lib_init_dev_msi_info(struct device *dev, struct irq_domain *domain,
 	 * device MSI domain aside of mask/unmask which is provided e.g. by
 	 * PCI/MSI device domains.
 	 */
-	info->chip->irq_set_affinity	= msi_domain_set_affinity;
+	chip->irq_set_affinity = msi_domain_set_affinity;
 	return true;
 }
 EXPORT_SYMBOL_GPL(msi_lib_init_dev_msi_info);
drivers/irqchip/irq-mvebu-gicp.c +1 -0
index 2b6183919ea4..d67f93f6d750 100644
@@ -161,6 +161,7 @@ static const struct irq_domain_ops gicp_domain_ops = {
 static const struct msi_parent_ops gicp_msi_parent_ops = {
 	.supported_flags	= GICP_MSI_FLAGS_SUPPORTED,
 	.required_flags		= GICP_MSI_FLAGS_REQUIRED,
+	.chip_flags		= MSI_CHIP_FLAG_SET_EOI | MSI_CHIP_FLAG_SET_ACK,
 	.bus_select_token       = DOMAIN_BUS_GENERIC_MSI,
 	.bus_select_mask	= MATCH_PLATFORM_MSI,
 	.prefix			= "GICP-",
drivers/irqchip/irq-mvebu-odmi.c +1 -0
index ff19bfd258dc..28f7e81df94f 100644
@@ -157,6 +157,7 @@ static const struct irq_domain_ops odmi_domain_ops = {
 static const struct msi_parent_ops odmi_msi_parent_ops = {
 	.supported_flags	= ODMI_MSI_FLAGS_SUPPORTED,
 	.required_flags		= ODMI_MSI_FLAGS_REQUIRED,
+	.chip_flags		= MSI_CHIP_FLAG_SET_EOI | MSI_CHIP_FLAG_SET_ACK,
 	.bus_select_token	= DOMAIN_BUS_GENERIC_MSI,
 	.bus_select_mask	= MATCH_PLATFORM_MSI,
 	.prefix			= "ODMI-",
drivers/irqchip/irq-mvebu-sei.c +1 -0
index 065166ab5dbc..ebd4a9014e8d 100644
@@ -356,6 +356,7 @@ static void mvebu_sei_reset(struct mvebu_sei *sei)
 static const struct msi_parent_ops sei_msi_parent_ops = {
 	.supported_flags	= SEI_MSI_FLAGS_SUPPORTED,
 	.required_flags		= SEI_MSI_FLAGS_REQUIRED,
+	.chip_flags		= MSI_CHIP_FLAG_SET_EOI | MSI_CHIP_FLAG_SET_ACK,
 	.bus_select_mask	= MATCH_PLATFORM_MSI,
 	.bus_select_token	= DOMAIN_BUS_GENERIC_MSI,
 	.prefix			= "SEI-",
include/linux/msi.h +11 -0
index b10093c4d00e..9abef442c146 100644
@@ -558,11 +558,21 @@ enum {
 	MSI_FLAG_NO_AFFINITY		= (1 << 21),
 };
 
+/*
+ * Flags for msi_parent_ops::chip_flags
+ */
+enum {
+	MSI_CHIP_FLAG_SET_EOI		= (1 << 0),
+	MSI_CHIP_FLAG_SET_ACK		= (1 << 1),
+};
+
 /**
  * struct msi_parent_ops - MSI parent domain callbacks and configuration info
  *
  * @supported_flags:	Required: The supported MSI flags of the parent domain
  * @required_flags:	Optional: The required MSI flags of the parent MSI domain
+ * @chip_flags:		Optional: Select MSI chip callbacks to update with defaults
+ *			in msi_lib_init_dev_msi_info().
  * @bus_select_token:	Optional: The bus token of the real parent domain for
  *			irq_domain::select()
  * @bus_select_mask:	Optional: A mask of supported BUS_DOMAINs for
@@ -575,6 +585,7 @@ enum {
 struct msi_parent_ops {
 	u32		supported_flags;
 	u32		required_flags;
+	u32		chip_flags;
 	u32		bus_select_token;
 	u32		bus_select_mask;
 	const char	*prefix;
Signature · 1 line
2.43.0
author
#10 lore raw

PATCH 3/4 irqchip/riscv-imsic: Set irq_set_affinity for IMSIC base

In reply to Anup Patel

From: Andrew Jones <[email protected]>

Instead of using imsic_irq_set_affinity() for non-leaf MSI domains, use imsic_irq_set_affinity() for the leaf IMSIC base domain and use irq_chip_set_affinity_parent() for non-leaf MSI domains.

---
 drivers/irqchip/irq-riscv-imsic-platform.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
drivers/irqchip/irq-riscv-imsic-platform.c +10 -8
index 707c7ccb4d08..33a8261e6017 100644
@@ -96,10 +96,9 @@ static int imsic_irq_set_affinity(struct irq_data *d, const struct cpumask *mask
 				  bool force)
 {
 	struct imsic_vector *old_vec, *new_vec;
-	struct irq_data *pd = d->parent_data;
 	struct imsic_vector tmp_vec;
 
-	old_vec = irq_data_get_irq_chip_data(pd);
+	old_vec = irq_data_get_irq_chip_data(d);
 	if (WARN_ON(!old_vec))
 		return -ENOENT;
 
@@ -139,17 +138,17 @@ static int imsic_irq_set_affinity(struct irq_data *d, const struct cpumask *mask
 		tmp_vec.local_id = new_vec->local_id;
 
 		/* Point device to the temporary vector */
-		imsic_msi_update_msg(d, &tmp_vec);
+		imsic_msi_update_msg(irq_get_irq_data(d->irq), &tmp_vec);
 	}
 
 	/* Point device to the new vector */
-	imsic_msi_update_msg(d, new_vec);
+	imsic_msi_update_msg(irq_get_irq_data(d->irq), new_vec);
 
 	/* Update irq descriptors with the new vector */
-	pd->chip_data = new_vec;
+	d->chip_data = new_vec;
 
-	/* Update effective affinity of parent irq data */
-	irq_data_update_effective_affinity(pd, cpumask_of(new_vec->cpu));
+	/* Update effective affinity */
+	irq_data_update_effective_affinity(d, cpumask_of(new_vec->cpu));
 
 	/* Move state of the old vector to the new vector */
 	imsic_vector_move(old_vec, new_vec);
@@ -162,6 +161,9 @@ static struct irq_chip imsic_irq_base_chip = {
 	.name			= "IMSIC",
 	.irq_mask		= imsic_irq_mask,
 	.irq_unmask		= imsic_irq_unmask,
+#ifdef CONFIG_SMP
+	.irq_set_affinity	= imsic_irq_set_affinity,
+#endif
 	.irq_retrigger		= imsic_irq_retrigger,
 	.irq_compose_msi_msg	= imsic_irq_compose_msg,
 	.flags			= IRQCHIP_SKIP_SET_WAKE |
@@ -272,7 +274,7 @@ static bool imsic_init_dev_msi_info(struct device *dev,
 		if (WARN_ON_ONCE(domain != real_parent))
 			return false;
 #ifdef CONFIG_SMP
-		info->chip->irq_set_affinity = imsic_irq_set_affinity;
+		info->chip->irq_set_affinity = irq_chip_set_affinity_parent;
 #endif
 		break;
 	default:
Signature · 1 line
2.43.0
author
#11 lore raw

PATCH 4/4 irqchip/riscv-imsic: Move to common MSI lib

In reply to Anup Patel

From: Thomas Gleixner <[email protected]>

Simplify the non-leaf MSI domain handling in the RISC-V IMSIC driver by using msi_lib_init_dev_msi_info() and msi_lib_irq_domain_select() provided by common MSI lib.

---
 drivers/irqchip/Kconfig                    |   8 +-
 drivers/irqchip/irq-riscv-imsic-platform.c | 114 +--------------------
 2 files changed, 6 insertions(+), 116 deletions(-)
drivers/irqchip/Kconfig +1 -7
index 55d7122121e2..6b767b8c974f 100644
@@ -587,13 +587,7 @@ config RISCV_IMSIC
 	select IRQ_DOMAIN_HIERARCHY
 	select GENERIC_IRQ_MATRIX_ALLOCATOR
 	select GENERIC_MSI_IRQ
-
-config RISCV_IMSIC_PCI
-	bool
-	depends on RISCV_IMSIC
-	depends on PCI
-	depends on PCI_MSI
-	default RISCV_IMSIC
+	select IRQ_MSI_LIB
 
 config SIFIVE_PLIC
 	bool
drivers/irqchip/irq-riscv-imsic-platform.c +5 -109

143 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH 1/4] irqchip/riscv-imsic: Handle non-atomic MSI updates for device

In reply to Anup Patel

drivers/irqchip/irq-riscv-imsic-state.c static void __imsic_local_sync(struct imsic_local_priv *lpriv) · line 164

Quoted 4 lines · Anup Patel
drivers/irqchip/irq-riscv-imsic-state.c +4 -0
+
+			tvec = vec->local_id == mvec->local_id ?
+			       NULL : &lpriv->vectors[mvec->local_id];
+			if (tvec && __imsic_id_read_clear_pending(tvec->local_id)) {

As I told you before:

I don't see a way how that can work remote with the IMSIC either even if you can easily access the pending state of the remote CPU:

CPU0 CPU1 Device
set_affinity()
write_msg(tmp)

    write(addr); // CPU1
    write(data); // vector 0x20
							raise IRQ (CPU1, vector 0x20)
				handle vector 0x20
				(other device)

    check_pending(CPU1, 0x20) == false -> Interrupt is lost

There is no guarantee that set_affinity() runs on the original target CPU (CPU 1). Your scheme only works, when CPU1 vector 0x20 is not used by some other device. If it's used, you lost as CPU1 will consume the vector and your pending check is not seeing anything.

x86 ensures CPU locality by deferring the affinity move to the next device interrupt on the original target CPU (CPU1 in the above example). See CONFIG_GENERIC_IRQ_PENDING.

The interrupt domains which are not affected (remap) set the IRQ_MOVE_PCNTXT flag to avoid that dance and don't use that affinity setter code path at all.

Thanks,

tglx

author
#3 lore raw

Re: [PATCH 1/4] irqchip/riscv-imsic: Handle non-atomic MSI updates for device

Hi Thomas,

Quoted 11 lines · Thomas Gleixner

drivers/irqchip/irq-riscv-imsic-state.c static void __imsic_local_sync(struct imsic_local_priv *lpriv) · line 164

Quoted 4 lines · Anup Patel
drivers/irqchip/irq-riscv-imsic-state.c +4 -0
+
+                     tvec = vec->local_id == mvec->local_id ?
+                            NULL : &lpriv->vectors[mvec->local_id];
+                     if (tvec && __imsic_id_read_clear_pending(tvec->local_id)) {

As I told you before:

I don't see a way how that can work remote with the IMSIC either even if you can easily access the pending state of the remote CPU:

For RISC-V IMSIC, a remote CPU cannot access the pending state of interrupts on some other CPU.

Quoted 12 lines

CPU0 CPU1 Device
set_affinity()
write_msg(tmp)

    write(addr); // CPU1
    write(data); // vector 0x20
                                                        raise IRQ (CPU1, vector 0x20)
                                handle vector 0x20
                                (other device)

    check_pending(CPU1, 0x20) == false -> Interrupt is lost
Quoted 8 lines

There is no guarantee that set_affinity() runs on the original target CPU (CPU 1). Your scheme only works, when CPU1 vector 0x20 is not used by some other device. If it's used, you lost as CPU1 will consume the vector and your pending check is not seeing anything.

x86 ensures CPU locality by deferring the affinity move to the next device interrupt on the original target CPU (CPU1 in the above example). See CONFIG_GENERIC_IRQ_PENDING.

I agree with you.

The IMSIC driver must do the affinity move upon the next device interrupt on the old CPU. I will update this patch in the next revision.

BTW, I did not find CONFIG_GENERIC_IRQ_PENDING. Is the name correct ?

Quoted 4 lines

The interrupt domains which are not affected (remap) set the IRQ_MOVE_PCNTXT flag to avoid that dance and don't use that affinity setter code path at all.

Yes, setting the IRQ_MOVE_PCNTXT flag in the remap domain makes perfect sense.

I suggest adding IRQ_MOVE_PCNTXT usage as part of Drew's irqbypass series which adds a remap domain in the IOMMU driver. Unless you insist on having it as part of this series ?

Regards,
Anup

#4 lore raw

Re: [PATCH 1/4] irqchip/riscv-imsic: Handle non-atomic MSI updates for device

Anup!

Quoted 10 lines · Anup Patel
Quoted 8 lines · Thomas Gleixner

There is no guarantee that set_affinity() runs on the original target CPU (CPU 1). Your scheme only works, when CPU1 vector 0x20 is not used by some other device. If it's used, you lost as CPU1 will consume the vector and your pending check is not seeing anything.

x86 ensures CPU locality by deferring the affinity move to the next device interrupt on the original target CPU (CPU1 in the above example). See CONFIG_GENERIC_IRQ_PENDING.

Quoted 7 lines

I agree with you.

The IMSIC driver must do the affinity move upon the next device interrupt on the old CPU. I will update this patch in the next revision.

BTW, I did not find CONFIG_GENERIC_IRQ_PENDING. Is the name correct ?

CONFIG_GENERIC_PENDING_IRQ is close enough :)

Quoted 10 lines
Quoted 3 lines

The interrupt domains which are not affected (remap) set the IRQ_MOVE_PCNTXT flag to avoid that dance and don't use that affinity setter code path at all.

Yes, setting the IRQ_MOVE_PCNTXT flag in the remap domain makes perfect sense.

I suggest adding IRQ_MOVE_PCNTXT usage as part of Drew's irqbypass series which adds a remap domain in the IOMMU driver. Unless you insist on having it as part of this series ?

You need to look at the other RISC-V controllers. Those which do not need this should set it. That's historically backwards.

I think we can reverse the logic here. As this needs backporting, I can't make a full cleanup of this, but for your problem the patch below should just work.

Select GENERIC_PENDING_IRQ and GENERIC_PENDING_IRQ_CHIPFLAGS and set the IRQCHIP_MOVE_DEFERRED flag on your interrrupt chip and the core logic takes care of the PCNTXT bits.

I'll convert x86 in a seperate step and remove the PCNTXT leftovers and the new config knob once the dust has settled.

Thanks,

tglx
---

include/linux/irq.h +2 -0
@@ -567,6 +567,7 @@ struct irq_chip {
  *                                    in the suspend path if they are in disabled state
  * IRQCHIP_AFFINITY_PRE_STARTUP:      Default affinity update before startup
  * IRQCHIP_IMMUTABLE:		      Don't ever change anything in this chip
+ * IRQCHIP_MOVE_DEFERRED:	      Move the interrupt in actual interrupt context
  */
 enum {
 	IRQCHIP_SET_TYPE_MASKED			= (1 <<  0),
@@ -581,6 +582,7 @@ enum {
 	IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND	= (1 <<  9),
 	IRQCHIP_AFFINITY_PRE_STARTUP		= (1 << 10),
 	IRQCHIP_IMMUTABLE			= (1 << 11),
+	IRQCHIP_MOVE_DEFERRED			= (1 << 12),
 };
 
 #include <linux/irqdesc.h>
kernel/irq/Kconfig +4 -0
@@ -31,6 +31,10 @@ config GENERIC_IRQ_EFFECTIVE_AFF_MASK
 config GENERIC_PENDING_IRQ
 	bool
 
+# Deduce delayed migration from top-level interrupt chip flags
+config GENERIC_PENDING_IRQ_CHIPFLAGS
+	bool
+
 # Support for generic irq migrating off cpu before the cpu is offline.
 config GENERIC_IRQ_MIGRATION
 	bool
kernel/irq/chip.c +15 -3
@@ -47,6 +47,13 @@ int irq_set_chip(unsigned int irq, const
 		return -EINVAL;
 
 	desc->irq_data.chip = (struct irq_chip *)(chip ?: &no_irq_chip);
+
+	if (IS_ENABLED(CONFIG_GENERIC_PENDING_IRQ_CHIPFLAGS) && chip) {
+		if (chip->flags & IRQCHIP_MOVE_DEFERRED)
+			irqd_clear(&desc->irq_data, IRQD_MOVE_PCNTXT);
+		else
+			irqd_set(&desc->irq_data, IRQD_MOVE_PCNTXT);
+	}
 	irq_put_desc_unlock(desc, flags);
 	/*
 	 * For !CONFIG_SPARSE_IRQ make the irq show up in
@@ -1114,16 +1121,21 @@ void irq_modify_status(unsigned int irq,
 	trigger = irqd_get_trigger_type(&desc->irq_data);
 
 	irqd_clear(&desc->irq_data, IRQD_NO_BALANCING | IRQD_PER_CPU |
-		   IRQD_TRIGGER_MASK | IRQD_LEVEL | IRQD_MOVE_PCNTXT);
+		   IRQD_TRIGGER_MASK | IRQD_LEVEL);
 	if (irq_settings_has_no_balance_set(desc))
 		irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
 	if (irq_settings_is_per_cpu(desc))
 		irqd_set(&desc->irq_data, IRQD_PER_CPU);
-	if (irq_settings_can_move_pcntxt(desc))
-		irqd_set(&desc->irq_data, IRQD_MOVE_PCNTXT);
 	if (irq_settings_is_level(desc))
 		irqd_set(&desc->irq_data, IRQD_LEVEL);
 
+	/* Keep this around until x86 is converted over */
+	if (!IS_ENABLED(CONFIG_GENERIC_PENDING_IRQ_CHIPFLAGS)) {
+		irqd_clear(&desc->irq_data, IRQD_MOVE_PCNTXT);
+		if (irq_settings_can_move_pcntxt(desc))
+			irqd_set(&desc->irq_data, IRQD_MOVE_PCNTXT);
+	}
+
 	tmp = irq_settings_get_trigger_mask(desc);
 	if (tmp != IRQ_TYPE_NONE)
 		trigger = tmp;
author
#5 lore raw

Re: [PATCH 1/4] irqchip/riscv-imsic: Handle non-atomic MSI updates for device

Quoted 31 lines · Thomas Gleixner

Anup!

Quoted 10 lines · Anup Patel
Quoted 8 lines · Thomas Gleixner

There is no guarantee that set_affinity() runs on the original target CPU (CPU 1). Your scheme only works, when CPU1 vector 0x20 is not used by some other device. If it's used, you lost as CPU1 will consume the vector and your pending check is not seeing anything.

x86 ensures CPU locality by deferring the affinity move to the next device interrupt on the original target CPU (CPU1 in the above example). See CONFIG_GENERIC_IRQ_PENDING.

Quoted 7 lines

I agree with you.

The IMSIC driver must do the affinity move upon the next device interrupt on the old CPU. I will update this patch in the next revision.

BTW, I did not find CONFIG_GENERIC_IRQ_PENDING. Is the name correct ?

CONFIG_GENERIC_PENDING_IRQ is close enough :)

Quoted 7 lines
Quoted 3 lines

The interrupt domains which are not affected (remap) set the IRQ_MOVE_PCNTXT flag to avoid that dance and don't use that affinity setter code path at all.

Yes, setting the IRQ_MOVE_PCNTXT flag in the remap domain makes perfect sense.

Quoted 6 lines
Quoted 3 lines

I suggest adding IRQ_MOVE_PCNTXT usage as part of Drew's irqbypass series which adds a remap domain in the IOMMU driver. Unless you insist on having it as part of this series ?

You need to look at the other RISC-V controllers. Those which do not need this should set it. That's historically backwards.

I will update the RISC-V APLIC MSI-mode driver in the next revision. This driver is a good candidate to use IRQ_MOVE_PCNTXT and IRQCHIP_MOVE_DEFERRED.

Quoted 8 lines

I think we can reverse the logic here. As this needs backporting, I can't make a full cleanup of this, but for your problem the patch below should just work.

Select GENERIC_PENDING_IRQ and GENERIC_PENDING_IRQ_CHIPFLAGS and set the IRQCHIP_MOVE_DEFERRED flag on your interrrupt chip and the core logic takes care of the PCNTXT bits.

Sure, I will update.

Thanks,
Anup

Quoted 83 lines

I'll convert x86 in a seperate step and remove the PCNTXT leftovers and the new config knob once the dust has settled.

Thanks,

tglx
---

include/linux/irq.h +2 -0
@@ -567,6 +567,7 @@ struct irq_chip {
  *                                    in the suspend path if they are in disabled state
  * IRQCHIP_AFFINITY_PRE_STARTUP:      Default affinity update before startup
  * IRQCHIP_IMMUTABLE:                Don't ever change anything in this chip
+ * IRQCHIP_MOVE_DEFERRED:            Move the interrupt in actual interrupt context
  */
 enum {
        IRQCHIP_SET_TYPE_MASKED                 = (1 <<  0),
@@ -581,6 +582,7 @@ enum {
        IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND        = (1 <<  9),
        IRQCHIP_AFFINITY_PRE_STARTUP            = (1 << 10),
        IRQCHIP_IMMUTABLE                       = (1 << 11),
+       IRQCHIP_MOVE_DEFERRED                   = (1 << 12),
 };
 
 #include <linux/irqdesc.h>
kernel/irq/Kconfig +4 -0
@@ -31,6 +31,10 @@ config GENERIC_IRQ_EFFECTIVE_AFF_MASK
 config GENERIC_PENDING_IRQ
        bool
 
+# Deduce delayed migration from top-level interrupt chip flags
+config GENERIC_PENDING_IRQ_CHIPFLAGS
+       bool
+
 # Support for generic irq migrating off cpu before the cpu is offline.
 config GENERIC_IRQ_MIGRATION
        bool
kernel/irq/chip.c +15 -3
@@ -47,6 +47,13 @@ int irq_set_chip(unsigned int irq, const
                return -EINVAL;
 
        desc->irq_data.chip = (struct irq_chip *)(chip ?: &no_irq_chip);
+
+       if (IS_ENABLED(CONFIG_GENERIC_PENDING_IRQ_CHIPFLAGS) && chip) {
+               if (chip->flags & IRQCHIP_MOVE_DEFERRED)
+                       irqd_clear(&desc->irq_data, IRQD_MOVE_PCNTXT);
+               else
+                       irqd_set(&desc->irq_data, IRQD_MOVE_PCNTXT);
+       }
        irq_put_desc_unlock(desc, flags);
        /*
         * For !CONFIG_SPARSE_IRQ make the irq show up in
@@ -1114,16 +1121,21 @@ void irq_modify_status(unsigned int irq,
        trigger = irqd_get_trigger_type(&desc->irq_data);
 
        irqd_clear(&desc->irq_data, IRQD_NO_BALANCING | IRQD_PER_CPU |
-                  IRQD_TRIGGER_MASK | IRQD_LEVEL | IRQD_MOVE_PCNTXT);
+                  IRQD_TRIGGER_MASK | IRQD_LEVEL);
        if (irq_settings_has_no_balance_set(desc))
                irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
        if (irq_settings_is_per_cpu(desc))
                irqd_set(&desc->irq_data, IRQD_PER_CPU);
-       if (irq_settings_can_move_pcntxt(desc))
-               irqd_set(&desc->irq_data, IRQD_MOVE_PCNTXT);
        if (irq_settings_is_level(desc))
                irqd_set(&desc->irq_data, IRQD_LEVEL);
 
+       /* Keep this around until x86 is converted over */
+       if (!IS_ENABLED(CONFIG_GENERIC_PENDING_IRQ_CHIPFLAGS)) {
+               irqd_clear(&desc->irq_data, IRQD_MOVE_PCNTXT);
+               if (irq_settings_can_move_pcntxt(desc))
+                       irqd_set(&desc->irq_data, IRQD_MOVE_PCNTXT);
+       }
+
        tmp = irq_settings_get_trigger_mask(desc);
        if (tmp != IRQ_TYPE_NONE)
                trigger = tmp;
author
#6 lore raw

Re: [PATCH 1/4] irqchip/riscv-imsic: Handle non-atomic MSI updates for device

In reply to Thomas Gleixner

Hi Thomas,

Quoted 87 lines · Thomas Gleixner

Anup!

Quoted 10 lines · Anup Patel
Quoted 8 lines · Thomas Gleixner

There is no guarantee that set_affinity() runs on the original target CPU (CPU 1). Your scheme only works, when CPU1 vector 0x20 is not used by some other device. If it's used, you lost as CPU1 will consume the vector and your pending check is not seeing anything.

x86 ensures CPU locality by deferring the affinity move to the next device interrupt on the original target CPU (CPU1 in the above example). See CONFIG_GENERIC_IRQ_PENDING.

Quoted 7 lines

I agree with you.

The IMSIC driver must do the affinity move upon the next device interrupt on the old CPU. I will update this patch in the next revision.

BTW, I did not find CONFIG_GENERIC_IRQ_PENDING. Is the name correct ?

CONFIG_GENERIC_PENDING_IRQ is close enough :)

Quoted 10 lines
Quoted 3 lines

The interrupt domains which are not affected (remap) set the IRQ_MOVE_PCNTXT flag to avoid that dance and don't use that affinity setter code path at all.

Yes, setting the IRQ_MOVE_PCNTXT flag in the remap domain makes perfect sense.

I suggest adding IRQ_MOVE_PCNTXT usage as part of Drew's irqbypass series which adds a remap domain in the IOMMU driver. Unless you insist on having it as part of this series ?

You need to look at the other RISC-V controllers. Those which do not need this should set it. That's historically backwards.

I think we can reverse the logic here. As this needs backporting, I can't make a full cleanup of this, but for your problem the patch below should just work.

Select GENERIC_PENDING_IRQ and GENERIC_PENDING_IRQ_CHIPFLAGS and set the IRQCHIP_MOVE_DEFERRED flag on your interrrupt chip and the core logic takes care of the PCNTXT bits.

I'll convert x86 in a seperate step and remove the PCNTXT leftovers and the new config knob once the dust has settled.

Thanks,

tglx
---

include/linux/irq.h +2 -0
@@ -567,6 +567,7 @@ struct irq_chip {
  *                                    in the suspend path if they are in disabled state
  * IRQCHIP_AFFINITY_PRE_STARTUP:      Default affinity update before startup
  * IRQCHIP_IMMUTABLE:                Don't ever change anything in this chip
+ * IRQCHIP_MOVE_DEFERRED:            Move the interrupt in actual interrupt context
  */
 enum {
        IRQCHIP_SET_TYPE_MASKED                 = (1 <<  0),
@@ -581,6 +582,7 @@ enum {
        IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND        = (1 <<  9),
        IRQCHIP_AFFINITY_PRE_STARTUP            = (1 << 10),
        IRQCHIP_IMMUTABLE                       = (1 << 11),
+       IRQCHIP_MOVE_DEFERRED                   = (1 << 12),
 };
 
 #include <linux/irqdesc.h>
kernel/irq/Kconfig +4 -0
@@ -31,6 +31,10 @@ config GENERIC_IRQ_EFFECTIVE_AFF_MASK
 config GENERIC_PENDING_IRQ
        bool
 
+# Deduce delayed migration from top-level interrupt chip flags
+config GENERIC_PENDING_IRQ_CHIPFLAGS
+       bool
+
 # Support for generic irq migrating off cpu before the cpu is offline.
 config GENERIC_IRQ_MIGRATION
        bool

kernel/irq/chip.c int irq_set_chip(unsigned int irq, const · line 56

Quoted 10 lines
kernel/irq/chip.c +7 -0
@@ -47,6 +47,13 @@ int irq_set_chip(unsigned int irq, const
                return -EINVAL;
 
        desc->irq_data.chip = (struct irq_chip *)(chip ?: &no_irq_chip);
+
+       if (IS_ENABLED(CONFIG_GENERIC_PENDING_IRQ_CHIPFLAGS) && chip) {
+               if (chip->flags & IRQCHIP_MOVE_DEFERRED)
+                       irqd_clear(&desc->irq_data, IRQD_MOVE_PCNTXT);
+               else
+                       irqd_set(&desc->irq_data, IRQD_MOVE_PCNTXT);
+       }

We need similar changes in irq_domain_set_hwirq_and_chip() because we use IRQ_DOMAIN_HIERARCHY in RISC-V.

kernel/irq/irqdomain.c +0 -0
@@ -1509,6 +1509,13 @@ int irq_domain_set_hwirq_and_chip(struct

irq_domain *domain, unsigned int virq,

        irq_data->chip = (struct irq_chip *)(chip ? chip : &no_irq_chip);
        irq_data->chip_data = chip_data;

+ if (IS_ENABLED(CONFIG_GENERIC_PENDING_IRQ_CHIPFLAGS) && chip) {
+ if (chip->flags & IRQCHIP_MOVE_DEFERRED)
+ irq_clear_status_flags(virq, IRQ_MOVE_PCNTXT);
+ else
+ irq_set_status_flags(virq, IRQ_MOVE_PCNTXT);
+ }
+

        return 0;

} EXPORT_SYMBOL_GPL(irq_domain_set_hwirq_and_chip);

Quoted 15 lines
kernel/irq/chip.c +1 -3
        irq_put_desc_unlock(desc, flags);
        /*
         * For !CONFIG_SPARSE_IRQ make the irq show up in
@@ -1114,16 +1121,21 @@ void irq_modify_status(unsigned int irq,
        trigger = irqd_get_trigger_type(&desc->irq_data);
 
        irqd_clear(&desc->irq_data, IRQD_NO_BALANCING | IRQD_PER_CPU |
-                  IRQD_TRIGGER_MASK | IRQD_LEVEL | IRQD_MOVE_PCNTXT);
+                  IRQD_TRIGGER_MASK | IRQD_LEVEL);
        if (irq_settings_has_no_balance_set(desc))
                irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
        if (irq_settings_is_per_cpu(desc))
                irqd_set(&desc->irq_data, IRQD_PER_CPU);
-       if (irq_settings_can_move_pcntxt(desc))
-               irqd_set(&desc->irq_data, IRQD_MOVE_PCNTXT);

kernel/irq/chip.c void irq_modify_status(unsigned int irq, · line 1138

Quoted 10 lines
kernel/irq/chip.c +7 -0
        if (irq_settings_is_level(desc))
                irqd_set(&desc->irq_data, IRQD_LEVEL);
 
+       /* Keep this around until x86 is converted over */
+       if (!IS_ENABLED(CONFIG_GENERIC_PENDING_IRQ_CHIPFLAGS)) {
+               irqd_clear(&desc->irq_data, IRQD_MOVE_PCNTXT);
+               if (irq_settings_can_move_pcntxt(desc))
+                       irqd_set(&desc->irq_data, IRQD_MOVE_PCNTXT);
+       }
+

These changes in irq_modify_status() need to be dropped to support the above changes in irq_domain_set_hwirq_and_chip().

kernel/irq/chip.c void irq_modify_status(unsigned int irq, · line 1141

Quoted 6 lines
kernel/irq/chip.c +0 -0
        tmp = irq_settings_get_trigger_mask(desc);
        if (tmp != IRQ_TYPE_NONE)
                trigger = tmp;

Let me know if you have alternate suggestions instead of the above changes.

Regards,
Anup

#7 lore raw

Re: [PATCH 1/4] irqchip/riscv-imsic: Handle non-atomic MSI updates for device

Quoted 5 lines · Anup Patel
Quoted 5 lines
kernel/irq/chip.c +0 -0
@@ -47,6 +47,13 @@ int irq_set_chip(unsigned int irq, const
                return -EINVAL;
 
Quoted 11 lines

kernel/irq/chip.c int irq_set_chip(unsigned int irq, const · line 56

Quoted 8 lines
kernel/irq/chip.c +7 -0
        desc->irq_data.chip = (struct irq_chip *)(chip ?: &no_irq_chip);
+
+       if (IS_ENABLED(CONFIG_GENERIC_PENDING_IRQ_CHIPFLAGS) && chip) {
+               if (chip->flags & IRQCHIP_MOVE_DEFERRED)
+                       irqd_clear(&desc->irq_data, IRQD_MOVE_PCNTXT);
+               else
+                       irqd_set(&desc->irq_data, IRQD_MOVE_PCNTXT);
+       }

We need similar changes in irq_domain_set_hwirq_and_chip() because we use IRQ_DOMAIN_HIERARCHY in RISC-V.

Grr, you are right. Let me add that to the base patch.

Quoted 18 lines
Quoted 18 lines
kernel/irq/chip.c +1 -3
        irq_put_desc_unlock(desc, flags);
        /*
         * For !CONFIG_SPARSE_IRQ make the irq show up in
@@ -1114,16 +1121,21 @@ void irq_modify_status(unsigned int irq,
        trigger = irqd_get_trigger_type(&desc->irq_data);
 
        irqd_clear(&desc->irq_data, IRQD_NO_BALANCING | IRQD_PER_CPU |
-                  IRQD_TRIGGER_MASK | IRQD_LEVEL | IRQD_MOVE_PCNTXT);
+                  IRQD_TRIGGER_MASK | IRQD_LEVEL);
        if (irq_settings_has_no_balance_set(desc))
                irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
        if (irq_settings_is_per_cpu(desc))
                irqd_set(&desc->irq_data, IRQD_PER_CPU);
-       if (irq_settings_can_move_pcntxt(desc))
-               irqd_set(&desc->irq_data, IRQD_MOVE_PCNTXT);
        if (irq_settings_is_level(desc))
                irqd_set(&desc->irq_data, IRQD_LEVEL);
 
Quoted 10 lines

kernel/irq/chip.c void irq_modify_status(unsigned int irq, · line 1138

Quoted 7 lines
kernel/irq/chip.c +7 -0
+       /* Keep this around until x86 is converted over */
+       if (!IS_ENABLED(CONFIG_GENERIC_PENDING_IRQ_CHIPFLAGS)) {
+               irqd_clear(&desc->irq_data, IRQD_MOVE_PCNTXT);
+               if (irq_settings_can_move_pcntxt(desc))
+                       irqd_set(&desc->irq_data, IRQD_MOVE_PCNTXT);
+       }
+

These changes in irq_modify_status() need to be dropped to support the above changes in irq_domain_set_hwirq_and_chip().

Why? With CONFIG_GENERIC_PENDING_IRQ_CHIPFLAGS enabled this hunk is compiled out. So nothing is modifying PCNTXT here. That's the whole point.

Thanks,

tglx

author
#8 lore raw

Re: [PATCH 1/4] irqchip/riscv-imsic: Handle non-atomic MSI updates for device

Quoted 50 lines · Thomas Gleixner
Quoted 5 lines · Anup Patel
Quoted 5 lines
kernel/irq/chip.c +0 -0
@@ -47,6 +47,13 @@ int irq_set_chip(unsigned int irq, const
                return -EINVAL;
 
Quoted 11 lines

kernel/irq/chip.c int irq_set_chip(unsigned int irq, const · line 56

Quoted 8 lines
kernel/irq/chip.c +7 -0
        desc->irq_data.chip = (struct irq_chip *)(chip ?: &no_irq_chip);
+
+       if (IS_ENABLED(CONFIG_GENERIC_PENDING_IRQ_CHIPFLAGS) && chip) {
+               if (chip->flags & IRQCHIP_MOVE_DEFERRED)
+                       irqd_clear(&desc->irq_data, IRQD_MOVE_PCNTXT);
+               else
+                       irqd_set(&desc->irq_data, IRQD_MOVE_PCNTXT);
+       }

We need similar changes in irq_domain_set_hwirq_and_chip() because we use IRQ_DOMAIN_HIERARCHY in RISC-V.

Grr, you are right. Let me add that to the base patch.

Quoted 18 lines
Quoted 18 lines
kernel/irq/chip.c +1 -3
        irq_put_desc_unlock(desc, flags);
        /*
         * For !CONFIG_SPARSE_IRQ make the irq show up in
@@ -1114,16 +1121,21 @@ void irq_modify_status(unsigned int irq,
        trigger = irqd_get_trigger_type(&desc->irq_data);
 
        irqd_clear(&desc->irq_data, IRQD_NO_BALANCING | IRQD_PER_CPU |
-                  IRQD_TRIGGER_MASK | IRQD_LEVEL | IRQD_MOVE_PCNTXT);
+                  IRQD_TRIGGER_MASK | IRQD_LEVEL);
        if (irq_settings_has_no_balance_set(desc))
                irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
        if (irq_settings_is_per_cpu(desc))
                irqd_set(&desc->irq_data, IRQD_PER_CPU);
-       if (irq_settings_can_move_pcntxt(desc))
-               irqd_set(&desc->irq_data, IRQD_MOVE_PCNTXT);
        if (irq_settings_is_level(desc))
                irqd_set(&desc->irq_data, IRQD_LEVEL);
 
Quoted 10 lines

kernel/irq/chip.c void irq_modify_status(unsigned int irq, · line 1138

Quoted 7 lines
kernel/irq/chip.c +7 -0
+       /* Keep this around until x86 is converted over */
+       if (!IS_ENABLED(CONFIG_GENERIC_PENDING_IRQ_CHIPFLAGS)) {
+               irqd_clear(&desc->irq_data, IRQD_MOVE_PCNTXT);
+               if (irq_settings_can_move_pcntxt(desc))
+                       irqd_set(&desc->irq_data, IRQD_MOVE_PCNTXT);
+       }
+

These changes in irq_modify_status() need to be dropped to support the above changes in irq_domain_set_hwirq_and_chip().

Quoted 4 lines

Why? With CONFIG_GENERIC_PENDING_IRQ_CHIPFLAGS enabled this hunk is compiled out. So nothing is modifying PCNTXT here. That's the whole point.

Understood, please ignore my previous comment.

I was using irq_set_status_flags() in irq_domain_set_hwirq_and_chip() which did not work because irq_modify_status() did not modify PCNTXT.

Regards,
Anup