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