blob: 078fbd08499c31eb5d893dbb8f8c135b87b0b301 [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 Gleixner0fa115d2017-09-13 23:29:38 +020039static struct irq_matrix *vector_matrix;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +020040#ifdef CONFIG_SMP
41static DEFINE_PER_CPU(struct hlist_head, cleanup_list);
42#endif
Jiang Liu74afab72014-10-27 16:12:00 +080043
44void lock_vector_lock(void)
45{
46 /* Used to the online set of cpus does not change
47 * during assign_irq_vector.
48 */
49 raw_spin_lock(&vector_lock);
50}
51
52void unlock_vector_lock(void)
53{
54 raw_spin_unlock(&vector_lock);
55}
56
Thomas Gleixner99a14822017-09-13 23:29:36 +020057void init_irq_alloc_info(struct irq_alloc_info *info,
58 const struct cpumask *mask)
59{
60 memset(info, 0, sizeof(*info));
61 info->mask = mask;
62}
63
64void copy_irq_alloc_info(struct irq_alloc_info *dst, struct irq_alloc_info *src)
65{
66 if (src)
67 *dst = *src;
68 else
69 memset(dst, 0, sizeof(*dst));
70}
71
Thomas Gleixner86ba6552017-09-13 23:29:30 +020072static struct apic_chip_data *apic_chip_data(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +080073{
Thomas Gleixner86ba6552017-09-13 23:29:30 +020074 if (!irqd)
Jiang Liub5dc8e62015-04-13 14:11:24 +080075 return NULL;
76
Thomas Gleixner86ba6552017-09-13 23:29:30 +020077 while (irqd->parent_data)
78 irqd = irqd->parent_data;
Jiang Liub5dc8e62015-04-13 14:11:24 +080079
Thomas Gleixner86ba6552017-09-13 23:29:30 +020080 return irqd->chip_data;
Jiang Liu74afab72014-10-27 16:12:00 +080081}
82
Thomas Gleixner86ba6552017-09-13 23:29:30 +020083struct irq_cfg *irqd_cfg(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +080084{
Thomas Gleixner86ba6552017-09-13 23:29:30 +020085 struct apic_chip_data *apicd = apic_chip_data(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +080086
Thomas Gleixner86ba6552017-09-13 23:29:30 +020087 return apicd ? &apicd->cfg : NULL;
Jiang Liu7f3262e2015-04-14 10:30:03 +080088}
Jake Oshinsc8f3e512015-12-10 17:52:59 +000089EXPORT_SYMBOL_GPL(irqd_cfg);
Jiang Liu7f3262e2015-04-14 10:30:03 +080090
91struct irq_cfg *irq_cfg(unsigned int irq)
92{
93 return irqd_cfg(irq_get_irq_data(irq));
94}
95
96static struct apic_chip_data *alloc_apic_chip_data(int node)
97{
Thomas Gleixner86ba6552017-09-13 23:29:30 +020098 struct apic_chip_data *apicd;
Jiang Liu7f3262e2015-04-14 10:30:03 +080099
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200100 apicd = kzalloc_node(sizeof(*apicd), GFP_KERNEL, node);
101 if (!apicd)
Jiang Liu74afab72014-10-27 16:12:00 +0800102 return NULL;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200103 if (!zalloc_cpumask_var_node(&apicd->domain, GFP_KERNEL, node))
Jiang Liu7f3262e2015-04-14 10:30:03 +0800104 goto out_data;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200105 if (!zalloc_cpumask_var_node(&apicd->old_domain, GFP_KERNEL, node))
Jiang Liu74afab72014-10-27 16:12:00 +0800106 goto out_domain;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200107 INIT_HLIST_NODE(&apicd->clist);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200108 return apicd;
Jiang Liu74afab72014-10-27 16:12:00 +0800109out_domain:
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200110 free_cpumask_var(apicd->domain);
Jiang Liu7f3262e2015-04-14 10:30:03 +0800111out_data:
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200112 kfree(apicd);
Jiang Liu74afab72014-10-27 16:12:00 +0800113 return NULL;
114}
115
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200116static void free_apic_chip_data(struct apic_chip_data *apicd)
Jiang Liu74afab72014-10-27 16:12:00 +0800117{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200118 if (apicd) {
119 free_cpumask_var(apicd->domain);
120 free_cpumask_var(apicd->old_domain);
121 kfree(apicd);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800122 }
Jiang Liu74afab72014-10-27 16:12:00 +0800123}
124
Jiang Liu7f3262e2015-04-14 10:30:03 +0800125static int __assign_irq_vector(int irq, struct apic_chip_data *d,
Thomas Gleixner0e24f7c2017-06-20 01:37:44 +0200126 const struct cpumask *mask,
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200127 struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +0800128{
129 /*
130 * NOTE! The local APIC isn't very good at handling
131 * multiple interrupts at the same interrupt level.
132 * As the interrupt level is determined by taking the
133 * vector number and shifting that right by 4, we
134 * want to spread these out a bit so that they don't
135 * all fall in the same interrupt level.
136 *
137 * Also, we've got to be careful not to trash gate
138 * 0x80, because int 0x80 is hm, kind of importantish. ;)
139 */
140 static int current_vector = FIRST_EXTERNAL_VECTOR + VECTOR_OFFSET_START;
141 static int current_offset = VECTOR_OFFSET_START % 16;
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000142 int cpu, vector;
Jiang Liu74afab72014-10-27 16:12:00 +0800143
Thomas Gleixner98229aa2015-12-31 16:30:54 +0000144 /*
145 * If there is still a move in progress or the previous move has not
146 * been cleaned up completely, tell the caller to come back later.
147 */
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200148 if (d->cfg.old_vector)
Jiang Liu74afab72014-10-27 16:12:00 +0800149 return -EBUSY;
150
Jiang Liu74afab72014-10-27 16:12:00 +0800151 /* Only try and allocate irqs on cpus that are present */
Jiang Liu7f3262e2015-04-14 10:30:03 +0800152 cpumask_clear(d->old_domain);
Jiang Liu8a580f72015-12-31 16:30:46 +0000153 cpumask_clear(searched_cpumask);
Jiang Liu74afab72014-10-27 16:12:00 +0800154 cpu = cpumask_first_and(mask, cpu_online_mask);
155 while (cpu < nr_cpu_ids) {
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000156 int new_cpu, offset;
Jiang Liu74afab72014-10-27 16:12:00 +0800157
Thomas Gleixnerfdba46f2017-09-13 23:29:27 +0200158 cpumask_copy(vector_cpumask, cpumask_of(cpu));
Jiang Liu74afab72014-10-27 16:12:00 +0800159
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000160 /*
161 * Clear the offline cpus from @vector_cpumask for searching
162 * and verify whether the result overlaps with @mask. If true,
Thomas Gleixner91cd9cb2017-06-20 01:37:43 +0200163 * then the call to apic->cpu_mask_to_apicid() will
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000164 * succeed as well. If not, no point in trying to find a
165 * vector in this mask.
166 */
167 cpumask_and(vector_searchmask, vector_cpumask, cpu_online_mask);
168 if (!cpumask_intersects(vector_searchmask, mask))
169 goto next_cpu;
170
Jiang Liuf7fa7ae2015-04-14 10:30:10 +0800171 if (cpumask_subset(vector_cpumask, d->domain)) {
Jiang Liuf7fa7ae2015-04-14 10:30:10 +0800172 if (cpumask_equal(vector_cpumask, d->domain))
Thomas Gleixner433cbd52015-12-31 16:30:46 +0000173 goto success;
Jiang Liu74afab72014-10-27 16:12:00 +0800174 /*
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000175 * Mark the cpus which are not longer in the mask for
176 * cleanup.
Jiang Liu74afab72014-10-27 16:12:00 +0800177 */
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000178 cpumask_andnot(d->old_domain, d->domain, vector_cpumask);
179 vector = d->cfg.vector;
180 goto update;
Jiang Liu74afab72014-10-27 16:12:00 +0800181 }
182
183 vector = current_vector;
184 offset = current_offset;
185next:
186 vector += 16;
Thomas Gleixner05161b92017-08-28 08:47:18 +0200187 if (vector >= FIRST_SYSTEM_VECTOR) {
Jiang Liu74afab72014-10-27 16:12:00 +0800188 offset = (offset + 1) % 16;
189 vector = FIRST_EXTERNAL_VECTOR + offset;
190 }
191
Thomas Gleixner95ffeb42015-12-31 16:30:47 +0000192 /* If the search wrapped around, try the next cpu */
193 if (unlikely(current_vector == vector))
194 goto next_cpu;
Jiang Liu74afab72014-10-27 16:12:00 +0800195
Thomas Gleixner7854f822017-09-13 23:29:26 +0200196 if (test_bit(vector, system_vectors))
Jiang Liu74afab72014-10-27 16:12:00 +0800197 goto next;
198
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000199 for_each_cpu(new_cpu, vector_searchmask) {
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000200 if (!IS_ERR_OR_NULL(per_cpu(vector_irq, new_cpu)[vector]))
Jiang Liu74afab72014-10-27 16:12:00 +0800201 goto next;
202 }
203 /* Found one! */
204 current_vector = vector;
205 current_offset = offset;
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000206 /* Schedule the old vector for cleanup on all cpus */
207 if (d->cfg.vector)
Jiang Liu7f3262e2015-04-14 10:30:03 +0800208 cpumask_copy(d->old_domain, d->domain);
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000209 for_each_cpu(new_cpu, vector_searchmask)
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000210 per_cpu(vector_irq, new_cpu)[vector] = irq_to_desc(irq);
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000211 goto update;
Thomas Gleixner95ffeb42015-12-31 16:30:47 +0000212
213next_cpu:
214 /*
215 * We exclude the current @vector_cpumask from the requested
216 * @mask and try again with the next online cpu in the
217 * result. We cannot modify @mask, so we use @vector_cpumask
218 * as a temporary buffer here as it will be reassigned when
219 * calling apic->vector_allocation_domain() above.
220 */
221 cpumask_or(searched_cpumask, searched_cpumask, vector_cpumask);
222 cpumask_andnot(vector_cpumask, mask, searched_cpumask);
223 cpu = cpumask_first_and(vector_cpumask, cpu_online_mask);
224 continue;
Jiang Liu74afab72014-10-27 16:12:00 +0800225 }
Thomas Gleixner433cbd52015-12-31 16:30:46 +0000226 return -ENOSPC;
Jiang Liu74afab72014-10-27 16:12:00 +0800227
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000228update:
Thomas Gleixner847667e2015-12-31 16:30:50 +0000229 /*
230 * Exclude offline cpus from the cleanup mask and set the
231 * move_in_progress flag when the result is not empty.
232 */
233 cpumask_and(d->old_domain, d->old_domain, cpu_online_mask);
234 d->move_in_progress = !cpumask_empty(d->old_domain);
Thomas Gleixner551adc62016-03-14 09:40:46 +0100235 d->cfg.old_vector = d->move_in_progress ? d->cfg.vector : 0;
Thomas Gleixner029c6e12017-09-13 23:29:31 +0200236 d->prev_cpu = d->cpu;
Thomas Gleixnerab25ac02015-12-31 16:30:49 +0000237 d->cfg.vector = vector;
238 cpumask_copy(d->domain, vector_cpumask);
Thomas Gleixner433cbd52015-12-31 16:30:46 +0000239success:
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000240 /*
241 * Cache destination APIC IDs into cfg->dest_apicid. This cannot fail
242 * as we already established, that mask & d->domain & cpu_online_mask
243 * is not empty.
Thomas Gleixner52b166a2017-06-20 01:37:42 +0200244 *
245 * vector_searchmask is a subset of d->domain and has the offline
246 * cpus masked out.
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000247 */
Thomas Gleixner91cd9cb2017-06-20 01:37:43 +0200248 cpumask_and(vector_searchmask, vector_searchmask, mask);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200249 BUG_ON(apic->cpu_mask_to_apicid(vector_searchmask, irqd,
Thomas Gleixner0e24f7c2017-06-20 01:37:44 +0200250 &d->cfg.dest_apicid));
Thomas Gleixner029c6e12017-09-13 23:29:31 +0200251 d->cpu = cpumask_first(vector_searchmask);
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000252 return 0;
Jiang Liu74afab72014-10-27 16:12:00 +0800253}
254
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200255static int assign_irq_vector(int irq, struct apic_chip_data *apicd,
Thomas Gleixner0e24f7c2017-06-20 01:37:44 +0200256 const struct cpumask *mask,
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200257 struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +0800258{
259 int err;
260 unsigned long flags;
261
262 raw_spin_lock_irqsave(&vector_lock, flags);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200263 err = __assign_irq_vector(irq, apicd, mask, irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800264 raw_spin_unlock_irqrestore(&vector_lock, flags);
265 return err;
266}
267
Jiang Liu486ca532015-05-07 10:53:56 +0800268static int assign_irq_vector_policy(int irq, int node,
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200269 struct apic_chip_data *apicd,
Thomas Gleixner0e24f7c2017-06-20 01:37:44 +0200270 struct irq_alloc_info *info,
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200271 struct irq_data *irqd)
Jiang Liu486ca532015-05-07 10:53:56 +0800272{
Thomas Gleixner258d86e2017-09-13 23:29:35 +0200273 if (info->mask)
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200274 return assign_irq_vector(irq, apicd, info->mask, irqd);
Jiang Liu486ca532015-05-07 10:53:56 +0800275 if (node != NUMA_NO_NODE &&
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200276 assign_irq_vector(irq, apicd, cpumask_of_node(node), irqd) == 0)
Jiang Liu486ca532015-05-07 10:53:56 +0800277 return 0;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200278 return assign_irq_vector(irq, apicd, cpu_online_mask, irqd);
Jiang Liu486ca532015-05-07 10:53:56 +0800279}
280
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200281static void clear_irq_vector(int irq, struct apic_chip_data *apicd)
Jiang Liu74afab72014-10-27 16:12:00 +0800282{
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200283 unsigned int vector = apicd->cfg.vector;
Jiang Liu74afab72014-10-27 16:12:00 +0800284
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200285 if (!vector)
Keith Busch1bdb8972016-04-27 14:22:32 -0600286 return;
Jiang Liu74afab72014-10-27 16:12:00 +0800287
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200288 per_cpu(vector_irq, apicd->cpu)[vector] = VECTOR_UNUSED;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200289 apicd->cfg.vector = 0;
Jiang Liu74afab72014-10-27 16:12:00 +0800290
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200291 /* Clean up move in progress */
292 vector = apicd->cfg.old_vector;
293 if (!vector)
Jiang Liu74afab72014-10-27 16:12:00 +0800294 return;
Jiang Liu74afab72014-10-27 16:12:00 +0800295
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200296 per_cpu(vector_irq, apicd->prev_cpu)[vector] = VECTOR_UNUSED;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200297 apicd->move_in_progress = 0;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200298 hlist_del_init(&apicd->clist);
Jiang Liu74afab72014-10-27 16:12:00 +0800299}
300
Jiang Liub5dc8e62015-04-13 14:11:24 +0800301static void x86_vector_free_irqs(struct irq_domain *domain,
302 unsigned int virq, unsigned int nr_irqs)
303{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200304 struct apic_chip_data *apicd;
305 struct irq_data *irqd;
Jiang Liu111abeb2015-12-31 16:30:44 +0000306 unsigned long flags;
Jiang Liub5dc8e62015-04-13 14:11:24 +0800307 int i;
308
309 for (i = 0; i < nr_irqs; i++) {
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200310 irqd = irq_domain_get_irq_data(x86_vector_domain, virq + i);
311 if (irqd && irqd->chip_data) {
Jiang Liu111abeb2015-12-31 16:30:44 +0000312 raw_spin_lock_irqsave(&vector_lock, flags);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200313 clear_irq_vector(virq + i, irqd->chip_data);
314 apicd = irqd->chip_data;
315 irq_domain_reset_irq_data(irqd);
Jiang Liu111abeb2015-12-31 16:30:44 +0000316 raw_spin_unlock_irqrestore(&vector_lock, flags);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200317 free_apic_chip_data(apicd);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800318 }
319 }
320}
321
322static int x86_vector_alloc_irqs(struct irq_domain *domain, unsigned int virq,
323 unsigned int nr_irqs, void *arg)
324{
325 struct irq_alloc_info *info = arg;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200326 struct apic_chip_data *apicd;
327 struct irq_data *irqd;
Jiang Liu5f2dbbc2015-06-01 16:05:14 +0800328 int i, err, node;
Jiang Liub5dc8e62015-04-13 14:11:24 +0800329
330 if (disable_apic)
331 return -ENXIO;
332
333 /* Currently vector allocator can't guarantee contiguous allocations */
334 if ((info->flags & X86_IRQ_ALLOC_CONTIGUOUS_VECTORS) && nr_irqs > 1)
335 return -ENOSYS;
336
Jiang Liub5dc8e62015-04-13 14:11:24 +0800337 for (i = 0; i < nr_irqs; i++) {
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200338 irqd = irq_domain_get_irq_data(domain, virq + i);
339 BUG_ON(!irqd);
340 node = irq_data_get_node(irqd);
Thomas Gleixner4ef76eb2017-09-13 23:29:34 +0200341 WARN_ON_ONCE(irqd->chip_data);
342 apicd = alloc_apic_chip_data(node);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200343 if (!apicd) {
Jiang Liub5dc8e62015-04-13 14:11:24 +0800344 err = -ENOMEM;
345 goto error;
346 }
347
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200348 irqd->chip = &lapic_controller;
349 irqd->chip_data = apicd;
350 irqd->hwirq = virq + i;
351 irqd_set_single_target(irqd);
Thomas Gleixner4ef76eb2017-09-13 23:29:34 +0200352 /*
353 * Make sure, that the legacy to IOAPIC transition stays on
354 * the same vector. This is required for check_timer() to
355 * work correctly as it might switch back to legacy mode.
356 */
357 if (info->flags & X86_IRQ_ALLOC_LEGACY) {
358 apicd->cfg.vector = ISA_IRQ_VECTOR(virq + i);
359 apicd->cpu = 0;
360 cpumask_copy(apicd->domain, cpumask_of(0));
361 }
362
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200363 err = assign_irq_vector_policy(virq + i, node, apicd, info,
364 irqd);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800365 if (err)
366 goto error;
367 }
368
369 return 0;
370
371error:
372 x86_vector_free_irqs(domain, virq, i + 1);
373 return err;
374}
375
Thomas Gleixnereb18cf52015-05-05 11:10:11 +0200376static const struct irq_domain_ops x86_vector_domain_ops = {
377 .alloc = x86_vector_alloc_irqs,
378 .free = x86_vector_free_irqs,
Jiang Liub5dc8e62015-04-13 14:11:24 +0800379};
380
Jiang Liu11d686e2014-10-27 16:12:05 +0800381int __init arch_probe_nr_irqs(void)
382{
383 int nr;
384
385 if (nr_irqs > (NR_VECTORS * nr_cpu_ids))
386 nr_irqs = NR_VECTORS * nr_cpu_ids;
387
388 nr = (gsi_top + nr_legacy_irqs()) + 8 * nr_cpu_ids;
389#if defined(CONFIG_PCI_MSI) || defined(CONFIG_HT_IRQ)
390 /*
391 * for MSI and HT dyn irq
392 */
393 if (gsi_top <= NR_IRQS_LEGACY)
394 nr += 8 * nr_cpu_ids;
395 else
396 nr += gsi_top * 16;
397#endif
398 if (nr < nr_irqs)
399 nr_irqs = nr;
400
Vitaly Kuznetsov8c058b02015-11-03 10:40:14 +0100401 /*
402 * We don't know if PIC is present at this point so we need to do
403 * probe() to get the right number of legacy IRQs.
404 */
405 return legacy_pic->probe();
Jiang Liu11d686e2014-10-27 16:12:05 +0800406}
407
Thomas Gleixner0fa115d2017-09-13 23:29:38 +0200408void lapic_assign_legacy_vector(unsigned int irq, bool replace)
409{
410 /*
411 * Use assign system here so it wont get accounted as allocated
412 * and moveable in the cpu hotplug check and it prevents managed
413 * irq reservation from touching it.
414 */
415 irq_matrix_assign_system(vector_matrix, ISA_IRQ_VECTOR(irq), replace);
416}
417
418void __init lapic_assign_system_vectors(void)
419{
420 unsigned int i, vector = 0;
421
422 for_each_set_bit_from(vector, system_vectors, NR_VECTORS)
423 irq_matrix_assign_system(vector_matrix, vector, false);
424
425 if (nr_legacy_irqs() > 1)
426 lapic_assign_legacy_vector(PIC_CASCADE_IR, false);
427
428 /* System vectors are reserved, online it */
429 irq_matrix_online(vector_matrix);
430
431 /* Mark the preallocated legacy interrupts */
432 for (i = 0; i < nr_legacy_irqs(); i++) {
433 if (i != PIC_CASCADE_IR)
434 irq_matrix_assign(vector_matrix, ISA_IRQ_VECTOR(i));
435 }
436}
437
Jiang Liu11d686e2014-10-27 16:12:05 +0800438int __init arch_early_irq_init(void)
439{
Thomas Gleixner9d35f852017-06-20 01:37:06 +0200440 struct fwnode_handle *fn;
441
Thomas Gleixner9d35f852017-06-20 01:37:06 +0200442 fn = irq_domain_alloc_named_fwnode("VECTOR");
443 BUG_ON(!fn);
444 x86_vector_domain = irq_domain_create_tree(fn, &x86_vector_domain_ops,
445 NULL);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800446 BUG_ON(x86_vector_domain == NULL);
Thomas Gleixner9d35f852017-06-20 01:37:06 +0200447 irq_domain_free_fwnode(fn);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800448 irq_set_default_host(x86_vector_domain);
449
Jiang Liu52f518a2015-04-13 14:11:35 +0800450 arch_init_msi_domain(x86_vector_domain);
Jiang Liu49e07d82015-04-13 14:11:43 +0800451 arch_init_htirq_domain(x86_vector_domain);
Jiang Liu52f518a2015-04-13 14:11:35 +0800452
Jiang Liuf7fa7ae2015-04-14 10:30:10 +0800453 BUG_ON(!alloc_cpumask_var(&vector_cpumask, GFP_KERNEL));
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000454 BUG_ON(!alloc_cpumask_var(&vector_searchmask, GFP_KERNEL));
Jiang Liu8a580f72015-12-31 16:30:46 +0000455 BUG_ON(!alloc_cpumask_var(&searched_cpumask, GFP_KERNEL));
Jiang Liuf7fa7ae2015-04-14 10:30:10 +0800456
Thomas Gleixner0fa115d2017-09-13 23:29:38 +0200457 /*
458 * Allocate the vector matrix allocator data structure and limit the
459 * search area.
460 */
461 vector_matrix = irq_alloc_matrix(NR_VECTORS, FIRST_EXTERNAL_VECTOR,
462 FIRST_SYSTEM_VECTOR);
463 BUG_ON(!vector_matrix);
464
Jiang Liu11d686e2014-10-27 16:12:05 +0800465 return arch_early_ioapic_init();
466}
467
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200468/* Temporary hack to keep things working */
469static void vector_update_shutdown_irqs(void)
Jiang Liu74afab72014-10-27 16:12:00 +0800470{
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000471 struct irq_desc *desc;
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200472 int irq;
Jiang Liu74afab72014-10-27 16:12:00 +0800473
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000474 for_each_irq_desc(irq, desc) {
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200475 struct irq_data *irqd = irq_desc_get_irq_data(desc);
476 struct apic_chip_data *ad = apic_chip_data(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800477
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200478 if (ad && ad->cfg.vector && ad->cpu == smp_processor_id())
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200479 this_cpu_write(vector_irq[ad->cfg.vector], desc);
Jiang Liu74afab72014-10-27 16:12:00 +0800480 }
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200481}
Jiang Liu74afab72014-10-27 16:12:00 +0800482
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200483static struct irq_desc *__setup_vector_irq(int vector)
484{
485 int isairq = vector - ISA_IRQ_VECTOR(0);
486
487 /* Check whether the irq is in the legacy space */
488 if (isairq < 0 || isairq >= nr_legacy_irqs())
489 return VECTOR_UNUSED;
490 /* Check whether the irq is handled by the IOAPIC */
491 if (test_bit(isairq, &io_apic_irqs))
492 return VECTOR_UNUSED;
493 return irq_to_desc(isairq);
Jiang Liu74afab72014-10-27 16:12:00 +0800494}
495
Thomas Gleixner0fa115d2017-09-13 23:29:38 +0200496/* Online the local APIC infrastructure and initialize the vectors */
497void lapic_online(void)
Jiang Liu74afab72014-10-27 16:12:00 +0800498{
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200499 unsigned int vector;
Jiang Liu74afab72014-10-27 16:12:00 +0800500
Thomas Gleixner5a3f75e2015-07-05 17:12:32 +0000501 lockdep_assert_held(&vector_lock);
Thomas Gleixner0fa115d2017-09-13 23:29:38 +0200502
503 /* Online the vector matrix array for this CPU */
504 irq_matrix_online(vector_matrix);
505
Jiang Liu74afab72014-10-27 16:12:00 +0800506 /*
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200507 * The interrupt affinity logic never targets interrupts to offline
508 * CPUs. The exception are the legacy PIC interrupts. In general
509 * they are only targeted to CPU0, but depending on the platform
510 * they can be distributed to any online CPU in hardware. The
511 * kernel has no influence on that. So all active legacy vectors
512 * must be installed on all CPUs. All non legacy interrupts can be
513 * cleared.
Jiang Liu74afab72014-10-27 16:12:00 +0800514 */
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200515 for (vector = 0; vector < NR_VECTORS; vector++)
516 this_cpu_write(vector_irq[vector], __setup_vector_irq(vector));
Jiang Liu74afab72014-10-27 16:12:00 +0800517
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200518 /*
519 * Until the rewrite of the managed interrupt management is in
520 * place it's necessary to walk the irq descriptors and check for
521 * interrupts which are targeted at this CPU.
522 */
523 vector_update_shutdown_irqs();
Jiang Liu74afab72014-10-27 16:12:00 +0800524}
525
Thomas Gleixner0fa115d2017-09-13 23:29:38 +0200526void lapic_offline(void)
527{
528 lock_vector_lock();
529 irq_matrix_offline(vector_matrix);
530 unlock_vector_lock();
531}
532
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200533static int apic_retrigger_irq(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +0800534{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200535 struct apic_chip_data *apicd = apic_chip_data(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800536 unsigned long flags;
Jiang Liu74afab72014-10-27 16:12:00 +0800537
538 raw_spin_lock_irqsave(&vector_lock, flags);
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200539 apic->send_IPI(apicd->cpu, apicd->cfg.vector);
Jiang Liu74afab72014-10-27 16:12:00 +0800540 raw_spin_unlock_irqrestore(&vector_lock, flags);
541
542 return 1;
543}
544
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200545void apic_ack_edge(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +0800546{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200547 irq_complete_move(irqd_cfg(irqd));
548 irq_move_irq(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800549 ack_APIC_irq();
550}
551
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200552static int apic_set_affinity(struct irq_data *irqd,
Jiang Liu68f9f442015-04-14 10:30:01 +0800553 const struct cpumask *dest, bool force)
Jiang Liub5dc8e62015-04-13 14:11:24 +0800554{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200555 struct apic_chip_data *apicd = irqd->chip_data;
556 int err, irq = irqd->irq;
Jiang Liub5dc8e62015-04-13 14:11:24 +0800557
Masahiro Yamada97f26452016-08-03 13:45:50 -0700558 if (!IS_ENABLED(CONFIG_SMP))
Jiang Liub5dc8e62015-04-13 14:11:24 +0800559 return -EPERM;
560
561 if (!cpumask_intersects(dest, cpu_online_mask))
562 return -EINVAL;
563
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200564 err = assign_irq_vector(irq, apicd, dest, irqd);
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000565 return err ? err : IRQ_SET_MASK_OK;
Jiang Liub5dc8e62015-04-13 14:11:24 +0800566}
567
568static struct irq_chip lapic_controller = {
Thomas Gleixner8947dfb2017-06-20 01:37:01 +0200569 .name = "APIC",
Jiang Liub5dc8e62015-04-13 14:11:24 +0800570 .irq_ack = apic_ack_edge,
Jiang Liu68f9f442015-04-14 10:30:01 +0800571 .irq_set_affinity = apic_set_affinity,
Jiang Liub5dc8e62015-04-13 14:11:24 +0800572 .irq_retrigger = apic_retrigger_irq,
573};
574
Jiang Liu74afab72014-10-27 16:12:00 +0800575#ifdef CONFIG_SMP
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200576
577asmlinkage __visible void __irq_entry smp_irq_move_cleanup_interrupt(void)
578{
579 struct hlist_head *clhead = this_cpu_ptr(&cleanup_list);
580 struct apic_chip_data *apicd;
581 struct hlist_node *tmp;
582
583 entering_ack_irq();
584 /* Prevent vectors vanishing under us */
585 raw_spin_lock(&vector_lock);
586
587 hlist_for_each_entry_safe(apicd, tmp, clhead, clist) {
588 unsigned int irr, vector = apicd->cfg.old_vector;
589
590 /*
591 * Paranoia: Check if the vector that needs to be cleaned
592 * up is registered at the APICs IRR. If so, then this is
593 * not the best time to clean it up. Clean it up in the
594 * next attempt by sending another IRQ_MOVE_CLEANUP_VECTOR
595 * to this CPU. IRQ_MOVE_CLEANUP_VECTOR is the lowest
596 * priority external vector, so on return from this
597 * interrupt the device interrupt will happen first.
598 */
599 irr = apic_read(APIC_IRR + (vector / 32 * 0x10));
600 if (irr & (1U << (vector % 32))) {
601 apic->send_IPI_self(IRQ_MOVE_CLEANUP_VECTOR);
602 continue;
603 }
604 hlist_del_init(&apicd->clist);
605 __this_cpu_write(vector_irq[vector], VECTOR_UNUSED);
606 apicd->cfg.old_vector = 0;
607 }
608
609 raw_spin_unlock(&vector_lock);
610 exiting_irq();
611}
612
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200613static void __send_cleanup_vector(struct apic_chip_data *apicd)
Jiang Liu74afab72014-10-27 16:12:00 +0800614{
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200615 unsigned int cpu;
616
Thomas Gleixnerc1684f52015-12-31 16:30:51 +0000617 raw_spin_lock(&vector_lock);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200618 apicd->move_in_progress = 0;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200619 cpu = apicd->prev_cpu;
620 if (cpu_online(cpu)) {
621 hlist_add_head(&apicd->clist, per_cpu_ptr(&cleanup_list, cpu));
622 apic->send_IPI(cpu, IRQ_MOVE_CLEANUP_VECTOR);
623 } else {
624 apicd->cfg.old_vector = 0;
625 }
Thomas Gleixnerc1684f52015-12-31 16:30:51 +0000626 raw_spin_unlock(&vector_lock);
Jiang Liu74afab72014-10-27 16:12:00 +0800627}
628
Jiang Liuc6c20022015-04-14 10:30:02 +0800629void send_cleanup_vector(struct irq_cfg *cfg)
630{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200631 struct apic_chip_data *apicd;
Jiang Liu7f3262e2015-04-14 10:30:03 +0800632
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200633 apicd = container_of(cfg, struct apic_chip_data, cfg);
634 if (apicd->move_in_progress)
635 __send_cleanup_vector(apicd);
Jiang Liuc6c20022015-04-14 10:30:02 +0800636}
637
Jiang Liu74afab72014-10-27 16:12:00 +0800638static void __irq_complete_move(struct irq_cfg *cfg, unsigned vector)
639{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200640 struct apic_chip_data *apicd;
Jiang Liu74afab72014-10-27 16:12:00 +0800641
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200642 apicd = container_of(cfg, struct apic_chip_data, cfg);
643 if (likely(!apicd->move_in_progress))
Jiang Liu74afab72014-10-27 16:12:00 +0800644 return;
645
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200646 if (vector == apicd->cfg.vector && apicd->cpu == smp_processor_id())
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200647 __send_cleanup_vector(apicd);
Jiang Liu74afab72014-10-27 16:12:00 +0800648}
649
650void irq_complete_move(struct irq_cfg *cfg)
651{
652 __irq_complete_move(cfg, ~get_irq_regs()->orig_ax);
653}
654
Thomas Gleixner90a22822015-12-31 16:30:53 +0000655/*
Thomas Gleixner551adc62016-03-14 09:40:46 +0100656 * Called from fixup_irqs() with @desc->lock held and interrupts disabled.
Thomas Gleixner90a22822015-12-31 16:30:53 +0000657 */
658void irq_force_complete_move(struct irq_desc *desc)
Jiang Liu74afab72014-10-27 16:12:00 +0800659{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200660 struct apic_chip_data *apicd;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200661 struct irq_data *irqd;
662 unsigned int vector;
Jiang Liu74afab72014-10-27 16:12:00 +0800663
Mika Westerbergdb91aa72016-10-03 13:17:08 +0300664 /*
665 * The function is called for all descriptors regardless of which
666 * irqdomain they belong to. For example if an IRQ is provided by
667 * an irq_chip as part of a GPIO driver, the chip data for that
668 * descriptor is specific to the irq_chip in question.
669 *
670 * Check first that the chip_data is what we expect
671 * (apic_chip_data) before touching it any further.
672 */
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200673 irqd = irq_domain_get_irq_data(x86_vector_domain,
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200674 irq_desc_get_irq(desc));
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200675 if (!irqd)
Mika Westerbergdb91aa72016-10-03 13:17:08 +0300676 return;
677
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200678 raw_spin_lock(&vector_lock);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200679 apicd = apic_chip_data(irqd);
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200680 if (!apicd)
681 goto unlock;
Thomas Gleixner56d7d2f2015-12-31 16:30:52 +0000682
Thomas Gleixner56d7d2f2015-12-31 16:30:52 +0000683 /*
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200684 * If old_vector is empty, no action required.
685 */
686 vector = apicd->cfg.old_vector;
687 if (!vector)
688 goto unlock;
689
690 /*
691 * This is tricky. If the cleanup of the old vector has not been
Thomas Gleixner98229aa2015-12-31 16:30:54 +0000692 * done yet, then the following setaffinity call will fail with
693 * -EBUSY. This can leave the interrupt in a stale state.
694 *
Thomas Gleixner551adc62016-03-14 09:40:46 +0100695 * All CPUs are stuck in stop machine with interrupts disabled so
696 * calling __irq_complete_move() would be completely pointless.
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200697 *
Thomas Gleixner551adc62016-03-14 09:40:46 +0100698 * 1) The interrupt is in move_in_progress state. That means that we
699 * have not seen an interrupt since the io_apic was reprogrammed to
700 * the new vector.
701 *
702 * 2) The interrupt has fired on the new vector, but the cleanup IPIs
703 * have not been processed yet.
704 */
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200705 if (apicd->move_in_progress) {
Thomas Gleixner551adc62016-03-14 09:40:46 +0100706 /*
707 * In theory there is a race:
708 *
709 * set_ioapic(new_vector) <-- Interrupt is raised before update
710 * is effective, i.e. it's raised on
711 * the old vector.
712 *
713 * So if the target cpu cannot handle that interrupt before
714 * the old vector is cleaned up, we get a spurious interrupt
715 * and in the worst case the ioapic irq line becomes stale.
716 *
717 * But in case of cpu hotplug this should be a non issue
718 * because if the affinity update happens right before all
719 * cpus rendevouz in stop machine, there is no way that the
720 * interrupt can be blocked on the target cpu because all cpus
721 * loops first with interrupts enabled in stop machine, so the
722 * old vector is not yet cleaned up when the interrupt fires.
723 *
724 * So the only way to run into this issue is if the delivery
725 * of the interrupt on the apic/system bus would be delayed
726 * beyond the point where the target cpu disables interrupts
727 * in stop machine. I doubt that it can happen, but at least
728 * there is a theroretical chance. Virtualization might be
729 * able to expose this, but AFAICT the IOAPIC emulation is not
730 * as stupid as the real hardware.
731 *
732 * Anyway, there is nothing we can do about that at this point
733 * w/o refactoring the whole fixup_irq() business completely.
734 * We print at least the irq number and the old vector number,
735 * so we have the necessary information when a problem in that
736 * area arises.
737 */
738 pr_warn("IRQ fixup: irq %d move in progress, old vector %d\n",
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200739 irqd->irq, vector);
Thomas Gleixner551adc62016-03-14 09:40:46 +0100740 }
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200741 per_cpu(vector_irq, apicd->prev_cpu)[vector] = VECTOR_UNUSED;
Thomas Gleixner551adc62016-03-14 09:40:46 +0100742 /* Cleanup the left overs of the (half finished) move */
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200743 cpumask_clear(apicd->old_domain);
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200744 apicd->cfg.old_vector = 0;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200745 apicd->move_in_progress = 0;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200746 hlist_del_init(&apicd->clist);
747unlock:
Thomas Gleixner56d7d2f2015-12-31 16:30:52 +0000748 raw_spin_unlock(&vector_lock);
Jiang Liu74afab72014-10-27 16:12:00 +0800749}
Jiang Liu74afab72014-10-27 16:12:00 +0800750#endif
751
Jiang Liu74afab72014-10-27 16:12:00 +0800752static void __init print_APIC_field(int base)
753{
754 int i;
755
756 printk(KERN_DEBUG);
757
758 for (i = 0; i < 8; i++)
759 pr_cont("%08x", apic_read(base + i*0x10));
760
761 pr_cont("\n");
762}
763
764static void __init print_local_APIC(void *dummy)
765{
766 unsigned int i, v, ver, maxlvt;
767 u64 icr;
768
Jiang Liu849d3562014-10-27 16:12:01 +0800769 pr_debug("printing local APIC contents on CPU#%d/%d:\n",
770 smp_processor_id(), hard_smp_processor_id());
Jiang Liu74afab72014-10-27 16:12:00 +0800771 v = apic_read(APIC_ID);
Jiang Liu849d3562014-10-27 16:12:01 +0800772 pr_info("... APIC ID: %08x (%01x)\n", v, read_apic_id());
Jiang Liu74afab72014-10-27 16:12:00 +0800773 v = apic_read(APIC_LVR);
Jiang Liu849d3562014-10-27 16:12:01 +0800774 pr_info("... APIC VERSION: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800775 ver = GET_APIC_VERSION(v);
776 maxlvt = lapic_get_maxlvt();
777
778 v = apic_read(APIC_TASKPRI);
Jiang Liu849d3562014-10-27 16:12:01 +0800779 pr_debug("... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
Jiang Liu74afab72014-10-27 16:12:00 +0800780
781 /* !82489DX */
782 if (APIC_INTEGRATED(ver)) {
783 if (!APIC_XAPIC(ver)) {
784 v = apic_read(APIC_ARBPRI);
Jiang Liu849d3562014-10-27 16:12:01 +0800785 pr_debug("... APIC ARBPRI: %08x (%02x)\n",
786 v, v & APIC_ARBPRI_MASK);
Jiang Liu74afab72014-10-27 16:12:00 +0800787 }
788 v = apic_read(APIC_PROCPRI);
Jiang Liu849d3562014-10-27 16:12:01 +0800789 pr_debug("... APIC PROCPRI: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800790 }
791
792 /*
793 * Remote read supported only in the 82489DX and local APIC for
794 * Pentium processors.
795 */
796 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
797 v = apic_read(APIC_RRR);
Jiang Liu849d3562014-10-27 16:12:01 +0800798 pr_debug("... APIC RRR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800799 }
800
801 v = apic_read(APIC_LDR);
Jiang Liu849d3562014-10-27 16:12:01 +0800802 pr_debug("... APIC LDR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800803 if (!x2apic_enabled()) {
804 v = apic_read(APIC_DFR);
Jiang Liu849d3562014-10-27 16:12:01 +0800805 pr_debug("... APIC DFR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800806 }
807 v = apic_read(APIC_SPIV);
Jiang Liu849d3562014-10-27 16:12:01 +0800808 pr_debug("... APIC SPIV: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800809
Jiang Liu849d3562014-10-27 16:12:01 +0800810 pr_debug("... APIC ISR field:\n");
Jiang Liu74afab72014-10-27 16:12:00 +0800811 print_APIC_field(APIC_ISR);
Jiang Liu849d3562014-10-27 16:12:01 +0800812 pr_debug("... APIC TMR field:\n");
Jiang Liu74afab72014-10-27 16:12:00 +0800813 print_APIC_field(APIC_TMR);
Jiang Liu849d3562014-10-27 16:12:01 +0800814 pr_debug("... APIC IRR field:\n");
Jiang Liu74afab72014-10-27 16:12:00 +0800815 print_APIC_field(APIC_IRR);
816
817 /* !82489DX */
818 if (APIC_INTEGRATED(ver)) {
819 /* Due to the Pentium erratum 3AP. */
820 if (maxlvt > 3)
821 apic_write(APIC_ESR, 0);
822
823 v = apic_read(APIC_ESR);
Jiang Liu849d3562014-10-27 16:12:01 +0800824 pr_debug("... APIC ESR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800825 }
826
827 icr = apic_icr_read();
Jiang Liu849d3562014-10-27 16:12:01 +0800828 pr_debug("... APIC ICR: %08x\n", (u32)icr);
829 pr_debug("... APIC ICR2: %08x\n", (u32)(icr >> 32));
Jiang Liu74afab72014-10-27 16:12:00 +0800830
831 v = apic_read(APIC_LVTT);
Jiang Liu849d3562014-10-27 16:12:01 +0800832 pr_debug("... APIC LVTT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800833
834 if (maxlvt > 3) {
835 /* PC is LVT#4. */
836 v = apic_read(APIC_LVTPC);
Jiang Liu849d3562014-10-27 16:12:01 +0800837 pr_debug("... APIC LVTPC: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800838 }
839 v = apic_read(APIC_LVT0);
Jiang Liu849d3562014-10-27 16:12:01 +0800840 pr_debug("... APIC LVT0: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800841 v = apic_read(APIC_LVT1);
Jiang Liu849d3562014-10-27 16:12:01 +0800842 pr_debug("... APIC LVT1: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800843
844 if (maxlvt > 2) {
845 /* ERR is LVT#3. */
846 v = apic_read(APIC_LVTERR);
Jiang Liu849d3562014-10-27 16:12:01 +0800847 pr_debug("... APIC LVTERR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800848 }
849
850 v = apic_read(APIC_TMICT);
Jiang Liu849d3562014-10-27 16:12:01 +0800851 pr_debug("... APIC TMICT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800852 v = apic_read(APIC_TMCCT);
Jiang Liu849d3562014-10-27 16:12:01 +0800853 pr_debug("... APIC TMCCT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800854 v = apic_read(APIC_TDCR);
Jiang Liu849d3562014-10-27 16:12:01 +0800855 pr_debug("... APIC TDCR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800856
857 if (boot_cpu_has(X86_FEATURE_EXTAPIC)) {
858 v = apic_read(APIC_EFEAT);
859 maxlvt = (v >> 16) & 0xff;
Jiang Liu849d3562014-10-27 16:12:01 +0800860 pr_debug("... APIC EFEAT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800861 v = apic_read(APIC_ECTRL);
Jiang Liu849d3562014-10-27 16:12:01 +0800862 pr_debug("... APIC ECTRL: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800863 for (i = 0; i < maxlvt; i++) {
864 v = apic_read(APIC_EILVTn(i));
Jiang Liu849d3562014-10-27 16:12:01 +0800865 pr_debug("... APIC EILVT%d: %08x\n", i, v);
Jiang Liu74afab72014-10-27 16:12:00 +0800866 }
867 }
868 pr_cont("\n");
869}
870
871static void __init print_local_APICs(int maxcpu)
872{
873 int cpu;
874
875 if (!maxcpu)
876 return;
877
878 preempt_disable();
879 for_each_online_cpu(cpu) {
880 if (cpu >= maxcpu)
881 break;
882 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
883 }
884 preempt_enable();
885}
886
887static void __init print_PIC(void)
888{
889 unsigned int v;
890 unsigned long flags;
891
892 if (!nr_legacy_irqs())
893 return;
894
Jiang Liu849d3562014-10-27 16:12:01 +0800895 pr_debug("\nprinting PIC contents\n");
Jiang Liu74afab72014-10-27 16:12:00 +0800896
897 raw_spin_lock_irqsave(&i8259A_lock, flags);
898
899 v = inb(0xa1) << 8 | inb(0x21);
Jiang Liu849d3562014-10-27 16:12:01 +0800900 pr_debug("... PIC IMR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800901
902 v = inb(0xa0) << 8 | inb(0x20);
Jiang Liu849d3562014-10-27 16:12:01 +0800903 pr_debug("... PIC IRR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800904
905 outb(0x0b, 0xa0);
906 outb(0x0b, 0x20);
907 v = inb(0xa0) << 8 | inb(0x20);
908 outb(0x0a, 0xa0);
909 outb(0x0a, 0x20);
910
911 raw_spin_unlock_irqrestore(&i8259A_lock, flags);
912
Jiang Liu849d3562014-10-27 16:12:01 +0800913 pr_debug("... PIC ISR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800914
915 v = inb(0x4d1) << 8 | inb(0x4d0);
Jiang Liu849d3562014-10-27 16:12:01 +0800916 pr_debug("... PIC ELCR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800917}
918
919static int show_lapic __initdata = 1;
920static __init int setup_show_lapic(char *arg)
921{
922 int num = -1;
923
924 if (strcmp(arg, "all") == 0) {
925 show_lapic = CONFIG_NR_CPUS;
926 } else {
927 get_option(&arg, &num);
928 if (num >= 0)
929 show_lapic = num;
930 }
931
932 return 1;
933}
934__setup("show_lapic=", setup_show_lapic);
935
936static int __init print_ICs(void)
937{
938 if (apic_verbosity == APIC_QUIET)
939 return 0;
940
941 print_PIC();
942
943 /* don't print out if apic is not there */
Borislav Petkov93984fb2016-04-04 22:25:00 +0200944 if (!boot_cpu_has(X86_FEATURE_APIC) && !apic_from_smp_config())
Jiang Liu74afab72014-10-27 16:12:00 +0800945 return 0;
946
947 print_local_APICs(show_lapic);
948 print_IO_APICs();
949
950 return 0;
951}
952
953late_initcall(print_ICs);