1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/module.h>
3 #include <linux/interrupt.h>
4 #include <linux/irqdomain.h>
5 #include <linux/device.h>
6 #include <linux/gfp.h>
7 #include <linux/irq.h>
8
9 #include "internals.h"
10
11 /*
12 * Device resource management aware IRQ request/free implementation.
13 */
14 struct irq_devres {
15 unsigned int irq;
16 void *dev_id;
17 };
18
devm_irq_release(struct device * dev,void * res)19 static void devm_irq_release(struct device *dev, void *res)
20 {
21 struct irq_devres *this = res;
22
23 free_irq(this->irq, this->dev_id);
24 }
25
devm_irq_match(struct device * dev,void * res,void * data)26 static int devm_irq_match(struct device *dev, void *res, void *data)
27 {
28 struct irq_devres *this = res, *match = data;
29
30 return this->irq == match->irq && this->dev_id == match->dev_id;
31 }
32
33 /**
34 * devm_request_threaded_irq - allocate an interrupt line for a managed device
35 * @dev: device to request interrupt for
36 * @irq: Interrupt line to allocate
37 * @handler: Function to be called when the IRQ occurs
38 * @thread_fn: function to be called in a threaded interrupt context. NULL
39 * for devices which handle everything in @handler
40 * @irqflags: Interrupt type flags
41 * @devname: An ascii name for the claiming device, dev_name(dev) if NULL
42 * @dev_id: A cookie passed back to the handler function
43 *
44 * Except for the extra @dev argument, this function takes the
45 * same arguments and performs the same function as
46 * request_threaded_irq(). IRQs requested with this function will be
47 * automatically freed on driver detach.
48 *
49 * If an IRQ allocated with this function needs to be freed
50 * separately, devm_free_irq() must be used.
51 */
devm_request_threaded_irq(struct device * dev,unsigned int irq,irq_handler_t handler,irq_handler_t thread_fn,unsigned long irqflags,const char * devname,void * dev_id)52 int devm_request_threaded_irq(struct device *dev, unsigned int irq,
53 irq_handler_t handler, irq_handler_t thread_fn,
54 unsigned long irqflags, const char *devname,
55 void *dev_id)
56 {
57 struct irq_devres *dr;
58 int rc;
59
60 dr = devres_alloc(devm_irq_release, sizeof(struct irq_devres),
61 GFP_KERNEL);
62 if (!dr)
63 return -ENOMEM;
64
65 if (!devname)
66 devname = dev_name(dev);
67
68 rc = request_threaded_irq(irq, handler, thread_fn, irqflags, devname,
69 dev_id);
70 if (rc) {
71 devres_free(dr);
72 return rc;
73 }
74
75 dr->irq = irq;
76 dr->dev_id = dev_id;
77 devres_add(dev, dr);
78
79 return 0;
80 }
81 EXPORT_SYMBOL(devm_request_threaded_irq);
82
83 /**
84 * devm_request_any_context_irq - allocate an interrupt line for a managed device
85 * @dev: device to request interrupt for
86 * @irq: Interrupt line to allocate
87 * @handler: Function to be called when the IRQ occurs
88 * @irqflags: Interrupt type flags
89 * @devname: An ascii name for the claiming device, dev_name(dev) if NULL
90 * @dev_id: A cookie passed back to the handler function
91 *
92 * Except for the extra @dev argument, this function takes the
93 * same arguments and performs the same function as
94 * request_any_context_irq(). IRQs requested with this function will be
95 * automatically freed on driver detach.
96 *
97 * If an IRQ allocated with this function needs to be freed
98 * separately, devm_free_irq() must be used.
99 */
devm_request_any_context_irq(struct device * dev,unsigned int irq,irq_handler_t handler,unsigned long irqflags,const char * devname,void * dev_id)100 int devm_request_any_context_irq(struct device *dev, unsigned int irq,
101 irq_handler_t handler, unsigned long irqflags,
102 const char *devname, void *dev_id)
103 {
104 struct irq_devres *dr;
105 int rc;
106
107 dr = devres_alloc(devm_irq_release, sizeof(struct irq_devres),
108 GFP_KERNEL);
109 if (!dr)
110 return -ENOMEM;
111
112 if (!devname)
113 devname = dev_name(dev);
114
115 rc = request_any_context_irq(irq, handler, irqflags, devname, dev_id);
116 if (rc < 0) {
117 devres_free(dr);
118 return rc;
119 }
120
121 dr->irq = irq;
122 dr->dev_id = dev_id;
123 devres_add(dev, dr);
124
125 return rc;
126 }
127 EXPORT_SYMBOL(devm_request_any_context_irq);
128
129 /**
130 * devm_free_irq - free an interrupt
131 * @dev: device to free interrupt for
132 * @irq: Interrupt line to free
133 * @dev_id: Device identity to free
134 *
135 * Except for the extra @dev argument, this function takes the
136 * same arguments and performs the same function as free_irq().
137 * This function instead of free_irq() should be used to manually
138 * free IRQs allocated with devm_request_irq().
139 */
devm_free_irq(struct device * dev,unsigned int irq,void * dev_id)140 void devm_free_irq(struct device *dev, unsigned int irq, void *dev_id)
141 {
142 struct irq_devres match_data = { irq, dev_id };
143
144 WARN_ON(devres_destroy(dev, devm_irq_release, devm_irq_match,
145 &match_data));
146 free_irq(irq, dev_id);
147 }
148 EXPORT_SYMBOL(devm_free_irq);
149
150 struct irq_desc_devres {
151 unsigned int from;
152 unsigned int cnt;
153 };
154
devm_irq_desc_release(struct device * dev,void * res)155 static void devm_irq_desc_release(struct device *dev, void *res)
156 {
157 struct irq_desc_devres *this = res;
158
159 irq_free_descs(this->from, this->cnt);
160 }
161
162 /**
163 * __devm_irq_alloc_descs - Allocate and initialize a range of irq descriptors
164 * for a managed device
165 * @dev: Device to allocate the descriptors for
166 * @irq: Allocate for specific irq number if irq >= 0
167 * @from: Start the search from this irq number
168 * @cnt: Number of consecutive irqs to allocate
169 * @node: Preferred node on which the irq descriptor should be allocated
170 * @owner: Owning module (can be NULL)
171 * @affinity: Optional pointer to an irq_affinity_desc array of size @cnt
172 * which hints where the irq descriptors should be allocated
173 * and which default affinities to use
174 *
175 * Returns the first irq number or error code.
176 *
177 * Note: Use the provided wrappers (devm_irq_alloc_desc*) for simplicity.
178 */
__devm_irq_alloc_descs(struct device * dev,int irq,unsigned int from,unsigned int cnt,int node,struct module * owner,const struct irq_affinity_desc * affinity)179 int __devm_irq_alloc_descs(struct device *dev, int irq, unsigned int from,
180 unsigned int cnt, int node, struct module *owner,
181 const struct irq_affinity_desc *affinity)
182 {
183 struct irq_desc_devres *dr;
184 int base;
185
186 dr = devres_alloc(devm_irq_desc_release, sizeof(*dr), GFP_KERNEL);
187 if (!dr)
188 return -ENOMEM;
189
190 base = __irq_alloc_descs(irq, from, cnt, node, owner, affinity);
191 if (base < 0) {
192 devres_free(dr);
193 return base;
194 }
195
196 dr->from = base;
197 dr->cnt = cnt;
198 devres_add(dev, dr);
199
200 return base;
201 }
202 EXPORT_SYMBOL_GPL(__devm_irq_alloc_descs);
203
204 #ifdef CONFIG_GENERIC_IRQ_CHIP
205 /**
206 * devm_irq_alloc_generic_chip - Allocate and initialize a generic chip
207 * for a managed device
208 * @dev: Device to allocate the generic chip for
209 * @name: Name of the irq chip
210 * @num_ct: Number of irq_chip_type instances associated with this
211 * @irq_base: Interrupt base nr for this chip
212 * @reg_base: Register base address (virtual)
213 * @handler: Default flow handler associated with this chip
214 *
215 * Returns an initialized irq_chip_generic structure. The chip defaults
216 * to the primary (index 0) irq_chip_type and @handler
217 */
218 struct irq_chip_generic *
devm_irq_alloc_generic_chip(struct device * dev,const char * name,int num_ct,unsigned int irq_base,void __iomem * reg_base,irq_flow_handler_t handler)219 devm_irq_alloc_generic_chip(struct device *dev, const char *name, int num_ct,
220 unsigned int irq_base, void __iomem *reg_base,
221 irq_flow_handler_t handler)
222 {
223 struct irq_chip_generic *gc;
224
225 gc = devm_kzalloc(dev, struct_size(gc, chip_types, num_ct), GFP_KERNEL);
226 if (gc)
227 irq_init_generic_chip(gc, name, num_ct,
228 irq_base, reg_base, handler);
229
230 return gc;
231 }
232 EXPORT_SYMBOL_GPL(devm_irq_alloc_generic_chip);
233
234 struct irq_generic_chip_devres {
235 struct irq_chip_generic *gc;
236 u32 msk;
237 unsigned int clr;
238 unsigned int set;
239 };
240
devm_irq_remove_generic_chip(struct device * dev,void * res)241 static void devm_irq_remove_generic_chip(struct device *dev, void *res)
242 {
243 struct irq_generic_chip_devres *this = res;
244
245 irq_remove_generic_chip(this->gc, this->msk, this->clr, this->set);
246 }
247
248 /**
249 * devm_irq_setup_generic_chip - Setup a range of interrupts with a generic
250 * chip for a managed device
251 *
252 * @dev: Device to setup the generic chip for
253 * @gc: Generic irq chip holding all data
254 * @msk: Bitmask holding the irqs to initialize relative to gc->irq_base
255 * @flags: Flags for initialization
256 * @clr: IRQ_* bits to clear
257 * @set: IRQ_* bits to set
258 *
259 * Set up max. 32 interrupts starting from gc->irq_base. Note, this
260 * initializes all interrupts to the primary irq_chip_type and its
261 * associated handler.
262 */
devm_irq_setup_generic_chip(struct device * dev,struct irq_chip_generic * gc,u32 msk,enum irq_gc_flags flags,unsigned int clr,unsigned int set)263 int devm_irq_setup_generic_chip(struct device *dev, struct irq_chip_generic *gc,
264 u32 msk, enum irq_gc_flags flags,
265 unsigned int clr, unsigned int set)
266 {
267 struct irq_generic_chip_devres *dr;
268
269 dr = devres_alloc(devm_irq_remove_generic_chip,
270 sizeof(*dr), GFP_KERNEL);
271 if (!dr)
272 return -ENOMEM;
273
274 irq_setup_generic_chip(gc, msk, flags, clr, set);
275
276 dr->gc = gc;
277 dr->msk = msk;
278 dr->clr = clr;
279 dr->set = set;
280 devres_add(dev, dr);
281
282 return 0;
283 }
284 EXPORT_SYMBOL_GPL(devm_irq_setup_generic_chip);
285 #endif /* CONFIG_GENERIC_IRQ_CHIP */
286
287 #ifdef CONFIG_IRQ_DOMAIN
devm_irq_domain_remove(struct device * dev,void * res)288 static void devm_irq_domain_remove(struct device *dev, void *res)
289 {
290 struct irq_domain **domain = res;
291
292 irq_domain_remove(*domain);
293 }
294
295 /**
296 * devm_irq_domain_instantiate() - Instantiate a new irq domain data for a
297 * managed device.
298 * @dev: Device to instantiate the domain for
299 * @info: Domain information pointer pointing to the information for this
300 * domain
301 *
302 * Return: A pointer to the instantiated irq domain or an ERR_PTR value.
303 */
devm_irq_domain_instantiate(struct device * dev,const struct irq_domain_info * info)304 struct irq_domain *devm_irq_domain_instantiate(struct device *dev,
305 const struct irq_domain_info *info)
306 {
307 struct irq_domain *domain;
308 struct irq_domain **dr;
309
310 dr = devres_alloc(devm_irq_domain_remove, sizeof(*dr), GFP_KERNEL);
311 if (!dr)
312 return ERR_PTR(-ENOMEM);
313
314 domain = irq_domain_instantiate(info);
315 if (!IS_ERR(domain)) {
316 *dr = domain;
317 devres_add(dev, dr);
318 } else {
319 devres_free(dr);
320 }
321
322 return domain;
323 }
324 EXPORT_SYMBOL_GPL(devm_irq_domain_instantiate);
325 #endif /* CONFIG_IRQ_DOMAIN */
326