blob: acdc74df649dcf359f5b9d3a84c1d211ab0f3b55 [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
Jiang Liu7f3262e2015-04-14 10:30:03 +080025struct apic_chip_data {
26 struct irq_cfg cfg;
Thomas Gleixner029c6e12017-09-13 23:29:31 +020027 unsigned int cpu;
28 unsigned int prev_cpu;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +020029 struct hlist_node clist;
Jiang Liu7f3262e2015-04-14 10:30:03 +080030 cpumask_var_t domain;
31 cpumask_var_t old_domain;
32 u8 move_in_progress : 1;
33};
34
Jiang Liub5dc8e62015-04-13 14:11:24 +080035struct irq_domain *x86_vector_domain;
Jake Oshinsc8f3e512015-12-10 17:52:59 +000036EXPORT_SYMBOL_GPL(x86_vector_domain);
Jiang Liu74afab72014-10-27 16:12:00 +080037static DEFINE_RAW_SPINLOCK(vector_lock);
Thomas Gleixner3716fd22015-12-31 16:30:48 +000038static cpumask_var_t vector_cpumask, vector_searchmask, searched_cpumask;
Jiang Liub5dc8e62015-04-13 14:11:24 +080039static struct irq_chip lapic_controller;
Thomas Gleixner0fa115d2017-09-13 23:29:38 +020040static struct irq_matrix *vector_matrix;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +020041#ifdef CONFIG_SMP
42static DEFINE_PER_CPU(struct hlist_head, cleanup_list);
43#endif
Jiang Liu74afab72014-10-27 16:12:00 +080044
45void lock_vector_lock(void)
46{
47 /* Used to the online set of cpus does not change
48 * during assign_irq_vector.
49 */
50 raw_spin_lock(&vector_lock);
51}
52
53void unlock_vector_lock(void)
54{
55 raw_spin_unlock(&vector_lock);
56}
57
Thomas Gleixner99a14822017-09-13 23:29:36 +020058void init_irq_alloc_info(struct irq_alloc_info *info,
59 const struct cpumask *mask)
60{
61 memset(info, 0, sizeof(*info));
62 info->mask = mask;
63}
64
65void copy_irq_alloc_info(struct irq_alloc_info *dst, struct irq_alloc_info *src)
66{
67 if (src)
68 *dst = *src;
69 else
70 memset(dst, 0, sizeof(*dst));
71}
72
Thomas Gleixner86ba6552017-09-13 23:29:30 +020073static struct apic_chip_data *apic_chip_data(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +080074{
Thomas Gleixner86ba6552017-09-13 23:29:30 +020075 if (!irqd)
Jiang Liub5dc8e62015-04-13 14:11:24 +080076 return NULL;
77
Thomas Gleixner86ba6552017-09-13 23:29:30 +020078 while (irqd->parent_data)
79 irqd = irqd->parent_data;
Jiang Liub5dc8e62015-04-13 14:11:24 +080080
Thomas Gleixner86ba6552017-09-13 23:29:30 +020081 return irqd->chip_data;
Jiang Liu74afab72014-10-27 16:12:00 +080082}
83
Thomas Gleixner86ba6552017-09-13 23:29:30 +020084struct irq_cfg *irqd_cfg(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +080085{
Thomas Gleixner86ba6552017-09-13 23:29:30 +020086 struct apic_chip_data *apicd = apic_chip_data(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +080087
Thomas Gleixner86ba6552017-09-13 23:29:30 +020088 return apicd ? &apicd->cfg : NULL;
Jiang Liu7f3262e2015-04-14 10:30:03 +080089}
Jake Oshinsc8f3e512015-12-10 17:52:59 +000090EXPORT_SYMBOL_GPL(irqd_cfg);
Jiang Liu7f3262e2015-04-14 10:30:03 +080091
92struct irq_cfg *irq_cfg(unsigned int irq)
93{
94 return irqd_cfg(irq_get_irq_data(irq));
95}
96
97static struct apic_chip_data *alloc_apic_chip_data(int node)
98{
Thomas Gleixner86ba6552017-09-13 23:29:30 +020099 struct apic_chip_data *apicd;
Jiang Liu7f3262e2015-04-14 10:30:03 +0800100
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200101 apicd = kzalloc_node(sizeof(*apicd), GFP_KERNEL, node);
102 if (!apicd)
Jiang Liu74afab72014-10-27 16:12:00 +0800103 return NULL;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200104 if (!zalloc_cpumask_var_node(&apicd->domain, GFP_KERNEL, node))
Jiang Liu7f3262e2015-04-14 10:30:03 +0800105 goto out_data;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200106 if (!zalloc_cpumask_var_node(&apicd->old_domain, GFP_KERNEL, node))
Jiang Liu74afab72014-10-27 16:12:00 +0800107 goto out_domain;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200108 INIT_HLIST_NODE(&apicd->clist);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200109 return apicd;
Jiang Liu74afab72014-10-27 16:12:00 +0800110out_domain:
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200111 free_cpumask_var(apicd->domain);
Jiang Liu7f3262e2015-04-14 10:30:03 +0800112out_data:
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200113 kfree(apicd);
Jiang Liu74afab72014-10-27 16:12:00 +0800114 return NULL;
115}
116
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200117static void free_apic_chip_data(struct apic_chip_data *apicd)
Jiang Liu74afab72014-10-27 16:12:00 +0800118{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200119 if (apicd) {
120 free_cpumask_var(apicd->domain);
121 free_cpumask_var(apicd->old_domain);
122 kfree(apicd);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800123 }
Jiang Liu74afab72014-10-27 16:12:00 +0800124}
125
Jiang Liu7f3262e2015-04-14 10:30:03 +0800126static int __assign_irq_vector(int irq, struct apic_chip_data *d,
Thomas Gleixner0e24f7c2017-06-20 01:37:44 +0200127 const struct cpumask *mask,
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200128 struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +0800129{
130 /*
131 * NOTE! The local APIC isn't very good at handling
132 * multiple interrupts at the same interrupt level.
133 * As the interrupt level is determined by taking the
134 * vector number and shifting that right by 4, we
135 * want to spread these out a bit so that they don't
136 * all fall in the same interrupt level.
137 *
138 * Also, we've got to be careful not to trash gate
139 * 0x80, because int 0x80 is hm, kind of importantish. ;)
140 */
141 static int current_vector = FIRST_EXTERNAL_VECTOR + VECTOR_OFFSET_START;
142 static int current_offset = VECTOR_OFFSET_START % 16;
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000143 int cpu, vector;
Jiang Liu74afab72014-10-27 16:12:00 +0800144
Thomas Gleixner98229aa2015-12-31 16:30:54 +0000145 /*
146 * If there is still a move in progress or the previous move has not
147 * been cleaned up completely, tell the caller to come back later.
148 */
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200149 if (d->cfg.old_vector)
Jiang Liu74afab72014-10-27 16:12:00 +0800150 return -EBUSY;
151
Jiang Liu74afab72014-10-27 16:12:00 +0800152 /* Only try and allocate irqs on cpus that are present */
Jiang Liu7f3262e2015-04-14 10:30:03 +0800153 cpumask_clear(d->old_domain);
Jiang Liu8a580f72015-12-31 16:30:46 +0000154 cpumask_clear(searched_cpumask);
Jiang Liu74afab72014-10-27 16:12:00 +0800155 cpu = cpumask_first_and(mask, cpu_online_mask);
156 while (cpu < nr_cpu_ids) {
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000157 int new_cpu, offset;
Jiang Liu74afab72014-10-27 16:12:00 +0800158
Thomas Gleixnerfdba46f2017-09-13 23:29:27 +0200159 cpumask_copy(vector_cpumask, cpumask_of(cpu));
Jiang Liu74afab72014-10-27 16:12:00 +0800160
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000161 /*
162 * Clear the offline cpus from @vector_cpumask for searching
163 * and verify whether the result overlaps with @mask. If true,
Thomas Gleixner91cd9cb2017-06-20 01:37:43 +0200164 * then the call to apic->cpu_mask_to_apicid() will
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000165 * succeed as well. If not, no point in trying to find a
166 * vector in this mask.
167 */
168 cpumask_and(vector_searchmask, vector_cpumask, cpu_online_mask);
169 if (!cpumask_intersects(vector_searchmask, mask))
170 goto next_cpu;
171
Jiang Liuf7fa7ae2015-04-14 10:30:10 +0800172 if (cpumask_subset(vector_cpumask, d->domain)) {
Jiang Liuf7fa7ae2015-04-14 10:30:10 +0800173 if (cpumask_equal(vector_cpumask, d->domain))
Thomas Gleixner433cbd52015-12-31 16:30:46 +0000174 goto success;
Jiang Liu74afab72014-10-27 16:12:00 +0800175 /*
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000176 * Mark the cpus which are not longer in the mask for
177 * cleanup.
Jiang Liu74afab72014-10-27 16:12:00 +0800178 */
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000179 cpumask_andnot(d->old_domain, d->domain, vector_cpumask);
180 vector = d->cfg.vector;
181 goto update;
Jiang Liu74afab72014-10-27 16:12:00 +0800182 }
183
184 vector = current_vector;
185 offset = current_offset;
186next:
187 vector += 16;
Thomas Gleixner05161b92017-08-28 08:47:18 +0200188 if (vector >= FIRST_SYSTEM_VECTOR) {
Jiang Liu74afab72014-10-27 16:12:00 +0800189 offset = (offset + 1) % 16;
190 vector = FIRST_EXTERNAL_VECTOR + offset;
191 }
192
Thomas Gleixner95ffeb42015-12-31 16:30:47 +0000193 /* If the search wrapped around, try the next cpu */
194 if (unlikely(current_vector == vector))
195 goto next_cpu;
Jiang Liu74afab72014-10-27 16:12:00 +0800196
Thomas Gleixner7854f822017-09-13 23:29:26 +0200197 if (test_bit(vector, system_vectors))
Jiang Liu74afab72014-10-27 16:12:00 +0800198 goto next;
199
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000200 for_each_cpu(new_cpu, vector_searchmask) {
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000201 if (!IS_ERR_OR_NULL(per_cpu(vector_irq, new_cpu)[vector]))
Jiang Liu74afab72014-10-27 16:12:00 +0800202 goto next;
203 }
204 /* Found one! */
205 current_vector = vector;
206 current_offset = offset;
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000207 /* Schedule the old vector for cleanup on all cpus */
208 if (d->cfg.vector)
Jiang Liu7f3262e2015-04-14 10:30:03 +0800209 cpumask_copy(d->old_domain, d->domain);
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000210 for_each_cpu(new_cpu, vector_searchmask)
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000211 per_cpu(vector_irq, new_cpu)[vector] = irq_to_desc(irq);
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000212 goto update;
Thomas Gleixner95ffeb42015-12-31 16:30:47 +0000213
214next_cpu:
215 /*
216 * We exclude the current @vector_cpumask from the requested
217 * @mask and try again with the next online cpu in the
218 * result. We cannot modify @mask, so we use @vector_cpumask
219 * as a temporary buffer here as it will be reassigned when
220 * calling apic->vector_allocation_domain() above.
221 */
222 cpumask_or(searched_cpumask, searched_cpumask, vector_cpumask);
223 cpumask_andnot(vector_cpumask, mask, searched_cpumask);
224 cpu = cpumask_first_and(vector_cpumask, cpu_online_mask);
225 continue;
Jiang Liu74afab72014-10-27 16:12:00 +0800226 }
Thomas Gleixner433cbd52015-12-31 16:30:46 +0000227 return -ENOSPC;
Jiang Liu74afab72014-10-27 16:12:00 +0800228
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000229update:
Thomas Gleixner847667e2015-12-31 16:30:50 +0000230 /*
231 * Exclude offline cpus from the cleanup mask and set the
232 * move_in_progress flag when the result is not empty.
233 */
234 cpumask_and(d->old_domain, d->old_domain, cpu_online_mask);
235 d->move_in_progress = !cpumask_empty(d->old_domain);
Thomas Gleixner551adc62016-03-14 09:40:46 +0100236 d->cfg.old_vector = d->move_in_progress ? d->cfg.vector : 0;
Thomas Gleixner029c6e12017-09-13 23:29:31 +0200237 d->prev_cpu = d->cpu;
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000238 d->cfg.vector = vector;
239 cpumask_copy(d->domain, vector_cpumask);
Thomas Gleixner433cbd52015-12-31 16:30:46 +0000240success:
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000241 /*
242 * Cache destination APIC IDs into cfg->dest_apicid. This cannot fail
243 * as we already established, that mask & d->domain & cpu_online_mask
244 * is not empty.
Thomas Gleixner52b166a2017-06-20 01:37:42 +0200245 *
246 * vector_searchmask is a subset of d->domain and has the offline
247 * cpus masked out.
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000248 */
Thomas Gleixner91cd9cb2017-06-20 01:37:43 +0200249 cpumask_and(vector_searchmask, vector_searchmask, mask);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200250 BUG_ON(apic->cpu_mask_to_apicid(vector_searchmask, irqd,
Thomas Gleixner0e24f7c2017-06-20 01:37:44 +0200251 &d->cfg.dest_apicid));
Thomas Gleixner029c6e12017-09-13 23:29:31 +0200252 d->cpu = cpumask_first(vector_searchmask);
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000253 return 0;
Jiang Liu74afab72014-10-27 16:12:00 +0800254}
255
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200256static int assign_irq_vector(int irq, struct apic_chip_data *apicd,
Thomas Gleixner0e24f7c2017-06-20 01:37:44 +0200257 const struct cpumask *mask,
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200258 struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +0800259{
260 int err;
261 unsigned long flags;
262
263 raw_spin_lock_irqsave(&vector_lock, flags);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200264 err = __assign_irq_vector(irq, apicd, mask, irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800265 raw_spin_unlock_irqrestore(&vector_lock, flags);
266 return err;
267}
268
Jiang Liu486ca532015-05-07 10:53:56 +0800269static int assign_irq_vector_policy(int irq, int node,
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200270 struct apic_chip_data *apicd,
Thomas Gleixner0e24f7c2017-06-20 01:37:44 +0200271 struct irq_alloc_info *info,
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200272 struct irq_data *irqd)
Jiang Liu486ca532015-05-07 10:53:56 +0800273{
Thomas Gleixner258d86e2017-09-13 23:29:35 +0200274 if (info->mask)
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200275 return assign_irq_vector(irq, apicd, info->mask, irqd);
Jiang Liu486ca532015-05-07 10:53:56 +0800276 if (node != NUMA_NO_NODE &&
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200277 assign_irq_vector(irq, apicd, cpumask_of_node(node), irqd) == 0)
Jiang Liu486ca532015-05-07 10:53:56 +0800278 return 0;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200279 return assign_irq_vector(irq, apicd, cpu_online_mask, irqd);
Jiang Liu486ca532015-05-07 10:53:56 +0800280}
281
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200282static void clear_irq_vector(int irq, struct apic_chip_data *apicd)
Jiang Liu74afab72014-10-27 16:12:00 +0800283{
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200284 unsigned int vector = apicd->cfg.vector;
Jiang Liu74afab72014-10-27 16:12:00 +0800285
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200286 if (!vector)
Keith Busch1bdb8972016-04-27 14:22:32 -0600287 return;
Jiang Liu74afab72014-10-27 16:12:00 +0800288
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200289 per_cpu(vector_irq, apicd->cpu)[vector] = VECTOR_UNUSED;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200290 apicd->cfg.vector = 0;
Jiang Liu74afab72014-10-27 16:12:00 +0800291
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200292 /* Clean up move in progress */
293 vector = apicd->cfg.old_vector;
294 if (!vector)
Jiang Liu74afab72014-10-27 16:12:00 +0800295 return;
Jiang Liu74afab72014-10-27 16:12:00 +0800296
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200297 per_cpu(vector_irq, apicd->prev_cpu)[vector] = VECTOR_UNUSED;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200298 apicd->move_in_progress = 0;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200299 hlist_del_init(&apicd->clist);
Jiang Liu74afab72014-10-27 16:12:00 +0800300}
301
Jiang Liub5dc8e62015-04-13 14:11:24 +0800302static void x86_vector_free_irqs(struct irq_domain *domain,
303 unsigned int virq, unsigned int nr_irqs)
304{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200305 struct apic_chip_data *apicd;
306 struct irq_data *irqd;
Jiang Liu111abeb2015-12-31 16:30:44 +0000307 unsigned long flags;
Jiang Liub5dc8e62015-04-13 14:11:24 +0800308 int i;
309
310 for (i = 0; i < nr_irqs; i++) {
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200311 irqd = irq_domain_get_irq_data(x86_vector_domain, virq + i);
312 if (irqd && irqd->chip_data) {
Jiang Liu111abeb2015-12-31 16:30:44 +0000313 raw_spin_lock_irqsave(&vector_lock, flags);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200314 clear_irq_vector(virq + i, irqd->chip_data);
315 apicd = irqd->chip_data;
316 irq_domain_reset_irq_data(irqd);
Jiang Liu111abeb2015-12-31 16:30:44 +0000317 raw_spin_unlock_irqrestore(&vector_lock, flags);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200318 free_apic_chip_data(apicd);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800319 }
320 }
321}
322
323static int x86_vector_alloc_irqs(struct irq_domain *domain, unsigned int virq,
324 unsigned int nr_irqs, void *arg)
325{
326 struct irq_alloc_info *info = arg;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200327 struct apic_chip_data *apicd;
328 struct irq_data *irqd;
Jiang Liu5f2dbbc2015-06-01 16:05:14 +0800329 int i, err, node;
Jiang Liub5dc8e62015-04-13 14:11:24 +0800330
331 if (disable_apic)
332 return -ENXIO;
333
334 /* Currently vector allocator can't guarantee contiguous allocations */
335 if ((info->flags & X86_IRQ_ALLOC_CONTIGUOUS_VECTORS) && nr_irqs > 1)
336 return -ENOSYS;
337
Jiang Liub5dc8e62015-04-13 14:11:24 +0800338 for (i = 0; i < nr_irqs; i++) {
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200339 irqd = irq_domain_get_irq_data(domain, virq + i);
340 BUG_ON(!irqd);
341 node = irq_data_get_node(irqd);
Thomas Gleixner4ef76eb2017-09-13 23:29:34 +0200342 WARN_ON_ONCE(irqd->chip_data);
343 apicd = alloc_apic_chip_data(node);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200344 if (!apicd) {
Jiang Liub5dc8e62015-04-13 14:11:24 +0800345 err = -ENOMEM;
346 goto error;
347 }
348
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200349 irqd->chip = &lapic_controller;
350 irqd->chip_data = apicd;
351 irqd->hwirq = virq + i;
352 irqd_set_single_target(irqd);
Thomas Gleixner4ef76eb2017-09-13 23:29:34 +0200353 /*
354 * Make sure, that the legacy to IOAPIC transition stays on
355 * the same vector. This is required for check_timer() to
356 * work correctly as it might switch back to legacy mode.
357 */
358 if (info->flags & X86_IRQ_ALLOC_LEGACY) {
359 apicd->cfg.vector = ISA_IRQ_VECTOR(virq + i);
360 apicd->cpu = 0;
361 cpumask_copy(apicd->domain, cpumask_of(0));
362 }
363
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200364 err = assign_irq_vector_policy(virq + i, node, apicd, info,
365 irqd);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800366 if (err)
367 goto error;
368 }
369
370 return 0;
371
372error:
373 x86_vector_free_irqs(domain, virq, i + 1);
374 return err;
375}
376
Thomas Gleixner65d7ed52017-09-13 23:29:39 +0200377#ifdef CONFIG_GENERIC_IRQ_DEBUGFS
378void x86_vector_debug_show(struct seq_file *m, struct irq_domain *d,
379 struct irq_data *irqd, int ind)
380{
381 unsigned int cpu, vec, prev_cpu, prev_vec;
382 struct apic_chip_data *apicd;
383 unsigned long flags;
384 int irq;
385
386 if (!irqd) {
387 irq_matrix_debug_show(m, vector_matrix, ind);
388 return;
389 }
390
391 irq = irqd->irq;
392 if (irq < nr_legacy_irqs() && !test_bit(irq, &io_apic_irqs)) {
393 seq_printf(m, "%*sVector: %5d\n", ind, "", ISA_IRQ_VECTOR(irq));
394 seq_printf(m, "%*sTarget: Legacy PIC all CPUs\n", ind, "");
395 return;
396 }
397
398 apicd = irqd->chip_data;
399 if (!apicd) {
400 seq_printf(m, "%*sVector: Not assigned\n", ind, "");
401 return;
402 }
403
404 raw_spin_lock_irqsave(&vector_lock, flags);
405 cpu = apicd->cpu;
406 vec = apicd->cfg.vector;
407 prev_cpu = apicd->prev_cpu;
408 prev_vec = apicd->cfg.old_vector;
409 raw_spin_unlock_irqrestore(&vector_lock, flags);
410 seq_printf(m, "%*sVector: %5u\n", ind, "", vec);
411 seq_printf(m, "%*sTarget: %5u\n", ind, "", cpu);
412 if (prev_vec) {
413 seq_printf(m, "%*sPrevious vector: %5u\n", ind, "", prev_vec);
414 seq_printf(m, "%*sPrevious target: %5u\n", ind, "", prev_cpu);
415 }
416}
417#endif
418
Thomas Gleixnereb18cf52015-05-05 11:10:11 +0200419static const struct irq_domain_ops x86_vector_domain_ops = {
Thomas Gleixner65d7ed52017-09-13 23:29:39 +0200420 .alloc = x86_vector_alloc_irqs,
421 .free = x86_vector_free_irqs,
422#ifdef CONFIG_GENERIC_IRQ_DEBUGFS
423 .debug_show = x86_vector_debug_show,
424#endif
Jiang Liub5dc8e62015-04-13 14:11:24 +0800425};
426
Jiang Liu11d686e2014-10-27 16:12:05 +0800427int __init arch_probe_nr_irqs(void)
428{
429 int nr;
430
431 if (nr_irqs > (NR_VECTORS * nr_cpu_ids))
432 nr_irqs = NR_VECTORS * nr_cpu_ids;
433
434 nr = (gsi_top + nr_legacy_irqs()) + 8 * nr_cpu_ids;
435#if defined(CONFIG_PCI_MSI) || defined(CONFIG_HT_IRQ)
436 /*
437 * for MSI and HT dyn irq
438 */
439 if (gsi_top <= NR_IRQS_LEGACY)
440 nr += 8 * nr_cpu_ids;
441 else
442 nr += gsi_top * 16;
443#endif
444 if (nr < nr_irqs)
445 nr_irqs = nr;
446
Vitaly Kuznetsov8c058b02015-11-03 10:40:14 +0100447 /*
448 * We don't know if PIC is present at this point so we need to do
449 * probe() to get the right number of legacy IRQs.
450 */
451 return legacy_pic->probe();
Jiang Liu11d686e2014-10-27 16:12:05 +0800452}
453
Thomas Gleixner0fa115d2017-09-13 23:29:38 +0200454void lapic_assign_legacy_vector(unsigned int irq, bool replace)
455{
456 /*
457 * Use assign system here so it wont get accounted as allocated
458 * and moveable in the cpu hotplug check and it prevents managed
459 * irq reservation from touching it.
460 */
461 irq_matrix_assign_system(vector_matrix, ISA_IRQ_VECTOR(irq), replace);
462}
463
464void __init lapic_assign_system_vectors(void)
465{
466 unsigned int i, vector = 0;
467
468 for_each_set_bit_from(vector, system_vectors, NR_VECTORS)
469 irq_matrix_assign_system(vector_matrix, vector, false);
470
471 if (nr_legacy_irqs() > 1)
472 lapic_assign_legacy_vector(PIC_CASCADE_IR, false);
473
474 /* System vectors are reserved, online it */
475 irq_matrix_online(vector_matrix);
476
477 /* Mark the preallocated legacy interrupts */
478 for (i = 0; i < nr_legacy_irqs(); i++) {
479 if (i != PIC_CASCADE_IR)
480 irq_matrix_assign(vector_matrix, ISA_IRQ_VECTOR(i));
481 }
482}
483
Jiang Liu11d686e2014-10-27 16:12:05 +0800484int __init arch_early_irq_init(void)
485{
Thomas Gleixner9d35f852017-06-20 01:37:06 +0200486 struct fwnode_handle *fn;
487
Thomas Gleixner9d35f852017-06-20 01:37:06 +0200488 fn = irq_domain_alloc_named_fwnode("VECTOR");
489 BUG_ON(!fn);
490 x86_vector_domain = irq_domain_create_tree(fn, &x86_vector_domain_ops,
491 NULL);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800492 BUG_ON(x86_vector_domain == NULL);
Thomas Gleixner9d35f852017-06-20 01:37:06 +0200493 irq_domain_free_fwnode(fn);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800494 irq_set_default_host(x86_vector_domain);
495
Jiang Liu52f518a2015-04-13 14:11:35 +0800496 arch_init_msi_domain(x86_vector_domain);
Jiang Liu49e07d82015-04-13 14:11:43 +0800497 arch_init_htirq_domain(x86_vector_domain);
Jiang Liu52f518a2015-04-13 14:11:35 +0800498
Jiang Liuf7fa7ae2015-04-14 10:30:10 +0800499 BUG_ON(!alloc_cpumask_var(&vector_cpumask, GFP_KERNEL));
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000500 BUG_ON(!alloc_cpumask_var(&vector_searchmask, GFP_KERNEL));
Jiang Liu8a580f72015-12-31 16:30:46 +0000501 BUG_ON(!alloc_cpumask_var(&searched_cpumask, GFP_KERNEL));
Jiang Liuf7fa7ae2015-04-14 10:30:10 +0800502
Thomas Gleixner0fa115d2017-09-13 23:29:38 +0200503 /*
504 * Allocate the vector matrix allocator data structure and limit the
505 * search area.
506 */
507 vector_matrix = irq_alloc_matrix(NR_VECTORS, FIRST_EXTERNAL_VECTOR,
508 FIRST_SYSTEM_VECTOR);
509 BUG_ON(!vector_matrix);
510
Jiang Liu11d686e2014-10-27 16:12:05 +0800511 return arch_early_ioapic_init();
512}
513
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200514/* Temporary hack to keep things working */
515static void vector_update_shutdown_irqs(void)
Jiang Liu74afab72014-10-27 16:12:00 +0800516{
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000517 struct irq_desc *desc;
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200518 int irq;
Jiang Liu74afab72014-10-27 16:12:00 +0800519
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000520 for_each_irq_desc(irq, desc) {
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200521 struct irq_data *irqd = irq_desc_get_irq_data(desc);
522 struct apic_chip_data *ad = apic_chip_data(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800523
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200524 if (ad && ad->cfg.vector && ad->cpu == smp_processor_id())
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200525 this_cpu_write(vector_irq[ad->cfg.vector], desc);
Jiang Liu74afab72014-10-27 16:12:00 +0800526 }
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200527}
Jiang Liu74afab72014-10-27 16:12:00 +0800528
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200529static struct irq_desc *__setup_vector_irq(int vector)
530{
531 int isairq = vector - ISA_IRQ_VECTOR(0);
532
533 /* Check whether the irq is in the legacy space */
534 if (isairq < 0 || isairq >= nr_legacy_irqs())
535 return VECTOR_UNUSED;
536 /* Check whether the irq is handled by the IOAPIC */
537 if (test_bit(isairq, &io_apic_irqs))
538 return VECTOR_UNUSED;
539 return irq_to_desc(isairq);
Jiang Liu74afab72014-10-27 16:12:00 +0800540}
541
Thomas Gleixner0fa115d2017-09-13 23:29:38 +0200542/* Online the local APIC infrastructure and initialize the vectors */
543void lapic_online(void)
Jiang Liu74afab72014-10-27 16:12:00 +0800544{
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200545 unsigned int vector;
Jiang Liu74afab72014-10-27 16:12:00 +0800546
Thomas Gleixner5a3f75e2015-07-05 17:12:32 +0000547 lockdep_assert_held(&vector_lock);
Thomas Gleixner0fa115d2017-09-13 23:29:38 +0200548
549 /* Online the vector matrix array for this CPU */
550 irq_matrix_online(vector_matrix);
551
Jiang Liu74afab72014-10-27 16:12:00 +0800552 /*
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200553 * The interrupt affinity logic never targets interrupts to offline
554 * CPUs. The exception are the legacy PIC interrupts. In general
555 * they are only targeted to CPU0, but depending on the platform
556 * they can be distributed to any online CPU in hardware. The
557 * kernel has no influence on that. So all active legacy vectors
558 * must be installed on all CPUs. All non legacy interrupts can be
559 * cleared.
Jiang Liu74afab72014-10-27 16:12:00 +0800560 */
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200561 for (vector = 0; vector < NR_VECTORS; vector++)
562 this_cpu_write(vector_irq[vector], __setup_vector_irq(vector));
Jiang Liu74afab72014-10-27 16:12:00 +0800563
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200564 /*
565 * Until the rewrite of the managed interrupt management is in
566 * place it's necessary to walk the irq descriptors and check for
567 * interrupts which are targeted at this CPU.
568 */
569 vector_update_shutdown_irqs();
Jiang Liu74afab72014-10-27 16:12:00 +0800570}
571
Thomas Gleixner0fa115d2017-09-13 23:29:38 +0200572void lapic_offline(void)
573{
574 lock_vector_lock();
575 irq_matrix_offline(vector_matrix);
576 unlock_vector_lock();
577}
578
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200579static int apic_retrigger_irq(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +0800580{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200581 struct apic_chip_data *apicd = apic_chip_data(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800582 unsigned long flags;
Jiang Liu74afab72014-10-27 16:12:00 +0800583
584 raw_spin_lock_irqsave(&vector_lock, flags);
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200585 apic->send_IPI(apicd->cpu, apicd->cfg.vector);
Jiang Liu74afab72014-10-27 16:12:00 +0800586 raw_spin_unlock_irqrestore(&vector_lock, flags);
587
588 return 1;
589}
590
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200591void apic_ack_edge(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +0800592{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200593 irq_complete_move(irqd_cfg(irqd));
594 irq_move_irq(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800595 ack_APIC_irq();
596}
597
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200598static int apic_set_affinity(struct irq_data *irqd,
Jiang Liu68f9f442015-04-14 10:30:01 +0800599 const struct cpumask *dest, bool force)
Jiang Liub5dc8e62015-04-13 14:11:24 +0800600{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200601 struct apic_chip_data *apicd = irqd->chip_data;
602 int err, irq = irqd->irq;
Jiang Liub5dc8e62015-04-13 14:11:24 +0800603
Masahiro Yamada97f26452016-08-03 13:45:50 -0700604 if (!IS_ENABLED(CONFIG_SMP))
Jiang Liub5dc8e62015-04-13 14:11:24 +0800605 return -EPERM;
606
607 if (!cpumask_intersects(dest, cpu_online_mask))
608 return -EINVAL;
609
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200610 err = assign_irq_vector(irq, apicd, dest, irqd);
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000611 return err ? err : IRQ_SET_MASK_OK;
Jiang Liub5dc8e62015-04-13 14:11:24 +0800612}
613
614static struct irq_chip lapic_controller = {
Thomas Gleixner8947dfb2017-06-20 01:37:01 +0200615 .name = "APIC",
Jiang Liub5dc8e62015-04-13 14:11:24 +0800616 .irq_ack = apic_ack_edge,
Jiang Liu68f9f442015-04-14 10:30:01 +0800617 .irq_set_affinity = apic_set_affinity,
Jiang Liub5dc8e62015-04-13 14:11:24 +0800618 .irq_retrigger = apic_retrigger_irq,
619};
620
Jiang Liu74afab72014-10-27 16:12:00 +0800621#ifdef CONFIG_SMP
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200622
623asmlinkage __visible void __irq_entry smp_irq_move_cleanup_interrupt(void)
624{
625 struct hlist_head *clhead = this_cpu_ptr(&cleanup_list);
626 struct apic_chip_data *apicd;
627 struct hlist_node *tmp;
628
629 entering_ack_irq();
630 /* Prevent vectors vanishing under us */
631 raw_spin_lock(&vector_lock);
632
633 hlist_for_each_entry_safe(apicd, tmp, clhead, clist) {
634 unsigned int irr, vector = apicd->cfg.old_vector;
635
636 /*
637 * Paranoia: Check if the vector that needs to be cleaned
638 * up is registered at the APICs IRR. If so, then this is
639 * not the best time to clean it up. Clean it up in the
640 * next attempt by sending another IRQ_MOVE_CLEANUP_VECTOR
641 * to this CPU. IRQ_MOVE_CLEANUP_VECTOR is the lowest
642 * priority external vector, so on return from this
643 * interrupt the device interrupt will happen first.
644 */
645 irr = apic_read(APIC_IRR + (vector / 32 * 0x10));
646 if (irr & (1U << (vector % 32))) {
647 apic->send_IPI_self(IRQ_MOVE_CLEANUP_VECTOR);
648 continue;
649 }
650 hlist_del_init(&apicd->clist);
651 __this_cpu_write(vector_irq[vector], VECTOR_UNUSED);
652 apicd->cfg.old_vector = 0;
653 }
654
655 raw_spin_unlock(&vector_lock);
656 exiting_irq();
657}
658
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200659static void __send_cleanup_vector(struct apic_chip_data *apicd)
Jiang Liu74afab72014-10-27 16:12:00 +0800660{
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200661 unsigned int cpu;
662
Thomas Gleixnerc1684f52015-12-31 16:30:51 +0000663 raw_spin_lock(&vector_lock);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200664 apicd->move_in_progress = 0;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200665 cpu = apicd->prev_cpu;
666 if (cpu_online(cpu)) {
667 hlist_add_head(&apicd->clist, per_cpu_ptr(&cleanup_list, cpu));
668 apic->send_IPI(cpu, IRQ_MOVE_CLEANUP_VECTOR);
669 } else {
670 apicd->cfg.old_vector = 0;
671 }
Thomas Gleixnerc1684f52015-12-31 16:30:51 +0000672 raw_spin_unlock(&vector_lock);
Jiang Liu74afab72014-10-27 16:12:00 +0800673}
674
Jiang Liuc6c20022015-04-14 10:30:02 +0800675void send_cleanup_vector(struct irq_cfg *cfg)
676{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200677 struct apic_chip_data *apicd;
Jiang Liu7f3262e2015-04-14 10:30:03 +0800678
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200679 apicd = container_of(cfg, struct apic_chip_data, cfg);
680 if (apicd->move_in_progress)
681 __send_cleanup_vector(apicd);
Jiang Liuc6c20022015-04-14 10:30:02 +0800682}
683
Jiang Liu74afab72014-10-27 16:12:00 +0800684static void __irq_complete_move(struct irq_cfg *cfg, unsigned vector)
685{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200686 struct apic_chip_data *apicd;
Jiang Liu74afab72014-10-27 16:12:00 +0800687
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200688 apicd = container_of(cfg, struct apic_chip_data, cfg);
689 if (likely(!apicd->move_in_progress))
Jiang Liu74afab72014-10-27 16:12:00 +0800690 return;
691
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200692 if (vector == apicd->cfg.vector && apicd->cpu == smp_processor_id())
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200693 __send_cleanup_vector(apicd);
Jiang Liu74afab72014-10-27 16:12:00 +0800694}
695
696void irq_complete_move(struct irq_cfg *cfg)
697{
698 __irq_complete_move(cfg, ~get_irq_regs()->orig_ax);
699}
700
Thomas Gleixner90a22822015-12-31 16:30:53 +0000701/*
Thomas Gleixner551adc62016-03-14 09:40:46 +0100702 * Called from fixup_irqs() with @desc->lock held and interrupts disabled.
Thomas Gleixner90a22822015-12-31 16:30:53 +0000703 */
704void irq_force_complete_move(struct irq_desc *desc)
Jiang Liu74afab72014-10-27 16:12:00 +0800705{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200706 struct apic_chip_data *apicd;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200707 struct irq_data *irqd;
708 unsigned int vector;
Jiang Liu74afab72014-10-27 16:12:00 +0800709
Mika Westerbergdb91aa72016-10-03 13:17:08 +0300710 /*
711 * The function is called for all descriptors regardless of which
712 * irqdomain they belong to. For example if an IRQ is provided by
713 * an irq_chip as part of a GPIO driver, the chip data for that
714 * descriptor is specific to the irq_chip in question.
715 *
716 * Check first that the chip_data is what we expect
717 * (apic_chip_data) before touching it any further.
718 */
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200719 irqd = irq_domain_get_irq_data(x86_vector_domain,
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200720 irq_desc_get_irq(desc));
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200721 if (!irqd)
Mika Westerbergdb91aa72016-10-03 13:17:08 +0300722 return;
723
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200724 raw_spin_lock(&vector_lock);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200725 apicd = apic_chip_data(irqd);
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200726 if (!apicd)
727 goto unlock;
Thomas Gleixner56d7d2f2015-12-31 16:30:52 +0000728
Thomas Gleixner56d7d2f2015-12-31 16:30:52 +0000729 /*
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200730 * If old_vector is empty, no action required.
731 */
732 vector = apicd->cfg.old_vector;
733 if (!vector)
734 goto unlock;
735
736 /*
737 * This is tricky. If the cleanup of the old vector has not been
Thomas Gleixner98229aa2015-12-31 16:30:54 +0000738 * done yet, then the following setaffinity call will fail with
739 * -EBUSY. This can leave the interrupt in a stale state.
740 *
Thomas Gleixner551adc62016-03-14 09:40:46 +0100741 * All CPUs are stuck in stop machine with interrupts disabled so
742 * calling __irq_complete_move() would be completely pointless.
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200743 *
Thomas Gleixner551adc62016-03-14 09:40:46 +0100744 * 1) The interrupt is in move_in_progress state. That means that we
745 * have not seen an interrupt since the io_apic was reprogrammed to
746 * the new vector.
747 *
748 * 2) The interrupt has fired on the new vector, but the cleanup IPIs
749 * have not been processed yet.
750 */
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200751 if (apicd->move_in_progress) {
Thomas Gleixner551adc62016-03-14 09:40:46 +0100752 /*
753 * In theory there is a race:
754 *
755 * set_ioapic(new_vector) <-- Interrupt is raised before update
756 * is effective, i.e. it's raised on
757 * the old vector.
758 *
759 * So if the target cpu cannot handle that interrupt before
760 * the old vector is cleaned up, we get a spurious interrupt
761 * and in the worst case the ioapic irq line becomes stale.
762 *
763 * But in case of cpu hotplug this should be a non issue
764 * because if the affinity update happens right before all
765 * cpus rendevouz in stop machine, there is no way that the
766 * interrupt can be blocked on the target cpu because all cpus
767 * loops first with interrupts enabled in stop machine, so the
768 * old vector is not yet cleaned up when the interrupt fires.
769 *
770 * So the only way to run into this issue is if the delivery
771 * of the interrupt on the apic/system bus would be delayed
772 * beyond the point where the target cpu disables interrupts
773 * in stop machine. I doubt that it can happen, but at least
774 * there is a theroretical chance. Virtualization might be
775 * able to expose this, but AFAICT the IOAPIC emulation is not
776 * as stupid as the real hardware.
777 *
778 * Anyway, there is nothing we can do about that at this point
779 * w/o refactoring the whole fixup_irq() business completely.
780 * We print at least the irq number and the old vector number,
781 * so we have the necessary information when a problem in that
782 * area arises.
783 */
784 pr_warn("IRQ fixup: irq %d move in progress, old vector %d\n",
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200785 irqd->irq, vector);
Thomas Gleixner551adc62016-03-14 09:40:46 +0100786 }
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200787 per_cpu(vector_irq, apicd->prev_cpu)[vector] = VECTOR_UNUSED;
Thomas Gleixner551adc62016-03-14 09:40:46 +0100788 /* Cleanup the left overs of the (half finished) move */
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200789 cpumask_clear(apicd->old_domain);
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200790 apicd->cfg.old_vector = 0;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200791 apicd->move_in_progress = 0;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200792 hlist_del_init(&apicd->clist);
793unlock:
Thomas Gleixner56d7d2f2015-12-31 16:30:52 +0000794 raw_spin_unlock(&vector_lock);
Jiang Liu74afab72014-10-27 16:12:00 +0800795}
Jiang Liu74afab72014-10-27 16:12:00 +0800796#endif
797
Jiang Liu74afab72014-10-27 16:12:00 +0800798static void __init print_APIC_field(int base)
799{
800 int i;
801
802 printk(KERN_DEBUG);
803
804 for (i = 0; i < 8; i++)
805 pr_cont("%08x", apic_read(base + i*0x10));
806
807 pr_cont("\n");
808}
809
810static void __init print_local_APIC(void *dummy)
811{
812 unsigned int i, v, ver, maxlvt;
813 u64 icr;
814
Jiang Liu849d3562014-10-27 16:12:01 +0800815 pr_debug("printing local APIC contents on CPU#%d/%d:\n",
816 smp_processor_id(), hard_smp_processor_id());
Jiang Liu74afab72014-10-27 16:12:00 +0800817 v = apic_read(APIC_ID);
Jiang Liu849d3562014-10-27 16:12:01 +0800818 pr_info("... APIC ID: %08x (%01x)\n", v, read_apic_id());
Jiang Liu74afab72014-10-27 16:12:00 +0800819 v = apic_read(APIC_LVR);
Jiang Liu849d3562014-10-27 16:12:01 +0800820 pr_info("... APIC VERSION: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800821 ver = GET_APIC_VERSION(v);
822 maxlvt = lapic_get_maxlvt();
823
824 v = apic_read(APIC_TASKPRI);
Jiang Liu849d3562014-10-27 16:12:01 +0800825 pr_debug("... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
Jiang Liu74afab72014-10-27 16:12:00 +0800826
827 /* !82489DX */
828 if (APIC_INTEGRATED(ver)) {
829 if (!APIC_XAPIC(ver)) {
830 v = apic_read(APIC_ARBPRI);
Jiang Liu849d3562014-10-27 16:12:01 +0800831 pr_debug("... APIC ARBPRI: %08x (%02x)\n",
832 v, v & APIC_ARBPRI_MASK);
Jiang Liu74afab72014-10-27 16:12:00 +0800833 }
834 v = apic_read(APIC_PROCPRI);
Jiang Liu849d3562014-10-27 16:12:01 +0800835 pr_debug("... APIC PROCPRI: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800836 }
837
838 /*
839 * Remote read supported only in the 82489DX and local APIC for
840 * Pentium processors.
841 */
842 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
843 v = apic_read(APIC_RRR);
Jiang Liu849d3562014-10-27 16:12:01 +0800844 pr_debug("... APIC RRR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800845 }
846
847 v = apic_read(APIC_LDR);
Jiang Liu849d3562014-10-27 16:12:01 +0800848 pr_debug("... APIC LDR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800849 if (!x2apic_enabled()) {
850 v = apic_read(APIC_DFR);
Jiang Liu849d3562014-10-27 16:12:01 +0800851 pr_debug("... APIC DFR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800852 }
853 v = apic_read(APIC_SPIV);
Jiang Liu849d3562014-10-27 16:12:01 +0800854 pr_debug("... APIC SPIV: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800855
Jiang Liu849d3562014-10-27 16:12:01 +0800856 pr_debug("... APIC ISR field:\n");
Jiang Liu74afab72014-10-27 16:12:00 +0800857 print_APIC_field(APIC_ISR);
Jiang Liu849d3562014-10-27 16:12:01 +0800858 pr_debug("... APIC TMR field:\n");
Jiang Liu74afab72014-10-27 16:12:00 +0800859 print_APIC_field(APIC_TMR);
Jiang Liu849d3562014-10-27 16:12:01 +0800860 pr_debug("... APIC IRR field:\n");
Jiang Liu74afab72014-10-27 16:12:00 +0800861 print_APIC_field(APIC_IRR);
862
863 /* !82489DX */
864 if (APIC_INTEGRATED(ver)) {
865 /* Due to the Pentium erratum 3AP. */
866 if (maxlvt > 3)
867 apic_write(APIC_ESR, 0);
868
869 v = apic_read(APIC_ESR);
Jiang Liu849d3562014-10-27 16:12:01 +0800870 pr_debug("... APIC ESR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800871 }
872
873 icr = apic_icr_read();
Jiang Liu849d3562014-10-27 16:12:01 +0800874 pr_debug("... APIC ICR: %08x\n", (u32)icr);
875 pr_debug("... APIC ICR2: %08x\n", (u32)(icr >> 32));
Jiang Liu74afab72014-10-27 16:12:00 +0800876
877 v = apic_read(APIC_LVTT);
Jiang Liu849d3562014-10-27 16:12:01 +0800878 pr_debug("... APIC LVTT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800879
880 if (maxlvt > 3) {
881 /* PC is LVT#4. */
882 v = apic_read(APIC_LVTPC);
Jiang Liu849d3562014-10-27 16:12:01 +0800883 pr_debug("... APIC LVTPC: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800884 }
885 v = apic_read(APIC_LVT0);
Jiang Liu849d3562014-10-27 16:12:01 +0800886 pr_debug("... APIC LVT0: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800887 v = apic_read(APIC_LVT1);
Jiang Liu849d3562014-10-27 16:12:01 +0800888 pr_debug("... APIC LVT1: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800889
890 if (maxlvt > 2) {
891 /* ERR is LVT#3. */
892 v = apic_read(APIC_LVTERR);
Jiang Liu849d3562014-10-27 16:12:01 +0800893 pr_debug("... APIC LVTERR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800894 }
895
896 v = apic_read(APIC_TMICT);
Jiang Liu849d3562014-10-27 16:12:01 +0800897 pr_debug("... APIC TMICT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800898 v = apic_read(APIC_TMCCT);
Jiang Liu849d3562014-10-27 16:12:01 +0800899 pr_debug("... APIC TMCCT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800900 v = apic_read(APIC_TDCR);
Jiang Liu849d3562014-10-27 16:12:01 +0800901 pr_debug("... APIC TDCR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800902
903 if (boot_cpu_has(X86_FEATURE_EXTAPIC)) {
904 v = apic_read(APIC_EFEAT);
905 maxlvt = (v >> 16) & 0xff;
Jiang Liu849d3562014-10-27 16:12:01 +0800906 pr_debug("... APIC EFEAT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800907 v = apic_read(APIC_ECTRL);
Jiang Liu849d3562014-10-27 16:12:01 +0800908 pr_debug("... APIC ECTRL: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800909 for (i = 0; i < maxlvt; i++) {
910 v = apic_read(APIC_EILVTn(i));
Jiang Liu849d3562014-10-27 16:12:01 +0800911 pr_debug("... APIC EILVT%d: %08x\n", i, v);
Jiang Liu74afab72014-10-27 16:12:00 +0800912 }
913 }
914 pr_cont("\n");
915}
916
917static void __init print_local_APICs(int maxcpu)
918{
919 int cpu;
920
921 if (!maxcpu)
922 return;
923
924 preempt_disable();
925 for_each_online_cpu(cpu) {
926 if (cpu >= maxcpu)
927 break;
928 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
929 }
930 preempt_enable();
931}
932
933static void __init print_PIC(void)
934{
935 unsigned int v;
936 unsigned long flags;
937
938 if (!nr_legacy_irqs())
939 return;
940
Jiang Liu849d3562014-10-27 16:12:01 +0800941 pr_debug("\nprinting PIC contents\n");
Jiang Liu74afab72014-10-27 16:12:00 +0800942
943 raw_spin_lock_irqsave(&i8259A_lock, flags);
944
945 v = inb(0xa1) << 8 | inb(0x21);
Jiang Liu849d3562014-10-27 16:12:01 +0800946 pr_debug("... PIC IMR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800947
948 v = inb(0xa0) << 8 | inb(0x20);
Jiang Liu849d3562014-10-27 16:12:01 +0800949 pr_debug("... PIC IRR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800950
951 outb(0x0b, 0xa0);
952 outb(0x0b, 0x20);
953 v = inb(0xa0) << 8 | inb(0x20);
954 outb(0x0a, 0xa0);
955 outb(0x0a, 0x20);
956
957 raw_spin_unlock_irqrestore(&i8259A_lock, flags);
958
Jiang Liu849d3562014-10-27 16:12:01 +0800959 pr_debug("... PIC ISR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800960
961 v = inb(0x4d1) << 8 | inb(0x4d0);
Jiang Liu849d3562014-10-27 16:12:01 +0800962 pr_debug("... PIC ELCR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800963}
964
965static int show_lapic __initdata = 1;
966static __init int setup_show_lapic(char *arg)
967{
968 int num = -1;
969
970 if (strcmp(arg, "all") == 0) {
971 show_lapic = CONFIG_NR_CPUS;
972 } else {
973 get_option(&arg, &num);
974 if (num >= 0)
975 show_lapic = num;
976 }
977
978 return 1;
979}
980__setup("show_lapic=", setup_show_lapic);
981
982static int __init print_ICs(void)
983{
984 if (apic_verbosity == APIC_QUIET)
985 return 0;
986
987 print_PIC();
988
989 /* don't print out if apic is not there */
Borislav Petkov93984fb2016-04-04 22:25:00 +0200990 if (!boot_cpu_has(X86_FEATURE_APIC) && !apic_from_smp_config())
Jiang Liu74afab72014-10-27 16:12:00 +0800991 return 0;
992
993 print_local_APICs(show_lapic);
994 print_IO_APICs();
995
996 return 0;
997}
998
999late_initcall(print_ICs);