perf/x86/intel: Fix event update for auto-reload
[linux/fpc-iii.git] / kernel / irq / proc.c
blobe8f374971e37cb59a7c3c4e12f0bf2baa339ba33
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * linux/kernel/irq/proc.c
5 * Copyright (C) 1992, 1998-2004 Linus Torvalds, Ingo Molnar
7 * This file contains the /proc/irq/ handling code.
8 */
10 #include <linux/irq.h>
11 #include <linux/gfp.h>
12 #include <linux/proc_fs.h>
13 #include <linux/seq_file.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel_stat.h>
16 #include <linux/mutex.h>
18 #include "internals.h"
21 * Access rules:
23 * procfs protects read/write of /proc/irq/N/ files against a
24 * concurrent free of the interrupt descriptor. remove_proc_entry()
25 * immediately prevents new read/writes to happen and waits for
26 * already running read/write functions to complete.
28 * We remove the proc entries first and then delete the interrupt
29 * descriptor from the radix tree and free it. So it is guaranteed
30 * that irq_to_desc(N) is valid as long as the read/writes are
31 * permitted by procfs.
33 * The read from /proc/interrupts is a different problem because there
34 * is no protection. So the lookup and the access to irqdesc
35 * information must be protected by sparse_irq_lock.
37 static struct proc_dir_entry *root_irq_dir;
39 #ifdef CONFIG_SMP
41 enum {
42 AFFINITY,
43 AFFINITY_LIST,
44 EFFECTIVE,
45 EFFECTIVE_LIST,
48 static int show_irq_affinity(int type, struct seq_file *m)
50 struct irq_desc *desc = irq_to_desc((long)m->private);
51 const struct cpumask *mask;
53 switch (type) {
54 case AFFINITY:
55 case AFFINITY_LIST:
56 mask = desc->irq_common_data.affinity;
57 #ifdef CONFIG_GENERIC_PENDING_IRQ
58 if (irqd_is_setaffinity_pending(&desc->irq_data))
59 mask = desc->pending_mask;
60 #endif
61 break;
62 case EFFECTIVE:
63 case EFFECTIVE_LIST:
64 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
65 mask = irq_data_get_effective_affinity_mask(&desc->irq_data);
66 break;
67 #endif
68 default:
69 return -EINVAL;
72 switch (type) {
73 case AFFINITY_LIST:
74 case EFFECTIVE_LIST:
75 seq_printf(m, "%*pbl\n", cpumask_pr_args(mask));
76 break;
77 case AFFINITY:
78 case EFFECTIVE:
79 seq_printf(m, "%*pb\n", cpumask_pr_args(mask));
80 break;
82 return 0;
85 static int irq_affinity_hint_proc_show(struct seq_file *m, void *v)
87 struct irq_desc *desc = irq_to_desc((long)m->private);
88 unsigned long flags;
89 cpumask_var_t mask;
91 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
92 return -ENOMEM;
94 raw_spin_lock_irqsave(&desc->lock, flags);
95 if (desc->affinity_hint)
96 cpumask_copy(mask, desc->affinity_hint);
97 raw_spin_unlock_irqrestore(&desc->lock, flags);
99 seq_printf(m, "%*pb\n", cpumask_pr_args(mask));
100 free_cpumask_var(mask);
102 return 0;
105 #ifndef is_affinity_mask_valid
106 #define is_affinity_mask_valid(val) 1
107 #endif
109 int no_irq_affinity;
110 static int irq_affinity_proc_show(struct seq_file *m, void *v)
112 return show_irq_affinity(AFFINITY, m);
115 static int irq_affinity_list_proc_show(struct seq_file *m, void *v)
117 return show_irq_affinity(AFFINITY_LIST, m);
121 static ssize_t write_irq_affinity(int type, struct file *file,
122 const char __user *buffer, size_t count, loff_t *pos)
124 unsigned int irq = (int)(long)PDE_DATA(file_inode(file));
125 cpumask_var_t new_value;
126 int err;
128 if (!irq_can_set_affinity_usr(irq) || no_irq_affinity)
129 return -EIO;
131 if (!alloc_cpumask_var(&new_value, GFP_KERNEL))
132 return -ENOMEM;
134 if (type)
135 err = cpumask_parselist_user(buffer, count, new_value);
136 else
137 err = cpumask_parse_user(buffer, count, new_value);
138 if (err)
139 goto free_cpumask;
141 if (!is_affinity_mask_valid(new_value)) {
142 err = -EINVAL;
143 goto free_cpumask;
147 * Do not allow disabling IRQs completely - it's a too easy
148 * way to make the system unusable accidentally :-) At least
149 * one online CPU still has to be targeted.
151 if (!cpumask_intersects(new_value, cpu_online_mask)) {
153 * Special case for empty set - allow the architecture code
154 * to set default SMP affinity.
156 err = irq_select_affinity_usr(irq) ? -EINVAL : count;
157 } else {
158 err = irq_set_affinity(irq, new_value);
159 if (!err)
160 err = count;
163 free_cpumask:
164 free_cpumask_var(new_value);
165 return err;
168 static ssize_t irq_affinity_proc_write(struct file *file,
169 const char __user *buffer, size_t count, loff_t *pos)
171 return write_irq_affinity(0, file, buffer, count, pos);
174 static ssize_t irq_affinity_list_proc_write(struct file *file,
175 const char __user *buffer, size_t count, loff_t *pos)
177 return write_irq_affinity(1, file, buffer, count, pos);
180 static int irq_affinity_proc_open(struct inode *inode, struct file *file)
182 return single_open(file, irq_affinity_proc_show, PDE_DATA(inode));
185 static int irq_affinity_list_proc_open(struct inode *inode, struct file *file)
187 return single_open(file, irq_affinity_list_proc_show, PDE_DATA(inode));
190 static int irq_affinity_hint_proc_open(struct inode *inode, struct file *file)
192 return single_open(file, irq_affinity_hint_proc_show, PDE_DATA(inode));
195 static const struct file_operations irq_affinity_proc_fops = {
196 .open = irq_affinity_proc_open,
197 .read = seq_read,
198 .llseek = seq_lseek,
199 .release = single_release,
200 .write = irq_affinity_proc_write,
203 static const struct file_operations irq_affinity_hint_proc_fops = {
204 .open = irq_affinity_hint_proc_open,
205 .read = seq_read,
206 .llseek = seq_lseek,
207 .release = single_release,
210 static const struct file_operations irq_affinity_list_proc_fops = {
211 .open = irq_affinity_list_proc_open,
212 .read = seq_read,
213 .llseek = seq_lseek,
214 .release = single_release,
215 .write = irq_affinity_list_proc_write,
218 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
219 static int irq_effective_aff_proc_show(struct seq_file *m, void *v)
221 return show_irq_affinity(EFFECTIVE, m);
224 static int irq_effective_aff_list_proc_show(struct seq_file *m, void *v)
226 return show_irq_affinity(EFFECTIVE_LIST, m);
229 static int irq_effective_aff_proc_open(struct inode *inode, struct file *file)
231 return single_open(file, irq_effective_aff_proc_show, PDE_DATA(inode));
234 static int irq_effective_aff_list_proc_open(struct inode *inode,
235 struct file *file)
237 return single_open(file, irq_effective_aff_list_proc_show,
238 PDE_DATA(inode));
241 static const struct file_operations irq_effective_aff_proc_fops = {
242 .open = irq_effective_aff_proc_open,
243 .read = seq_read,
244 .llseek = seq_lseek,
245 .release = single_release,
248 static const struct file_operations irq_effective_aff_list_proc_fops = {
249 .open = irq_effective_aff_list_proc_open,
250 .read = seq_read,
251 .llseek = seq_lseek,
252 .release = single_release,
254 #endif
256 static int default_affinity_show(struct seq_file *m, void *v)
258 seq_printf(m, "%*pb\n", cpumask_pr_args(irq_default_affinity));
259 return 0;
262 static ssize_t default_affinity_write(struct file *file,
263 const char __user *buffer, size_t count, loff_t *ppos)
265 cpumask_var_t new_value;
266 int err;
268 if (!alloc_cpumask_var(&new_value, GFP_KERNEL))
269 return -ENOMEM;
271 err = cpumask_parse_user(buffer, count, new_value);
272 if (err)
273 goto out;
275 if (!is_affinity_mask_valid(new_value)) {
276 err = -EINVAL;
277 goto out;
281 * Do not allow disabling IRQs completely - it's a too easy
282 * way to make the system unusable accidentally :-) At least
283 * one online CPU still has to be targeted.
285 if (!cpumask_intersects(new_value, cpu_online_mask)) {
286 err = -EINVAL;
287 goto out;
290 cpumask_copy(irq_default_affinity, new_value);
291 err = count;
293 out:
294 free_cpumask_var(new_value);
295 return err;
298 static int default_affinity_open(struct inode *inode, struct file *file)
300 return single_open(file, default_affinity_show, PDE_DATA(inode));
303 static const struct file_operations default_affinity_proc_fops = {
304 .open = default_affinity_open,
305 .read = seq_read,
306 .llseek = seq_lseek,
307 .release = single_release,
308 .write = default_affinity_write,
311 static int irq_node_proc_show(struct seq_file *m, void *v)
313 struct irq_desc *desc = irq_to_desc((long) m->private);
315 seq_printf(m, "%d\n", irq_desc_get_node(desc));
316 return 0;
319 static int irq_node_proc_open(struct inode *inode, struct file *file)
321 return single_open(file, irq_node_proc_show, PDE_DATA(inode));
324 static const struct file_operations irq_node_proc_fops = {
325 .open = irq_node_proc_open,
326 .read = seq_read,
327 .llseek = seq_lseek,
328 .release = single_release,
330 #endif
332 static int irq_spurious_proc_show(struct seq_file *m, void *v)
334 struct irq_desc *desc = irq_to_desc((long) m->private);
336 seq_printf(m, "count %u\n" "unhandled %u\n" "last_unhandled %u ms\n",
337 desc->irq_count, desc->irqs_unhandled,
338 jiffies_to_msecs(desc->last_unhandled));
339 return 0;
342 static int irq_spurious_proc_open(struct inode *inode, struct file *file)
344 return single_open(file, irq_spurious_proc_show, PDE_DATA(inode));
347 static const struct file_operations irq_spurious_proc_fops = {
348 .open = irq_spurious_proc_open,
349 .read = seq_read,
350 .llseek = seq_lseek,
351 .release = single_release,
354 #define MAX_NAMELEN 128
356 static int name_unique(unsigned int irq, struct irqaction *new_action)
358 struct irq_desc *desc = irq_to_desc(irq);
359 struct irqaction *action;
360 unsigned long flags;
361 int ret = 1;
363 raw_spin_lock_irqsave(&desc->lock, flags);
364 for_each_action_of_desc(desc, action) {
365 if ((action != new_action) && action->name &&
366 !strcmp(new_action->name, action->name)) {
367 ret = 0;
368 break;
371 raw_spin_unlock_irqrestore(&desc->lock, flags);
372 return ret;
375 void register_handler_proc(unsigned int irq, struct irqaction *action)
377 char name [MAX_NAMELEN];
378 struct irq_desc *desc = irq_to_desc(irq);
380 if (!desc->dir || action->dir || !action->name ||
381 !name_unique(irq, action))
382 return;
384 snprintf(name, MAX_NAMELEN, "%s", action->name);
386 /* create /proc/irq/1234/handler/ */
387 action->dir = proc_mkdir(name, desc->dir);
390 #undef MAX_NAMELEN
392 #define MAX_NAMELEN 10
394 void register_irq_proc(unsigned int irq, struct irq_desc *desc)
396 static DEFINE_MUTEX(register_lock);
397 void __maybe_unused *irqp = (void *)(unsigned long) irq;
398 char name [MAX_NAMELEN];
400 if (!root_irq_dir || (desc->irq_data.chip == &no_irq_chip))
401 return;
404 * irq directories are registered only when a handler is
405 * added, not when the descriptor is created, so multiple
406 * tasks might try to register at the same time.
408 mutex_lock(&register_lock);
410 if (desc->dir)
411 goto out_unlock;
413 sprintf(name, "%d", irq);
415 /* create /proc/irq/1234 */
416 desc->dir = proc_mkdir(name, root_irq_dir);
417 if (!desc->dir)
418 goto out_unlock;
420 #ifdef CONFIG_SMP
421 /* create /proc/irq/<irq>/smp_affinity */
422 proc_create_data("smp_affinity", 0644, desc->dir,
423 &irq_affinity_proc_fops, irqp);
425 /* create /proc/irq/<irq>/affinity_hint */
426 proc_create_data("affinity_hint", 0444, desc->dir,
427 &irq_affinity_hint_proc_fops, irqp);
429 /* create /proc/irq/<irq>/smp_affinity_list */
430 proc_create_data("smp_affinity_list", 0644, desc->dir,
431 &irq_affinity_list_proc_fops, irqp);
433 proc_create_data("node", 0444, desc->dir,
434 &irq_node_proc_fops, irqp);
435 # ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
436 proc_create_data("effective_affinity", 0444, desc->dir,
437 &irq_effective_aff_proc_fops, irqp);
438 proc_create_data("effective_affinity_list", 0444, desc->dir,
439 &irq_effective_aff_list_proc_fops, irqp);
440 # endif
441 #endif
442 proc_create_data("spurious", 0444, desc->dir,
443 &irq_spurious_proc_fops, (void *)(long)irq);
445 out_unlock:
446 mutex_unlock(&register_lock);
449 void unregister_irq_proc(unsigned int irq, struct irq_desc *desc)
451 char name [MAX_NAMELEN];
453 if (!root_irq_dir || !desc->dir)
454 return;
455 #ifdef CONFIG_SMP
456 remove_proc_entry("smp_affinity", desc->dir);
457 remove_proc_entry("affinity_hint", desc->dir);
458 remove_proc_entry("smp_affinity_list", desc->dir);
459 remove_proc_entry("node", desc->dir);
460 # ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
461 remove_proc_entry("effective_affinity", desc->dir);
462 remove_proc_entry("effective_affinity_list", desc->dir);
463 # endif
464 #endif
465 remove_proc_entry("spurious", desc->dir);
467 sprintf(name, "%u", irq);
468 remove_proc_entry(name, root_irq_dir);
471 #undef MAX_NAMELEN
473 void unregister_handler_proc(unsigned int irq, struct irqaction *action)
475 proc_remove(action->dir);
478 static void register_default_affinity_proc(void)
480 #ifdef CONFIG_SMP
481 proc_create("irq/default_smp_affinity", 0644, NULL,
482 &default_affinity_proc_fops);
483 #endif
486 void init_irq_proc(void)
488 unsigned int irq;
489 struct irq_desc *desc;
491 /* create /proc/irq */
492 root_irq_dir = proc_mkdir("irq", NULL);
493 if (!root_irq_dir)
494 return;
496 register_default_affinity_proc();
499 * Create entries for all existing IRQs.
501 for_each_irq_desc(irq, desc)
502 register_irq_proc(irq, desc);
505 #ifdef CONFIG_GENERIC_IRQ_SHOW
507 int __weak arch_show_interrupts(struct seq_file *p, int prec)
509 return 0;
512 #ifndef ACTUAL_NR_IRQS
513 # define ACTUAL_NR_IRQS nr_irqs
514 #endif
516 int show_interrupts(struct seq_file *p, void *v)
518 static int prec;
520 unsigned long flags, any_count = 0;
521 int i = *(loff_t *) v, j;
522 struct irqaction *action;
523 struct irq_desc *desc;
525 if (i > ACTUAL_NR_IRQS)
526 return 0;
528 if (i == ACTUAL_NR_IRQS)
529 return arch_show_interrupts(p, prec);
531 /* print header and calculate the width of the first column */
532 if (i == 0) {
533 for (prec = 3, j = 1000; prec < 10 && j <= nr_irqs; ++prec)
534 j *= 10;
536 seq_printf(p, "%*s", prec + 8, "");
537 for_each_online_cpu(j)
538 seq_printf(p, "CPU%-8d", j);
539 seq_putc(p, '\n');
542 irq_lock_sparse();
543 desc = irq_to_desc(i);
544 if (!desc)
545 goto outsparse;
547 raw_spin_lock_irqsave(&desc->lock, flags);
548 for_each_online_cpu(j)
549 any_count |= kstat_irqs_cpu(i, j);
550 action = desc->action;
551 if ((!action || irq_desc_is_chained(desc)) && !any_count)
552 goto out;
554 seq_printf(p, "%*d: ", prec, i);
555 for_each_online_cpu(j)
556 seq_printf(p, "%10u ", kstat_irqs_cpu(i, j));
558 if (desc->irq_data.chip) {
559 if (desc->irq_data.chip->irq_print_chip)
560 desc->irq_data.chip->irq_print_chip(&desc->irq_data, p);
561 else if (desc->irq_data.chip->name)
562 seq_printf(p, " %8s", desc->irq_data.chip->name);
563 else
564 seq_printf(p, " %8s", "-");
565 } else {
566 seq_printf(p, " %8s", "None");
568 if (desc->irq_data.domain)
569 seq_printf(p, " %*d", prec, (int) desc->irq_data.hwirq);
570 else
571 seq_printf(p, " %*s", prec, "");
572 #ifdef CONFIG_GENERIC_IRQ_SHOW_LEVEL
573 seq_printf(p, " %-8s", irqd_is_level_type(&desc->irq_data) ? "Level" : "Edge");
574 #endif
575 if (desc->name)
576 seq_printf(p, "-%-8s", desc->name);
578 if (action) {
579 seq_printf(p, " %s", action->name);
580 while ((action = action->next) != NULL)
581 seq_printf(p, ", %s", action->name);
584 seq_putc(p, '\n');
585 out:
586 raw_spin_unlock_irqrestore(&desc->lock, flags);
587 outsparse:
588 irq_unlock_sparse();
589 return 0;
591 #endif