blob: a75de0792942a95e48b3d4caca7b3723a6725597 [file] [log] [blame]
Jiang Liu74afab72014-10-27 16:12:00 +08001/*
Bjorn Helgaasfd2fa6c2017-11-22 16:13:37 -06002 * Local APIC related interfaces to support IOAPIC, MSI, etc.
Jiang Liu74afab72014-10-27 16:12:00 +08003 *
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,
Thomas Gleixner4900be82017-09-13 23:29:51 +020036 is_managed : 1,
37 can_reserve : 1,
38 has_reserved : 1;
Jiang Liu7f3262e2015-04-14 10:30:03 +080039};
40
Jiang Liub5dc8e62015-04-13 14:11:24 +080041struct irq_domain *x86_vector_domain;
Jake Oshinsc8f3e512015-12-10 17:52:59 +000042EXPORT_SYMBOL_GPL(x86_vector_domain);
Jiang Liu74afab72014-10-27 16:12:00 +080043static DEFINE_RAW_SPINLOCK(vector_lock);
Thomas Gleixner69cde002017-09-13 23:29:42 +020044static cpumask_var_t vector_searchmask;
Jiang Liub5dc8e62015-04-13 14:11:24 +080045static struct irq_chip lapic_controller;
Thomas Gleixner0fa115d2017-09-13 23:29:38 +020046static struct irq_matrix *vector_matrix;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +020047#ifdef CONFIG_SMP
48static DEFINE_PER_CPU(struct hlist_head, cleanup_list);
49#endif
Jiang Liu74afab72014-10-27 16:12:00 +080050
51void lock_vector_lock(void)
52{
53 /* Used to the online set of cpus does not change
54 * during assign_irq_vector.
55 */
56 raw_spin_lock(&vector_lock);
57}
58
59void unlock_vector_lock(void)
60{
61 raw_spin_unlock(&vector_lock);
62}
63
Thomas Gleixner99a14822017-09-13 23:29:36 +020064void init_irq_alloc_info(struct irq_alloc_info *info,
65 const struct cpumask *mask)
66{
67 memset(info, 0, sizeof(*info));
68 info->mask = mask;
69}
70
71void copy_irq_alloc_info(struct irq_alloc_info *dst, struct irq_alloc_info *src)
72{
73 if (src)
74 *dst = *src;
75 else
76 memset(dst, 0, sizeof(*dst));
77}
78
Thomas Gleixner86ba6552017-09-13 23:29:30 +020079static struct apic_chip_data *apic_chip_data(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +080080{
Thomas Gleixner86ba6552017-09-13 23:29:30 +020081 if (!irqd)
Jiang Liub5dc8e62015-04-13 14:11:24 +080082 return NULL;
83
Thomas Gleixner86ba6552017-09-13 23:29:30 +020084 while (irqd->parent_data)
85 irqd = irqd->parent_data;
Jiang Liub5dc8e62015-04-13 14:11:24 +080086
Thomas Gleixner86ba6552017-09-13 23:29:30 +020087 return irqd->chip_data;
Jiang Liu74afab72014-10-27 16:12:00 +080088}
89
Thomas Gleixner86ba6552017-09-13 23:29:30 +020090struct irq_cfg *irqd_cfg(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +080091{
Thomas Gleixner86ba6552017-09-13 23:29:30 +020092 struct apic_chip_data *apicd = apic_chip_data(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +080093
Thomas Gleixnerba224fe2017-09-13 23:29:45 +020094 return apicd ? &apicd->hw_irq_cfg : NULL;
Jiang Liu7f3262e2015-04-14 10:30:03 +080095}
Jake Oshinsc8f3e512015-12-10 17:52:59 +000096EXPORT_SYMBOL_GPL(irqd_cfg);
Jiang Liu7f3262e2015-04-14 10:30:03 +080097
98struct irq_cfg *irq_cfg(unsigned int irq)
99{
100 return irqd_cfg(irq_get_irq_data(irq));
101}
102
103static struct apic_chip_data *alloc_apic_chip_data(int node)
104{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200105 struct apic_chip_data *apicd;
Jiang Liu7f3262e2015-04-14 10:30:03 +0800106
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200107 apicd = kzalloc_node(sizeof(*apicd), GFP_KERNEL, node);
Thomas Gleixner69cde002017-09-13 23:29:42 +0200108 if (apicd)
109 INIT_HLIST_NODE(&apicd->clist);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200110 return apicd;
Jiang Liu74afab72014-10-27 16:12:00 +0800111}
112
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200113static void free_apic_chip_data(struct apic_chip_data *apicd)
Jiang Liu74afab72014-10-27 16:12:00 +0800114{
Thomas Gleixner69cde002017-09-13 23:29:42 +0200115 kfree(apicd);
Jiang Liu74afab72014-10-27 16:12:00 +0800116}
117
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200118static void apic_update_irq_cfg(struct irq_data *irqd, unsigned int vector,
119 unsigned int cpu)
Jiang Liu74afab72014-10-27 16:12:00 +0800120{
Thomas Gleixner69cde002017-09-13 23:29:42 +0200121 struct apic_chip_data *apicd = apic_chip_data(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800122
Thomas Gleixner69cde002017-09-13 23:29:42 +0200123 lockdep_assert_held(&vector_lock);
Jiang Liu74afab72014-10-27 16:12:00 +0800124
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200125 apicd->hw_irq_cfg.vector = vector;
126 apicd->hw_irq_cfg.dest_apicid = apic->calc_dest_apicid(cpu);
127 irq_data_update_effective_affinity(irqd, cpumask_of(cpu));
128 trace_vector_config(irqd->irq, vector, cpu,
129 apicd->hw_irq_cfg.dest_apicid);
Thomas Gleixner69cde002017-09-13 23:29:42 +0200130}
Jiang Liu74afab72014-10-27 16:12:00 +0800131
Thomas Gleixner69cde002017-09-13 23:29:42 +0200132static void apic_update_vector(struct irq_data *irqd, unsigned int newvec,
133 unsigned int newcpu)
134{
135 struct apic_chip_data *apicd = apic_chip_data(irqd);
136 struct irq_desc *desc = irq_data_to_desc(irqd);
Thomas Gleixnere84cf6a2018-02-22 12:08:06 +0100137 bool managed = irqd_affinity_is_managed(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800138
Thomas Gleixner69cde002017-09-13 23:29:42 +0200139 lockdep_assert_held(&vector_lock);
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000140
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200141 trace_vector_update(irqd->irq, newvec, newcpu, apicd->vector,
Thomas Gleixner69cde002017-09-13 23:29:42 +0200142 apicd->cpu);
Jiang Liu74afab72014-10-27 16:12:00 +0800143
Thomas Gleixnere84cf6a2018-02-22 12:08:06 +0100144 /*
145 * If there is no vector associated or if the associated vector is
146 * the shutdown vector, which is associated to make PCI/MSI
147 * shutdown mode work, then there is nothing to release. Clear out
148 * prev_vector for this and the offlined target case.
149 */
150 apicd->prev_vector = 0;
151 if (!apicd->vector || apicd->vector == MANAGED_IRQ_SHUTDOWN_VECTOR)
152 goto setnew;
153 /*
154 * If the target CPU of the previous vector is online, then mark
155 * the vector as move in progress and store it for cleanup when the
156 * first interrupt on the new vector arrives. If the target CPU is
157 * offline then the regular release mechanism via the cleanup
158 * vector is not possible and the vector can be immediately freed
159 * in the underlying matrix allocator.
160 */
161 if (cpu_online(apicd->cpu)) {
Thomas Gleixner69cde002017-09-13 23:29:42 +0200162 apicd->move_in_progress = true;
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200163 apicd->prev_vector = apicd->vector;
Thomas Gleixner69cde002017-09-13 23:29:42 +0200164 apicd->prev_cpu = apicd->cpu;
165 } else {
Thomas Gleixnere84cf6a2018-02-22 12:08:06 +0100166 irq_matrix_free(vector_matrix, apicd->cpu, apicd->vector,
167 managed);
Jiang Liu74afab72014-10-27 16:12:00 +0800168 }
Jiang Liu74afab72014-10-27 16:12:00 +0800169
Thomas Gleixnere84cf6a2018-02-22 12:08:06 +0100170setnew:
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200171 apicd->vector = newvec;
Thomas Gleixner69cde002017-09-13 23:29:42 +0200172 apicd->cpu = newcpu;
173 BUG_ON(!IS_ERR_OR_NULL(per_cpu(vector_irq, newcpu)[newvec]));
174 per_cpu(vector_irq, newcpu)[newvec] = desc;
175}
176
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200177static void vector_assign_managed_shutdown(struct irq_data *irqd)
178{
179 unsigned int cpu = cpumask_first(cpu_online_mask);
180
181 apic_update_irq_cfg(irqd, MANAGED_IRQ_SHUTDOWN_VECTOR, cpu);
182}
183
184static int reserve_managed_vector(struct irq_data *irqd)
185{
186 const struct cpumask *affmsk = irq_data_get_affinity_mask(irqd);
187 struct apic_chip_data *apicd = apic_chip_data(irqd);
188 unsigned long flags;
189 int ret;
190
191 raw_spin_lock_irqsave(&vector_lock, flags);
192 apicd->is_managed = true;
193 ret = irq_matrix_reserve_managed(vector_matrix, affmsk);
194 raw_spin_unlock_irqrestore(&vector_lock, flags);
195 trace_vector_reserve_managed(irqd->irq, ret);
196 return ret;
197}
198
Thomas Gleixner4900be82017-09-13 23:29:51 +0200199static void reserve_irq_vector_locked(struct irq_data *irqd)
200{
201 struct apic_chip_data *apicd = apic_chip_data(irqd);
202
203 irq_matrix_reserve(vector_matrix);
204 apicd->can_reserve = true;
205 apicd->has_reserved = true;
Thomas Gleixner945f50a2017-12-29 16:57:00 +0100206 irqd_set_can_reserve(irqd);
Thomas Gleixner4900be82017-09-13 23:29:51 +0200207 trace_vector_reserve(irqd->irq, 0);
208 vector_assign_managed_shutdown(irqd);
209}
210
211static int reserve_irq_vector(struct irq_data *irqd)
212{
213 unsigned long flags;
214
215 raw_spin_lock_irqsave(&vector_lock, flags);
216 reserve_irq_vector_locked(irqd);
217 raw_spin_unlock_irqrestore(&vector_lock, flags);
218 return 0;
219}
220
Dou Liyang27733972018-05-11 16:09:56 +0800221static int
222assign_vector_locked(struct irq_data *irqd, const struct cpumask *dest)
Thomas Gleixner69cde002017-09-13 23:29:42 +0200223{
224 struct apic_chip_data *apicd = apic_chip_data(irqd);
Thomas Gleixner4900be82017-09-13 23:29:51 +0200225 bool resvd = apicd->has_reserved;
Thomas Gleixner69cde002017-09-13 23:29:42 +0200226 unsigned int cpu = apicd->cpu;
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200227 int vector = apicd->vector;
228
229 lockdep_assert_held(&vector_lock);
Thomas Gleixner69cde002017-09-13 23:29:42 +0200230
Thomas Gleixner847667e2015-12-31 16:30:50 +0000231 /*
Thomas Gleixner69cde002017-09-13 23:29:42 +0200232 * If the current target CPU is online and in the new requested
233 * affinity mask, there is no point in moving the interrupt from
234 * one CPU to another.
Thomas Gleixner847667e2015-12-31 16:30:50 +0000235 */
Thomas Gleixner69cde002017-09-13 23:29:42 +0200236 if (vector && cpu_online(cpu) && cpumask_test_cpu(cpu, dest))
237 return 0;
238
Thomas Gleixner4900be82017-09-13 23:29:51 +0200239 vector = irq_matrix_alloc(vector_matrix, dest, resvd, &cpu);
Thomas Gleixner4900be82017-09-13 23:29:51 +0200240 trace_vector_alloc(irqd->irq, vector, resvd, vector);
Thomas Gleixner69cde002017-09-13 23:29:42 +0200241 if (vector < 0)
242 return vector;
Dou Liyang27733972018-05-11 16:09:56 +0800243 apic_update_vector(irqd, vector, cpu);
244 apic_update_irq_cfg(irqd, vector, cpu);
Thomas Gleixner69cde002017-09-13 23:29:42 +0200245
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000246 return 0;
Jiang Liu74afab72014-10-27 16:12:00 +0800247}
248
Thomas Gleixner69cde002017-09-13 23:29:42 +0200249static int assign_irq_vector(struct irq_data *irqd, const struct cpumask *dest)
Jiang Liu74afab72014-10-27 16:12:00 +0800250{
Jiang Liu74afab72014-10-27 16:12:00 +0800251 unsigned long flags;
Thomas Gleixner69cde002017-09-13 23:29:42 +0200252 int ret;
Jiang Liu74afab72014-10-27 16:12:00 +0800253
254 raw_spin_lock_irqsave(&vector_lock, flags);
Thomas Gleixner69cde002017-09-13 23:29:42 +0200255 cpumask_and(vector_searchmask, dest, cpu_online_mask);
256 ret = assign_vector_locked(irqd, vector_searchmask);
Jiang Liu74afab72014-10-27 16:12:00 +0800257 raw_spin_unlock_irqrestore(&vector_lock, flags);
Thomas Gleixner69cde002017-09-13 23:29:42 +0200258 return ret;
Jiang Liu74afab72014-10-27 16:12:00 +0800259}
260
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200261static int assign_irq_vector_any_locked(struct irq_data *irqd)
Jiang Liu486ca532015-05-07 10:53:56 +0800262{
Thomas Gleixnerd6ffc6a2017-09-13 23:29:54 +0200263 /* Get the affinity mask - either irq_default_affinity or (user) set */
264 const struct cpumask *affmsk = irq_data_get_affinity_mask(irqd);
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200265 int node = irq_data_get_node(irqd);
266
Thomas Gleixnerd6ffc6a2017-09-13 23:29:54 +0200267 if (node == NUMA_NO_NODE)
268 goto all;
269 /* Try the intersection of @affmsk and node mask */
270 cpumask_and(vector_searchmask, cpumask_of_node(node), affmsk);
271 if (!assign_vector_locked(irqd, vector_searchmask))
272 return 0;
273 /* Try the node mask */
274 if (!assign_vector_locked(irqd, cpumask_of_node(node)))
275 return 0;
276all:
277 /* Try the full affinity mask */
278 cpumask_and(vector_searchmask, affmsk, cpu_online_mask);
279 if (!assign_vector_locked(irqd, vector_searchmask))
280 return 0;
281 /* Try the full online mask */
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200282 return assign_vector_locked(irqd, cpu_online_mask);
283}
284
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200285static int
286assign_irq_vector_policy(struct irq_data *irqd, struct irq_alloc_info *info)
287{
288 if (irqd_affinity_is_managed(irqd))
289 return reserve_managed_vector(irqd);
Thomas Gleixner258d86e2017-09-13 23:29:35 +0200290 if (info->mask)
Thomas Gleixner69cde002017-09-13 23:29:42 +0200291 return assign_irq_vector(irqd, info->mask);
Thomas Gleixner464d1232017-09-13 23:29:52 +0200292 /*
293 * Make only a global reservation with no guarantee. A real vector
294 * is associated at activation time.
295 */
Thomas Gleixner4900be82017-09-13 23:29:51 +0200296 return reserve_irq_vector(irqd);
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200297}
298
299static int
300assign_managed_vector(struct irq_data *irqd, const struct cpumask *dest)
301{
302 const struct cpumask *affmsk = irq_data_get_affinity_mask(irqd);
303 struct apic_chip_data *apicd = apic_chip_data(irqd);
304 int vector, cpu;
305
306 cpumask_and(vector_searchmask, vector_searchmask, affmsk);
307 cpu = cpumask_first(vector_searchmask);
308 if (cpu >= nr_cpu_ids)
309 return -EINVAL;
310 /* set_affinity might call here for nothing */
311 if (apicd->vector && cpumask_test_cpu(apicd->cpu, vector_searchmask))
Jiang Liu486ca532015-05-07 10:53:56 +0800312 return 0;
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200313 vector = irq_matrix_alloc_managed(vector_matrix, cpu);
314 trace_vector_alloc_managed(irqd->irq, vector, vector);
315 if (vector < 0)
316 return vector;
317 apic_update_vector(irqd, vector, cpu);
318 apic_update_irq_cfg(irqd, vector, cpu);
319 return 0;
Jiang Liu486ca532015-05-07 10:53:56 +0800320}
321
Thomas Gleixner69cde002017-09-13 23:29:42 +0200322static void clear_irq_vector(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +0800323{
Thomas Gleixner69cde002017-09-13 23:29:42 +0200324 struct apic_chip_data *apicd = apic_chip_data(irqd);
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200325 bool managed = irqd_affinity_is_managed(irqd);
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200326 unsigned int vector = apicd->vector;
Jiang Liu74afab72014-10-27 16:12:00 +0800327
Thomas Gleixner69cde002017-09-13 23:29:42 +0200328 lockdep_assert_held(&vector_lock);
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200329
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200330 if (!vector)
Keith Busch1bdb8972016-04-27 14:22:32 -0600331 return;
Jiang Liu74afab72014-10-27 16:12:00 +0800332
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200333 trace_vector_clear(irqd->irq, vector, apicd->cpu, apicd->prev_vector,
Thomas Gleixner69cde002017-09-13 23:29:42 +0200334 apicd->prev_cpu);
335
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200336 per_cpu(vector_irq, apicd->cpu)[vector] = VECTOR_UNUSED;
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200337 irq_matrix_free(vector_matrix, apicd->cpu, vector, managed);
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200338 apicd->vector = 0;
Jiang Liu74afab72014-10-27 16:12:00 +0800339
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200340 /* Clean up move in progress */
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200341 vector = apicd->prev_vector;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200342 if (!vector)
Jiang Liu74afab72014-10-27 16:12:00 +0800343 return;
Jiang Liu74afab72014-10-27 16:12:00 +0800344
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200345 per_cpu(vector_irq, apicd->prev_cpu)[vector] = VECTOR_UNUSED;
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200346 irq_matrix_free(vector_matrix, apicd->prev_cpu, vector, managed);
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200347 apicd->prev_vector = 0;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200348 apicd->move_in_progress = 0;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200349 hlist_del_init(&apicd->clist);
Jiang Liu74afab72014-10-27 16:12:00 +0800350}
351
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200352static void x86_vector_deactivate(struct irq_domain *dom, struct irq_data *irqd)
353{
354 struct apic_chip_data *apicd = apic_chip_data(irqd);
355 unsigned long flags;
356
357 trace_vector_deactivate(irqd->irq, apicd->is_managed,
Thomas Gleixner4900be82017-09-13 23:29:51 +0200358 apicd->can_reserve, false);
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200359
Thomas Gleixner4900be82017-09-13 23:29:51 +0200360 /* Regular fixed assigned interrupt */
361 if (!apicd->is_managed && !apicd->can_reserve)
362 return;
363 /* If the interrupt has a global reservation, nothing to do */
364 if (apicd->has_reserved)
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200365 return;
366
367 raw_spin_lock_irqsave(&vector_lock, flags);
368 clear_irq_vector(irqd);
Thomas Gleixner4900be82017-09-13 23:29:51 +0200369 if (apicd->can_reserve)
370 reserve_irq_vector_locked(irqd);
371 else
372 vector_assign_managed_shutdown(irqd);
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200373 raw_spin_unlock_irqrestore(&vector_lock, flags);
374}
375
Thomas Gleixner4900be82017-09-13 23:29:51 +0200376static int activate_reserved(struct irq_data *irqd)
377{
378 struct apic_chip_data *apicd = apic_chip_data(irqd);
379 int ret;
380
381 ret = assign_irq_vector_any_locked(irqd);
Thomas Gleixnerbc976232017-12-29 10:47:22 +0100382 if (!ret) {
Thomas Gleixner4900be82017-09-13 23:29:51 +0200383 apicd->has_reserved = false;
Thomas Gleixnerbc976232017-12-29 10:47:22 +0100384 /*
385 * Core might have disabled reservation mode after
386 * allocating the irq descriptor. Ideally this should
387 * happen before allocation time, but that would require
388 * completely convoluted ways of transporting that
389 * information.
390 */
391 if (!irqd_can_reserve(irqd))
392 apicd->can_reserve = false;
393 }
Thomas Gleixner4900be82017-09-13 23:29:51 +0200394 return ret;
395}
396
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200397static int activate_managed(struct irq_data *irqd)
398{
399 const struct cpumask *dest = irq_data_get_affinity_mask(irqd);
400 int ret;
401
402 cpumask_and(vector_searchmask, dest, cpu_online_mask);
403 if (WARN_ON_ONCE(cpumask_empty(vector_searchmask))) {
404 /* Something in the core code broke! Survive gracefully */
405 pr_err("Managed startup for irq %u, but no CPU\n", irqd->irq);
406 return EINVAL;
407 }
408
409 ret = assign_managed_vector(irqd, vector_searchmask);
410 /*
411 * This should not happen. The vector reservation got buggered. Handle
412 * it gracefully.
413 */
414 if (WARN_ON_ONCE(ret < 0)) {
415 pr_err("Managed startup irq %u, no vector available\n",
416 irqd->irq);
417 }
418 return ret;
419}
420
421static int x86_vector_activate(struct irq_domain *dom, struct irq_data *irqd,
Thomas Gleixner702cb0a2017-12-29 16:59:06 +0100422 bool reserve)
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200423{
424 struct apic_chip_data *apicd = apic_chip_data(irqd);
425 unsigned long flags;
426 int ret = 0;
427
428 trace_vector_activate(irqd->irq, apicd->is_managed,
Thomas Gleixner702cb0a2017-12-29 16:59:06 +0100429 apicd->can_reserve, reserve);
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200430
Thomas Gleixner4900be82017-09-13 23:29:51 +0200431 /* Nothing to do for fixed assigned vectors */
432 if (!apicd->can_reserve && !apicd->is_managed)
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200433 return 0;
434
435 raw_spin_lock_irqsave(&vector_lock, flags);
Thomas Gleixner702cb0a2017-12-29 16:59:06 +0100436 if (reserve || irqd_is_managed_and_shutdown(irqd))
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200437 vector_assign_managed_shutdown(irqd);
Thomas Gleixner4900be82017-09-13 23:29:51 +0200438 else if (apicd->is_managed)
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200439 ret = activate_managed(irqd);
Thomas Gleixner4900be82017-09-13 23:29:51 +0200440 else if (apicd->has_reserved)
441 ret = activate_reserved(irqd);
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200442 raw_spin_unlock_irqrestore(&vector_lock, flags);
443 return ret;
444}
445
446static void vector_free_reserved_and_managed(struct irq_data *irqd)
447{
448 const struct cpumask *dest = irq_data_get_affinity_mask(irqd);
449 struct apic_chip_data *apicd = apic_chip_data(irqd);
450
Thomas Gleixner4900be82017-09-13 23:29:51 +0200451 trace_vector_teardown(irqd->irq, apicd->is_managed,
452 apicd->has_reserved);
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200453
Thomas Gleixner4900be82017-09-13 23:29:51 +0200454 if (apicd->has_reserved)
455 irq_matrix_remove_reserved(vector_matrix);
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200456 if (apicd->is_managed)
457 irq_matrix_remove_managed(vector_matrix, dest);
458}
459
Jiang Liub5dc8e62015-04-13 14:11:24 +0800460static void x86_vector_free_irqs(struct irq_domain *domain,
461 unsigned int virq, unsigned int nr_irqs)
462{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200463 struct apic_chip_data *apicd;
464 struct irq_data *irqd;
Jiang Liu111abeb2015-12-31 16:30:44 +0000465 unsigned long flags;
Jiang Liub5dc8e62015-04-13 14:11:24 +0800466 int i;
467
468 for (i = 0; i < nr_irqs; i++) {
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200469 irqd = irq_domain_get_irq_data(x86_vector_domain, virq + i);
470 if (irqd && irqd->chip_data) {
Jiang Liu111abeb2015-12-31 16:30:44 +0000471 raw_spin_lock_irqsave(&vector_lock, flags);
Thomas Gleixner69cde002017-09-13 23:29:42 +0200472 clear_irq_vector(irqd);
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200473 vector_free_reserved_and_managed(irqd);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200474 apicd = irqd->chip_data;
475 irq_domain_reset_irq_data(irqd);
Jiang Liu111abeb2015-12-31 16:30:44 +0000476 raw_spin_unlock_irqrestore(&vector_lock, flags);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200477 free_apic_chip_data(apicd);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800478 }
479 }
480}
481
Thomas Gleixner464d1232017-09-13 23:29:52 +0200482static bool vector_configure_legacy(unsigned int virq, struct irq_data *irqd,
483 struct apic_chip_data *apicd)
484{
485 unsigned long flags;
486 bool realloc = false;
487
488 apicd->vector = ISA_IRQ_VECTOR(virq);
489 apicd->cpu = 0;
490
491 raw_spin_lock_irqsave(&vector_lock, flags);
492 /*
493 * If the interrupt is activated, then it must stay at this vector
494 * position. That's usually the timer interrupt (0).
495 */
496 if (irqd_is_activated(irqd)) {
497 trace_vector_setup(virq, true, 0);
498 apic_update_irq_cfg(irqd, apicd->vector, apicd->cpu);
499 } else {
500 /* Release the vector */
501 apicd->can_reserve = true;
Thomas Gleixner945f50a2017-12-29 16:57:00 +0100502 irqd_set_can_reserve(irqd);
Thomas Gleixner464d1232017-09-13 23:29:52 +0200503 clear_irq_vector(irqd);
504 realloc = true;
505 }
506 raw_spin_unlock_irqrestore(&vector_lock, flags);
507 return realloc;
508}
509
Jiang Liub5dc8e62015-04-13 14:11:24 +0800510static int x86_vector_alloc_irqs(struct irq_domain *domain, unsigned int virq,
511 unsigned int nr_irqs, void *arg)
512{
513 struct irq_alloc_info *info = arg;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200514 struct apic_chip_data *apicd;
515 struct irq_data *irqd;
Jiang Liu5f2dbbc2015-06-01 16:05:14 +0800516 int i, err, node;
Jiang Liub5dc8e62015-04-13 14:11:24 +0800517
518 if (disable_apic)
519 return -ENXIO;
520
521 /* Currently vector allocator can't guarantee contiguous allocations */
522 if ((info->flags & X86_IRQ_ALLOC_CONTIGUOUS_VECTORS) && nr_irqs > 1)
523 return -ENOSYS;
524
Jiang Liub5dc8e62015-04-13 14:11:24 +0800525 for (i = 0; i < nr_irqs; i++) {
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200526 irqd = irq_domain_get_irq_data(domain, virq + i);
527 BUG_ON(!irqd);
528 node = irq_data_get_node(irqd);
Thomas Gleixner4ef76eb2017-09-13 23:29:34 +0200529 WARN_ON_ONCE(irqd->chip_data);
530 apicd = alloc_apic_chip_data(node);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200531 if (!apicd) {
Jiang Liub5dc8e62015-04-13 14:11:24 +0800532 err = -ENOMEM;
533 goto error;
534 }
535
Thomas Gleixner69cde002017-09-13 23:29:42 +0200536 apicd->irq = virq + i;
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200537 irqd->chip = &lapic_controller;
538 irqd->chip_data = apicd;
539 irqd->hwirq = virq + i;
540 irqd_set_single_target(irqd);
Thomas Gleixner4ef76eb2017-09-13 23:29:34 +0200541 /*
Thomas Gleixner69cde002017-09-13 23:29:42 +0200542 * Legacy vectors are already assigned when the IOAPIC
543 * takes them over. They stay on the same vector. This is
544 * required for check_timer() to work correctly as it might
545 * switch back to legacy mode. Only update the hardware
546 * config.
Thomas Gleixner4ef76eb2017-09-13 23:29:34 +0200547 */
548 if (info->flags & X86_IRQ_ALLOC_LEGACY) {
Thomas Gleixner464d1232017-09-13 23:29:52 +0200549 if (!vector_configure_legacy(virq + i, irqd, apicd))
550 continue;
Thomas Gleixner4ef76eb2017-09-13 23:29:34 +0200551 }
552
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200553 err = assign_irq_vector_policy(irqd, info);
Thomas Gleixner69cde002017-09-13 23:29:42 +0200554 trace_vector_setup(virq + i, false, err);
Thomas Gleixner45d55e72018-01-16 12:20:18 +0100555 if (err) {
556 irqd->chip_data = NULL;
557 free_apic_chip_data(apicd);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800558 goto error;
Thomas Gleixner45d55e72018-01-16 12:20:18 +0100559 }
Jiang Liub5dc8e62015-04-13 14:11:24 +0800560 }
561
562 return 0;
563
564error:
Thomas Gleixner45d55e72018-01-16 12:20:18 +0100565 x86_vector_free_irqs(domain, virq, i);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800566 return err;
567}
568
Thomas Gleixner65d7ed52017-09-13 23:29:39 +0200569#ifdef CONFIG_GENERIC_IRQ_DEBUGFS
Colin Ian Kingd553d032017-12-06 17:33:58 +0000570static void x86_vector_debug_show(struct seq_file *m, struct irq_domain *d,
571 struct irq_data *irqd, int ind)
Thomas Gleixner65d7ed52017-09-13 23:29:39 +0200572{
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200573 unsigned int cpu, vector, prev_cpu, prev_vector;
Thomas Gleixner65d7ed52017-09-13 23:29:39 +0200574 struct apic_chip_data *apicd;
575 unsigned long flags;
576 int irq;
577
578 if (!irqd) {
579 irq_matrix_debug_show(m, vector_matrix, ind);
580 return;
581 }
582
583 irq = irqd->irq;
584 if (irq < nr_legacy_irqs() && !test_bit(irq, &io_apic_irqs)) {
585 seq_printf(m, "%*sVector: %5d\n", ind, "", ISA_IRQ_VECTOR(irq));
586 seq_printf(m, "%*sTarget: Legacy PIC all CPUs\n", ind, "");
587 return;
588 }
589
590 apicd = irqd->chip_data;
591 if (!apicd) {
592 seq_printf(m, "%*sVector: Not assigned\n", ind, "");
593 return;
594 }
595
596 raw_spin_lock_irqsave(&vector_lock, flags);
597 cpu = apicd->cpu;
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200598 vector = apicd->vector;
Thomas Gleixner65d7ed52017-09-13 23:29:39 +0200599 prev_cpu = apicd->prev_cpu;
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200600 prev_vector = apicd->prev_vector;
Thomas Gleixner65d7ed52017-09-13 23:29:39 +0200601 raw_spin_unlock_irqrestore(&vector_lock, flags);
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200602 seq_printf(m, "%*sVector: %5u\n", ind, "", vector);
Thomas Gleixner65d7ed52017-09-13 23:29:39 +0200603 seq_printf(m, "%*sTarget: %5u\n", ind, "", cpu);
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200604 if (prev_vector) {
605 seq_printf(m, "%*sPrevious vector: %5u\n", ind, "", prev_vector);
Thomas Gleixner65d7ed52017-09-13 23:29:39 +0200606 seq_printf(m, "%*sPrevious target: %5u\n", ind, "", prev_cpu);
607 }
608}
609#endif
610
Thomas Gleixnereb18cf52015-05-05 11:10:11 +0200611static const struct irq_domain_ops x86_vector_domain_ops = {
Thomas Gleixner65d7ed52017-09-13 23:29:39 +0200612 .alloc = x86_vector_alloc_irqs,
613 .free = x86_vector_free_irqs,
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200614 .activate = x86_vector_activate,
615 .deactivate = x86_vector_deactivate,
Thomas Gleixner65d7ed52017-09-13 23:29:39 +0200616#ifdef CONFIG_GENERIC_IRQ_DEBUGFS
617 .debug_show = x86_vector_debug_show,
618#endif
Jiang Liub5dc8e62015-04-13 14:11:24 +0800619};
620
Jiang Liu11d686e2014-10-27 16:12:05 +0800621int __init arch_probe_nr_irqs(void)
622{
623 int nr;
624
625 if (nr_irqs > (NR_VECTORS * nr_cpu_ids))
626 nr_irqs = NR_VECTORS * nr_cpu_ids;
627
628 nr = (gsi_top + nr_legacy_irqs()) + 8 * nr_cpu_ids;
Bjorn Helgaasfd2fa6c2017-11-22 16:13:37 -0600629#if defined(CONFIG_PCI_MSI)
Jiang Liu11d686e2014-10-27 16:12:05 +0800630 /*
631 * for MSI and HT dyn irq
632 */
633 if (gsi_top <= NR_IRQS_LEGACY)
634 nr += 8 * nr_cpu_ids;
635 else
636 nr += gsi_top * 16;
637#endif
638 if (nr < nr_irqs)
639 nr_irqs = nr;
640
Vitaly Kuznetsov8c058b02015-11-03 10:40:14 +0100641 /*
642 * We don't know if PIC is present at this point so we need to do
643 * probe() to get the right number of legacy IRQs.
644 */
645 return legacy_pic->probe();
Jiang Liu11d686e2014-10-27 16:12:05 +0800646}
647
Thomas Gleixner0fa115d2017-09-13 23:29:38 +0200648void lapic_assign_legacy_vector(unsigned int irq, bool replace)
649{
650 /*
651 * Use assign system here so it wont get accounted as allocated
652 * and moveable in the cpu hotplug check and it prevents managed
653 * irq reservation from touching it.
654 */
655 irq_matrix_assign_system(vector_matrix, ISA_IRQ_VECTOR(irq), replace);
656}
657
658void __init lapic_assign_system_vectors(void)
659{
660 unsigned int i, vector = 0;
661
662 for_each_set_bit_from(vector, system_vectors, NR_VECTORS)
663 irq_matrix_assign_system(vector_matrix, vector, false);
664
665 if (nr_legacy_irqs() > 1)
666 lapic_assign_legacy_vector(PIC_CASCADE_IR, false);
667
668 /* System vectors are reserved, online it */
669 irq_matrix_online(vector_matrix);
670
671 /* Mark the preallocated legacy interrupts */
672 for (i = 0; i < nr_legacy_irqs(); i++) {
673 if (i != PIC_CASCADE_IR)
674 irq_matrix_assign(vector_matrix, ISA_IRQ_VECTOR(i));
675 }
676}
677
Jiang Liu11d686e2014-10-27 16:12:05 +0800678int __init arch_early_irq_init(void)
679{
Thomas Gleixner9d35f852017-06-20 01:37:06 +0200680 struct fwnode_handle *fn;
681
Thomas Gleixner9d35f852017-06-20 01:37:06 +0200682 fn = irq_domain_alloc_named_fwnode("VECTOR");
683 BUG_ON(!fn);
684 x86_vector_domain = irq_domain_create_tree(fn, &x86_vector_domain_ops,
685 NULL);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800686 BUG_ON(x86_vector_domain == NULL);
Thomas Gleixner9d35f852017-06-20 01:37:06 +0200687 irq_domain_free_fwnode(fn);
Jiang Liub5dc8e62015-04-13 14:11:24 +0800688 irq_set_default_host(x86_vector_domain);
689
Jiang Liu52f518a2015-04-13 14:11:35 +0800690 arch_init_msi_domain(x86_vector_domain);
691
Thomas Gleixner3716fd22015-12-31 16:30:48 +0000692 BUG_ON(!alloc_cpumask_var(&vector_searchmask, GFP_KERNEL));
Jiang Liuf7fa7ae2015-04-14 10:30:10 +0800693
Thomas Gleixner0fa115d2017-09-13 23:29:38 +0200694 /*
695 * Allocate the vector matrix allocator data structure and limit the
696 * search area.
697 */
698 vector_matrix = irq_alloc_matrix(NR_VECTORS, FIRST_EXTERNAL_VECTOR,
699 FIRST_SYSTEM_VECTOR);
700 BUG_ON(!vector_matrix);
701
Jiang Liu11d686e2014-10-27 16:12:05 +0800702 return arch_early_ioapic_init();
703}
704
Thomas Gleixnerba801642017-09-13 23:29:44 +0200705#ifdef CONFIG_SMP
Jiang Liu74afab72014-10-27 16:12:00 +0800706
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200707static struct irq_desc *__setup_vector_irq(int vector)
708{
709 int isairq = vector - ISA_IRQ_VECTOR(0);
710
711 /* Check whether the irq is in the legacy space */
712 if (isairq < 0 || isairq >= nr_legacy_irqs())
713 return VECTOR_UNUSED;
714 /* Check whether the irq is handled by the IOAPIC */
715 if (test_bit(isairq, &io_apic_irqs))
716 return VECTOR_UNUSED;
717 return irq_to_desc(isairq);
Jiang Liu74afab72014-10-27 16:12:00 +0800718}
719
Thomas Gleixner0fa115d2017-09-13 23:29:38 +0200720/* Online the local APIC infrastructure and initialize the vectors */
721void lapic_online(void)
Jiang Liu74afab72014-10-27 16:12:00 +0800722{
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200723 unsigned int vector;
Jiang Liu74afab72014-10-27 16:12:00 +0800724
Thomas Gleixner5a3f75e2015-07-05 17:12:32 +0000725 lockdep_assert_held(&vector_lock);
Thomas Gleixner0fa115d2017-09-13 23:29:38 +0200726
727 /* Online the vector matrix array for this CPU */
728 irq_matrix_online(vector_matrix);
729
Jiang Liu74afab72014-10-27 16:12:00 +0800730 /*
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200731 * The interrupt affinity logic never targets interrupts to offline
732 * CPUs. The exception are the legacy PIC interrupts. In general
733 * they are only targeted to CPU0, but depending on the platform
734 * they can be distributed to any online CPU in hardware. The
735 * kernel has no influence on that. So all active legacy vectors
736 * must be installed on all CPUs. All non legacy interrupts can be
737 * cleared.
Jiang Liu74afab72014-10-27 16:12:00 +0800738 */
Thomas Gleixnerf0cc6cc2017-09-13 23:29:29 +0200739 for (vector = 0; vector < NR_VECTORS; vector++)
740 this_cpu_write(vector_irq[vector], __setup_vector_irq(vector));
Jiang Liu74afab72014-10-27 16:12:00 +0800741}
742
Thomas Gleixner0fa115d2017-09-13 23:29:38 +0200743void lapic_offline(void)
744{
745 lock_vector_lock();
746 irq_matrix_offline(vector_matrix);
747 unlock_vector_lock();
748}
749
Thomas Gleixnerba801642017-09-13 23:29:44 +0200750static int apic_set_affinity(struct irq_data *irqd,
751 const struct cpumask *dest, bool force)
752{
Thomas Gleixner02edee12017-10-12 11:05:28 +0200753 struct apic_chip_data *apicd = apic_chip_data(irqd);
Thomas Gleixnerba801642017-09-13 23:29:44 +0200754 int err;
755
Thomas Gleixner02edee12017-10-12 11:05:28 +0200756 /*
757 * Core code can call here for inactive interrupts. For inactive
758 * interrupts which use managed or reservation mode there is no
759 * point in going through the vector assignment right now as the
760 * activation will assign a vector which fits the destination
761 * cpumask. Let the core code store the destination mask and be
762 * done with it.
763 */
764 if (!irqd_is_activated(irqd) &&
765 (apicd->is_managed || apicd->can_reserve))
766 return IRQ_SET_MASK_OK;
767
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200768 raw_spin_lock(&vector_lock);
769 cpumask_and(vector_searchmask, dest, cpu_online_mask);
770 if (irqd_affinity_is_managed(irqd))
771 err = assign_managed_vector(irqd, vector_searchmask);
772 else
773 err = assign_vector_locked(irqd, vector_searchmask);
774 raw_spin_unlock(&vector_lock);
Thomas Gleixnerba801642017-09-13 23:29:44 +0200775 return err ? err : IRQ_SET_MASK_OK;
776}
777
778#else
779# define apic_set_affinity NULL
780#endif
781
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200782static int apic_retrigger_irq(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +0800783{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200784 struct apic_chip_data *apicd = apic_chip_data(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800785 unsigned long flags;
Jiang Liu74afab72014-10-27 16:12:00 +0800786
787 raw_spin_lock_irqsave(&vector_lock, flags);
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200788 apic->send_IPI(apicd->cpu, apicd->vector);
Jiang Liu74afab72014-10-27 16:12:00 +0800789 raw_spin_unlock_irqrestore(&vector_lock, flags);
790
791 return 1;
792}
793
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200794void apic_ack_edge(struct irq_data *irqd)
Jiang Liu74afab72014-10-27 16:12:00 +0800795{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200796 irq_complete_move(irqd_cfg(irqd));
797 irq_move_irq(irqd);
Jiang Liu74afab72014-10-27 16:12:00 +0800798 ack_APIC_irq();
799}
800
Jiang Liub5dc8e62015-04-13 14:11:24 +0800801static struct irq_chip lapic_controller = {
Thomas Gleixner8947dfb2017-06-20 01:37:01 +0200802 .name = "APIC",
Jiang Liub5dc8e62015-04-13 14:11:24 +0800803 .irq_ack = apic_ack_edge,
Jiang Liu68f9f442015-04-14 10:30:01 +0800804 .irq_set_affinity = apic_set_affinity,
Jiang Liub5dc8e62015-04-13 14:11:24 +0800805 .irq_retrigger = apic_retrigger_irq,
806};
807
Jiang Liu74afab72014-10-27 16:12:00 +0800808#ifdef CONFIG_SMP
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200809
Thomas Gleixner69cde002017-09-13 23:29:42 +0200810static void free_moved_vector(struct apic_chip_data *apicd)
811{
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200812 unsigned int vector = apicd->prev_vector;
Thomas Gleixner69cde002017-09-13 23:29:42 +0200813 unsigned int cpu = apicd->prev_cpu;
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200814 bool managed = apicd->is_managed;
Thomas Gleixner69cde002017-09-13 23:29:42 +0200815
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200816 /*
817 * This should never happen. Managed interrupts are not
818 * migrated except on CPU down, which does not involve the
819 * cleanup vector. But try to keep the accounting correct
820 * nevertheless.
821 */
822 WARN_ON_ONCE(managed);
823
Thomas Gleixner0696d052017-10-16 16:16:19 +0200824 trace_vector_free_moved(apicd->irq, cpu, vector, managed);
Thomas Gleixner2db1f952017-09-13 23:29:50 +0200825 irq_matrix_free(vector_matrix, cpu, vector, managed);
Thomas Gleixner0696d052017-10-16 16:16:19 +0200826 per_cpu(vector_irq, cpu)[vector] = VECTOR_UNUSED;
Thomas Gleixner69cde002017-09-13 23:29:42 +0200827 hlist_del_init(&apicd->clist);
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200828 apicd->prev_vector = 0;
Thomas Gleixner69cde002017-09-13 23:29:42 +0200829 apicd->move_in_progress = 0;
830}
831
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200832asmlinkage __visible void __irq_entry smp_irq_move_cleanup_interrupt(void)
833{
834 struct hlist_head *clhead = this_cpu_ptr(&cleanup_list);
835 struct apic_chip_data *apicd;
836 struct hlist_node *tmp;
837
838 entering_ack_irq();
839 /* Prevent vectors vanishing under us */
840 raw_spin_lock(&vector_lock);
841
842 hlist_for_each_entry_safe(apicd, tmp, clhead, clist) {
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200843 unsigned int irr, vector = apicd->prev_vector;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200844
845 /*
846 * Paranoia: Check if the vector that needs to be cleaned
847 * up is registered at the APICs IRR. If so, then this is
848 * not the best time to clean it up. Clean it up in the
849 * next attempt by sending another IRQ_MOVE_CLEANUP_VECTOR
850 * to this CPU. IRQ_MOVE_CLEANUP_VECTOR is the lowest
851 * priority external vector, so on return from this
852 * interrupt the device interrupt will happen first.
853 */
854 irr = apic_read(APIC_IRR + (vector / 32 * 0x10));
855 if (irr & (1U << (vector % 32))) {
856 apic->send_IPI_self(IRQ_MOVE_CLEANUP_VECTOR);
857 continue;
858 }
Thomas Gleixner69cde002017-09-13 23:29:42 +0200859 free_moved_vector(apicd);
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200860 }
861
862 raw_spin_unlock(&vector_lock);
863 exiting_irq();
864}
865
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200866static void __send_cleanup_vector(struct apic_chip_data *apicd)
Jiang Liu74afab72014-10-27 16:12:00 +0800867{
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200868 unsigned int cpu;
869
Thomas Gleixnerc1684f52015-12-31 16:30:51 +0000870 raw_spin_lock(&vector_lock);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200871 apicd->move_in_progress = 0;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200872 cpu = apicd->prev_cpu;
873 if (cpu_online(cpu)) {
874 hlist_add_head(&apicd->clist, per_cpu_ptr(&cleanup_list, cpu));
875 apic->send_IPI(cpu, IRQ_MOVE_CLEANUP_VECTOR);
876 } else {
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200877 apicd->prev_vector = 0;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200878 }
Thomas Gleixnerc1684f52015-12-31 16:30:51 +0000879 raw_spin_unlock(&vector_lock);
Jiang Liu74afab72014-10-27 16:12:00 +0800880}
881
Jiang Liuc6c20022015-04-14 10:30:02 +0800882void send_cleanup_vector(struct irq_cfg *cfg)
883{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200884 struct apic_chip_data *apicd;
Jiang Liu7f3262e2015-04-14 10:30:03 +0800885
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200886 apicd = container_of(cfg, struct apic_chip_data, hw_irq_cfg);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200887 if (apicd->move_in_progress)
888 __send_cleanup_vector(apicd);
Jiang Liuc6c20022015-04-14 10:30:02 +0800889}
890
Jiang Liu74afab72014-10-27 16:12:00 +0800891static void __irq_complete_move(struct irq_cfg *cfg, unsigned vector)
892{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200893 struct apic_chip_data *apicd;
Jiang Liu74afab72014-10-27 16:12:00 +0800894
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200895 apicd = container_of(cfg, struct apic_chip_data, hw_irq_cfg);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200896 if (likely(!apicd->move_in_progress))
Jiang Liu74afab72014-10-27 16:12:00 +0800897 return;
898
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200899 if (vector == apicd->vector && apicd->cpu == smp_processor_id())
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200900 __send_cleanup_vector(apicd);
Jiang Liu74afab72014-10-27 16:12:00 +0800901}
902
903void irq_complete_move(struct irq_cfg *cfg)
904{
905 __irq_complete_move(cfg, ~get_irq_regs()->orig_ax);
906}
907
Thomas Gleixner90a22822015-12-31 16:30:53 +0000908/*
Thomas Gleixner551adc62016-03-14 09:40:46 +0100909 * Called from fixup_irqs() with @desc->lock held and interrupts disabled.
Thomas Gleixner90a22822015-12-31 16:30:53 +0000910 */
911void irq_force_complete_move(struct irq_desc *desc)
Jiang Liu74afab72014-10-27 16:12:00 +0800912{
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200913 struct apic_chip_data *apicd;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200914 struct irq_data *irqd;
915 unsigned int vector;
Jiang Liu74afab72014-10-27 16:12:00 +0800916
Mika Westerbergdb91aa72016-10-03 13:17:08 +0300917 /*
918 * The function is called for all descriptors regardless of which
919 * irqdomain they belong to. For example if an IRQ is provided by
920 * an irq_chip as part of a GPIO driver, the chip data for that
921 * descriptor is specific to the irq_chip in question.
922 *
923 * Check first that the chip_data is what we expect
924 * (apic_chip_data) before touching it any further.
925 */
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200926 irqd = irq_domain_get_irq_data(x86_vector_domain,
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200927 irq_desc_get_irq(desc));
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200928 if (!irqd)
Mika Westerbergdb91aa72016-10-03 13:17:08 +0300929 return;
930
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200931 raw_spin_lock(&vector_lock);
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200932 apicd = apic_chip_data(irqd);
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200933 if (!apicd)
934 goto unlock;
Thomas Gleixner56d7d2f2015-12-31 16:30:52 +0000935
Thomas Gleixner56d7d2f2015-12-31 16:30:52 +0000936 /*
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200937 * If prev_vector is empty, no action required.
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200938 */
Thomas Gleixnerba224fe2017-09-13 23:29:45 +0200939 vector = apicd->prev_vector;
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200940 if (!vector)
941 goto unlock;
942
943 /*
944 * This is tricky. If the cleanup of the old vector has not been
Thomas Gleixner98229aa2015-12-31 16:30:54 +0000945 * done yet, then the following setaffinity call will fail with
946 * -EBUSY. This can leave the interrupt in a stale state.
947 *
Thomas Gleixner551adc62016-03-14 09:40:46 +0100948 * All CPUs are stuck in stop machine with interrupts disabled so
949 * calling __irq_complete_move() would be completely pointless.
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200950 *
Thomas Gleixner551adc62016-03-14 09:40:46 +0100951 * 1) The interrupt is in move_in_progress state. That means that we
952 * have not seen an interrupt since the io_apic was reprogrammed to
953 * the new vector.
954 *
955 * 2) The interrupt has fired on the new vector, but the cleanup IPIs
956 * have not been processed yet.
957 */
Thomas Gleixner86ba6552017-09-13 23:29:30 +0200958 if (apicd->move_in_progress) {
Thomas Gleixner551adc62016-03-14 09:40:46 +0100959 /*
960 * In theory there is a race:
961 *
962 * set_ioapic(new_vector) <-- Interrupt is raised before update
963 * is effective, i.e. it's raised on
964 * the old vector.
965 *
966 * So if the target cpu cannot handle that interrupt before
967 * the old vector is cleaned up, we get a spurious interrupt
968 * and in the worst case the ioapic irq line becomes stale.
969 *
970 * But in case of cpu hotplug this should be a non issue
971 * because if the affinity update happens right before all
972 * cpus rendevouz in stop machine, there is no way that the
973 * interrupt can be blocked on the target cpu because all cpus
974 * loops first with interrupts enabled in stop machine, so the
975 * old vector is not yet cleaned up when the interrupt fires.
976 *
977 * So the only way to run into this issue is if the delivery
978 * of the interrupt on the apic/system bus would be delayed
979 * beyond the point where the target cpu disables interrupts
980 * in stop machine. I doubt that it can happen, but at least
981 * there is a theroretical chance. Virtualization might be
982 * able to expose this, but AFAICT the IOAPIC emulation is not
983 * as stupid as the real hardware.
984 *
985 * Anyway, there is nothing we can do about that at this point
986 * w/o refactoring the whole fixup_irq() business completely.
987 * We print at least the irq number and the old vector number,
988 * so we have the necessary information when a problem in that
989 * area arises.
990 */
991 pr_warn("IRQ fixup: irq %d move in progress, old vector %d\n",
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200992 irqd->irq, vector);
Thomas Gleixner551adc62016-03-14 09:40:46 +0100993 }
Thomas Gleixner69cde002017-09-13 23:29:42 +0200994 free_moved_vector(apicd);
Thomas Gleixnerdccfe312017-09-13 23:29:32 +0200995unlock:
Thomas Gleixner56d7d2f2015-12-31 16:30:52 +0000996 raw_spin_unlock(&vector_lock);
Jiang Liu74afab72014-10-27 16:12:00 +0800997}
Thomas Gleixner2cffad72017-09-13 23:29:53 +0200998
999#ifdef CONFIG_HOTPLUG_CPU
1000/*
1001 * Note, this is not accurate accounting, but at least good enough to
1002 * prevent that the actual interrupt move will run out of vectors.
1003 */
1004int lapic_can_unplug_cpu(void)
1005{
1006 unsigned int rsvd, avl, tomove, cpu = smp_processor_id();
1007 int ret = 0;
1008
1009 raw_spin_lock(&vector_lock);
1010 tomove = irq_matrix_allocated(vector_matrix);
1011 avl = irq_matrix_available(vector_matrix, true);
1012 if (avl < tomove) {
1013 pr_warn("CPU %u has %u vectors, %u available. Cannot disable CPU\n",
1014 cpu, tomove, avl);
1015 ret = -ENOSPC;
1016 goto out;
1017 }
1018 rsvd = irq_matrix_reserved(vector_matrix);
1019 if (avl < rsvd) {
1020 pr_warn("Reserved vectors %u > available %u. IRQ request may fail\n",
1021 rsvd, avl);
1022 }
1023out:
1024 raw_spin_unlock(&vector_lock);
1025 return ret;
1026}
1027#endif /* HOTPLUG_CPU */
1028#endif /* SMP */
Jiang Liu74afab72014-10-27 16:12:00 +08001029
Jiang Liu74afab72014-10-27 16:12:00 +08001030static void __init print_APIC_field(int base)
1031{
1032 int i;
1033
1034 printk(KERN_DEBUG);
1035
1036 for (i = 0; i < 8; i++)
1037 pr_cont("%08x", apic_read(base + i*0x10));
1038
1039 pr_cont("\n");
1040}
1041
1042static void __init print_local_APIC(void *dummy)
1043{
1044 unsigned int i, v, ver, maxlvt;
1045 u64 icr;
1046
Jiang Liu849d3562014-10-27 16:12:01 +08001047 pr_debug("printing local APIC contents on CPU#%d/%d:\n",
1048 smp_processor_id(), hard_smp_processor_id());
Jiang Liu74afab72014-10-27 16:12:00 +08001049 v = apic_read(APIC_ID);
Jiang Liu849d3562014-10-27 16:12:01 +08001050 pr_info("... APIC ID: %08x (%01x)\n", v, read_apic_id());
Jiang Liu74afab72014-10-27 16:12:00 +08001051 v = apic_read(APIC_LVR);
Jiang Liu849d3562014-10-27 16:12:01 +08001052 pr_info("... APIC VERSION: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001053 ver = GET_APIC_VERSION(v);
1054 maxlvt = lapic_get_maxlvt();
1055
1056 v = apic_read(APIC_TASKPRI);
Jiang Liu849d3562014-10-27 16:12:01 +08001057 pr_debug("... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
Jiang Liu74afab72014-10-27 16:12:00 +08001058
1059 /* !82489DX */
1060 if (APIC_INTEGRATED(ver)) {
1061 if (!APIC_XAPIC(ver)) {
1062 v = apic_read(APIC_ARBPRI);
Jiang Liu849d3562014-10-27 16:12:01 +08001063 pr_debug("... APIC ARBPRI: %08x (%02x)\n",
1064 v, v & APIC_ARBPRI_MASK);
Jiang Liu74afab72014-10-27 16:12:00 +08001065 }
1066 v = apic_read(APIC_PROCPRI);
Jiang Liu849d3562014-10-27 16:12:01 +08001067 pr_debug("... APIC PROCPRI: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001068 }
1069
1070 /*
1071 * Remote read supported only in the 82489DX and local APIC for
1072 * Pentium processors.
1073 */
1074 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
1075 v = apic_read(APIC_RRR);
Jiang Liu849d3562014-10-27 16:12:01 +08001076 pr_debug("... APIC RRR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001077 }
1078
1079 v = apic_read(APIC_LDR);
Jiang Liu849d3562014-10-27 16:12:01 +08001080 pr_debug("... APIC LDR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001081 if (!x2apic_enabled()) {
1082 v = apic_read(APIC_DFR);
Jiang Liu849d3562014-10-27 16:12:01 +08001083 pr_debug("... APIC DFR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001084 }
1085 v = apic_read(APIC_SPIV);
Jiang Liu849d3562014-10-27 16:12:01 +08001086 pr_debug("... APIC SPIV: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001087
Jiang Liu849d3562014-10-27 16:12:01 +08001088 pr_debug("... APIC ISR field:\n");
Jiang Liu74afab72014-10-27 16:12:00 +08001089 print_APIC_field(APIC_ISR);
Jiang Liu849d3562014-10-27 16:12:01 +08001090 pr_debug("... APIC TMR field:\n");
Jiang Liu74afab72014-10-27 16:12:00 +08001091 print_APIC_field(APIC_TMR);
Jiang Liu849d3562014-10-27 16:12:01 +08001092 pr_debug("... APIC IRR field:\n");
Jiang Liu74afab72014-10-27 16:12:00 +08001093 print_APIC_field(APIC_IRR);
1094
1095 /* !82489DX */
1096 if (APIC_INTEGRATED(ver)) {
1097 /* Due to the Pentium erratum 3AP. */
1098 if (maxlvt > 3)
1099 apic_write(APIC_ESR, 0);
1100
1101 v = apic_read(APIC_ESR);
Jiang Liu849d3562014-10-27 16:12:01 +08001102 pr_debug("... APIC ESR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001103 }
1104
1105 icr = apic_icr_read();
Jiang Liu849d3562014-10-27 16:12:01 +08001106 pr_debug("... APIC ICR: %08x\n", (u32)icr);
1107 pr_debug("... APIC ICR2: %08x\n", (u32)(icr >> 32));
Jiang Liu74afab72014-10-27 16:12:00 +08001108
1109 v = apic_read(APIC_LVTT);
Jiang Liu849d3562014-10-27 16:12:01 +08001110 pr_debug("... APIC LVTT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001111
1112 if (maxlvt > 3) {
1113 /* PC is LVT#4. */
1114 v = apic_read(APIC_LVTPC);
Jiang Liu849d3562014-10-27 16:12:01 +08001115 pr_debug("... APIC LVTPC: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001116 }
1117 v = apic_read(APIC_LVT0);
Jiang Liu849d3562014-10-27 16:12:01 +08001118 pr_debug("... APIC LVT0: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001119 v = apic_read(APIC_LVT1);
Jiang Liu849d3562014-10-27 16:12:01 +08001120 pr_debug("... APIC LVT1: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001121
1122 if (maxlvt > 2) {
1123 /* ERR is LVT#3. */
1124 v = apic_read(APIC_LVTERR);
Jiang Liu849d3562014-10-27 16:12:01 +08001125 pr_debug("... APIC LVTERR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001126 }
1127
1128 v = apic_read(APIC_TMICT);
Jiang Liu849d3562014-10-27 16:12:01 +08001129 pr_debug("... APIC TMICT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001130 v = apic_read(APIC_TMCCT);
Jiang Liu849d3562014-10-27 16:12:01 +08001131 pr_debug("... APIC TMCCT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001132 v = apic_read(APIC_TDCR);
Jiang Liu849d3562014-10-27 16:12:01 +08001133 pr_debug("... APIC TDCR: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001134
1135 if (boot_cpu_has(X86_FEATURE_EXTAPIC)) {
1136 v = apic_read(APIC_EFEAT);
1137 maxlvt = (v >> 16) & 0xff;
Jiang Liu849d3562014-10-27 16:12:01 +08001138 pr_debug("... APIC EFEAT: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001139 v = apic_read(APIC_ECTRL);
Jiang Liu849d3562014-10-27 16:12:01 +08001140 pr_debug("... APIC ECTRL: %08x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001141 for (i = 0; i < maxlvt; i++) {
1142 v = apic_read(APIC_EILVTn(i));
Jiang Liu849d3562014-10-27 16:12:01 +08001143 pr_debug("... APIC EILVT%d: %08x\n", i, v);
Jiang Liu74afab72014-10-27 16:12:00 +08001144 }
1145 }
1146 pr_cont("\n");
1147}
1148
1149static void __init print_local_APICs(int maxcpu)
1150{
1151 int cpu;
1152
1153 if (!maxcpu)
1154 return;
1155
1156 preempt_disable();
1157 for_each_online_cpu(cpu) {
1158 if (cpu >= maxcpu)
1159 break;
1160 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
1161 }
1162 preempt_enable();
1163}
1164
1165static void __init print_PIC(void)
1166{
1167 unsigned int v;
1168 unsigned long flags;
1169
1170 if (!nr_legacy_irqs())
1171 return;
1172
Jiang Liu849d3562014-10-27 16:12:01 +08001173 pr_debug("\nprinting PIC contents\n");
Jiang Liu74afab72014-10-27 16:12:00 +08001174
1175 raw_spin_lock_irqsave(&i8259A_lock, flags);
1176
1177 v = inb(0xa1) << 8 | inb(0x21);
Jiang Liu849d3562014-10-27 16:12:01 +08001178 pr_debug("... PIC IMR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001179
1180 v = inb(0xa0) << 8 | inb(0x20);
Jiang Liu849d3562014-10-27 16:12:01 +08001181 pr_debug("... PIC IRR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001182
1183 outb(0x0b, 0xa0);
1184 outb(0x0b, 0x20);
1185 v = inb(0xa0) << 8 | inb(0x20);
1186 outb(0x0a, 0xa0);
1187 outb(0x0a, 0x20);
1188
1189 raw_spin_unlock_irqrestore(&i8259A_lock, flags);
1190
Jiang Liu849d3562014-10-27 16:12:01 +08001191 pr_debug("... PIC ISR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001192
1193 v = inb(0x4d1) << 8 | inb(0x4d0);
Jiang Liu849d3562014-10-27 16:12:01 +08001194 pr_debug("... PIC ELCR: %04x\n", v);
Jiang Liu74afab72014-10-27 16:12:00 +08001195}
1196
1197static int show_lapic __initdata = 1;
1198static __init int setup_show_lapic(char *arg)
1199{
1200 int num = -1;
1201
1202 if (strcmp(arg, "all") == 0) {
1203 show_lapic = CONFIG_NR_CPUS;
1204 } else {
1205 get_option(&arg, &num);
1206 if (num >= 0)
1207 show_lapic = num;
1208 }
1209
1210 return 1;
1211}
1212__setup("show_lapic=", setup_show_lapic);
1213
1214static int __init print_ICs(void)
1215{
1216 if (apic_verbosity == APIC_QUIET)
1217 return 0;
1218
1219 print_PIC();
1220
1221 /* don't print out if apic is not there */
Borislav Petkov93984fb2016-04-04 22:25:00 +02001222 if (!boot_cpu_has(X86_FEATURE_APIC) && !apic_from_smp_config())
Jiang Liu74afab72014-10-27 16:12:00 +08001223 return 0;
1224
1225 print_local_APICs(show_lapic);
1226 print_IO_APICs();
1227
1228 return 0;
1229}
1230
1231late_initcall(print_ICs);