blob: d6feb9ca8f52ff24ffd06eac586bbff7fe44bba5 [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;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +020028 struct hlist_node clist;
Jiang Liu7f3262e2015-04-14 10:30:03 +080029 cpumask_var_t domain;
30 cpumask_var_t old_domain;
31 u8 move_in_progress : 1;
32};
33
Jiang Liub5dc8e62015-04-13 14:11:24 +080034struct irq_domain *x86_vector_domain;
Jake Oshinsc8f3e512015-12-10 17:52:59 +000035EXPORT_SYMBOL_GPL(x86_vector_domain);
Jiang Liu74afab72014-10-27 16:12:00 +080036static DEFINE_RAW_SPINLOCK(vector_lock);
Thomas Gleixner3716fd22015-12-31 16:30:48 +000037static cpumask_var_t vector_cpumask, vector_searchmask, searched_cpumask;
Jiang Liub5dc8e62015-04-13 14:11:24 +080038static struct irq_chip lapic_controller;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +020039#ifdef CONFIG_SMP
40static DEFINE_PER_CPU(struct hlist_head, cleanup_list);
41#endif
Jiang Liu74afab72014-10-27 16:12:00 +080042
43void lock_vector_lock(void)
44{
45 /* Used to the online set of cpus does not change
46 * during assign_irq_vector.
47 */
48 raw_spin_lock(&vector_lock);
49}
50
51void unlock_vector_lock(void)
52{
53 raw_spin_unlock(&vector_lock);
54}
55
Thomas Gleixner86ba6552017-09-13 23:29:30 +020056static struct apic_chip_data *apic_chip_data(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +080057{
Thomas Gleixner86ba6552017-09-13 23:29:30 +020058 if (!irqd)
Jiang Liub5dc8e62015-04-13 14:11:24 +080059 return NULL;
60
Thomas Gleixner86ba6552017-09-13 23:29:30 +020061 while (irqd->parent_data)
62 irqd = irqd->parent_data;
Jiang Liub5dc8e62015-04-13 14:11:24 +080063
Thomas Gleixner86ba6552017-09-13 23:29:30 +020064 return irqd->chip_data;
Jiang Liu74afab72014-10-27 16:12:00 +080065}
66
Thomas Gleixner86ba6552017-09-13 23:29:30 +020067struct irq_cfg *irqd_cfg(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +080068{
Thomas Gleixner86ba6552017-09-13 23:29:30 +020069 struct apic_chip_data *apicd = apic_chip_data(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +080070
Thomas Gleixner86ba6552017-09-13 23:29:30 +020071 return apicd ? &apicd->cfg : NULL;
Jiang Liu7f3262e2015-04-14 10:30:03 +080072}
Jake Oshinsc8f3e512015-12-10 17:52:59 +000073EXPORT_SYMBOL_GPL(irqd_cfg);
Jiang Liu7f3262e2015-04-14 10:30:03 +080074
75struct irq_cfg *irq_cfg(unsigned int irq)
76{
77 return irqd_cfg(irq_get_irq_data(irq));
78}
79
80static struct apic_chip_data *alloc_apic_chip_data(int node)
81{
Thomas Gleixner86ba6552017-09-13 23:29:30 +020082 struct apic_chip_data *apicd;
Jiang Liu7f3262e2015-04-14 10:30:03 +080083
Thomas Gleixner86ba6552017-09-13 23:29:30 +020084 apicd = kzalloc_node(sizeof(*apicd), GFP_KERNEL, node);
85 if (!apicd)
Jiang Liu74afab72014-10-27 16:12:00 +080086 return NULL;
Thomas Gleixner86ba6552017-09-13 23:29:30 +020087 if (!zalloc_cpumask_var_node(&apicd->domain, GFP_KERNEL, node))
Jiang Liu7f3262e2015-04-14 10:30:03 +080088 goto out_data;
Thomas Gleixner86ba6552017-09-13 23:29:30 +020089 if (!zalloc_cpumask_var_node(&apicd->old_domain, GFP_KERNEL, node))
Jiang Liu74afab72014-10-27 16:12:00 +080090 goto out_domain;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +020091 INIT_HLIST_NODE(&apicd->clist);
Thomas Gleixner86ba6552017-09-13 23:29:30 +020092 return apicd;
Jiang Liu74afab72014-10-27 16:12:00 +080093out_domain:
Thomas Gleixner86ba6552017-09-13 23:29:30 +020094 free_cpumask_var(apicd->domain);
Jiang Liu7f3262e2015-04-14 10:30:03 +080095out_data:
Thomas Gleixner86ba6552017-09-13 23:29:30 +020096 kfree(apicd);
Jiang Liu74afab72014-10-27 16:12:00 +080097 return NULL;
98}
99
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200100static void free_apic_chip_data(struct apic_chip_data *apicd)
Jiang Liu74afab72014-10-27 16:12:00 +0800101{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200102 if (apicd) {
103 free_cpumask_var(apicd->domain);
104 free_cpumask_var(apicd->old_domain);
105 kfree(apicd);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800106 }
Jiang Liu74afab72014-10-27 16:12:00 +0800107}
108
Jiang Liu7f3262e2015-04-14 10:30:03 +0800109static int __assign_irq_vector(int irq, struct apic_chip_data *d,
Thomas Gleixner0e24f7c2017-06-20 01:37:44 +0200110 const struct cpumask *mask,
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200111 struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +0800112{
113 /*
114 * NOTE! The local APIC isn't very good at handling
115 * multiple interrupts at the same interrupt level.
116 * As the interrupt level is determined by taking the
117 * vector number and shifting that right by 4, we
118 * want to spread these out a bit so that they don't
119 * all fall in the same interrupt level.
120 *
121 * Also, we've got to be careful not to trash gate
122 * 0x80, because int 0x80 is hm, kind of importantish. ;)
123 */
124 static int current_vector = FIRST_EXTERNAL_VECTOR + VECTOR_OFFSET_START;
125 static int current_offset = VECTOR_OFFSET_START % 16;
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000126 int cpu, vector;
Jiang Liu74afab72014-10-27 16:12:00 +0800127
Thomas Gleixner98229aa2015-12-31 16:30:54 +0000128 /*
129 * If there is still a move in progress or the previous move has not
130 * been cleaned up completely, tell the caller to come back later.
131 */
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200132 if (d->cfg.old_vector)
Jiang Liu74afab72014-10-27 16:12:00 +0800133 return -EBUSY;
134
Jiang Liu74afab72014-10-27 16:12:00 +0800135 /* Only try and allocate irqs on cpus that are present */
Jiang Liu7f3262e2015-04-14 10:30:03 +0800136 cpumask_clear(d->old_domain);
Jiang Liu8a580f72015-12-31 16:30:46 +0000137 cpumask_clear(searched_cpumask);
Jiang Liu74afab72014-10-27 16:12:00 +0800138 cpu = cpumask_first_and(mask, cpu_online_mask);
139 while (cpu < nr_cpu_ids) {
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000140 int new_cpu, offset;
Jiang Liu74afab72014-10-27 16:12:00 +0800141
Thomas Gleixnerfdba46f2017-09-13 23:29:27 +0200142 cpumask_copy(vector_cpumask, cpumask_of(cpu));
Jiang Liu74afab72014-10-27 16:12:00 +0800143
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000144 /*
145 * Clear the offline cpus from @vector_cpumask for searching
146 * and verify whether the result overlaps with @mask. If true,
Thomas Gleixner91cd9cb2017-06-20 01:37:43 +0200147 * then the call to apic->cpu_mask_to_apicid() will
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000148 * succeed as well. If not, no point in trying to find a
149 * vector in this mask.
150 */
151 cpumask_and(vector_searchmask, vector_cpumask, cpu_online_mask);
152 if (!cpumask_intersects(vector_searchmask, mask))
153 goto next_cpu;
154
Jiang Liuf7fa7ae2015-04-14 10:30:10 +0800155 if (cpumask_subset(vector_cpumask, d->domain)) {
Jiang Liuf7fa7ae2015-04-14 10:30:10 +0800156 if (cpumask_equal(vector_cpumask, d->domain))
Thomas Gleixner433cbd52015-12-31 16:30:46 +0000157 goto success;
Jiang Liu74afab72014-10-27 16:12:00 +0800158 /*
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000159 * Mark the cpus which are not longer in the mask for
160 * cleanup.
Jiang Liu74afab72014-10-27 16:12:00 +0800161 */
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000162 cpumask_andnot(d->old_domain, d->domain, vector_cpumask);
163 vector = d->cfg.vector;
164 goto update;
Jiang Liu74afab72014-10-27 16:12:00 +0800165 }
166
167 vector = current_vector;
168 offset = current_offset;
169next:
170 vector += 16;
Thomas Gleixner05161b92017-08-28 08:47:18 +0200171 if (vector >= FIRST_SYSTEM_VECTOR) {
Jiang Liu74afab72014-10-27 16:12:00 +0800172 offset = (offset + 1) % 16;
173 vector = FIRST_EXTERNAL_VECTOR + offset;
174 }
175
Thomas Gleixner95ffeb42015-12-31 16:30:47 +0000176 /* If the search wrapped around, try the next cpu */
177 if (unlikely(current_vector == vector))
178 goto next_cpu;
Jiang Liu74afab72014-10-27 16:12:00 +0800179
Thomas Gleixner7854f822017-09-13 23:29:26 +0200180 if (test_bit(vector, system_vectors))
Jiang Liu74afab72014-10-27 16:12:00 +0800181 goto next;
182
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000183 for_each_cpu(new_cpu, vector_searchmask) {
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000184 if (!IS_ERR_OR_NULL(per_cpu(vector_irq, new_cpu)[vector]))
Jiang Liu74afab72014-10-27 16:12:00 +0800185 goto next;
186 }
187 /* Found one! */
188 current_vector = vector;
189 current_offset = offset;
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000190 /* Schedule the old vector for cleanup on all cpus */
191 if (d->cfg.vector)
Jiang Liu7f3262e2015-04-14 10:30:03 +0800192 cpumask_copy(d->old_domain, d->domain);
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000193 for_each_cpu(new_cpu, vector_searchmask)
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000194 per_cpu(vector_irq, new_cpu)[vector] = irq_to_desc(irq);
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000195 goto update;
Thomas Gleixner95ffeb42015-12-31 16:30:47 +0000196
197next_cpu:
198 /*
199 * We exclude the current @vector_cpumask from the requested
200 * @mask and try again with the next online cpu in the
201 * result. We cannot modify @mask, so we use @vector_cpumask
202 * as a temporary buffer here as it will be reassigned when
203 * calling apic->vector_allocation_domain() above.
204 */
205 cpumask_or(searched_cpumask, searched_cpumask, vector_cpumask);
206 cpumask_andnot(vector_cpumask, mask, searched_cpumask);
207 cpu = cpumask_first_and(vector_cpumask, cpu_online_mask);
208 continue;
Jiang Liu74afab72014-10-27 16:12:00 +0800209 }
Thomas Gleixner433cbd52015-12-31 16:30:46 +0000210 return -ENOSPC;
Jiang Liu74afab72014-10-27 16:12:00 +0800211
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000212update:
Thomas Gleixner847667e2015-12-31 16:30:50 +0000213 /*
214 * Exclude offline cpus from the cleanup mask and set the
215 * move_in_progress flag when the result is not empty.
216 */
217 cpumask_and(d->old_domain, d->old_domain, cpu_online_mask);
218 d->move_in_progress = !cpumask_empty(d->old_domain);
Thomas Gleixner551adc62016-03-14 09:40:46 +0100219 d->cfg.old_vector = d->move_in_progress ? d->cfg.vector : 0;
Thomas Gleixner029c6e12017-09-13 23:29:31 +0200220 d->prev_cpu = d->cpu;
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000221 d->cfg.vector = vector;
222 cpumask_copy(d->domain, vector_cpumask);
Thomas Gleixner433cbd52015-12-31 16:30:46 +0000223success:
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000224 /*
225 * Cache destination APIC IDs into cfg->dest_apicid. This cannot fail
226 * as we already established, that mask & d->domain & cpu_online_mask
227 * is not empty.
Thomas Gleixner52b166a2017-06-20 01:37:42 +0200228 *
229 * vector_searchmask is a subset of d->domain and has the offline
230 * cpus masked out.
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000231 */
Thomas Gleixner91cd9cb2017-06-20 01:37:43 +0200232 cpumask_and(vector_searchmask, vector_searchmask, mask);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200233 BUG_ON(apic->cpu_mask_to_apicid(vector_searchmask, irqd,
Thomas Gleixner0e24f7c2017-06-20 01:37:44 +0200234 &d->cfg.dest_apicid));
Thomas Gleixner029c6e12017-09-13 23:29:31 +0200235 d->cpu = cpumask_first(vector_searchmask);
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000236 return 0;
Jiang Liu74afab72014-10-27 16:12:00 +0800237}
238
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200239static int assign_irq_vector(int irq, struct apic_chip_data *apicd,
Thomas Gleixner0e24f7c2017-06-20 01:37:44 +0200240 const struct cpumask *mask,
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200241 struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +0800242{
243 int err;
244 unsigned long flags;
245
246 raw_spin_lock_irqsave(&vector_lock, flags);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200247 err = __assign_irq_vector(irq, apicd, mask, irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800248 raw_spin_unlock_irqrestore(&vector_lock, flags);
249 return err;
250}
251
Jiang Liu486ca532015-05-07 10:53:56 +0800252static int assign_irq_vector_policy(int irq, int node,
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200253 struct apic_chip_data *apicd,
Thomas Gleixner0e24f7c2017-06-20 01:37:44 +0200254 struct irq_alloc_info *info,
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200255 struct irq_data *irqd)
Jiang Liu486ca532015-05-07 10:53:56 +0800256{
257 if (info && info->mask)
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200258 return assign_irq_vector(irq, apicd, info->mask, irqd);
Jiang Liu486ca532015-05-07 10:53:56 +0800259 if (node != NUMA_NO_NODE &&
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200260 assign_irq_vector(irq, apicd, cpumask_of_node(node), irqd) == 0)
Jiang Liu486ca532015-05-07 10:53:56 +0800261 return 0;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200262 return assign_irq_vector(irq, apicd, cpu_online_mask, irqd);
Jiang Liu486ca532015-05-07 10:53:56 +0800263}
264
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200265static void clear_irq_vector(int irq, struct apic_chip_data *apicd)
Jiang Liu74afab72014-10-27 16:12:00 +0800266{
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200267 unsigned int vector = apicd->cfg.vector;
Jiang Liu74afab72014-10-27 16:12:00 +0800268
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200269 if (!vector)
Keith Busch1bdb8972016-04-27 14:22:32 -0600270 return;
Jiang Liu74afab72014-10-27 16:12:00 +0800271
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200272 per_cpu(vector_irq, apicd->cpu)[vector] = VECTOR_UNUSED;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200273 apicd->cfg.vector = 0;
Jiang Liu74afab72014-10-27 16:12:00 +0800274
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200275 /* Clean up move in progress */
276 vector = apicd->cfg.old_vector;
277 if (!vector)
Jiang Liu74afab72014-10-27 16:12:00 +0800278 return;
Jiang Liu74afab72014-10-27 16:12:00 +0800279
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200280 per_cpu(vector_irq, apicd->prev_cpu)[vector] = VECTOR_UNUSED;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200281 apicd->move_in_progress = 0;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200282 hlist_del_init(&apicd->clist);
Jiang Liu74afab72014-10-27 16:12:00 +0800283}
284
Jiang Liub5dc8e62015-04-13 14:11:24 +0800285void init_irq_alloc_info(struct irq_alloc_info *info,
286 const struct cpumask *mask)
287{
288 memset(info, 0, sizeof(*info));
289 info->mask = mask;
290}
291
292void copy_irq_alloc_info(struct irq_alloc_info *dst, struct irq_alloc_info *src)
293{
294 if (src)
295 *dst = *src;
296 else
297 memset(dst, 0, sizeof(*dst));
298}
299
Jiang Liub5dc8e62015-04-13 14:11:24 +0800300static void x86_vector_free_irqs(struct irq_domain *domain,
301 unsigned int virq, unsigned int nr_irqs)
302{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200303 struct apic_chip_data *apicd;
304 struct irq_data *irqd;
Jiang Liu111abeb2015-12-31 16:30:44 +0000305 unsigned long flags;
Jiang Liub5dc8e62015-04-13 14:11:24 +0800306 int i;
307
308 for (i = 0; i < nr_irqs; i++) {
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200309 irqd = irq_domain_get_irq_data(x86_vector_domain, virq + i);
310 if (irqd && irqd->chip_data) {
Jiang Liu111abeb2015-12-31 16:30:44 +0000311 raw_spin_lock_irqsave(&vector_lock, flags);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200312 clear_irq_vector(virq + i, irqd->chip_data);
313 apicd = irqd->chip_data;
314 irq_domain_reset_irq_data(irqd);
Jiang Liu111abeb2015-12-31 16:30:44 +0000315 raw_spin_unlock_irqrestore(&vector_lock, flags);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200316 free_apic_chip_data(apicd);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800317 }
318 }
319}
320
321static int x86_vector_alloc_irqs(struct irq_domain *domain, unsigned int virq,
322 unsigned int nr_irqs, void *arg)
323{
324 struct irq_alloc_info *info = arg;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200325 struct apic_chip_data *apicd;
326 struct irq_data *irqd;
Jiang Liu5f2dbbc2015-06-01 16:05:14 +0800327 int i, err, node;
Jiang Liub5dc8e62015-04-13 14:11:24 +0800328
329 if (disable_apic)
330 return -ENXIO;
331
332 /* Currently vector allocator can't guarantee contiguous allocations */
333 if ((info->flags & X86_IRQ_ALLOC_CONTIGUOUS_VECTORS) && nr_irqs > 1)
334 return -ENOSYS;
335
Jiang Liub5dc8e62015-04-13 14:11:24 +0800336 for (i = 0; i < nr_irqs; i++) {
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200337 irqd = irq_domain_get_irq_data(domain, virq + i);
338 BUG_ON(!irqd);
339 node = irq_data_get_node(irqd);
Thomas Gleixner4ef76eb2017-09-13 23:29:34 +0200340 WARN_ON_ONCE(irqd->chip_data);
341 apicd = alloc_apic_chip_data(node);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200342 if (!apicd) {
Jiang Liub5dc8e62015-04-13 14:11:24 +0800343 err = -ENOMEM;
344 goto error;
345 }
346
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200347 irqd->chip = &lapic_controller;
348 irqd->chip_data = apicd;
349 irqd->hwirq = virq + i;
350 irqd_set_single_target(irqd);
Thomas Gleixner4ef76eb2017-09-13 23:29:34 +0200351 /*
352 * Make sure, that the legacy to IOAPIC transition stays on
353 * the same vector. This is required for check_timer() to
354 * work correctly as it might switch back to legacy mode.
355 */
356 if (info->flags & X86_IRQ_ALLOC_LEGACY) {
357 apicd->cfg.vector = ISA_IRQ_VECTOR(virq + i);
358 apicd->cpu = 0;
359 cpumask_copy(apicd->domain, cpumask_of(0));
360 }
361
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200362 err = assign_irq_vector_policy(virq + i, node, apicd, info,
363 irqd);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800364 if (err)
365 goto error;
366 }
367
368 return 0;
369
370error:
371 x86_vector_free_irqs(domain, virq, i + 1);
372 return err;
373}
374
Thomas Gleixnereb18cf52015-05-05 11:10:11 +0200375static const struct irq_domain_ops x86_vector_domain_ops = {
376 .alloc = x86_vector_alloc_irqs,
377 .free = x86_vector_free_irqs,
Jiang Liub5dc8e62015-04-13 14:11:24 +0800378};
379
Jiang Liu11d686e2014-10-27 16:12:05 +0800380int __init arch_probe_nr_irqs(void)
381{
382 int nr;
383
384 if (nr_irqs > (NR_VECTORS * nr_cpu_ids))
385 nr_irqs = NR_VECTORS * nr_cpu_ids;
386
387 nr = (gsi_top + nr_legacy_irqs()) + 8 * nr_cpu_ids;
388#if defined(CONFIG_PCI_MSI) || defined(CONFIG_HT_IRQ)
389 /*
390 * for MSI and HT dyn irq
391 */
392 if (gsi_top <= NR_IRQS_LEGACY)
393 nr += 8 * nr_cpu_ids;
394 else
395 nr += gsi_top * 16;
396#endif
397 if (nr < nr_irqs)
398 nr_irqs = nr;
399
Vitaly Kuznetsov8c058b02015-11-03 10:40:14 +0100400 /*
401 * We don't know if PIC is present at this point so we need to do
402 * probe() to get the right number of legacy IRQs.
403 */
404 return legacy_pic->probe();
Jiang Liu11d686e2014-10-27 16:12:05 +0800405}
406
407int __init arch_early_irq_init(void)
408{
Thomas Gleixner9d35f852017-06-20 01:37:06 +0200409 struct fwnode_handle *fn;
410
Thomas Gleixner9d35f852017-06-20 01:37:06 +0200411 fn = irq_domain_alloc_named_fwnode("VECTOR");
412 BUG_ON(!fn);
413 x86_vector_domain = irq_domain_create_tree(fn, &x86_vector_domain_ops,
414 NULL);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800415 BUG_ON(x86_vector_domain == NULL);
Thomas Gleixner9d35f852017-06-20 01:37:06 +0200416 irq_domain_free_fwnode(fn);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800417 irq_set_default_host(x86_vector_domain);
418
Jiang Liu52f518a2015-04-13 14:11:35 +0800419 arch_init_msi_domain(x86_vector_domain);
Jiang Liu49e07d82015-04-13 14:11:43 +0800420 arch_init_htirq_domain(x86_vector_domain);
Jiang Liu52f518a2015-04-13 14:11:35 +0800421
Jiang Liuf7fa7ae2015-04-14 10:30:10 +0800422 BUG_ON(!alloc_cpumask_var(&vector_cpumask, GFP_KERNEL));
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000423 BUG_ON(!alloc_cpumask_var(&vector_searchmask, GFP_KERNEL));
Jiang Liu8a580f72015-12-31 16:30:46 +0000424 BUG_ON(!alloc_cpumask_var(&searched_cpumask, GFP_KERNEL));
Jiang Liuf7fa7ae2015-04-14 10:30:10 +0800425
Jiang Liu11d686e2014-10-27 16:12:05 +0800426 return arch_early_ioapic_init();
427}
428
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200429/* Temporary hack to keep things working */
430static void vector_update_shutdown_irqs(void)
Jiang Liu74afab72014-10-27 16:12:00 +0800431{
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000432 struct irq_desc *desc;
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200433 int irq;
Jiang Liu74afab72014-10-27 16:12:00 +0800434
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000435 for_each_irq_desc(irq, desc) {
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200436 struct irq_data *irqd = irq_desc_get_irq_data(desc);
437 struct apic_chip_data *ad = apic_chip_data(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800438
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200439 if (ad && ad->cfg.vector && ad->cpu == smp_processor_id())
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200440 this_cpu_write(vector_irq[ad->cfg.vector], desc);
Jiang Liu74afab72014-10-27 16:12:00 +0800441 }
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200442}
Jiang Liu74afab72014-10-27 16:12:00 +0800443
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200444static struct irq_desc *__setup_vector_irq(int vector)
445{
446 int isairq = vector - ISA_IRQ_VECTOR(0);
447
448 /* Check whether the irq is in the legacy space */
449 if (isairq < 0 || isairq >= nr_legacy_irqs())
450 return VECTOR_UNUSED;
451 /* Check whether the irq is handled by the IOAPIC */
452 if (test_bit(isairq, &io_apic_irqs))
453 return VECTOR_UNUSED;
454 return irq_to_desc(isairq);
Jiang Liu74afab72014-10-27 16:12:00 +0800455}
456
457/*
Thomas Gleixner5a3f75e2015-07-05 17:12:32 +0000458 * Setup the vector to irq mappings. Must be called with vector_lock held.
Jiang Liu74afab72014-10-27 16:12:00 +0800459 */
460void setup_vector_irq(int cpu)
461{
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200462 unsigned int vector;
Jiang Liu74afab72014-10-27 16:12:00 +0800463
Thomas Gleixner5a3f75e2015-07-05 17:12:32 +0000464 lockdep_assert_held(&vector_lock);
Jiang Liu74afab72014-10-27 16:12:00 +0800465 /*
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200466 * The interrupt affinity logic never targets interrupts to offline
467 * CPUs. The exception are the legacy PIC interrupts. In general
468 * they are only targeted to CPU0, but depending on the platform
469 * they can be distributed to any online CPU in hardware. The
470 * kernel has no influence on that. So all active legacy vectors
471 * must be installed on all CPUs. All non legacy interrupts can be
472 * cleared.
Jiang Liu74afab72014-10-27 16:12:00 +0800473 */
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200474 for (vector = 0; vector < NR_VECTORS; vector++)
475 this_cpu_write(vector_irq[vector], __setup_vector_irq(vector));
Jiang Liu74afab72014-10-27 16:12:00 +0800476
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200477 /*
478 * Until the rewrite of the managed interrupt management is in
479 * place it's necessary to walk the irq descriptors and check for
480 * interrupts which are targeted at this CPU.
481 */
482 vector_update_shutdown_irqs();
Jiang Liu74afab72014-10-27 16:12:00 +0800483}
484
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200485static int apic_retrigger_irq(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +0800486{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200487 struct apic_chip_data *apicd = apic_chip_data(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800488 unsigned long flags;
Jiang Liu74afab72014-10-27 16:12:00 +0800489
490 raw_spin_lock_irqsave(&vector_lock, flags);
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200491 apic->send_IPI(apicd->cpu, apicd->cfg.vector);
Jiang Liu74afab72014-10-27 16:12:00 +0800492 raw_spin_unlock_irqrestore(&vector_lock, flags);
493
494 return 1;
495}
496
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200497void apic_ack_edge(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +0800498{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200499 irq_complete_move(irqd_cfg(irqd));
500 irq_move_irq(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800501 ack_APIC_irq();
502}
503
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200504static int apic_set_affinity(struct irq_data *irqd,
Jiang Liu68f9f442015-04-14 10:30:01 +0800505 const struct cpumask *dest, bool force)
Jiang Liub5dc8e62015-04-13 14:11:24 +0800506{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200507 struct apic_chip_data *apicd = irqd->chip_data;
508 int err, irq = irqd->irq;
Jiang Liub5dc8e62015-04-13 14:11:24 +0800509
Masahiro Yamada97f26452016-08-03 13:45:50 -0700510 if (!IS_ENABLED(CONFIG_SMP))
Jiang Liub5dc8e62015-04-13 14:11:24 +0800511 return -EPERM;
512
513 if (!cpumask_intersects(dest, cpu_online_mask))
514 return -EINVAL;
515
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200516 err = assign_irq_vector(irq, apicd, dest, irqd);
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000517 return err ? err : IRQ_SET_MASK_OK;
Jiang Liub5dc8e62015-04-13 14:11:24 +0800518}
519
520static struct irq_chip lapic_controller = {
Thomas Gleixner8947dfb2017-06-20 01:37:01 +0200521 .name = "APIC",
Jiang Liub5dc8e62015-04-13 14:11:24 +0800522 .irq_ack = apic_ack_edge,
Jiang Liu68f9f442015-04-14 10:30:01 +0800523 .irq_set_affinity = apic_set_affinity,
Jiang Liub5dc8e62015-04-13 14:11:24 +0800524 .irq_retrigger = apic_retrigger_irq,
525};
526
Jiang Liu74afab72014-10-27 16:12:00 +0800527#ifdef CONFIG_SMP
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200528
529asmlinkage __visible void __irq_entry smp_irq_move_cleanup_interrupt(void)
530{
531 struct hlist_head *clhead = this_cpu_ptr(&cleanup_list);
532 struct apic_chip_data *apicd;
533 struct hlist_node *tmp;
534
535 entering_ack_irq();
536 /* Prevent vectors vanishing under us */
537 raw_spin_lock(&vector_lock);
538
539 hlist_for_each_entry_safe(apicd, tmp, clhead, clist) {
540 unsigned int irr, vector = apicd->cfg.old_vector;
541
542 /*
543 * Paranoia: Check if the vector that needs to be cleaned
544 * up is registered at the APICs IRR. If so, then this is
545 * not the best time to clean it up. Clean it up in the
546 * next attempt by sending another IRQ_MOVE_CLEANUP_VECTOR
547 * to this CPU. IRQ_MOVE_CLEANUP_VECTOR is the lowest
548 * priority external vector, so on return from this
549 * interrupt the device interrupt will happen first.
550 */
551 irr = apic_read(APIC_IRR + (vector / 32 * 0x10));
552 if (irr & (1U << (vector % 32))) {
553 apic->send_IPI_self(IRQ_MOVE_CLEANUP_VECTOR);
554 continue;
555 }
556 hlist_del_init(&apicd->clist);
557 __this_cpu_write(vector_irq[vector], VECTOR_UNUSED);
558 apicd->cfg.old_vector = 0;
559 }
560
561 raw_spin_unlock(&vector_lock);
562 exiting_irq();
563}
564
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200565static void __send_cleanup_vector(struct apic_chip_data *apicd)
Jiang Liu74afab72014-10-27 16:12:00 +0800566{
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200567 unsigned int cpu;
568
Thomas Gleixnerc1684f52015-12-31 16:30:51 +0000569 raw_spin_lock(&vector_lock);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200570 apicd->move_in_progress = 0;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200571 cpu = apicd->prev_cpu;
572 if (cpu_online(cpu)) {
573 hlist_add_head(&apicd->clist, per_cpu_ptr(&cleanup_list, cpu));
574 apic->send_IPI(cpu, IRQ_MOVE_CLEANUP_VECTOR);
575 } else {
576 apicd->cfg.old_vector = 0;
577 }
Thomas Gleixnerc1684f52015-12-31 16:30:51 +0000578 raw_spin_unlock(&vector_lock);
Jiang Liu74afab72014-10-27 16:12:00 +0800579}
580
Jiang Liuc6c20022015-04-14 10:30:02 +0800581void send_cleanup_vector(struct irq_cfg *cfg)
582{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200583 struct apic_chip_data *apicd;
Jiang Liu7f3262e2015-04-14 10:30:03 +0800584
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200585 apicd = container_of(cfg, struct apic_chip_data, cfg);
586 if (apicd->move_in_progress)
587 __send_cleanup_vector(apicd);
Jiang Liuc6c20022015-04-14 10:30:02 +0800588}
589
Jiang Liu74afab72014-10-27 16:12:00 +0800590static void __irq_complete_move(struct irq_cfg *cfg, unsigned vector)
591{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200592 struct apic_chip_data *apicd;
Jiang Liu74afab72014-10-27 16:12:00 +0800593
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200594 apicd = container_of(cfg, struct apic_chip_data, cfg);
595 if (likely(!apicd->move_in_progress))
Jiang Liu74afab72014-10-27 16:12:00 +0800596 return;
597
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200598 if (vector == apicd->cfg.vector && apicd->cpu == smp_processor_id())
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200599 __send_cleanup_vector(apicd);
Jiang Liu74afab72014-10-27 16:12:00 +0800600}
601
602void irq_complete_move(struct irq_cfg *cfg)
603{
604 __irq_complete_move(cfg, ~get_irq_regs()->orig_ax);
605}
606
Thomas Gleixner90a22822015-12-31 16:30:53 +0000607/*
Thomas Gleixner551adc62016-03-14 09:40:46 +0100608 * Called from fixup_irqs() with @desc->lock held and interrupts disabled.
Thomas Gleixner90a22822015-12-31 16:30:53 +0000609 */
610void irq_force_complete_move(struct irq_desc *desc)
Jiang Liu74afab72014-10-27 16:12:00 +0800611{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200612 struct apic_chip_data *apicd;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200613 struct irq_data *irqd;
614 unsigned int vector;
Jiang Liu74afab72014-10-27 16:12:00 +0800615
Mika Westerbergdb91aa72016-10-03 13:17:08 +0300616 /*
617 * The function is called for all descriptors regardless of which
618 * irqdomain they belong to. For example if an IRQ is provided by
619 * an irq_chip as part of a GPIO driver, the chip data for that
620 * descriptor is specific to the irq_chip in question.
621 *
622 * Check first that the chip_data is what we expect
623 * (apic_chip_data) before touching it any further.
624 */
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200625 irqd = irq_domain_get_irq_data(x86_vector_domain,
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200626 irq_desc_get_irq(desc));
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200627 if (!irqd)
Mika Westerbergdb91aa72016-10-03 13:17:08 +0300628 return;
629
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200630 raw_spin_lock(&vector_lock);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200631 apicd = apic_chip_data(irqd);
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200632 if (!apicd)
633 goto unlock;
Thomas Gleixner56d7d2f2015-12-31 16:30:52 +0000634
Thomas Gleixner56d7d2f2015-12-31 16:30:52 +0000635 /*
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200636 * If old_vector is empty, no action required.
637 */
638 vector = apicd->cfg.old_vector;
639 if (!vector)
640 goto unlock;
641
642 /*
643 * This is tricky. If the cleanup of the old vector has not been
Thomas Gleixner98229aa2015-12-31 16:30:54 +0000644 * done yet, then the following setaffinity call will fail with
645 * -EBUSY. This can leave the interrupt in a stale state.
646 *
Thomas Gleixner551adc62016-03-14 09:40:46 +0100647 * All CPUs are stuck in stop machine with interrupts disabled so
648 * calling __irq_complete_move() would be completely pointless.
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200649 *
Thomas Gleixner551adc62016-03-14 09:40:46 +0100650 * 1) The interrupt is in move_in_progress state. That means that we
651 * have not seen an interrupt since the io_apic was reprogrammed to
652 * the new vector.
653 *
654 * 2) The interrupt has fired on the new vector, but the cleanup IPIs
655 * have not been processed yet.
656 */
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200657 if (apicd->move_in_progress) {
Thomas Gleixner551adc62016-03-14 09:40:46 +0100658 /*
659 * In theory there is a race:
660 *
661 * set_ioapic(new_vector) <-- Interrupt is raised before update
662 * is effective, i.e. it's raised on
663 * the old vector.
664 *
665 * So if the target cpu cannot handle that interrupt before
666 * the old vector is cleaned up, we get a spurious interrupt
667 * and in the worst case the ioapic irq line becomes stale.
668 *
669 * But in case of cpu hotplug this should be a non issue
670 * because if the affinity update happens right before all
671 * cpus rendevouz in stop machine, there is no way that the
672 * interrupt can be blocked on the target cpu because all cpus
673 * loops first with interrupts enabled in stop machine, so the
674 * old vector is not yet cleaned up when the interrupt fires.
675 *
676 * So the only way to run into this issue is if the delivery
677 * of the interrupt on the apic/system bus would be delayed
678 * beyond the point where the target cpu disables interrupts
679 * in stop machine. I doubt that it can happen, but at least
680 * there is a theroretical chance. Virtualization might be
681 * able to expose this, but AFAICT the IOAPIC emulation is not
682 * as stupid as the real hardware.
683 *
684 * Anyway, there is nothing we can do about that at this point
685 * w/o refactoring the whole fixup_irq() business completely.
686 * We print at least the irq number and the old vector number,
687 * so we have the necessary information when a problem in that
688 * area arises.
689 */
690 pr_warn("IRQ fixup: irq %d move in progress, old vector %d\n",
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200691 irqd->irq, vector);
Thomas Gleixner551adc62016-03-14 09:40:46 +0100692 }
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200693 per_cpu(vector_irq, apicd->prev_cpu)[vector] = VECTOR_UNUSED;
Thomas Gleixner551adc62016-03-14 09:40:46 +0100694 /* Cleanup the left overs of the (half finished) move */
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200695 cpumask_clear(apicd->old_domain);
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200696 apicd->cfg.old_vector = 0;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200697 apicd->move_in_progress = 0;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200698 hlist_del_init(&apicd->clist);
699unlock:
Thomas Gleixner56d7d2f2015-12-31 16:30:52 +0000700 raw_spin_unlock(&vector_lock);
Jiang Liu74afab72014-10-27 16:12:00 +0800701}
Jiang Liu74afab72014-10-27 16:12:00 +0800702#endif
703
Jiang Liu74afab72014-10-27 16:12:00 +0800704static void __init print_APIC_field(int base)
705{
706 int i;
707
708 printk(KERN_DEBUG);
709
710 for (i = 0; i < 8; i++)
711 pr_cont("%08x", apic_read(base + i*0x10));
712
713 pr_cont("\n");
714}
715
716static void __init print_local_APIC(void *dummy)
717{
718 unsigned int i, v, ver, maxlvt;
719 u64 icr;
720
Jiang Liu849d3562014-10-27 16:12:01 +0800721 pr_debug("printing local APIC contents on CPU#%d/%d:\n",
722 smp_processor_id(), hard_smp_processor_id());
Jiang Liu74afab72014-10-27 16:12:00 +0800723 v = apic_read(APIC_ID);
Jiang Liu849d3562014-10-27 16:12:01 +0800724 pr_info("... APIC ID: %08x (%01x)\n", v, read_apic_id());
Jiang Liu74afab72014-10-27 16:12:00 +0800725 v = apic_read(APIC_LVR);
Jiang Liu849d3562014-10-27 16:12:01 +0800726 pr_info("... APIC VERSION: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800727 ver = GET_APIC_VERSION(v);
728 maxlvt = lapic_get_maxlvt();
729
730 v = apic_read(APIC_TASKPRI);
Jiang Liu849d3562014-10-27 16:12:01 +0800731 pr_debug("... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
Jiang Liu74afab72014-10-27 16:12:00 +0800732
733 /* !82489DX */
734 if (APIC_INTEGRATED(ver)) {
735 if (!APIC_XAPIC(ver)) {
736 v = apic_read(APIC_ARBPRI);
Jiang Liu849d3562014-10-27 16:12:01 +0800737 pr_debug("... APIC ARBPRI: %08x (%02x)\n",
738 v, v & APIC_ARBPRI_MASK);
Jiang Liu74afab72014-10-27 16:12:00 +0800739 }
740 v = apic_read(APIC_PROCPRI);
Jiang Liu849d3562014-10-27 16:12:01 +0800741 pr_debug("... APIC PROCPRI: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800742 }
743
744 /*
745 * Remote read supported only in the 82489DX and local APIC for
746 * Pentium processors.
747 */
748 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
749 v = apic_read(APIC_RRR);
Jiang Liu849d3562014-10-27 16:12:01 +0800750 pr_debug("... APIC RRR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800751 }
752
753 v = apic_read(APIC_LDR);
Jiang Liu849d3562014-10-27 16:12:01 +0800754 pr_debug("... APIC LDR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800755 if (!x2apic_enabled()) {
756 v = apic_read(APIC_DFR);
Jiang Liu849d3562014-10-27 16:12:01 +0800757 pr_debug("... APIC DFR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800758 }
759 v = apic_read(APIC_SPIV);
Jiang Liu849d3562014-10-27 16:12:01 +0800760 pr_debug("... APIC SPIV: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800761
Jiang Liu849d3562014-10-27 16:12:01 +0800762 pr_debug("... APIC ISR field:\n");
Jiang Liu74afab72014-10-27 16:12:00 +0800763 print_APIC_field(APIC_ISR);
Jiang Liu849d3562014-10-27 16:12:01 +0800764 pr_debug("... APIC TMR field:\n");
Jiang Liu74afab72014-10-27 16:12:00 +0800765 print_APIC_field(APIC_TMR);
Jiang Liu849d3562014-10-27 16:12:01 +0800766 pr_debug("... APIC IRR field:\n");
Jiang Liu74afab72014-10-27 16:12:00 +0800767 print_APIC_field(APIC_IRR);
768
769 /* !82489DX */
770 if (APIC_INTEGRATED(ver)) {
771 /* Due to the Pentium erratum 3AP. */
772 if (maxlvt > 3)
773 apic_write(APIC_ESR, 0);
774
775 v = apic_read(APIC_ESR);
Jiang Liu849d3562014-10-27 16:12:01 +0800776 pr_debug("... APIC ESR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800777 }
778
779 icr = apic_icr_read();
Jiang Liu849d3562014-10-27 16:12:01 +0800780 pr_debug("... APIC ICR: %08x\n", (u32)icr);
781 pr_debug("... APIC ICR2: %08x\n", (u32)(icr >> 32));
Jiang Liu74afab72014-10-27 16:12:00 +0800782
783 v = apic_read(APIC_LVTT);
Jiang Liu849d3562014-10-27 16:12:01 +0800784 pr_debug("... APIC LVTT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800785
786 if (maxlvt > 3) {
787 /* PC is LVT#4. */
788 v = apic_read(APIC_LVTPC);
Jiang Liu849d3562014-10-27 16:12:01 +0800789 pr_debug("... APIC LVTPC: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800790 }
791 v = apic_read(APIC_LVT0);
Jiang Liu849d3562014-10-27 16:12:01 +0800792 pr_debug("... APIC LVT0: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800793 v = apic_read(APIC_LVT1);
Jiang Liu849d3562014-10-27 16:12:01 +0800794 pr_debug("... APIC LVT1: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800795
796 if (maxlvt > 2) {
797 /* ERR is LVT#3. */
798 v = apic_read(APIC_LVTERR);
Jiang Liu849d3562014-10-27 16:12:01 +0800799 pr_debug("... APIC LVTERR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800800 }
801
802 v = apic_read(APIC_TMICT);
Jiang Liu849d3562014-10-27 16:12:01 +0800803 pr_debug("... APIC TMICT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800804 v = apic_read(APIC_TMCCT);
Jiang Liu849d3562014-10-27 16:12:01 +0800805 pr_debug("... APIC TMCCT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800806 v = apic_read(APIC_TDCR);
Jiang Liu849d3562014-10-27 16:12:01 +0800807 pr_debug("... APIC TDCR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800808
809 if (boot_cpu_has(X86_FEATURE_EXTAPIC)) {
810 v = apic_read(APIC_EFEAT);
811 maxlvt = (v >> 16) & 0xff;
Jiang Liu849d3562014-10-27 16:12:01 +0800812 pr_debug("... APIC EFEAT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800813 v = apic_read(APIC_ECTRL);
Jiang Liu849d3562014-10-27 16:12:01 +0800814 pr_debug("... APIC ECTRL: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800815 for (i = 0; i < maxlvt; i++) {
816 v = apic_read(APIC_EILVTn(i));
Jiang Liu849d3562014-10-27 16:12:01 +0800817 pr_debug("... APIC EILVT%d: %08x\n", i, v);
Jiang Liu74afab72014-10-27 16:12:00 +0800818 }
819 }
820 pr_cont("\n");
821}
822
823static void __init print_local_APICs(int maxcpu)
824{
825 int cpu;
826
827 if (!maxcpu)
828 return;
829
830 preempt_disable();
831 for_each_online_cpu(cpu) {
832 if (cpu >= maxcpu)
833 break;
834 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
835 }
836 preempt_enable();
837}
838
839static void __init print_PIC(void)
840{
841 unsigned int v;
842 unsigned long flags;
843
844 if (!nr_legacy_irqs())
845 return;
846
Jiang Liu849d3562014-10-27 16:12:01 +0800847 pr_debug("\nprinting PIC contents\n");
Jiang Liu74afab72014-10-27 16:12:00 +0800848
849 raw_spin_lock_irqsave(&i8259A_lock, flags);
850
851 v = inb(0xa1) << 8 | inb(0x21);
Jiang Liu849d3562014-10-27 16:12:01 +0800852 pr_debug("... PIC IMR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800853
854 v = inb(0xa0) << 8 | inb(0x20);
Jiang Liu849d3562014-10-27 16:12:01 +0800855 pr_debug("... PIC IRR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800856
857 outb(0x0b, 0xa0);
858 outb(0x0b, 0x20);
859 v = inb(0xa0) << 8 | inb(0x20);
860 outb(0x0a, 0xa0);
861 outb(0x0a, 0x20);
862
863 raw_spin_unlock_irqrestore(&i8259A_lock, flags);
864
Jiang Liu849d3562014-10-27 16:12:01 +0800865 pr_debug("... PIC ISR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800866
867 v = inb(0x4d1) << 8 | inb(0x4d0);
Jiang Liu849d3562014-10-27 16:12:01 +0800868 pr_debug("... PIC ELCR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800869}
870
871static int show_lapic __initdata = 1;
872static __init int setup_show_lapic(char *arg)
873{
874 int num = -1;
875
876 if (strcmp(arg, "all") == 0) {
877 show_lapic = CONFIG_NR_CPUS;
878 } else {
879 get_option(&arg, &num);
880 if (num >= 0)
881 show_lapic = num;
882 }
883
884 return 1;
885}
886__setup("show_lapic=", setup_show_lapic);
887
888static int __init print_ICs(void)
889{
890 if (apic_verbosity == APIC_QUIET)
891 return 0;
892
893 print_PIC();
894
895 /* don't print out if apic is not there */
Borislav Petkov93984fb2016-04-04 22:25:00 +0200896 if (!boot_cpu_has(X86_FEATURE_APIC) && !apic_from_smp_config())
Jiang Liu74afab72014-10-27 16:12:00 +0800897 return 0;
898
899 print_local_APICs(show_lapic);
900 print_IO_APICs();
901
902 return 0;
903}
904
905late_initcall(print_ICs);