blob: 7a9e0c6dd75633816353ceddab6b61a8bf9d9cb7 [file] [log] [blame]
Jiang Liu74afab72014-10-27 16:12:00 +08001/*
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 Liub5dc8e62015-04-13 14:11:24 +08006 * Jiang Liu <jiang.liu@linux.intel.com>
7 * Enable support of hierarchical irqdomains
Jiang Liu74afab72014-10-27 16:12:00 +08008 *
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 Liu74afab72014-10-27 16:12:00 +080016#include <linux/slab.h>
Jiang Liud746d1e2015-04-14 10:30:09 +080017#include <asm/irqdomain.h>
Jiang Liu74afab72014-10-27 16:12:00 +080018#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 Liu7f3262e2015-04-14 10:30:03 +080024struct apic_chip_data {
25 struct irq_cfg cfg;
Thomas Gleixner029c6e12017-09-13 23:29:31 +020026 unsigned int cpu;
27 unsigned int prev_cpu;
Jiang Liu7f3262e2015-04-14 10:30:03 +080028 cpumask_var_t domain;
29 cpumask_var_t old_domain;
30 u8 move_in_progress : 1;
31};
32
Jiang Liub5dc8e62015-04-13 14:11:24 +080033struct irq_domain *x86_vector_domain;
Jake Oshinsc8f3e512015-12-10 17:52:59 +000034EXPORT_SYMBOL_GPL(x86_vector_domain);
Jiang Liu74afab72014-10-27 16:12:00 +080035static DEFINE_RAW_SPINLOCK(vector_lock);
Thomas Gleixner3716fd22015-12-31 16:30:48 +000036static cpumask_var_t vector_cpumask, vector_searchmask, searched_cpumask;
Jiang Liub5dc8e62015-04-13 14:11:24 +080037static struct irq_chip lapic_controller;
Jiang Liu13315322015-04-13 14:11:56 +080038#ifdef CONFIG_X86_IO_APIC
Jiang Liu7f3262e2015-04-14 10:30:03 +080039static struct apic_chip_data *legacy_irq_data[NR_IRQS_LEGACY];
Jiang Liu13315322015-04-13 14:11:56 +080040#endif
Jiang Liu74afab72014-10-27 16:12:00 +080041
42void lock_vector_lock(void)
43{
44 /* Used to the online set of cpus does not change
45 * during assign_irq_vector.
46 */
47 raw_spin_lock(&vector_lock);
48}
49
50void unlock_vector_lock(void)
51{
52 raw_spin_unlock(&vector_lock);
53}
54
Thomas Gleixner86ba6552017-09-13 23:29:30 +020055static struct apic_chip_data *apic_chip_data(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +080056{
Thomas Gleixner86ba6552017-09-13 23:29:30 +020057 if (!irqd)
Jiang Liub5dc8e62015-04-13 14:11:24 +080058 return NULL;
59
Thomas Gleixner86ba6552017-09-13 23:29:30 +020060 while (irqd->parent_data)
61 irqd = irqd->parent_data;
Jiang Liub5dc8e62015-04-13 14:11:24 +080062
Thomas Gleixner86ba6552017-09-13 23:29:30 +020063 return irqd->chip_data;
Jiang Liu74afab72014-10-27 16:12:00 +080064}
65
Thomas Gleixner86ba6552017-09-13 23:29:30 +020066struct irq_cfg *irqd_cfg(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +080067{
Thomas Gleixner86ba6552017-09-13 23:29:30 +020068 struct apic_chip_data *apicd = apic_chip_data(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +080069
Thomas Gleixner86ba6552017-09-13 23:29:30 +020070 return apicd ? &apicd->cfg : NULL;
Jiang Liu7f3262e2015-04-14 10:30:03 +080071}
Jake Oshinsc8f3e512015-12-10 17:52:59 +000072EXPORT_SYMBOL_GPL(irqd_cfg);
Jiang Liu7f3262e2015-04-14 10:30:03 +080073
74struct irq_cfg *irq_cfg(unsigned int irq)
75{
76 return irqd_cfg(irq_get_irq_data(irq));
77}
78
79static struct apic_chip_data *alloc_apic_chip_data(int node)
80{
Thomas Gleixner86ba6552017-09-13 23:29:30 +020081 struct apic_chip_data *apicd;
Jiang Liu7f3262e2015-04-14 10:30:03 +080082
Thomas Gleixner86ba6552017-09-13 23:29:30 +020083 apicd = kzalloc_node(sizeof(*apicd), GFP_KERNEL, node);
84 if (!apicd)
Jiang Liu74afab72014-10-27 16:12:00 +080085 return NULL;
Thomas Gleixner86ba6552017-09-13 23:29:30 +020086 if (!zalloc_cpumask_var_node(&apicd->domain, GFP_KERNEL, node))
Jiang Liu7f3262e2015-04-14 10:30:03 +080087 goto out_data;
Thomas Gleixner86ba6552017-09-13 23:29:30 +020088 if (!zalloc_cpumask_var_node(&apicd->old_domain, GFP_KERNEL, node))
Jiang Liu74afab72014-10-27 16:12:00 +080089 goto out_domain;
Thomas Gleixner86ba6552017-09-13 23:29:30 +020090 return apicd;
Jiang Liu74afab72014-10-27 16:12:00 +080091out_domain:
Thomas Gleixner86ba6552017-09-13 23:29:30 +020092 free_cpumask_var(apicd->domain);
Jiang Liu7f3262e2015-04-14 10:30:03 +080093out_data:
Thomas Gleixner86ba6552017-09-13 23:29:30 +020094 kfree(apicd);
Jiang Liu74afab72014-10-27 16:12:00 +080095 return NULL;
96}
97
Thomas Gleixner86ba6552017-09-13 23:29:30 +020098static void free_apic_chip_data(struct apic_chip_data *apicd)
Jiang Liu74afab72014-10-27 16:12:00 +080099{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200100 if (apicd) {
101 free_cpumask_var(apicd->domain);
102 free_cpumask_var(apicd->old_domain);
103 kfree(apicd);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800104 }
Jiang Liu74afab72014-10-27 16:12:00 +0800105}
106
Jiang Liu7f3262e2015-04-14 10:30:03 +0800107static int __assign_irq_vector(int irq, struct apic_chip_data *d,
Thomas Gleixner0e24f7c2017-06-20 01:37:44 +0200108 const struct cpumask *mask,
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200109 struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +0800110{
111 /*
112 * NOTE! The local APIC isn't very good at handling
113 * multiple interrupts at the same interrupt level.
114 * As the interrupt level is determined by taking the
115 * vector number and shifting that right by 4, we
116 * want to spread these out a bit so that they don't
117 * all fall in the same interrupt level.
118 *
119 * Also, we've got to be careful not to trash gate
120 * 0x80, because int 0x80 is hm, kind of importantish. ;)
121 */
122 static int current_vector = FIRST_EXTERNAL_VECTOR + VECTOR_OFFSET_START;
123 static int current_offset = VECTOR_OFFSET_START % 16;
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000124 int cpu, vector;
Jiang Liu74afab72014-10-27 16:12:00 +0800125
Thomas Gleixner98229aa2015-12-31 16:30:54 +0000126 /*
127 * If there is still a move in progress or the previous move has not
128 * been cleaned up completely, tell the caller to come back later.
129 */
130 if (d->move_in_progress ||
131 cpumask_intersects(d->old_domain, cpu_online_mask))
Jiang Liu74afab72014-10-27 16:12:00 +0800132 return -EBUSY;
133
Jiang Liu74afab72014-10-27 16:12:00 +0800134 /* Only try and allocate irqs on cpus that are present */
Jiang Liu7f3262e2015-04-14 10:30:03 +0800135 cpumask_clear(d->old_domain);
Jiang Liu8a580f72015-12-31 16:30:46 +0000136 cpumask_clear(searched_cpumask);
Jiang Liu74afab72014-10-27 16:12:00 +0800137 cpu = cpumask_first_and(mask, cpu_online_mask);
138 while (cpu < nr_cpu_ids) {
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000139 int new_cpu, offset;
Jiang Liu74afab72014-10-27 16:12:00 +0800140
Thomas Gleixnerfdba46f2017-09-13 23:29:27 +0200141 cpumask_copy(vector_cpumask, cpumask_of(cpu));
Jiang Liu74afab72014-10-27 16:12:00 +0800142
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000143 /*
144 * Clear the offline cpus from @vector_cpumask for searching
145 * and verify whether the result overlaps with @mask. If true,
Thomas Gleixner91cd9cb2017-06-20 01:37:43 +0200146 * then the call to apic->cpu_mask_to_apicid() will
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000147 * succeed as well. If not, no point in trying to find a
148 * vector in this mask.
149 */
150 cpumask_and(vector_searchmask, vector_cpumask, cpu_online_mask);
151 if (!cpumask_intersects(vector_searchmask, mask))
152 goto next_cpu;
153
Jiang Liuf7fa7ae2015-04-14 10:30:10 +0800154 if (cpumask_subset(vector_cpumask, d->domain)) {
Jiang Liuf7fa7ae2015-04-14 10:30:10 +0800155 if (cpumask_equal(vector_cpumask, d->domain))
Thomas Gleixner433cbd52015-12-31 16:30:46 +0000156 goto success;
Jiang Liu74afab72014-10-27 16:12:00 +0800157 /*
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000158 * Mark the cpus which are not longer in the mask for
159 * cleanup.
Jiang Liu74afab72014-10-27 16:12:00 +0800160 */
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000161 cpumask_andnot(d->old_domain, d->domain, vector_cpumask);
162 vector = d->cfg.vector;
163 goto update;
Jiang Liu74afab72014-10-27 16:12:00 +0800164 }
165
166 vector = current_vector;
167 offset = current_offset;
168next:
169 vector += 16;
Thomas Gleixner05161b92017-08-28 08:47:18 +0200170 if (vector >= FIRST_SYSTEM_VECTOR) {
Jiang Liu74afab72014-10-27 16:12:00 +0800171 offset = (offset + 1) % 16;
172 vector = FIRST_EXTERNAL_VECTOR + offset;
173 }
174
Thomas Gleixner95ffeb42015-12-31 16:30:47 +0000175 /* If the search wrapped around, try the next cpu */
176 if (unlikely(current_vector == vector))
177 goto next_cpu;
Jiang Liu74afab72014-10-27 16:12:00 +0800178
Thomas Gleixner7854f822017-09-13 23:29:26 +0200179 if (test_bit(vector, system_vectors))
Jiang Liu74afab72014-10-27 16:12:00 +0800180 goto next;
181
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000182 for_each_cpu(new_cpu, vector_searchmask) {
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000183 if (!IS_ERR_OR_NULL(per_cpu(vector_irq, new_cpu)[vector]))
Jiang Liu74afab72014-10-27 16:12:00 +0800184 goto next;
185 }
186 /* Found one! */
187 current_vector = vector;
188 current_offset = offset;
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000189 /* Schedule the old vector for cleanup on all cpus */
190 if (d->cfg.vector)
Jiang Liu7f3262e2015-04-14 10:30:03 +0800191 cpumask_copy(d->old_domain, d->domain);
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000192 for_each_cpu(new_cpu, vector_searchmask)
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000193 per_cpu(vector_irq, new_cpu)[vector] = irq_to_desc(irq);
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000194 goto update;
Thomas Gleixner95ffeb42015-12-31 16:30:47 +0000195
196next_cpu:
197 /*
198 * We exclude the current @vector_cpumask from the requested
199 * @mask and try again with the next online cpu in the
200 * result. We cannot modify @mask, so we use @vector_cpumask
201 * as a temporary buffer here as it will be reassigned when
202 * calling apic->vector_allocation_domain() above.
203 */
204 cpumask_or(searched_cpumask, searched_cpumask, vector_cpumask);
205 cpumask_andnot(vector_cpumask, mask, searched_cpumask);
206 cpu = cpumask_first_and(vector_cpumask, cpu_online_mask);
207 continue;
Jiang Liu74afab72014-10-27 16:12:00 +0800208 }
Thomas Gleixner433cbd52015-12-31 16:30:46 +0000209 return -ENOSPC;
Jiang Liu74afab72014-10-27 16:12:00 +0800210
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000211update:
Thomas Gleixner847667e2015-12-31 16:30:50 +0000212 /*
213 * Exclude offline cpus from the cleanup mask and set the
214 * move_in_progress flag when the result is not empty.
215 */
216 cpumask_and(d->old_domain, d->old_domain, cpu_online_mask);
217 d->move_in_progress = !cpumask_empty(d->old_domain);
Thomas Gleixner551adc62016-03-14 09:40:46 +0100218 d->cfg.old_vector = d->move_in_progress ? d->cfg.vector : 0;
Thomas Gleixner029c6e12017-09-13 23:29:31 +0200219 d->prev_cpu = d->cpu;
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000220 d->cfg.vector = vector;
221 cpumask_copy(d->domain, vector_cpumask);
Thomas Gleixner433cbd52015-12-31 16:30:46 +0000222success:
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000223 /*
224 * Cache destination APIC IDs into cfg->dest_apicid. This cannot fail
225 * as we already established, that mask & d->domain & cpu_online_mask
226 * is not empty.
Thomas Gleixner52b166a2017-06-20 01:37:42 +0200227 *
228 * vector_searchmask is a subset of d->domain and has the offline
229 * cpus masked out.
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000230 */
Thomas Gleixner91cd9cb2017-06-20 01:37:43 +0200231 cpumask_and(vector_searchmask, vector_searchmask, mask);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200232 BUG_ON(apic->cpu_mask_to_apicid(vector_searchmask, irqd,
Thomas Gleixner0e24f7c2017-06-20 01:37:44 +0200233 &d->cfg.dest_apicid));
Thomas Gleixner029c6e12017-09-13 23:29:31 +0200234 d->cpu = cpumask_first(vector_searchmask);
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000235 return 0;
Jiang Liu74afab72014-10-27 16:12:00 +0800236}
237
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200238static int assign_irq_vector(int irq, struct apic_chip_data *apicd,
Thomas Gleixner0e24f7c2017-06-20 01:37:44 +0200239 const struct cpumask *mask,
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200240 struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +0800241{
242 int err;
243 unsigned long flags;
244
245 raw_spin_lock_irqsave(&vector_lock, flags);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200246 err = __assign_irq_vector(irq, apicd, mask, irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800247 raw_spin_unlock_irqrestore(&vector_lock, flags);
248 return err;
249}
250
Jiang Liu486ca532015-05-07 10:53:56 +0800251static int assign_irq_vector_policy(int irq, int node,
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200252 struct apic_chip_data *apicd,
Thomas Gleixner0e24f7c2017-06-20 01:37:44 +0200253 struct irq_alloc_info *info,
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200254 struct irq_data *irqd)
Jiang Liu486ca532015-05-07 10:53:56 +0800255{
256 if (info && info->mask)
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200257 return assign_irq_vector(irq, apicd, info->mask, irqd);
Jiang Liu486ca532015-05-07 10:53:56 +0800258 if (node != NUMA_NO_NODE &&
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200259 assign_irq_vector(irq, apicd, cpumask_of_node(node), irqd) == 0)
Jiang Liu486ca532015-05-07 10:53:56 +0800260 return 0;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200261 return assign_irq_vector(irq, apicd, cpu_online_mask, irqd);
Jiang Liu486ca532015-05-07 10:53:56 +0800262}
263
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200264static void clear_irq_vector(int irq, struct apic_chip_data *apicd)
Jiang Liu74afab72014-10-27 16:12:00 +0800265{
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000266 struct irq_desc *desc;
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000267 int cpu, vector;
Jiang Liu74afab72014-10-27 16:12:00 +0800268
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200269 if (!apicd->cfg.vector)
Keith Busch1bdb8972016-04-27 14:22:32 -0600270 return;
Jiang Liu74afab72014-10-27 16:12:00 +0800271
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200272 vector = apicd->cfg.vector;
273 for_each_cpu_and(cpu, apicd->domain, cpu_online_mask)
Thomas Gleixner7276c6a2015-08-02 20:38:25 +0000274 per_cpu(vector_irq, cpu)[vector] = VECTOR_UNUSED;
Jiang Liu74afab72014-10-27 16:12:00 +0800275
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200276 apicd->cfg.vector = 0;
277 cpumask_clear(apicd->domain);
Jiang Liu74afab72014-10-27 16:12:00 +0800278
Thomas Gleixner98229aa2015-12-31 16:30:54 +0000279 /*
280 * If move is in progress or the old_domain mask is not empty,
281 * i.e. the cleanup IPI has not been processed yet, we need to remove
282 * the old references to desc from all cpus vector tables.
283 */
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200284 if (!apicd->move_in_progress && cpumask_empty(apicd->old_domain))
Jiang Liu74afab72014-10-27 16:12:00 +0800285 return;
Jiang Liu74afab72014-10-27 16:12:00 +0800286
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000287 desc = irq_to_desc(irq);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200288 for_each_cpu_and(cpu, apicd->old_domain, cpu_online_mask) {
Jiang Liu74afab72014-10-27 16:12:00 +0800289 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS;
290 vector++) {
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000291 if (per_cpu(vector_irq, cpu)[vector] != desc)
Jiang Liu74afab72014-10-27 16:12:00 +0800292 continue;
Thomas Gleixner7276c6a2015-08-02 20:38:25 +0000293 per_cpu(vector_irq, cpu)[vector] = VECTOR_UNUSED;
Jiang Liu74afab72014-10-27 16:12:00 +0800294 break;
295 }
296 }
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200297 apicd->move_in_progress = 0;
Jiang Liu74afab72014-10-27 16:12:00 +0800298}
299
Jiang Liub5dc8e62015-04-13 14:11:24 +0800300void init_irq_alloc_info(struct irq_alloc_info *info,
301 const struct cpumask *mask)
302{
303 memset(info, 0, sizeof(*info));
304 info->mask = mask;
305}
306
307void copy_irq_alloc_info(struct irq_alloc_info *dst, struct irq_alloc_info *src)
308{
309 if (src)
310 *dst = *src;
311 else
312 memset(dst, 0, sizeof(*dst));
313}
314
Jiang Liub5dc8e62015-04-13 14:11:24 +0800315static void x86_vector_free_irqs(struct irq_domain *domain,
316 unsigned int virq, unsigned int nr_irqs)
317{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200318 struct apic_chip_data *apicd;
319 struct irq_data *irqd;
Jiang Liu111abeb2015-12-31 16:30:44 +0000320 unsigned long flags;
Jiang Liub5dc8e62015-04-13 14:11:24 +0800321 int i;
322
323 for (i = 0; i < nr_irqs; i++) {
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200324 irqd = irq_domain_get_irq_data(x86_vector_domain, virq + i);
325 if (irqd && irqd->chip_data) {
Jiang Liu111abeb2015-12-31 16:30:44 +0000326 raw_spin_lock_irqsave(&vector_lock, flags);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200327 clear_irq_vector(virq + i, irqd->chip_data);
328 apicd = irqd->chip_data;
329 irq_domain_reset_irq_data(irqd);
Jiang Liu111abeb2015-12-31 16:30:44 +0000330 raw_spin_unlock_irqrestore(&vector_lock, flags);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200331 free_apic_chip_data(apicd);
Jiang Liu13315322015-04-13 14:11:56 +0800332#ifdef CONFIG_X86_IO_APIC
333 if (virq + i < nr_legacy_irqs())
Jiang Liu7f3262e2015-04-14 10:30:03 +0800334 legacy_irq_data[virq + i] = NULL;
Jiang Liu13315322015-04-13 14:11:56 +0800335#endif
Jiang Liub5dc8e62015-04-13 14:11:24 +0800336 }
337 }
338}
339
340static int x86_vector_alloc_irqs(struct irq_domain *domain, unsigned int virq,
341 unsigned int nr_irqs, void *arg)
342{
343 struct irq_alloc_info *info = arg;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200344 struct apic_chip_data *apicd;
345 struct irq_data *irqd;
Jiang Liu5f2dbbc2015-06-01 16:05:14 +0800346 int i, err, node;
Jiang Liub5dc8e62015-04-13 14:11:24 +0800347
348 if (disable_apic)
349 return -ENXIO;
350
351 /* Currently vector allocator can't guarantee contiguous allocations */
352 if ((info->flags & X86_IRQ_ALLOC_CONTIGUOUS_VECTORS) && nr_irqs > 1)
353 return -ENOSYS;
354
Jiang Liub5dc8e62015-04-13 14:11:24 +0800355 for (i = 0; i < nr_irqs; i++) {
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200356 irqd = irq_domain_get_irq_data(domain, virq + i);
357 BUG_ON(!irqd);
358 node = irq_data_get_node(irqd);
Jiang Liu13315322015-04-13 14:11:56 +0800359#ifdef CONFIG_X86_IO_APIC
Jiang Liu7f3262e2015-04-14 10:30:03 +0800360 if (virq + i < nr_legacy_irqs() && legacy_irq_data[virq + i])
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200361 apicd = legacy_irq_data[virq + i];
Jiang Liu13315322015-04-13 14:11:56 +0800362 else
363#endif
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200364 apicd = alloc_apic_chip_data(node);
365 if (!apicd) {
Jiang Liub5dc8e62015-04-13 14:11:24 +0800366 err = -ENOMEM;
367 goto error;
368 }
369
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200370 irqd->chip = &lapic_controller;
371 irqd->chip_data = apicd;
372 irqd->hwirq = virq + i;
373 irqd_set_single_target(irqd);
374 err = assign_irq_vector_policy(virq + i, node, apicd, info,
375 irqd);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800376 if (err)
377 goto error;
378 }
379
380 return 0;
381
382error:
383 x86_vector_free_irqs(domain, virq, i + 1);
384 return err;
385}
386
Thomas Gleixnereb18cf52015-05-05 11:10:11 +0200387static const struct irq_domain_ops x86_vector_domain_ops = {
388 .alloc = x86_vector_alloc_irqs,
389 .free = x86_vector_free_irqs,
Jiang Liub5dc8e62015-04-13 14:11:24 +0800390};
391
Jiang Liu11d686e2014-10-27 16:12:05 +0800392int __init arch_probe_nr_irqs(void)
393{
394 int nr;
395
396 if (nr_irqs > (NR_VECTORS * nr_cpu_ids))
397 nr_irqs = NR_VECTORS * nr_cpu_ids;
398
399 nr = (gsi_top + nr_legacy_irqs()) + 8 * nr_cpu_ids;
400#if defined(CONFIG_PCI_MSI) || defined(CONFIG_HT_IRQ)
401 /*
402 * for MSI and HT dyn irq
403 */
404 if (gsi_top <= NR_IRQS_LEGACY)
405 nr += 8 * nr_cpu_ids;
406 else
407 nr += gsi_top * 16;
408#endif
409 if (nr < nr_irqs)
410 nr_irqs = nr;
411
Vitaly Kuznetsov8c058b02015-11-03 10:40:14 +0100412 /*
413 * We don't know if PIC is present at this point so we need to do
414 * probe() to get the right number of legacy IRQs.
415 */
416 return legacy_pic->probe();
Jiang Liu11d686e2014-10-27 16:12:05 +0800417}
418
Jiang Liu13315322015-04-13 14:11:56 +0800419#ifdef CONFIG_X86_IO_APIC
Dou Liyanga884d252017-06-21 18:14:21 +0800420static void __init init_legacy_irqs(void)
Jiang Liu13315322015-04-13 14:11:56 +0800421{
422 int i, node = cpu_to_node(0);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200423 struct apic_chip_data *apicd;
Jiang Liu13315322015-04-13 14:11:56 +0800424
425 /*
426 * For legacy IRQ's, start with assigning irq0 to irq15 to
Ingo Molnar191a6632015-05-11 16:05:09 +0200427 * ISA_IRQ_VECTOR(i) for all cpu's.
Jiang Liu13315322015-04-13 14:11:56 +0800428 */
429 for (i = 0; i < nr_legacy_irqs(); i++) {
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200430 apicd = legacy_irq_data[i] = alloc_apic_chip_data(node);
431 BUG_ON(!apicd);
Ingo Molnar191a6632015-05-11 16:05:09 +0200432
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200433 apicd->cfg.vector = ISA_IRQ_VECTOR(i);
434 cpumask_copy(apicd->domain, cpumask_of(0));
Thomas Gleixner029c6e12017-09-13 23:29:31 +0200435 apicd->cpu = 0;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200436 irq_set_chip_data(i, apicd);
Jiang Liu13315322015-04-13 14:11:56 +0800437 }
438}
439#else
Dou Liyanga884d252017-06-21 18:14:21 +0800440static inline void init_legacy_irqs(void) { }
Jiang Liu13315322015-04-13 14:11:56 +0800441#endif
442
Jiang Liu11d686e2014-10-27 16:12:05 +0800443int __init arch_early_irq_init(void)
444{
Thomas Gleixner9d35f852017-06-20 01:37:06 +0200445 struct fwnode_handle *fn;
446
Jiang Liu13315322015-04-13 14:11:56 +0800447 init_legacy_irqs();
448
Thomas Gleixner9d35f852017-06-20 01:37:06 +0200449 fn = irq_domain_alloc_named_fwnode("VECTOR");
450 BUG_ON(!fn);
451 x86_vector_domain = irq_domain_create_tree(fn, &x86_vector_domain_ops,
452 NULL);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800453 BUG_ON(x86_vector_domain == NULL);
Thomas Gleixner9d35f852017-06-20 01:37:06 +0200454 irq_domain_free_fwnode(fn);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800455 irq_set_default_host(x86_vector_domain);
456
Jiang Liu52f518a2015-04-13 14:11:35 +0800457 arch_init_msi_domain(x86_vector_domain);
Jiang Liu49e07d82015-04-13 14:11:43 +0800458 arch_init_htirq_domain(x86_vector_domain);
Jiang Liu52f518a2015-04-13 14:11:35 +0800459
Jiang Liuf7fa7ae2015-04-14 10:30:10 +0800460 BUG_ON(!alloc_cpumask_var(&vector_cpumask, GFP_KERNEL));
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000461 BUG_ON(!alloc_cpumask_var(&vector_searchmask, GFP_KERNEL));
Jiang Liu8a580f72015-12-31 16:30:46 +0000462 BUG_ON(!alloc_cpumask_var(&searched_cpumask, GFP_KERNEL));
Jiang Liuf7fa7ae2015-04-14 10:30:10 +0800463
Jiang Liu11d686e2014-10-27 16:12:05 +0800464 return arch_early_ioapic_init();
465}
466
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200467/* Temporary hack to keep things working */
468static void vector_update_shutdown_irqs(void)
Jiang Liu74afab72014-10-27 16:12:00 +0800469{
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000470 struct irq_desc *desc;
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200471 int irq;
Jiang Liu74afab72014-10-27 16:12:00 +0800472
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000473 for_each_irq_desc(irq, desc) {
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200474 struct irq_data *irqd = irq_desc_get_irq_data(desc);
475 struct apic_chip_data *ad = apic_chip_data(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800476
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200477 if (ad && cpumask_test_cpu(cpu, ad->domain) && ad->cfg.vector)
478 this_cpu_write(vector_irq[ad->cfg.vector], desc);
Jiang Liu74afab72014-10-27 16:12:00 +0800479 }
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200480}
Jiang Liu74afab72014-10-27 16:12:00 +0800481
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200482static struct irq_desc *__setup_vector_irq(int vector)
483{
484 int isairq = vector - ISA_IRQ_VECTOR(0);
485
486 /* Check whether the irq is in the legacy space */
487 if (isairq < 0 || isairq >= nr_legacy_irqs())
488 return VECTOR_UNUSED;
489 /* Check whether the irq is handled by the IOAPIC */
490 if (test_bit(isairq, &io_apic_irqs))
491 return VECTOR_UNUSED;
492 return irq_to_desc(isairq);
Jiang Liu74afab72014-10-27 16:12:00 +0800493}
494
495/*
Thomas Gleixner5a3f75e2015-07-05 17:12:32 +0000496 * Setup the vector to irq mappings. Must be called with vector_lock held.
Jiang Liu74afab72014-10-27 16:12:00 +0800497 */
498void setup_vector_irq(int cpu)
499{
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200500 unsigned int vector;
Jiang Liu74afab72014-10-27 16:12:00 +0800501
Thomas Gleixner5a3f75e2015-07-05 17:12:32 +0000502 lockdep_assert_held(&vector_lock);
Jiang Liu74afab72014-10-27 16:12:00 +0800503 /*
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200504 * The interrupt affinity logic never targets interrupts to offline
505 * CPUs. The exception are the legacy PIC interrupts. In general
506 * they are only targeted to CPU0, but depending on the platform
507 * they can be distributed to any online CPU in hardware. The
508 * kernel has no influence on that. So all active legacy vectors
509 * must be installed on all CPUs. All non legacy interrupts can be
510 * cleared.
Jiang Liu74afab72014-10-27 16:12:00 +0800511 */
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200512 for (vector = 0; vector < NR_VECTORS; vector++)
513 this_cpu_write(vector_irq[vector], __setup_vector_irq(vector));
Jiang Liu74afab72014-10-27 16:12:00 +0800514
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200515 /*
516 * Until the rewrite of the managed interrupt management is in
517 * place it's necessary to walk the irq descriptors and check for
518 * interrupts which are targeted at this CPU.
519 */
520 vector_update_shutdown_irqs();
Jiang Liu74afab72014-10-27 16:12:00 +0800521}
522
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200523static int apic_retrigger_irq(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +0800524{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200525 struct apic_chip_data *apicd = apic_chip_data(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800526 unsigned long flags;
527 int cpu;
528
529 raw_spin_lock_irqsave(&vector_lock, flags);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200530 cpu = cpumask_first_and(apicd->domain, cpu_online_mask);
531 apic->send_IPI_mask(cpumask_of(cpu), apicd->cfg.vector);
Jiang Liu74afab72014-10-27 16:12:00 +0800532 raw_spin_unlock_irqrestore(&vector_lock, flags);
533
534 return 1;
535}
536
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200537void apic_ack_edge(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +0800538{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200539 irq_complete_move(irqd_cfg(irqd));
540 irq_move_irq(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800541 ack_APIC_irq();
542}
543
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200544static int apic_set_affinity(struct irq_data *irqd,
Jiang Liu68f9f442015-04-14 10:30:01 +0800545 const struct cpumask *dest, bool force)
Jiang Liub5dc8e62015-04-13 14:11:24 +0800546{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200547 struct apic_chip_data *apicd = irqd->chip_data;
548 int err, irq = irqd->irq;
Jiang Liub5dc8e62015-04-13 14:11:24 +0800549
Masahiro Yamada97f26452016-08-03 13:45:50 -0700550 if (!IS_ENABLED(CONFIG_SMP))
Jiang Liub5dc8e62015-04-13 14:11:24 +0800551 return -EPERM;
552
553 if (!cpumask_intersects(dest, cpu_online_mask))
554 return -EINVAL;
555
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200556 err = assign_irq_vector(irq, apicd, dest, irqd);
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000557 return err ? err : IRQ_SET_MASK_OK;
Jiang Liub5dc8e62015-04-13 14:11:24 +0800558}
559
560static struct irq_chip lapic_controller = {
Thomas Gleixner8947dfb2017-06-20 01:37:01 +0200561 .name = "APIC",
Jiang Liub5dc8e62015-04-13 14:11:24 +0800562 .irq_ack = apic_ack_edge,
Jiang Liu68f9f442015-04-14 10:30:01 +0800563 .irq_set_affinity = apic_set_affinity,
Jiang Liub5dc8e62015-04-13 14:11:24 +0800564 .irq_retrigger = apic_retrigger_irq,
565};
566
Jiang Liu74afab72014-10-27 16:12:00 +0800567#ifdef CONFIG_SMP
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200568static void __send_cleanup_vector(struct apic_chip_data *apicd)
Jiang Liu74afab72014-10-27 16:12:00 +0800569{
Thomas Gleixnerc1684f52015-12-31 16:30:51 +0000570 raw_spin_lock(&vector_lock);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200571 cpumask_and(apicd->old_domain, apicd->old_domain, cpu_online_mask);
572 apicd->move_in_progress = 0;
573 if (!cpumask_empty(apicd->old_domain))
574 apic->send_IPI_mask(apicd->old_domain, IRQ_MOVE_CLEANUP_VECTOR);
Thomas Gleixnerc1684f52015-12-31 16:30:51 +0000575 raw_spin_unlock(&vector_lock);
Jiang Liu74afab72014-10-27 16:12:00 +0800576}
577
Jiang Liuc6c20022015-04-14 10:30:02 +0800578void send_cleanup_vector(struct irq_cfg *cfg)
579{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200580 struct apic_chip_data *apicd;
Jiang Liu7f3262e2015-04-14 10:30:03 +0800581
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200582 apicd = container_of(cfg, struct apic_chip_data, cfg);
583 if (apicd->move_in_progress)
584 __send_cleanup_vector(apicd);
Jiang Liuc6c20022015-04-14 10:30:02 +0800585}
586
Daniel Bristot de Oliveirac4158ff2017-01-04 12:20:33 +0100587asmlinkage __visible void __irq_entry smp_irq_move_cleanup_interrupt(void)
Jiang Liu74afab72014-10-27 16:12:00 +0800588{
589 unsigned vector, me;
590
Thomas Gleixner6af7faf2015-05-15 15:48:25 +0200591 entering_ack_irq();
Jiang Liu74afab72014-10-27 16:12:00 +0800592
Thomas Gleixnerdf54c492015-08-02 20:38:23 +0000593 /* Prevent vectors vanishing under us */
594 raw_spin_lock(&vector_lock);
595
Jiang Liu74afab72014-10-27 16:12:00 +0800596 me = smp_processor_id();
597 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200598 struct apic_chip_data *apicd;
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000599 struct irq_desc *desc;
600 unsigned int irr;
Jiang Liu74afab72014-10-27 16:12:00 +0800601
Thomas Gleixnerdf54c492015-08-02 20:38:23 +0000602 retry:
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000603 desc = __this_cpu_read(vector_irq[vector]);
604 if (IS_ERR_OR_NULL(desc))
Jiang Liu74afab72014-10-27 16:12:00 +0800605 continue;
606
Thomas Gleixnerdf54c492015-08-02 20:38:23 +0000607 if (!raw_spin_trylock(&desc->lock)) {
608 raw_spin_unlock(&vector_lock);
609 cpu_relax();
610 raw_spin_lock(&vector_lock);
611 goto retry;
612 }
Jiang Liu74afab72014-10-27 16:12:00 +0800613
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200614 apicd = apic_chip_data(irq_desc_get_irq_data(desc));
615 if (!apicd)
Thomas Gleixnerdf54c492015-08-02 20:38:23 +0000616 goto unlock;
Jiang Liu74afab72014-10-27 16:12:00 +0800617
618 /*
Thomas Gleixner98229aa2015-12-31 16:30:54 +0000619 * Nothing to cleanup if irq migration is in progress
620 * or this cpu is not set in the cleanup mask.
Jiang Liu74afab72014-10-27 16:12:00 +0800621 */
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200622 if (apicd->move_in_progress ||
623 !cpumask_test_cpu(me, apicd->old_domain))
Jiang Liu74afab72014-10-27 16:12:00 +0800624 goto unlock;
625
Thomas Gleixner98229aa2015-12-31 16:30:54 +0000626 /*
627 * We have two cases to handle here:
628 * 1) vector is unchanged but the target mask got reduced
629 * 2) vector and the target mask has changed
630 *
631 * #1 is obvious, but in #2 we have two vectors with the same
632 * irq descriptor: the old and the new vector. So we need to
633 * make sure that we only cleanup the old vector. The new
634 * vector has the current @vector number in the config and
635 * this cpu is part of the target mask. We better leave that
636 * one alone.
637 */
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200638 if (vector == apicd->cfg.vector &&
639 cpumask_test_cpu(me, apicd->domain))
Jiang Liu74afab72014-10-27 16:12:00 +0800640 goto unlock;
641
642 irr = apic_read(APIC_IRR + (vector / 32 * 0x10));
643 /*
644 * Check if the vector that needs to be cleanedup is
645 * registered at the cpu's IRR. If so, then this is not
646 * the best time to clean it up. Lets clean it up in the
647 * next attempt by sending another IRQ_MOVE_CLEANUP_VECTOR
648 * to myself.
649 */
650 if (irr & (1 << (vector % 32))) {
651 apic->send_IPI_self(IRQ_MOVE_CLEANUP_VECTOR);
652 goto unlock;
653 }
Thomas Gleixner7276c6a2015-08-02 20:38:25 +0000654 __this_cpu_write(vector_irq[vector], VECTOR_UNUSED);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200655 cpumask_clear_cpu(me, apicd->old_domain);
Jiang Liu74afab72014-10-27 16:12:00 +0800656unlock:
657 raw_spin_unlock(&desc->lock);
658 }
659
Thomas Gleixnerdf54c492015-08-02 20:38:23 +0000660 raw_spin_unlock(&vector_lock);
661
Thomas Gleixner6af7faf2015-05-15 15:48:25 +0200662 exiting_irq();
Jiang Liu74afab72014-10-27 16:12:00 +0800663}
664
665static void __irq_complete_move(struct irq_cfg *cfg, unsigned vector)
666{
667 unsigned me;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200668 struct apic_chip_data *apicd;
Jiang Liu74afab72014-10-27 16:12:00 +0800669
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200670 apicd = container_of(cfg, struct apic_chip_data, cfg);
671 if (likely(!apicd->move_in_progress))
Jiang Liu74afab72014-10-27 16:12:00 +0800672 return;
673
674 me = smp_processor_id();
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200675 if (vector == apicd->cfg.vector && cpumask_test_cpu(me, apicd->domain))
676 __send_cleanup_vector(apicd);
Jiang Liu74afab72014-10-27 16:12:00 +0800677}
678
679void irq_complete_move(struct irq_cfg *cfg)
680{
681 __irq_complete_move(cfg, ~get_irq_regs()->orig_ax);
682}
683
Thomas Gleixner90a22822015-12-31 16:30:53 +0000684/*
Thomas Gleixner551adc62016-03-14 09:40:46 +0100685 * Called from fixup_irqs() with @desc->lock held and interrupts disabled.
Thomas Gleixner90a22822015-12-31 16:30:53 +0000686 */
687void irq_force_complete_move(struct irq_desc *desc)
Jiang Liu74afab72014-10-27 16:12:00 +0800688{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200689 struct irq_data *irqd;
690 struct apic_chip_data *apicd;
Mika Westerbergdb91aa72016-10-03 13:17:08 +0300691 struct irq_cfg *cfg;
Thomas Gleixner551adc62016-03-14 09:40:46 +0100692 unsigned int cpu;
Jiang Liu74afab72014-10-27 16:12:00 +0800693
Mika Westerbergdb91aa72016-10-03 13:17:08 +0300694 /*
695 * The function is called for all descriptors regardless of which
696 * irqdomain they belong to. For example if an IRQ is provided by
697 * an irq_chip as part of a GPIO driver, the chip data for that
698 * descriptor is specific to the irq_chip in question.
699 *
700 * Check first that the chip_data is what we expect
701 * (apic_chip_data) before touching it any further.
702 */
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200703 irqd = irq_domain_get_irq_data(x86_vector_domain,
Mika Westerbergdb91aa72016-10-03 13:17:08 +0300704 irq_desc_get_irq(desc));
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200705 if (!irqd)
Mika Westerbergdb91aa72016-10-03 13:17:08 +0300706 return;
707
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200708 apicd = apic_chip_data(irqd);
709 cfg = apicd ? &apicd->cfg : NULL;
Mika Westerbergdb91aa72016-10-03 13:17:08 +0300710
Thomas Gleixner56d7d2f2015-12-31 16:30:52 +0000711 if (!cfg)
712 return;
713
Thomas Gleixner56d7d2f2015-12-31 16:30:52 +0000714 /*
Thomas Gleixner98229aa2015-12-31 16:30:54 +0000715 * This is tricky. If the cleanup of @data->old_domain has not been
716 * done yet, then the following setaffinity call will fail with
717 * -EBUSY. This can leave the interrupt in a stale state.
718 *
Thomas Gleixner551adc62016-03-14 09:40:46 +0100719 * All CPUs are stuck in stop machine with interrupts disabled so
720 * calling __irq_complete_move() would be completely pointless.
Thomas Gleixner56d7d2f2015-12-31 16:30:52 +0000721 */
722 raw_spin_lock(&vector_lock);
Thomas Gleixner551adc62016-03-14 09:40:46 +0100723 /*
724 * Clean out all offline cpus (including the outgoing one) from the
725 * old_domain mask.
726 */
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200727 cpumask_and(apicd->old_domain, apicd->old_domain, cpu_online_mask);
Thomas Gleixner551adc62016-03-14 09:40:46 +0100728
729 /*
730 * If move_in_progress is cleared and the old_domain mask is empty,
731 * then there is nothing to cleanup. fixup_irqs() will take care of
732 * the stale vectors on the outgoing cpu.
733 */
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200734 if (!apicd->move_in_progress && cpumask_empty(apicd->old_domain)) {
Thomas Gleixner98229aa2015-12-31 16:30:54 +0000735 raw_spin_unlock(&vector_lock);
Thomas Gleixner551adc62016-03-14 09:40:46 +0100736 return;
Thomas Gleixner98229aa2015-12-31 16:30:54 +0000737 }
Thomas Gleixner551adc62016-03-14 09:40:46 +0100738
739 /*
740 * 1) The interrupt is in move_in_progress state. That means that we
741 * have not seen an interrupt since the io_apic was reprogrammed to
742 * the new vector.
743 *
744 * 2) The interrupt has fired on the new vector, but the cleanup IPIs
745 * have not been processed yet.
746 */
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200747 if (apicd->move_in_progress) {
Thomas Gleixner551adc62016-03-14 09:40:46 +0100748 /*
749 * In theory there is a race:
750 *
751 * set_ioapic(new_vector) <-- Interrupt is raised before update
752 * is effective, i.e. it's raised on
753 * the old vector.
754 *
755 * So if the target cpu cannot handle that interrupt before
756 * the old vector is cleaned up, we get a spurious interrupt
757 * and in the worst case the ioapic irq line becomes stale.
758 *
759 * But in case of cpu hotplug this should be a non issue
760 * because if the affinity update happens right before all
761 * cpus rendevouz in stop machine, there is no way that the
762 * interrupt can be blocked on the target cpu because all cpus
763 * loops first with interrupts enabled in stop machine, so the
764 * old vector is not yet cleaned up when the interrupt fires.
765 *
766 * So the only way to run into this issue is if the delivery
767 * of the interrupt on the apic/system bus would be delayed
768 * beyond the point where the target cpu disables interrupts
769 * in stop machine. I doubt that it can happen, but at least
770 * there is a theroretical chance. Virtualization might be
771 * able to expose this, but AFAICT the IOAPIC emulation is not
772 * as stupid as the real hardware.
773 *
774 * Anyway, there is nothing we can do about that at this point
775 * w/o refactoring the whole fixup_irq() business completely.
776 * We print at least the irq number and the old vector number,
777 * so we have the necessary information when a problem in that
778 * area arises.
779 */
780 pr_warn("IRQ fixup: irq %d move in progress, old vector %d\n",
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200781 irqd->irq, cfg->old_vector);
Thomas Gleixner551adc62016-03-14 09:40:46 +0100782 }
783 /*
784 * If old_domain is not empty, then other cpus still have the irq
785 * descriptor set in their vector array. Clean it up.
786 */
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200787 for_each_cpu(cpu, apicd->old_domain)
Thomas Gleixner551adc62016-03-14 09:40:46 +0100788 per_cpu(vector_irq, cpu)[cfg->old_vector] = VECTOR_UNUSED;
789
790 /* Cleanup the left overs of the (half finished) move */
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200791 cpumask_clear(apicd->old_domain);
792 apicd->move_in_progress = 0;
Thomas Gleixner56d7d2f2015-12-31 16:30:52 +0000793 raw_spin_unlock(&vector_lock);
Jiang Liu74afab72014-10-27 16:12:00 +0800794}
Jiang Liu74afab72014-10-27 16:12:00 +0800795#endif
796
Jiang Liu74afab72014-10-27 16:12:00 +0800797static void __init print_APIC_field(int base)
798{
799 int i;
800
801 printk(KERN_DEBUG);
802
803 for (i = 0; i < 8; i++)
804 pr_cont("%08x", apic_read(base + i*0x10));
805
806 pr_cont("\n");
807}
808
809static void __init print_local_APIC(void *dummy)
810{
811 unsigned int i, v, ver, maxlvt;
812 u64 icr;
813
Jiang Liu849d3562014-10-27 16:12:01 +0800814 pr_debug("printing local APIC contents on CPU#%d/%d:\n",
815 smp_processor_id(), hard_smp_processor_id());
Jiang Liu74afab72014-10-27 16:12:00 +0800816 v = apic_read(APIC_ID);
Jiang Liu849d3562014-10-27 16:12:01 +0800817 pr_info("... APIC ID: %08x (%01x)\n", v, read_apic_id());
Jiang Liu74afab72014-10-27 16:12:00 +0800818 v = apic_read(APIC_LVR);
Jiang Liu849d3562014-10-27 16:12:01 +0800819 pr_info("... APIC VERSION: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800820 ver = GET_APIC_VERSION(v);
821 maxlvt = lapic_get_maxlvt();
822
823 v = apic_read(APIC_TASKPRI);
Jiang Liu849d3562014-10-27 16:12:01 +0800824 pr_debug("... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
Jiang Liu74afab72014-10-27 16:12:00 +0800825
826 /* !82489DX */
827 if (APIC_INTEGRATED(ver)) {
828 if (!APIC_XAPIC(ver)) {
829 v = apic_read(APIC_ARBPRI);
Jiang Liu849d3562014-10-27 16:12:01 +0800830 pr_debug("... APIC ARBPRI: %08x (%02x)\n",
831 v, v & APIC_ARBPRI_MASK);
Jiang Liu74afab72014-10-27 16:12:00 +0800832 }
833 v = apic_read(APIC_PROCPRI);
Jiang Liu849d3562014-10-27 16:12:01 +0800834 pr_debug("... APIC PROCPRI: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800835 }
836
837 /*
838 * Remote read supported only in the 82489DX and local APIC for
839 * Pentium processors.
840 */
841 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
842 v = apic_read(APIC_RRR);
Jiang Liu849d3562014-10-27 16:12:01 +0800843 pr_debug("... APIC RRR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800844 }
845
846 v = apic_read(APIC_LDR);
Jiang Liu849d3562014-10-27 16:12:01 +0800847 pr_debug("... APIC LDR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800848 if (!x2apic_enabled()) {
849 v = apic_read(APIC_DFR);
Jiang Liu849d3562014-10-27 16:12:01 +0800850 pr_debug("... APIC DFR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800851 }
852 v = apic_read(APIC_SPIV);
Jiang Liu849d3562014-10-27 16:12:01 +0800853 pr_debug("... APIC SPIV: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800854
Jiang Liu849d3562014-10-27 16:12:01 +0800855 pr_debug("... APIC ISR field:\n");
Jiang Liu74afab72014-10-27 16:12:00 +0800856 print_APIC_field(APIC_ISR);
Jiang Liu849d3562014-10-27 16:12:01 +0800857 pr_debug("... APIC TMR field:\n");
Jiang Liu74afab72014-10-27 16:12:00 +0800858 print_APIC_field(APIC_TMR);
Jiang Liu849d3562014-10-27 16:12:01 +0800859 pr_debug("... APIC IRR field:\n");
Jiang Liu74afab72014-10-27 16:12:00 +0800860 print_APIC_field(APIC_IRR);
861
862 /* !82489DX */
863 if (APIC_INTEGRATED(ver)) {
864 /* Due to the Pentium erratum 3AP. */
865 if (maxlvt > 3)
866 apic_write(APIC_ESR, 0);
867
868 v = apic_read(APIC_ESR);
Jiang Liu849d3562014-10-27 16:12:01 +0800869 pr_debug("... APIC ESR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800870 }
871
872 icr = apic_icr_read();
Jiang Liu849d3562014-10-27 16:12:01 +0800873 pr_debug("... APIC ICR: %08x\n", (u32)icr);
874 pr_debug("... APIC ICR2: %08x\n", (u32)(icr >> 32));
Jiang Liu74afab72014-10-27 16:12:00 +0800875
876 v = apic_read(APIC_LVTT);
Jiang Liu849d3562014-10-27 16:12:01 +0800877 pr_debug("... APIC LVTT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800878
879 if (maxlvt > 3) {
880 /* PC is LVT#4. */
881 v = apic_read(APIC_LVTPC);
Jiang Liu849d3562014-10-27 16:12:01 +0800882 pr_debug("... APIC LVTPC: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800883 }
884 v = apic_read(APIC_LVT0);
Jiang Liu849d3562014-10-27 16:12:01 +0800885 pr_debug("... APIC LVT0: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800886 v = apic_read(APIC_LVT1);
Jiang Liu849d3562014-10-27 16:12:01 +0800887 pr_debug("... APIC LVT1: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800888
889 if (maxlvt > 2) {
890 /* ERR is LVT#3. */
891 v = apic_read(APIC_LVTERR);
Jiang Liu849d3562014-10-27 16:12:01 +0800892 pr_debug("... APIC LVTERR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800893 }
894
895 v = apic_read(APIC_TMICT);
Jiang Liu849d3562014-10-27 16:12:01 +0800896 pr_debug("... APIC TMICT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800897 v = apic_read(APIC_TMCCT);
Jiang Liu849d3562014-10-27 16:12:01 +0800898 pr_debug("... APIC TMCCT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800899 v = apic_read(APIC_TDCR);
Jiang Liu849d3562014-10-27 16:12:01 +0800900 pr_debug("... APIC TDCR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800901
902 if (boot_cpu_has(X86_FEATURE_EXTAPIC)) {
903 v = apic_read(APIC_EFEAT);
904 maxlvt = (v >> 16) & 0xff;
Jiang Liu849d3562014-10-27 16:12:01 +0800905 pr_debug("... APIC EFEAT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800906 v = apic_read(APIC_ECTRL);
Jiang Liu849d3562014-10-27 16:12:01 +0800907 pr_debug("... APIC ECTRL: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800908 for (i = 0; i < maxlvt; i++) {
909 v = apic_read(APIC_EILVTn(i));
Jiang Liu849d3562014-10-27 16:12:01 +0800910 pr_debug("... APIC EILVT%d: %08x\n", i, v);
Jiang Liu74afab72014-10-27 16:12:00 +0800911 }
912 }
913 pr_cont("\n");
914}
915
916static void __init print_local_APICs(int maxcpu)
917{
918 int cpu;
919
920 if (!maxcpu)
921 return;
922
923 preempt_disable();
924 for_each_online_cpu(cpu) {
925 if (cpu >= maxcpu)
926 break;
927 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
928 }
929 preempt_enable();
930}
931
932static void __init print_PIC(void)
933{
934 unsigned int v;
935 unsigned long flags;
936
937 if (!nr_legacy_irqs())
938 return;
939
Jiang Liu849d3562014-10-27 16:12:01 +0800940 pr_debug("\nprinting PIC contents\n");
Jiang Liu74afab72014-10-27 16:12:00 +0800941
942 raw_spin_lock_irqsave(&i8259A_lock, flags);
943
944 v = inb(0xa1) << 8 | inb(0x21);
Jiang Liu849d3562014-10-27 16:12:01 +0800945 pr_debug("... PIC IMR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800946
947 v = inb(0xa0) << 8 | inb(0x20);
Jiang Liu849d3562014-10-27 16:12:01 +0800948 pr_debug("... PIC IRR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800949
950 outb(0x0b, 0xa0);
951 outb(0x0b, 0x20);
952 v = inb(0xa0) << 8 | inb(0x20);
953 outb(0x0a, 0xa0);
954 outb(0x0a, 0x20);
955
956 raw_spin_unlock_irqrestore(&i8259A_lock, flags);
957
Jiang Liu849d3562014-10-27 16:12:01 +0800958 pr_debug("... PIC ISR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800959
960 v = inb(0x4d1) << 8 | inb(0x4d0);
Jiang Liu849d3562014-10-27 16:12:01 +0800961 pr_debug("... PIC ELCR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800962}
963
964static int show_lapic __initdata = 1;
965static __init int setup_show_lapic(char *arg)
966{
967 int num = -1;
968
969 if (strcmp(arg, "all") == 0) {
970 show_lapic = CONFIG_NR_CPUS;
971 } else {
972 get_option(&arg, &num);
973 if (num >= 0)
974 show_lapic = num;
975 }
976
977 return 1;
978}
979__setup("show_lapic=", setup_show_lapic);
980
981static int __init print_ICs(void)
982{
983 if (apic_verbosity == APIC_QUIET)
984 return 0;
985
986 print_PIC();
987
988 /* don't print out if apic is not there */
Borislav Petkov93984fb2016-04-04 22:25:00 +0200989 if (!boot_cpu_has(X86_FEATURE_APIC) && !apic_from_smp_config())
Jiang Liu74afab72014-10-27 16:12:00 +0800990 return 0;
991
992 print_local_APICs(show_lapic);
993 print_IO_APICs();
994
995 return 0;
996}
997
998late_initcall(print_ICs);