Merge /spare/repo/linux-2.6/
[linux-2.6] / arch / um / kernel / irq_user.c
1 /* 
2  * Copyright (C) 2000 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <errno.h>
9 #include <signal.h>
10 #include <string.h>
11 #include <sys/poll.h>
12 #include <sys/types.h>
13 #include <sys/time.h>
14 #include "user_util.h"
15 #include "kern_util.h"
16 #include "user.h"
17 #include "process.h"
18 #include "signal_user.h"
19 #include "sigio.h"
20 #include "irq_user.h"
21 #include "os.h"
22
23 struct irq_fd {
24         struct irq_fd *next;
25         void *id;
26         int fd;
27         int type;
28         int irq;
29         int pid;
30         int events;
31         int current_events;
32         int freed;
33 };
34
35 static struct irq_fd *active_fds = NULL;
36 static struct irq_fd **last_irq_ptr = &active_fds;
37
38 static struct pollfd *pollfds = NULL;
39 static int pollfds_num = 0;
40 static int pollfds_size = 0;
41
42 extern int io_count, intr_count;
43
44 void sigio_handler(int sig, union uml_pt_regs *regs)
45 {
46         struct irq_fd *irq_fd, *next;
47         int i, n;
48
49         if(smp_sigio_handler()) return;
50         while(1){
51                 n = poll(pollfds, pollfds_num, 0);
52                 if(n < 0){
53                         if(errno == EINTR) continue;
54                         printk("sigio_handler : poll returned %d, "
55                                "errno = %d\n", n, errno);
56                         break;
57                 }
58                 if(n == 0) break;
59
60                 irq_fd = active_fds;
61                 for(i = 0; i < pollfds_num; i++){
62                         if(pollfds[i].revents != 0){
63                                 irq_fd->current_events = pollfds[i].revents;
64                                 pollfds[i].fd = -1;
65                         }
66                         irq_fd = irq_fd->next;
67                 }
68
69                 for(irq_fd = active_fds; irq_fd != NULL; irq_fd = next){
70                         next = irq_fd->next;
71                         if(irq_fd->current_events != 0){
72                                 irq_fd->current_events = 0;
73                                 do_IRQ(irq_fd->irq, regs);
74
75                                 /* This is here because the next irq may be
76                                  * freed in the handler.  If a console goes
77                                  * away, both the read and write irqs will be
78                                  * freed.  After do_IRQ, ->next will point to
79                                  * a good IRQ.
80                                  * Irqs can't be freed inside their handlers,
81                                  * so the next best thing is to have them
82                                  * marked as needing freeing, so that they
83                                  * can be freed here.
84                                  */
85                                 next = irq_fd->next;
86                                 if(irq_fd->freed){
87                                         free_irq(irq_fd->irq, irq_fd->id);
88                                 }
89                         }
90                 }
91         }
92 }
93
94 int activate_ipi(int fd, int pid)
95 {
96         return(os_set_fd_async(fd, pid));
97 }
98
99 static void maybe_sigio_broken(int fd, int type)
100 {
101         if(isatty(fd)){
102                 if((type == IRQ_WRITE) && !pty_output_sigio){
103                         write_sigio_workaround();
104                         add_sigio_fd(fd, 0);
105                 }
106                 else if((type == IRQ_READ) && !pty_close_sigio){
107                         write_sigio_workaround();
108                         add_sigio_fd(fd, 1);                    
109                 }
110         }
111 }
112
113 int activate_fd(int irq, int fd, int type, void *dev_id)
114 {
115         struct pollfd *tmp_pfd;
116         struct irq_fd *new_fd, *irq_fd;
117         unsigned long flags;
118         int pid, events, err, n, size;
119
120         pid = os_getpid();
121         err = os_set_fd_async(fd, pid);
122         if(err < 0)
123                 goto out;
124
125         new_fd = um_kmalloc(sizeof(*new_fd));
126         err = -ENOMEM;
127         if(new_fd == NULL)
128                 goto out;
129
130         if(type == IRQ_READ) events = POLLIN | POLLPRI;
131         else events = POLLOUT;
132         *new_fd = ((struct irq_fd) { .next              = NULL,
133                                      .id                = dev_id,
134                                      .fd                = fd,
135                                      .type              = type,
136                                      .irq               = irq,
137                                      .pid               = pid,
138                                      .events            = events,
139                                      .current_events    = 0,
140                                      .freed             = 0  } );
141
142         /* Critical section - locked by a spinlock because this stuff can
143          * be changed from interrupt handlers.  The stuff above is done 
144          * outside the lock because it allocates memory.
145          */
146
147         /* Actually, it only looks like it can be called from interrupt
148          * context.  The culprit is reactivate_fd, which calls 
149          * maybe_sigio_broken, which calls write_sigio_workaround,
150          * which calls activate_fd.  However, write_sigio_workaround should
151          * only be called once, at boot time.  That would make it clear that
152          * this is called only from process context, and can be locked with
153          * a semaphore.
154          */
155         flags = irq_lock();
156         for(irq_fd = active_fds; irq_fd != NULL; irq_fd = irq_fd->next){
157                 if((irq_fd->fd == fd) && (irq_fd->type == type)){
158                         printk("Registering fd %d twice\n", fd);
159                         printk("Irqs : %d, %d\n", irq_fd->irq, irq);
160                         printk("Ids : 0x%x, 0x%x\n", irq_fd->id, dev_id);
161                         goto out_unlock;
162                 }
163         }
164
165         n = pollfds_num;
166         if(n == pollfds_size){
167                 while(1){
168                         /* Here we have to drop the lock in order to call 
169                          * kmalloc, which might sleep.  If something else
170                          * came in and changed the pollfds array, we free
171                          * the buffer and try again.
172                          */
173                         irq_unlock(flags);
174                         size = (pollfds_num + 1) * sizeof(pollfds[0]);
175                         tmp_pfd = um_kmalloc(size);
176                         flags = irq_lock();
177                         if(tmp_pfd == NULL)
178                                 goto out_unlock;
179                         if(n == pollfds_size)
180                                 break;
181                         kfree(tmp_pfd);
182                 }
183                 if(pollfds != NULL){
184                         memcpy(tmp_pfd, pollfds,
185                                sizeof(pollfds[0]) * pollfds_size);
186                         kfree(pollfds);
187                 }
188                 pollfds = tmp_pfd;
189                 pollfds_size++;
190         }
191
192         if(type == IRQ_WRITE) 
193                 fd = -1;
194
195         pollfds[pollfds_num] = ((struct pollfd) { .fd   = fd,
196                                                   .events       = events,
197                                                   .revents      = 0 });
198         pollfds_num++;
199
200         *last_irq_ptr = new_fd;
201         last_irq_ptr = &new_fd->next;
202
203         irq_unlock(flags);
204
205         /* This calls activate_fd, so it has to be outside the critical
206          * section.
207          */
208         maybe_sigio_broken(fd, type);
209
210         return(0);
211
212  out_unlock:
213         irq_unlock(flags);
214         kfree(new_fd);
215  out:
216         return(err);
217 }
218
219 static void free_irq_by_cb(int (*test)(struct irq_fd *, void *), void *arg)
220 {
221         struct irq_fd **prev;
222         unsigned long flags;
223         int i = 0;
224
225         flags = irq_lock();
226         prev = &active_fds;
227         while(*prev != NULL){
228                 if((*test)(*prev, arg)){
229                         struct irq_fd *old_fd = *prev;
230                         if((pollfds[i].fd != -1) && 
231                            (pollfds[i].fd != (*prev)->fd)){
232                                 printk("free_irq_by_cb - mismatch between "
233                                        "active_fds and pollfds, fd %d vs %d\n",
234                                        (*prev)->fd, pollfds[i].fd);
235                                 goto out;
236                         }
237
238                         pollfds_num--;
239
240                         /* This moves the *whole* array after pollfds[i] (though
241                          * it doesn't spot as such)! */
242
243                         memmove(&pollfds[i], &pollfds[i + 1],
244                                (pollfds_num - i) * sizeof(pollfds[0]));
245
246                         if(last_irq_ptr == &old_fd->next) 
247                                 last_irq_ptr = prev;
248                         *prev = (*prev)->next;
249                         if(old_fd->type == IRQ_WRITE) 
250                                 ignore_sigio_fd(old_fd->fd);
251                         kfree(old_fd);
252                         continue;
253                 }
254                 prev = &(*prev)->next;
255                 i++;
256         }
257  out:
258         irq_unlock(flags);
259 }
260
261 struct irq_and_dev {
262         int irq;
263         void *dev;
264 };
265
266 static int same_irq_and_dev(struct irq_fd *irq, void *d)
267 {
268         struct irq_and_dev *data = d;
269
270         return((irq->irq == data->irq) && (irq->id == data->dev));
271 }
272
273 void free_irq_by_irq_and_dev(unsigned int irq, void *dev)
274 {
275         struct irq_and_dev data = ((struct irq_and_dev) { .irq  = irq,
276                                                           .dev  = dev });
277
278         free_irq_by_cb(same_irq_and_dev, &data);
279 }
280
281 static int same_fd(struct irq_fd *irq, void *fd)
282 {
283         return(irq->fd == *((int *) fd));
284 }
285
286 void free_irq_by_fd(int fd)
287 {
288         free_irq_by_cb(same_fd, &fd);
289 }
290
291 static struct irq_fd *find_irq_by_fd(int fd, int irqnum, int *index_out)
292 {
293         struct irq_fd *irq;
294         int i = 0;
295
296         for(irq=active_fds; irq != NULL; irq = irq->next){
297                 if((irq->fd == fd) && (irq->irq == irqnum)) break;
298                 i++;
299         }
300         if(irq == NULL){
301                 printk("find_irq_by_fd doesn't have descriptor %d\n", fd);
302                 goto out;
303         }
304         if((pollfds[i].fd != -1) && (pollfds[i].fd != fd)){
305                 printk("find_irq_by_fd - mismatch between active_fds and "
306                        "pollfds, fd %d vs %d, need %d\n", irq->fd, 
307                        pollfds[i].fd, fd);
308                 irq = NULL;
309                 goto out;
310         }
311         *index_out = i;
312  out:
313         return(irq);
314 }
315
316 void free_irq_later(int irq, void *dev_id)
317 {
318         struct irq_fd *irq_fd;
319         unsigned long flags;
320
321         flags = irq_lock();
322         for(irq_fd = active_fds; irq_fd != NULL; irq_fd = irq_fd->next){
323                 if((irq_fd->irq == irq) && (irq_fd->id == dev_id))
324                         break;
325         }
326         if(irq_fd == NULL){
327                 printk("free_irq_later found no irq, irq = %d, "
328                        "dev_id = 0x%p\n", irq, dev_id);
329                 goto out;
330         }
331         irq_fd->freed = 1;
332  out:
333         irq_unlock(flags);
334 }
335
336 void reactivate_fd(int fd, int irqnum)
337 {
338         struct irq_fd *irq;
339         unsigned long flags;
340         int i;
341
342         flags = irq_lock();
343         irq = find_irq_by_fd(fd, irqnum, &i);
344         if(irq == NULL){
345                 irq_unlock(flags);
346                 return;
347         }
348
349         pollfds[i].fd = irq->fd;
350
351         irq_unlock(flags);
352
353         /* This calls activate_fd, so it has to be outside the critical
354          * section.
355          */
356         maybe_sigio_broken(fd, irq->type);
357 }
358
359 void deactivate_fd(int fd, int irqnum)
360 {
361         struct irq_fd *irq;
362         unsigned long flags;
363         int i;
364
365         flags = irq_lock();
366         irq = find_irq_by_fd(fd, irqnum, &i);
367         if(irq == NULL)
368                 goto out;
369         pollfds[i].fd = -1;
370  out:
371         irq_unlock(flags);
372 }
373
374 int deactivate_all_fds(void)
375 {
376         struct irq_fd *irq;
377         int err;
378
379         for(irq=active_fds;irq != NULL;irq = irq->next){
380                 err = os_clear_fd_async(irq->fd);
381                 if(err)
382                         return(err);
383         }
384         /* If there is a signal already queued, after unblocking ignore it */
385         set_handler(SIGIO, SIG_IGN, 0, -1);
386
387         return(0);
388 }
389
390 void forward_ipi(int fd, int pid)
391 {
392         int err;
393
394         err = os_set_owner(fd, pid);
395         if(err < 0)
396                 printk("forward_ipi: set_owner failed, fd = %d, me = %d, "
397                        "target = %d, err = %d\n", fd, os_getpid(), pid, -err);
398 }
399
400 void forward_interrupts(int pid)
401 {
402         struct irq_fd *irq;
403         unsigned long flags;
404         int err;
405
406         flags = irq_lock();
407         for(irq=active_fds;irq != NULL;irq = irq->next){
408                 err = os_set_owner(irq->fd, pid);
409                 if(err < 0){
410                         /* XXX Just remove the irq rather than
411                          * print out an infinite stream of these
412                          */
413                         printk("Failed to forward %d to pid %d, err = %d\n",
414                                irq->fd, pid, -err);
415                 }
416
417                 irq->pid = pid;
418         }
419         irq_unlock(flags);
420 }
421
422 void init_irq_signals(int on_sigstack)
423 {
424         __sighandler_t h;
425         int flags;
426
427         flags = on_sigstack ? SA_ONSTACK : 0;
428         if(timer_irq_inited) h = (__sighandler_t) alarm_handler;
429         else h = boot_timer_handler;
430
431         set_handler(SIGVTALRM, h, flags | SA_RESTART, 
432                     SIGUSR1, SIGIO, SIGWINCH, SIGALRM, -1);
433         set_handler(SIGIO, (__sighandler_t) sig_handler, flags | SA_RESTART,
434                     SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
435         signal(SIGWINCH, SIG_IGN);
436 }
437
438 /*
439  * Overrides for Emacs so that we follow Linus's tabbing style.
440  * Emacs will notice this stuff at the end of the file and automatically
441  * adjust the settings for this buffer only.  This must remain at the end
442  * of the file.
443  * ---------------------------------------------------------------------------
444  * Local variables:
445  * c-file-style: "linux"
446  * End:
447  */