blob: 0092a6e0d5ee6e79c0306e020f4560a0e0f98e00 [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>
16#include <linux/irqdomain.h>
17#include <linux/slab.h>
18#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 Liub5dc8e62015-04-13 14:11:24 +080024struct irq_domain *x86_vector_domain;
Jiang Liu74afab72014-10-27 16:12:00 +080025static DEFINE_RAW_SPINLOCK(vector_lock);
Jiang Liub5dc8e62015-04-13 14:11:24 +080026static struct irq_chip lapic_controller;
Jiang Liu13315322015-04-13 14:11:56 +080027#ifdef CONFIG_X86_IO_APIC
28static struct irq_cfg *legacy_irq_cfgs[NR_IRQS_LEGACY];
29#endif
Jiang Liu74afab72014-10-27 16:12:00 +080030
31void lock_vector_lock(void)
32{
33 /* Used to the online set of cpus does not change
34 * during assign_irq_vector.
35 */
36 raw_spin_lock(&vector_lock);
37}
38
39void unlock_vector_lock(void)
40{
41 raw_spin_unlock(&vector_lock);
42}
43
44struct irq_cfg *irq_cfg(unsigned int irq)
45{
Jiang Liub5dc8e62015-04-13 14:11:24 +080046 return irqd_cfg(irq_get_irq_data(irq));
Jiang Liu74afab72014-10-27 16:12:00 +080047}
48
49struct irq_cfg *irqd_cfg(struct irq_data *irq_data)
50{
Jiang Liub5dc8e62015-04-13 14:11:24 +080051 if (!irq_data)
52 return NULL;
53
54 while (irq_data->parent_data)
55 irq_data = irq_data->parent_data;
56
Jiang Liu74afab72014-10-27 16:12:00 +080057 return irq_data->chip_data;
58}
59
Jiang Liub5dc8e62015-04-13 14:11:24 +080060static struct irq_cfg *alloc_irq_cfg(int node)
Jiang Liu74afab72014-10-27 16:12:00 +080061{
62 struct irq_cfg *cfg;
63
64 cfg = kzalloc_node(sizeof(*cfg), GFP_KERNEL, node);
65 if (!cfg)
66 return NULL;
67 if (!zalloc_cpumask_var_node(&cfg->domain, GFP_KERNEL, node))
68 goto out_cfg;
69 if (!zalloc_cpumask_var_node(&cfg->old_domain, GFP_KERNEL, node))
70 goto out_domain;
Jiang Liu74afab72014-10-27 16:12:00 +080071 return cfg;
72out_domain:
73 free_cpumask_var(cfg->domain);
74out_cfg:
75 kfree(cfg);
76 return NULL;
77}
78
Jiang Liub5dc8e62015-04-13 14:11:24 +080079static void free_irq_cfg(struct irq_cfg *cfg)
Jiang Liu74afab72014-10-27 16:12:00 +080080{
Jiang Liub5dc8e62015-04-13 14:11:24 +080081 if (cfg) {
82 free_cpumask_var(cfg->domain);
83 free_cpumask_var(cfg->old_domain);
84 kfree(cfg);
85 }
Jiang Liu74afab72014-10-27 16:12:00 +080086}
87
88static int
89__assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
90{
91 /*
92 * NOTE! The local APIC isn't very good at handling
93 * multiple interrupts at the same interrupt level.
94 * As the interrupt level is determined by taking the
95 * vector number and shifting that right by 4, we
96 * want to spread these out a bit so that they don't
97 * all fall in the same interrupt level.
98 *
99 * Also, we've got to be careful not to trash gate
100 * 0x80, because int 0x80 is hm, kind of importantish. ;)
101 */
102 static int current_vector = FIRST_EXTERNAL_VECTOR + VECTOR_OFFSET_START;
103 static int current_offset = VECTOR_OFFSET_START % 16;
104 int cpu, err;
105 cpumask_var_t tmp_mask;
106
107 if (cfg->move_in_progress)
108 return -EBUSY;
109
110 if (!alloc_cpumask_var(&tmp_mask, GFP_ATOMIC))
111 return -ENOMEM;
112
113 /* Only try and allocate irqs on cpus that are present */
114 err = -ENOSPC;
115 cpumask_clear(cfg->old_domain);
116 cpu = cpumask_first_and(mask, cpu_online_mask);
117 while (cpu < nr_cpu_ids) {
118 int new_cpu, vector, offset;
119
120 apic->vector_allocation_domain(cpu, tmp_mask, mask);
121
122 if (cpumask_subset(tmp_mask, cfg->domain)) {
123 err = 0;
124 if (cpumask_equal(tmp_mask, cfg->domain))
125 break;
126 /*
127 * New cpumask using the vector is a proper subset of
128 * the current in use mask. So cleanup the vector
129 * allocation for the members that are not used anymore.
130 */
131 cpumask_andnot(cfg->old_domain, cfg->domain, tmp_mask);
132 cfg->move_in_progress =
133 cpumask_intersects(cfg->old_domain, cpu_online_mask);
134 cpumask_and(cfg->domain, cfg->domain, tmp_mask);
135 break;
136 }
137
138 vector = current_vector;
139 offset = current_offset;
140next:
141 vector += 16;
142 if (vector >= first_system_vector) {
143 offset = (offset + 1) % 16;
144 vector = FIRST_EXTERNAL_VECTOR + offset;
145 }
146
147 if (unlikely(current_vector == vector)) {
148 cpumask_or(cfg->old_domain, cfg->old_domain, tmp_mask);
149 cpumask_andnot(tmp_mask, mask, cfg->old_domain);
150 cpu = cpumask_first_and(tmp_mask, cpu_online_mask);
151 continue;
152 }
153
154 if (test_bit(vector, used_vectors))
155 goto next;
156
157 for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask) {
158 if (per_cpu(vector_irq, new_cpu)[vector] >
159 VECTOR_UNDEFINED)
160 goto next;
161 }
162 /* Found one! */
163 current_vector = vector;
164 current_offset = offset;
165 if (cfg->vector) {
166 cpumask_copy(cfg->old_domain, cfg->domain);
167 cfg->move_in_progress =
168 cpumask_intersects(cfg->old_domain, cpu_online_mask);
169 }
170 for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask)
171 per_cpu(vector_irq, new_cpu)[vector] = irq;
172 cfg->vector = vector;
173 cpumask_copy(cfg->domain, tmp_mask);
174 err = 0;
175 break;
176 }
177 free_cpumask_var(tmp_mask);
178
Jiang Liu5f0052f2015-04-13 14:11:23 +0800179 if (!err) {
180 /* cache destination APIC IDs into cfg->dest_apicid */
181 err = apic->cpu_mask_to_apicid_and(mask, cfg->domain,
182 &cfg->dest_apicid);
183 }
184
Jiang Liu74afab72014-10-27 16:12:00 +0800185 return err;
186}
187
Jiang Liuf9705102015-04-14 10:30:00 +0800188static int assign_irq_vector(int irq, struct irq_cfg *cfg,
189 const struct cpumask *mask)
Jiang Liu74afab72014-10-27 16:12:00 +0800190{
191 int err;
192 unsigned long flags;
193
194 raw_spin_lock_irqsave(&vector_lock, flags);
195 err = __assign_irq_vector(irq, cfg, mask);
196 raw_spin_unlock_irqrestore(&vector_lock, flags);
197 return err;
198}
199
Jiang Liuf9705102015-04-14 10:30:00 +0800200static void clear_irq_vector(int irq, struct irq_cfg *cfg)
Jiang Liu74afab72014-10-27 16:12:00 +0800201{
202 int cpu, vector;
203 unsigned long flags;
204
205 raw_spin_lock_irqsave(&vector_lock, flags);
206 BUG_ON(!cfg->vector);
207
208 vector = cfg->vector;
209 for_each_cpu_and(cpu, cfg->domain, cpu_online_mask)
210 per_cpu(vector_irq, cpu)[vector] = VECTOR_UNDEFINED;
211
212 cfg->vector = 0;
213 cpumask_clear(cfg->domain);
214
215 if (likely(!cfg->move_in_progress)) {
216 raw_spin_unlock_irqrestore(&vector_lock, flags);
217 return;
218 }
219
220 for_each_cpu_and(cpu, cfg->old_domain, cpu_online_mask) {
221 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS;
222 vector++) {
223 if (per_cpu(vector_irq, cpu)[vector] != irq)
224 continue;
225 per_cpu(vector_irq, cpu)[vector] = VECTOR_UNDEFINED;
226 break;
227 }
228 }
229 cfg->move_in_progress = 0;
230 raw_spin_unlock_irqrestore(&vector_lock, flags);
231}
232
Jiang Liub5dc8e62015-04-13 14:11:24 +0800233void init_irq_alloc_info(struct irq_alloc_info *info,
234 const struct cpumask *mask)
235{
236 memset(info, 0, sizeof(*info));
237 info->mask = mask;
238}
239
240void copy_irq_alloc_info(struct irq_alloc_info *dst, struct irq_alloc_info *src)
241{
242 if (src)
243 *dst = *src;
244 else
245 memset(dst, 0, sizeof(*dst));
246}
247
248static inline const struct cpumask *
249irq_alloc_info_get_mask(struct irq_alloc_info *info)
250{
251 return (!info || !info->mask) ? apic->target_cpus() : info->mask;
252}
253
254static void x86_vector_free_irqs(struct irq_domain *domain,
255 unsigned int virq, unsigned int nr_irqs)
256{
257 struct irq_data *irq_data;
258 int i;
259
260 for (i = 0; i < nr_irqs; i++) {
261 irq_data = irq_domain_get_irq_data(x86_vector_domain, virq + i);
262 if (irq_data && irq_data->chip_data) {
Jiang Liub5dc8e62015-04-13 14:11:24 +0800263 clear_irq_vector(virq + i, irq_data->chip_data);
264 free_irq_cfg(irq_data->chip_data);
Jiang Liu13315322015-04-13 14:11:56 +0800265#ifdef CONFIG_X86_IO_APIC
266 if (virq + i < nr_legacy_irqs())
267 legacy_irq_cfgs[virq + i] = NULL;
268#endif
Jiang Liub5dc8e62015-04-13 14:11:24 +0800269 irq_domain_reset_irq_data(irq_data);
270 }
271 }
272}
273
274static int x86_vector_alloc_irqs(struct irq_domain *domain, unsigned int virq,
275 unsigned int nr_irqs, void *arg)
276{
277 struct irq_alloc_info *info = arg;
278 const struct cpumask *mask;
279 struct irq_data *irq_data;
280 struct irq_cfg *cfg;
281 int i, err;
282
283 if (disable_apic)
284 return -ENXIO;
285
286 /* Currently vector allocator can't guarantee contiguous allocations */
287 if ((info->flags & X86_IRQ_ALLOC_CONTIGUOUS_VECTORS) && nr_irqs > 1)
288 return -ENOSYS;
289
290 mask = irq_alloc_info_get_mask(info);
291 for (i = 0; i < nr_irqs; i++) {
292 irq_data = irq_domain_get_irq_data(domain, virq + i);
293 BUG_ON(!irq_data);
Jiang Liu13315322015-04-13 14:11:56 +0800294#ifdef CONFIG_X86_IO_APIC
295 if (virq + i < nr_legacy_irqs() && legacy_irq_cfgs[virq + i])
296 cfg = legacy_irq_cfgs[virq + i];
297 else
298#endif
299 cfg = alloc_irq_cfg(irq_data->node);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800300 if (!cfg) {
301 err = -ENOMEM;
302 goto error;
303 }
304
305 irq_data->chip = &lapic_controller;
306 irq_data->chip_data = cfg;
307 irq_data->hwirq = virq + i;
308 err = assign_irq_vector(virq, cfg, mask);
309 if (err)
310 goto error;
311 }
312
313 return 0;
314
315error:
316 x86_vector_free_irqs(domain, virq, i + 1);
317 return err;
318}
319
320static struct irq_domain_ops x86_vector_domain_ops = {
321 .alloc = x86_vector_alloc_irqs,
322 .free = x86_vector_free_irqs,
323};
324
Jiang Liu11d686e2014-10-27 16:12:05 +0800325int __init arch_probe_nr_irqs(void)
326{
327 int nr;
328
329 if (nr_irqs > (NR_VECTORS * nr_cpu_ids))
330 nr_irqs = NR_VECTORS * nr_cpu_ids;
331
332 nr = (gsi_top + nr_legacy_irqs()) + 8 * nr_cpu_ids;
333#if defined(CONFIG_PCI_MSI) || defined(CONFIG_HT_IRQ)
334 /*
335 * for MSI and HT dyn irq
336 */
337 if (gsi_top <= NR_IRQS_LEGACY)
338 nr += 8 * nr_cpu_ids;
339 else
340 nr += gsi_top * 16;
341#endif
342 if (nr < nr_irqs)
343 nr_irqs = nr;
344
345 return nr_legacy_irqs();
346}
347
Jiang Liu13315322015-04-13 14:11:56 +0800348#ifdef CONFIG_X86_IO_APIC
349static void init_legacy_irqs(void)
350{
351 int i, node = cpu_to_node(0);
352 struct irq_cfg *cfg;
353
354 /*
355 * For legacy IRQ's, start with assigning irq0 to irq15 to
356 * IRQ0_VECTOR to IRQ15_VECTOR for all cpu's.
357 */
358 for (i = 0; i < nr_legacy_irqs(); i++) {
359 cfg = legacy_irq_cfgs[i] = alloc_irq_cfg(node);
360 BUG_ON(!cfg);
361 /*
362 * For legacy IRQ's, start with assigning irq0 to irq15 to
363 * IRQ0_VECTOR to IRQ15_VECTOR for all cpu's.
364 */
365 cfg->vector = IRQ0_VECTOR + i;
366 cpumask_setall(cfg->domain);
367 irq_set_chip_data(i, cfg);
368 }
369}
370#else
371static void init_legacy_irqs(void) { }
372#endif
373
Jiang Liu11d686e2014-10-27 16:12:05 +0800374int __init arch_early_irq_init(void)
375{
Jiang Liu13315322015-04-13 14:11:56 +0800376 init_legacy_irqs();
377
Jiang Liub5dc8e62015-04-13 14:11:24 +0800378 x86_vector_domain = irq_domain_add_tree(NULL, &x86_vector_domain_ops,
379 NULL);
380 BUG_ON(x86_vector_domain == NULL);
381 irq_set_default_host(x86_vector_domain);
382
Jiang Liu52f518a2015-04-13 14:11:35 +0800383 arch_init_msi_domain(x86_vector_domain);
Jiang Liu49e07d82015-04-13 14:11:43 +0800384 arch_init_htirq_domain(x86_vector_domain);
Jiang Liu52f518a2015-04-13 14:11:35 +0800385
Jiang Liu11d686e2014-10-27 16:12:05 +0800386 return arch_early_ioapic_init();
387}
388
Jiang Liu74afab72014-10-27 16:12:00 +0800389static void __setup_vector_irq(int cpu)
390{
391 /* Initialize vector_irq on a new cpu */
392 int irq, vector;
393 struct irq_cfg *cfg;
394
395 /*
396 * vector_lock will make sure that we don't run into irq vector
397 * assignments that might be happening on another cpu in parallel,
398 * while we setup our initial vector to irq mappings.
399 */
400 raw_spin_lock(&vector_lock);
401 /* Mark the inuse vectors */
402 for_each_active_irq(irq) {
403 cfg = irq_cfg(irq);
404 if (!cfg)
405 continue;
406
407 if (!cpumask_test_cpu(cpu, cfg->domain))
408 continue;
409 vector = cfg->vector;
410 per_cpu(vector_irq, cpu)[vector] = irq;
411 }
412 /* Mark the free vectors */
413 for (vector = 0; vector < NR_VECTORS; ++vector) {
414 irq = per_cpu(vector_irq, cpu)[vector];
415 if (irq <= VECTOR_UNDEFINED)
416 continue;
417
418 cfg = irq_cfg(irq);
419 if (!cpumask_test_cpu(cpu, cfg->domain))
420 per_cpu(vector_irq, cpu)[vector] = VECTOR_UNDEFINED;
421 }
422 raw_spin_unlock(&vector_lock);
423}
424
425/*
426 * Setup the vector to irq mappings.
427 */
428void setup_vector_irq(int cpu)
429{
430 int irq;
431
432 /*
433 * On most of the platforms, legacy PIC delivers the interrupts on the
434 * boot cpu. But there are certain platforms where PIC interrupts are
435 * delivered to multiple cpu's. If the legacy IRQ is handled by the
436 * legacy PIC, for the new cpu that is coming online, setup the static
437 * legacy vector to irq mapping:
438 */
439 for (irq = 0; irq < nr_legacy_irqs(); irq++)
440 per_cpu(vector_irq, cpu)[IRQ0_VECTOR + irq] = irq;
441
442 __setup_vector_irq(cpu);
443}
444
Jiang Liuf9705102015-04-14 10:30:00 +0800445static int apic_retrigger_irq(struct irq_data *data)
Jiang Liu74afab72014-10-27 16:12:00 +0800446{
Jiang Liua9786092014-10-27 16:12:07 +0800447 struct irq_cfg *cfg = irqd_cfg(data);
Jiang Liu74afab72014-10-27 16:12:00 +0800448 unsigned long flags;
449 int cpu;
450
451 raw_spin_lock_irqsave(&vector_lock, flags);
452 cpu = cpumask_first_and(cfg->domain, cpu_online_mask);
453 apic->send_IPI_mask(cpumask_of(cpu), cfg->vector);
454 raw_spin_unlock_irqrestore(&vector_lock, flags);
455
456 return 1;
457}
458
459void apic_ack_edge(struct irq_data *data)
460{
Jiang Liua9786092014-10-27 16:12:07 +0800461 irq_complete_move(irqd_cfg(data));
Jiang Liu74afab72014-10-27 16:12:00 +0800462 irq_move_irq(data);
463 ack_APIC_irq();
464}
465
Jiang Liu68f9f442015-04-14 10:30:01 +0800466static int apic_set_affinity(struct irq_data *irq_data,
467 const struct cpumask *dest, bool force)
Jiang Liub5dc8e62015-04-13 14:11:24 +0800468{
469 struct irq_cfg *cfg = irq_data->chip_data;
470 int err, irq = irq_data->irq;
471
472 if (!config_enabled(CONFIG_SMP))
473 return -EPERM;
474
475 if (!cpumask_intersects(dest, cpu_online_mask))
476 return -EINVAL;
477
478 err = assign_irq_vector(irq, cfg, dest);
479 if (err) {
480 struct irq_data *top = irq_get_irq_data(irq);
481
482 if (assign_irq_vector(irq, cfg, top->affinity))
483 pr_err("Failed to recover vector for irq %d\n", irq);
484 return err;
485 }
486
487 return IRQ_SET_MASK_OK;
488}
489
490static struct irq_chip lapic_controller = {
491 .irq_ack = apic_ack_edge,
Jiang Liu68f9f442015-04-14 10:30:01 +0800492 .irq_set_affinity = apic_set_affinity,
Jiang Liub5dc8e62015-04-13 14:11:24 +0800493 .irq_retrigger = apic_retrigger_irq,
494};
495
Jiang Liu74afab72014-10-27 16:12:00 +0800496#ifdef CONFIG_SMP
Jiang Liuc6c20022015-04-14 10:30:02 +0800497static void __send_cleanup_vector(struct irq_cfg *cfg)
Jiang Liu74afab72014-10-27 16:12:00 +0800498{
499 cpumask_var_t cleanup_mask;
500
501 if (unlikely(!alloc_cpumask_var(&cleanup_mask, GFP_ATOMIC))) {
502 unsigned int i;
503
504 for_each_cpu_and(i, cfg->old_domain, cpu_online_mask)
505 apic->send_IPI_mask(cpumask_of(i),
506 IRQ_MOVE_CLEANUP_VECTOR);
507 } else {
508 cpumask_and(cleanup_mask, cfg->old_domain, cpu_online_mask);
509 apic->send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
510 free_cpumask_var(cleanup_mask);
511 }
512 cfg->move_in_progress = 0;
513}
514
Jiang Liuc6c20022015-04-14 10:30:02 +0800515void send_cleanup_vector(struct irq_cfg *cfg)
516{
517 if (cfg->move_in_progress)
518 __send_cleanup_vector(cfg);
519}
520
Jiang Liu74afab72014-10-27 16:12:00 +0800521asmlinkage __visible void smp_irq_move_cleanup_interrupt(void)
522{
523 unsigned vector, me;
524
525 ack_APIC_irq();
526 irq_enter();
527 exit_idle();
528
529 me = smp_processor_id();
530 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
531 int irq;
532 unsigned int irr;
533 struct irq_desc *desc;
534 struct irq_cfg *cfg;
535
536 irq = __this_cpu_read(vector_irq[vector]);
537
538 if (irq <= VECTOR_UNDEFINED)
539 continue;
540
541 desc = irq_to_desc(irq);
542 if (!desc)
543 continue;
544
545 cfg = irq_cfg(irq);
546 if (!cfg)
547 continue;
548
549 raw_spin_lock(&desc->lock);
550
551 /*
552 * Check if the irq migration is in progress. If so, we
553 * haven't received the cleanup request yet for this irq.
554 */
555 if (cfg->move_in_progress)
556 goto unlock;
557
558 if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
559 goto unlock;
560
561 irr = apic_read(APIC_IRR + (vector / 32 * 0x10));
562 /*
563 * Check if the vector that needs to be cleanedup is
564 * registered at the cpu's IRR. If so, then this is not
565 * the best time to clean it up. Lets clean it up in the
566 * next attempt by sending another IRQ_MOVE_CLEANUP_VECTOR
567 * to myself.
568 */
569 if (irr & (1 << (vector % 32))) {
570 apic->send_IPI_self(IRQ_MOVE_CLEANUP_VECTOR);
571 goto unlock;
572 }
573 __this_cpu_write(vector_irq[vector], VECTOR_UNDEFINED);
574unlock:
575 raw_spin_unlock(&desc->lock);
576 }
577
578 irq_exit();
579}
580
581static void __irq_complete_move(struct irq_cfg *cfg, unsigned vector)
582{
583 unsigned me;
584
585 if (likely(!cfg->move_in_progress))
586 return;
587
588 me = smp_processor_id();
589
590 if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
Jiang Liuc6c20022015-04-14 10:30:02 +0800591 __send_cleanup_vector(cfg);
Jiang Liu74afab72014-10-27 16:12:00 +0800592}
593
594void irq_complete_move(struct irq_cfg *cfg)
595{
596 __irq_complete_move(cfg, ~get_irq_regs()->orig_ax);
597}
598
599void irq_force_complete_move(int irq)
600{
601 struct irq_cfg *cfg = irq_cfg(irq);
602
603 if (!cfg)
604 return;
605
606 __irq_complete_move(cfg, cfg->vector);
607}
Jiang Liu74afab72014-10-27 16:12:00 +0800608#endif
609
Jiang Liu74afab72014-10-27 16:12:00 +0800610static void __init print_APIC_field(int base)
611{
612 int i;
613
614 printk(KERN_DEBUG);
615
616 for (i = 0; i < 8; i++)
617 pr_cont("%08x", apic_read(base + i*0x10));
618
619 pr_cont("\n");
620}
621
622static void __init print_local_APIC(void *dummy)
623{
624 unsigned int i, v, ver, maxlvt;
625 u64 icr;
626
Jiang Liu849d3562014-10-27 16:12:01 +0800627 pr_debug("printing local APIC contents on CPU#%d/%d:\n",
628 smp_processor_id(), hard_smp_processor_id());
Jiang Liu74afab72014-10-27 16:12:00 +0800629 v = apic_read(APIC_ID);
Jiang Liu849d3562014-10-27 16:12:01 +0800630 pr_info("... APIC ID: %08x (%01x)\n", v, read_apic_id());
Jiang Liu74afab72014-10-27 16:12:00 +0800631 v = apic_read(APIC_LVR);
Jiang Liu849d3562014-10-27 16:12:01 +0800632 pr_info("... APIC VERSION: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800633 ver = GET_APIC_VERSION(v);
634 maxlvt = lapic_get_maxlvt();
635
636 v = apic_read(APIC_TASKPRI);
Jiang Liu849d3562014-10-27 16:12:01 +0800637 pr_debug("... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
Jiang Liu74afab72014-10-27 16:12:00 +0800638
639 /* !82489DX */
640 if (APIC_INTEGRATED(ver)) {
641 if (!APIC_XAPIC(ver)) {
642 v = apic_read(APIC_ARBPRI);
Jiang Liu849d3562014-10-27 16:12:01 +0800643 pr_debug("... APIC ARBPRI: %08x (%02x)\n",
644 v, v & APIC_ARBPRI_MASK);
Jiang Liu74afab72014-10-27 16:12:00 +0800645 }
646 v = apic_read(APIC_PROCPRI);
Jiang Liu849d3562014-10-27 16:12:01 +0800647 pr_debug("... APIC PROCPRI: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800648 }
649
650 /*
651 * Remote read supported only in the 82489DX and local APIC for
652 * Pentium processors.
653 */
654 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
655 v = apic_read(APIC_RRR);
Jiang Liu849d3562014-10-27 16:12:01 +0800656 pr_debug("... APIC RRR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800657 }
658
659 v = apic_read(APIC_LDR);
Jiang Liu849d3562014-10-27 16:12:01 +0800660 pr_debug("... APIC LDR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800661 if (!x2apic_enabled()) {
662 v = apic_read(APIC_DFR);
Jiang Liu849d3562014-10-27 16:12:01 +0800663 pr_debug("... APIC DFR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800664 }
665 v = apic_read(APIC_SPIV);
Jiang Liu849d3562014-10-27 16:12:01 +0800666 pr_debug("... APIC SPIV: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800667
Jiang Liu849d3562014-10-27 16:12:01 +0800668 pr_debug("... APIC ISR field:\n");
Jiang Liu74afab72014-10-27 16:12:00 +0800669 print_APIC_field(APIC_ISR);
Jiang Liu849d3562014-10-27 16:12:01 +0800670 pr_debug("... APIC TMR field:\n");
Jiang Liu74afab72014-10-27 16:12:00 +0800671 print_APIC_field(APIC_TMR);
Jiang Liu849d3562014-10-27 16:12:01 +0800672 pr_debug("... APIC IRR field:\n");
Jiang Liu74afab72014-10-27 16:12:00 +0800673 print_APIC_field(APIC_IRR);
674
675 /* !82489DX */
676 if (APIC_INTEGRATED(ver)) {
677 /* Due to the Pentium erratum 3AP. */
678 if (maxlvt > 3)
679 apic_write(APIC_ESR, 0);
680
681 v = apic_read(APIC_ESR);
Jiang Liu849d3562014-10-27 16:12:01 +0800682 pr_debug("... APIC ESR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800683 }
684
685 icr = apic_icr_read();
Jiang Liu849d3562014-10-27 16:12:01 +0800686 pr_debug("... APIC ICR: %08x\n", (u32)icr);
687 pr_debug("... APIC ICR2: %08x\n", (u32)(icr >> 32));
Jiang Liu74afab72014-10-27 16:12:00 +0800688
689 v = apic_read(APIC_LVTT);
Jiang Liu849d3562014-10-27 16:12:01 +0800690 pr_debug("... APIC LVTT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800691
692 if (maxlvt > 3) {
693 /* PC is LVT#4. */
694 v = apic_read(APIC_LVTPC);
Jiang Liu849d3562014-10-27 16:12:01 +0800695 pr_debug("... APIC LVTPC: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800696 }
697 v = apic_read(APIC_LVT0);
Jiang Liu849d3562014-10-27 16:12:01 +0800698 pr_debug("... APIC LVT0: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800699 v = apic_read(APIC_LVT1);
Jiang Liu849d3562014-10-27 16:12:01 +0800700 pr_debug("... APIC LVT1: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800701
702 if (maxlvt > 2) {
703 /* ERR is LVT#3. */
704 v = apic_read(APIC_LVTERR);
Jiang Liu849d3562014-10-27 16:12:01 +0800705 pr_debug("... APIC LVTERR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800706 }
707
708 v = apic_read(APIC_TMICT);
Jiang Liu849d3562014-10-27 16:12:01 +0800709 pr_debug("... APIC TMICT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800710 v = apic_read(APIC_TMCCT);
Jiang Liu849d3562014-10-27 16:12:01 +0800711 pr_debug("... APIC TMCCT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800712 v = apic_read(APIC_TDCR);
Jiang Liu849d3562014-10-27 16:12:01 +0800713 pr_debug("... APIC TDCR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800714
715 if (boot_cpu_has(X86_FEATURE_EXTAPIC)) {
716 v = apic_read(APIC_EFEAT);
717 maxlvt = (v >> 16) & 0xff;
Jiang Liu849d3562014-10-27 16:12:01 +0800718 pr_debug("... APIC EFEAT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800719 v = apic_read(APIC_ECTRL);
Jiang Liu849d3562014-10-27 16:12:01 +0800720 pr_debug("... APIC ECTRL: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800721 for (i = 0; i < maxlvt; i++) {
722 v = apic_read(APIC_EILVTn(i));
Jiang Liu849d3562014-10-27 16:12:01 +0800723 pr_debug("... APIC EILVT%d: %08x\n", i, v);
Jiang Liu74afab72014-10-27 16:12:00 +0800724 }
725 }
726 pr_cont("\n");
727}
728
729static void __init print_local_APICs(int maxcpu)
730{
731 int cpu;
732
733 if (!maxcpu)
734 return;
735
736 preempt_disable();
737 for_each_online_cpu(cpu) {
738 if (cpu >= maxcpu)
739 break;
740 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
741 }
742 preempt_enable();
743}
744
745static void __init print_PIC(void)
746{
747 unsigned int v;
748 unsigned long flags;
749
750 if (!nr_legacy_irqs())
751 return;
752
Jiang Liu849d3562014-10-27 16:12:01 +0800753 pr_debug("\nprinting PIC contents\n");
Jiang Liu74afab72014-10-27 16:12:00 +0800754
755 raw_spin_lock_irqsave(&i8259A_lock, flags);
756
757 v = inb(0xa1) << 8 | inb(0x21);
Jiang Liu849d3562014-10-27 16:12:01 +0800758 pr_debug("... PIC IMR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800759
760 v = inb(0xa0) << 8 | inb(0x20);
Jiang Liu849d3562014-10-27 16:12:01 +0800761 pr_debug("... PIC IRR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800762
763 outb(0x0b, 0xa0);
764 outb(0x0b, 0x20);
765 v = inb(0xa0) << 8 | inb(0x20);
766 outb(0x0a, 0xa0);
767 outb(0x0a, 0x20);
768
769 raw_spin_unlock_irqrestore(&i8259A_lock, flags);
770
Jiang Liu849d3562014-10-27 16:12:01 +0800771 pr_debug("... PIC ISR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800772
773 v = inb(0x4d1) << 8 | inb(0x4d0);
Jiang Liu849d3562014-10-27 16:12:01 +0800774 pr_debug("... PIC ELCR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800775}
776
777static int show_lapic __initdata = 1;
778static __init int setup_show_lapic(char *arg)
779{
780 int num = -1;
781
782 if (strcmp(arg, "all") == 0) {
783 show_lapic = CONFIG_NR_CPUS;
784 } else {
785 get_option(&arg, &num);
786 if (num >= 0)
787 show_lapic = num;
788 }
789
790 return 1;
791}
792__setup("show_lapic=", setup_show_lapic);
793
794static int __init print_ICs(void)
795{
796 if (apic_verbosity == APIC_QUIET)
797 return 0;
798
799 print_PIC();
800
801 /* don't print out if apic is not there */
802 if (!cpu_has_apic && !apic_from_smp_config())
803 return 0;
804
805 print_local_APICs(show_lapic);
806 print_IO_APICs();
807
808 return 0;
809}
810
811late_initcall(print_ICs);