Merge tag 'ras_for_4.2' of git://git.kernel.org/pub/scm/linux/kernel/git/ras/ras...
[deliverable/linux.git] / arch / x86 / kernel / irq.c
1 /*
2 * Common interrupt code for 32 and 64 bit
3 */
4 #include <linux/cpu.h>
5 #include <linux/interrupt.h>
6 #include <linux/kernel_stat.h>
7 #include <linux/of.h>
8 #include <linux/seq_file.h>
9 #include <linux/smp.h>
10 #include <linux/ftrace.h>
11 #include <linux/delay.h>
12 #include <linux/export.h>
13
14 #include <asm/apic.h>
15 #include <asm/io_apic.h>
16 #include <asm/irq.h>
17 #include <asm/idle.h>
18 #include <asm/mce.h>
19 #include <asm/hw_irq.h>
20 #include <asm/desc.h>
21
22 #define CREATE_TRACE_POINTS
23 #include <asm/trace/irq_vectors.h>
24
25 atomic_t irq_err_count;
26
27 /* Function pointer for generic interrupt vector handling */
28 void (*x86_platform_ipi_callback)(void) = NULL;
29
30 /*
31 * 'what should we do if we get a hw irq event on an illegal vector'.
32 * each architecture has to answer this themselves.
33 */
34 void ack_bad_irq(unsigned int irq)
35 {
36 if (printk_ratelimit())
37 pr_err("unexpected IRQ trap at vector %02x\n", irq);
38
39 /*
40 * Currently unexpected vectors happen only on SMP and APIC.
41 * We _must_ ack these because every local APIC has only N
42 * irq slots per priority level, and a 'hanging, unacked' IRQ
43 * holds up an irq slot - in excessive cases (when multiple
44 * unexpected vectors occur) that might lock up the APIC
45 * completely.
46 * But only ack when the APIC is enabled -AK
47 */
48 ack_APIC_irq();
49 }
50
51 #define irq_stats(x) (&per_cpu(irq_stat, x))
52 /*
53 * /proc/interrupts printing for arch specific interrupts
54 */
55 int arch_show_interrupts(struct seq_file *p, int prec)
56 {
57 int j;
58
59 seq_printf(p, "%*s: ", prec, "NMI");
60 for_each_online_cpu(j)
61 seq_printf(p, "%10u ", irq_stats(j)->__nmi_count);
62 seq_puts(p, " Non-maskable interrupts\n");
63 #ifdef CONFIG_X86_LOCAL_APIC
64 seq_printf(p, "%*s: ", prec, "LOC");
65 for_each_online_cpu(j)
66 seq_printf(p, "%10u ", irq_stats(j)->apic_timer_irqs);
67 seq_puts(p, " Local timer interrupts\n");
68
69 seq_printf(p, "%*s: ", prec, "SPU");
70 for_each_online_cpu(j)
71 seq_printf(p, "%10u ", irq_stats(j)->irq_spurious_count);
72 seq_puts(p, " Spurious interrupts\n");
73 seq_printf(p, "%*s: ", prec, "PMI");
74 for_each_online_cpu(j)
75 seq_printf(p, "%10u ", irq_stats(j)->apic_perf_irqs);
76 seq_puts(p, " Performance monitoring interrupts\n");
77 seq_printf(p, "%*s: ", prec, "IWI");
78 for_each_online_cpu(j)
79 seq_printf(p, "%10u ", irq_stats(j)->apic_irq_work_irqs);
80 seq_puts(p, " IRQ work interrupts\n");
81 seq_printf(p, "%*s: ", prec, "RTR");
82 for_each_online_cpu(j)
83 seq_printf(p, "%10u ", irq_stats(j)->icr_read_retry_count);
84 seq_puts(p, " APIC ICR read retries\n");
85 #endif
86 if (x86_platform_ipi_callback) {
87 seq_printf(p, "%*s: ", prec, "PLT");
88 for_each_online_cpu(j)
89 seq_printf(p, "%10u ", irq_stats(j)->x86_platform_ipis);
90 seq_puts(p, " Platform interrupts\n");
91 }
92 #ifdef CONFIG_SMP
93 seq_printf(p, "%*s: ", prec, "RES");
94 for_each_online_cpu(j)
95 seq_printf(p, "%10u ", irq_stats(j)->irq_resched_count);
96 seq_puts(p, " Rescheduling interrupts\n");
97 seq_printf(p, "%*s: ", prec, "CAL");
98 for_each_online_cpu(j)
99 seq_printf(p, "%10u ", irq_stats(j)->irq_call_count -
100 irq_stats(j)->irq_tlb_count);
101 seq_puts(p, " Function call interrupts\n");
102 seq_printf(p, "%*s: ", prec, "TLB");
103 for_each_online_cpu(j)
104 seq_printf(p, "%10u ", irq_stats(j)->irq_tlb_count);
105 seq_puts(p, " TLB shootdowns\n");
106 #endif
107 #ifdef CONFIG_X86_THERMAL_VECTOR
108 seq_printf(p, "%*s: ", prec, "TRM");
109 for_each_online_cpu(j)
110 seq_printf(p, "%10u ", irq_stats(j)->irq_thermal_count);
111 seq_puts(p, " Thermal event interrupts\n");
112 #endif
113 #ifdef CONFIG_X86_MCE_THRESHOLD
114 seq_printf(p, "%*s: ", prec, "THR");
115 for_each_online_cpu(j)
116 seq_printf(p, "%10u ", irq_stats(j)->irq_threshold_count);
117 seq_puts(p, " Threshold APIC interrupts\n");
118 #endif
119 #ifdef CONFIG_X86_MCE_AMD
120 seq_printf(p, "%*s: ", prec, "DFR");
121 for_each_online_cpu(j)
122 seq_printf(p, "%10u ", irq_stats(j)->irq_deferred_error_count);
123 seq_puts(p, " Deferred Error APIC interrupts\n");
124 #endif
125 #ifdef CONFIG_X86_MCE
126 seq_printf(p, "%*s: ", prec, "MCE");
127 for_each_online_cpu(j)
128 seq_printf(p, "%10u ", per_cpu(mce_exception_count, j));
129 seq_puts(p, " Machine check exceptions\n");
130 seq_printf(p, "%*s: ", prec, "MCP");
131 for_each_online_cpu(j)
132 seq_printf(p, "%10u ", per_cpu(mce_poll_count, j));
133 seq_puts(p, " Machine check polls\n");
134 #endif
135 #if IS_ENABLED(CONFIG_HYPERV) || defined(CONFIG_XEN)
136 seq_printf(p, "%*s: ", prec, "HYP");
137 for_each_online_cpu(j)
138 seq_printf(p, "%10u ", irq_stats(j)->irq_hv_callback_count);
139 seq_puts(p, " Hypervisor callback interrupts\n");
140 #endif
141 seq_printf(p, "%*s: %10u\n", prec, "ERR", atomic_read(&irq_err_count));
142 #if defined(CONFIG_X86_IO_APIC)
143 seq_printf(p, "%*s: %10u\n", prec, "MIS", atomic_read(&irq_mis_count));
144 #endif
145 return 0;
146 }
147
148 /*
149 * /proc/stat helpers
150 */
151 u64 arch_irq_stat_cpu(unsigned int cpu)
152 {
153 u64 sum = irq_stats(cpu)->__nmi_count;
154
155 #ifdef CONFIG_X86_LOCAL_APIC
156 sum += irq_stats(cpu)->apic_timer_irqs;
157 sum += irq_stats(cpu)->irq_spurious_count;
158 sum += irq_stats(cpu)->apic_perf_irqs;
159 sum += irq_stats(cpu)->apic_irq_work_irqs;
160 sum += irq_stats(cpu)->icr_read_retry_count;
161 #endif
162 if (x86_platform_ipi_callback)
163 sum += irq_stats(cpu)->x86_platform_ipis;
164 #ifdef CONFIG_SMP
165 sum += irq_stats(cpu)->irq_resched_count;
166 sum += irq_stats(cpu)->irq_call_count;
167 #endif
168 #ifdef CONFIG_X86_THERMAL_VECTOR
169 sum += irq_stats(cpu)->irq_thermal_count;
170 #endif
171 #ifdef CONFIG_X86_MCE_THRESHOLD
172 sum += irq_stats(cpu)->irq_threshold_count;
173 #endif
174 #ifdef CONFIG_X86_MCE
175 sum += per_cpu(mce_exception_count, cpu);
176 sum += per_cpu(mce_poll_count, cpu);
177 #endif
178 return sum;
179 }
180
181 u64 arch_irq_stat(void)
182 {
183 u64 sum = atomic_read(&irq_err_count);
184 return sum;
185 }
186
187
188 /*
189 * do_IRQ handles all normal device IRQ's (the special
190 * SMP cross-CPU interrupts have their own specific
191 * handlers).
192 */
193 __visible unsigned int __irq_entry do_IRQ(struct pt_regs *regs)
194 {
195 struct pt_regs *old_regs = set_irq_regs(regs);
196
197 /* high bit used in ret_from_ code */
198 unsigned vector = ~regs->orig_ax;
199 unsigned irq;
200
201 irq_enter();
202 exit_idle();
203
204 irq = __this_cpu_read(vector_irq[vector]);
205
206 if (!handle_irq(irq, regs)) {
207 ack_APIC_irq();
208
209 if (irq != VECTOR_RETRIGGERED) {
210 pr_emerg_ratelimited("%s: %d.%d No irq handler for vector (irq %d)\n",
211 __func__, smp_processor_id(),
212 vector, irq);
213 } else {
214 __this_cpu_write(vector_irq[vector], VECTOR_UNDEFINED);
215 }
216 }
217
218 irq_exit();
219
220 set_irq_regs(old_regs);
221 return 1;
222 }
223
224 /*
225 * Handler for X86_PLATFORM_IPI_VECTOR.
226 */
227 void __smp_x86_platform_ipi(void)
228 {
229 inc_irq_stat(x86_platform_ipis);
230
231 if (x86_platform_ipi_callback)
232 x86_platform_ipi_callback();
233 }
234
235 __visible void smp_x86_platform_ipi(struct pt_regs *regs)
236 {
237 struct pt_regs *old_regs = set_irq_regs(regs);
238
239 entering_ack_irq();
240 __smp_x86_platform_ipi();
241 exiting_irq();
242 set_irq_regs(old_regs);
243 }
244
245 #ifdef CONFIG_HAVE_KVM
246 /*
247 * Handler for POSTED_INTERRUPT_VECTOR.
248 */
249 __visible void smp_kvm_posted_intr_ipi(struct pt_regs *regs)
250 {
251 struct pt_regs *old_regs = set_irq_regs(regs);
252
253 ack_APIC_irq();
254
255 irq_enter();
256
257 exit_idle();
258
259 inc_irq_stat(kvm_posted_intr_ipis);
260
261 irq_exit();
262
263 set_irq_regs(old_regs);
264 }
265 #endif
266
267 __visible void smp_trace_x86_platform_ipi(struct pt_regs *regs)
268 {
269 struct pt_regs *old_regs = set_irq_regs(regs);
270
271 entering_ack_irq();
272 trace_x86_platform_ipi_entry(X86_PLATFORM_IPI_VECTOR);
273 __smp_x86_platform_ipi();
274 trace_x86_platform_ipi_exit(X86_PLATFORM_IPI_VECTOR);
275 exiting_irq();
276 set_irq_regs(old_regs);
277 }
278
279 EXPORT_SYMBOL_GPL(vector_used_by_percpu_irq);
280
281 #ifdef CONFIG_HOTPLUG_CPU
282
283 /* These two declarations are only used in check_irq_vectors_for_cpu_disable()
284 * below, which is protected by stop_machine(). Putting them on the stack
285 * results in a stack frame overflow. Dynamically allocating could result in a
286 * failure so declare these two cpumasks as global.
287 */
288 static struct cpumask affinity_new, online_new;
289
290 /*
291 * This cpu is going to be removed and its vectors migrated to the remaining
292 * online cpus. Check to see if there are enough vectors in the remaining cpus.
293 * This function is protected by stop_machine().
294 */
295 int check_irq_vectors_for_cpu_disable(void)
296 {
297 int irq, cpu;
298 unsigned int this_cpu, vector, this_count, count;
299 struct irq_desc *desc;
300 struct irq_data *data;
301
302 this_cpu = smp_processor_id();
303 cpumask_copy(&online_new, cpu_online_mask);
304 cpumask_clear_cpu(this_cpu, &online_new);
305
306 this_count = 0;
307 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
308 irq = __this_cpu_read(vector_irq[vector]);
309 if (irq >= 0) {
310 desc = irq_to_desc(irq);
311 if (!desc)
312 continue;
313
314 data = irq_desc_get_irq_data(desc);
315 cpumask_copy(&affinity_new, data->affinity);
316 cpumask_clear_cpu(this_cpu, &affinity_new);
317
318 /* Do not count inactive or per-cpu irqs. */
319 if (!irq_has_action(irq) || irqd_is_per_cpu(data))
320 continue;
321
322 /*
323 * A single irq may be mapped to multiple
324 * cpu's vector_irq[] (for example IOAPIC cluster
325 * mode). In this case we have two
326 * possibilities:
327 *
328 * 1) the resulting affinity mask is empty; that is
329 * this the down'd cpu is the last cpu in the irq's
330 * affinity mask, or
331 *
332 * 2) the resulting affinity mask is no longer
333 * a subset of the online cpus but the affinity
334 * mask is not zero; that is the down'd cpu is the
335 * last online cpu in a user set affinity mask.
336 */
337 if (cpumask_empty(&affinity_new) ||
338 !cpumask_subset(&affinity_new, &online_new))
339 this_count++;
340 }
341 }
342
343 count = 0;
344 for_each_online_cpu(cpu) {
345 if (cpu == this_cpu)
346 continue;
347 /*
348 * We scan from FIRST_EXTERNAL_VECTOR to first system
349 * vector. If the vector is marked in the used vectors
350 * bitmap or an irq is assigned to it, we don't count
351 * it as available.
352 */
353 for (vector = FIRST_EXTERNAL_VECTOR;
354 vector < first_system_vector; vector++) {
355 if (!test_bit(vector, used_vectors) &&
356 per_cpu(vector_irq, cpu)[vector] < 0)
357 count++;
358 }
359 }
360
361 if (count < this_count) {
362 pr_warn("CPU %d disable failed: CPU has %u vectors assigned and there are only %u available.\n",
363 this_cpu, this_count, count);
364 return -ERANGE;
365 }
366 return 0;
367 }
368
369 /* A cpu has been removed from cpu_online_mask. Reset irq affinities. */
370 void fixup_irqs(void)
371 {
372 unsigned int irq, vector;
373 static int warned;
374 struct irq_desc *desc;
375 struct irq_data *data;
376 struct irq_chip *chip;
377 int ret;
378
379 for_each_irq_desc(irq, desc) {
380 int break_affinity = 0;
381 int set_affinity = 1;
382 const struct cpumask *affinity;
383
384 if (!desc)
385 continue;
386 if (irq == 2)
387 continue;
388
389 /* interrupt's are disabled at this point */
390 raw_spin_lock(&desc->lock);
391
392 data = irq_desc_get_irq_data(desc);
393 affinity = data->affinity;
394 if (!irq_has_action(irq) || irqd_is_per_cpu(data) ||
395 cpumask_subset(affinity, cpu_online_mask)) {
396 raw_spin_unlock(&desc->lock);
397 continue;
398 }
399
400 /*
401 * Complete the irq move. This cpu is going down and for
402 * non intr-remapping case, we can't wait till this interrupt
403 * arrives at this cpu before completing the irq move.
404 */
405 irq_force_complete_move(irq);
406
407 if (cpumask_any_and(affinity, cpu_online_mask) >= nr_cpu_ids) {
408 break_affinity = 1;
409 affinity = cpu_online_mask;
410 }
411
412 chip = irq_data_get_irq_chip(data);
413 if (!irqd_can_move_in_process_context(data) && chip->irq_mask)
414 chip->irq_mask(data);
415
416 if (chip->irq_set_affinity) {
417 ret = chip->irq_set_affinity(data, affinity, true);
418 if (ret == -ENOSPC)
419 pr_crit("IRQ %d set affinity failed because there are no available vectors. The device assigned to this IRQ is unstable.\n", irq);
420 } else {
421 if (!(warned++))
422 set_affinity = 0;
423 }
424
425 /*
426 * We unmask if the irq was not marked masked by the
427 * core code. That respects the lazy irq disable
428 * behaviour.
429 */
430 if (!irqd_can_move_in_process_context(data) &&
431 !irqd_irq_masked(data) && chip->irq_unmask)
432 chip->irq_unmask(data);
433
434 raw_spin_unlock(&desc->lock);
435
436 if (break_affinity && set_affinity)
437 pr_notice("Broke affinity for irq %i\n", irq);
438 else if (!set_affinity)
439 pr_notice("Cannot set affinity for irq %i\n", irq);
440 }
441
442 /*
443 * We can remove mdelay() and then send spuriuous interrupts to
444 * new cpu targets for all the irqs that were handled previously by
445 * this cpu. While it works, I have seen spurious interrupt messages
446 * (nothing wrong but still...).
447 *
448 * So for now, retain mdelay(1) and check the IRR and then send those
449 * interrupts to new targets as this cpu is already offlined...
450 */
451 mdelay(1);
452
453 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
454 unsigned int irr;
455
456 if (__this_cpu_read(vector_irq[vector]) <= VECTOR_UNDEFINED)
457 continue;
458
459 irr = apic_read(APIC_IRR + (vector / 32 * 0x10));
460 if (irr & (1 << (vector % 32))) {
461 irq = __this_cpu_read(vector_irq[vector]);
462
463 desc = irq_to_desc(irq);
464 data = irq_desc_get_irq_data(desc);
465 chip = irq_data_get_irq_chip(data);
466 raw_spin_lock(&desc->lock);
467 if (chip->irq_retrigger) {
468 chip->irq_retrigger(data);
469 __this_cpu_write(vector_irq[vector], VECTOR_RETRIGGERED);
470 }
471 raw_spin_unlock(&desc->lock);
472 }
473 if (__this_cpu_read(vector_irq[vector]) != VECTOR_RETRIGGERED)
474 __this_cpu_write(vector_irq[vector], VECTOR_UNDEFINED);
475 }
476 }
477 #endif
This page took 0.041836 seconds and 6 git commands to generate.