Yang Yingliang | f1e0bb0 | 2015-09-24 17:32:13 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Generic cpu hotunplug interrupt migration code copied from the |
| 3 | * arch/arm implementation |
| 4 | * |
| 5 | * Copyright (C) Russell King |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | #include <linux/interrupt.h> |
| 12 | #include <linux/ratelimit.h> |
| 13 | #include <linux/irq.h> |
Olav Haugan | ca2a91e | 2016-05-29 19:35:54 -0700 | [diff] [blame] | 14 | #include <linux/cpumask.h> |
Yang Yingliang | f1e0bb0 | 2015-09-24 17:32:13 +0800 | [diff] [blame] | 15 | |
| 16 | #include "internals.h" |
| 17 | |
| 18 | static bool migrate_one_irq(struct irq_desc *desc) |
| 19 | { |
| 20 | struct irq_data *d = irq_desc_get_irq_data(desc); |
| 21 | const struct cpumask *affinity = d->common->affinity; |
| 22 | struct irq_chip *c; |
| 23 | bool ret = false; |
Olav Haugan | ca2a91e | 2016-05-29 19:35:54 -0700 | [diff] [blame] | 24 | struct cpumask available_cpus; |
Yang Yingliang | f1e0bb0 | 2015-09-24 17:32:13 +0800 | [diff] [blame] | 25 | |
| 26 | /* |
| 27 | * If this is a per-CPU interrupt, or the affinity does not |
| 28 | * include this CPU, then we have nothing to do. |
| 29 | */ |
| 30 | if (irqd_is_per_cpu(d) || |
| 31 | !cpumask_test_cpu(smp_processor_id(), affinity)) |
| 32 | return false; |
| 33 | |
Olav Haugan | ca2a91e | 2016-05-29 19:35:54 -0700 | [diff] [blame] | 34 | cpumask_copy(&available_cpus, affinity); |
| 35 | cpumask_andnot(&available_cpus, &available_cpus, cpu_isolated_mask); |
| 36 | affinity = &available_cpus; |
| 37 | |
Yang Yingliang | f1e0bb0 | 2015-09-24 17:32:13 +0800 | [diff] [blame] | 38 | if (cpumask_any_and(affinity, cpu_online_mask) >= nr_cpu_ids) { |
Pavankumar Kondeti | f817f45 | 2017-06-16 09:36:25 +0530 | [diff] [blame] | 39 | /* |
| 40 | * The order of preference for selecting a fallback CPU is |
| 41 | * |
| 42 | * (1) online and un-isolated CPU from default affinity |
| 43 | * (2) online and un-isolated CPU |
| 44 | * (3) online CPU |
| 45 | */ |
Olav Haugan | ca2a91e | 2016-05-29 19:35:54 -0700 | [diff] [blame] | 46 | cpumask_andnot(&available_cpus, cpu_online_mask, |
| 47 | cpu_isolated_mask); |
Pavankumar Kondeti | f817f45 | 2017-06-16 09:36:25 +0530 | [diff] [blame] | 48 | if (cpumask_intersects(&available_cpus, irq_default_affinity)) |
| 49 | cpumask_and(&available_cpus, &available_cpus, |
| 50 | irq_default_affinity); |
| 51 | else if (cpumask_empty(&available_cpus)) |
Olav Haugan | ca2a91e | 2016-05-29 19:35:54 -0700 | [diff] [blame] | 52 | affinity = cpu_online_mask; |
Pavankumar Kondeti | f817f45 | 2017-06-16 09:36:25 +0530 | [diff] [blame] | 53 | |
Pavankumar Kondeti | 3c61a7d | 2017-06-22 13:33:08 +0530 | [diff] [blame] | 54 | /* |
| 55 | * We are overriding the affinity with all online and |
| 56 | * un-isolated cpus. irq_set_affinity_locked() call |
| 57 | * below notify this mask to PM QOS affinity listener. |
| 58 | * That results in applying the CPU_DMA_LATENCY QOS |
| 59 | * to all the CPUs specified in the mask. But the low |
| 60 | * level irqchip driver sets the affinity of an irq |
| 61 | * to only one CPU. So pick only one CPU from the |
| 62 | * prepared mask while overriding the user affinity. |
| 63 | */ |
| 64 | affinity = cpumask_of(cpumask_any(affinity)); |
Yang Yingliang | f1e0bb0 | 2015-09-24 17:32:13 +0800 | [diff] [blame] | 65 | ret = true; |
| 66 | } |
| 67 | |
| 68 | c = irq_data_get_irq_chip(d); |
| 69 | if (!c->irq_set_affinity) { |
Thomas Gleixner | 58c9c87 | 2015-10-22 14:34:57 +0200 | [diff] [blame] | 70 | pr_debug("IRQ%u: unable to set affinity\n", d->irq); |
Yang Yingliang | f1e0bb0 | 2015-09-24 17:32:13 +0800 | [diff] [blame] | 71 | } else { |
Maulik Shah | 813aa77 | 2017-02-21 20:44:46 +0530 | [diff] [blame] | 72 | int r = irq_set_affinity_locked(d, affinity, false); |
Yang Yingliang | f1e0bb0 | 2015-09-24 17:32:13 +0800 | [diff] [blame] | 73 | if (r) |
| 74 | pr_warn_ratelimited("IRQ%u: set affinity failed(%d).\n", |
| 75 | d->irq, r); |
| 76 | } |
| 77 | |
| 78 | return ret; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * irq_migrate_all_off_this_cpu - Migrate irqs away from offline cpu |
| 83 | * |
| 84 | * The current CPU has been marked offline. Migrate IRQs off this CPU. |
| 85 | * If the affinity settings do not allow other CPUs, force them onto any |
| 86 | * available CPU. |
| 87 | * |
| 88 | * Note: we must iterate over all IRQs, whether they have an attached |
| 89 | * action structure or not, as we need to get chained interrupts too. |
| 90 | */ |
| 91 | void irq_migrate_all_off_this_cpu(void) |
| 92 | { |
| 93 | unsigned int irq; |
| 94 | struct irq_desc *desc; |
| 95 | unsigned long flags; |
| 96 | |
| 97 | local_irq_save(flags); |
| 98 | |
| 99 | for_each_active_irq(irq) { |
| 100 | bool affinity_broken; |
| 101 | |
| 102 | desc = irq_to_desc(irq); |
Prasad Sodagudi | b00a54c9 | 2016-10-07 08:35:08 +0530 | [diff] [blame] | 103 | if (!desc) |
| 104 | continue; |
| 105 | |
Yang Yingliang | f1e0bb0 | 2015-09-24 17:32:13 +0800 | [diff] [blame] | 106 | raw_spin_lock(&desc->lock); |
| 107 | affinity_broken = migrate_one_irq(desc); |
| 108 | raw_spin_unlock(&desc->lock); |
| 109 | |
| 110 | if (affinity_broken) |
| 111 | pr_warn_ratelimited("IRQ%u no longer affine to CPU%u\n", |
| 112 | irq, smp_processor_id()); |
| 113 | } |
| 114 | |
| 115 | local_irq_restore(flags); |
| 116 | } |