blob: 88219b80d9ec9a2e8b9660c1b11b4ea3bdfb525e [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 Gleixner69cde002017-09-13 23:29:42 +020031 unsigned int irq;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +020032 struct hlist_node clist;
Jiang Liu7f3262e2015-04-14 10:30:03 +080033 u8 move_in_progress : 1;
34};
35
Jiang Liub5dc8e62015-04-13 14:11:24 +080036struct irq_domain *x86_vector_domain;
Jake Oshinsc8f3e512015-12-10 17:52:59 +000037EXPORT_SYMBOL_GPL(x86_vector_domain);
Jiang Liu74afab72014-10-27 16:12:00 +080038static DEFINE_RAW_SPINLOCK(vector_lock);
Thomas Gleixner69cde002017-09-13 23:29:42 +020039static cpumask_var_t vector_searchmask;
Jiang Liub5dc8e62015-04-13 14:11:24 +080040static struct irq_chip lapic_controller;
Thomas Gleixner0fa115d2017-09-13 23:29:38 +020041static struct irq_matrix *vector_matrix;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +020042#ifdef CONFIG_SMP
43static DEFINE_PER_CPU(struct hlist_head, cleanup_list);
44#endif
Jiang Liu74afab72014-10-27 16:12:00 +080045
46void lock_vector_lock(void)
47{
48 /* Used to the online set of cpus does not change
49 * during assign_irq_vector.
50 */
51 raw_spin_lock(&vector_lock);
52}
53
54void unlock_vector_lock(void)
55{
56 raw_spin_unlock(&vector_lock);
57}
58
Thomas Gleixner99a14822017-09-13 23:29:36 +020059void init_irq_alloc_info(struct irq_alloc_info *info,
60 const struct cpumask *mask)
61{
62 memset(info, 0, sizeof(*info));
63 info->mask = mask;
64}
65
66void copy_irq_alloc_info(struct irq_alloc_info *dst, struct irq_alloc_info *src)
67{
68 if (src)
69 *dst = *src;
70 else
71 memset(dst, 0, sizeof(*dst));
72}
73
Thomas Gleixner86ba6552017-09-13 23:29:30 +020074static struct apic_chip_data *apic_chip_data(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +080075{
Thomas Gleixner86ba6552017-09-13 23:29:30 +020076 if (!irqd)
Jiang Liub5dc8e62015-04-13 14:11:24 +080077 return NULL;
78
Thomas Gleixner86ba6552017-09-13 23:29:30 +020079 while (irqd->parent_data)
80 irqd = irqd->parent_data;
Jiang Liub5dc8e62015-04-13 14:11:24 +080081
Thomas Gleixner86ba6552017-09-13 23:29:30 +020082 return irqd->chip_data;
Jiang Liu74afab72014-10-27 16:12:00 +080083}
84
Thomas Gleixner86ba6552017-09-13 23:29:30 +020085struct irq_cfg *irqd_cfg(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +080086{
Thomas Gleixner86ba6552017-09-13 23:29:30 +020087 struct apic_chip_data *apicd = apic_chip_data(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +080088
Thomas Gleixner86ba6552017-09-13 23:29:30 +020089 return apicd ? &apicd->cfg : NULL;
Jiang Liu7f3262e2015-04-14 10:30:03 +080090}
Jake Oshinsc8f3e512015-12-10 17:52:59 +000091EXPORT_SYMBOL_GPL(irqd_cfg);
Jiang Liu7f3262e2015-04-14 10:30:03 +080092
93struct irq_cfg *irq_cfg(unsigned int irq)
94{
95 return irqd_cfg(irq_get_irq_data(irq));
96}
97
98static struct apic_chip_data *alloc_apic_chip_data(int node)
99{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200100 struct apic_chip_data *apicd;
Jiang Liu7f3262e2015-04-14 10:30:03 +0800101
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200102 apicd = kzalloc_node(sizeof(*apicd), GFP_KERNEL, node);
Thomas Gleixner69cde002017-09-13 23:29:42 +0200103 if (apicd)
104 INIT_HLIST_NODE(&apicd->clist);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200105 return apicd;
Jiang Liu74afab72014-10-27 16:12:00 +0800106}
107
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200108static void free_apic_chip_data(struct apic_chip_data *apicd)
Jiang Liu74afab72014-10-27 16:12:00 +0800109{
Thomas Gleixner69cde002017-09-13 23:29:42 +0200110 kfree(apicd);
Jiang Liu74afab72014-10-27 16:12:00 +0800111}
112
Thomas Gleixner69cde002017-09-13 23:29:42 +0200113static void apic_update_irq_cfg(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +0800114{
Thomas Gleixner69cde002017-09-13 23:29:42 +0200115 struct apic_chip_data *apicd = apic_chip_data(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800116
Thomas Gleixner69cde002017-09-13 23:29:42 +0200117 lockdep_assert_held(&vector_lock);
Jiang Liu74afab72014-10-27 16:12:00 +0800118
Thomas Gleixner69cde002017-09-13 23:29:42 +0200119 apicd->cfg.dest_apicid = apic->calc_dest_apicid(apicd->cpu);
120 irq_data_update_effective_affinity(irqd, cpumask_of(apicd->cpu));
121 trace_vector_config(irqd->irq, apicd->cfg.vector, apicd->cpu,
122 apicd->cfg.dest_apicid);
123}
Jiang Liu74afab72014-10-27 16:12:00 +0800124
Thomas Gleixner69cde002017-09-13 23:29:42 +0200125static void apic_update_vector(struct irq_data *irqd, unsigned int newvec,
126 unsigned int newcpu)
127{
128 struct apic_chip_data *apicd = apic_chip_data(irqd);
129 struct irq_desc *desc = irq_data_to_desc(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800130
Thomas Gleixner69cde002017-09-13 23:29:42 +0200131 lockdep_assert_held(&vector_lock);
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000132
Thomas Gleixner69cde002017-09-13 23:29:42 +0200133 trace_vector_update(irqd->irq, newvec, newcpu, apicd->cfg.vector,
134 apicd->cpu);
Jiang Liu74afab72014-10-27 16:12:00 +0800135
Thomas Gleixner69cde002017-09-13 23:29:42 +0200136 /* Setup the vector move, if required */
137 if (apicd->cfg.vector && cpu_online(apicd->cpu)) {
138 apicd->move_in_progress = true;
139 apicd->cfg.old_vector = apicd->cfg.vector;
140 apicd->prev_cpu = apicd->cpu;
141 } else {
142 apicd->cfg.old_vector = 0;
Jiang Liu74afab72014-10-27 16:12:00 +0800143 }
Jiang Liu74afab72014-10-27 16:12:00 +0800144
Thomas Gleixner69cde002017-09-13 23:29:42 +0200145 apicd->cfg.vector = newvec;
146 apicd->cpu = newcpu;
147 BUG_ON(!IS_ERR_OR_NULL(per_cpu(vector_irq, newcpu)[newvec]));
148 per_cpu(vector_irq, newcpu)[newvec] = desc;
149}
150
151static int allocate_vector(struct irq_data *irqd, const struct cpumask *dest)
152{
153 struct apic_chip_data *apicd = apic_chip_data(irqd);
154 int vector = apicd->cfg.vector;
155 unsigned int cpu = apicd->cpu;
156
Thomas Gleixner847667e2015-12-31 16:30:50 +0000157 /*
Thomas Gleixner69cde002017-09-13 23:29:42 +0200158 * If the current target CPU is online and in the new requested
159 * affinity mask, there is no point in moving the interrupt from
160 * one CPU to another.
Thomas Gleixner847667e2015-12-31 16:30:50 +0000161 */
Thomas Gleixner69cde002017-09-13 23:29:42 +0200162 if (vector && cpu_online(cpu) && cpumask_test_cpu(cpu, dest))
163 return 0;
164
165 vector = irq_matrix_alloc(vector_matrix, dest, false, &cpu);
166 if (vector > 0)
167 apic_update_vector(irqd, vector, cpu);
168 trace_vector_alloc(irqd->irq, vector, false, vector);
169 return vector;
170}
171
172static int assign_vector_locked(struct irq_data *irqd,
173 const struct cpumask *dest)
174{
175 int vector = allocate_vector(irqd, dest);
176
177 if (vector < 0)
178 return vector;
179
180 apic_update_irq_cfg(irqd);
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000181 return 0;
Jiang Liu74afab72014-10-27 16:12:00 +0800182}
183
Thomas Gleixner69cde002017-09-13 23:29:42 +0200184static int assign_irq_vector(struct irq_data *irqd, const struct cpumask *dest)
Jiang Liu74afab72014-10-27 16:12:00 +0800185{
Jiang Liu74afab72014-10-27 16:12:00 +0800186 unsigned long flags;
Thomas Gleixner69cde002017-09-13 23:29:42 +0200187 int ret;
Jiang Liu74afab72014-10-27 16:12:00 +0800188
189 raw_spin_lock_irqsave(&vector_lock, flags);
Thomas Gleixner69cde002017-09-13 23:29:42 +0200190 cpumask_and(vector_searchmask, dest, cpu_online_mask);
191 ret = assign_vector_locked(irqd, vector_searchmask);
Jiang Liu74afab72014-10-27 16:12:00 +0800192 raw_spin_unlock_irqrestore(&vector_lock, flags);
Thomas Gleixner69cde002017-09-13 23:29:42 +0200193 return ret;
Jiang Liu74afab72014-10-27 16:12:00 +0800194}
195
Thomas Gleixner69cde002017-09-13 23:29:42 +0200196static int assign_irq_vector_policy(struct irq_data *irqd,
197 struct irq_alloc_info *info, int node)
Jiang Liu486ca532015-05-07 10:53:56 +0800198{
Thomas Gleixner258d86e2017-09-13 23:29:35 +0200199 if (info->mask)
Thomas Gleixner69cde002017-09-13 23:29:42 +0200200 return assign_irq_vector(irqd, info->mask);
Jiang Liu486ca532015-05-07 10:53:56 +0800201 if (node != NUMA_NO_NODE &&
Thomas Gleixner69cde002017-09-13 23:29:42 +0200202 !assign_irq_vector(irqd, cpumask_of_node(node)))
Jiang Liu486ca532015-05-07 10:53:56 +0800203 return 0;
Thomas Gleixner69cde002017-09-13 23:29:42 +0200204 return assign_irq_vector(irqd, cpu_online_mask);
Jiang Liu486ca532015-05-07 10:53:56 +0800205}
206
Thomas Gleixner69cde002017-09-13 23:29:42 +0200207static void clear_irq_vector(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +0800208{
Thomas Gleixner69cde002017-09-13 23:29:42 +0200209 struct apic_chip_data *apicd = apic_chip_data(irqd);
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200210 unsigned int vector = apicd->cfg.vector;
Jiang Liu74afab72014-10-27 16:12:00 +0800211
Thomas Gleixner69cde002017-09-13 23:29:42 +0200212 lockdep_assert_held(&vector_lock);
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200213 if (!vector)
Keith Busch1bdb8972016-04-27 14:22:32 -0600214 return;
Jiang Liu74afab72014-10-27 16:12:00 +0800215
Thomas Gleixner69cde002017-09-13 23:29:42 +0200216 trace_vector_clear(irqd->irq, vector, apicd->cpu, apicd->cfg.old_vector,
217 apicd->prev_cpu);
218
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200219 per_cpu(vector_irq, apicd->cpu)[vector] = VECTOR_UNUSED;
Thomas Gleixner69cde002017-09-13 23:29:42 +0200220 irq_matrix_free(vector_matrix, apicd->cpu, vector, false);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200221 apicd->cfg.vector = 0;
Jiang Liu74afab72014-10-27 16:12:00 +0800222
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200223 /* Clean up move in progress */
224 vector = apicd->cfg.old_vector;
225 if (!vector)
Jiang Liu74afab72014-10-27 16:12:00 +0800226 return;
Jiang Liu74afab72014-10-27 16:12:00 +0800227
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200228 per_cpu(vector_irq, apicd->prev_cpu)[vector] = VECTOR_UNUSED;
Thomas Gleixner69cde002017-09-13 23:29:42 +0200229 irq_matrix_free(vector_matrix, apicd->prev_cpu, vector, false);
230 apicd->cfg.old_vector = 0;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200231 apicd->move_in_progress = 0;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200232 hlist_del_init(&apicd->clist);
Jiang Liu74afab72014-10-27 16:12:00 +0800233}
234
Jiang Liub5dc8e62015-04-13 14:11:24 +0800235static void x86_vector_free_irqs(struct irq_domain *domain,
236 unsigned int virq, unsigned int nr_irqs)
237{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200238 struct apic_chip_data *apicd;
239 struct irq_data *irqd;
Jiang Liu111abeb2015-12-31 16:30:44 +0000240 unsigned long flags;
Jiang Liub5dc8e62015-04-13 14:11:24 +0800241 int i;
242
243 for (i = 0; i < nr_irqs; i++) {
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200244 irqd = irq_domain_get_irq_data(x86_vector_domain, virq + i);
245 if (irqd && irqd->chip_data) {
Jiang Liu111abeb2015-12-31 16:30:44 +0000246 raw_spin_lock_irqsave(&vector_lock, flags);
Thomas Gleixner69cde002017-09-13 23:29:42 +0200247 clear_irq_vector(irqd);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200248 apicd = irqd->chip_data;
249 irq_domain_reset_irq_data(irqd);
Jiang Liu111abeb2015-12-31 16:30:44 +0000250 raw_spin_unlock_irqrestore(&vector_lock, flags);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200251 free_apic_chip_data(apicd);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800252 }
253 }
254}
255
256static int x86_vector_alloc_irqs(struct irq_domain *domain, unsigned int virq,
257 unsigned int nr_irqs, void *arg)
258{
259 struct irq_alloc_info *info = arg;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200260 struct apic_chip_data *apicd;
261 struct irq_data *irqd;
Thomas Gleixner69cde002017-09-13 23:29:42 +0200262 unsigned long flags;
Jiang Liu5f2dbbc2015-06-01 16:05:14 +0800263 int i, err, node;
Jiang Liub5dc8e62015-04-13 14:11:24 +0800264
265 if (disable_apic)
266 return -ENXIO;
267
268 /* Currently vector allocator can't guarantee contiguous allocations */
269 if ((info->flags & X86_IRQ_ALLOC_CONTIGUOUS_VECTORS) && nr_irqs > 1)
270 return -ENOSYS;
271
Jiang Liub5dc8e62015-04-13 14:11:24 +0800272 for (i = 0; i < nr_irqs; i++) {
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200273 irqd = irq_domain_get_irq_data(domain, virq + i);
274 BUG_ON(!irqd);
275 node = irq_data_get_node(irqd);
Thomas Gleixner4ef76eb2017-09-13 23:29:34 +0200276 WARN_ON_ONCE(irqd->chip_data);
277 apicd = alloc_apic_chip_data(node);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200278 if (!apicd) {
Jiang Liub5dc8e62015-04-13 14:11:24 +0800279 err = -ENOMEM;
280 goto error;
281 }
282
Thomas Gleixner69cde002017-09-13 23:29:42 +0200283 apicd->irq = virq + i;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200284 irqd->chip = &lapic_controller;
285 irqd->chip_data = apicd;
286 irqd->hwirq = virq + i;
287 irqd_set_single_target(irqd);
Thomas Gleixner4ef76eb2017-09-13 23:29:34 +0200288 /*
Thomas Gleixner69cde002017-09-13 23:29:42 +0200289 * Legacy vectors are already assigned when the IOAPIC
290 * takes them over. They stay on the same vector. This is
291 * required for check_timer() to work correctly as it might
292 * switch back to legacy mode. Only update the hardware
293 * config.
Thomas Gleixner4ef76eb2017-09-13 23:29:34 +0200294 */
295 if (info->flags & X86_IRQ_ALLOC_LEGACY) {
296 apicd->cfg.vector = ISA_IRQ_VECTOR(virq + i);
297 apicd->cpu = 0;
Thomas Gleixner69cde002017-09-13 23:29:42 +0200298 trace_vector_setup(virq + i, true, 0);
299 raw_spin_lock_irqsave(&vector_lock, flags);
300 apic_update_irq_cfg(irqd);
301 raw_spin_unlock_irqrestore(&vector_lock, flags);
302 continue;
Thomas Gleixner4ef76eb2017-09-13 23:29:34 +0200303 }
304
Thomas Gleixner69cde002017-09-13 23:29:42 +0200305 err = assign_irq_vector_policy(irqd, info, node);
306 trace_vector_setup(virq + i, false, err);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800307 if (err)
308 goto error;
309 }
310
311 return 0;
312
313error:
314 x86_vector_free_irqs(domain, virq, i + 1);
315 return err;
316}
317
Thomas Gleixner65d7ed52017-09-13 23:29:39 +0200318#ifdef CONFIG_GENERIC_IRQ_DEBUGFS
319void x86_vector_debug_show(struct seq_file *m, struct irq_domain *d,
320 struct irq_data *irqd, int ind)
321{
322 unsigned int cpu, vec, prev_cpu, prev_vec;
323 struct apic_chip_data *apicd;
324 unsigned long flags;
325 int irq;
326
327 if (!irqd) {
328 irq_matrix_debug_show(m, vector_matrix, ind);
329 return;
330 }
331
332 irq = irqd->irq;
333 if (irq < nr_legacy_irqs() && !test_bit(irq, &io_apic_irqs)) {
334 seq_printf(m, "%*sVector: %5d\n", ind, "", ISA_IRQ_VECTOR(irq));
335 seq_printf(m, "%*sTarget: Legacy PIC all CPUs\n", ind, "");
336 return;
337 }
338
339 apicd = irqd->chip_data;
340 if (!apicd) {
341 seq_printf(m, "%*sVector: Not assigned\n", ind, "");
342 return;
343 }
344
345 raw_spin_lock_irqsave(&vector_lock, flags);
346 cpu = apicd->cpu;
347 vec = apicd->cfg.vector;
348 prev_cpu = apicd->prev_cpu;
349 prev_vec = apicd->cfg.old_vector;
350 raw_spin_unlock_irqrestore(&vector_lock, flags);
351 seq_printf(m, "%*sVector: %5u\n", ind, "", vec);
352 seq_printf(m, "%*sTarget: %5u\n", ind, "", cpu);
353 if (prev_vec) {
354 seq_printf(m, "%*sPrevious vector: %5u\n", ind, "", prev_vec);
355 seq_printf(m, "%*sPrevious target: %5u\n", ind, "", prev_cpu);
356 }
357}
358#endif
359
Thomas Gleixnereb18cf52015-05-05 11:10:11 +0200360static const struct irq_domain_ops x86_vector_domain_ops = {
Thomas Gleixner65d7ed52017-09-13 23:29:39 +0200361 .alloc = x86_vector_alloc_irqs,
362 .free = x86_vector_free_irqs,
363#ifdef CONFIG_GENERIC_IRQ_DEBUGFS
364 .debug_show = x86_vector_debug_show,
365#endif
Jiang Liub5dc8e62015-04-13 14:11:24 +0800366};
367
Jiang Liu11d686e2014-10-27 16:12:05 +0800368int __init arch_probe_nr_irqs(void)
369{
370 int nr;
371
372 if (nr_irqs > (NR_VECTORS * nr_cpu_ids))
373 nr_irqs = NR_VECTORS * nr_cpu_ids;
374
375 nr = (gsi_top + nr_legacy_irqs()) + 8 * nr_cpu_ids;
376#if defined(CONFIG_PCI_MSI) || defined(CONFIG_HT_IRQ)
377 /*
378 * for MSI and HT dyn irq
379 */
380 if (gsi_top <= NR_IRQS_LEGACY)
381 nr += 8 * nr_cpu_ids;
382 else
383 nr += gsi_top * 16;
384#endif
385 if (nr < nr_irqs)
386 nr_irqs = nr;
387
Vitaly Kuznetsov8c058b02015-11-03 10:40:14 +0100388 /*
389 * We don't know if PIC is present at this point so we need to do
390 * probe() to get the right number of legacy IRQs.
391 */
392 return legacy_pic->probe();
Jiang Liu11d686e2014-10-27 16:12:05 +0800393}
394
Thomas Gleixner0fa115d2017-09-13 23:29:38 +0200395void lapic_assign_legacy_vector(unsigned int irq, bool replace)
396{
397 /*
398 * Use assign system here so it wont get accounted as allocated
399 * and moveable in the cpu hotplug check and it prevents managed
400 * irq reservation from touching it.
401 */
402 irq_matrix_assign_system(vector_matrix, ISA_IRQ_VECTOR(irq), replace);
403}
404
405void __init lapic_assign_system_vectors(void)
406{
407 unsigned int i, vector = 0;
408
409 for_each_set_bit_from(vector, system_vectors, NR_VECTORS)
410 irq_matrix_assign_system(vector_matrix, vector, false);
411
412 if (nr_legacy_irqs() > 1)
413 lapic_assign_legacy_vector(PIC_CASCADE_IR, false);
414
415 /* System vectors are reserved, online it */
416 irq_matrix_online(vector_matrix);
417
418 /* Mark the preallocated legacy interrupts */
419 for (i = 0; i < nr_legacy_irqs(); i++) {
420 if (i != PIC_CASCADE_IR)
421 irq_matrix_assign(vector_matrix, ISA_IRQ_VECTOR(i));
422 }
423}
424
Jiang Liu11d686e2014-10-27 16:12:05 +0800425int __init arch_early_irq_init(void)
426{
Thomas Gleixner9d35f852017-06-20 01:37:06 +0200427 struct fwnode_handle *fn;
428
Thomas Gleixner9d35f852017-06-20 01:37:06 +0200429 fn = irq_domain_alloc_named_fwnode("VECTOR");
430 BUG_ON(!fn);
431 x86_vector_domain = irq_domain_create_tree(fn, &x86_vector_domain_ops,
432 NULL);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800433 BUG_ON(x86_vector_domain == NULL);
Thomas Gleixner9d35f852017-06-20 01:37:06 +0200434 irq_domain_free_fwnode(fn);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800435 irq_set_default_host(x86_vector_domain);
436
Jiang Liu52f518a2015-04-13 14:11:35 +0800437 arch_init_msi_domain(x86_vector_domain);
Jiang Liu49e07d82015-04-13 14:11:43 +0800438 arch_init_htirq_domain(x86_vector_domain);
Jiang Liu52f518a2015-04-13 14:11:35 +0800439
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000440 BUG_ON(!alloc_cpumask_var(&vector_searchmask, GFP_KERNEL));
Jiang Liuf7fa7ae2015-04-14 10:30:10 +0800441
Thomas Gleixner0fa115d2017-09-13 23:29:38 +0200442 /*
443 * Allocate the vector matrix allocator data structure and limit the
444 * search area.
445 */
446 vector_matrix = irq_alloc_matrix(NR_VECTORS, FIRST_EXTERNAL_VECTOR,
447 FIRST_SYSTEM_VECTOR);
448 BUG_ON(!vector_matrix);
449
Jiang Liu11d686e2014-10-27 16:12:05 +0800450 return arch_early_ioapic_init();
451}
452
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200453/* Temporary hack to keep things working */
454static void vector_update_shutdown_irqs(void)
Jiang Liu74afab72014-10-27 16:12:00 +0800455{
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000456 struct irq_desc *desc;
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200457 int irq;
Jiang Liu74afab72014-10-27 16:12:00 +0800458
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000459 for_each_irq_desc(irq, desc) {
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200460 struct irq_data *irqd = irq_desc_get_irq_data(desc);
461 struct apic_chip_data *ad = apic_chip_data(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800462
Thomas Gleixner69cde002017-09-13 23:29:42 +0200463 if (!ad || !ad->cfg.vector || ad->cpu != smp_processor_id())
464 continue;
465 this_cpu_write(vector_irq[ad->cfg.vector], desc);
466 irq_matrix_assign(vector_matrix, ad->cfg.vector);
Jiang Liu74afab72014-10-27 16:12:00 +0800467 }
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200468}
Jiang Liu74afab72014-10-27 16:12:00 +0800469
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200470static struct irq_desc *__setup_vector_irq(int vector)
471{
472 int isairq = vector - ISA_IRQ_VECTOR(0);
473
474 /* Check whether the irq is in the legacy space */
475 if (isairq < 0 || isairq >= nr_legacy_irqs())
476 return VECTOR_UNUSED;
477 /* Check whether the irq is handled by the IOAPIC */
478 if (test_bit(isairq, &io_apic_irqs))
479 return VECTOR_UNUSED;
480 return irq_to_desc(isairq);
Jiang Liu74afab72014-10-27 16:12:00 +0800481}
482
Thomas Gleixner0fa115d2017-09-13 23:29:38 +0200483/* Online the local APIC infrastructure and initialize the vectors */
484void lapic_online(void)
Jiang Liu74afab72014-10-27 16:12:00 +0800485{
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200486 unsigned int vector;
Jiang Liu74afab72014-10-27 16:12:00 +0800487
Thomas Gleixner5a3f75e2015-07-05 17:12:32 +0000488 lockdep_assert_held(&vector_lock);
Thomas Gleixner0fa115d2017-09-13 23:29:38 +0200489
490 /* Online the vector matrix array for this CPU */
491 irq_matrix_online(vector_matrix);
492
Jiang Liu74afab72014-10-27 16:12:00 +0800493 /*
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200494 * The interrupt affinity logic never targets interrupts to offline
495 * CPUs. The exception are the legacy PIC interrupts. In general
496 * they are only targeted to CPU0, but depending on the platform
497 * they can be distributed to any online CPU in hardware. The
498 * kernel has no influence on that. So all active legacy vectors
499 * must be installed on all CPUs. All non legacy interrupts can be
500 * cleared.
Jiang Liu74afab72014-10-27 16:12:00 +0800501 */
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200502 for (vector = 0; vector < NR_VECTORS; vector++)
503 this_cpu_write(vector_irq[vector], __setup_vector_irq(vector));
Jiang Liu74afab72014-10-27 16:12:00 +0800504
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200505 /*
506 * Until the rewrite of the managed interrupt management is in
507 * place it's necessary to walk the irq descriptors and check for
508 * interrupts which are targeted at this CPU.
509 */
510 vector_update_shutdown_irqs();
Jiang Liu74afab72014-10-27 16:12:00 +0800511}
512
Thomas Gleixner0fa115d2017-09-13 23:29:38 +0200513void lapic_offline(void)
514{
515 lock_vector_lock();
516 irq_matrix_offline(vector_matrix);
517 unlock_vector_lock();
518}
519
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200520static int apic_retrigger_irq(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +0800521{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200522 struct apic_chip_data *apicd = apic_chip_data(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800523 unsigned long flags;
Jiang Liu74afab72014-10-27 16:12:00 +0800524
525 raw_spin_lock_irqsave(&vector_lock, flags);
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200526 apic->send_IPI(apicd->cpu, apicd->cfg.vector);
Jiang Liu74afab72014-10-27 16:12:00 +0800527 raw_spin_unlock_irqrestore(&vector_lock, flags);
528
529 return 1;
530}
531
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200532void apic_ack_edge(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +0800533{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200534 irq_complete_move(irqd_cfg(irqd));
535 irq_move_irq(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800536 ack_APIC_irq();
537}
538
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200539static int apic_set_affinity(struct irq_data *irqd,
Jiang Liu68f9f442015-04-14 10:30:01 +0800540 const struct cpumask *dest, bool force)
Jiang Liub5dc8e62015-04-13 14:11:24 +0800541{
Thomas Gleixner69cde002017-09-13 23:29:42 +0200542 int err;
Jiang Liub5dc8e62015-04-13 14:11:24 +0800543
Masahiro Yamada97f26452016-08-03 13:45:50 -0700544 if (!IS_ENABLED(CONFIG_SMP))
Jiang Liub5dc8e62015-04-13 14:11:24 +0800545 return -EPERM;
546
547 if (!cpumask_intersects(dest, cpu_online_mask))
548 return -EINVAL;
549
Thomas Gleixner69cde002017-09-13 23:29:42 +0200550 err = assign_irq_vector(irqd, dest);
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000551 return err ? err : IRQ_SET_MASK_OK;
Jiang Liub5dc8e62015-04-13 14:11:24 +0800552}
553
554static struct irq_chip lapic_controller = {
Thomas Gleixner8947dfb2017-06-20 01:37:01 +0200555 .name = "APIC",
Jiang Liub5dc8e62015-04-13 14:11:24 +0800556 .irq_ack = apic_ack_edge,
Jiang Liu68f9f442015-04-14 10:30:01 +0800557 .irq_set_affinity = apic_set_affinity,
Jiang Liub5dc8e62015-04-13 14:11:24 +0800558 .irq_retrigger = apic_retrigger_irq,
559};
560
Jiang Liu74afab72014-10-27 16:12:00 +0800561#ifdef CONFIG_SMP
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200562
Thomas Gleixner69cde002017-09-13 23:29:42 +0200563static void free_moved_vector(struct apic_chip_data *apicd)
564{
565 unsigned int vector = apicd->cfg.old_vector;
566 unsigned int cpu = apicd->prev_cpu;
567
568 trace_vector_free_moved(apicd->irq, vector, false);
569 irq_matrix_free(vector_matrix, cpu, vector, false);
570 __this_cpu_write(vector_irq[vector], VECTOR_UNUSED);
571 hlist_del_init(&apicd->clist);
572 apicd->cfg.old_vector = 0;
573 apicd->move_in_progress = 0;
574}
575
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200576asmlinkage __visible void __irq_entry smp_irq_move_cleanup_interrupt(void)
577{
578 struct hlist_head *clhead = this_cpu_ptr(&cleanup_list);
579 struct apic_chip_data *apicd;
580 struct hlist_node *tmp;
581
582 entering_ack_irq();
583 /* Prevent vectors vanishing under us */
584 raw_spin_lock(&vector_lock);
585
586 hlist_for_each_entry_safe(apicd, tmp, clhead, clist) {
587 unsigned int irr, vector = apicd->cfg.old_vector;
588
589 /*
590 * Paranoia: Check if the vector that needs to be cleaned
591 * up is registered at the APICs IRR. If so, then this is
592 * not the best time to clean it up. Clean it up in the
593 * next attempt by sending another IRQ_MOVE_CLEANUP_VECTOR
594 * to this CPU. IRQ_MOVE_CLEANUP_VECTOR is the lowest
595 * priority external vector, so on return from this
596 * interrupt the device interrupt will happen first.
597 */
598 irr = apic_read(APIC_IRR + (vector / 32 * 0x10));
599 if (irr & (1U << (vector % 32))) {
600 apic->send_IPI_self(IRQ_MOVE_CLEANUP_VECTOR);
601 continue;
602 }
Thomas Gleixner69cde002017-09-13 23:29:42 +0200603 free_moved_vector(apicd);
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200604 }
605
606 raw_spin_unlock(&vector_lock);
607 exiting_irq();
608}
609
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200610static void __send_cleanup_vector(struct apic_chip_data *apicd)
Jiang Liu74afab72014-10-27 16:12:00 +0800611{
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200612 unsigned int cpu;
613
Thomas Gleixnerc1684f52015-12-31 16:30:51 +0000614 raw_spin_lock(&vector_lock);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200615 apicd->move_in_progress = 0;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200616 cpu = apicd->prev_cpu;
617 if (cpu_online(cpu)) {
618 hlist_add_head(&apicd->clist, per_cpu_ptr(&cleanup_list, cpu));
619 apic->send_IPI(cpu, IRQ_MOVE_CLEANUP_VECTOR);
620 } else {
621 apicd->cfg.old_vector = 0;
622 }
Thomas Gleixnerc1684f52015-12-31 16:30:51 +0000623 raw_spin_unlock(&vector_lock);
Jiang Liu74afab72014-10-27 16:12:00 +0800624}
625
Jiang Liuc6c20022015-04-14 10:30:02 +0800626void send_cleanup_vector(struct irq_cfg *cfg)
627{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200628 struct apic_chip_data *apicd;
Jiang Liu7f3262e2015-04-14 10:30:03 +0800629
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200630 apicd = container_of(cfg, struct apic_chip_data, cfg);
631 if (apicd->move_in_progress)
632 __send_cleanup_vector(apicd);
Jiang Liuc6c20022015-04-14 10:30:02 +0800633}
634
Jiang Liu74afab72014-10-27 16:12:00 +0800635static void __irq_complete_move(struct irq_cfg *cfg, unsigned vector)
636{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200637 struct apic_chip_data *apicd;
Jiang Liu74afab72014-10-27 16:12:00 +0800638
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200639 apicd = container_of(cfg, struct apic_chip_data, cfg);
640 if (likely(!apicd->move_in_progress))
Jiang Liu74afab72014-10-27 16:12:00 +0800641 return;
642
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200643 if (vector == apicd->cfg.vector && apicd->cpu == smp_processor_id())
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200644 __send_cleanup_vector(apicd);
Jiang Liu74afab72014-10-27 16:12:00 +0800645}
646
647void irq_complete_move(struct irq_cfg *cfg)
648{
649 __irq_complete_move(cfg, ~get_irq_regs()->orig_ax);
650}
651
Thomas Gleixner90a22822015-12-31 16:30:53 +0000652/*
Thomas Gleixner551adc62016-03-14 09:40:46 +0100653 * Called from fixup_irqs() with @desc->lock held and interrupts disabled.
Thomas Gleixner90a22822015-12-31 16:30:53 +0000654 */
655void irq_force_complete_move(struct irq_desc *desc)
Jiang Liu74afab72014-10-27 16:12:00 +0800656{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200657 struct apic_chip_data *apicd;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200658 struct irq_data *irqd;
659 unsigned int vector;
Jiang Liu74afab72014-10-27 16:12:00 +0800660
Mika Westerbergdb91aa72016-10-03 13:17:08 +0300661 /*
662 * The function is called for all descriptors regardless of which
663 * irqdomain they belong to. For example if an IRQ is provided by
664 * an irq_chip as part of a GPIO driver, the chip data for that
665 * descriptor is specific to the irq_chip in question.
666 *
667 * Check first that the chip_data is what we expect
668 * (apic_chip_data) before touching it any further.
669 */
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200670 irqd = irq_domain_get_irq_data(x86_vector_domain,
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200671 irq_desc_get_irq(desc));
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200672 if (!irqd)
Mika Westerbergdb91aa72016-10-03 13:17:08 +0300673 return;
674
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200675 raw_spin_lock(&vector_lock);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200676 apicd = apic_chip_data(irqd);
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200677 if (!apicd)
678 goto unlock;
Thomas Gleixner56d7d2f2015-12-31 16:30:52 +0000679
Thomas Gleixner56d7d2f2015-12-31 16:30:52 +0000680 /*
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200681 * If old_vector is empty, no action required.
682 */
683 vector = apicd->cfg.old_vector;
684 if (!vector)
685 goto unlock;
686
687 /*
688 * This is tricky. If the cleanup of the old vector has not been
Thomas Gleixner98229aa2015-12-31 16:30:54 +0000689 * done yet, then the following setaffinity call will fail with
690 * -EBUSY. This can leave the interrupt in a stale state.
691 *
Thomas Gleixner551adc62016-03-14 09:40:46 +0100692 * All CPUs are stuck in stop machine with interrupts disabled so
693 * calling __irq_complete_move() would be completely pointless.
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200694 *
Thomas Gleixner551adc62016-03-14 09:40:46 +0100695 * 1) The interrupt is in move_in_progress state. That means that we
696 * have not seen an interrupt since the io_apic was reprogrammed to
697 * the new vector.
698 *
699 * 2) The interrupt has fired on the new vector, but the cleanup IPIs
700 * have not been processed yet.
701 */
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200702 if (apicd->move_in_progress) {
Thomas Gleixner551adc62016-03-14 09:40:46 +0100703 /*
704 * In theory there is a race:
705 *
706 * set_ioapic(new_vector) <-- Interrupt is raised before update
707 * is effective, i.e. it's raised on
708 * the old vector.
709 *
710 * So if the target cpu cannot handle that interrupt before
711 * the old vector is cleaned up, we get a spurious interrupt
712 * and in the worst case the ioapic irq line becomes stale.
713 *
714 * But in case of cpu hotplug this should be a non issue
715 * because if the affinity update happens right before all
716 * cpus rendevouz in stop machine, there is no way that the
717 * interrupt can be blocked on the target cpu because all cpus
718 * loops first with interrupts enabled in stop machine, so the
719 * old vector is not yet cleaned up when the interrupt fires.
720 *
721 * So the only way to run into this issue is if the delivery
722 * of the interrupt on the apic/system bus would be delayed
723 * beyond the point where the target cpu disables interrupts
724 * in stop machine. I doubt that it can happen, but at least
725 * there is a theroretical chance. Virtualization might be
726 * able to expose this, but AFAICT the IOAPIC emulation is not
727 * as stupid as the real hardware.
728 *
729 * Anyway, there is nothing we can do about that at this point
730 * w/o refactoring the whole fixup_irq() business completely.
731 * We print at least the irq number and the old vector number,
732 * so we have the necessary information when a problem in that
733 * area arises.
734 */
735 pr_warn("IRQ fixup: irq %d move in progress, old vector %d\n",
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200736 irqd->irq, vector);
Thomas Gleixner551adc62016-03-14 09:40:46 +0100737 }
Thomas Gleixner69cde002017-09-13 23:29:42 +0200738 free_moved_vector(apicd);
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200739unlock:
Thomas Gleixner56d7d2f2015-12-31 16:30:52 +0000740 raw_spin_unlock(&vector_lock);
Jiang Liu74afab72014-10-27 16:12:00 +0800741}
Jiang Liu74afab72014-10-27 16:12:00 +0800742#endif
743
Jiang Liu74afab72014-10-27 16:12:00 +0800744static void __init print_APIC_field(int base)
745{
746 int i;
747
748 printk(KERN_DEBUG);
749
750 for (i = 0; i < 8; i++)
751 pr_cont("%08x", apic_read(base + i*0x10));
752
753 pr_cont("\n");
754}
755
756static void __init print_local_APIC(void *dummy)
757{
758 unsigned int i, v, ver, maxlvt;
759 u64 icr;
760
Jiang Liu849d3562014-10-27 16:12:01 +0800761 pr_debug("printing local APIC contents on CPU#%d/%d:\n",
762 smp_processor_id(), hard_smp_processor_id());
Jiang Liu74afab72014-10-27 16:12:00 +0800763 v = apic_read(APIC_ID);
Jiang Liu849d3562014-10-27 16:12:01 +0800764 pr_info("... APIC ID: %08x (%01x)\n", v, read_apic_id());
Jiang Liu74afab72014-10-27 16:12:00 +0800765 v = apic_read(APIC_LVR);
Jiang Liu849d3562014-10-27 16:12:01 +0800766 pr_info("... APIC VERSION: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800767 ver = GET_APIC_VERSION(v);
768 maxlvt = lapic_get_maxlvt();
769
770 v = apic_read(APIC_TASKPRI);
Jiang Liu849d3562014-10-27 16:12:01 +0800771 pr_debug("... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
Jiang Liu74afab72014-10-27 16:12:00 +0800772
773 /* !82489DX */
774 if (APIC_INTEGRATED(ver)) {
775 if (!APIC_XAPIC(ver)) {
776 v = apic_read(APIC_ARBPRI);
Jiang Liu849d3562014-10-27 16:12:01 +0800777 pr_debug("... APIC ARBPRI: %08x (%02x)\n",
778 v, v & APIC_ARBPRI_MASK);
Jiang Liu74afab72014-10-27 16:12:00 +0800779 }
780 v = apic_read(APIC_PROCPRI);
Jiang Liu849d3562014-10-27 16:12:01 +0800781 pr_debug("... APIC PROCPRI: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800782 }
783
784 /*
785 * Remote read supported only in the 82489DX and local APIC for
786 * Pentium processors.
787 */
788 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
789 v = apic_read(APIC_RRR);
Jiang Liu849d3562014-10-27 16:12:01 +0800790 pr_debug("... APIC RRR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800791 }
792
793 v = apic_read(APIC_LDR);
Jiang Liu849d3562014-10-27 16:12:01 +0800794 pr_debug("... APIC LDR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800795 if (!x2apic_enabled()) {
796 v = apic_read(APIC_DFR);
Jiang Liu849d3562014-10-27 16:12:01 +0800797 pr_debug("... APIC DFR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800798 }
799 v = apic_read(APIC_SPIV);
Jiang Liu849d3562014-10-27 16:12:01 +0800800 pr_debug("... APIC SPIV: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800801
Jiang Liu849d3562014-10-27 16:12:01 +0800802 pr_debug("... APIC ISR field:\n");
Jiang Liu74afab72014-10-27 16:12:00 +0800803 print_APIC_field(APIC_ISR);
Jiang Liu849d3562014-10-27 16:12:01 +0800804 pr_debug("... APIC TMR field:\n");
Jiang Liu74afab72014-10-27 16:12:00 +0800805 print_APIC_field(APIC_TMR);
Jiang Liu849d3562014-10-27 16:12:01 +0800806 pr_debug("... APIC IRR field:\n");
Jiang Liu74afab72014-10-27 16:12:00 +0800807 print_APIC_field(APIC_IRR);
808
809 /* !82489DX */
810 if (APIC_INTEGRATED(ver)) {
811 /* Due to the Pentium erratum 3AP. */
812 if (maxlvt > 3)
813 apic_write(APIC_ESR, 0);
814
815 v = apic_read(APIC_ESR);
Jiang Liu849d3562014-10-27 16:12:01 +0800816 pr_debug("... APIC ESR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800817 }
818
819 icr = apic_icr_read();
Jiang Liu849d3562014-10-27 16:12:01 +0800820 pr_debug("... APIC ICR: %08x\n", (u32)icr);
821 pr_debug("... APIC ICR2: %08x\n", (u32)(icr >> 32));
Jiang Liu74afab72014-10-27 16:12:00 +0800822
823 v = apic_read(APIC_LVTT);
Jiang Liu849d3562014-10-27 16:12:01 +0800824 pr_debug("... APIC LVTT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800825
826 if (maxlvt > 3) {
827 /* PC is LVT#4. */
828 v = apic_read(APIC_LVTPC);
Jiang Liu849d3562014-10-27 16:12:01 +0800829 pr_debug("... APIC LVTPC: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800830 }
831 v = apic_read(APIC_LVT0);
Jiang Liu849d3562014-10-27 16:12:01 +0800832 pr_debug("... APIC LVT0: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800833 v = apic_read(APIC_LVT1);
Jiang Liu849d3562014-10-27 16:12:01 +0800834 pr_debug("... APIC LVT1: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800835
836 if (maxlvt > 2) {
837 /* ERR is LVT#3. */
838 v = apic_read(APIC_LVTERR);
Jiang Liu849d3562014-10-27 16:12:01 +0800839 pr_debug("... APIC LVTERR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800840 }
841
842 v = apic_read(APIC_TMICT);
Jiang Liu849d3562014-10-27 16:12:01 +0800843 pr_debug("... APIC TMICT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800844 v = apic_read(APIC_TMCCT);
Jiang Liu849d3562014-10-27 16:12:01 +0800845 pr_debug("... APIC TMCCT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800846 v = apic_read(APIC_TDCR);
Jiang Liu849d3562014-10-27 16:12:01 +0800847 pr_debug("... APIC TDCR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800848
849 if (boot_cpu_has(X86_FEATURE_EXTAPIC)) {
850 v = apic_read(APIC_EFEAT);
851 maxlvt = (v >> 16) & 0xff;
Jiang Liu849d3562014-10-27 16:12:01 +0800852 pr_debug("... APIC EFEAT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800853 v = apic_read(APIC_ECTRL);
Jiang Liu849d3562014-10-27 16:12:01 +0800854 pr_debug("... APIC ECTRL: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800855 for (i = 0; i < maxlvt; i++) {
856 v = apic_read(APIC_EILVTn(i));
Jiang Liu849d3562014-10-27 16:12:01 +0800857 pr_debug("... APIC EILVT%d: %08x\n", i, v);
Jiang Liu74afab72014-10-27 16:12:00 +0800858 }
859 }
860 pr_cont("\n");
861}
862
863static void __init print_local_APICs(int maxcpu)
864{
865 int cpu;
866
867 if (!maxcpu)
868 return;
869
870 preempt_disable();
871 for_each_online_cpu(cpu) {
872 if (cpu >= maxcpu)
873 break;
874 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
875 }
876 preempt_enable();
877}
878
879static void __init print_PIC(void)
880{
881 unsigned int v;
882 unsigned long flags;
883
884 if (!nr_legacy_irqs())
885 return;
886
Jiang Liu849d3562014-10-27 16:12:01 +0800887 pr_debug("\nprinting PIC contents\n");
Jiang Liu74afab72014-10-27 16:12:00 +0800888
889 raw_spin_lock_irqsave(&i8259A_lock, flags);
890
891 v = inb(0xa1) << 8 | inb(0x21);
Jiang Liu849d3562014-10-27 16:12:01 +0800892 pr_debug("... PIC IMR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800893
894 v = inb(0xa0) << 8 | inb(0x20);
Jiang Liu849d3562014-10-27 16:12:01 +0800895 pr_debug("... PIC IRR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800896
897 outb(0x0b, 0xa0);
898 outb(0x0b, 0x20);
899 v = inb(0xa0) << 8 | inb(0x20);
900 outb(0x0a, 0xa0);
901 outb(0x0a, 0x20);
902
903 raw_spin_unlock_irqrestore(&i8259A_lock, flags);
904
Jiang Liu849d3562014-10-27 16:12:01 +0800905 pr_debug("... PIC ISR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800906
907 v = inb(0x4d1) << 8 | inb(0x4d0);
Jiang Liu849d3562014-10-27 16:12:01 +0800908 pr_debug("... PIC ELCR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800909}
910
911static int show_lapic __initdata = 1;
912static __init int setup_show_lapic(char *arg)
913{
914 int num = -1;
915
916 if (strcmp(arg, "all") == 0) {
917 show_lapic = CONFIG_NR_CPUS;
918 } else {
919 get_option(&arg, &num);
920 if (num >= 0)
921 show_lapic = num;
922 }
923
924 return 1;
925}
926__setup("show_lapic=", setup_show_lapic);
927
928static int __init print_ICs(void)
929{
930 if (apic_verbosity == APIC_QUIET)
931 return 0;
932
933 print_PIC();
934
935 /* don't print out if apic is not there */
Borislav Petkov93984fb2016-04-04 22:25:00 +0200936 if (!boot_cpu_has(X86_FEATURE_APIC) && !apic_from_smp_config())
Jiang Liu74afab72014-10-27 16:12:00 +0800937 return 0;
938
939 print_local_APICs(show_lapic);
940 print_IO_APICs();
941
942 return 0;
943}
944
945late_initcall(print_ICs);