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> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/compiler.h> |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 16 | #include <linux/slab.h> |
Jiang Liu | d746d1e | 2015-04-14 10:30:09 +0800 | [diff] [blame] | 17 | #include <asm/irqdomain.h> |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 18 | #include <asm/hw_irq.h> |
| 19 | #include <asm/apic.h> |
| 20 | #include <asm/i8259.h> |
| 21 | #include <asm/desc.h> |
| 22 | #include <asm/irq_remapping.h> |
| 23 | |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 24 | struct apic_chip_data { |
| 25 | struct irq_cfg cfg; |
| 26 | cpumask_var_t domain; |
| 27 | cpumask_var_t old_domain; |
| 28 | u8 move_in_progress : 1; |
| 29 | }; |
| 30 | |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 31 | struct irq_domain *x86_vector_domain; |
Jake Oshins | c8f3e51 | 2015-12-10 17:52:59 +0000 | [diff] [blame] | 32 | EXPORT_SYMBOL_GPL(x86_vector_domain); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 33 | static DEFINE_RAW_SPINLOCK(vector_lock); |
Thomas Gleixner | 3716fd2 | 2015-12-31 16:30:48 +0000 | [diff] [blame^] | 34 | static cpumask_var_t vector_cpumask, vector_searchmask, searched_cpumask; |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 35 | static struct irq_chip lapic_controller; |
Jiang Liu | 1331532 | 2015-04-13 14:11:56 +0800 | [diff] [blame] | 36 | #ifdef CONFIG_X86_IO_APIC |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 37 | static struct apic_chip_data *legacy_irq_data[NR_IRQS_LEGACY]; |
Jiang Liu | 1331532 | 2015-04-13 14:11:56 +0800 | [diff] [blame] | 38 | #endif |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 39 | |
| 40 | void lock_vector_lock(void) |
| 41 | { |
| 42 | /* Used to the online set of cpus does not change |
| 43 | * during assign_irq_vector. |
| 44 | */ |
| 45 | raw_spin_lock(&vector_lock); |
| 46 | } |
| 47 | |
| 48 | void unlock_vector_lock(void) |
| 49 | { |
| 50 | raw_spin_unlock(&vector_lock); |
| 51 | } |
| 52 | |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 53 | static struct apic_chip_data *apic_chip_data(struct irq_data *irq_data) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 54 | { |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 55 | if (!irq_data) |
| 56 | return NULL; |
| 57 | |
| 58 | while (irq_data->parent_data) |
| 59 | irq_data = irq_data->parent_data; |
| 60 | |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 61 | return irq_data->chip_data; |
| 62 | } |
| 63 | |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 64 | struct irq_cfg *irqd_cfg(struct irq_data *irq_data) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 65 | { |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 66 | struct apic_chip_data *data = apic_chip_data(irq_data); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 67 | |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 68 | return data ? &data->cfg : NULL; |
| 69 | } |
Jake Oshins | c8f3e51 | 2015-12-10 17:52:59 +0000 | [diff] [blame] | 70 | EXPORT_SYMBOL_GPL(irqd_cfg); |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 71 | |
| 72 | struct irq_cfg *irq_cfg(unsigned int irq) |
| 73 | { |
| 74 | return irqd_cfg(irq_get_irq_data(irq)); |
| 75 | } |
| 76 | |
| 77 | static struct apic_chip_data *alloc_apic_chip_data(int node) |
| 78 | { |
| 79 | struct apic_chip_data *data; |
| 80 | |
| 81 | data = kzalloc_node(sizeof(*data), GFP_KERNEL, node); |
| 82 | if (!data) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 83 | return NULL; |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 84 | if (!zalloc_cpumask_var_node(&data->domain, GFP_KERNEL, node)) |
| 85 | goto out_data; |
| 86 | if (!zalloc_cpumask_var_node(&data->old_domain, GFP_KERNEL, node)) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 87 | goto out_domain; |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 88 | return data; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 89 | out_domain: |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 90 | free_cpumask_var(data->domain); |
| 91 | out_data: |
| 92 | kfree(data); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 93 | return NULL; |
| 94 | } |
| 95 | |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 96 | static void free_apic_chip_data(struct apic_chip_data *data) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 97 | { |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 98 | if (data) { |
| 99 | free_cpumask_var(data->domain); |
| 100 | free_cpumask_var(data->old_domain); |
| 101 | kfree(data); |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 102 | } |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 103 | } |
| 104 | |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 105 | static int __assign_irq_vector(int irq, struct apic_chip_data *d, |
| 106 | const struct cpumask *mask) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 107 | { |
| 108 | /* |
| 109 | * NOTE! The local APIC isn't very good at handling |
| 110 | * multiple interrupts at the same interrupt level. |
| 111 | * As the interrupt level is determined by taking the |
| 112 | * vector number and shifting that right by 4, we |
| 113 | * want to spread these out a bit so that they don't |
| 114 | * all fall in the same interrupt level. |
| 115 | * |
| 116 | * Also, we've got to be careful not to trash gate |
| 117 | * 0x80, because int 0x80 is hm, kind of importantish. ;) |
| 118 | */ |
| 119 | static int current_vector = FIRST_EXTERNAL_VECTOR + VECTOR_OFFSET_START; |
| 120 | static int current_offset = VECTOR_OFFSET_START % 16; |
Thomas Gleixner | 433cbd5 | 2015-12-31 16:30:46 +0000 | [diff] [blame] | 121 | int cpu; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 122 | |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 123 | if (d->move_in_progress) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 124 | return -EBUSY; |
| 125 | |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 126 | /* Only try and allocate irqs on cpus that are present */ |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 127 | cpumask_clear(d->old_domain); |
Jiang Liu | 8a580f7 | 2015-12-31 16:30:46 +0000 | [diff] [blame] | 128 | cpumask_clear(searched_cpumask); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 129 | cpu = cpumask_first_and(mask, cpu_online_mask); |
| 130 | while (cpu < nr_cpu_ids) { |
| 131 | int new_cpu, vector, offset; |
| 132 | |
Thomas Gleixner | 3716fd2 | 2015-12-31 16:30:48 +0000 | [diff] [blame^] | 133 | /* Get the possible target cpus for @mask/@cpu from the apic */ |
Jiang Liu | f7fa7ae | 2015-04-14 10:30:10 +0800 | [diff] [blame] | 134 | apic->vector_allocation_domain(cpu, vector_cpumask, mask); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 135 | |
Thomas Gleixner | 3716fd2 | 2015-12-31 16:30:48 +0000 | [diff] [blame^] | 136 | /* |
| 137 | * Clear the offline cpus from @vector_cpumask for searching |
| 138 | * and verify whether the result overlaps with @mask. If true, |
| 139 | * then the call to apic->cpu_mask_to_apicid_and() will |
| 140 | * succeed as well. If not, no point in trying to find a |
| 141 | * vector in this mask. |
| 142 | */ |
| 143 | cpumask_and(vector_searchmask, vector_cpumask, cpu_online_mask); |
| 144 | if (!cpumask_intersects(vector_searchmask, mask)) |
| 145 | goto next_cpu; |
| 146 | |
Jiang Liu | f7fa7ae | 2015-04-14 10:30:10 +0800 | [diff] [blame] | 147 | if (cpumask_subset(vector_cpumask, d->domain)) { |
Jiang Liu | f7fa7ae | 2015-04-14 10:30:10 +0800 | [diff] [blame] | 148 | if (cpumask_equal(vector_cpumask, d->domain)) |
Thomas Gleixner | 433cbd5 | 2015-12-31 16:30:46 +0000 | [diff] [blame] | 149 | goto success; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 150 | /* |
| 151 | * New cpumask using the vector is a proper subset of |
| 152 | * the current in use mask. So cleanup the vector |
| 153 | * allocation for the members that are not used anymore. |
| 154 | */ |
Jiang Liu | f7fa7ae | 2015-04-14 10:30:10 +0800 | [diff] [blame] | 155 | cpumask_andnot(d->old_domain, d->domain, |
| 156 | vector_cpumask); |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 157 | d->move_in_progress = |
| 158 | cpumask_intersects(d->old_domain, cpu_online_mask); |
Jiang Liu | f7fa7ae | 2015-04-14 10:30:10 +0800 | [diff] [blame] | 159 | cpumask_and(d->domain, d->domain, vector_cpumask); |
Thomas Gleixner | 433cbd5 | 2015-12-31 16:30:46 +0000 | [diff] [blame] | 160 | goto success; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | vector = current_vector; |
| 164 | offset = current_offset; |
| 165 | next: |
| 166 | vector += 16; |
| 167 | if (vector >= first_system_vector) { |
| 168 | offset = (offset + 1) % 16; |
| 169 | vector = FIRST_EXTERNAL_VECTOR + offset; |
| 170 | } |
| 171 | |
Thomas Gleixner | 95ffeb4 | 2015-12-31 16:30:47 +0000 | [diff] [blame] | 172 | /* If the search wrapped around, try the next cpu */ |
| 173 | if (unlikely(current_vector == vector)) |
| 174 | goto next_cpu; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 175 | |
| 176 | if (test_bit(vector, used_vectors)) |
| 177 | goto next; |
| 178 | |
Thomas Gleixner | 3716fd2 | 2015-12-31 16:30:48 +0000 | [diff] [blame^] | 179 | for_each_cpu(new_cpu, vector_searchmask) { |
Thomas Gleixner | a782a7e | 2015-08-02 20:38:27 +0000 | [diff] [blame] | 180 | if (!IS_ERR_OR_NULL(per_cpu(vector_irq, new_cpu)[vector])) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 181 | goto next; |
| 182 | } |
| 183 | /* Found one! */ |
| 184 | current_vector = vector; |
| 185 | current_offset = offset; |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 186 | if (d->cfg.vector) { |
| 187 | cpumask_copy(d->old_domain, d->domain); |
| 188 | d->move_in_progress = |
| 189 | cpumask_intersects(d->old_domain, cpu_online_mask); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 190 | } |
Thomas Gleixner | 3716fd2 | 2015-12-31 16:30:48 +0000 | [diff] [blame^] | 191 | for_each_cpu(new_cpu, vector_searchmask) |
Thomas Gleixner | a782a7e | 2015-08-02 20:38:27 +0000 | [diff] [blame] | 192 | per_cpu(vector_irq, new_cpu)[vector] = irq_to_desc(irq); |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 193 | d->cfg.vector = vector; |
Jiang Liu | f7fa7ae | 2015-04-14 10:30:10 +0800 | [diff] [blame] | 194 | cpumask_copy(d->domain, vector_cpumask); |
Thomas Gleixner | 433cbd5 | 2015-12-31 16:30:46 +0000 | [diff] [blame] | 195 | goto success; |
Thomas Gleixner | 95ffeb4 | 2015-12-31 16:30:47 +0000 | [diff] [blame] | 196 | |
| 197 | next_cpu: |
| 198 | /* |
| 199 | * We exclude the current @vector_cpumask from the requested |
| 200 | * @mask and try again with the next online cpu in the |
| 201 | * result. We cannot modify @mask, so we use @vector_cpumask |
| 202 | * as a temporary buffer here as it will be reassigned when |
| 203 | * calling apic->vector_allocation_domain() above. |
| 204 | */ |
| 205 | cpumask_or(searched_cpumask, searched_cpumask, vector_cpumask); |
| 206 | cpumask_andnot(vector_cpumask, mask, searched_cpumask); |
| 207 | cpu = cpumask_first_and(vector_cpumask, cpu_online_mask); |
| 208 | continue; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 209 | } |
Thomas Gleixner | 433cbd5 | 2015-12-31 16:30:46 +0000 | [diff] [blame] | 210 | return -ENOSPC; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 211 | |
Thomas Gleixner | 433cbd5 | 2015-12-31 16:30:46 +0000 | [diff] [blame] | 212 | success: |
Thomas Gleixner | 3716fd2 | 2015-12-31 16:30:48 +0000 | [diff] [blame^] | 213 | /* |
| 214 | * Cache destination APIC IDs into cfg->dest_apicid. This cannot fail |
| 215 | * as we already established, that mask & d->domain & cpu_online_mask |
| 216 | * is not empty. |
| 217 | */ |
| 218 | BUG_ON(apic->cpu_mask_to_apicid_and(mask, d->domain, |
| 219 | &d->cfg.dest_apicid)); |
| 220 | return 0; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 221 | } |
| 222 | |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 223 | static int assign_irq_vector(int irq, struct apic_chip_data *data, |
Jiang Liu | f970510 | 2015-04-14 10:30:00 +0800 | [diff] [blame] | 224 | const struct cpumask *mask) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 225 | { |
| 226 | int err; |
| 227 | unsigned long flags; |
| 228 | |
| 229 | raw_spin_lock_irqsave(&vector_lock, flags); |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 230 | err = __assign_irq_vector(irq, data, mask); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 231 | raw_spin_unlock_irqrestore(&vector_lock, flags); |
| 232 | return err; |
| 233 | } |
| 234 | |
Jiang Liu | 486ca53 | 2015-05-07 10:53:56 +0800 | [diff] [blame] | 235 | static int assign_irq_vector_policy(int irq, int node, |
| 236 | struct apic_chip_data *data, |
| 237 | struct irq_alloc_info *info) |
| 238 | { |
| 239 | if (info && info->mask) |
| 240 | return assign_irq_vector(irq, data, info->mask); |
| 241 | if (node != NUMA_NO_NODE && |
| 242 | assign_irq_vector(irq, data, cpumask_of_node(node)) == 0) |
| 243 | return 0; |
| 244 | return assign_irq_vector(irq, data, apic->target_cpus()); |
| 245 | } |
| 246 | |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 247 | static void clear_irq_vector(int irq, struct apic_chip_data *data) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 248 | { |
Thomas Gleixner | a782a7e | 2015-08-02 20:38:27 +0000 | [diff] [blame] | 249 | struct irq_desc *desc; |
Thomas Gleixner | a782a7e | 2015-08-02 20:38:27 +0000 | [diff] [blame] | 250 | int cpu, vector; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 251 | |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 252 | BUG_ON(!data->cfg.vector); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 253 | |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 254 | vector = data->cfg.vector; |
| 255 | for_each_cpu_and(cpu, data->domain, cpu_online_mask) |
Thomas Gleixner | 7276c6a | 2015-08-02 20:38:25 +0000 | [diff] [blame] | 256 | per_cpu(vector_irq, cpu)[vector] = VECTOR_UNUSED; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 257 | |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 258 | data->cfg.vector = 0; |
| 259 | cpumask_clear(data->domain); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 260 | |
Jiang Liu | 111abeb | 2015-12-31 16:30:44 +0000 | [diff] [blame] | 261 | if (likely(!data->move_in_progress)) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 262 | return; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 263 | |
Thomas Gleixner | a782a7e | 2015-08-02 20:38:27 +0000 | [diff] [blame] | 264 | desc = irq_to_desc(irq); |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 265 | for_each_cpu_and(cpu, data->old_domain, cpu_online_mask) { |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 266 | for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; |
| 267 | vector++) { |
Thomas Gleixner | a782a7e | 2015-08-02 20:38:27 +0000 | [diff] [blame] | 268 | if (per_cpu(vector_irq, cpu)[vector] != desc) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 269 | continue; |
Thomas Gleixner | 7276c6a | 2015-08-02 20:38:25 +0000 | [diff] [blame] | 270 | per_cpu(vector_irq, cpu)[vector] = VECTOR_UNUSED; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 271 | break; |
| 272 | } |
| 273 | } |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 274 | data->move_in_progress = 0; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 275 | } |
| 276 | |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 277 | void init_irq_alloc_info(struct irq_alloc_info *info, |
| 278 | const struct cpumask *mask) |
| 279 | { |
| 280 | memset(info, 0, sizeof(*info)); |
| 281 | info->mask = mask; |
| 282 | } |
| 283 | |
| 284 | void copy_irq_alloc_info(struct irq_alloc_info *dst, struct irq_alloc_info *src) |
| 285 | { |
| 286 | if (src) |
| 287 | *dst = *src; |
| 288 | else |
| 289 | memset(dst, 0, sizeof(*dst)); |
| 290 | } |
| 291 | |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 292 | static void x86_vector_free_irqs(struct irq_domain *domain, |
| 293 | unsigned int virq, unsigned int nr_irqs) |
| 294 | { |
Jiang Liu | 111abeb | 2015-12-31 16:30:44 +0000 | [diff] [blame] | 295 | struct apic_chip_data *apic_data; |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 296 | struct irq_data *irq_data; |
Jiang Liu | 111abeb | 2015-12-31 16:30:44 +0000 | [diff] [blame] | 297 | unsigned long flags; |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 298 | int i; |
| 299 | |
| 300 | for (i = 0; i < nr_irqs; i++) { |
| 301 | irq_data = irq_domain_get_irq_data(x86_vector_domain, virq + i); |
| 302 | if (irq_data && irq_data->chip_data) { |
Jiang Liu | 111abeb | 2015-12-31 16:30:44 +0000 | [diff] [blame] | 303 | raw_spin_lock_irqsave(&vector_lock, flags); |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 304 | clear_irq_vector(virq + i, irq_data->chip_data); |
Jiang Liu | 111abeb | 2015-12-31 16:30:44 +0000 | [diff] [blame] | 305 | apic_data = irq_data->chip_data; |
| 306 | irq_domain_reset_irq_data(irq_data); |
| 307 | raw_spin_unlock_irqrestore(&vector_lock, flags); |
| 308 | free_apic_chip_data(apic_data); |
Jiang Liu | 1331532 | 2015-04-13 14:11:56 +0800 | [diff] [blame] | 309 | #ifdef CONFIG_X86_IO_APIC |
| 310 | if (virq + i < nr_legacy_irqs()) |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 311 | legacy_irq_data[virq + i] = NULL; |
Jiang Liu | 1331532 | 2015-04-13 14:11:56 +0800 | [diff] [blame] | 312 | #endif |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | static int x86_vector_alloc_irqs(struct irq_domain *domain, unsigned int virq, |
| 318 | unsigned int nr_irqs, void *arg) |
| 319 | { |
| 320 | struct irq_alloc_info *info = arg; |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 321 | struct apic_chip_data *data; |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 322 | struct irq_data *irq_data; |
Jiang Liu | 5f2dbbc | 2015-06-01 16:05:14 +0800 | [diff] [blame] | 323 | int i, err, node; |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 324 | |
| 325 | if (disable_apic) |
| 326 | return -ENXIO; |
| 327 | |
| 328 | /* Currently vector allocator can't guarantee contiguous allocations */ |
| 329 | if ((info->flags & X86_IRQ_ALLOC_CONTIGUOUS_VECTORS) && nr_irqs > 1) |
| 330 | return -ENOSYS; |
| 331 | |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 332 | for (i = 0; i < nr_irqs; i++) { |
| 333 | irq_data = irq_domain_get_irq_data(domain, virq + i); |
| 334 | BUG_ON(!irq_data); |
Jiang Liu | 5f2dbbc | 2015-06-01 16:05:14 +0800 | [diff] [blame] | 335 | node = irq_data_get_node(irq_data); |
Jiang Liu | 1331532 | 2015-04-13 14:11:56 +0800 | [diff] [blame] | 336 | #ifdef CONFIG_X86_IO_APIC |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 337 | if (virq + i < nr_legacy_irqs() && legacy_irq_data[virq + i]) |
| 338 | data = legacy_irq_data[virq + i]; |
Jiang Liu | 1331532 | 2015-04-13 14:11:56 +0800 | [diff] [blame] | 339 | else |
| 340 | #endif |
Jiang Liu | 5f2dbbc | 2015-06-01 16:05:14 +0800 | [diff] [blame] | 341 | data = alloc_apic_chip_data(node); |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 342 | if (!data) { |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 343 | err = -ENOMEM; |
| 344 | goto error; |
| 345 | } |
| 346 | |
| 347 | irq_data->chip = &lapic_controller; |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 348 | irq_data->chip_data = data; |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 349 | irq_data->hwirq = virq + i; |
Linus Torvalds | 43af987 | 2015-09-01 15:20:51 -0700 | [diff] [blame] | 350 | err = assign_irq_vector_policy(virq + i, node, data, info); |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 351 | if (err) |
| 352 | goto error; |
| 353 | } |
| 354 | |
| 355 | return 0; |
| 356 | |
| 357 | error: |
| 358 | x86_vector_free_irqs(domain, virq, i + 1); |
| 359 | return err; |
| 360 | } |
| 361 | |
Thomas Gleixner | eb18cf5 | 2015-05-05 11:10:11 +0200 | [diff] [blame] | 362 | static const struct irq_domain_ops x86_vector_domain_ops = { |
| 363 | .alloc = x86_vector_alloc_irqs, |
| 364 | .free = x86_vector_free_irqs, |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 365 | }; |
| 366 | |
Jiang Liu | 11d686e | 2014-10-27 16:12:05 +0800 | [diff] [blame] | 367 | int __init arch_probe_nr_irqs(void) |
| 368 | { |
| 369 | int nr; |
| 370 | |
| 371 | if (nr_irqs > (NR_VECTORS * nr_cpu_ids)) |
| 372 | nr_irqs = NR_VECTORS * nr_cpu_ids; |
| 373 | |
| 374 | nr = (gsi_top + nr_legacy_irqs()) + 8 * nr_cpu_ids; |
| 375 | #if defined(CONFIG_PCI_MSI) || defined(CONFIG_HT_IRQ) |
| 376 | /* |
| 377 | * for MSI and HT dyn irq |
| 378 | */ |
| 379 | if (gsi_top <= NR_IRQS_LEGACY) |
| 380 | nr += 8 * nr_cpu_ids; |
| 381 | else |
| 382 | nr += gsi_top * 16; |
| 383 | #endif |
| 384 | if (nr < nr_irqs) |
| 385 | nr_irqs = nr; |
| 386 | |
Vitaly Kuznetsov | 8c058b0 | 2015-11-03 10:40:14 +0100 | [diff] [blame] | 387 | /* |
| 388 | * We don't know if PIC is present at this point so we need to do |
| 389 | * probe() to get the right number of legacy IRQs. |
| 390 | */ |
| 391 | return legacy_pic->probe(); |
Jiang Liu | 11d686e | 2014-10-27 16:12:05 +0800 | [diff] [blame] | 392 | } |
| 393 | |
Jiang Liu | 1331532 | 2015-04-13 14:11:56 +0800 | [diff] [blame] | 394 | #ifdef CONFIG_X86_IO_APIC |
| 395 | static void init_legacy_irqs(void) |
| 396 | { |
| 397 | int i, node = cpu_to_node(0); |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 398 | struct apic_chip_data *data; |
Jiang Liu | 1331532 | 2015-04-13 14:11:56 +0800 | [diff] [blame] | 399 | |
| 400 | /* |
| 401 | * For legacy IRQ's, start with assigning irq0 to irq15 to |
Ingo Molnar | 191a6635 | 2015-05-11 16:05:09 +0200 | [diff] [blame] | 402 | * ISA_IRQ_VECTOR(i) for all cpu's. |
Jiang Liu | 1331532 | 2015-04-13 14:11:56 +0800 | [diff] [blame] | 403 | */ |
| 404 | for (i = 0; i < nr_legacy_irqs(); i++) { |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 405 | data = legacy_irq_data[i] = alloc_apic_chip_data(node); |
| 406 | BUG_ON(!data); |
Ingo Molnar | 191a6635 | 2015-05-11 16:05:09 +0200 | [diff] [blame] | 407 | |
| 408 | data->cfg.vector = ISA_IRQ_VECTOR(i); |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 409 | cpumask_setall(data->domain); |
| 410 | irq_set_chip_data(i, data); |
Jiang Liu | 1331532 | 2015-04-13 14:11:56 +0800 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | #else |
| 414 | static void init_legacy_irqs(void) { } |
| 415 | #endif |
| 416 | |
Jiang Liu | 11d686e | 2014-10-27 16:12:05 +0800 | [diff] [blame] | 417 | int __init arch_early_irq_init(void) |
| 418 | { |
Jiang Liu | 1331532 | 2015-04-13 14:11:56 +0800 | [diff] [blame] | 419 | init_legacy_irqs(); |
| 420 | |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 421 | x86_vector_domain = irq_domain_add_tree(NULL, &x86_vector_domain_ops, |
| 422 | NULL); |
| 423 | BUG_ON(x86_vector_domain == NULL); |
| 424 | irq_set_default_host(x86_vector_domain); |
| 425 | |
Jiang Liu | 52f518a | 2015-04-13 14:11:35 +0800 | [diff] [blame] | 426 | arch_init_msi_domain(x86_vector_domain); |
Jiang Liu | 49e07d8 | 2015-04-13 14:11:43 +0800 | [diff] [blame] | 427 | arch_init_htirq_domain(x86_vector_domain); |
Jiang Liu | 52f518a | 2015-04-13 14:11:35 +0800 | [diff] [blame] | 428 | |
Jiang Liu | f7fa7ae | 2015-04-14 10:30:10 +0800 | [diff] [blame] | 429 | BUG_ON(!alloc_cpumask_var(&vector_cpumask, GFP_KERNEL)); |
Thomas Gleixner | 3716fd2 | 2015-12-31 16:30:48 +0000 | [diff] [blame^] | 430 | BUG_ON(!alloc_cpumask_var(&vector_searchmask, GFP_KERNEL)); |
Jiang Liu | 8a580f7 | 2015-12-31 16:30:46 +0000 | [diff] [blame] | 431 | BUG_ON(!alloc_cpumask_var(&searched_cpumask, GFP_KERNEL)); |
Jiang Liu | f7fa7ae | 2015-04-14 10:30:10 +0800 | [diff] [blame] | 432 | |
Jiang Liu | 11d686e | 2014-10-27 16:12:05 +0800 | [diff] [blame] | 433 | return arch_early_ioapic_init(); |
| 434 | } |
| 435 | |
Thomas Gleixner | a782a7e | 2015-08-02 20:38:27 +0000 | [diff] [blame] | 436 | /* Initialize vector_irq on a new cpu */ |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 437 | static void __setup_vector_irq(int cpu) |
| 438 | { |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 439 | struct apic_chip_data *data; |
Thomas Gleixner | a782a7e | 2015-08-02 20:38:27 +0000 | [diff] [blame] | 440 | struct irq_desc *desc; |
| 441 | int irq, vector; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 442 | |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 443 | /* Mark the inuse vectors */ |
Thomas Gleixner | a782a7e | 2015-08-02 20:38:27 +0000 | [diff] [blame] | 444 | for_each_irq_desc(irq, desc) { |
| 445 | struct irq_data *idata = irq_desc_get_irq_data(desc); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 446 | |
Thomas Gleixner | a782a7e | 2015-08-02 20:38:27 +0000 | [diff] [blame] | 447 | data = apic_chip_data(idata); |
| 448 | if (!data || !cpumask_test_cpu(cpu, data->domain)) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 449 | continue; |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 450 | vector = data->cfg.vector; |
Thomas Gleixner | a782a7e | 2015-08-02 20:38:27 +0000 | [diff] [blame] | 451 | per_cpu(vector_irq, cpu)[vector] = desc; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 452 | } |
| 453 | /* Mark the free vectors */ |
| 454 | for (vector = 0; vector < NR_VECTORS; ++vector) { |
Thomas Gleixner | a782a7e | 2015-08-02 20:38:27 +0000 | [diff] [blame] | 455 | desc = per_cpu(vector_irq, cpu)[vector]; |
| 456 | if (IS_ERR_OR_NULL(desc)) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 457 | continue; |
| 458 | |
Thomas Gleixner | a782a7e | 2015-08-02 20:38:27 +0000 | [diff] [blame] | 459 | data = apic_chip_data(irq_desc_get_irq_data(desc)); |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 460 | if (!cpumask_test_cpu(cpu, data->domain)) |
Thomas Gleixner | 7276c6a | 2015-08-02 20:38:25 +0000 | [diff] [blame] | 461 | per_cpu(vector_irq, cpu)[vector] = VECTOR_UNUSED; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 462 | } |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /* |
Thomas Gleixner | 5a3f75e | 2015-07-05 17:12:32 +0000 | [diff] [blame] | 466 | * Setup the vector to irq mappings. Must be called with vector_lock held. |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 467 | */ |
| 468 | void setup_vector_irq(int cpu) |
| 469 | { |
| 470 | int irq; |
| 471 | |
Thomas Gleixner | 5a3f75e | 2015-07-05 17:12:32 +0000 | [diff] [blame] | 472 | lockdep_assert_held(&vector_lock); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 473 | /* |
| 474 | * On most of the platforms, legacy PIC delivers the interrupts on the |
| 475 | * boot cpu. But there are certain platforms where PIC interrupts are |
| 476 | * delivered to multiple cpu's. If the legacy IRQ is handled by the |
| 477 | * legacy PIC, for the new cpu that is coming online, setup the static |
| 478 | * legacy vector to irq mapping: |
| 479 | */ |
| 480 | for (irq = 0; irq < nr_legacy_irqs(); irq++) |
Thomas Gleixner | a782a7e | 2015-08-02 20:38:27 +0000 | [diff] [blame] | 481 | per_cpu(vector_irq, cpu)[ISA_IRQ_VECTOR(irq)] = irq_to_desc(irq); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 482 | |
| 483 | __setup_vector_irq(cpu); |
| 484 | } |
| 485 | |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 486 | static int apic_retrigger_irq(struct irq_data *irq_data) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 487 | { |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 488 | struct apic_chip_data *data = apic_chip_data(irq_data); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 489 | unsigned long flags; |
| 490 | int cpu; |
| 491 | |
| 492 | raw_spin_lock_irqsave(&vector_lock, flags); |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 493 | cpu = cpumask_first_and(data->domain, cpu_online_mask); |
| 494 | apic->send_IPI_mask(cpumask_of(cpu), data->cfg.vector); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 495 | raw_spin_unlock_irqrestore(&vector_lock, flags); |
| 496 | |
| 497 | return 1; |
| 498 | } |
| 499 | |
| 500 | void apic_ack_edge(struct irq_data *data) |
| 501 | { |
Jiang Liu | a978609 | 2014-10-27 16:12:07 +0800 | [diff] [blame] | 502 | irq_complete_move(irqd_cfg(data)); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 503 | irq_move_irq(data); |
| 504 | ack_APIC_irq(); |
| 505 | } |
| 506 | |
Jiang Liu | 68f9f44 | 2015-04-14 10:30:01 +0800 | [diff] [blame] | 507 | static int apic_set_affinity(struct irq_data *irq_data, |
| 508 | const struct cpumask *dest, bool force) |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 509 | { |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 510 | struct apic_chip_data *data = irq_data->chip_data; |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 511 | int err, irq = irq_data->irq; |
| 512 | |
| 513 | if (!config_enabled(CONFIG_SMP)) |
| 514 | return -EPERM; |
| 515 | |
| 516 | if (!cpumask_intersects(dest, cpu_online_mask)) |
| 517 | return -EINVAL; |
| 518 | |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 519 | err = assign_irq_vector(irq, data, dest); |
Thomas Gleixner | 3716fd2 | 2015-12-31 16:30:48 +0000 | [diff] [blame^] | 520 | return err ? err : IRQ_SET_MASK_OK; |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | static struct irq_chip lapic_controller = { |
| 524 | .irq_ack = apic_ack_edge, |
Jiang Liu | 68f9f44 | 2015-04-14 10:30:01 +0800 | [diff] [blame] | 525 | .irq_set_affinity = apic_set_affinity, |
Jiang Liu | b5dc8e6 | 2015-04-13 14:11:24 +0800 | [diff] [blame] | 526 | .irq_retrigger = apic_retrigger_irq, |
| 527 | }; |
| 528 | |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 529 | #ifdef CONFIG_SMP |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 530 | static void __send_cleanup_vector(struct apic_chip_data *data) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 531 | { |
| 532 | cpumask_var_t cleanup_mask; |
| 533 | |
| 534 | if (unlikely(!alloc_cpumask_var(&cleanup_mask, GFP_ATOMIC))) { |
| 535 | unsigned int i; |
| 536 | |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 537 | for_each_cpu_and(i, data->old_domain, cpu_online_mask) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 538 | apic->send_IPI_mask(cpumask_of(i), |
| 539 | IRQ_MOVE_CLEANUP_VECTOR); |
| 540 | } else { |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 541 | cpumask_and(cleanup_mask, data->old_domain, cpu_online_mask); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 542 | apic->send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR); |
| 543 | free_cpumask_var(cleanup_mask); |
| 544 | } |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 545 | data->move_in_progress = 0; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 546 | } |
| 547 | |
Jiang Liu | c6c2002 | 2015-04-14 10:30:02 +0800 | [diff] [blame] | 548 | void send_cleanup_vector(struct irq_cfg *cfg) |
| 549 | { |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 550 | struct apic_chip_data *data; |
| 551 | |
| 552 | data = container_of(cfg, struct apic_chip_data, cfg); |
| 553 | if (data->move_in_progress) |
| 554 | __send_cleanup_vector(data); |
Jiang Liu | c6c2002 | 2015-04-14 10:30:02 +0800 | [diff] [blame] | 555 | } |
| 556 | |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 557 | asmlinkage __visible void smp_irq_move_cleanup_interrupt(void) |
| 558 | { |
| 559 | unsigned vector, me; |
| 560 | |
Thomas Gleixner | 6af7faf | 2015-05-15 15:48:25 +0200 | [diff] [blame] | 561 | entering_ack_irq(); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 562 | |
Thomas Gleixner | df54c49 | 2015-08-02 20:38:23 +0000 | [diff] [blame] | 563 | /* Prevent vectors vanishing under us */ |
| 564 | raw_spin_lock(&vector_lock); |
| 565 | |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 566 | me = smp_processor_id(); |
| 567 | for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) { |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 568 | struct apic_chip_data *data; |
Thomas Gleixner | a782a7e | 2015-08-02 20:38:27 +0000 | [diff] [blame] | 569 | struct irq_desc *desc; |
| 570 | unsigned int irr; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 571 | |
Thomas Gleixner | df54c49 | 2015-08-02 20:38:23 +0000 | [diff] [blame] | 572 | retry: |
Thomas Gleixner | a782a7e | 2015-08-02 20:38:27 +0000 | [diff] [blame] | 573 | desc = __this_cpu_read(vector_irq[vector]); |
| 574 | if (IS_ERR_OR_NULL(desc)) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 575 | continue; |
| 576 | |
Thomas Gleixner | df54c49 | 2015-08-02 20:38:23 +0000 | [diff] [blame] | 577 | if (!raw_spin_trylock(&desc->lock)) { |
| 578 | raw_spin_unlock(&vector_lock); |
| 579 | cpu_relax(); |
| 580 | raw_spin_lock(&vector_lock); |
| 581 | goto retry; |
| 582 | } |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 583 | |
Thomas Gleixner | a782a7e | 2015-08-02 20:38:27 +0000 | [diff] [blame] | 584 | data = apic_chip_data(irq_desc_get_irq_data(desc)); |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 585 | if (!data) |
Thomas Gleixner | df54c49 | 2015-08-02 20:38:23 +0000 | [diff] [blame] | 586 | goto unlock; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 587 | |
| 588 | /* |
| 589 | * Check if the irq migration is in progress. If so, we |
| 590 | * haven't received the cleanup request yet for this irq. |
| 591 | */ |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 592 | if (data->move_in_progress) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 593 | goto unlock; |
| 594 | |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 595 | if (vector == data->cfg.vector && |
| 596 | cpumask_test_cpu(me, data->domain)) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 597 | goto unlock; |
| 598 | |
| 599 | irr = apic_read(APIC_IRR + (vector / 32 * 0x10)); |
| 600 | /* |
| 601 | * Check if the vector that needs to be cleanedup is |
| 602 | * registered at the cpu's IRR. If so, then this is not |
| 603 | * the best time to clean it up. Lets clean it up in the |
| 604 | * next attempt by sending another IRQ_MOVE_CLEANUP_VECTOR |
| 605 | * to myself. |
| 606 | */ |
| 607 | if (irr & (1 << (vector % 32))) { |
| 608 | apic->send_IPI_self(IRQ_MOVE_CLEANUP_VECTOR); |
| 609 | goto unlock; |
| 610 | } |
Thomas Gleixner | 7276c6a | 2015-08-02 20:38:25 +0000 | [diff] [blame] | 611 | __this_cpu_write(vector_irq[vector], VECTOR_UNUSED); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 612 | unlock: |
| 613 | raw_spin_unlock(&desc->lock); |
| 614 | } |
| 615 | |
Thomas Gleixner | df54c49 | 2015-08-02 20:38:23 +0000 | [diff] [blame] | 616 | raw_spin_unlock(&vector_lock); |
| 617 | |
Thomas Gleixner | 6af7faf | 2015-05-15 15:48:25 +0200 | [diff] [blame] | 618 | exiting_irq(); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | static void __irq_complete_move(struct irq_cfg *cfg, unsigned vector) |
| 622 | { |
| 623 | unsigned me; |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 624 | struct apic_chip_data *data; |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 625 | |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 626 | data = container_of(cfg, struct apic_chip_data, cfg); |
| 627 | if (likely(!data->move_in_progress)) |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 628 | return; |
| 629 | |
| 630 | me = smp_processor_id(); |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 631 | if (vector == data->cfg.vector && cpumask_test_cpu(me, data->domain)) |
| 632 | __send_cleanup_vector(data); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | void irq_complete_move(struct irq_cfg *cfg) |
| 636 | { |
| 637 | __irq_complete_move(cfg, ~get_irq_regs()->orig_ax); |
| 638 | } |
| 639 | |
| 640 | void irq_force_complete_move(int irq) |
| 641 | { |
| 642 | struct irq_cfg *cfg = irq_cfg(irq); |
| 643 | |
Jiang Liu | 7f3262e | 2015-04-14 10:30:03 +0800 | [diff] [blame] | 644 | if (cfg) |
| 645 | __irq_complete_move(cfg, cfg->vector); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 646 | } |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 647 | #endif |
| 648 | |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 649 | static void __init print_APIC_field(int base) |
| 650 | { |
| 651 | int i; |
| 652 | |
| 653 | printk(KERN_DEBUG); |
| 654 | |
| 655 | for (i = 0; i < 8; i++) |
| 656 | pr_cont("%08x", apic_read(base + i*0x10)); |
| 657 | |
| 658 | pr_cont("\n"); |
| 659 | } |
| 660 | |
| 661 | static void __init print_local_APIC(void *dummy) |
| 662 | { |
| 663 | unsigned int i, v, ver, maxlvt; |
| 664 | u64 icr; |
| 665 | |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 666 | pr_debug("printing local APIC contents on CPU#%d/%d:\n", |
| 667 | smp_processor_id(), hard_smp_processor_id()); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 668 | v = apic_read(APIC_ID); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 669 | pr_info("... APIC ID: %08x (%01x)\n", v, read_apic_id()); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 670 | v = apic_read(APIC_LVR); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 671 | pr_info("... APIC VERSION: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 672 | ver = GET_APIC_VERSION(v); |
| 673 | maxlvt = lapic_get_maxlvt(); |
| 674 | |
| 675 | v = apic_read(APIC_TASKPRI); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 676 | pr_debug("... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 677 | |
| 678 | /* !82489DX */ |
| 679 | if (APIC_INTEGRATED(ver)) { |
| 680 | if (!APIC_XAPIC(ver)) { |
| 681 | v = apic_read(APIC_ARBPRI); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 682 | pr_debug("... APIC ARBPRI: %08x (%02x)\n", |
| 683 | v, v & APIC_ARBPRI_MASK); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 684 | } |
| 685 | v = apic_read(APIC_PROCPRI); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 686 | pr_debug("... APIC PROCPRI: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | /* |
| 690 | * Remote read supported only in the 82489DX and local APIC for |
| 691 | * Pentium processors. |
| 692 | */ |
| 693 | if (!APIC_INTEGRATED(ver) || maxlvt == 3) { |
| 694 | v = apic_read(APIC_RRR); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 695 | pr_debug("... APIC RRR: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | v = apic_read(APIC_LDR); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 699 | pr_debug("... APIC LDR: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 700 | if (!x2apic_enabled()) { |
| 701 | v = apic_read(APIC_DFR); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 702 | pr_debug("... APIC DFR: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 703 | } |
| 704 | v = apic_read(APIC_SPIV); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 705 | pr_debug("... APIC SPIV: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 706 | |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 707 | pr_debug("... APIC ISR field:\n"); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 708 | print_APIC_field(APIC_ISR); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 709 | pr_debug("... APIC TMR field:\n"); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 710 | print_APIC_field(APIC_TMR); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 711 | pr_debug("... APIC IRR field:\n"); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 712 | print_APIC_field(APIC_IRR); |
| 713 | |
| 714 | /* !82489DX */ |
| 715 | if (APIC_INTEGRATED(ver)) { |
| 716 | /* Due to the Pentium erratum 3AP. */ |
| 717 | if (maxlvt > 3) |
| 718 | apic_write(APIC_ESR, 0); |
| 719 | |
| 720 | v = apic_read(APIC_ESR); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 721 | pr_debug("... APIC ESR: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | icr = apic_icr_read(); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 725 | pr_debug("... APIC ICR: %08x\n", (u32)icr); |
| 726 | pr_debug("... APIC ICR2: %08x\n", (u32)(icr >> 32)); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 727 | |
| 728 | v = apic_read(APIC_LVTT); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 729 | pr_debug("... APIC LVTT: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 730 | |
| 731 | if (maxlvt > 3) { |
| 732 | /* PC is LVT#4. */ |
| 733 | v = apic_read(APIC_LVTPC); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 734 | pr_debug("... APIC LVTPC: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 735 | } |
| 736 | v = apic_read(APIC_LVT0); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 737 | pr_debug("... APIC LVT0: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 738 | v = apic_read(APIC_LVT1); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 739 | pr_debug("... APIC LVT1: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 740 | |
| 741 | if (maxlvt > 2) { |
| 742 | /* ERR is LVT#3. */ |
| 743 | v = apic_read(APIC_LVTERR); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 744 | pr_debug("... APIC LVTERR: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | v = apic_read(APIC_TMICT); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 748 | pr_debug("... APIC TMICT: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 749 | v = apic_read(APIC_TMCCT); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 750 | pr_debug("... APIC TMCCT: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 751 | v = apic_read(APIC_TDCR); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 752 | pr_debug("... APIC TDCR: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 753 | |
| 754 | if (boot_cpu_has(X86_FEATURE_EXTAPIC)) { |
| 755 | v = apic_read(APIC_EFEAT); |
| 756 | maxlvt = (v >> 16) & 0xff; |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 757 | pr_debug("... APIC EFEAT: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 758 | v = apic_read(APIC_ECTRL); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 759 | pr_debug("... APIC ECTRL: %08x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 760 | for (i = 0; i < maxlvt; i++) { |
| 761 | v = apic_read(APIC_EILVTn(i)); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 762 | pr_debug("... APIC EILVT%d: %08x\n", i, v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 763 | } |
| 764 | } |
| 765 | pr_cont("\n"); |
| 766 | } |
| 767 | |
| 768 | static void __init print_local_APICs(int maxcpu) |
| 769 | { |
| 770 | int cpu; |
| 771 | |
| 772 | if (!maxcpu) |
| 773 | return; |
| 774 | |
| 775 | preempt_disable(); |
| 776 | for_each_online_cpu(cpu) { |
| 777 | if (cpu >= maxcpu) |
| 778 | break; |
| 779 | smp_call_function_single(cpu, print_local_APIC, NULL, 1); |
| 780 | } |
| 781 | preempt_enable(); |
| 782 | } |
| 783 | |
| 784 | static void __init print_PIC(void) |
| 785 | { |
| 786 | unsigned int v; |
| 787 | unsigned long flags; |
| 788 | |
| 789 | if (!nr_legacy_irqs()) |
| 790 | return; |
| 791 | |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 792 | pr_debug("\nprinting PIC contents\n"); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 793 | |
| 794 | raw_spin_lock_irqsave(&i8259A_lock, flags); |
| 795 | |
| 796 | v = inb(0xa1) << 8 | inb(0x21); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 797 | pr_debug("... PIC IMR: %04x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 798 | |
| 799 | v = inb(0xa0) << 8 | inb(0x20); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 800 | pr_debug("... PIC IRR: %04x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 801 | |
| 802 | outb(0x0b, 0xa0); |
| 803 | outb(0x0b, 0x20); |
| 804 | v = inb(0xa0) << 8 | inb(0x20); |
| 805 | outb(0x0a, 0xa0); |
| 806 | outb(0x0a, 0x20); |
| 807 | |
| 808 | raw_spin_unlock_irqrestore(&i8259A_lock, flags); |
| 809 | |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 810 | pr_debug("... PIC ISR: %04x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 811 | |
| 812 | v = inb(0x4d1) << 8 | inb(0x4d0); |
Jiang Liu | 849d356 | 2014-10-27 16:12:01 +0800 | [diff] [blame] | 813 | pr_debug("... PIC ELCR: %04x\n", v); |
Jiang Liu | 74afab7 | 2014-10-27 16:12:00 +0800 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | static int show_lapic __initdata = 1; |
| 817 | static __init int setup_show_lapic(char *arg) |
| 818 | { |
| 819 | int num = -1; |
| 820 | |
| 821 | if (strcmp(arg, "all") == 0) { |
| 822 | show_lapic = CONFIG_NR_CPUS; |
| 823 | } else { |
| 824 | get_option(&arg, &num); |
| 825 | if (num >= 0) |
| 826 | show_lapic = num; |
| 827 | } |
| 828 | |
| 829 | return 1; |
| 830 | } |
| 831 | __setup("show_lapic=", setup_show_lapic); |
| 832 | |
| 833 | static int __init print_ICs(void) |
| 834 | { |
| 835 | if (apic_verbosity == APIC_QUIET) |
| 836 | return 0; |
| 837 | |
| 838 | print_PIC(); |
| 839 | |
| 840 | /* don't print out if apic is not there */ |
| 841 | if (!cpu_has_apic && !apic_from_smp_config()) |
| 842 | return 0; |
| 843 | |
| 844 | print_local_APICs(show_lapic); |
| 845 | print_IO_APICs(); |
| 846 | |
| 847 | return 0; |
| 848 | } |
| 849 | |
| 850 | late_initcall(print_ICs); |