genirq: Expose irq_desc->node in proc/irq
[safe/jmp/linux-2.6] / kernel / irq / proc.c
1 /*
2  * linux/kernel/irq/proc.c
3  *
4  * Copyright (C) 1992, 1998-2004 Linus Torvalds, Ingo Molnar
5  *
6  * This file contains the /proc/irq/ handling code.
7  */
8
9 #include <linux/irq.h>
10 #include <linux/proc_fs.h>
11 #include <linux/seq_file.h>
12 #include <linux/interrupt.h>
13
14 #include "internals.h"
15
16 static struct proc_dir_entry *root_irq_dir;
17
18 #ifdef CONFIG_SMP
19
20 static int irq_affinity_proc_show(struct seq_file *m, void *v)
21 {
22         struct irq_desc *desc = irq_to_desc((long)m->private);
23         const struct cpumask *mask = desc->affinity;
24
25 #ifdef CONFIG_GENERIC_PENDING_IRQ
26         if (desc->status & IRQ_MOVE_PENDING)
27                 mask = desc->pending_mask;
28 #endif
29         seq_cpumask(m, mask);
30         seq_putc(m, '\n');
31         return 0;
32 }
33
34 #ifndef is_affinity_mask_valid
35 #define is_affinity_mask_valid(val) 1
36 #endif
37
38 int no_irq_affinity;
39 static ssize_t irq_affinity_proc_write(struct file *file,
40                 const char __user *buffer, size_t count, loff_t *pos)
41 {
42         unsigned int irq = (int)(long)PDE(file->f_path.dentry->d_inode)->data;
43         cpumask_var_t new_value;
44         int err;
45
46         if (!irq_to_desc(irq)->chip->set_affinity || no_irq_affinity ||
47             irq_balancing_disabled(irq))
48                 return -EIO;
49
50         if (!alloc_cpumask_var(&new_value, GFP_KERNEL))
51                 return -ENOMEM;
52
53         err = cpumask_parse_user(buffer, count, new_value);
54         if (err)
55                 goto free_cpumask;
56
57         if (!is_affinity_mask_valid(new_value)) {
58                 err = -EINVAL;
59                 goto free_cpumask;
60         }
61
62         /*
63          * Do not allow disabling IRQs completely - it's a too easy
64          * way to make the system unusable accidentally :-) At least
65          * one online CPU still has to be targeted.
66          */
67         if (!cpumask_intersects(new_value, cpu_online_mask)) {
68                 /* Special case for empty set - allow the architecture
69                    code to set default SMP affinity. */
70                 err = irq_select_affinity_usr(irq) ? -EINVAL : count;
71         } else {
72                 irq_set_affinity(irq, new_value);
73                 err = count;
74         }
75
76 free_cpumask:
77         free_cpumask_var(new_value);
78         return err;
79 }
80
81 static int irq_affinity_proc_open(struct inode *inode, struct file *file)
82 {
83         return single_open(file, irq_affinity_proc_show, PDE(inode)->data);
84 }
85
86 static const struct file_operations irq_affinity_proc_fops = {
87         .open           = irq_affinity_proc_open,
88         .read           = seq_read,
89         .llseek         = seq_lseek,
90         .release        = single_release,
91         .write          = irq_affinity_proc_write,
92 };
93
94 static int default_affinity_show(struct seq_file *m, void *v)
95 {
96         seq_cpumask(m, irq_default_affinity);
97         seq_putc(m, '\n');
98         return 0;
99 }
100
101 static ssize_t default_affinity_write(struct file *file,
102                 const char __user *buffer, size_t count, loff_t *ppos)
103 {
104         cpumask_var_t new_value;
105         int err;
106
107         if (!alloc_cpumask_var(&new_value, GFP_KERNEL))
108                 return -ENOMEM;
109
110         err = cpumask_parse_user(buffer, count, new_value);
111         if (err)
112                 goto out;
113
114         if (!is_affinity_mask_valid(new_value)) {
115                 err = -EINVAL;
116                 goto out;
117         }
118
119         /*
120          * Do not allow disabling IRQs completely - it's a too easy
121          * way to make the system unusable accidentally :-) At least
122          * one online CPU still has to be targeted.
123          */
124         if (!cpumask_intersects(new_value, cpu_online_mask)) {
125                 err = -EINVAL;
126                 goto out;
127         }
128
129         cpumask_copy(irq_default_affinity, new_value);
130         err = count;
131
132 out:
133         free_cpumask_var(new_value);
134         return err;
135 }
136
137 static int default_affinity_open(struct inode *inode, struct file *file)
138 {
139         return single_open(file, default_affinity_show, PDE(inode)->data);
140 }
141
142 static const struct file_operations default_affinity_proc_fops = {
143         .open           = default_affinity_open,
144         .read           = seq_read,
145         .llseek         = seq_lseek,
146         .release        = single_release,
147         .write          = default_affinity_write,
148 };
149
150 static int irq_node_proc_show(struct seq_file *m, void *v)
151 {
152         struct irq_desc *desc = irq_to_desc((long) m->private);
153
154         seq_printf(m, "%d\n", desc->node);
155         return 0;
156 }
157
158 static int irq_node_proc_open(struct inode *inode, struct file *file)
159 {
160         return single_open(file, irq_node_proc_show, PDE(inode)->data);
161 }
162
163 static const struct file_operations irq_node_proc_fops = {
164         .open           = irq_node_proc_open,
165         .read           = seq_read,
166         .llseek         = seq_lseek,
167         .release        = single_release,
168 };
169 #endif
170
171 static int irq_spurious_proc_show(struct seq_file *m, void *v)
172 {
173         struct irq_desc *desc = irq_to_desc((long) m->private);
174
175         seq_printf(m, "count %u\n" "unhandled %u\n" "last_unhandled %u ms\n",
176                    desc->irq_count, desc->irqs_unhandled,
177                    jiffies_to_msecs(desc->last_unhandled));
178         return 0;
179 }
180
181 static int irq_spurious_proc_open(struct inode *inode, struct file *file)
182 {
183         return single_open(file, irq_spurious_proc_show, NULL);
184 }
185
186 static const struct file_operations irq_spurious_proc_fops = {
187         .open           = irq_spurious_proc_open,
188         .read           = seq_read,
189         .llseek         = seq_lseek,
190         .release        = single_release,
191 };
192
193 #define MAX_NAMELEN 128
194
195 static int name_unique(unsigned int irq, struct irqaction *new_action)
196 {
197         struct irq_desc *desc = irq_to_desc(irq);
198         struct irqaction *action;
199         unsigned long flags;
200         int ret = 1;
201
202         raw_spin_lock_irqsave(&desc->lock, flags);
203         for (action = desc->action ; action; action = action->next) {
204                 if ((action != new_action) && action->name &&
205                                 !strcmp(new_action->name, action->name)) {
206                         ret = 0;
207                         break;
208                 }
209         }
210         raw_spin_unlock_irqrestore(&desc->lock, flags);
211         return ret;
212 }
213
214 void register_handler_proc(unsigned int irq, struct irqaction *action)
215 {
216         char name [MAX_NAMELEN];
217         struct irq_desc *desc = irq_to_desc(irq);
218
219         if (!desc->dir || action->dir || !action->name ||
220                                         !name_unique(irq, action))
221                 return;
222
223         memset(name, 0, MAX_NAMELEN);
224         snprintf(name, MAX_NAMELEN, "%s", action->name);
225
226         /* create /proc/irq/1234/handler/ */
227         action->dir = proc_mkdir(name, desc->dir);
228 }
229
230 #undef MAX_NAMELEN
231
232 #define MAX_NAMELEN 10
233
234 void register_irq_proc(unsigned int irq, struct irq_desc *desc)
235 {
236         char name [MAX_NAMELEN];
237
238         if (!root_irq_dir || (desc->chip == &no_irq_chip) || desc->dir)
239                 return;
240
241         memset(name, 0, MAX_NAMELEN);
242         sprintf(name, "%d", irq);
243
244         /* create /proc/irq/1234 */
245         desc->dir = proc_mkdir(name, root_irq_dir);
246         if (!desc->dir)
247                 return;
248
249 #ifdef CONFIG_SMP
250         /* create /proc/irq/<irq>/smp_affinity */
251         proc_create_data("smp_affinity", 0600, desc->dir,
252                          &irq_affinity_proc_fops, (void *)(long)irq);
253
254         proc_create_data("node", 0444, desc->dir,
255                          &irq_node_proc_fops, (void *)(long)irq);
256 #endif
257
258         proc_create_data("spurious", 0444, desc->dir,
259                          &irq_spurious_proc_fops, (void *)(long)irq);
260 }
261
262 #undef MAX_NAMELEN
263
264 void unregister_handler_proc(unsigned int irq, struct irqaction *action)
265 {
266         if (action->dir) {
267                 struct irq_desc *desc = irq_to_desc(irq);
268
269                 remove_proc_entry(action->dir->name, desc->dir);
270         }
271 }
272
273 static void register_default_affinity_proc(void)
274 {
275 #ifdef CONFIG_SMP
276         proc_create("irq/default_smp_affinity", 0600, NULL,
277                     &default_affinity_proc_fops);
278 #endif
279 }
280
281 void init_irq_proc(void)
282 {
283         unsigned int irq;
284         struct irq_desc *desc;
285
286         /* create /proc/irq */
287         root_irq_dir = proc_mkdir("irq", NULL);
288         if (!root_irq_dir)
289                 return;
290
291         register_default_affinity_proc();
292
293         /*
294          * Create entries for all existing IRQs.
295          */
296         for_each_irq_desc(irq, desc) {
297                 if (!desc)
298                         continue;
299
300                 register_irq_proc(irq, desc);
301         }
302 }
303