blob: 3f53572c89cbf92b9de41e79229c9c417569024d [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 {
Thomas Gleixnerba224fe2017-09-13 23:29:45 +020028 struct irq_cfg hw_irq_cfg;
29 unsigned int vector;
30 unsigned int prev_vector;
Thomas Gleixner029c6e12017-09-13 23:29:31 +020031 unsigned int cpu;
32 unsigned int prev_cpu;
Thomas Gleixner69cde002017-09-13 23:29:42 +020033 unsigned int irq;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +020034 struct hlist_node clist;
Thomas Gleixner2db1f952017-09-13 23:29:50 +020035 unsigned int move_in_progress : 1,
36 is_managed : 1;
Jiang Liu7f3262e2015-04-14 10:30:03 +080037};
38
Jiang Liub5dc8e62015-04-13 14:11:24 +080039struct irq_domain *x86_vector_domain;
Jake Oshinsc8f3e512015-12-10 17:52:59 +000040EXPORT_SYMBOL_GPL(x86_vector_domain);
Jiang Liu74afab72014-10-27 16:12:00 +080041static DEFINE_RAW_SPINLOCK(vector_lock);
Thomas Gleixner69cde002017-09-13 23:29:42 +020042static cpumask_var_t vector_searchmask;
Jiang Liub5dc8e62015-04-13 14:11:24 +080043static struct irq_chip lapic_controller;
Thomas Gleixner0fa115d2017-09-13 23:29:38 +020044static struct irq_matrix *vector_matrix;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +020045#ifdef CONFIG_SMP
46static DEFINE_PER_CPU(struct hlist_head, cleanup_list);
47#endif
Jiang Liu74afab72014-10-27 16:12:00 +080048
49void lock_vector_lock(void)
50{
51 /* Used to the online set of cpus does not change
52 * during assign_irq_vector.
53 */
54 raw_spin_lock(&vector_lock);
55}
56
57void unlock_vector_lock(void)
58{
59 raw_spin_unlock(&vector_lock);
60}
61
Thomas Gleixner99a14822017-09-13 23:29:36 +020062void init_irq_alloc_info(struct irq_alloc_info *info,
63 const struct cpumask *mask)
64{
65 memset(info, 0, sizeof(*info));
66 info->mask = mask;
67}
68
69void copy_irq_alloc_info(struct irq_alloc_info *dst, struct irq_alloc_info *src)
70{
71 if (src)
72 *dst = *src;
73 else
74 memset(dst, 0, sizeof(*dst));
75}
76
Thomas Gleixner86ba6552017-09-13 23:29:30 +020077static struct apic_chip_data *apic_chip_data(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +080078{
Thomas Gleixner86ba6552017-09-13 23:29:30 +020079 if (!irqd)
Jiang Liub5dc8e62015-04-13 14:11:24 +080080 return NULL;
81
Thomas Gleixner86ba6552017-09-13 23:29:30 +020082 while (irqd->parent_data)
83 irqd = irqd->parent_data;
Jiang Liub5dc8e62015-04-13 14:11:24 +080084
Thomas Gleixner86ba6552017-09-13 23:29:30 +020085 return irqd->chip_data;
Jiang Liu74afab72014-10-27 16:12:00 +080086}
87
Thomas Gleixner86ba6552017-09-13 23:29:30 +020088struct irq_cfg *irqd_cfg(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +080089{
Thomas Gleixner86ba6552017-09-13 23:29:30 +020090 struct apic_chip_data *apicd = apic_chip_data(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +080091
Thomas Gleixnerba224fe2017-09-13 23:29:45 +020092 return apicd ? &apicd->hw_irq_cfg : NULL;
Jiang Liu7f3262e2015-04-14 10:30:03 +080093}
Jake Oshinsc8f3e512015-12-10 17:52:59 +000094EXPORT_SYMBOL_GPL(irqd_cfg);
Jiang Liu7f3262e2015-04-14 10:30:03 +080095
96struct irq_cfg *irq_cfg(unsigned int irq)
97{
98 return irqd_cfg(irq_get_irq_data(irq));
99}
100
101static struct apic_chip_data *alloc_apic_chip_data(int node)
102{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200103 struct apic_chip_data *apicd;
Jiang Liu7f3262e2015-04-14 10:30:03 +0800104
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200105 apicd = kzalloc_node(sizeof(*apicd), GFP_KERNEL, node);
Thomas Gleixner69cde002017-09-13 23:29:42 +0200106 if (apicd)
107 INIT_HLIST_NODE(&apicd->clist);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200108 return apicd;
Jiang Liu74afab72014-10-27 16:12:00 +0800109}
110
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200111static void free_apic_chip_data(struct apic_chip_data *apicd)
Jiang Liu74afab72014-10-27 16:12:00 +0800112{
Thomas Gleixner69cde002017-09-13 23:29:42 +0200113 kfree(apicd);
Jiang Liu74afab72014-10-27 16:12:00 +0800114}
115
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200116static void apic_update_irq_cfg(struct irq_data *irqd, unsigned int vector,
117 unsigned int cpu)
Jiang Liu74afab72014-10-27 16:12:00 +0800118{
Thomas Gleixner69cde002017-09-13 23:29:42 +0200119 struct apic_chip_data *apicd = apic_chip_data(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800120
Thomas Gleixner69cde002017-09-13 23:29:42 +0200121 lockdep_assert_held(&vector_lock);
Jiang Liu74afab72014-10-27 16:12:00 +0800122
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200123 apicd->hw_irq_cfg.vector = vector;
124 apicd->hw_irq_cfg.dest_apicid = apic->calc_dest_apicid(cpu);
125 irq_data_update_effective_affinity(irqd, cpumask_of(cpu));
126 trace_vector_config(irqd->irq, vector, cpu,
127 apicd->hw_irq_cfg.dest_apicid);
Thomas Gleixner69cde002017-09-13 23:29:42 +0200128}
Jiang Liu74afab72014-10-27 16:12:00 +0800129
Thomas Gleixner69cde002017-09-13 23:29:42 +0200130static void apic_update_vector(struct irq_data *irqd, unsigned int newvec,
131 unsigned int newcpu)
132{
133 struct apic_chip_data *apicd = apic_chip_data(irqd);
134 struct irq_desc *desc = irq_data_to_desc(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800135
Thomas Gleixner69cde002017-09-13 23:29:42 +0200136 lockdep_assert_held(&vector_lock);
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000137
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200138 trace_vector_update(irqd->irq, newvec, newcpu, apicd->vector,
Thomas Gleixner69cde002017-09-13 23:29:42 +0200139 apicd->cpu);
Jiang Liu74afab72014-10-27 16:12:00 +0800140
Thomas Gleixner69cde002017-09-13 23:29:42 +0200141 /* Setup the vector move, if required */
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200142 if (apicd->vector && cpu_online(apicd->cpu)) {
Thomas Gleixner69cde002017-09-13 23:29:42 +0200143 apicd->move_in_progress = true;
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200144 apicd->prev_vector = apicd->vector;
Thomas Gleixner69cde002017-09-13 23:29:42 +0200145 apicd->prev_cpu = apicd->cpu;
146 } else {
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200147 apicd->prev_vector = 0;
Jiang Liu74afab72014-10-27 16:12:00 +0800148 }
Jiang Liu74afab72014-10-27 16:12:00 +0800149
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200150 apicd->vector = newvec;
Thomas Gleixner69cde002017-09-13 23:29:42 +0200151 apicd->cpu = newcpu;
152 BUG_ON(!IS_ERR_OR_NULL(per_cpu(vector_irq, newcpu)[newvec]));
153 per_cpu(vector_irq, newcpu)[newvec] = desc;
154}
155
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200156static void vector_assign_managed_shutdown(struct irq_data *irqd)
157{
158 unsigned int cpu = cpumask_first(cpu_online_mask);
159
160 apic_update_irq_cfg(irqd, MANAGED_IRQ_SHUTDOWN_VECTOR, cpu);
161}
162
163static int reserve_managed_vector(struct irq_data *irqd)
164{
165 const struct cpumask *affmsk = irq_data_get_affinity_mask(irqd);
166 struct apic_chip_data *apicd = apic_chip_data(irqd);
167 unsigned long flags;
168 int ret;
169
170 raw_spin_lock_irqsave(&vector_lock, flags);
171 apicd->is_managed = true;
172 ret = irq_matrix_reserve_managed(vector_matrix, affmsk);
173 raw_spin_unlock_irqrestore(&vector_lock, flags);
174 trace_vector_reserve_managed(irqd->irq, ret);
175 return ret;
176}
177
Thomas Gleixner69cde002017-09-13 23:29:42 +0200178static int allocate_vector(struct irq_data *irqd, const struct cpumask *dest)
179{
180 struct apic_chip_data *apicd = apic_chip_data(irqd);
Thomas Gleixner69cde002017-09-13 23:29:42 +0200181 unsigned int cpu = apicd->cpu;
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200182 int vector = apicd->vector;
183
184 lockdep_assert_held(&vector_lock);
Thomas Gleixner69cde002017-09-13 23:29:42 +0200185
Thomas Gleixner847667e2015-12-31 16:30:50 +0000186 /*
Thomas Gleixner69cde002017-09-13 23:29:42 +0200187 * If the current target CPU is online and in the new requested
188 * affinity mask, there is no point in moving the interrupt from
189 * one CPU to another.
Thomas Gleixner847667e2015-12-31 16:30:50 +0000190 */
Thomas Gleixner69cde002017-09-13 23:29:42 +0200191 if (vector && cpu_online(cpu) && cpumask_test_cpu(cpu, dest))
192 return 0;
193
194 vector = irq_matrix_alloc(vector_matrix, dest, false, &cpu);
195 if (vector > 0)
196 apic_update_vector(irqd, vector, cpu);
197 trace_vector_alloc(irqd->irq, vector, false, vector);
198 return vector;
199}
200
201static int assign_vector_locked(struct irq_data *irqd,
202 const struct cpumask *dest)
203{
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200204 struct apic_chip_data *apicd = apic_chip_data(irqd);
Thomas Gleixner69cde002017-09-13 23:29:42 +0200205 int vector = allocate_vector(irqd, dest);
206
207 if (vector < 0)
208 return vector;
209
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200210 apic_update_irq_cfg(irqd, apicd->vector, apicd->cpu);
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000211 return 0;
Jiang Liu74afab72014-10-27 16:12:00 +0800212}
213
Thomas Gleixner69cde002017-09-13 23:29:42 +0200214static int assign_irq_vector(struct irq_data *irqd, const struct cpumask *dest)
Jiang Liu74afab72014-10-27 16:12:00 +0800215{
Jiang Liu74afab72014-10-27 16:12:00 +0800216 unsigned long flags;
Thomas Gleixner69cde002017-09-13 23:29:42 +0200217 int ret;
Jiang Liu74afab72014-10-27 16:12:00 +0800218
219 raw_spin_lock_irqsave(&vector_lock, flags);
Thomas Gleixner69cde002017-09-13 23:29:42 +0200220 cpumask_and(vector_searchmask, dest, cpu_online_mask);
221 ret = assign_vector_locked(irqd, vector_searchmask);
Jiang Liu74afab72014-10-27 16:12:00 +0800222 raw_spin_unlock_irqrestore(&vector_lock, flags);
Thomas Gleixner69cde002017-09-13 23:29:42 +0200223 return ret;
Jiang Liu74afab72014-10-27 16:12:00 +0800224}
225
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200226static int assign_irq_vector_any_locked(struct irq_data *irqd)
Jiang Liu486ca532015-05-07 10:53:56 +0800227{
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200228 int node = irq_data_get_node(irqd);
229
230 if (node != NUMA_NO_NODE) {
231 if (!assign_vector_locked(irqd, cpumask_of_node(node)))
232 return 0;
233 }
234 return assign_vector_locked(irqd, cpu_online_mask);
235}
236
237static int assign_irq_vector_any(struct irq_data *irqd)
238{
239 unsigned long flags;
240 int ret;
241
242 raw_spin_lock_irqsave(&vector_lock, flags);
243 ret = assign_irq_vector_any_locked(irqd);
244 raw_spin_unlock_irqrestore(&vector_lock, flags);
245 return ret;
246}
247
248static int
249assign_irq_vector_policy(struct irq_data *irqd, struct irq_alloc_info *info)
250{
251 if (irqd_affinity_is_managed(irqd))
252 return reserve_managed_vector(irqd);
Thomas Gleixner258d86e2017-09-13 23:29:35 +0200253 if (info->mask)
Thomas Gleixner69cde002017-09-13 23:29:42 +0200254 return assign_irq_vector(irqd, info->mask);
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200255 return assign_irq_vector_any(irqd);
256}
257
258static int
259assign_managed_vector(struct irq_data *irqd, const struct cpumask *dest)
260{
261 const struct cpumask *affmsk = irq_data_get_affinity_mask(irqd);
262 struct apic_chip_data *apicd = apic_chip_data(irqd);
263 int vector, cpu;
264
265 cpumask_and(vector_searchmask, vector_searchmask, affmsk);
266 cpu = cpumask_first(vector_searchmask);
267 if (cpu >= nr_cpu_ids)
268 return -EINVAL;
269 /* set_affinity might call here for nothing */
270 if (apicd->vector && cpumask_test_cpu(apicd->cpu, vector_searchmask))
Jiang Liu486ca532015-05-07 10:53:56 +0800271 return 0;
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200272 vector = irq_matrix_alloc_managed(vector_matrix, cpu);
273 trace_vector_alloc_managed(irqd->irq, vector, vector);
274 if (vector < 0)
275 return vector;
276 apic_update_vector(irqd, vector, cpu);
277 apic_update_irq_cfg(irqd, vector, cpu);
278 return 0;
Jiang Liu486ca532015-05-07 10:53:56 +0800279}
280
Thomas Gleixner69cde002017-09-13 23:29:42 +0200281static void clear_irq_vector(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +0800282{
Thomas Gleixner69cde002017-09-13 23:29:42 +0200283 struct apic_chip_data *apicd = apic_chip_data(irqd);
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200284 bool managed = irqd_affinity_is_managed(irqd);
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200285 unsigned int vector = apicd->vector;
Jiang Liu74afab72014-10-27 16:12:00 +0800286
Thomas Gleixner69cde002017-09-13 23:29:42 +0200287 lockdep_assert_held(&vector_lock);
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200288
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200289 if (!vector)
Keith Busch1bdb8972016-04-27 14:22:32 -0600290 return;
Jiang Liu74afab72014-10-27 16:12:00 +0800291
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200292 trace_vector_clear(irqd->irq, vector, apicd->cpu, apicd->prev_vector,
Thomas Gleixner69cde002017-09-13 23:29:42 +0200293 apicd->prev_cpu);
294
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200295 per_cpu(vector_irq, apicd->cpu)[vector] = VECTOR_UNUSED;
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200296 irq_matrix_free(vector_matrix, apicd->cpu, vector, managed);
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200297 apicd->vector = 0;
Jiang Liu74afab72014-10-27 16:12:00 +0800298
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200299 /* Clean up move in progress */
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200300 vector = apicd->prev_vector;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200301 if (!vector)
Jiang Liu74afab72014-10-27 16:12:00 +0800302 return;
Jiang Liu74afab72014-10-27 16:12:00 +0800303
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200304 per_cpu(vector_irq, apicd->prev_cpu)[vector] = VECTOR_UNUSED;
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200305 irq_matrix_free(vector_matrix, apicd->prev_cpu, vector, managed);
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200306 apicd->prev_vector = 0;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200307 apicd->move_in_progress = 0;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200308 hlist_del_init(&apicd->clist);
Jiang Liu74afab72014-10-27 16:12:00 +0800309}
310
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200311static void x86_vector_deactivate(struct irq_domain *dom, struct irq_data *irqd)
312{
313 struct apic_chip_data *apicd = apic_chip_data(irqd);
314 unsigned long flags;
315
316 trace_vector_deactivate(irqd->irq, apicd->is_managed,
317 false, false);
318
319 if (apicd->is_managed)
320 return;
321
322 raw_spin_lock_irqsave(&vector_lock, flags);
323 clear_irq_vector(irqd);
324 vector_assign_managed_shutdown(irqd);
325 raw_spin_unlock_irqrestore(&vector_lock, flags);
326}
327
328static int activate_managed(struct irq_data *irqd)
329{
330 const struct cpumask *dest = irq_data_get_affinity_mask(irqd);
331 int ret;
332
333 cpumask_and(vector_searchmask, dest, cpu_online_mask);
334 if (WARN_ON_ONCE(cpumask_empty(vector_searchmask))) {
335 /* Something in the core code broke! Survive gracefully */
336 pr_err("Managed startup for irq %u, but no CPU\n", irqd->irq);
337 return EINVAL;
338 }
339
340 ret = assign_managed_vector(irqd, vector_searchmask);
341 /*
342 * This should not happen. The vector reservation got buggered. Handle
343 * it gracefully.
344 */
345 if (WARN_ON_ONCE(ret < 0)) {
346 pr_err("Managed startup irq %u, no vector available\n",
347 irqd->irq);
348 }
349 return ret;
350}
351
352static int x86_vector_activate(struct irq_domain *dom, struct irq_data *irqd,
353 bool early)
354{
355 struct apic_chip_data *apicd = apic_chip_data(irqd);
356 unsigned long flags;
357 int ret = 0;
358
359 trace_vector_activate(irqd->irq, apicd->is_managed,
360 false, early);
361
362 if (!apicd->is_managed)
363 return 0;
364
365 raw_spin_lock_irqsave(&vector_lock, flags);
366 if (early || irqd_is_managed_and_shutdown(irqd))
367 vector_assign_managed_shutdown(irqd);
368 else
369 ret = activate_managed(irqd);
370 raw_spin_unlock_irqrestore(&vector_lock, flags);
371 return ret;
372}
373
374static void vector_free_reserved_and_managed(struct irq_data *irqd)
375{
376 const struct cpumask *dest = irq_data_get_affinity_mask(irqd);
377 struct apic_chip_data *apicd = apic_chip_data(irqd);
378
379 trace_vector_teardown(irqd->irq, apicd->is_managed, false);
380
381 if (apicd->is_managed)
382 irq_matrix_remove_managed(vector_matrix, dest);
383}
384
Jiang Liub5dc8e62015-04-13 14:11:24 +0800385static void x86_vector_free_irqs(struct irq_domain *domain,
386 unsigned int virq, unsigned int nr_irqs)
387{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200388 struct apic_chip_data *apicd;
389 struct irq_data *irqd;
Jiang Liu111abeb2015-12-31 16:30:44 +0000390 unsigned long flags;
Jiang Liub5dc8e62015-04-13 14:11:24 +0800391 int i;
392
393 for (i = 0; i < nr_irqs; i++) {
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200394 irqd = irq_domain_get_irq_data(x86_vector_domain, virq + i);
395 if (irqd && irqd->chip_data) {
Jiang Liu111abeb2015-12-31 16:30:44 +0000396 raw_spin_lock_irqsave(&vector_lock, flags);
Thomas Gleixner69cde002017-09-13 23:29:42 +0200397 clear_irq_vector(irqd);
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200398 vector_free_reserved_and_managed(irqd);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200399 apicd = irqd->chip_data;
400 irq_domain_reset_irq_data(irqd);
Jiang Liu111abeb2015-12-31 16:30:44 +0000401 raw_spin_unlock_irqrestore(&vector_lock, flags);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200402 free_apic_chip_data(apicd);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800403 }
404 }
405}
406
407static int x86_vector_alloc_irqs(struct irq_domain *domain, unsigned int virq,
408 unsigned int nr_irqs, void *arg)
409{
410 struct irq_alloc_info *info = arg;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200411 struct apic_chip_data *apicd;
412 struct irq_data *irqd;
Thomas Gleixner69cde002017-09-13 23:29:42 +0200413 unsigned long flags;
Jiang Liu5f2dbbc2015-06-01 16:05:14 +0800414 int i, err, node;
Jiang Liub5dc8e62015-04-13 14:11:24 +0800415
416 if (disable_apic)
417 return -ENXIO;
418
419 /* Currently vector allocator can't guarantee contiguous allocations */
420 if ((info->flags & X86_IRQ_ALLOC_CONTIGUOUS_VECTORS) && nr_irqs > 1)
421 return -ENOSYS;
422
Jiang Liub5dc8e62015-04-13 14:11:24 +0800423 for (i = 0; i < nr_irqs; i++) {
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200424 irqd = irq_domain_get_irq_data(domain, virq + i);
425 BUG_ON(!irqd);
426 node = irq_data_get_node(irqd);
Thomas Gleixner4ef76eb2017-09-13 23:29:34 +0200427 WARN_ON_ONCE(irqd->chip_data);
428 apicd = alloc_apic_chip_data(node);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200429 if (!apicd) {
Jiang Liub5dc8e62015-04-13 14:11:24 +0800430 err = -ENOMEM;
431 goto error;
432 }
433
Thomas Gleixner69cde002017-09-13 23:29:42 +0200434 apicd->irq = virq + i;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200435 irqd->chip = &lapic_controller;
436 irqd->chip_data = apicd;
437 irqd->hwirq = virq + i;
438 irqd_set_single_target(irqd);
Thomas Gleixner4ef76eb2017-09-13 23:29:34 +0200439 /*
Thomas Gleixner69cde002017-09-13 23:29:42 +0200440 * Legacy vectors are already assigned when the IOAPIC
441 * takes them over. They stay on the same vector. This is
442 * required for check_timer() to work correctly as it might
443 * switch back to legacy mode. Only update the hardware
444 * config.
Thomas Gleixner4ef76eb2017-09-13 23:29:34 +0200445 */
446 if (info->flags & X86_IRQ_ALLOC_LEGACY) {
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200447 apicd->vector = ISA_IRQ_VECTOR(virq + i);
Thomas Gleixner4ef76eb2017-09-13 23:29:34 +0200448 apicd->cpu = 0;
Thomas Gleixner69cde002017-09-13 23:29:42 +0200449 trace_vector_setup(virq + i, true, 0);
450 raw_spin_lock_irqsave(&vector_lock, flags);
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200451 apic_update_irq_cfg(irqd, apicd->vector, apicd->cpu);
Thomas Gleixner69cde002017-09-13 23:29:42 +0200452 raw_spin_unlock_irqrestore(&vector_lock, flags);
453 continue;
Thomas Gleixner4ef76eb2017-09-13 23:29:34 +0200454 }
455
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200456 err = assign_irq_vector_policy(irqd, info);
Thomas Gleixner69cde002017-09-13 23:29:42 +0200457 trace_vector_setup(virq + i, false, err);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800458 if (err)
459 goto error;
460 }
461
462 return 0;
463
464error:
465 x86_vector_free_irqs(domain, virq, i + 1);
466 return err;
467}
468
Thomas Gleixner65d7ed52017-09-13 23:29:39 +0200469#ifdef CONFIG_GENERIC_IRQ_DEBUGFS
470void x86_vector_debug_show(struct seq_file *m, struct irq_domain *d,
471 struct irq_data *irqd, int ind)
472{
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200473 unsigned int cpu, vector, prev_cpu, prev_vector;
Thomas Gleixner65d7ed52017-09-13 23:29:39 +0200474 struct apic_chip_data *apicd;
475 unsigned long flags;
476 int irq;
477
478 if (!irqd) {
479 irq_matrix_debug_show(m, vector_matrix, ind);
480 return;
481 }
482
483 irq = irqd->irq;
484 if (irq < nr_legacy_irqs() && !test_bit(irq, &io_apic_irqs)) {
485 seq_printf(m, "%*sVector: %5d\n", ind, "", ISA_IRQ_VECTOR(irq));
486 seq_printf(m, "%*sTarget: Legacy PIC all CPUs\n", ind, "");
487 return;
488 }
489
490 apicd = irqd->chip_data;
491 if (!apicd) {
492 seq_printf(m, "%*sVector: Not assigned\n", ind, "");
493 return;
494 }
495
496 raw_spin_lock_irqsave(&vector_lock, flags);
497 cpu = apicd->cpu;
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200498 vector = apicd->vector;
Thomas Gleixner65d7ed52017-09-13 23:29:39 +0200499 prev_cpu = apicd->prev_cpu;
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200500 prev_vector = apicd->prev_vector;
Thomas Gleixner65d7ed52017-09-13 23:29:39 +0200501 raw_spin_unlock_irqrestore(&vector_lock, flags);
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200502 seq_printf(m, "%*sVector: %5u\n", ind, "", vector);
Thomas Gleixner65d7ed52017-09-13 23:29:39 +0200503 seq_printf(m, "%*sTarget: %5u\n", ind, "", cpu);
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200504 if (prev_vector) {
505 seq_printf(m, "%*sPrevious vector: %5u\n", ind, "", prev_vector);
Thomas Gleixner65d7ed52017-09-13 23:29:39 +0200506 seq_printf(m, "%*sPrevious target: %5u\n", ind, "", prev_cpu);
507 }
508}
509#endif
510
Thomas Gleixnereb18cf52015-05-05 11:10:11 +0200511static const struct irq_domain_ops x86_vector_domain_ops = {
Thomas Gleixner65d7ed52017-09-13 23:29:39 +0200512 .alloc = x86_vector_alloc_irqs,
513 .free = x86_vector_free_irqs,
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200514 .activate = x86_vector_activate,
515 .deactivate = x86_vector_deactivate,
Thomas Gleixner65d7ed52017-09-13 23:29:39 +0200516#ifdef CONFIG_GENERIC_IRQ_DEBUGFS
517 .debug_show = x86_vector_debug_show,
518#endif
Jiang Liub5dc8e62015-04-13 14:11:24 +0800519};
520
Jiang Liu11d686e2014-10-27 16:12:05 +0800521int __init arch_probe_nr_irqs(void)
522{
523 int nr;
524
525 if (nr_irqs > (NR_VECTORS * nr_cpu_ids))
526 nr_irqs = NR_VECTORS * nr_cpu_ids;
527
528 nr = (gsi_top + nr_legacy_irqs()) + 8 * nr_cpu_ids;
529#if defined(CONFIG_PCI_MSI) || defined(CONFIG_HT_IRQ)
530 /*
531 * for MSI and HT dyn irq
532 */
533 if (gsi_top <= NR_IRQS_LEGACY)
534 nr += 8 * nr_cpu_ids;
535 else
536 nr += gsi_top * 16;
537#endif
538 if (nr < nr_irqs)
539 nr_irqs = nr;
540
Vitaly Kuznetsov8c058b02015-11-03 10:40:14 +0100541 /*
542 * We don't know if PIC is present at this point so we need to do
543 * probe() to get the right number of legacy IRQs.
544 */
545 return legacy_pic->probe();
Jiang Liu11d686e2014-10-27 16:12:05 +0800546}
547
Thomas Gleixner0fa115d2017-09-13 23:29:38 +0200548void lapic_assign_legacy_vector(unsigned int irq, bool replace)
549{
550 /*
551 * Use assign system here so it wont get accounted as allocated
552 * and moveable in the cpu hotplug check and it prevents managed
553 * irq reservation from touching it.
554 */
555 irq_matrix_assign_system(vector_matrix, ISA_IRQ_VECTOR(irq), replace);
556}
557
558void __init lapic_assign_system_vectors(void)
559{
560 unsigned int i, vector = 0;
561
562 for_each_set_bit_from(vector, system_vectors, NR_VECTORS)
563 irq_matrix_assign_system(vector_matrix, vector, false);
564
565 if (nr_legacy_irqs() > 1)
566 lapic_assign_legacy_vector(PIC_CASCADE_IR, false);
567
568 /* System vectors are reserved, online it */
569 irq_matrix_online(vector_matrix);
570
571 /* Mark the preallocated legacy interrupts */
572 for (i = 0; i < nr_legacy_irqs(); i++) {
573 if (i != PIC_CASCADE_IR)
574 irq_matrix_assign(vector_matrix, ISA_IRQ_VECTOR(i));
575 }
576}
577
Jiang Liu11d686e2014-10-27 16:12:05 +0800578int __init arch_early_irq_init(void)
579{
Thomas Gleixner9d35f852017-06-20 01:37:06 +0200580 struct fwnode_handle *fn;
581
Thomas Gleixner9d35f852017-06-20 01:37:06 +0200582 fn = irq_domain_alloc_named_fwnode("VECTOR");
583 BUG_ON(!fn);
584 x86_vector_domain = irq_domain_create_tree(fn, &x86_vector_domain_ops,
585 NULL);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800586 BUG_ON(x86_vector_domain == NULL);
Thomas Gleixner9d35f852017-06-20 01:37:06 +0200587 irq_domain_free_fwnode(fn);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800588 irq_set_default_host(x86_vector_domain);
589
Jiang Liu52f518a2015-04-13 14:11:35 +0800590 arch_init_msi_domain(x86_vector_domain);
Jiang Liu49e07d82015-04-13 14:11:43 +0800591 arch_init_htirq_domain(x86_vector_domain);
Jiang Liu52f518a2015-04-13 14:11:35 +0800592
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000593 BUG_ON(!alloc_cpumask_var(&vector_searchmask, GFP_KERNEL));
Jiang Liuf7fa7ae2015-04-14 10:30:10 +0800594
Thomas Gleixner0fa115d2017-09-13 23:29:38 +0200595 /*
596 * Allocate the vector matrix allocator data structure and limit the
597 * search area.
598 */
599 vector_matrix = irq_alloc_matrix(NR_VECTORS, FIRST_EXTERNAL_VECTOR,
600 FIRST_SYSTEM_VECTOR);
601 BUG_ON(!vector_matrix);
602
Jiang Liu11d686e2014-10-27 16:12:05 +0800603 return arch_early_ioapic_init();
604}
605
Thomas Gleixnerba801642017-09-13 23:29:44 +0200606#ifdef CONFIG_SMP
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200607/* Temporary hack to keep things working */
608static void vector_update_shutdown_irqs(void)
Jiang Liu74afab72014-10-27 16:12:00 +0800609{
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000610 struct irq_desc *desc;
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200611 int irq;
Jiang Liu74afab72014-10-27 16:12:00 +0800612
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000613 for_each_irq_desc(irq, desc) {
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200614 struct irq_data *irqd = irq_desc_get_irq_data(desc);
615 struct apic_chip_data *ad = apic_chip_data(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800616
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200617 if (!ad || !ad->vector || ad->cpu != smp_processor_id())
Thomas Gleixner69cde002017-09-13 23:29:42 +0200618 continue;
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200619 this_cpu_write(vector_irq[ad->vector], desc);
620 irq_matrix_assign(vector_matrix, ad->vector);
Jiang Liu74afab72014-10-27 16:12:00 +0800621 }
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200622}
Jiang Liu74afab72014-10-27 16:12:00 +0800623
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200624static struct irq_desc *__setup_vector_irq(int vector)
625{
626 int isairq = vector - ISA_IRQ_VECTOR(0);
627
628 /* Check whether the irq is in the legacy space */
629 if (isairq < 0 || isairq >= nr_legacy_irqs())
630 return VECTOR_UNUSED;
631 /* Check whether the irq is handled by the IOAPIC */
632 if (test_bit(isairq, &io_apic_irqs))
633 return VECTOR_UNUSED;
634 return irq_to_desc(isairq);
Jiang Liu74afab72014-10-27 16:12:00 +0800635}
636
Thomas Gleixner0fa115d2017-09-13 23:29:38 +0200637/* Online the local APIC infrastructure and initialize the vectors */
638void lapic_online(void)
Jiang Liu74afab72014-10-27 16:12:00 +0800639{
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200640 unsigned int vector;
Jiang Liu74afab72014-10-27 16:12:00 +0800641
Thomas Gleixner5a3f75e2015-07-05 17:12:32 +0000642 lockdep_assert_held(&vector_lock);
Thomas Gleixner0fa115d2017-09-13 23:29:38 +0200643
644 /* Online the vector matrix array for this CPU */
645 irq_matrix_online(vector_matrix);
646
Jiang Liu74afab72014-10-27 16:12:00 +0800647 /*
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200648 * The interrupt affinity logic never targets interrupts to offline
649 * CPUs. The exception are the legacy PIC interrupts. In general
650 * they are only targeted to CPU0, but depending on the platform
651 * they can be distributed to any online CPU in hardware. The
652 * kernel has no influence on that. So all active legacy vectors
653 * must be installed on all CPUs. All non legacy interrupts can be
654 * cleared.
Jiang Liu74afab72014-10-27 16:12:00 +0800655 */
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200656 for (vector = 0; vector < NR_VECTORS; vector++)
657 this_cpu_write(vector_irq[vector], __setup_vector_irq(vector));
Jiang Liu74afab72014-10-27 16:12:00 +0800658
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200659 /*
660 * Until the rewrite of the managed interrupt management is in
661 * place it's necessary to walk the irq descriptors and check for
662 * interrupts which are targeted at this CPU.
663 */
664 vector_update_shutdown_irqs();
Jiang Liu74afab72014-10-27 16:12:00 +0800665}
666
Thomas Gleixner0fa115d2017-09-13 23:29:38 +0200667void lapic_offline(void)
668{
669 lock_vector_lock();
670 irq_matrix_offline(vector_matrix);
671 unlock_vector_lock();
672}
673
Thomas Gleixnerba801642017-09-13 23:29:44 +0200674static int apic_set_affinity(struct irq_data *irqd,
675 const struct cpumask *dest, bool force)
676{
677 int err;
678
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200679 raw_spin_lock(&vector_lock);
680 cpumask_and(vector_searchmask, dest, cpu_online_mask);
681 if (irqd_affinity_is_managed(irqd))
682 err = assign_managed_vector(irqd, vector_searchmask);
683 else
684 err = assign_vector_locked(irqd, vector_searchmask);
685 raw_spin_unlock(&vector_lock);
Thomas Gleixnerba801642017-09-13 23:29:44 +0200686 return err ? err : IRQ_SET_MASK_OK;
687}
688
689#else
690# define apic_set_affinity NULL
691#endif
692
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200693static int apic_retrigger_irq(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +0800694{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200695 struct apic_chip_data *apicd = apic_chip_data(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800696 unsigned long flags;
Jiang Liu74afab72014-10-27 16:12:00 +0800697
698 raw_spin_lock_irqsave(&vector_lock, flags);
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200699 apic->send_IPI(apicd->cpu, apicd->vector);
Jiang Liu74afab72014-10-27 16:12:00 +0800700 raw_spin_unlock_irqrestore(&vector_lock, flags);
701
702 return 1;
703}
704
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200705void apic_ack_edge(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +0800706{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200707 irq_complete_move(irqd_cfg(irqd));
708 irq_move_irq(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800709 ack_APIC_irq();
710}
711
Jiang Liub5dc8e62015-04-13 14:11:24 +0800712static struct irq_chip lapic_controller = {
Thomas Gleixner8947dfb2017-06-20 01:37:01 +0200713 .name = "APIC",
Jiang Liub5dc8e62015-04-13 14:11:24 +0800714 .irq_ack = apic_ack_edge,
Jiang Liu68f9f442015-04-14 10:30:01 +0800715 .irq_set_affinity = apic_set_affinity,
Jiang Liub5dc8e62015-04-13 14:11:24 +0800716 .irq_retrigger = apic_retrigger_irq,
717};
718
Jiang Liu74afab72014-10-27 16:12:00 +0800719#ifdef CONFIG_SMP
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200720
Thomas Gleixner69cde002017-09-13 23:29:42 +0200721static void free_moved_vector(struct apic_chip_data *apicd)
722{
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200723 unsigned int vector = apicd->prev_vector;
Thomas Gleixner69cde002017-09-13 23:29:42 +0200724 unsigned int cpu = apicd->prev_cpu;
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200725 bool managed = apicd->is_managed;
Thomas Gleixner69cde002017-09-13 23:29:42 +0200726
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200727 /*
728 * This should never happen. Managed interrupts are not
729 * migrated except on CPU down, which does not involve the
730 * cleanup vector. But try to keep the accounting correct
731 * nevertheless.
732 */
733 WARN_ON_ONCE(managed);
734
735 trace_vector_free_moved(apicd->irq, vector, managed);
736 irq_matrix_free(vector_matrix, cpu, vector, managed);
Thomas Gleixner69cde002017-09-13 23:29:42 +0200737 __this_cpu_write(vector_irq[vector], VECTOR_UNUSED);
738 hlist_del_init(&apicd->clist);
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200739 apicd->prev_vector = 0;
Thomas Gleixner69cde002017-09-13 23:29:42 +0200740 apicd->move_in_progress = 0;
741}
742
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200743asmlinkage __visible void __irq_entry smp_irq_move_cleanup_interrupt(void)
744{
745 struct hlist_head *clhead = this_cpu_ptr(&cleanup_list);
746 struct apic_chip_data *apicd;
747 struct hlist_node *tmp;
748
749 entering_ack_irq();
750 /* Prevent vectors vanishing under us */
751 raw_spin_lock(&vector_lock);
752
753 hlist_for_each_entry_safe(apicd, tmp, clhead, clist) {
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200754 unsigned int irr, vector = apicd->prev_vector;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200755
756 /*
757 * Paranoia: Check if the vector that needs to be cleaned
758 * up is registered at the APICs IRR. If so, then this is
759 * not the best time to clean it up. Clean it up in the
760 * next attempt by sending another IRQ_MOVE_CLEANUP_VECTOR
761 * to this CPU. IRQ_MOVE_CLEANUP_VECTOR is the lowest
762 * priority external vector, so on return from this
763 * interrupt the device interrupt will happen first.
764 */
765 irr = apic_read(APIC_IRR + (vector / 32 * 0x10));
766 if (irr & (1U << (vector % 32))) {
767 apic->send_IPI_self(IRQ_MOVE_CLEANUP_VECTOR);
768 continue;
769 }
Thomas Gleixner69cde002017-09-13 23:29:42 +0200770 free_moved_vector(apicd);
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200771 }
772
773 raw_spin_unlock(&vector_lock);
774 exiting_irq();
775}
776
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200777static void __send_cleanup_vector(struct apic_chip_data *apicd)
Jiang Liu74afab72014-10-27 16:12:00 +0800778{
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200779 unsigned int cpu;
780
Thomas Gleixnerc1684f52015-12-31 16:30:51 +0000781 raw_spin_lock(&vector_lock);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200782 apicd->move_in_progress = 0;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200783 cpu = apicd->prev_cpu;
784 if (cpu_online(cpu)) {
785 hlist_add_head(&apicd->clist, per_cpu_ptr(&cleanup_list, cpu));
786 apic->send_IPI(cpu, IRQ_MOVE_CLEANUP_VECTOR);
787 } else {
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200788 apicd->prev_vector = 0;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200789 }
Thomas Gleixnerc1684f52015-12-31 16:30:51 +0000790 raw_spin_unlock(&vector_lock);
Jiang Liu74afab72014-10-27 16:12:00 +0800791}
792
Jiang Liuc6c20022015-04-14 10:30:02 +0800793void send_cleanup_vector(struct irq_cfg *cfg)
794{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200795 struct apic_chip_data *apicd;
Jiang Liu7f3262e2015-04-14 10:30:03 +0800796
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200797 apicd = container_of(cfg, struct apic_chip_data, hw_irq_cfg);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200798 if (apicd->move_in_progress)
799 __send_cleanup_vector(apicd);
Jiang Liuc6c20022015-04-14 10:30:02 +0800800}
801
Jiang Liu74afab72014-10-27 16:12:00 +0800802static void __irq_complete_move(struct irq_cfg *cfg, unsigned vector)
803{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200804 struct apic_chip_data *apicd;
Jiang Liu74afab72014-10-27 16:12:00 +0800805
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200806 apicd = container_of(cfg, struct apic_chip_data, hw_irq_cfg);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200807 if (likely(!apicd->move_in_progress))
Jiang Liu74afab72014-10-27 16:12:00 +0800808 return;
809
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200810 if (vector == apicd->vector && apicd->cpu == smp_processor_id())
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200811 __send_cleanup_vector(apicd);
Jiang Liu74afab72014-10-27 16:12:00 +0800812}
813
814void irq_complete_move(struct irq_cfg *cfg)
815{
816 __irq_complete_move(cfg, ~get_irq_regs()->orig_ax);
817}
818
Thomas Gleixner90a22822015-12-31 16:30:53 +0000819/*
Thomas Gleixner551adc62016-03-14 09:40:46 +0100820 * Called from fixup_irqs() with @desc->lock held and interrupts disabled.
Thomas Gleixner90a22822015-12-31 16:30:53 +0000821 */
822void irq_force_complete_move(struct irq_desc *desc)
Jiang Liu74afab72014-10-27 16:12:00 +0800823{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200824 struct apic_chip_data *apicd;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200825 struct irq_data *irqd;
826 unsigned int vector;
Jiang Liu74afab72014-10-27 16:12:00 +0800827
Mika Westerbergdb91aa72016-10-03 13:17:08 +0300828 /*
829 * The function is called for all descriptors regardless of which
830 * irqdomain they belong to. For example if an IRQ is provided by
831 * an irq_chip as part of a GPIO driver, the chip data for that
832 * descriptor is specific to the irq_chip in question.
833 *
834 * Check first that the chip_data is what we expect
835 * (apic_chip_data) before touching it any further.
836 */
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200837 irqd = irq_domain_get_irq_data(x86_vector_domain,
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200838 irq_desc_get_irq(desc));
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200839 if (!irqd)
Mika Westerbergdb91aa72016-10-03 13:17:08 +0300840 return;
841
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200842 raw_spin_lock(&vector_lock);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200843 apicd = apic_chip_data(irqd);
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200844 if (!apicd)
845 goto unlock;
Thomas Gleixner56d7d2f2015-12-31 16:30:52 +0000846
Thomas Gleixner56d7d2f2015-12-31 16:30:52 +0000847 /*
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200848 * If prev_vector is empty, no action required.
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200849 */
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200850 vector = apicd->prev_vector;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200851 if (!vector)
852 goto unlock;
853
854 /*
855 * This is tricky. If the cleanup of the old vector has not been
Thomas Gleixner98229aa2015-12-31 16:30:54 +0000856 * done yet, then the following setaffinity call will fail with
857 * -EBUSY. This can leave the interrupt in a stale state.
858 *
Thomas Gleixner551adc62016-03-14 09:40:46 +0100859 * All CPUs are stuck in stop machine with interrupts disabled so
860 * calling __irq_complete_move() would be completely pointless.
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200861 *
Thomas Gleixner551adc62016-03-14 09:40:46 +0100862 * 1) The interrupt is in move_in_progress state. That means that we
863 * have not seen an interrupt since the io_apic was reprogrammed to
864 * the new vector.
865 *
866 * 2) The interrupt has fired on the new vector, but the cleanup IPIs
867 * have not been processed yet.
868 */
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200869 if (apicd->move_in_progress) {
Thomas Gleixner551adc62016-03-14 09:40:46 +0100870 /*
871 * In theory there is a race:
872 *
873 * set_ioapic(new_vector) <-- Interrupt is raised before update
874 * is effective, i.e. it's raised on
875 * the old vector.
876 *
877 * So if the target cpu cannot handle that interrupt before
878 * the old vector is cleaned up, we get a spurious interrupt
879 * and in the worst case the ioapic irq line becomes stale.
880 *
881 * But in case of cpu hotplug this should be a non issue
882 * because if the affinity update happens right before all
883 * cpus rendevouz in stop machine, there is no way that the
884 * interrupt can be blocked on the target cpu because all cpus
885 * loops first with interrupts enabled in stop machine, so the
886 * old vector is not yet cleaned up when the interrupt fires.
887 *
888 * So the only way to run into this issue is if the delivery
889 * of the interrupt on the apic/system bus would be delayed
890 * beyond the point where the target cpu disables interrupts
891 * in stop machine. I doubt that it can happen, but at least
892 * there is a theroretical chance. Virtualization might be
893 * able to expose this, but AFAICT the IOAPIC emulation is not
894 * as stupid as the real hardware.
895 *
896 * Anyway, there is nothing we can do about that at this point
897 * w/o refactoring the whole fixup_irq() business completely.
898 * We print at least the irq number and the old vector number,
899 * so we have the necessary information when a problem in that
900 * area arises.
901 */
902 pr_warn("IRQ fixup: irq %d move in progress, old vector %d\n",
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200903 irqd->irq, vector);
Thomas Gleixner551adc62016-03-14 09:40:46 +0100904 }
Thomas Gleixner69cde002017-09-13 23:29:42 +0200905 free_moved_vector(apicd);
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200906unlock:
Thomas Gleixner56d7d2f2015-12-31 16:30:52 +0000907 raw_spin_unlock(&vector_lock);
Jiang Liu74afab72014-10-27 16:12:00 +0800908}
Jiang Liu74afab72014-10-27 16:12:00 +0800909#endif
910
Jiang Liu74afab72014-10-27 16:12:00 +0800911static void __init print_APIC_field(int base)
912{
913 int i;
914
915 printk(KERN_DEBUG);
916
917 for (i = 0; i < 8; i++)
918 pr_cont("%08x", apic_read(base + i*0x10));
919
920 pr_cont("\n");
921}
922
923static void __init print_local_APIC(void *dummy)
924{
925 unsigned int i, v, ver, maxlvt;
926 u64 icr;
927
Jiang Liu849d3562014-10-27 16:12:01 +0800928 pr_debug("printing local APIC contents on CPU#%d/%d:\n",
929 smp_processor_id(), hard_smp_processor_id());
Jiang Liu74afab72014-10-27 16:12:00 +0800930 v = apic_read(APIC_ID);
Jiang Liu849d3562014-10-27 16:12:01 +0800931 pr_info("... APIC ID: %08x (%01x)\n", v, read_apic_id());
Jiang Liu74afab72014-10-27 16:12:00 +0800932 v = apic_read(APIC_LVR);
Jiang Liu849d3562014-10-27 16:12:01 +0800933 pr_info("... APIC VERSION: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800934 ver = GET_APIC_VERSION(v);
935 maxlvt = lapic_get_maxlvt();
936
937 v = apic_read(APIC_TASKPRI);
Jiang Liu849d3562014-10-27 16:12:01 +0800938 pr_debug("... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
Jiang Liu74afab72014-10-27 16:12:00 +0800939
940 /* !82489DX */
941 if (APIC_INTEGRATED(ver)) {
942 if (!APIC_XAPIC(ver)) {
943 v = apic_read(APIC_ARBPRI);
Jiang Liu849d3562014-10-27 16:12:01 +0800944 pr_debug("... APIC ARBPRI: %08x (%02x)\n",
945 v, v & APIC_ARBPRI_MASK);
Jiang Liu74afab72014-10-27 16:12:00 +0800946 }
947 v = apic_read(APIC_PROCPRI);
Jiang Liu849d3562014-10-27 16:12:01 +0800948 pr_debug("... APIC PROCPRI: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800949 }
950
951 /*
952 * Remote read supported only in the 82489DX and local APIC for
953 * Pentium processors.
954 */
955 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
956 v = apic_read(APIC_RRR);
Jiang Liu849d3562014-10-27 16:12:01 +0800957 pr_debug("... APIC RRR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800958 }
959
960 v = apic_read(APIC_LDR);
Jiang Liu849d3562014-10-27 16:12:01 +0800961 pr_debug("... APIC LDR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800962 if (!x2apic_enabled()) {
963 v = apic_read(APIC_DFR);
Jiang Liu849d3562014-10-27 16:12:01 +0800964 pr_debug("... APIC DFR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800965 }
966 v = apic_read(APIC_SPIV);
Jiang Liu849d3562014-10-27 16:12:01 +0800967 pr_debug("... APIC SPIV: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800968
Jiang Liu849d3562014-10-27 16:12:01 +0800969 pr_debug("... APIC ISR field:\n");
Jiang Liu74afab72014-10-27 16:12:00 +0800970 print_APIC_field(APIC_ISR);
Jiang Liu849d3562014-10-27 16:12:01 +0800971 pr_debug("... APIC TMR field:\n");
Jiang Liu74afab72014-10-27 16:12:00 +0800972 print_APIC_field(APIC_TMR);
Jiang Liu849d3562014-10-27 16:12:01 +0800973 pr_debug("... APIC IRR field:\n");
Jiang Liu74afab72014-10-27 16:12:00 +0800974 print_APIC_field(APIC_IRR);
975
976 /* !82489DX */
977 if (APIC_INTEGRATED(ver)) {
978 /* Due to the Pentium erratum 3AP. */
979 if (maxlvt > 3)
980 apic_write(APIC_ESR, 0);
981
982 v = apic_read(APIC_ESR);
Jiang Liu849d3562014-10-27 16:12:01 +0800983 pr_debug("... APIC ESR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800984 }
985
986 icr = apic_icr_read();
Jiang Liu849d3562014-10-27 16:12:01 +0800987 pr_debug("... APIC ICR: %08x\n", (u32)icr);
988 pr_debug("... APIC ICR2: %08x\n", (u32)(icr >> 32));
Jiang Liu74afab72014-10-27 16:12:00 +0800989
990 v = apic_read(APIC_LVTT);
Jiang Liu849d3562014-10-27 16:12:01 +0800991 pr_debug("... APIC LVTT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800992
993 if (maxlvt > 3) {
994 /* PC is LVT#4. */
995 v = apic_read(APIC_LVTPC);
Jiang Liu849d3562014-10-27 16:12:01 +0800996 pr_debug("... APIC LVTPC: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +0800997 }
998 v = apic_read(APIC_LVT0);
Jiang Liu849d3562014-10-27 16:12:01 +0800999 pr_debug("... APIC LVT0: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001000 v = apic_read(APIC_LVT1);
Jiang Liu849d3562014-10-27 16:12:01 +08001001 pr_debug("... APIC LVT1: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001002
1003 if (maxlvt > 2) {
1004 /* ERR is LVT#3. */
1005 v = apic_read(APIC_LVTERR);
Jiang Liu849d3562014-10-27 16:12:01 +08001006 pr_debug("... APIC LVTERR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001007 }
1008
1009 v = apic_read(APIC_TMICT);
Jiang Liu849d3562014-10-27 16:12:01 +08001010 pr_debug("... APIC TMICT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001011 v = apic_read(APIC_TMCCT);
Jiang Liu849d3562014-10-27 16:12:01 +08001012 pr_debug("... APIC TMCCT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001013 v = apic_read(APIC_TDCR);
Jiang Liu849d3562014-10-27 16:12:01 +08001014 pr_debug("... APIC TDCR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001015
1016 if (boot_cpu_has(X86_FEATURE_EXTAPIC)) {
1017 v = apic_read(APIC_EFEAT);
1018 maxlvt = (v >> 16) & 0xff;
Jiang Liu849d3562014-10-27 16:12:01 +08001019 pr_debug("... APIC EFEAT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001020 v = apic_read(APIC_ECTRL);
Jiang Liu849d3562014-10-27 16:12:01 +08001021 pr_debug("... APIC ECTRL: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001022 for (i = 0; i < maxlvt; i++) {
1023 v = apic_read(APIC_EILVTn(i));
Jiang Liu849d3562014-10-27 16:12:01 +08001024 pr_debug("... APIC EILVT%d: %08x\n", i, v);
Jiang Liu74afab72014-10-27 16:12:00 +08001025 }
1026 }
1027 pr_cont("\n");
1028}
1029
1030static void __init print_local_APICs(int maxcpu)
1031{
1032 int cpu;
1033
1034 if (!maxcpu)
1035 return;
1036
1037 preempt_disable();
1038 for_each_online_cpu(cpu) {
1039 if (cpu >= maxcpu)
1040 break;
1041 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
1042 }
1043 preempt_enable();
1044}
1045
1046static void __init print_PIC(void)
1047{
1048 unsigned int v;
1049 unsigned long flags;
1050
1051 if (!nr_legacy_irqs())
1052 return;
1053
Jiang Liu849d3562014-10-27 16:12:01 +08001054 pr_debug("\nprinting PIC contents\n");
Jiang Liu74afab72014-10-27 16:12:00 +08001055
1056 raw_spin_lock_irqsave(&i8259A_lock, flags);
1057
1058 v = inb(0xa1) << 8 | inb(0x21);
Jiang Liu849d3562014-10-27 16:12:01 +08001059 pr_debug("... PIC IMR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001060
1061 v = inb(0xa0) << 8 | inb(0x20);
Jiang Liu849d3562014-10-27 16:12:01 +08001062 pr_debug("... PIC IRR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001063
1064 outb(0x0b, 0xa0);
1065 outb(0x0b, 0x20);
1066 v = inb(0xa0) << 8 | inb(0x20);
1067 outb(0x0a, 0xa0);
1068 outb(0x0a, 0x20);
1069
1070 raw_spin_unlock_irqrestore(&i8259A_lock, flags);
1071
Jiang Liu849d3562014-10-27 16:12:01 +08001072 pr_debug("... PIC ISR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001073
1074 v = inb(0x4d1) << 8 | inb(0x4d0);
Jiang Liu849d3562014-10-27 16:12:01 +08001075 pr_debug("... PIC ELCR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001076}
1077
1078static int show_lapic __initdata = 1;
1079static __init int setup_show_lapic(char *arg)
1080{
1081 int num = -1;
1082
1083 if (strcmp(arg, "all") == 0) {
1084 show_lapic = CONFIG_NR_CPUS;
1085 } else {
1086 get_option(&arg, &num);
1087 if (num >= 0)
1088 show_lapic = num;
1089 }
1090
1091 return 1;
1092}
1093__setup("show_lapic=", setup_show_lapic);
1094
1095static int __init print_ICs(void)
1096{
1097 if (apic_verbosity == APIC_QUIET)
1098 return 0;
1099
1100 print_PIC();
1101
1102 /* don't print out if apic is not there */
Borislav Petkov93984fb2016-04-04 22:25:00 +02001103 if (!boot_cpu_has(X86_FEATURE_APIC) && !apic_from_smp_config())
Jiang Liu74afab72014-10-27 16:12:00 +08001104 return 0;
1105
1106 print_local_APICs(show_lapic);
1107 print_IO_APICs();
1108
1109 return 0;
1110}
1111
1112late_initcall(print_ICs);