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