Merge branch 'linus' into cpus4096
[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         cpumask_t *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_t new_value;
105         int err;
106
107         err = cpumask_parse_user(buffer, count, &new_value);
108         if (err)
109                 return err;
110
111         if (!is_affinity_mask_valid(new_value))
112                 return -EINVAL;
113
114         /*
115          * Do not allow disabling IRQs completely - it's a too easy
116          * way to make the system unusable accidentally :-) At least
117          * one online CPU still has to be targeted.
118          */
119         if (!cpus_intersects(new_value, cpu_online_map))
120                 return -EINVAL;
121
122         irq_default_affinity = new_value;
123
124         return count;
125 }
126
127 static int default_affinity_open(struct inode *inode, struct file *file)
128 {
129         return single_open(file, default_affinity_show, NULL);
130 }
131
132 static const struct file_operations default_affinity_proc_fops = {
133         .open           = default_affinity_open,
134         .read           = seq_read,
135         .llseek         = seq_lseek,
136         .release        = single_release,
137         .write          = default_affinity_write,
138 };
139 #endif
140
141 static int irq_spurious_read(char *page, char **start, off_t off,
142                                   int count, int *eof, void *data)
143 {
144         struct irq_desc *desc = irq_to_desc((long) data);
145         return sprintf(page, "count %u\n"
146                              "unhandled %u\n"
147                              "last_unhandled %u ms\n",
148                         desc->irq_count,
149                         desc->irqs_unhandled,
150                         jiffies_to_msecs(desc->last_unhandled));
151 }
152
153 #define MAX_NAMELEN 128
154
155 static int name_unique(unsigned int irq, struct irqaction *new_action)
156 {
157         struct irq_desc *desc = irq_to_desc(irq);
158         struct irqaction *action;
159         unsigned long flags;
160         int ret = 1;
161
162         spin_lock_irqsave(&desc->lock, flags);
163         for (action = desc->action ; action; action = action->next) {
164                 if ((action != new_action) && action->name &&
165                                 !strcmp(new_action->name, action->name)) {
166                         ret = 0;
167                         break;
168                 }
169         }
170         spin_unlock_irqrestore(&desc->lock, flags);
171         return ret;
172 }
173
174 void register_handler_proc(unsigned int irq, struct irqaction *action)
175 {
176         char name [MAX_NAMELEN];
177         struct irq_desc *desc = irq_to_desc(irq);
178
179         if (!desc->dir || action->dir || !action->name ||
180                                         !name_unique(irq, action))
181                 return;
182
183         memset(name, 0, MAX_NAMELEN);
184         snprintf(name, MAX_NAMELEN, "%s", action->name);
185
186         /* create /proc/irq/1234/handler/ */
187         action->dir = proc_mkdir(name, desc->dir);
188 }
189
190 #undef MAX_NAMELEN
191
192 #define MAX_NAMELEN 10
193
194 void register_irq_proc(unsigned int irq, struct irq_desc *desc)
195 {
196         char name [MAX_NAMELEN];
197         struct proc_dir_entry *entry;
198
199         if (!root_irq_dir || (desc->chip == &no_irq_chip) || desc->dir)
200                 return;
201
202         memset(name, 0, MAX_NAMELEN);
203         sprintf(name, "%d", irq);
204
205         /* create /proc/irq/1234 */
206         desc->dir = proc_mkdir(name, root_irq_dir);
207
208 #ifdef CONFIG_SMP
209         /* create /proc/irq/<irq>/smp_affinity */
210         proc_create_data("smp_affinity", 0600, desc->dir,
211                          &irq_affinity_proc_fops, (void *)(long)irq);
212 #endif
213
214         entry = create_proc_entry("spurious", 0444, desc->dir);
215         if (entry) {
216                 entry->data = (void *)(long)irq;
217                 entry->read_proc = irq_spurious_read;
218         }
219 }
220
221 #undef MAX_NAMELEN
222
223 void unregister_handler_proc(unsigned int irq, struct irqaction *action)
224 {
225         if (action->dir) {
226                 struct irq_desc *desc = irq_to_desc(irq);
227
228                 remove_proc_entry(action->dir->name, desc->dir);
229         }
230 }
231
232 static void register_default_affinity_proc(void)
233 {
234 #ifdef CONFIG_SMP
235         proc_create("irq/default_smp_affinity", 0600, NULL,
236                     &default_affinity_proc_fops);
237 #endif
238 }
239
240 void init_irq_proc(void)
241 {
242         unsigned int irq;
243         struct irq_desc *desc;
244
245         /* create /proc/irq */
246         root_irq_dir = proc_mkdir("irq", NULL);
247         if (!root_irq_dir)
248                 return;
249
250         register_default_affinity_proc();
251
252         /*
253          * Create entries for all existing IRQs.
254          */
255         for_each_irq_desc(irq, desc) {
256                 if (!desc)
257                         continue;
258
259                 register_irq_proc(irq, desc);
260         }
261 }
262