xref: /freebsd/sys/compat/linuxkpi/common/include/linux/interrupt.h (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2013-2015 Mellanox Technologies, Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 #ifndef	_LINUX_INTERRUPT_H_
32 #define	_LINUX_INTERRUPT_H_
33 
34 #include <linux/device.h>
35 #include <linux/pci.h>
36 #include <linux/irqreturn.h>
37 
38 #include <sys/bus.h>
39 #include <sys/rman.h>
40 
41 typedef	irqreturn_t	(*irq_handler_t)(int, void *);
42 
43 #define	IRQF_SHARED	RF_SHAREABLE
44 
45 struct irq_ent {
46 	struct list_head	links;
47 	struct device	*dev;
48 	struct resource	*res;
49 	void		*arg;
50 	irqreturn_t	(*handler)(int, void *);
51 	void		*tag;
52 	unsigned int	irq;
53 };
54 
55 static inline int
56 linux_irq_rid(struct device *dev, unsigned int irq)
57 {
58 	/* check for MSI- or MSIX- interrupt */
59 	if (irq >= dev->irq_start && irq < dev->irq_end)
60 		return (irq - dev->irq_start + 1);
61 	else
62 		return (0);
63 }
64 
65 extern void linux_irq_handler(void *);
66 
67 static inline struct irq_ent *
68 linux_irq_ent(struct device *dev, unsigned int irq)
69 {
70 	struct irq_ent *irqe;
71 
72 	list_for_each_entry(irqe, &dev->irqents, links)
73 		if (irqe->irq == irq)
74 			return (irqe);
75 
76 	return (NULL);
77 }
78 
79 static inline int
80 request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,
81     const char *name, void *arg)
82 {
83 	struct resource *res;
84 	struct irq_ent *irqe;
85 	struct device *dev;
86 	int error;
87 	int rid;
88 
89 	dev = linux_pci_find_irq_dev(irq);
90 	if (dev == NULL)
91 		return -ENXIO;
92 	rid = linux_irq_rid(dev, irq);
93 	res = bus_alloc_resource_any(dev->bsddev, SYS_RES_IRQ, &rid,
94 	    flags | RF_ACTIVE);
95 	if (res == NULL)
96 		return (-ENXIO);
97 	irqe = kmalloc(sizeof(*irqe), GFP_KERNEL);
98 	irqe->dev = dev;
99 	irqe->res = res;
100 	irqe->arg = arg;
101 	irqe->handler = handler;
102 	irqe->irq = irq;
103 	error = bus_setup_intr(dev->bsddev, res, INTR_TYPE_NET | INTR_MPSAFE,
104 	    NULL, linux_irq_handler, irqe, &irqe->tag);
105 	if (error) {
106 		bus_release_resource(dev->bsddev, SYS_RES_IRQ, rid, irqe->res);
107 		kfree(irqe);
108 		return (-error);
109 	}
110 	list_add(&irqe->links, &dev->irqents);
111 
112 	return 0;
113 }
114 
115 static inline int
116 enable_irq(unsigned int irq)
117 {
118 	struct irq_ent *irqe;
119 	struct device *dev;
120 
121 	dev = linux_pci_find_irq_dev(irq);
122 	if (dev == NULL)
123 		return -EINVAL;
124 	irqe = linux_irq_ent(dev, irq);
125 	if (irqe == NULL || irqe->tag != NULL)
126 		return -EINVAL;
127 	return -bus_setup_intr(dev->bsddev, irqe->res, INTR_TYPE_NET | INTR_MPSAFE,
128 	    NULL, linux_irq_handler, irqe, &irqe->tag);
129 }
130 
131 static inline void
132 disable_irq(unsigned int irq)
133 {
134 	struct irq_ent *irqe;
135 	struct device *dev;
136 
137 	dev = linux_pci_find_irq_dev(irq);
138 	if (dev == NULL)
139 		return;
140 	irqe = linux_irq_ent(dev, irq);
141 	if (irqe == NULL)
142 		return;
143 	if (irqe->tag != NULL)
144 		bus_teardown_intr(dev->bsddev, irqe->res, irqe->tag);
145 	irqe->tag = NULL;
146 }
147 
148 static inline int
149 bind_irq_to_cpu(unsigned int irq, int cpu_id)
150 {
151 	struct irq_ent *irqe;
152 	struct device *dev;
153 
154 	dev = linux_pci_find_irq_dev(irq);
155 	if (dev == NULL)
156 		return (-ENOENT);
157 
158 	irqe = linux_irq_ent(dev, irq);
159 	if (irqe == NULL)
160 		return (-ENOENT);
161 
162 	return (-bus_bind_intr(dev->bsddev, irqe->res, cpu_id));
163 }
164 
165 static inline void
166 free_irq(unsigned int irq, void *device)
167 {
168 	struct irq_ent *irqe;
169 	struct device *dev;
170 	int rid;
171 
172 	dev = linux_pci_find_irq_dev(irq);
173 	if (dev == NULL)
174 		return;
175 	rid = linux_irq_rid(dev, irq);
176 	irqe = linux_irq_ent(dev, irq);
177 	if (irqe == NULL)
178 		return;
179 	if (irqe->tag != NULL)
180 		bus_teardown_intr(dev->bsddev, irqe->res, irqe->tag);
181 	bus_release_resource(dev->bsddev, SYS_RES_IRQ, rid, irqe->res);
182 	list_del(&irqe->links);
183 	kfree(irqe);
184 }
185 
186 /*
187  * LinuxKPI tasklet support
188  */
189 typedef void tasklet_func_t(unsigned long);
190 
191 struct tasklet_struct {
192 	TAILQ_ENTRY(tasklet_struct) entry;
193 	tasklet_func_t *func;
194 	/* Our "state" implementation is different. Avoid same name as Linux. */
195 	volatile u_int tasklet_state;
196 	atomic_t count;
197 	unsigned long data;
198 };
199 
200 #define	DECLARE_TASKLET(_name, _func, _data)	\
201 struct tasklet_struct _name = { .func = (_func), .data = (_data) }
202 
203 #define	tasklet_hi_schedule(t)	tasklet_schedule(t)
204 
205 extern void tasklet_schedule(struct tasklet_struct *);
206 extern void tasklet_kill(struct tasklet_struct *);
207 extern void tasklet_init(struct tasklet_struct *, tasklet_func_t *,
208     unsigned long data);
209 extern void tasklet_enable(struct tasklet_struct *);
210 extern void tasklet_disable(struct tasklet_struct *);
211 extern int tasklet_trylock(struct tasklet_struct *);
212 extern void tasklet_unlock(struct tasklet_struct *);
213 extern void tasklet_unlock_wait(struct tasklet_struct *ts);
214 
215 #endif	/* _LINUX_INTERRUPT_H_ */
216