blob: a2761740d345d56d6a8466bd13c7ea72dea5e3ca [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>
Thomas Gleixner65d7ed52017-09-13 23:29:39 +020014#include <linux/seq_file.h>
Jiang Liu74afab72014-10-27 16:12:00 +080015#include <linux/init.h>
16#include <linux/compiler.h>
Jiang Liu74afab72014-10-27 16:12:00 +080017#include <linux/slab.h>
Jiang Liud746d1e2015-04-14 10:30:09 +080018#include <asm/irqdomain.h>
Jiang Liu74afab72014-10-27 16:12:00 +080019#include <asm/hw_irq.h>
20#include <asm/apic.h>
21#include <asm/i8259.h>
22#include <asm/desc.h>
23#include <asm/irq_remapping.h>
24
Thomas Gleixner8d1e3dc2017-09-13 23:29:41 +020025#include <asm/trace/irq_vectors.h>
26
Jiang Liu7f3262e2015-04-14 10:30:03 +080027struct apic_chip_data {
28 struct irq_cfg cfg;
Thomas Gleixner029c6e12017-09-13 23:29:31 +020029 unsigned int cpu;
30 unsigned int prev_cpu;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +020031 struct hlist_node clist;
Jiang Liu7f3262e2015-04-14 10:30:03 +080032 cpumask_var_t domain;
33 cpumask_var_t old_domain;
34 u8 move_in_progress : 1;
35};
36
Jiang Liub5dc8e62015-04-13 14:11:24 +080037struct irq_domain *x86_vector_domain;
Jake Oshinsc8f3e512015-12-10 17:52:59 +000038EXPORT_SYMBOL_GPL(x86_vector_domain);
Jiang Liu74afab72014-10-27 16:12:00 +080039static DEFINE_RAW_SPINLOCK(vector_lock);
Thomas Gleixner3716fd22015-12-31 16:30:48 +000040static cpumask_var_t vector_cpumask, vector_searchmask, searched_cpumask;
Jiang Liub5dc8e62015-04-13 14:11:24 +080041static struct irq_chip lapic_controller;
Thomas Gleixner0fa115d2017-09-13 23:29:38 +020042static struct irq_matrix *vector_matrix;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +020043#ifdef CONFIG_SMP
44static DEFINE_PER_CPU(struct hlist_head, cleanup_list);
45#endif
Jiang Liu74afab72014-10-27 16:12:00 +080046
47void lock_vector_lock(void)
48{
49 /* Used to the online set of cpus does not change
50 * during assign_irq_vector.
51 */
52 raw_spin_lock(&vector_lock);
53}
54
55void unlock_vector_lock(void)
56{
57 raw_spin_unlock(&vector_lock);
58}
59
Thomas Gleixner99a14822017-09-13 23:29:36 +020060void init_irq_alloc_info(struct irq_alloc_info *info,
61 const struct cpumask *mask)
62{
63 memset(info, 0, sizeof(*info));
64 info->mask = mask;
65}
66
67void copy_irq_alloc_info(struct irq_alloc_info *dst, struct irq_alloc_info *src)
68{
69 if (src)
70 *dst = *src;
71 else
72 memset(dst, 0, sizeof(*dst));
73}
74
Thomas Gleixner86ba6552017-09-13 23:29:30 +020075static struct apic_chip_data *apic_chip_data(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +080076{
Thomas Gleixner86ba6552017-09-13 23:29:30 +020077 if (!irqd)
Jiang Liub5dc8e62015-04-13 14:11:24 +080078 return NULL;
79
Thomas Gleixner86ba6552017-09-13 23:29:30 +020080 while (irqd->parent_data)
81 irqd = irqd->parent_data;
Jiang Liub5dc8e62015-04-13 14:11:24 +080082
Thomas Gleixner86ba6552017-09-13 23:29:30 +020083 return irqd->chip_data;
Jiang Liu74afab72014-10-27 16:12:00 +080084}
85
Thomas Gleixner86ba6552017-09-13 23:29:30 +020086struct irq_cfg *irqd_cfg(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +080087{
Thomas Gleixner86ba6552017-09-13 23:29:30 +020088 struct apic_chip_data *apicd = apic_chip_data(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +080089
Thomas Gleixner86ba6552017-09-13 23:29:30 +020090 return apicd ? &apicd->cfg : NULL;
Jiang Liu7f3262e2015-04-14 10:30:03 +080091}
Jake Oshinsc8f3e512015-12-10 17:52:59 +000092EXPORT_SYMBOL_GPL(irqd_cfg);
Jiang Liu7f3262e2015-04-14 10:30:03 +080093
94struct irq_cfg *irq_cfg(unsigned int irq)
95{
96 return irqd_cfg(irq_get_irq_data(irq));
97}
98
99static struct apic_chip_data *alloc_apic_chip_data(int node)
100{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200101 struct apic_chip_data *apicd;
Jiang Liu7f3262e2015-04-14 10:30:03 +0800102
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200103 apicd = kzalloc_node(sizeof(*apicd), GFP_KERNEL, node);
104 if (!apicd)
Jiang Liu74afab72014-10-27 16:12:00 +0800105 return NULL;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200106 if (!zalloc_cpumask_var_node(&apicd->domain, GFP_KERNEL, node))
Jiang Liu7f3262e2015-04-14 10:30:03 +0800107 goto out_data;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200108 if (!zalloc_cpumask_var_node(&apicd->old_domain, GFP_KERNEL, node))
Jiang Liu74afab72014-10-27 16:12:00 +0800109 goto out_domain;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200110 INIT_HLIST_NODE(&apicd->clist);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200111 return apicd;
Jiang Liu74afab72014-10-27 16:12:00 +0800112out_domain:
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200113 free_cpumask_var(apicd->domain);
Jiang Liu7f3262e2015-04-14 10:30:03 +0800114out_data:
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200115 kfree(apicd);
Jiang Liu74afab72014-10-27 16:12:00 +0800116 return NULL;
117}
118
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200119static void free_apic_chip_data(struct apic_chip_data *apicd)
Jiang Liu74afab72014-10-27 16:12:00 +0800120{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200121 if (apicd) {
122 free_cpumask_var(apicd->domain);
123 free_cpumask_var(apicd->old_domain);
124 kfree(apicd);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800125 }
Jiang Liu74afab72014-10-27 16:12:00 +0800126}
127
Jiang Liu7f3262e2015-04-14 10:30:03 +0800128static int __assign_irq_vector(int irq, struct apic_chip_data *d,
Thomas Gleixner0e24f7c2017-06-20 01:37:44 +0200129 const struct cpumask *mask,
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200130 struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +0800131{
132 /*
133 * NOTE! The local APIC isn't very good at handling
134 * multiple interrupts at the same interrupt level.
135 * As the interrupt level is determined by taking the
136 * vector number and shifting that right by 4, we
137 * want to spread these out a bit so that they don't
138 * all fall in the same interrupt level.
139 *
140 * Also, we've got to be careful not to trash gate
141 * 0x80, because int 0x80 is hm, kind of importantish. ;)
142 */
143 static int current_vector = FIRST_EXTERNAL_VECTOR + VECTOR_OFFSET_START;
144 static int current_offset = VECTOR_OFFSET_START % 16;
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000145 int cpu, vector;
Jiang Liu74afab72014-10-27 16:12:00 +0800146
Thomas Gleixner98229aa2015-12-31 16:30:54 +0000147 /*
148 * If there is still a move in progress or the previous move has not
149 * been cleaned up completely, tell the caller to come back later.
150 */
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200151 if (d->cfg.old_vector)
Jiang Liu74afab72014-10-27 16:12:00 +0800152 return -EBUSY;
153
Jiang Liu74afab72014-10-27 16:12:00 +0800154 /* Only try and allocate irqs on cpus that are present */
Jiang Liu7f3262e2015-04-14 10:30:03 +0800155 cpumask_clear(d->old_domain);
Jiang Liu8a580f72015-12-31 16:30:46 +0000156 cpumask_clear(searched_cpumask);
Jiang Liu74afab72014-10-27 16:12:00 +0800157 cpu = cpumask_first_and(mask, cpu_online_mask);
158 while (cpu < nr_cpu_ids) {
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000159 int new_cpu, offset;
Jiang Liu74afab72014-10-27 16:12:00 +0800160
Thomas Gleixnerfdba46f2017-09-13 23:29:27 +0200161 cpumask_copy(vector_cpumask, cpumask_of(cpu));
Jiang Liu74afab72014-10-27 16:12:00 +0800162
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000163 /*
164 * Clear the offline cpus from @vector_cpumask for searching
165 * and verify whether the result overlaps with @mask. If true,
Thomas Gleixner91cd9cb2017-06-20 01:37:43 +0200166 * then the call to apic->cpu_mask_to_apicid() will
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000167 * succeed as well. If not, no point in trying to find a
168 * vector in this mask.
169 */
170 cpumask_and(vector_searchmask, vector_cpumask, cpu_online_mask);
171 if (!cpumask_intersects(vector_searchmask, mask))
172 goto next_cpu;
173
Jiang Liuf7fa7ae2015-04-14 10:30:10 +0800174 if (cpumask_subset(vector_cpumask, d->domain)) {
Jiang Liuf7fa7ae2015-04-14 10:30:10 +0800175 if (cpumask_equal(vector_cpumask, d->domain))
Thomas Gleixner433cbd52015-12-31 16:30:46 +0000176 goto success;
Jiang Liu74afab72014-10-27 16:12:00 +0800177 /*
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000178 * Mark the cpus which are not longer in the mask for
179 * cleanup.
Jiang Liu74afab72014-10-27 16:12:00 +0800180 */
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000181 cpumask_andnot(d->old_domain, d->domain, vector_cpumask);
182 vector = d->cfg.vector;
183 goto update;
Jiang Liu74afab72014-10-27 16:12:00 +0800184 }
185
186 vector = current_vector;
187 offset = current_offset;
188next:
189 vector += 16;
Thomas Gleixner05161b92017-08-28 08:47:18 +0200190 if (vector >= FIRST_SYSTEM_VECTOR) {
Jiang Liu74afab72014-10-27 16:12:00 +0800191 offset = (offset + 1) % 16;
192 vector = FIRST_EXTERNAL_VECTOR + offset;
193 }
194
Thomas Gleixner95ffeb42015-12-31 16:30:47 +0000195 /* If the search wrapped around, try the next cpu */
196 if (unlikely(current_vector == vector))
197 goto next_cpu;
Jiang Liu74afab72014-10-27 16:12:00 +0800198
Thomas Gleixner7854f822017-09-13 23:29:26 +0200199 if (test_bit(vector, system_vectors))
Jiang Liu74afab72014-10-27 16:12:00 +0800200 goto next;
201
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000202 for_each_cpu(new_cpu, vector_searchmask) {
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000203 if (!IS_ERR_OR_NULL(per_cpu(vector_irq, new_cpu)[vector]))
Jiang Liu74afab72014-10-27 16:12:00 +0800204 goto next;
205 }
206 /* Found one! */
207 current_vector = vector;
208 current_offset = offset;
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000209 /* Schedule the old vector for cleanup on all cpus */
210 if (d->cfg.vector)
Jiang Liu7f3262e2015-04-14 10:30:03 +0800211 cpumask_copy(d->old_domain, d->domain);
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000212 for_each_cpu(new_cpu, vector_searchmask)
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000213 per_cpu(vector_irq, new_cpu)[vector] = irq_to_desc(irq);
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000214 goto update;
Thomas Gleixner95ffeb42015-12-31 16:30:47 +0000215
216next_cpu:
217 /*
218 * We exclude the current @vector_cpumask from the requested
219 * @mask and try again with the next online cpu in the
220 * result. We cannot modify @mask, so we use @vector_cpumask
221 * as a temporary buffer here as it will be reassigned when
222 * calling apic->vector_allocation_domain() above.
223 */
224 cpumask_or(searched_cpumask, searched_cpumask, vector_cpumask);
225 cpumask_andnot(vector_cpumask, mask, searched_cpumask);
226 cpu = cpumask_first_and(vector_cpumask, cpu_online_mask);
227 continue;
Jiang Liu74afab72014-10-27 16:12:00 +0800228 }
Thomas Gleixner433cbd52015-12-31 16:30:46 +0000229 return -ENOSPC;
Jiang Liu74afab72014-10-27 16:12:00 +0800230
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000231update:
Thomas Gleixner847667e2015-12-31 16:30:50 +0000232 /*
233 * Exclude offline cpus from the cleanup mask and set the
234 * move_in_progress flag when the result is not empty.
235 */
236 cpumask_and(d->old_domain, d->old_domain, cpu_online_mask);
237 d->move_in_progress = !cpumask_empty(d->old_domain);
Thomas Gleixner551adc62016-03-14 09:40:46 +0100238 d->cfg.old_vector = d->move_in_progress ? d->cfg.vector : 0;
Thomas Gleixner029c6e12017-09-13 23:29:31 +0200239 d->prev_cpu = d->cpu;
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000240 d->cfg.vector = vector;
241 cpumask_copy(d->domain, vector_cpumask);
Thomas Gleixner433cbd52015-12-31 16:30:46 +0000242success:
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000243 /*
244 * Cache destination APIC IDs into cfg->dest_apicid. This cannot fail
245 * as we already established, that mask & d->domain & cpu_online_mask
246 * is not empty.
Thomas Gleixner52b166a2017-06-20 01:37:42 +0200247 *
248 * vector_searchmask is a subset of d->domain and has the offline
249 * cpus masked out.
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000250 */
Thomas Gleixner91cd9cb2017-06-20 01:37:43 +0200251 cpumask_and(vector_searchmask, vector_searchmask, mask);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200252 BUG_ON(apic->cpu_mask_to_apicid(vector_searchmask, irqd,
Thomas Gleixner0e24f7c2017-06-20 01:37:44 +0200253 &d->cfg.dest_apicid));
Thomas Gleixner029c6e12017-09-13 23:29:31 +0200254 d->cpu = cpumask_first(vector_searchmask);
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000255 return 0;
Jiang Liu74afab72014-10-27 16:12:00 +0800256}
257
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200258static int assign_irq_vector(int irq, struct apic_chip_data *apicd,
Thomas Gleixner0e24f7c2017-06-20 01:37:44 +0200259 const struct cpumask *mask,
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200260 struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +0800261{
262 int err;
263 unsigned long flags;
264
265 raw_spin_lock_irqsave(&vector_lock, flags);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200266 err = __assign_irq_vector(irq, apicd, mask, irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800267 raw_spin_unlock_irqrestore(&vector_lock, flags);
268 return err;
269}
270
Jiang Liu486ca532015-05-07 10:53:56 +0800271static int assign_irq_vector_policy(int irq, int node,
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200272 struct apic_chip_data *apicd,
Thomas Gleixner0e24f7c2017-06-20 01:37:44 +0200273 struct irq_alloc_info *info,
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200274 struct irq_data *irqd)
Jiang Liu486ca532015-05-07 10:53:56 +0800275{
Thomas Gleixner258d86e2017-09-13 23:29:35 +0200276 if (info->mask)
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200277 return assign_irq_vector(irq, apicd, info->mask, irqd);
Jiang Liu486ca532015-05-07 10:53:56 +0800278 if (node != NUMA_NO_NODE &&
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200279 assign_irq_vector(irq, apicd, cpumask_of_node(node), irqd) == 0)
Jiang Liu486ca532015-05-07 10:53:56 +0800280 return 0;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200281 return assign_irq_vector(irq, apicd, cpu_online_mask, irqd);
Jiang Liu486ca532015-05-07 10:53:56 +0800282}
283
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200284static void clear_irq_vector(int irq, struct apic_chip_data *apicd)
Jiang Liu74afab72014-10-27 16:12:00 +0800285{
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200286 unsigned int vector = apicd->cfg.vector;
Jiang Liu74afab72014-10-27 16:12:00 +0800287
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200288 if (!vector)
Keith Busch1bdb8972016-04-27 14:22:32 -0600289 return;
Jiang Liu74afab72014-10-27 16:12:00 +0800290
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200291 per_cpu(vector_irq, apicd->cpu)[vector] = VECTOR_UNUSED;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200292 apicd->cfg.vector = 0;
Jiang Liu74afab72014-10-27 16:12:00 +0800293
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200294 /* Clean up move in progress */
295 vector = apicd->cfg.old_vector;
296 if (!vector)
Jiang Liu74afab72014-10-27 16:12:00 +0800297 return;
Jiang Liu74afab72014-10-27 16:12:00 +0800298
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200299 per_cpu(vector_irq, apicd->prev_cpu)[vector] = VECTOR_UNUSED;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200300 apicd->move_in_progress = 0;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200301 hlist_del_init(&apicd->clist);
Jiang Liu74afab72014-10-27 16:12:00 +0800302}
303
Jiang Liub5dc8e62015-04-13 14:11:24 +0800304static void x86_vector_free_irqs(struct irq_domain *domain,
305 unsigned int virq, unsigned int nr_irqs)
306{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200307 struct apic_chip_data *apicd;
308 struct irq_data *irqd;
Jiang Liu111abeb2015-12-31 16:30:44 +0000309 unsigned long flags;
Jiang Liub5dc8e62015-04-13 14:11:24 +0800310 int i;
311
312 for (i = 0; i < nr_irqs; i++) {
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200313 irqd = irq_domain_get_irq_data(x86_vector_domain, virq + i);
314 if (irqd && irqd->chip_data) {
Jiang Liu111abeb2015-12-31 16:30:44 +0000315 raw_spin_lock_irqsave(&vector_lock, flags);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200316 clear_irq_vector(virq + i, irqd->chip_data);
317 apicd = irqd->chip_data;
318 irq_domain_reset_irq_data(irqd);
Jiang Liu111abeb2015-12-31 16:30:44 +0000319 raw_spin_unlock_irqrestore(&vector_lock, flags);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200320 free_apic_chip_data(apicd);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800321 }
322 }
323}
324
325static int x86_vector_alloc_irqs(struct irq_domain *domain, unsigned int virq,
326 unsigned int nr_irqs, void *arg)
327{
328 struct irq_alloc_info *info = arg;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200329 struct apic_chip_data *apicd;
330 struct irq_data *irqd;
Jiang Liu5f2dbbc2015-06-01 16:05:14 +0800331 int i, err, node;
Jiang Liub5dc8e62015-04-13 14:11:24 +0800332
333 if (disable_apic)
334 return -ENXIO;
335
336 /* Currently vector allocator can't guarantee contiguous allocations */
337 if ((info->flags & X86_IRQ_ALLOC_CONTIGUOUS_VECTORS) && nr_irqs > 1)
338 return -ENOSYS;
339
Jiang Liub5dc8e62015-04-13 14:11:24 +0800340 for (i = 0; i < nr_irqs; i++) {
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200341 irqd = irq_domain_get_irq_data(domain, virq + i);
342 BUG_ON(!irqd);
343 node = irq_data_get_node(irqd);
Thomas Gleixner4ef76eb2017-09-13 23:29:34 +0200344 WARN_ON_ONCE(irqd->chip_data);
345 apicd = alloc_apic_chip_data(node);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200346 if (!apicd) {
Jiang Liub5dc8e62015-04-13 14:11:24 +0800347 err = -ENOMEM;
348 goto error;
349 }
350
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200351 irqd->chip = &lapic_controller;
352 irqd->chip_data = apicd;
353 irqd->hwirq = virq + i;
354 irqd_set_single_target(irqd);
Thomas Gleixner4ef76eb2017-09-13 23:29:34 +0200355 /*
356 * Make sure, that the legacy to IOAPIC transition stays on
357 * the same vector. This is required for check_timer() to
358 * work correctly as it might switch back to legacy mode.
359 */
360 if (info->flags & X86_IRQ_ALLOC_LEGACY) {
361 apicd->cfg.vector = ISA_IRQ_VECTOR(virq + i);
362 apicd->cpu = 0;
363 cpumask_copy(apicd->domain, cpumask_of(0));
364 }
365
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200366 err = assign_irq_vector_policy(virq + i, node, apicd, info,
367 irqd);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800368 if (err)
369 goto error;
370 }
371
372 return 0;
373
374error:
375 x86_vector_free_irqs(domain, virq, i + 1);
376 return err;
377}
378
Thomas Gleixner65d7ed52017-09-13 23:29:39 +0200379#ifdef CONFIG_GENERIC_IRQ_DEBUGFS
380void x86_vector_debug_show(struct seq_file *m, struct irq_domain *d,
381 struct irq_data *irqd, int ind)
382{
383 unsigned int cpu, vec, prev_cpu, prev_vec;
384 struct apic_chip_data *apicd;
385 unsigned long flags;
386 int irq;
387
388 if (!irqd) {
389 irq_matrix_debug_show(m, vector_matrix, ind);
390 return;
391 }
392
393 irq = irqd->irq;
394 if (irq < nr_legacy_irqs() && !test_bit(irq, &io_apic_irqs)) {
395 seq_printf(m, "%*sVector: %5d\n", ind, "", ISA_IRQ_VECTOR(irq));
396 seq_printf(m, "%*sTarget: Legacy PIC all CPUs\n", ind, "");
397 return;
398 }
399
400 apicd = irqd->chip_data;
401 if (!apicd) {
402 seq_printf(m, "%*sVector: Not assigned\n", ind, "");
403 return;
404 }
405
406 raw_spin_lock_irqsave(&vector_lock, flags);
407 cpu = apicd->cpu;
408 vec = apicd->cfg.vector;
409 prev_cpu = apicd->prev_cpu;
410 prev_vec = apicd->cfg.old_vector;
411 raw_spin_unlock_irqrestore(&vector_lock, flags);
412 seq_printf(m, "%*sVector: %5u\n", ind, "", vec);
413 seq_printf(m, "%*sTarget: %5u\n", ind, "", cpu);
414 if (prev_vec) {
415 seq_printf(m, "%*sPrevious vector: %5u\n", ind, "", prev_vec);
416 seq_printf(m, "%*sPrevious target: %5u\n", ind, "", prev_cpu);
417 }
418}
419#endif
420
Thomas Gleixnereb18cf52015-05-05 11:10:11 +0200421static const struct irq_domain_ops x86_vector_domain_ops = {
Thomas Gleixner65d7ed52017-09-13 23:29:39 +0200422 .alloc = x86_vector_alloc_irqs,
423 .free = x86_vector_free_irqs,
424#ifdef CONFIG_GENERIC_IRQ_DEBUGFS
425 .debug_show = x86_vector_debug_show,
426#endif
Jiang Liub5dc8e62015-04-13 14:11:24 +0800427};
428
Jiang Liu11d686e2014-10-27 16:12:05 +0800429int __init arch_probe_nr_irqs(void)
430{
431 int nr;
432
433 if (nr_irqs > (NR_VECTORS * nr_cpu_ids))
434 nr_irqs = NR_VECTORS * nr_cpu_ids;
435
436 nr = (gsi_top + nr_legacy_irqs()) + 8 * nr_cpu_ids;
437#if defined(CONFIG_PCI_MSI) || defined(CONFIG_HT_IRQ)
438 /*
439 * for MSI and HT dyn irq
440 */
441 if (gsi_top <= NR_IRQS_LEGACY)
442 nr += 8 * nr_cpu_ids;
443 else
444 nr += gsi_top * 16;
445#endif
446 if (nr < nr_irqs)
447 nr_irqs = nr;
448
Vitaly Kuznetsov8c058b02015-11-03 10:40:14 +0100449 /*
450 * We don't know if PIC is present at this point so we need to do
451 * probe() to get the right number of legacy IRQs.
452 */
453 return legacy_pic->probe();
Jiang Liu11d686e2014-10-27 16:12:05 +0800454}
455
Thomas Gleixner0fa115d2017-09-13 23:29:38 +0200456void lapic_assign_legacy_vector(unsigned int irq, bool replace)
457{
458 /*
459 * Use assign system here so it wont get accounted as allocated
460 * and moveable in the cpu hotplug check and it prevents managed
461 * irq reservation from touching it.
462 */
463 irq_matrix_assign_system(vector_matrix, ISA_IRQ_VECTOR(irq), replace);
464}
465
466void __init lapic_assign_system_vectors(void)
467{
468 unsigned int i, vector = 0;
469
470 for_each_set_bit_from(vector, system_vectors, NR_VECTORS)
471 irq_matrix_assign_system(vector_matrix, vector, false);
472
473 if (nr_legacy_irqs() > 1)
474 lapic_assign_legacy_vector(PIC_CASCADE_IR, false);
475
476 /* System vectors are reserved, online it */
477 irq_matrix_online(vector_matrix);
478
479 /* Mark the preallocated legacy interrupts */
480 for (i = 0; i < nr_legacy_irqs(); i++) {
481 if (i != PIC_CASCADE_IR)
482 irq_matrix_assign(vector_matrix, ISA_IRQ_VECTOR(i));
483 }
484}
485
Jiang Liu11d686e2014-10-27 16:12:05 +0800486int __init arch_early_irq_init(void)
487{
Thomas Gleixner9d35f852017-06-20 01:37:06 +0200488 struct fwnode_handle *fn;
489
Thomas Gleixner9d35f852017-06-20 01:37:06 +0200490 fn = irq_domain_alloc_named_fwnode("VECTOR");
491 BUG_ON(!fn);
492 x86_vector_domain = irq_domain_create_tree(fn, &x86_vector_domain_ops,
493 NULL);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800494 BUG_ON(x86_vector_domain == NULL);
Thomas Gleixner9d35f852017-06-20 01:37:06 +0200495 irq_domain_free_fwnode(fn);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800496 irq_set_default_host(x86_vector_domain);
497
Jiang Liu52f518a2015-04-13 14:11:35 +0800498 arch_init_msi_domain(x86_vector_domain);
Jiang Liu49e07d82015-04-13 14:11:43 +0800499 arch_init_htirq_domain(x86_vector_domain);
Jiang Liu52f518a2015-04-13 14:11:35 +0800500
Jiang Liuf7fa7ae2015-04-14 10:30:10 +0800501 BUG_ON(!alloc_cpumask_var(&vector_cpumask, GFP_KERNEL));
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000502 BUG_ON(!alloc_cpumask_var(&vector_searchmask, GFP_KERNEL));
Jiang Liu8a580f72015-12-31 16:30:46 +0000503 BUG_ON(!alloc_cpumask_var(&searched_cpumask, GFP_KERNEL));
Jiang Liuf7fa7ae2015-04-14 10:30:10 +0800504
Thomas Gleixner0fa115d2017-09-13 23:29:38 +0200505 /*
506 * Allocate the vector matrix allocator data structure and limit the
507 * search area.
508 */
509 vector_matrix = irq_alloc_matrix(NR_VECTORS, FIRST_EXTERNAL_VECTOR,
510 FIRST_SYSTEM_VECTOR);
511 BUG_ON(!vector_matrix);
512
Jiang Liu11d686e2014-10-27 16:12:05 +0800513 return arch_early_ioapic_init();
514}
515
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200516/* Temporary hack to keep things working */
517static void vector_update_shutdown_irqs(void)
Jiang Liu74afab72014-10-27 16:12:00 +0800518{
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000519 struct irq_desc *desc;
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200520 int irq;
Jiang Liu74afab72014-10-27 16:12:00 +0800521
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000522 for_each_irq_desc(irq, desc) {
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200523 struct irq_data *irqd = irq_desc_get_irq_data(desc);
524 struct apic_chip_data *ad = apic_chip_data(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800525
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200526 if (ad && ad->cfg.vector && ad->cpu == smp_processor_id())
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200527 this_cpu_write(vector_irq[ad->cfg.vector], desc);
Jiang Liu74afab72014-10-27 16:12:00 +0800528 }
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200529}
Jiang Liu74afab72014-10-27 16:12:00 +0800530
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200531static struct irq_desc *__setup_vector_irq(int vector)
532{
533 int isairq = vector - ISA_IRQ_VECTOR(0);
534
535 /* Check whether the irq is in the legacy space */
536 if (isairq < 0 || isairq >= nr_legacy_irqs())
537 return VECTOR_UNUSED;
538 /* Check whether the irq is handled by the IOAPIC */
539 if (test_bit(isairq, &io_apic_irqs))
540 return VECTOR_UNUSED;
541 return irq_to_desc(isairq);
Jiang Liu74afab72014-10-27 16:12:00 +0800542}
543
Thomas Gleixner0fa115d2017-09-13 23:29:38 +0200544/* Online the local APIC infrastructure and initialize the vectors */
545void lapic_online(void)
Jiang Liu74afab72014-10-27 16:12:00 +0800546{
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200547 unsigned int vector;
Jiang Liu74afab72014-10-27 16:12:00 +0800548
Thomas Gleixner5a3f75e2015-07-05 17:12:32 +0000549 lockdep_assert_held(&vector_lock);
Thomas Gleixner0fa115d2017-09-13 23:29:38 +0200550
551 /* Online the vector matrix array for this CPU */
552 irq_matrix_online(vector_matrix);
553
Jiang Liu74afab72014-10-27 16:12:00 +0800554 /*
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200555 * The interrupt affinity logic never targets interrupts to offline
556 * CPUs. The exception are the legacy PIC interrupts. In general
557 * they are only targeted to CPU0, but depending on the platform
558 * they can be distributed to any online CPU in hardware. The
559 * kernel has no influence on that. So all active legacy vectors
560 * must be installed on all CPUs. All non legacy interrupts can be
561 * cleared.
Jiang Liu74afab72014-10-27 16:12:00 +0800562 */
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200563 for (vector = 0; vector < NR_VECTORS; vector++)
564 this_cpu_write(vector_irq[vector], __setup_vector_irq(vector));
Jiang Liu74afab72014-10-27 16:12:00 +0800565
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200566 /*
567 * Until the rewrite of the managed interrupt management is in
568 * place it's necessary to walk the irq descriptors and check for
569 * interrupts which are targeted at this CPU.
570 */
571 vector_update_shutdown_irqs();
Jiang Liu74afab72014-10-27 16:12:00 +0800572}
573
Thomas Gleixner0fa115d2017-09-13 23:29:38 +0200574void lapic_offline(void)
575{
576 lock_vector_lock();
577 irq_matrix_offline(vector_matrix);
578 unlock_vector_lock();
579}
580
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200581static int apic_retrigger_irq(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +0800582{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200583 struct apic_chip_data *apicd = apic_chip_data(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800584 unsigned long flags;
Jiang Liu74afab72014-10-27 16:12:00 +0800585
586 raw_spin_lock_irqsave(&vector_lock, flags);
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200587 apic->send_IPI(apicd->cpu, apicd->cfg.vector);
Jiang Liu74afab72014-10-27 16:12:00 +0800588 raw_spin_unlock_irqrestore(&vector_lock, flags);
589
590 return 1;
591}
592
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200593void apic_ack_edge(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +0800594{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200595 irq_complete_move(irqd_cfg(irqd));
596 irq_move_irq(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800597 ack_APIC_irq();
598}
599
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200600static int apic_set_affinity(struct irq_data *irqd,
Jiang Liu68f9f442015-04-14 10:30:01 +0800601 const struct cpumask *dest, bool force)
Jiang Liub5dc8e62015-04-13 14:11:24 +0800602{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200603 struct apic_chip_data *apicd = irqd->chip_data;
604 int err, irq = irqd->irq;
Jiang Liub5dc8e62015-04-13 14:11:24 +0800605
Masahiro Yamada97f26452016-08-03 13:45:50 -0700606 if (!IS_ENABLED(CONFIG_SMP))
Jiang Liub5dc8e62015-04-13 14:11:24 +0800607 return -EPERM;
608
609 if (!cpumask_intersects(dest, cpu_online_mask))
610 return -EINVAL;
611
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200612 err = assign_irq_vector(irq, apicd, dest, irqd);
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000613 return err ? err : IRQ_SET_MASK_OK;
Jiang Liub5dc8e62015-04-13 14:11:24 +0800614}
615
616static struct irq_chip lapic_controller = {
Thomas Gleixner8947dfb2017-06-20 01:37:01 +0200617 .name = "APIC",
Jiang Liub5dc8e62015-04-13 14:11:24 +0800618 .irq_ack = apic_ack_edge,
Jiang Liu68f9f442015-04-14 10:30:01 +0800619 .irq_set_affinity = apic_set_affinity,
Jiang Liub5dc8e62015-04-13 14:11:24 +0800620 .irq_retrigger = apic_retrigger_irq,
621};
622
Jiang Liu74afab72014-10-27 16:12:00 +0800623#ifdef CONFIG_SMP
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200624
625asmlinkage __visible void __irq_entry smp_irq_move_cleanup_interrupt(void)
626{
627 struct hlist_head *clhead = this_cpu_ptr(&cleanup_list);
628 struct apic_chip_data *apicd;
629 struct hlist_node *tmp;
630
631 entering_ack_irq();
632 /* Prevent vectors vanishing under us */
633 raw_spin_lock(&vector_lock);
634
635 hlist_for_each_entry_safe(apicd, tmp, clhead, clist) {
636 unsigned int irr, vector = apicd->cfg.old_vector;
637
638 /*
639 * Paranoia: Check if the vector that needs to be cleaned
640 * up is registered at the APICs IRR. If so, then this is
641 * not the best time to clean it up. Clean it up in the
642 * next attempt by sending another IRQ_MOVE_CLEANUP_VECTOR
643 * to this CPU. IRQ_MOVE_CLEANUP_VECTOR is the lowest
644 * priority external vector, so on return from this
645 * interrupt the device interrupt will happen first.
646 */
647 irr = apic_read(APIC_IRR + (vector / 32 * 0x10));
648 if (irr & (1U << (vector % 32))) {
649 apic->send_IPI_self(IRQ_MOVE_CLEANUP_VECTOR);
650 continue;
651 }
652 hlist_del_init(&apicd->clist);
653 __this_cpu_write(vector_irq[vector], VECTOR_UNUSED);
654 apicd->cfg.old_vector = 0;
655 }
656
657 raw_spin_unlock(&vector_lock);
658 exiting_irq();
659}
660
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200661static void __send_cleanup_vector(struct apic_chip_data *apicd)
Jiang Liu74afab72014-10-27 16:12:00 +0800662{
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200663 unsigned int cpu;
664
Thomas Gleixnerc1684f52015-12-31 16:30:51 +0000665 raw_spin_lock(&vector_lock);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200666 apicd->move_in_progress = 0;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200667 cpu = apicd->prev_cpu;
668 if (cpu_online(cpu)) {
669 hlist_add_head(&apicd->clist, per_cpu_ptr(&cleanup_list, cpu));
670 apic->send_IPI(cpu, IRQ_MOVE_CLEANUP_VECTOR);
671 } else {
672 apicd->cfg.old_vector = 0;
673 }
Thomas Gleixnerc1684f52015-12-31 16:30:51 +0000674 raw_spin_unlock(&vector_lock);
Jiang Liu74afab72014-10-27 16:12:00 +0800675}
676
Jiang Liuc6c20022015-04-14 10:30:02 +0800677void send_cleanup_vector(struct irq_cfg *cfg)
678{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200679 struct apic_chip_data *apicd;
Jiang Liu7f3262e2015-04-14 10:30:03 +0800680
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200681 apicd = container_of(cfg, struct apic_chip_data, cfg);
682 if (apicd->move_in_progress)
683 __send_cleanup_vector(apicd);
Jiang Liuc6c20022015-04-14 10:30:02 +0800684}
685
Jiang Liu74afab72014-10-27 16:12:00 +0800686static void __irq_complete_move(struct irq_cfg *cfg, unsigned vector)
687{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200688 struct apic_chip_data *apicd;
Jiang Liu74afab72014-10-27 16:12:00 +0800689
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200690 apicd = container_of(cfg, struct apic_chip_data, cfg);
691 if (likely(!apicd->move_in_progress))
Jiang Liu74afab72014-10-27 16:12:00 +0800692 return;
693
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200694 if (vector == apicd->cfg.vector && apicd->cpu == smp_processor_id())
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200695 __send_cleanup_vector(apicd);
Jiang Liu74afab72014-10-27 16:12:00 +0800696}
697
698void irq_complete_move(struct irq_cfg *cfg)
699{
700 __irq_complete_move(cfg, ~get_irq_regs()->orig_ax);
701}
702
Thomas Gleixner90a22822015-12-31 16:30:53 +0000703/*
Thomas Gleixner551adc62016-03-14 09:40:46 +0100704 * Called from fixup_irqs() with @desc->lock held and interrupts disabled.
Thomas Gleixner90a22822015-12-31 16:30:53 +0000705 */
706void irq_force_complete_move(struct irq_desc *desc)
Jiang Liu74afab72014-10-27 16:12:00 +0800707{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200708 struct apic_chip_data *apicd;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200709 struct irq_data *irqd;
710 unsigned int vector;
Jiang Liu74afab72014-10-27 16:12:00 +0800711
Mika Westerbergdb91aa72016-10-03 13:17:08 +0300712 /*
713 * The function is called for all descriptors regardless of which
714 * irqdomain they belong to. For example if an IRQ is provided by
715 * an irq_chip as part of a GPIO driver, the chip data for that
716 * descriptor is specific to the irq_chip in question.
717 *
718 * Check first that the chip_data is what we expect
719 * (apic_chip_data) before touching it any further.
720 */
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200721 irqd = irq_domain_get_irq_data(x86_vector_domain,
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200722 irq_desc_get_irq(desc));
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200723 if (!irqd)
Mika Westerbergdb91aa72016-10-03 13:17:08 +0300724 return;
725
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200726 raw_spin_lock(&vector_lock);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200727 apicd = apic_chip_data(irqd);
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200728 if (!apicd)
729 goto unlock;
Thomas Gleixner56d7d2f2015-12-31 16:30:52 +0000730
Thomas Gleixner56d7d2f2015-12-31 16:30:52 +0000731 /*
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200732 * If old_vector is empty, no action required.
733 */
734 vector = apicd->cfg.old_vector;
735 if (!vector)
736 goto unlock;
737
738 /*
739 * This is tricky. If the cleanup of the old vector has not been
Thomas Gleixner98229aa2015-12-31 16:30:54 +0000740 * done yet, then the following setaffinity call will fail with
741 * -EBUSY. This can leave the interrupt in a stale state.
742 *
Thomas Gleixner551adc62016-03-14 09:40:46 +0100743 * All CPUs are stuck in stop machine with interrupts disabled so
744 * calling __irq_complete_move() would be completely pointless.
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200745 *
Thomas Gleixner551adc62016-03-14 09:40:46 +0100746 * 1) The interrupt is in move_in_progress state. That means that we
747 * have not seen an interrupt since the io_apic was reprogrammed to
748 * the new vector.
749 *
750 * 2) The interrupt has fired on the new vector, but the cleanup IPIs
751 * have not been processed yet.
752 */
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200753 if (apicd->move_in_progress) {
Thomas Gleixner551adc62016-03-14 09:40:46 +0100754 /*
755 * In theory there is a race:
756 *
757 * set_ioapic(new_vector) <-- Interrupt is raised before update
758 * is effective, i.e. it's raised on
759 * the old vector.
760 *
761 * So if the target cpu cannot handle that interrupt before
762 * the old vector is cleaned up, we get a spurious interrupt
763 * and in the worst case the ioapic irq line becomes stale.
764 *
765 * But in case of cpu hotplug this should be a non issue
766 * because if the affinity update happens right before all
767 * cpus rendevouz in stop machine, there is no way that the
768 * interrupt can be blocked on the target cpu because all cpus
769 * loops first with interrupts enabled in stop machine, so the
770 * old vector is not yet cleaned up when the interrupt fires.
771 *
772 * So the only way to run into this issue is if the delivery
773 * of the interrupt on the apic/system bus would be delayed
774 * beyond the point where the target cpu disables interrupts
775 * in stop machine. I doubt that it can happen, but at least
776 * there is a theroretical chance. Virtualization might be
777 * able to expose this, but AFAICT the IOAPIC emulation is not
778 * as stupid as the real hardware.
779 *
780 * Anyway, there is nothing we can do about that at this point
781 * w/o refactoring the whole fixup_irq() business completely.
782 * We print at least the irq number and the old vector number,
783 * so we have the necessary information when a problem in that
784 * area arises.
785 */
786 pr_warn("IRQ fixup: irq %d move in progress, old vector %d\n",
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200787 irqd->irq, vector);
Thomas Gleixner551adc62016-03-14 09:40:46 +0100788 }
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200789 per_cpu(vector_irq, apicd->prev_cpu)[vector] = VECTOR_UNUSED;
Thomas Gleixner551adc62016-03-14 09:40:46 +0100790 /* Cleanup the left overs of the (half finished) move */
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200791 cpumask_clear(apicd->old_domain);
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200792 apicd->cfg.old_vector = 0;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200793 apicd->move_in_progress = 0;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200794 hlist_del_init(&apicd->clist);
795unlock:
Thomas Gleixner56d7d2f2015-12-31 16:30:52 +0000796 raw_spin_unlock(&vector_lock);
Jiang Liu74afab72014-10-27 16:12:00 +0800797}
Jiang Liu74afab72014-10-27 16:12:00 +0800798#endif
799
Jiang Liu74afab72014-10-27 16:12:00 +0800800static void __init print_APIC_field(int base)
801{
802 int i;
803
804 printk(KERN_DEBUG);
805
806 for (i = 0; i < 8; i++)
807 pr_cont("%08x", apic_read(base + i*0x10));
808
809 pr_cont("\n");
810}
811
812static void __init print_local_APIC(void *dummy)
813{
814 unsigned int i, v, ver, maxlvt;
815 u64 icr;
816
Jiang Liu849d3562014-10-27 16:12:01 +0800817 pr_debug("printing local APIC contents on CPU#%d/%d:\n",
818 smp_processor_id(), hard_smp_processor_id());
Jiang Liu74afab72014-10-27 16:12:00 +0800819 v = apic_read(APIC_ID);
Jiang Liu849d3562014-10-27 16:12:01 +0800820 pr_info("... APIC ID: %08x (%01x)\n", v, read_apic_id());
Jiang Liu74afab72014-10-27 16:12:00 +0800821 v = apic_read(APIC_LVR);
Jiang Liu849d3562014-10-27 16:12:01 +0800822 pr_info("... APIC VERSION: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800823 ver = GET_APIC_VERSION(v);
824 maxlvt = lapic_get_maxlvt();
825
826 v = apic_read(APIC_TASKPRI);
Jiang Liu849d3562014-10-27 16:12:01 +0800827 pr_debug("... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
Jiang Liu74afab72014-10-27 16:12:00 +0800828
829 /* !82489DX */
830 if (APIC_INTEGRATED(ver)) {
831 if (!APIC_XAPIC(ver)) {
832 v = apic_read(APIC_ARBPRI);
Jiang Liu849d3562014-10-27 16:12:01 +0800833 pr_debug("... APIC ARBPRI: %08x (%02x)\n",
834 v, v & APIC_ARBPRI_MASK);
Jiang Liu74afab72014-10-27 16:12:00 +0800835 }
836 v = apic_read(APIC_PROCPRI);
Jiang Liu849d3562014-10-27 16:12:01 +0800837 pr_debug("... APIC PROCPRI: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800838 }
839
840 /*
841 * Remote read supported only in the 82489DX and local APIC for
842 * Pentium processors.
843 */
844 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
845 v = apic_read(APIC_RRR);
Jiang Liu849d3562014-10-27 16:12:01 +0800846 pr_debug("... APIC RRR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800847 }
848
849 v = apic_read(APIC_LDR);
Jiang Liu849d3562014-10-27 16:12:01 +0800850 pr_debug("... APIC LDR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800851 if (!x2apic_enabled()) {
852 v = apic_read(APIC_DFR);
Jiang Liu849d3562014-10-27 16:12:01 +0800853 pr_debug("... APIC DFR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800854 }
855 v = apic_read(APIC_SPIV);
Jiang Liu849d3562014-10-27 16:12:01 +0800856 pr_debug("... APIC SPIV: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800857
Jiang Liu849d3562014-10-27 16:12:01 +0800858 pr_debug("... APIC ISR field:\n");
Jiang Liu74afab72014-10-27 16:12:00 +0800859 print_APIC_field(APIC_ISR);
Jiang Liu849d3562014-10-27 16:12:01 +0800860 pr_debug("... APIC TMR field:\n");
Jiang Liu74afab72014-10-27 16:12:00 +0800861 print_APIC_field(APIC_TMR);
Jiang Liu849d3562014-10-27 16:12:01 +0800862 pr_debug("... APIC IRR field:\n");
Jiang Liu74afab72014-10-27 16:12:00 +0800863 print_APIC_field(APIC_IRR);
864
865 /* !82489DX */
866 if (APIC_INTEGRATED(ver)) {
867 /* Due to the Pentium erratum 3AP. */
868 if (maxlvt > 3)
869 apic_write(APIC_ESR, 0);
870
871 v = apic_read(APIC_ESR);
Jiang Liu849d3562014-10-27 16:12:01 +0800872 pr_debug("... APIC ESR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800873 }
874
875 icr = apic_icr_read();
Jiang Liu849d3562014-10-27 16:12:01 +0800876 pr_debug("... APIC ICR: %08x\n", (u32)icr);
877 pr_debug("... APIC ICR2: %08x\n", (u32)(icr >> 32));
Jiang Liu74afab72014-10-27 16:12:00 +0800878
879 v = apic_read(APIC_LVTT);
Jiang Liu849d3562014-10-27 16:12:01 +0800880 pr_debug("... APIC LVTT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800881
882 if (maxlvt > 3) {
883 /* PC is LVT#4. */
884 v = apic_read(APIC_LVTPC);
Jiang Liu849d3562014-10-27 16:12:01 +0800885 pr_debug("... APIC LVTPC: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800886 }
887 v = apic_read(APIC_LVT0);
Jiang Liu849d3562014-10-27 16:12:01 +0800888 pr_debug("... APIC LVT0: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800889 v = apic_read(APIC_LVT1);
Jiang Liu849d3562014-10-27 16:12:01 +0800890 pr_debug("... APIC LVT1: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800891
892 if (maxlvt > 2) {
893 /* ERR is LVT#3. */
894 v = apic_read(APIC_LVTERR);
Jiang Liu849d3562014-10-27 16:12:01 +0800895 pr_debug("... APIC LVTERR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800896 }
897
898 v = apic_read(APIC_TMICT);
Jiang Liu849d3562014-10-27 16:12:01 +0800899 pr_debug("... APIC TMICT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800900 v = apic_read(APIC_TMCCT);
Jiang Liu849d3562014-10-27 16:12:01 +0800901 pr_debug("... APIC TMCCT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800902 v = apic_read(APIC_TDCR);
Jiang Liu849d3562014-10-27 16:12:01 +0800903 pr_debug("... APIC TDCR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800904
905 if (boot_cpu_has(X86_FEATURE_EXTAPIC)) {
906 v = apic_read(APIC_EFEAT);
907 maxlvt = (v >> 16) & 0xff;
Jiang Liu849d3562014-10-27 16:12:01 +0800908 pr_debug("... APIC EFEAT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800909 v = apic_read(APIC_ECTRL);
Jiang Liu849d3562014-10-27 16:12:01 +0800910 pr_debug("... APIC ECTRL: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800911 for (i = 0; i < maxlvt; i++) {
912 v = apic_read(APIC_EILVTn(i));
Jiang Liu849d3562014-10-27 16:12:01 +0800913 pr_debug("... APIC EILVT%d: %08x\n", i, v);
Jiang Liu74afab72014-10-27 16:12:00 +0800914 }
915 }
916 pr_cont("\n");
917}
918
919static void __init print_local_APICs(int maxcpu)
920{
921 int cpu;
922
923 if (!maxcpu)
924 return;
925
926 preempt_disable();
927 for_each_online_cpu(cpu) {
928 if (cpu >= maxcpu)
929 break;
930 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
931 }
932 preempt_enable();
933}
934
935static void __init print_PIC(void)
936{
937 unsigned int v;
938 unsigned long flags;
939
940 if (!nr_legacy_irqs())
941 return;
942
Jiang Liu849d3562014-10-27 16:12:01 +0800943 pr_debug("\nprinting PIC contents\n");
Jiang Liu74afab72014-10-27 16:12:00 +0800944
945 raw_spin_lock_irqsave(&i8259A_lock, flags);
946
947 v = inb(0xa1) << 8 | inb(0x21);
Jiang Liu849d3562014-10-27 16:12:01 +0800948 pr_debug("... PIC IMR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800949
950 v = inb(0xa0) << 8 | inb(0x20);
Jiang Liu849d3562014-10-27 16:12:01 +0800951 pr_debug("... PIC IRR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800952
953 outb(0x0b, 0xa0);
954 outb(0x0b, 0x20);
955 v = inb(0xa0) << 8 | inb(0x20);
956 outb(0x0a, 0xa0);
957 outb(0x0a, 0x20);
958
959 raw_spin_unlock_irqrestore(&i8259A_lock, flags);
960
Jiang Liu849d3562014-10-27 16:12:01 +0800961 pr_debug("... PIC ISR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800962
963 v = inb(0x4d1) << 8 | inb(0x4d0);
Jiang Liu849d3562014-10-27 16:12:01 +0800964 pr_debug("... PIC ELCR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800965}
966
967static int show_lapic __initdata = 1;
968static __init int setup_show_lapic(char *arg)
969{
970 int num = -1;
971
972 if (strcmp(arg, "all") == 0) {
973 show_lapic = CONFIG_NR_CPUS;
974 } else {
975 get_option(&arg, &num);
976 if (num >= 0)
977 show_lapic = num;
978 }
979
980 return 1;
981}
982__setup("show_lapic=", setup_show_lapic);
983
984static int __init print_ICs(void)
985{
986 if (apic_verbosity == APIC_QUIET)
987 return 0;
988
989 print_PIC();
990
991 /* don't print out if apic is not there */
Borislav Petkov93984fb2016-04-04 22:25:00 +0200992 if (!boot_cpu_has(X86_FEATURE_APIC) && !apic_from_smp_config())
Jiang Liu74afab72014-10-27 16:12:00 +0800993 return 0;
994
995 print_local_APICs(show_lapic);
996 print_IO_APICs();
997
998 return 0;
999}
1000
1001late_initcall(print_ICs);