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