Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Local APIC related interfaces to support IOAPIC, MSI, HT_IRQ etc. |
| 3 | * |
| 4 | * Copyright (C) 1997, 1998, 1999, 2000, 2009 Ingo Molnar, Hajnalka Szabo |
| 5 | * Moved from arch/x86/kernel/apic/io_apic.c. |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 6 | * Jiang Liu <jiang.liu@linux.intel.com> |
| 7 | * Enable support of hierarchical irqdomains |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | #include <linux/interrupt.h> |
Thomas Gleixner | 65d7ed5 | 2017-09-13 23:29:39 +0200 | [diff] [blame] | 14 | #include <linux/seq_file.h> |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 15 | #include <linux/init.h> |
| 16 | #include <linux/compiler.h> |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 17 | #include <linux/slab.h> |
Jiang Liu | d746d1e | 2015-04-14 10:30:09 +0800 | [diff] [blame] | 18 | #include <asm/irqdomain.h> |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 19 | #include <asm/hw_irq.h> |
| 20 | #include <asm/apic.h> |
| 21 | #include <asm/i8259.h> |
| 22 | #include <asm/desc.h> |
| 23 | #include <asm/irq_remapping.h> |
| 24 | |
Thomas Gleixner | 8d1e3dc | 2017-09-13 23:29:41 +0200 | [diff] [blame] | 25 | #include <asm/trace/irq_vectors.h> |
| 26 | |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 27 | struct apic_chip_data { |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 28 | struct irq_cfg hw_irq_cfg; |
| 29 | unsigned int vector; |
| 30 | unsigned int prev_vector; |
Thomas Gleixner | 029c6e1 | 2017-09-13 23:29:31 +0200 | [diff] [blame] | 31 | unsigned int cpu; |
| 32 | unsigned int prev_cpu; |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 33 | unsigned int irq; |
Thomas Gleixner | dccfe31 | 2017-09-13 23:29:32 +0200 | [diff] [blame] | 34 | struct hlist_node clist; |
Thomas Gleixner | 2db1f95 | 2017-09-13 23:29:50 +0200 | [diff] [blame] | 35 | unsigned int move_in_progress : 1, |
Thomas Gleixner | 4900be8 | 2017-09-13 23:29:51 +0200 | [diff] [blame^] | 36 | is_managed : 1, |
| 37 | can_reserve : 1, |
| 38 | has_reserved : 1; |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 39 | }; |
| 40 | |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 41 | struct irq_domain *x86_vector_domain; |
Jake Oshins | c8f3e51 | 2015-12-10 17:52:59 +0000 | [diff] [blame] | 42 | EXPORT_SYMBOL_GPL(x86_vector_domain); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 43 | static DEFINE_RAW_SPINLOCK(vector_lock); |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 44 | static cpumask_var_t vector_searchmask; |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 45 | static struct irq_chip lapic_controller; |
Thomas Gleixner | 0fa115d | 2017-09-13 23:29:38 +0200 | [diff] [blame] | 46 | static struct irq_matrix *vector_matrix; |
Thomas Gleixner | dccfe31 | 2017-09-13 23:29:32 +0200 | [diff] [blame] | 47 | #ifdef CONFIG_SMP |
| 48 | static DEFINE_PER_CPU(struct hlist_head, cleanup_list); |
| 49 | #endif |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 50 | |
| 51 | void lock_vector_lock(void) |
| 52 | { |
| 53 | /* Used to the online set of cpus does not change |
| 54 | * during assign_irq_vector. |
| 55 | */ |
| 56 | raw_spin_lock(&vector_lock); |
| 57 | } |
| 58 | |
| 59 | void unlock_vector_lock(void) |
| 60 | { |
| 61 | raw_spin_unlock(&vector_lock); |
| 62 | } |
| 63 | |
Thomas Gleixner | 99a1482 | 2017-09-13 23:29:36 +0200 | [diff] [blame] | 64 | void init_irq_alloc_info(struct irq_alloc_info *info, |
| 65 | const struct cpumask *mask) |
| 66 | { |
| 67 | memset(info, 0, sizeof(*info)); |
| 68 | info->mask = mask; |
| 69 | } |
| 70 | |
| 71 | void copy_irq_alloc_info(struct irq_alloc_info *dst, struct irq_alloc_info *src) |
| 72 | { |
| 73 | if (src) |
| 74 | *dst = *src; |
| 75 | else |
| 76 | memset(dst, 0, sizeof(*dst)); |
| 77 | } |
| 78 | |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 79 | static struct apic_chip_data *apic_chip_data(struct irq_data *irqd) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 80 | { |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 81 | if (!irqd) |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 82 | return NULL; |
| 83 | |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 84 | while (irqd->parent_data) |
| 85 | irqd = irqd->parent_data; |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 86 | |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 87 | return irqd->chip_data; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 88 | } |
| 89 | |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 90 | struct irq_cfg *irqd_cfg(struct irq_data *irqd) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 91 | { |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 92 | struct apic_chip_data *apicd = apic_chip_data(irqd); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 93 | |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 94 | return apicd ? &apicd->hw_irq_cfg : NULL; |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 95 | } |
Jake Oshins | c8f3e51 | 2015-12-10 17:52:59 +0000 | [diff] [blame] | 96 | EXPORT_SYMBOL_GPL(irqd_cfg); |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 97 | |
| 98 | struct irq_cfg *irq_cfg(unsigned int irq) |
| 99 | { |
| 100 | return irqd_cfg(irq_get_irq_data(irq)); |
| 101 | } |
| 102 | |
| 103 | static struct apic_chip_data *alloc_apic_chip_data(int node) |
| 104 | { |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 105 | struct apic_chip_data *apicd; |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 106 | |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 107 | apicd = kzalloc_node(sizeof(*apicd), GFP_KERNEL, node); |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 108 | if (apicd) |
| 109 | INIT_HLIST_NODE(&apicd->clist); |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 110 | return apicd; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 111 | } |
| 112 | |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 113 | static void free_apic_chip_data(struct apic_chip_data *apicd) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 114 | { |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 115 | kfree(apicd); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 116 | } |
| 117 | |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 118 | static void apic_update_irq_cfg(struct irq_data *irqd, unsigned int vector, |
| 119 | unsigned int cpu) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 120 | { |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 121 | struct apic_chip_data *apicd = apic_chip_data(irqd); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 122 | |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 123 | lockdep_assert_held(&vector_lock); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 124 | |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 125 | apicd->hw_irq_cfg.vector = vector; |
| 126 | apicd->hw_irq_cfg.dest_apicid = apic->calc_dest_apicid(cpu); |
| 127 | irq_data_update_effective_affinity(irqd, cpumask_of(cpu)); |
| 128 | trace_vector_config(irqd->irq, vector, cpu, |
| 129 | apicd->hw_irq_cfg.dest_apicid); |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 130 | } |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 131 | |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 132 | static void apic_update_vector(struct irq_data *irqd, unsigned int newvec, |
| 133 | unsigned int newcpu) |
| 134 | { |
| 135 | struct apic_chip_data *apicd = apic_chip_data(irqd); |
| 136 | struct irq_desc *desc = irq_data_to_desc(irqd); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 137 | |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 138 | lockdep_assert_held(&vector_lock); |
Thomas Gleixner | 3716fd2 | 2015-12-31 16:30:48 +0000 | [diff] [blame] | 139 | |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 140 | trace_vector_update(irqd->irq, newvec, newcpu, apicd->vector, |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 141 | apicd->cpu); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 142 | |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 143 | /* Setup the vector move, if required */ |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 144 | if (apicd->vector && cpu_online(apicd->cpu)) { |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 145 | apicd->move_in_progress = true; |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 146 | apicd->prev_vector = apicd->vector; |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 147 | apicd->prev_cpu = apicd->cpu; |
| 148 | } else { |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 149 | apicd->prev_vector = 0; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 150 | } |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 151 | |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 152 | apicd->vector = newvec; |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 153 | apicd->cpu = newcpu; |
| 154 | BUG_ON(!IS_ERR_OR_NULL(per_cpu(vector_irq, newcpu)[newvec])); |
| 155 | per_cpu(vector_irq, newcpu)[newvec] = desc; |
| 156 | } |
| 157 | |
Thomas Gleixner | 2db1f95 | 2017-09-13 23:29:50 +0200 | [diff] [blame] | 158 | static void vector_assign_managed_shutdown(struct irq_data *irqd) |
| 159 | { |
| 160 | unsigned int cpu = cpumask_first(cpu_online_mask); |
| 161 | |
| 162 | apic_update_irq_cfg(irqd, MANAGED_IRQ_SHUTDOWN_VECTOR, cpu); |
| 163 | } |
| 164 | |
| 165 | static int reserve_managed_vector(struct irq_data *irqd) |
| 166 | { |
| 167 | const struct cpumask *affmsk = irq_data_get_affinity_mask(irqd); |
| 168 | struct apic_chip_data *apicd = apic_chip_data(irqd); |
| 169 | unsigned long flags; |
| 170 | int ret; |
| 171 | |
| 172 | raw_spin_lock_irqsave(&vector_lock, flags); |
| 173 | apicd->is_managed = true; |
| 174 | ret = irq_matrix_reserve_managed(vector_matrix, affmsk); |
| 175 | raw_spin_unlock_irqrestore(&vector_lock, flags); |
| 176 | trace_vector_reserve_managed(irqd->irq, ret); |
| 177 | return ret; |
| 178 | } |
| 179 | |
Thomas Gleixner | 4900be8 | 2017-09-13 23:29:51 +0200 | [diff] [blame^] | 180 | static void reserve_irq_vector_locked(struct irq_data *irqd) |
| 181 | { |
| 182 | struct apic_chip_data *apicd = apic_chip_data(irqd); |
| 183 | |
| 184 | irq_matrix_reserve(vector_matrix); |
| 185 | apicd->can_reserve = true; |
| 186 | apicd->has_reserved = true; |
| 187 | trace_vector_reserve(irqd->irq, 0); |
| 188 | vector_assign_managed_shutdown(irqd); |
| 189 | } |
| 190 | |
| 191 | static int reserve_irq_vector(struct irq_data *irqd) |
| 192 | { |
| 193 | unsigned long flags; |
| 194 | |
| 195 | raw_spin_lock_irqsave(&vector_lock, flags); |
| 196 | reserve_irq_vector_locked(irqd); |
| 197 | raw_spin_unlock_irqrestore(&vector_lock, flags); |
| 198 | return 0; |
| 199 | } |
| 200 | |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 201 | static int allocate_vector(struct irq_data *irqd, const struct cpumask *dest) |
| 202 | { |
| 203 | struct apic_chip_data *apicd = apic_chip_data(irqd); |
Thomas Gleixner | 4900be8 | 2017-09-13 23:29:51 +0200 | [diff] [blame^] | 204 | bool resvd = apicd->has_reserved; |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 205 | unsigned int cpu = apicd->cpu; |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 206 | int vector = apicd->vector; |
| 207 | |
| 208 | lockdep_assert_held(&vector_lock); |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 209 | |
Thomas Gleixner | 847667e | 2015-12-31 16:30:50 +0000 | [diff] [blame] | 210 | /* |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 211 | * If the current target CPU is online and in the new requested |
| 212 | * affinity mask, there is no point in moving the interrupt from |
| 213 | * one CPU to another. |
Thomas Gleixner | 847667e | 2015-12-31 16:30:50 +0000 | [diff] [blame] | 214 | */ |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 215 | if (vector && cpu_online(cpu) && cpumask_test_cpu(cpu, dest)) |
| 216 | return 0; |
| 217 | |
Thomas Gleixner | 4900be8 | 2017-09-13 23:29:51 +0200 | [diff] [blame^] | 218 | vector = irq_matrix_alloc(vector_matrix, dest, resvd, &cpu); |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 219 | if (vector > 0) |
| 220 | apic_update_vector(irqd, vector, cpu); |
Thomas Gleixner | 4900be8 | 2017-09-13 23:29:51 +0200 | [diff] [blame^] | 221 | trace_vector_alloc(irqd->irq, vector, resvd, vector); |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 222 | return vector; |
| 223 | } |
| 224 | |
| 225 | static int assign_vector_locked(struct irq_data *irqd, |
| 226 | const struct cpumask *dest) |
| 227 | { |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 228 | struct apic_chip_data *apicd = apic_chip_data(irqd); |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 229 | int vector = allocate_vector(irqd, dest); |
| 230 | |
| 231 | if (vector < 0) |
| 232 | return vector; |
| 233 | |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 234 | apic_update_irq_cfg(irqd, apicd->vector, apicd->cpu); |
Thomas Gleixner | 3716fd2 | 2015-12-31 16:30:48 +0000 | [diff] [blame] | 235 | return 0; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 236 | } |
| 237 | |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 238 | static int assign_irq_vector(struct irq_data *irqd, const struct cpumask *dest) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 239 | { |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 240 | unsigned long flags; |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 241 | int ret; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 242 | |
| 243 | raw_spin_lock_irqsave(&vector_lock, flags); |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 244 | cpumask_and(vector_searchmask, dest, cpu_online_mask); |
| 245 | ret = assign_vector_locked(irqd, vector_searchmask); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 246 | raw_spin_unlock_irqrestore(&vector_lock, flags); |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 247 | return ret; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 248 | } |
| 249 | |
Thomas Gleixner | 2db1f95 | 2017-09-13 23:29:50 +0200 | [diff] [blame] | 250 | static int assign_irq_vector_any_locked(struct irq_data *irqd) |
Jiang Liu | 486ca53 | 2015-05-07 10:53:56 +0800 | [diff] [blame] | 251 | { |
Thomas Gleixner | 2db1f95 | 2017-09-13 23:29:50 +0200 | [diff] [blame] | 252 | int node = irq_data_get_node(irqd); |
| 253 | |
| 254 | if (node != NUMA_NO_NODE) { |
| 255 | if (!assign_vector_locked(irqd, cpumask_of_node(node))) |
| 256 | return 0; |
| 257 | } |
| 258 | return assign_vector_locked(irqd, cpu_online_mask); |
| 259 | } |
| 260 | |
| 261 | static int assign_irq_vector_any(struct irq_data *irqd) |
| 262 | { |
| 263 | unsigned long flags; |
| 264 | int ret; |
| 265 | |
| 266 | raw_spin_lock_irqsave(&vector_lock, flags); |
| 267 | ret = assign_irq_vector_any_locked(irqd); |
| 268 | raw_spin_unlock_irqrestore(&vector_lock, flags); |
| 269 | return ret; |
| 270 | } |
| 271 | |
| 272 | static int |
| 273 | assign_irq_vector_policy(struct irq_data *irqd, struct irq_alloc_info *info) |
| 274 | { |
| 275 | if (irqd_affinity_is_managed(irqd)) |
| 276 | return reserve_managed_vector(irqd); |
Thomas Gleixner | 258d86e | 2017-09-13 23:29:35 +0200 | [diff] [blame] | 277 | if (info->mask) |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 278 | return assign_irq_vector(irqd, info->mask); |
Thomas Gleixner | 4900be8 | 2017-09-13 23:29:51 +0200 | [diff] [blame^] | 279 | if (info->type != X86_IRQ_ALLOC_TYPE_MSI && |
| 280 | info->type != X86_IRQ_ALLOC_TYPE_MSIX) |
| 281 | return assign_irq_vector_any(irqd); |
| 282 | /* For MSI(X) make only a global reservation with no guarantee */ |
| 283 | return reserve_irq_vector(irqd); |
Thomas Gleixner | 2db1f95 | 2017-09-13 23:29:50 +0200 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | static int |
| 287 | assign_managed_vector(struct irq_data *irqd, const struct cpumask *dest) |
| 288 | { |
| 289 | const struct cpumask *affmsk = irq_data_get_affinity_mask(irqd); |
| 290 | struct apic_chip_data *apicd = apic_chip_data(irqd); |
| 291 | int vector, cpu; |
| 292 | |
| 293 | cpumask_and(vector_searchmask, vector_searchmask, affmsk); |
| 294 | cpu = cpumask_first(vector_searchmask); |
| 295 | if (cpu >= nr_cpu_ids) |
| 296 | return -EINVAL; |
| 297 | /* set_affinity might call here for nothing */ |
| 298 | if (apicd->vector && cpumask_test_cpu(apicd->cpu, vector_searchmask)) |
Jiang Liu | 486ca53 | 2015-05-07 10:53:56 +0800 | [diff] [blame] | 299 | return 0; |
Thomas Gleixner | 2db1f95 | 2017-09-13 23:29:50 +0200 | [diff] [blame] | 300 | vector = irq_matrix_alloc_managed(vector_matrix, cpu); |
| 301 | trace_vector_alloc_managed(irqd->irq, vector, vector); |
| 302 | if (vector < 0) |
| 303 | return vector; |
| 304 | apic_update_vector(irqd, vector, cpu); |
| 305 | apic_update_irq_cfg(irqd, vector, cpu); |
| 306 | return 0; |
Jiang Liu | 486ca53 | 2015-05-07 10:53:56 +0800 | [diff] [blame] | 307 | } |
| 308 | |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 309 | static void clear_irq_vector(struct irq_data *irqd) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 310 | { |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 311 | struct apic_chip_data *apicd = apic_chip_data(irqd); |
Thomas Gleixner | 2db1f95 | 2017-09-13 23:29:50 +0200 | [diff] [blame] | 312 | bool managed = irqd_affinity_is_managed(irqd); |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 313 | unsigned int vector = apicd->vector; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 314 | |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 315 | lockdep_assert_held(&vector_lock); |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 316 | |
Thomas Gleixner | dccfe31 | 2017-09-13 23:29:32 +0200 | [diff] [blame] | 317 | if (!vector) |
Keith Busch | 1bdb897 | 2016-04-27 14:22:32 -0600 | [diff] [blame] | 318 | return; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 319 | |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 320 | trace_vector_clear(irqd->irq, vector, apicd->cpu, apicd->prev_vector, |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 321 | apicd->prev_cpu); |
| 322 | |
Thomas Gleixner | dccfe31 | 2017-09-13 23:29:32 +0200 | [diff] [blame] | 323 | per_cpu(vector_irq, apicd->cpu)[vector] = VECTOR_UNUSED; |
Thomas Gleixner | 2db1f95 | 2017-09-13 23:29:50 +0200 | [diff] [blame] | 324 | irq_matrix_free(vector_matrix, apicd->cpu, vector, managed); |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 325 | apicd->vector = 0; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 326 | |
Thomas Gleixner | dccfe31 | 2017-09-13 23:29:32 +0200 | [diff] [blame] | 327 | /* Clean up move in progress */ |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 328 | vector = apicd->prev_vector; |
Thomas Gleixner | dccfe31 | 2017-09-13 23:29:32 +0200 | [diff] [blame] | 329 | if (!vector) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 330 | return; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 331 | |
Thomas Gleixner | dccfe31 | 2017-09-13 23:29:32 +0200 | [diff] [blame] | 332 | per_cpu(vector_irq, apicd->prev_cpu)[vector] = VECTOR_UNUSED; |
Thomas Gleixner | 2db1f95 | 2017-09-13 23:29:50 +0200 | [diff] [blame] | 333 | irq_matrix_free(vector_matrix, apicd->prev_cpu, vector, managed); |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 334 | apicd->prev_vector = 0; |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 335 | apicd->move_in_progress = 0; |
Thomas Gleixner | dccfe31 | 2017-09-13 23:29:32 +0200 | [diff] [blame] | 336 | hlist_del_init(&apicd->clist); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 337 | } |
| 338 | |
Thomas Gleixner | 2db1f95 | 2017-09-13 23:29:50 +0200 | [diff] [blame] | 339 | static void x86_vector_deactivate(struct irq_domain *dom, struct irq_data *irqd) |
| 340 | { |
| 341 | struct apic_chip_data *apicd = apic_chip_data(irqd); |
| 342 | unsigned long flags; |
| 343 | |
| 344 | trace_vector_deactivate(irqd->irq, apicd->is_managed, |
Thomas Gleixner | 4900be8 | 2017-09-13 23:29:51 +0200 | [diff] [blame^] | 345 | apicd->can_reserve, false); |
Thomas Gleixner | 2db1f95 | 2017-09-13 23:29:50 +0200 | [diff] [blame] | 346 | |
Thomas Gleixner | 4900be8 | 2017-09-13 23:29:51 +0200 | [diff] [blame^] | 347 | /* Regular fixed assigned interrupt */ |
| 348 | if (!apicd->is_managed && !apicd->can_reserve) |
| 349 | return; |
| 350 | /* If the interrupt has a global reservation, nothing to do */ |
| 351 | if (apicd->has_reserved) |
Thomas Gleixner | 2db1f95 | 2017-09-13 23:29:50 +0200 | [diff] [blame] | 352 | return; |
| 353 | |
| 354 | raw_spin_lock_irqsave(&vector_lock, flags); |
| 355 | clear_irq_vector(irqd); |
Thomas Gleixner | 4900be8 | 2017-09-13 23:29:51 +0200 | [diff] [blame^] | 356 | if (apicd->can_reserve) |
| 357 | reserve_irq_vector_locked(irqd); |
| 358 | else |
| 359 | vector_assign_managed_shutdown(irqd); |
Thomas Gleixner | 2db1f95 | 2017-09-13 23:29:50 +0200 | [diff] [blame] | 360 | raw_spin_unlock_irqrestore(&vector_lock, flags); |
| 361 | } |
| 362 | |
Thomas Gleixner | 4900be8 | 2017-09-13 23:29:51 +0200 | [diff] [blame^] | 363 | static int activate_reserved(struct irq_data *irqd) |
| 364 | { |
| 365 | struct apic_chip_data *apicd = apic_chip_data(irqd); |
| 366 | int ret; |
| 367 | |
| 368 | ret = assign_irq_vector_any_locked(irqd); |
| 369 | if (!ret) |
| 370 | apicd->has_reserved = false; |
| 371 | return ret; |
| 372 | } |
| 373 | |
Thomas Gleixner | 2db1f95 | 2017-09-13 23:29:50 +0200 | [diff] [blame] | 374 | static int activate_managed(struct irq_data *irqd) |
| 375 | { |
| 376 | const struct cpumask *dest = irq_data_get_affinity_mask(irqd); |
| 377 | int ret; |
| 378 | |
| 379 | cpumask_and(vector_searchmask, dest, cpu_online_mask); |
| 380 | if (WARN_ON_ONCE(cpumask_empty(vector_searchmask))) { |
| 381 | /* Something in the core code broke! Survive gracefully */ |
| 382 | pr_err("Managed startup for irq %u, but no CPU\n", irqd->irq); |
| 383 | return EINVAL; |
| 384 | } |
| 385 | |
| 386 | ret = assign_managed_vector(irqd, vector_searchmask); |
| 387 | /* |
| 388 | * This should not happen. The vector reservation got buggered. Handle |
| 389 | * it gracefully. |
| 390 | */ |
| 391 | if (WARN_ON_ONCE(ret < 0)) { |
| 392 | pr_err("Managed startup irq %u, no vector available\n", |
| 393 | irqd->irq); |
| 394 | } |
| 395 | return ret; |
| 396 | } |
| 397 | |
| 398 | static int x86_vector_activate(struct irq_domain *dom, struct irq_data *irqd, |
| 399 | bool early) |
| 400 | { |
| 401 | struct apic_chip_data *apicd = apic_chip_data(irqd); |
| 402 | unsigned long flags; |
| 403 | int ret = 0; |
| 404 | |
| 405 | trace_vector_activate(irqd->irq, apicd->is_managed, |
Thomas Gleixner | 4900be8 | 2017-09-13 23:29:51 +0200 | [diff] [blame^] | 406 | apicd->can_reserve, early); |
Thomas Gleixner | 2db1f95 | 2017-09-13 23:29:50 +0200 | [diff] [blame] | 407 | |
Thomas Gleixner | 4900be8 | 2017-09-13 23:29:51 +0200 | [diff] [blame^] | 408 | /* Nothing to do for fixed assigned vectors */ |
| 409 | if (!apicd->can_reserve && !apicd->is_managed) |
Thomas Gleixner | 2db1f95 | 2017-09-13 23:29:50 +0200 | [diff] [blame] | 410 | return 0; |
| 411 | |
| 412 | raw_spin_lock_irqsave(&vector_lock, flags); |
| 413 | if (early || irqd_is_managed_and_shutdown(irqd)) |
| 414 | vector_assign_managed_shutdown(irqd); |
Thomas Gleixner | 4900be8 | 2017-09-13 23:29:51 +0200 | [diff] [blame^] | 415 | else if (apicd->is_managed) |
Thomas Gleixner | 2db1f95 | 2017-09-13 23:29:50 +0200 | [diff] [blame] | 416 | ret = activate_managed(irqd); |
Thomas Gleixner | 4900be8 | 2017-09-13 23:29:51 +0200 | [diff] [blame^] | 417 | else if (apicd->has_reserved) |
| 418 | ret = activate_reserved(irqd); |
Thomas Gleixner | 2db1f95 | 2017-09-13 23:29:50 +0200 | [diff] [blame] | 419 | raw_spin_unlock_irqrestore(&vector_lock, flags); |
| 420 | return ret; |
| 421 | } |
| 422 | |
| 423 | static void vector_free_reserved_and_managed(struct irq_data *irqd) |
| 424 | { |
| 425 | const struct cpumask *dest = irq_data_get_affinity_mask(irqd); |
| 426 | struct apic_chip_data *apicd = apic_chip_data(irqd); |
| 427 | |
Thomas Gleixner | 4900be8 | 2017-09-13 23:29:51 +0200 | [diff] [blame^] | 428 | trace_vector_teardown(irqd->irq, apicd->is_managed, |
| 429 | apicd->has_reserved); |
Thomas Gleixner | 2db1f95 | 2017-09-13 23:29:50 +0200 | [diff] [blame] | 430 | |
Thomas Gleixner | 4900be8 | 2017-09-13 23:29:51 +0200 | [diff] [blame^] | 431 | if (apicd->has_reserved) |
| 432 | irq_matrix_remove_reserved(vector_matrix); |
Thomas Gleixner | 2db1f95 | 2017-09-13 23:29:50 +0200 | [diff] [blame] | 433 | if (apicd->is_managed) |
| 434 | irq_matrix_remove_managed(vector_matrix, dest); |
| 435 | } |
| 436 | |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 437 | static void x86_vector_free_irqs(struct irq_domain *domain, |
| 438 | unsigned int virq, unsigned int nr_irqs) |
| 439 | { |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 440 | struct apic_chip_data *apicd; |
| 441 | struct irq_data *irqd; |
Jiang Liu | 111abeb | 2015-12-31 16:30:44 +0000 | [diff] [blame] | 442 | unsigned long flags; |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 443 | int i; |
| 444 | |
| 445 | for (i = 0; i < nr_irqs; i++) { |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 446 | irqd = irq_domain_get_irq_data(x86_vector_domain, virq + i); |
| 447 | if (irqd && irqd->chip_data) { |
Jiang Liu | 111abeb | 2015-12-31 16:30:44 +0000 | [diff] [blame] | 448 | raw_spin_lock_irqsave(&vector_lock, flags); |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 449 | clear_irq_vector(irqd); |
Thomas Gleixner | 2db1f95 | 2017-09-13 23:29:50 +0200 | [diff] [blame] | 450 | vector_free_reserved_and_managed(irqd); |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 451 | apicd = irqd->chip_data; |
| 452 | irq_domain_reset_irq_data(irqd); |
Jiang Liu | 111abeb | 2015-12-31 16:30:44 +0000 | [diff] [blame] | 453 | raw_spin_unlock_irqrestore(&vector_lock, flags); |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 454 | free_apic_chip_data(apicd); |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | static int x86_vector_alloc_irqs(struct irq_domain *domain, unsigned int virq, |
| 460 | unsigned int nr_irqs, void *arg) |
| 461 | { |
| 462 | struct irq_alloc_info *info = arg; |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 463 | struct apic_chip_data *apicd; |
| 464 | struct irq_data *irqd; |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 465 | unsigned long flags; |
Jiang Liu | 5f2dbbc | 2015-06-01 16:05:14 +0800 | [diff] [blame] | 466 | int i, err, node; |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 467 | |
| 468 | if (disable_apic) |
| 469 | return -ENXIO; |
| 470 | |
| 471 | /* Currently vector allocator can't guarantee contiguous allocations */ |
| 472 | if ((info->flags & X86_IRQ_ALLOC_CONTIGUOUS_VECTORS) && nr_irqs > 1) |
| 473 | return -ENOSYS; |
| 474 | |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 475 | for (i = 0; i < nr_irqs; i++) { |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 476 | irqd = irq_domain_get_irq_data(domain, virq + i); |
| 477 | BUG_ON(!irqd); |
| 478 | node = irq_data_get_node(irqd); |
Thomas Gleixner | 4ef76eb | 2017-09-13 23:29:34 +0200 | [diff] [blame] | 479 | WARN_ON_ONCE(irqd->chip_data); |
| 480 | apicd = alloc_apic_chip_data(node); |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 481 | if (!apicd) { |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 482 | err = -ENOMEM; |
| 483 | goto error; |
| 484 | } |
| 485 | |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 486 | apicd->irq = virq + i; |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 487 | irqd->chip = &lapic_controller; |
| 488 | irqd->chip_data = apicd; |
| 489 | irqd->hwirq = virq + i; |
| 490 | irqd_set_single_target(irqd); |
Thomas Gleixner | 4ef76eb | 2017-09-13 23:29:34 +0200 | [diff] [blame] | 491 | /* |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 492 | * Legacy vectors are already assigned when the IOAPIC |
| 493 | * takes them over. They stay on the same vector. This is |
| 494 | * required for check_timer() to work correctly as it might |
| 495 | * switch back to legacy mode. Only update the hardware |
| 496 | * config. |
Thomas Gleixner | 4ef76eb | 2017-09-13 23:29:34 +0200 | [diff] [blame] | 497 | */ |
| 498 | if (info->flags & X86_IRQ_ALLOC_LEGACY) { |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 499 | apicd->vector = ISA_IRQ_VECTOR(virq + i); |
Thomas Gleixner | 4ef76eb | 2017-09-13 23:29:34 +0200 | [diff] [blame] | 500 | apicd->cpu = 0; |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 501 | trace_vector_setup(virq + i, true, 0); |
| 502 | raw_spin_lock_irqsave(&vector_lock, flags); |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 503 | apic_update_irq_cfg(irqd, apicd->vector, apicd->cpu); |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 504 | raw_spin_unlock_irqrestore(&vector_lock, flags); |
| 505 | continue; |
Thomas Gleixner | 4ef76eb | 2017-09-13 23:29:34 +0200 | [diff] [blame] | 506 | } |
| 507 | |
Thomas Gleixner | 2db1f95 | 2017-09-13 23:29:50 +0200 | [diff] [blame] | 508 | err = assign_irq_vector_policy(irqd, info); |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 509 | trace_vector_setup(virq + i, false, err); |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 510 | if (err) |
| 511 | goto error; |
| 512 | } |
| 513 | |
| 514 | return 0; |
| 515 | |
| 516 | error: |
| 517 | x86_vector_free_irqs(domain, virq, i + 1); |
| 518 | return err; |
| 519 | } |
| 520 | |
Thomas Gleixner | 65d7ed5 | 2017-09-13 23:29:39 +0200 | [diff] [blame] | 521 | #ifdef CONFIG_GENERIC_IRQ_DEBUGFS |
| 522 | void x86_vector_debug_show(struct seq_file *m, struct irq_domain *d, |
| 523 | struct irq_data *irqd, int ind) |
| 524 | { |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 525 | unsigned int cpu, vector, prev_cpu, prev_vector; |
Thomas Gleixner | 65d7ed5 | 2017-09-13 23:29:39 +0200 | [diff] [blame] | 526 | struct apic_chip_data *apicd; |
| 527 | unsigned long flags; |
| 528 | int irq; |
| 529 | |
| 530 | if (!irqd) { |
| 531 | irq_matrix_debug_show(m, vector_matrix, ind); |
| 532 | return; |
| 533 | } |
| 534 | |
| 535 | irq = irqd->irq; |
| 536 | if (irq < nr_legacy_irqs() && !test_bit(irq, &io_apic_irqs)) { |
| 537 | seq_printf(m, "%*sVector: %5d\n", ind, "", ISA_IRQ_VECTOR(irq)); |
| 538 | seq_printf(m, "%*sTarget: Legacy PIC all CPUs\n", ind, ""); |
| 539 | return; |
| 540 | } |
| 541 | |
| 542 | apicd = irqd->chip_data; |
| 543 | if (!apicd) { |
| 544 | seq_printf(m, "%*sVector: Not assigned\n", ind, ""); |
| 545 | return; |
| 546 | } |
| 547 | |
| 548 | raw_spin_lock_irqsave(&vector_lock, flags); |
| 549 | cpu = apicd->cpu; |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 550 | vector = apicd->vector; |
Thomas Gleixner | 65d7ed5 | 2017-09-13 23:29:39 +0200 | [diff] [blame] | 551 | prev_cpu = apicd->prev_cpu; |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 552 | prev_vector = apicd->prev_vector; |
Thomas Gleixner | 65d7ed5 | 2017-09-13 23:29:39 +0200 | [diff] [blame] | 553 | raw_spin_unlock_irqrestore(&vector_lock, flags); |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 554 | seq_printf(m, "%*sVector: %5u\n", ind, "", vector); |
Thomas Gleixner | 65d7ed5 | 2017-09-13 23:29:39 +0200 | [diff] [blame] | 555 | seq_printf(m, "%*sTarget: %5u\n", ind, "", cpu); |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 556 | if (prev_vector) { |
| 557 | seq_printf(m, "%*sPrevious vector: %5u\n", ind, "", prev_vector); |
Thomas Gleixner | 65d7ed5 | 2017-09-13 23:29:39 +0200 | [diff] [blame] | 558 | seq_printf(m, "%*sPrevious target: %5u\n", ind, "", prev_cpu); |
| 559 | } |
| 560 | } |
| 561 | #endif |
| 562 | |
Thomas Gleixner | eb18cf5 | 2015-05-05 11:10:11 +0200 | [diff] [blame] | 563 | static const struct irq_domain_ops x86_vector_domain_ops = { |
Thomas Gleixner | 65d7ed5 | 2017-09-13 23:29:39 +0200 | [diff] [blame] | 564 | .alloc = x86_vector_alloc_irqs, |
| 565 | .free = x86_vector_free_irqs, |
Thomas Gleixner | 2db1f95 | 2017-09-13 23:29:50 +0200 | [diff] [blame] | 566 | .activate = x86_vector_activate, |
| 567 | .deactivate = x86_vector_deactivate, |
Thomas Gleixner | 65d7ed5 | 2017-09-13 23:29:39 +0200 | [diff] [blame] | 568 | #ifdef CONFIG_GENERIC_IRQ_DEBUGFS |
| 569 | .debug_show = x86_vector_debug_show, |
| 570 | #endif |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 571 | }; |
| 572 | |
Jiang Liu | 11d686e | 2014-10-27 16:12:05 +0800 | [diff] [blame] | 573 | int __init arch_probe_nr_irqs(void) |
| 574 | { |
| 575 | int nr; |
| 576 | |
| 577 | if (nr_irqs > (NR_VECTORS * nr_cpu_ids)) |
| 578 | nr_irqs = NR_VECTORS * nr_cpu_ids; |
| 579 | |
| 580 | nr = (gsi_top + nr_legacy_irqs()) + 8 * nr_cpu_ids; |
| 581 | #if defined(CONFIG_PCI_MSI) || defined(CONFIG_HT_IRQ) |
| 582 | /* |
| 583 | * for MSI and HT dyn irq |
| 584 | */ |
| 585 | if (gsi_top <= NR_IRQS_LEGACY) |
| 586 | nr += 8 * nr_cpu_ids; |
| 587 | else |
| 588 | nr += gsi_top * 16; |
| 589 | #endif |
| 590 | if (nr < nr_irqs) |
| 591 | nr_irqs = nr; |
| 592 | |
Vitaly Kuznetsov | 8c058b0 | 2015-11-03 10:40:14 +0100 | [diff] [blame] | 593 | /* |
| 594 | * We don't know if PIC is present at this point so we need to do |
| 595 | * probe() to get the right number of legacy IRQs. |
| 596 | */ |
| 597 | return legacy_pic->probe(); |
Jiang Liu | 11d686e | 2014-10-27 16:12:05 +0800 | [diff] [blame] | 598 | } |
| 599 | |
Thomas Gleixner | 0fa115d | 2017-09-13 23:29:38 +0200 | [diff] [blame] | 600 | void lapic_assign_legacy_vector(unsigned int irq, bool replace) |
| 601 | { |
| 602 | /* |
| 603 | * Use assign system here so it wont get accounted as allocated |
| 604 | * and moveable in the cpu hotplug check and it prevents managed |
| 605 | * irq reservation from touching it. |
| 606 | */ |
| 607 | irq_matrix_assign_system(vector_matrix, ISA_IRQ_VECTOR(irq), replace); |
| 608 | } |
| 609 | |
| 610 | void __init lapic_assign_system_vectors(void) |
| 611 | { |
| 612 | unsigned int i, vector = 0; |
| 613 | |
| 614 | for_each_set_bit_from(vector, system_vectors, NR_VECTORS) |
| 615 | irq_matrix_assign_system(vector_matrix, vector, false); |
| 616 | |
| 617 | if (nr_legacy_irqs() > 1) |
| 618 | lapic_assign_legacy_vector(PIC_CASCADE_IR, false); |
| 619 | |
| 620 | /* System vectors are reserved, online it */ |
| 621 | irq_matrix_online(vector_matrix); |
| 622 | |
| 623 | /* Mark the preallocated legacy interrupts */ |
| 624 | for (i = 0; i < nr_legacy_irqs(); i++) { |
| 625 | if (i != PIC_CASCADE_IR) |
| 626 | irq_matrix_assign(vector_matrix, ISA_IRQ_VECTOR(i)); |
| 627 | } |
| 628 | } |
| 629 | |
Jiang Liu | 11d686e | 2014-10-27 16:12:05 +0800 | [diff] [blame] | 630 | int __init arch_early_irq_init(void) |
| 631 | { |
Thomas Gleixner | 9d35f85 | 2017-06-20 01:37:06 +0200 | [diff] [blame] | 632 | struct fwnode_handle *fn; |
| 633 | |
Thomas Gleixner | 9d35f85 | 2017-06-20 01:37:06 +0200 | [diff] [blame] | 634 | fn = irq_domain_alloc_named_fwnode("VECTOR"); |
| 635 | BUG_ON(!fn); |
| 636 | x86_vector_domain = irq_domain_create_tree(fn, &x86_vector_domain_ops, |
| 637 | NULL); |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 638 | BUG_ON(x86_vector_domain == NULL); |
Thomas Gleixner | 9d35f85 | 2017-06-20 01:37:06 +0200 | [diff] [blame] | 639 | irq_domain_free_fwnode(fn); |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 640 | irq_set_default_host(x86_vector_domain); |
| 641 | |
Jiang Liu | 52f518a | 2015-04-13 14:11:35 +0800 | [diff] [blame] | 642 | arch_init_msi_domain(x86_vector_domain); |
Jiang Liu | 49e07d8 | 2015-04-13 14:11:43 +0800 | [diff] [blame] | 643 | arch_init_htirq_domain(x86_vector_domain); |
Jiang Liu | 52f518a | 2015-04-13 14:11:35 +0800 | [diff] [blame] | 644 | |
Thomas Gleixner | 3716fd2 | 2015-12-31 16:30:48 +0000 | [diff] [blame] | 645 | BUG_ON(!alloc_cpumask_var(&vector_searchmask, GFP_KERNEL)); |
Jiang Liu | f7fa7ae | 2015-04-14 10:30:10 +0800 | [diff] [blame] | 646 | |
Thomas Gleixner | 0fa115d | 2017-09-13 23:29:38 +0200 | [diff] [blame] | 647 | /* |
| 648 | * Allocate the vector matrix allocator data structure and limit the |
| 649 | * search area. |
| 650 | */ |
| 651 | vector_matrix = irq_alloc_matrix(NR_VECTORS, FIRST_EXTERNAL_VECTOR, |
| 652 | FIRST_SYSTEM_VECTOR); |
| 653 | BUG_ON(!vector_matrix); |
| 654 | |
Jiang Liu | 11d686e | 2014-10-27 16:12:05 +0800 | [diff] [blame] | 655 | return arch_early_ioapic_init(); |
| 656 | } |
| 657 | |
Thomas Gleixner | ba80164 | 2017-09-13 23:29:44 +0200 | [diff] [blame] | 658 | #ifdef CONFIG_SMP |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 659 | |
Thomas Gleixner | f0cc6cc | 2017-09-13 23:29:29 +0200 | [diff] [blame] | 660 | static struct irq_desc *__setup_vector_irq(int vector) |
| 661 | { |
| 662 | int isairq = vector - ISA_IRQ_VECTOR(0); |
| 663 | |
| 664 | /* Check whether the irq is in the legacy space */ |
| 665 | if (isairq < 0 || isairq >= nr_legacy_irqs()) |
| 666 | return VECTOR_UNUSED; |
| 667 | /* Check whether the irq is handled by the IOAPIC */ |
| 668 | if (test_bit(isairq, &io_apic_irqs)) |
| 669 | return VECTOR_UNUSED; |
| 670 | return irq_to_desc(isairq); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 671 | } |
| 672 | |
Thomas Gleixner | 0fa115d | 2017-09-13 23:29:38 +0200 | [diff] [blame] | 673 | /* Online the local APIC infrastructure and initialize the vectors */ |
| 674 | void lapic_online(void) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 675 | { |
Thomas Gleixner | f0cc6cc | 2017-09-13 23:29:29 +0200 | [diff] [blame] | 676 | unsigned int vector; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 677 | |
Thomas Gleixner | 5a3f75e | 2015-07-05 17:12:32 +0000 | [diff] [blame] | 678 | lockdep_assert_held(&vector_lock); |
Thomas Gleixner | 0fa115d | 2017-09-13 23:29:38 +0200 | [diff] [blame] | 679 | |
| 680 | /* Online the vector matrix array for this CPU */ |
| 681 | irq_matrix_online(vector_matrix); |
| 682 | |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 683 | /* |
Thomas Gleixner | f0cc6cc | 2017-09-13 23:29:29 +0200 | [diff] [blame] | 684 | * The interrupt affinity logic never targets interrupts to offline |
| 685 | * CPUs. The exception are the legacy PIC interrupts. In general |
| 686 | * they are only targeted to CPU0, but depending on the platform |
| 687 | * they can be distributed to any online CPU in hardware. The |
| 688 | * kernel has no influence on that. So all active legacy vectors |
| 689 | * must be installed on all CPUs. All non legacy interrupts can be |
| 690 | * cleared. |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 691 | */ |
Thomas Gleixner | f0cc6cc | 2017-09-13 23:29:29 +0200 | [diff] [blame] | 692 | for (vector = 0; vector < NR_VECTORS; vector++) |
| 693 | this_cpu_write(vector_irq[vector], __setup_vector_irq(vector)); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 694 | } |
| 695 | |
Thomas Gleixner | 0fa115d | 2017-09-13 23:29:38 +0200 | [diff] [blame] | 696 | void lapic_offline(void) |
| 697 | { |
| 698 | lock_vector_lock(); |
| 699 | irq_matrix_offline(vector_matrix); |
| 700 | unlock_vector_lock(); |
| 701 | } |
| 702 | |
Thomas Gleixner | ba80164 | 2017-09-13 23:29:44 +0200 | [diff] [blame] | 703 | static int apic_set_affinity(struct irq_data *irqd, |
| 704 | const struct cpumask *dest, bool force) |
| 705 | { |
| 706 | int err; |
| 707 | |
Thomas Gleixner | 2db1f95 | 2017-09-13 23:29:50 +0200 | [diff] [blame] | 708 | raw_spin_lock(&vector_lock); |
| 709 | cpumask_and(vector_searchmask, dest, cpu_online_mask); |
| 710 | if (irqd_affinity_is_managed(irqd)) |
| 711 | err = assign_managed_vector(irqd, vector_searchmask); |
| 712 | else |
| 713 | err = assign_vector_locked(irqd, vector_searchmask); |
| 714 | raw_spin_unlock(&vector_lock); |
Thomas Gleixner | ba80164 | 2017-09-13 23:29:44 +0200 | [diff] [blame] | 715 | return err ? err : IRQ_SET_MASK_OK; |
| 716 | } |
| 717 | |
| 718 | #else |
| 719 | # define apic_set_affinity NULL |
| 720 | #endif |
| 721 | |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 722 | static int apic_retrigger_irq(struct irq_data *irqd) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 723 | { |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 724 | struct apic_chip_data *apicd = apic_chip_data(irqd); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 725 | unsigned long flags; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 726 | |
| 727 | raw_spin_lock_irqsave(&vector_lock, flags); |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 728 | apic->send_IPI(apicd->cpu, apicd->vector); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 729 | raw_spin_unlock_irqrestore(&vector_lock, flags); |
| 730 | |
| 731 | return 1; |
| 732 | } |
| 733 | |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 734 | void apic_ack_edge(struct irq_data *irqd) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 735 | { |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 736 | irq_complete_move(irqd_cfg(irqd)); |
| 737 | irq_move_irq(irqd); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 738 | ack_APIC_irq(); |
| 739 | } |
| 740 | |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 741 | static struct irq_chip lapic_controller = { |
Thomas Gleixner | 8947dfb | 2017-06-20 01:37:01 +0200 | [diff] [blame] | 742 | .name = "APIC", |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 743 | .irq_ack = apic_ack_edge, |
Jiang Liu | 68f9f44 | 2015-04-14 10:30:01 +0800 | [diff] [blame] | 744 | .irq_set_affinity = apic_set_affinity, |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 745 | .irq_retrigger = apic_retrigger_irq, |
| 746 | }; |
| 747 | |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 748 | #ifdef CONFIG_SMP |
Thomas Gleixner | dccfe31 | 2017-09-13 23:29:32 +0200 | [diff] [blame] | 749 | |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 750 | static void free_moved_vector(struct apic_chip_data *apicd) |
| 751 | { |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 752 | unsigned int vector = apicd->prev_vector; |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 753 | unsigned int cpu = apicd->prev_cpu; |
Thomas Gleixner | 2db1f95 | 2017-09-13 23:29:50 +0200 | [diff] [blame] | 754 | bool managed = apicd->is_managed; |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 755 | |
Thomas Gleixner | 2db1f95 | 2017-09-13 23:29:50 +0200 | [diff] [blame] | 756 | /* |
| 757 | * This should never happen. Managed interrupts are not |
| 758 | * migrated except on CPU down, which does not involve the |
| 759 | * cleanup vector. But try to keep the accounting correct |
| 760 | * nevertheless. |
| 761 | */ |
| 762 | WARN_ON_ONCE(managed); |
| 763 | |
| 764 | trace_vector_free_moved(apicd->irq, vector, managed); |
| 765 | irq_matrix_free(vector_matrix, cpu, vector, managed); |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 766 | __this_cpu_write(vector_irq[vector], VECTOR_UNUSED); |
| 767 | hlist_del_init(&apicd->clist); |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 768 | apicd->prev_vector = 0; |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 769 | apicd->move_in_progress = 0; |
| 770 | } |
| 771 | |
Thomas Gleixner | dccfe31 | 2017-09-13 23:29:32 +0200 | [diff] [blame] | 772 | asmlinkage __visible void __irq_entry smp_irq_move_cleanup_interrupt(void) |
| 773 | { |
| 774 | struct hlist_head *clhead = this_cpu_ptr(&cleanup_list); |
| 775 | struct apic_chip_data *apicd; |
| 776 | struct hlist_node *tmp; |
| 777 | |
| 778 | entering_ack_irq(); |
| 779 | /* Prevent vectors vanishing under us */ |
| 780 | raw_spin_lock(&vector_lock); |
| 781 | |
| 782 | hlist_for_each_entry_safe(apicd, tmp, clhead, clist) { |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 783 | unsigned int irr, vector = apicd->prev_vector; |
Thomas Gleixner | dccfe31 | 2017-09-13 23:29:32 +0200 | [diff] [blame] | 784 | |
| 785 | /* |
| 786 | * Paranoia: Check if the vector that needs to be cleaned |
| 787 | * up is registered at the APICs IRR. If so, then this is |
| 788 | * not the best time to clean it up. Clean it up in the |
| 789 | * next attempt by sending another IRQ_MOVE_CLEANUP_VECTOR |
| 790 | * to this CPU. IRQ_MOVE_CLEANUP_VECTOR is the lowest |
| 791 | * priority external vector, so on return from this |
| 792 | * interrupt the device interrupt will happen first. |
| 793 | */ |
| 794 | irr = apic_read(APIC_IRR + (vector / 32 * 0x10)); |
| 795 | if (irr & (1U << (vector % 32))) { |
| 796 | apic->send_IPI_self(IRQ_MOVE_CLEANUP_VECTOR); |
| 797 | continue; |
| 798 | } |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 799 | free_moved_vector(apicd); |
Thomas Gleixner | dccfe31 | 2017-09-13 23:29:32 +0200 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | raw_spin_unlock(&vector_lock); |
| 803 | exiting_irq(); |
| 804 | } |
| 805 | |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 806 | static void __send_cleanup_vector(struct apic_chip_data *apicd) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 807 | { |
Thomas Gleixner | dccfe31 | 2017-09-13 23:29:32 +0200 | [diff] [blame] | 808 | unsigned int cpu; |
| 809 | |
Thomas Gleixner | c1684f5 | 2015-12-31 16:30:51 +0000 | [diff] [blame] | 810 | raw_spin_lock(&vector_lock); |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 811 | apicd->move_in_progress = 0; |
Thomas Gleixner | dccfe31 | 2017-09-13 23:29:32 +0200 | [diff] [blame] | 812 | cpu = apicd->prev_cpu; |
| 813 | if (cpu_online(cpu)) { |
| 814 | hlist_add_head(&apicd->clist, per_cpu_ptr(&cleanup_list, cpu)); |
| 815 | apic->send_IPI(cpu, IRQ_MOVE_CLEANUP_VECTOR); |
| 816 | } else { |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 817 | apicd->prev_vector = 0; |
Thomas Gleixner | dccfe31 | 2017-09-13 23:29:32 +0200 | [diff] [blame] | 818 | } |
Thomas Gleixner | c1684f5 | 2015-12-31 16:30:51 +0000 | [diff] [blame] | 819 | raw_spin_unlock(&vector_lock); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 820 | } |
| 821 | |
Jiang Liu | c6c2002 | 2015-04-14 10:30:02 +0800 | [diff] [blame] | 822 | void send_cleanup_vector(struct irq_cfg *cfg) |
| 823 | { |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 824 | struct apic_chip_data *apicd; |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 825 | |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 826 | apicd = container_of(cfg, struct apic_chip_data, hw_irq_cfg); |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 827 | if (apicd->move_in_progress) |
| 828 | __send_cleanup_vector(apicd); |
Jiang Liu | c6c2002 | 2015-04-14 10:30:02 +0800 | [diff] [blame] | 829 | } |
| 830 | |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 831 | static void __irq_complete_move(struct irq_cfg *cfg, unsigned vector) |
| 832 | { |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 833 | struct apic_chip_data *apicd; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 834 | |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 835 | apicd = container_of(cfg, struct apic_chip_data, hw_irq_cfg); |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 836 | if (likely(!apicd->move_in_progress)) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 837 | return; |
| 838 | |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 839 | if (vector == apicd->vector && apicd->cpu == smp_processor_id()) |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 840 | __send_cleanup_vector(apicd); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | void irq_complete_move(struct irq_cfg *cfg) |
| 844 | { |
| 845 | __irq_complete_move(cfg, ~get_irq_regs()->orig_ax); |
| 846 | } |
| 847 | |
Thomas Gleixner | 90a2282 | 2015-12-31 16:30:53 +0000 | [diff] [blame] | 848 | /* |
Thomas Gleixner | 551adc6 | 2016-03-14 09:40:46 +0100 | [diff] [blame] | 849 | * Called from fixup_irqs() with @desc->lock held and interrupts disabled. |
Thomas Gleixner | 90a2282 | 2015-12-31 16:30:53 +0000 | [diff] [blame] | 850 | */ |
| 851 | void irq_force_complete_move(struct irq_desc *desc) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 852 | { |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 853 | struct apic_chip_data *apicd; |
Thomas Gleixner | dccfe31 | 2017-09-13 23:29:32 +0200 | [diff] [blame] | 854 | struct irq_data *irqd; |
| 855 | unsigned int vector; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 856 | |
Mika Westerberg | db91aa7 | 2016-10-03 13:17:08 +0300 | [diff] [blame] | 857 | /* |
| 858 | * The function is called for all descriptors regardless of which |
| 859 | * irqdomain they belong to. For example if an IRQ is provided by |
| 860 | * an irq_chip as part of a GPIO driver, the chip data for that |
| 861 | * descriptor is specific to the irq_chip in question. |
| 862 | * |
| 863 | * Check first that the chip_data is what we expect |
| 864 | * (apic_chip_data) before touching it any further. |
| 865 | */ |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 866 | irqd = irq_domain_get_irq_data(x86_vector_domain, |
Thomas Gleixner | dccfe31 | 2017-09-13 23:29:32 +0200 | [diff] [blame] | 867 | irq_desc_get_irq(desc)); |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 868 | if (!irqd) |
Mika Westerberg | db91aa7 | 2016-10-03 13:17:08 +0300 | [diff] [blame] | 869 | return; |
| 870 | |
Thomas Gleixner | dccfe31 | 2017-09-13 23:29:32 +0200 | [diff] [blame] | 871 | raw_spin_lock(&vector_lock); |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 872 | apicd = apic_chip_data(irqd); |
Thomas Gleixner | dccfe31 | 2017-09-13 23:29:32 +0200 | [diff] [blame] | 873 | if (!apicd) |
| 874 | goto unlock; |
Thomas Gleixner | 56d7d2f | 2015-12-31 16:30:52 +0000 | [diff] [blame] | 875 | |
Thomas Gleixner | 56d7d2f | 2015-12-31 16:30:52 +0000 | [diff] [blame] | 876 | /* |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 877 | * If prev_vector is empty, no action required. |
Thomas Gleixner | dccfe31 | 2017-09-13 23:29:32 +0200 | [diff] [blame] | 878 | */ |
Thomas Gleixner | ba224fe | 2017-09-13 23:29:45 +0200 | [diff] [blame] | 879 | vector = apicd->prev_vector; |
Thomas Gleixner | dccfe31 | 2017-09-13 23:29:32 +0200 | [diff] [blame] | 880 | if (!vector) |
| 881 | goto unlock; |
| 882 | |
| 883 | /* |
| 884 | * This is tricky. If the cleanup of the old vector has not been |
Thomas Gleixner | 98229aa | 2015-12-31 16:30:54 +0000 | [diff] [blame] | 885 | * done yet, then the following setaffinity call will fail with |
| 886 | * -EBUSY. This can leave the interrupt in a stale state. |
| 887 | * |
Thomas Gleixner | 551adc6 | 2016-03-14 09:40:46 +0100 | [diff] [blame] | 888 | * All CPUs are stuck in stop machine with interrupts disabled so |
| 889 | * calling __irq_complete_move() would be completely pointless. |
Thomas Gleixner | dccfe31 | 2017-09-13 23:29:32 +0200 | [diff] [blame] | 890 | * |
Thomas Gleixner | 551adc6 | 2016-03-14 09:40:46 +0100 | [diff] [blame] | 891 | * 1) The interrupt is in move_in_progress state. That means that we |
| 892 | * have not seen an interrupt since the io_apic was reprogrammed to |
| 893 | * the new vector. |
| 894 | * |
| 895 | * 2) The interrupt has fired on the new vector, but the cleanup IPIs |
| 896 | * have not been processed yet. |
| 897 | */ |
Thomas Gleixner | 86ba655 | 2017-09-13 23:29:30 +0200 | [diff] [blame] | 898 | if (apicd->move_in_progress) { |
Thomas Gleixner | 551adc6 | 2016-03-14 09:40:46 +0100 | [diff] [blame] | 899 | /* |
| 900 | * In theory there is a race: |
| 901 | * |
| 902 | * set_ioapic(new_vector) <-- Interrupt is raised before update |
| 903 | * is effective, i.e. it's raised on |
| 904 | * the old vector. |
| 905 | * |
| 906 | * So if the target cpu cannot handle that interrupt before |
| 907 | * the old vector is cleaned up, we get a spurious interrupt |
| 908 | * and in the worst case the ioapic irq line becomes stale. |
| 909 | * |
| 910 | * But in case of cpu hotplug this should be a non issue |
| 911 | * because if the affinity update happens right before all |
| 912 | * cpus rendevouz in stop machine, there is no way that the |
| 913 | * interrupt can be blocked on the target cpu because all cpus |
| 914 | * loops first with interrupts enabled in stop machine, so the |
| 915 | * old vector is not yet cleaned up when the interrupt fires. |
| 916 | * |
| 917 | * So the only way to run into this issue is if the delivery |
| 918 | * of the interrupt on the apic/system bus would be delayed |
| 919 | * beyond the point where the target cpu disables interrupts |
| 920 | * in stop machine. I doubt that it can happen, but at least |
| 921 | * there is a theroretical chance. Virtualization might be |
| 922 | * able to expose this, but AFAICT the IOAPIC emulation is not |
| 923 | * as stupid as the real hardware. |
| 924 | * |
| 925 | * Anyway, there is nothing we can do about that at this point |
| 926 | * w/o refactoring the whole fixup_irq() business completely. |
| 927 | * We print at least the irq number and the old vector number, |
| 928 | * so we have the necessary information when a problem in that |
| 929 | * area arises. |
| 930 | */ |
| 931 | pr_warn("IRQ fixup: irq %d move in progress, old vector %d\n", |
Thomas Gleixner | dccfe31 | 2017-09-13 23:29:32 +0200 | [diff] [blame] | 932 | irqd->irq, vector); |
Thomas Gleixner | 551adc6 | 2016-03-14 09:40:46 +0100 | [diff] [blame] | 933 | } |
Thomas Gleixner | 69cde00 | 2017-09-13 23:29:42 +0200 | [diff] [blame] | 934 | free_moved_vector(apicd); |
Thomas Gleixner | dccfe31 | 2017-09-13 23:29:32 +0200 | [diff] [blame] | 935 | unlock: |
Thomas Gleixner | 56d7d2f | 2015-12-31 16:30:52 +0000 | [diff] [blame] | 936 | raw_spin_unlock(&vector_lock); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 937 | } |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 938 | #endif |
| 939 | |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 940 | static void __init print_APIC_field(int base) |
| 941 | { |
| 942 | int i; |
| 943 | |
| 944 | printk(KERN_DEBUG); |
| 945 | |
| 946 | for (i = 0; i < 8; i++) |
| 947 | pr_cont("%08x", apic_read(base + i*0x10)); |
| 948 | |
| 949 | pr_cont("\n"); |
| 950 | } |
| 951 | |
| 952 | static void __init print_local_APIC(void *dummy) |
| 953 | { |
| 954 | unsigned int i, v, ver, maxlvt; |
| 955 | u64 icr; |
| 956 | |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 957 | pr_debug("printing local APIC contents on CPU#%d/%d:\n", |
| 958 | smp_processor_id(), hard_smp_processor_id()); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 959 | v = apic_read(APIC_ID); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 960 | pr_info("... APIC ID: %08x (%01x)\n", v, read_apic_id()); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 961 | v = apic_read(APIC_LVR); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 962 | pr_info("... APIC VERSION: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 963 | ver = GET_APIC_VERSION(v); |
| 964 | maxlvt = lapic_get_maxlvt(); |
| 965 | |
| 966 | v = apic_read(APIC_TASKPRI); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 967 | pr_debug("... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 968 | |
| 969 | /* !82489DX */ |
| 970 | if (APIC_INTEGRATED(ver)) { |
| 971 | if (!APIC_XAPIC(ver)) { |
| 972 | v = apic_read(APIC_ARBPRI); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 973 | pr_debug("... APIC ARBPRI: %08x (%02x)\n", |
| 974 | v, v & APIC_ARBPRI_MASK); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 975 | } |
| 976 | v = apic_read(APIC_PROCPRI); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 977 | pr_debug("... APIC PROCPRI: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | /* |
| 981 | * Remote read supported only in the 82489DX and local APIC for |
| 982 | * Pentium processors. |
| 983 | */ |
| 984 | if (!APIC_INTEGRATED(ver) || maxlvt == 3) { |
| 985 | v = apic_read(APIC_RRR); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 986 | pr_debug("... APIC RRR: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | v = apic_read(APIC_LDR); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 990 | pr_debug("... APIC LDR: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 991 | if (!x2apic_enabled()) { |
| 992 | v = apic_read(APIC_DFR); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 993 | pr_debug("... APIC DFR: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 994 | } |
| 995 | v = apic_read(APIC_SPIV); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 996 | pr_debug("... APIC SPIV: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 997 | |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 998 | pr_debug("... APIC ISR field:\n"); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 999 | print_APIC_field(APIC_ISR); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 1000 | pr_debug("... APIC TMR field:\n"); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 1001 | print_APIC_field(APIC_TMR); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 1002 | pr_debug("... APIC IRR field:\n"); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 1003 | print_APIC_field(APIC_IRR); |
| 1004 | |
| 1005 | /* !82489DX */ |
| 1006 | if (APIC_INTEGRATED(ver)) { |
| 1007 | /* Due to the Pentium erratum 3AP. */ |
| 1008 | if (maxlvt > 3) |
| 1009 | apic_write(APIC_ESR, 0); |
| 1010 | |
| 1011 | v = apic_read(APIC_ESR); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 1012 | pr_debug("... APIC ESR: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | icr = apic_icr_read(); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 1016 | pr_debug("... APIC ICR: %08x\n", (u32)icr); |
| 1017 | pr_debug("... APIC ICR2: %08x\n", (u32)(icr >> 32)); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 1018 | |
| 1019 | v = apic_read(APIC_LVTT); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 1020 | pr_debug("... APIC LVTT: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 1021 | |
| 1022 | if (maxlvt > 3) { |
| 1023 | /* PC is LVT#4. */ |
| 1024 | v = apic_read(APIC_LVTPC); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 1025 | pr_debug("... APIC LVTPC: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 1026 | } |
| 1027 | v = apic_read(APIC_LVT0); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 1028 | pr_debug("... APIC LVT0: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 1029 | v = apic_read(APIC_LVT1); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 1030 | pr_debug("... APIC LVT1: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 1031 | |
| 1032 | if (maxlvt > 2) { |
| 1033 | /* ERR is LVT#3. */ |
| 1034 | v = apic_read(APIC_LVTERR); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 1035 | pr_debug("... APIC LVTERR: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | v = apic_read(APIC_TMICT); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 1039 | pr_debug("... APIC TMICT: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 1040 | v = apic_read(APIC_TMCCT); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 1041 | pr_debug("... APIC TMCCT: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 1042 | v = apic_read(APIC_TDCR); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 1043 | pr_debug("... APIC TDCR: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 1044 | |
| 1045 | if (boot_cpu_has(X86_FEATURE_EXTAPIC)) { |
| 1046 | v = apic_read(APIC_EFEAT); |
| 1047 | maxlvt = (v >> 16) & 0xff; |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 1048 | pr_debug("... APIC EFEAT: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 1049 | v = apic_read(APIC_ECTRL); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 1050 | pr_debug("... APIC ECTRL: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 1051 | for (i = 0; i < maxlvt; i++) { |
| 1052 | v = apic_read(APIC_EILVTn(i)); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 1053 | pr_debug("... APIC EILVT%d: %08x\n", i, v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 1054 | } |
| 1055 | } |
| 1056 | pr_cont("\n"); |
| 1057 | } |
| 1058 | |
| 1059 | static void __init print_local_APICs(int maxcpu) |
| 1060 | { |
| 1061 | int cpu; |
| 1062 | |
| 1063 | if (!maxcpu) |
| 1064 | return; |
| 1065 | |
| 1066 | preempt_disable(); |
| 1067 | for_each_online_cpu(cpu) { |
| 1068 | if (cpu >= maxcpu) |
| 1069 | break; |
| 1070 | smp_call_function_single(cpu, print_local_APIC, NULL, 1); |
| 1071 | } |
| 1072 | preempt_enable(); |
| 1073 | } |
| 1074 | |
| 1075 | static void __init print_PIC(void) |
| 1076 | { |
| 1077 | unsigned int v; |
| 1078 | unsigned long flags; |
| 1079 | |
| 1080 | if (!nr_legacy_irqs()) |
| 1081 | return; |
| 1082 | |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 1083 | pr_debug("\nprinting PIC contents\n"); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 1084 | |
| 1085 | raw_spin_lock_irqsave(&i8259A_lock, flags); |
| 1086 | |
| 1087 | v = inb(0xa1) << 8 | inb(0x21); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 1088 | pr_debug("... PIC IMR: %04x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 1089 | |
| 1090 | v = inb(0xa0) << 8 | inb(0x20); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 1091 | pr_debug("... PIC IRR: %04x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 1092 | |
| 1093 | outb(0x0b, 0xa0); |
| 1094 | outb(0x0b, 0x20); |
| 1095 | v = inb(0xa0) << 8 | inb(0x20); |
| 1096 | outb(0x0a, 0xa0); |
| 1097 | outb(0x0a, 0x20); |
| 1098 | |
| 1099 | raw_spin_unlock_irqrestore(&i8259A_lock, flags); |
| 1100 | |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 1101 | pr_debug("... PIC ISR: %04x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 1102 | |
| 1103 | v = inb(0x4d1) << 8 | inb(0x4d0); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 1104 | pr_debug("... PIC ELCR: %04x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | static int show_lapic __initdata = 1; |
| 1108 | static __init int setup_show_lapic(char *arg) |
| 1109 | { |
| 1110 | int num = -1; |
| 1111 | |
| 1112 | if (strcmp(arg, "all") == 0) { |
| 1113 | show_lapic = CONFIG_NR_CPUS; |
| 1114 | } else { |
| 1115 | get_option(&arg, &num); |
| 1116 | if (num >= 0) |
| 1117 | show_lapic = num; |
| 1118 | } |
| 1119 | |
| 1120 | return 1; |
| 1121 | } |
| 1122 | __setup("show_lapic=", setup_show_lapic); |
| 1123 | |
| 1124 | static int __init print_ICs(void) |
| 1125 | { |
| 1126 | if (apic_verbosity == APIC_QUIET) |
| 1127 | return 0; |
| 1128 | |
| 1129 | print_PIC(); |
| 1130 | |
| 1131 | /* don't print out if apic is not there */ |
Borislav Petkov | 93984fb | 2016-04-04 22:25:00 +0200 | [diff] [blame] | 1132 | if (!boot_cpu_has(X86_FEATURE_APIC) && !apic_from_smp_config()) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 1133 | return 0; |
| 1134 | |
| 1135 | print_local_APICs(show_lapic); |
| 1136 | print_IO_APICs(); |
| 1137 | |
| 1138 | return 0; |
| 1139 | } |
| 1140 | |
| 1141 | late_initcall(print_ICs); |