1 /* 2 * Copyright (c) 2017-2018 François Tigeot <ftigeot@wolfpond.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice unmodified, this list of conditions, and the following 10 * disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #ifndef _LINUX_INTERRUPT_H_ 28 #define _LINUX_INTERRUPT_H_ 29 30 #include <linux/kernel.h> 31 #include <linux/bitops.h> 32 #include <linux/cpumask.h> 33 #include <linux/irqreturn.h> 34 #include <linux/hardirq.h> 35 #include <linux/irqflags.h> 36 #include <linux/hrtimer.h> 37 #include <linux/kref.h> 38 39 #include <linux/atomic.h> 40 41 #define IRQF_SHARED 0x00000080 42 43 struct tasklet_struct { 44 unsigned long state; 45 void (*func)(unsigned long); 46 unsigned long data; 47 struct lock lock; 48 }; 49 50 enum { 51 TASKLET_STATE_SCHED, 52 TASKLET_STATE_RUN 53 }; 54 55 /* 56 * TODO: verify these points: 57 * - tasklets that have the same type cannot be run on multiple processors at the same time 58 * - tasklets always run on the processor from which they were originally 59 * submitted 60 * - when a tasklet is scheduled, its state is set to TASKLET_STATE_SCHED, and the tasklet 61 * added to a queue 62 * - during the execution of its function, the tasklet state is set to TASKLET_STATE_RUN 63 * and the TASKLET_STATE_SCHED state is removed 64 */ 65 66 /* XXX scheduling and execution should be handled separately */ 67 static inline void 68 tasklet_schedule(struct tasklet_struct *t) 69 { 70 set_bit(TASKLET_STATE_SCHED, &t->state); 71 72 lockmgr(&t->lock, LK_EXCLUSIVE); 73 clear_bit(TASKLET_STATE_SCHED, &t->state); 74 75 set_bit(TASKLET_STATE_RUN, &t->state); 76 if (t->func) 77 t->func(t->data); 78 clear_bit(TASKLET_STATE_RUN, &t->state); 79 80 lockmgr(&t->lock, LK_RELEASE); 81 } 82 83 /* This function ensures that the tasklet is not scheduled to run again */ 84 /* XXX this doesn't kill anything */ 85 static inline void 86 tasklet_kill(struct tasklet_struct *t) 87 { 88 lockmgr(&t->lock, LK_EXCLUSIVE); 89 clear_bit(TASKLET_STATE_SCHED, &t->state); 90 lockmgr(&t->lock, LK_RELEASE); 91 } 92 93 static inline void 94 tasklet_init(struct tasklet_struct *t, void (*func)(unsigned long), unsigned long data) 95 { 96 lockinit(&t->lock, "ltasklet", 0, LK_CANRECURSE); 97 t->state = 0; 98 t->func = func; 99 t->data = data; 100 } 101 102 typedef irqreturn_t (*irq_handler_t)(int, void *); 103 104 int request_irq(unsigned int irq, irq_handler_t handler, 105 unsigned long flags, const char *name, void *dev); 106 107 void free_irq(unsigned int irq, void *dev_id); 108 109 #endif /* _LINUX_INTERRUPT_H_ */ 110