ARM: CPU hotplug: ensure we migrate all IRQs off a downed CPU

Squash following three patches since the patch with 2ef7570 commit
prevents the MSM platforms from booting, which gets solved in the
next commit ca15af1, but adds build failure; which gets solved in the
final commit 78359cb.

commit 2ef75701d1711a1feee2a82b42a2597ddc05f88b
Author: Russell King <rmk+kernel@arm.linux.org.uk>
Date:   Thu Jul 21 14:51:13 2011 +0100

    ARM: CPU hotplug: fix abuse of irqdesc->node

    irqdesc's node member is supposed to mark the numa node number for the
    interrupt.  Our use of it is non-standard.  Remove this, replacing the
    functionality with a test of the affinity mask.

    Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>

commit ca15af19ac07908c8ca386f6d944a18aa343b868
Author: Russell King <rmk+kernel@arm.linux.org.uk>
Date:   Thu Jul 21 15:07:56 2011 +0100

    ARM: CPU hotplug: pass in proper affinity mask on IRQ migration

    Now that the GIC takes care of selecting a target interrupt from the
    affinity mask, we don't need all this complexity in the core code
    anymore.  Just detect when we need to break affinity.

    Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>

commit 78359cb86b8c4c8946f6732eac2757fa5e1d4de4
Author: Russell King <rmk+kernel@arm.linux.org.uk>
Date:   Thu Jul 21 15:14:21 2011 +0100

    ARM: CPU hotplug: ensure we migrate all IRQs off a downed CPU

    Our selection of interrupts to consider for IRQ migration is sub-
    standard.  We were potentially including per-CPU interrupts in our
    migration strategy, but omitting chained interrupts.  This caused
    some interrupts to remain on a downed CPU.

    We were also trying to migrate interrupts which were not migratable,
    resulting in an OOPS.

    Instead, iterate over all interrupts, skipping per-CPU interrupts
    or interrupts whose affinity does not include the downed CPU, and
    attempt to set the affinity for every one else if their chip
    implements irq_set_affinity().

    Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>

Change-Id: I1559f4f9b2aa52ce026505c7e4d5e7cf9bd102af
Signed-off-by: Ravi Kumar <kumarrav@codeaurora.org>
[tsoni@codeaurora.org: squash the commits to avoid boot & build failures]
Signed-off-by: Trilok Soni <tsoni@codeaurora.org>
diff --git a/arch/arm/kernel/irq.c b/arch/arm/kernel/irq.c
index 4ef97a0..5bd484f 100644
--- a/arch/arm/kernel/irq.c
+++ b/arch/arm/kernel/irq.c
@@ -144,54 +144,63 @@
 
 #ifdef CONFIG_HOTPLUG_CPU
 
-static bool migrate_one_irq(struct irq_data *d)
+static bool migrate_one_irq(struct irq_desc *desc)
 {
-	unsigned int cpu = cpumask_any_and(d->affinity, cpu_online_mask);
+	struct irq_data *d = irq_desc_get_irq_data(desc);
+	const struct cpumask *affinity = d->affinity;
+	struct irq_chip *c;
 	bool ret = false;
 
-	if (cpu >= nr_cpu_ids) {
-		cpu = cpumask_any(cpu_online_mask);
+	/*
+	 * If this is a per-CPU interrupt, or the affinity does not
+	 * include this CPU, then we have nothing to do.
+	 */
+	if (irqd_is_per_cpu(d) || !cpumask_test_cpu(smp_processor_id(), affinity))
+		return false;
+
+	if (cpumask_any_and(affinity, cpu_online_mask) >= nr_cpu_ids) {
+		affinity = cpu_online_mask;
 		ret = true;
 	}
 
-	pr_debug("IRQ%u: moving from cpu%u to cpu%u\n", d->irq, d->node, cpu);
-
-	d->chip->irq_set_affinity(d, cpumask_of(cpu), true);
+	c = irq_data_get_irq_chip(d);
+	if (c->irq_set_affinity)
+		c->irq_set_affinity(d, affinity, true);
+	else
+		pr_debug("IRQ%u: unable to set affinity\n", d->irq);
 
 	return ret;
 }
 
 /*
- * The CPU has been marked offline.  Migrate IRQs off this CPU.  If
- * the affinity settings do not allow other CPUs, force them onto any
+ * The current CPU has been marked offline.  Migrate IRQs off this CPU.
+ * If the affinity settings do not allow other CPUs, force them onto any
  * available CPU.
+ *
+ * Note: we must iterate over all IRQs, whether they have an attached
+ * action structure or not, as we need to get chained interrupts too.
  */
 void migrate_irqs(void)
 {
-	unsigned int i, cpu = smp_processor_id();
+	unsigned int i;
 	struct irq_desc *desc;
 	unsigned long flags;
 
 	local_irq_save(flags);
 
 	for_each_irq_desc(i, desc) {
-		struct irq_data *d = &desc->irq_data;
 		bool affinity_broken = false;
 
+		if (!desc)
+			continue;
+
 		raw_spin_lock(&desc->lock);
-		do {
-			if (desc->action == NULL)
-				break;
-
-			if (d->node != cpu)
-				break;
-
-			affinity_broken = migrate_one_irq(d);
-		} while (0);
+		affinity_broken = migrate_one_irq(desc);
 		raw_spin_unlock(&desc->lock);
 
 		if (affinity_broken && printk_ratelimit())
-			pr_warning("IRQ%u no longer affine to CPU%u\n", i, cpu);
+			pr_warning("IRQ%u no longer affine to CPU%u\n", i,
+				smp_processor_id());
 	}
 
 	local_irq_restore(flags);