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