1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Library implementing the most common irq chip callback functions
4 *
5 * Copyright (C) 2011, Thomas Gleixner
6 */
7 #include <linux/io.h>
8 #include <linux/irq.h>
9 #include <linux/slab.h>
10 #include <linux/export.h>
11 #include <linux/irqdomain.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel_stat.h>
14 #include <linux/syscore_ops.h>
15
16 #include "internals.h"
17
18 static LIST_HEAD(gc_list);
19 static DEFINE_RAW_SPINLOCK(gc_lock);
20
21 /**
22 * irq_gc_noop - NOOP function
23 * @d: irq_data
24 */
irq_gc_noop(struct irq_data * d)25 void irq_gc_noop(struct irq_data *d)
26 {
27 }
28 EXPORT_SYMBOL_GPL(irq_gc_noop);
29
30 /**
31 * irq_gc_mask_disable_reg - Mask chip via disable register
32 * @d: irq_data
33 *
34 * Chip has separate enable/disable registers instead of a single mask
35 * register.
36 */
irq_gc_mask_disable_reg(struct irq_data * d)37 void irq_gc_mask_disable_reg(struct irq_data *d)
38 {
39 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
40 struct irq_chip_type *ct = irq_data_get_chip_type(d);
41 u32 mask = d->mask;
42
43 irq_gc_lock(gc);
44 irq_reg_writel(gc, mask, ct->regs.disable);
45 *ct->mask_cache &= ~mask;
46 irq_gc_unlock(gc);
47 }
48 EXPORT_SYMBOL_GPL(irq_gc_mask_disable_reg);
49
50 /**
51 * irq_gc_mask_set_bit - Mask chip via setting bit in mask register
52 * @d: irq_data
53 *
54 * Chip has a single mask register. Values of this register are cached
55 * and protected by gc->lock
56 */
irq_gc_mask_set_bit(struct irq_data * d)57 void irq_gc_mask_set_bit(struct irq_data *d)
58 {
59 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
60 struct irq_chip_type *ct = irq_data_get_chip_type(d);
61 u32 mask = d->mask;
62
63 irq_gc_lock(gc);
64 *ct->mask_cache |= mask;
65 irq_reg_writel(gc, *ct->mask_cache, ct->regs.mask);
66 irq_gc_unlock(gc);
67 }
68 EXPORT_SYMBOL_GPL(irq_gc_mask_set_bit);
69
70 /**
71 * irq_gc_mask_clr_bit - Mask chip via clearing bit in mask register
72 * @d: irq_data
73 *
74 * Chip has a single mask register. Values of this register are cached
75 * and protected by gc->lock
76 */
irq_gc_mask_clr_bit(struct irq_data * d)77 void irq_gc_mask_clr_bit(struct irq_data *d)
78 {
79 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
80 struct irq_chip_type *ct = irq_data_get_chip_type(d);
81 u32 mask = d->mask;
82
83 irq_gc_lock(gc);
84 *ct->mask_cache &= ~mask;
85 irq_reg_writel(gc, *ct->mask_cache, ct->regs.mask);
86 irq_gc_unlock(gc);
87 }
88 EXPORT_SYMBOL_GPL(irq_gc_mask_clr_bit);
89
90 /**
91 * irq_gc_unmask_enable_reg - Unmask chip via enable register
92 * @d: irq_data
93 *
94 * Chip has separate enable/disable registers instead of a single mask
95 * register.
96 */
irq_gc_unmask_enable_reg(struct irq_data * d)97 void irq_gc_unmask_enable_reg(struct irq_data *d)
98 {
99 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
100 struct irq_chip_type *ct = irq_data_get_chip_type(d);
101 u32 mask = d->mask;
102
103 irq_gc_lock(gc);
104 irq_reg_writel(gc, mask, ct->regs.enable);
105 *ct->mask_cache |= mask;
106 irq_gc_unlock(gc);
107 }
108 EXPORT_SYMBOL_GPL(irq_gc_unmask_enable_reg);
109
110 /**
111 * irq_gc_ack_set_bit - Ack pending interrupt via setting bit
112 * @d: irq_data
113 */
irq_gc_ack_set_bit(struct irq_data * d)114 void irq_gc_ack_set_bit(struct irq_data *d)
115 {
116 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
117 struct irq_chip_type *ct = irq_data_get_chip_type(d);
118 u32 mask = d->mask;
119
120 irq_gc_lock(gc);
121 irq_reg_writel(gc, mask, ct->regs.ack);
122 irq_gc_unlock(gc);
123 }
124 EXPORT_SYMBOL_GPL(irq_gc_ack_set_bit);
125
126 /**
127 * irq_gc_ack_clr_bit - Ack pending interrupt via clearing bit
128 * @d: irq_data
129 */
irq_gc_ack_clr_bit(struct irq_data * d)130 void irq_gc_ack_clr_bit(struct irq_data *d)
131 {
132 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
133 struct irq_chip_type *ct = irq_data_get_chip_type(d);
134 u32 mask = ~d->mask;
135
136 irq_gc_lock(gc);
137 irq_reg_writel(gc, mask, ct->regs.ack);
138 irq_gc_unlock(gc);
139 }
140
141 /**
142 * irq_gc_mask_disable_and_ack_set - Mask and ack pending interrupt
143 * @d: irq_data
144 *
145 * This generic implementation of the irq_mask_ack method is for chips
146 * with separate enable/disable registers instead of a single mask
147 * register and where a pending interrupt is acknowledged by setting a
148 * bit.
149 *
150 * Note: This is the only permutation currently used. Similar generic
151 * functions should be added here if other permutations are required.
152 */
irq_gc_mask_disable_and_ack_set(struct irq_data * d)153 void irq_gc_mask_disable_and_ack_set(struct irq_data *d)
154 {
155 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
156 struct irq_chip_type *ct = irq_data_get_chip_type(d);
157 u32 mask = d->mask;
158
159 irq_gc_lock(gc);
160 irq_reg_writel(gc, mask, ct->regs.disable);
161 *ct->mask_cache &= ~mask;
162 irq_reg_writel(gc, mask, ct->regs.ack);
163 irq_gc_unlock(gc);
164 }
165 EXPORT_SYMBOL_GPL(irq_gc_mask_disable_and_ack_set);
166
167 /**
168 * irq_gc_eoi - EOI interrupt
169 * @d: irq_data
170 */
irq_gc_eoi(struct irq_data * d)171 void irq_gc_eoi(struct irq_data *d)
172 {
173 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
174 struct irq_chip_type *ct = irq_data_get_chip_type(d);
175 u32 mask = d->mask;
176
177 irq_gc_lock(gc);
178 irq_reg_writel(gc, mask, ct->regs.eoi);
179 irq_gc_unlock(gc);
180 }
181
182 /**
183 * irq_gc_set_wake - Set/clr wake bit for an interrupt
184 * @d: irq_data
185 * @on: Indicates whether the wake bit should be set or cleared
186 *
187 * For chips where the wake from suspend functionality is not
188 * configured in a separate register and the wakeup active state is
189 * just stored in a bitmask.
190 */
irq_gc_set_wake(struct irq_data * d,unsigned int on)191 int irq_gc_set_wake(struct irq_data *d, unsigned int on)
192 {
193 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
194 u32 mask = d->mask;
195
196 if (!(mask & gc->wake_enabled))
197 return -EINVAL;
198
199 irq_gc_lock(gc);
200 if (on)
201 gc->wake_active |= mask;
202 else
203 gc->wake_active &= ~mask;
204 irq_gc_unlock(gc);
205 return 0;
206 }
207 EXPORT_SYMBOL_GPL(irq_gc_set_wake);
208
irq_readl_be(void __iomem * addr)209 static u32 irq_readl_be(void __iomem *addr)
210 {
211 return ioread32be(addr);
212 }
213
irq_writel_be(u32 val,void __iomem * addr)214 static void irq_writel_be(u32 val, void __iomem *addr)
215 {
216 iowrite32be(val, addr);
217 }
218
irq_init_generic_chip(struct irq_chip_generic * gc,const char * name,int num_ct,unsigned int irq_base,void __iomem * reg_base,irq_flow_handler_t handler)219 void irq_init_generic_chip(struct irq_chip_generic *gc, const char *name,
220 int num_ct, unsigned int irq_base,
221 void __iomem *reg_base, irq_flow_handler_t handler)
222 {
223 struct irq_chip_type *ct = gc->chip_types;
224 int i;
225
226 raw_spin_lock_init(&gc->lock);
227 gc->num_ct = num_ct;
228 gc->irq_base = irq_base;
229 gc->reg_base = reg_base;
230 for (i = 0; i < num_ct; i++)
231 ct[i].chip.name = name;
232 gc->chip_types->handler = handler;
233 }
234
235 /**
236 * irq_alloc_generic_chip - Allocate a generic chip and initialize it
237 * @name: Name of the irq chip
238 * @num_ct: Number of irq_chip_type instances associated with this
239 * @irq_base: Interrupt base nr for this chip
240 * @reg_base: Register base address (virtual)
241 * @handler: Default flow handler associated with this chip
242 *
243 * Returns an initialized irq_chip_generic structure. The chip defaults
244 * to the primary (index 0) irq_chip_type and @handler
245 */
246 struct irq_chip_generic *
irq_alloc_generic_chip(const char * name,int num_ct,unsigned int irq_base,void __iomem * reg_base,irq_flow_handler_t handler)247 irq_alloc_generic_chip(const char *name, int num_ct, unsigned int irq_base,
248 void __iomem *reg_base, irq_flow_handler_t handler)
249 {
250 struct irq_chip_generic *gc;
251
252 gc = kzalloc(struct_size(gc, chip_types, num_ct), GFP_KERNEL);
253 if (gc) {
254 irq_init_generic_chip(gc, name, num_ct, irq_base, reg_base,
255 handler);
256 }
257 return gc;
258 }
259 EXPORT_SYMBOL_GPL(irq_alloc_generic_chip);
260
261 static void
irq_gc_init_mask_cache(struct irq_chip_generic * gc,enum irq_gc_flags flags)262 irq_gc_init_mask_cache(struct irq_chip_generic *gc, enum irq_gc_flags flags)
263 {
264 struct irq_chip_type *ct = gc->chip_types;
265 u32 *mskptr = &gc->mask_cache, mskreg = ct->regs.mask;
266 int i;
267
268 for (i = 0; i < gc->num_ct; i++) {
269 if (flags & IRQ_GC_MASK_CACHE_PER_TYPE) {
270 mskptr = &ct[i].mask_cache_priv;
271 mskreg = ct[i].regs.mask;
272 }
273 ct[i].mask_cache = mskptr;
274 if (flags & IRQ_GC_INIT_MASK_CACHE)
275 *mskptr = irq_reg_readl(gc, mskreg);
276 }
277 }
278
279 /**
280 * irq_domain_alloc_generic_chips - Allocate generic chips for an irq domain
281 * @d: irq domain for which to allocate chips
282 * @info: Generic chip information
283 *
284 * Return: 0 on success, negative error code on failure
285 */
irq_domain_alloc_generic_chips(struct irq_domain * d,const struct irq_domain_chip_generic_info * info)286 int irq_domain_alloc_generic_chips(struct irq_domain *d,
287 const struct irq_domain_chip_generic_info *info)
288 {
289 struct irq_domain_chip_generic *dgc;
290 struct irq_chip_generic *gc;
291 unsigned long flags;
292 int numchips, i;
293 size_t dgc_sz;
294 size_t gc_sz;
295 size_t sz;
296 void *tmp;
297 int ret;
298
299 if (d->gc)
300 return -EBUSY;
301
302 numchips = DIV_ROUND_UP(d->revmap_size, info->irqs_per_chip);
303 if (!numchips)
304 return -EINVAL;
305
306 /* Allocate a pointer, generic chip and chiptypes for each chip */
307 gc_sz = struct_size(gc, chip_types, info->num_ct);
308 dgc_sz = struct_size(dgc, gc, numchips);
309 sz = dgc_sz + numchips * gc_sz;
310
311 tmp = dgc = kzalloc(sz, GFP_KERNEL);
312 if (!dgc)
313 return -ENOMEM;
314 dgc->irqs_per_chip = info->irqs_per_chip;
315 dgc->num_chips = numchips;
316 dgc->irq_flags_to_set = info->irq_flags_to_set;
317 dgc->irq_flags_to_clear = info->irq_flags_to_clear;
318 dgc->gc_flags = info->gc_flags;
319 dgc->exit = info->exit;
320 d->gc = dgc;
321
322 /* Calc pointer to the first generic chip */
323 tmp += dgc_sz;
324 for (i = 0; i < numchips; i++) {
325 /* Store the pointer to the generic chip */
326 dgc->gc[i] = gc = tmp;
327 irq_init_generic_chip(gc, info->name, info->num_ct,
328 i * dgc->irqs_per_chip, NULL,
329 info->handler);
330
331 gc->domain = d;
332 if (dgc->gc_flags & IRQ_GC_BE_IO) {
333 gc->reg_readl = &irq_readl_be;
334 gc->reg_writel = &irq_writel_be;
335 }
336
337 if (info->init) {
338 ret = info->init(gc);
339 if (ret)
340 goto err;
341 }
342
343 raw_spin_lock_irqsave(&gc_lock, flags);
344 list_add_tail(&gc->list, &gc_list);
345 raw_spin_unlock_irqrestore(&gc_lock, flags);
346 /* Calc pointer to the next generic chip */
347 tmp += gc_sz;
348 }
349 return 0;
350
351 err:
352 while (i--) {
353 if (dgc->exit)
354 dgc->exit(dgc->gc[i]);
355 irq_remove_generic_chip(dgc->gc[i], ~0U, 0, 0);
356 }
357 d->gc = NULL;
358 kfree(dgc);
359 return ret;
360 }
361 EXPORT_SYMBOL_GPL(irq_domain_alloc_generic_chips);
362
363 /**
364 * irq_domain_remove_generic_chips - Remove generic chips from an irq domain
365 * @d: irq domain for which generic chips are to be removed
366 */
irq_domain_remove_generic_chips(struct irq_domain * d)367 void irq_domain_remove_generic_chips(struct irq_domain *d)
368 {
369 struct irq_domain_chip_generic *dgc = d->gc;
370 unsigned int i;
371
372 if (!dgc)
373 return;
374
375 for (i = 0; i < dgc->num_chips; i++) {
376 if (dgc->exit)
377 dgc->exit(dgc->gc[i]);
378 irq_remove_generic_chip(dgc->gc[i], ~0U, 0, 0);
379 }
380 d->gc = NULL;
381 kfree(dgc);
382 }
383 EXPORT_SYMBOL_GPL(irq_domain_remove_generic_chips);
384
385 /**
386 * __irq_alloc_domain_generic_chips - Allocate generic chips for an irq domain
387 * @d: irq domain for which to allocate chips
388 * @irqs_per_chip: Number of interrupts each chip handles (max 32)
389 * @num_ct: Number of irq_chip_type instances associated with this
390 * @name: Name of the irq chip
391 * @handler: Default flow handler associated with these chips
392 * @clr: IRQ_* bits to clear in the mapping function
393 * @set: IRQ_* bits to set in the mapping function
394 * @gcflags: Generic chip specific setup flags
395 */
__irq_alloc_domain_generic_chips(struct irq_domain * d,int irqs_per_chip,int num_ct,const char * name,irq_flow_handler_t handler,unsigned int clr,unsigned int set,enum irq_gc_flags gcflags)396 int __irq_alloc_domain_generic_chips(struct irq_domain *d, int irqs_per_chip,
397 int num_ct, const char *name,
398 irq_flow_handler_t handler,
399 unsigned int clr, unsigned int set,
400 enum irq_gc_flags gcflags)
401 {
402 struct irq_domain_chip_generic_info info = {
403 .irqs_per_chip = irqs_per_chip,
404 .num_ct = num_ct,
405 .name = name,
406 .handler = handler,
407 .irq_flags_to_clear = clr,
408 .irq_flags_to_set = set,
409 .gc_flags = gcflags,
410 };
411
412 return irq_domain_alloc_generic_chips(d, &info);
413 }
414 EXPORT_SYMBOL_GPL(__irq_alloc_domain_generic_chips);
415
416 static struct irq_chip_generic *
__irq_get_domain_generic_chip(struct irq_domain * d,unsigned int hw_irq)417 __irq_get_domain_generic_chip(struct irq_domain *d, unsigned int hw_irq)
418 {
419 struct irq_domain_chip_generic *dgc = d->gc;
420 int idx;
421
422 if (!dgc)
423 return ERR_PTR(-ENODEV);
424 idx = hw_irq / dgc->irqs_per_chip;
425 if (idx >= dgc->num_chips)
426 return ERR_PTR(-EINVAL);
427 return dgc->gc[idx];
428 }
429
430 /**
431 * irq_get_domain_generic_chip - Get a pointer to the generic chip of a hw_irq
432 * @d: irq domain pointer
433 * @hw_irq: Hardware interrupt number
434 */
435 struct irq_chip_generic *
irq_get_domain_generic_chip(struct irq_domain * d,unsigned int hw_irq)436 irq_get_domain_generic_chip(struct irq_domain *d, unsigned int hw_irq)
437 {
438 struct irq_chip_generic *gc = __irq_get_domain_generic_chip(d, hw_irq);
439
440 return !IS_ERR(gc) ? gc : NULL;
441 }
442 EXPORT_SYMBOL_GPL(irq_get_domain_generic_chip);
443
444 /*
445 * Separate lockdep classes for interrupt chip which can nest irq_desc
446 * lock and request mutex.
447 */
448 static struct lock_class_key irq_nested_lock_class;
449 static struct lock_class_key irq_nested_request_class;
450
451 /*
452 * irq_map_generic_chip - Map a generic chip for an irq domain
453 */
irq_map_generic_chip(struct irq_domain * d,unsigned int virq,irq_hw_number_t hw_irq)454 int irq_map_generic_chip(struct irq_domain *d, unsigned int virq,
455 irq_hw_number_t hw_irq)
456 {
457 struct irq_data *data = irq_domain_get_irq_data(d, virq);
458 struct irq_domain_chip_generic *dgc = d->gc;
459 struct irq_chip_generic *gc;
460 struct irq_chip_type *ct;
461 struct irq_chip *chip;
462 unsigned long flags;
463 int idx;
464
465 gc = __irq_get_domain_generic_chip(d, hw_irq);
466 if (IS_ERR(gc))
467 return PTR_ERR(gc);
468
469 idx = hw_irq % dgc->irqs_per_chip;
470
471 if (test_bit(idx, &gc->unused))
472 return -ENOTSUPP;
473
474 if (test_bit(idx, &gc->installed))
475 return -EBUSY;
476
477 ct = gc->chip_types;
478 chip = &ct->chip;
479
480 /* We only init the cache for the first mapping of a generic chip */
481 if (!gc->installed) {
482 raw_spin_lock_irqsave(&gc->lock, flags);
483 irq_gc_init_mask_cache(gc, dgc->gc_flags);
484 raw_spin_unlock_irqrestore(&gc->lock, flags);
485 }
486
487 /* Mark the interrupt as installed */
488 set_bit(idx, &gc->installed);
489
490 if (dgc->gc_flags & IRQ_GC_INIT_NESTED_LOCK)
491 irq_set_lockdep_class(virq, &irq_nested_lock_class,
492 &irq_nested_request_class);
493
494 if (chip->irq_calc_mask)
495 chip->irq_calc_mask(data);
496 else
497 data->mask = 1 << idx;
498
499 irq_domain_set_info(d, virq, hw_irq, chip, gc, ct->handler, NULL, NULL);
500 irq_modify_status(virq, dgc->irq_flags_to_clear, dgc->irq_flags_to_set);
501 return 0;
502 }
503
irq_unmap_generic_chip(struct irq_domain * d,unsigned int virq)504 void irq_unmap_generic_chip(struct irq_domain *d, unsigned int virq)
505 {
506 struct irq_data *data = irq_domain_get_irq_data(d, virq);
507 struct irq_domain_chip_generic *dgc = d->gc;
508 unsigned int hw_irq = data->hwirq;
509 struct irq_chip_generic *gc;
510 int irq_idx;
511
512 gc = irq_get_domain_generic_chip(d, hw_irq);
513 if (!gc)
514 return;
515
516 irq_idx = hw_irq % dgc->irqs_per_chip;
517
518 clear_bit(irq_idx, &gc->installed);
519 irq_domain_set_info(d, virq, hw_irq, &no_irq_chip, NULL, NULL, NULL,
520 NULL);
521
522 }
523
524 const struct irq_domain_ops irq_generic_chip_ops = {
525 .map = irq_map_generic_chip,
526 .unmap = irq_unmap_generic_chip,
527 .xlate = irq_domain_xlate_onetwocell,
528 };
529 EXPORT_SYMBOL_GPL(irq_generic_chip_ops);
530
531 /**
532 * irq_setup_generic_chip - Setup a range of interrupts with a generic chip
533 * @gc: Generic irq chip holding all data
534 * @msk: Bitmask holding the irqs to initialize relative to gc->irq_base
535 * @flags: Flags for initialization
536 * @clr: IRQ_* bits to clear
537 * @set: IRQ_* bits to set
538 *
539 * Set up max. 32 interrupts starting from gc->irq_base. Note, this
540 * initializes all interrupts to the primary irq_chip_type and its
541 * associated handler.
542 */
irq_setup_generic_chip(struct irq_chip_generic * gc,u32 msk,enum irq_gc_flags flags,unsigned int clr,unsigned int set)543 void irq_setup_generic_chip(struct irq_chip_generic *gc, u32 msk,
544 enum irq_gc_flags flags, unsigned int clr,
545 unsigned int set)
546 {
547 struct irq_chip_type *ct = gc->chip_types;
548 struct irq_chip *chip = &ct->chip;
549 unsigned int i;
550
551 raw_spin_lock(&gc_lock);
552 list_add_tail(&gc->list, &gc_list);
553 raw_spin_unlock(&gc_lock);
554
555 irq_gc_init_mask_cache(gc, flags);
556
557 for (i = gc->irq_base; msk; msk >>= 1, i++) {
558 if (!(msk & 0x01))
559 continue;
560
561 if (flags & IRQ_GC_INIT_NESTED_LOCK)
562 irq_set_lockdep_class(i, &irq_nested_lock_class,
563 &irq_nested_request_class);
564
565 if (!(flags & IRQ_GC_NO_MASK)) {
566 struct irq_data *d = irq_get_irq_data(i);
567
568 if (chip->irq_calc_mask)
569 chip->irq_calc_mask(d);
570 else
571 d->mask = 1 << (i - gc->irq_base);
572 }
573 irq_set_chip_and_handler(i, chip, ct->handler);
574 irq_set_chip_data(i, gc);
575 irq_modify_status(i, clr, set);
576 }
577 gc->irq_cnt = i - gc->irq_base;
578 }
579 EXPORT_SYMBOL_GPL(irq_setup_generic_chip);
580
581 /**
582 * irq_setup_alt_chip - Switch to alternative chip
583 * @d: irq_data for this interrupt
584 * @type: Flow type to be initialized
585 *
586 * Only to be called from chip->irq_set_type() callbacks.
587 */
irq_setup_alt_chip(struct irq_data * d,unsigned int type)588 int irq_setup_alt_chip(struct irq_data *d, unsigned int type)
589 {
590 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
591 struct irq_chip_type *ct = gc->chip_types;
592 unsigned int i;
593
594 for (i = 0; i < gc->num_ct; i++, ct++) {
595 if (ct->type & type) {
596 d->chip = &ct->chip;
597 irq_data_to_desc(d)->handle_irq = ct->handler;
598 return 0;
599 }
600 }
601 return -EINVAL;
602 }
603 EXPORT_SYMBOL_GPL(irq_setup_alt_chip);
604
605 /**
606 * irq_remove_generic_chip - Remove a chip
607 * @gc: Generic irq chip holding all data
608 * @msk: Bitmask holding the irqs to initialize relative to gc->irq_base
609 * @clr: IRQ_* bits to clear
610 * @set: IRQ_* bits to set
611 *
612 * Remove up to 32 interrupts starting from gc->irq_base.
613 */
irq_remove_generic_chip(struct irq_chip_generic * gc,u32 msk,unsigned int clr,unsigned int set)614 void irq_remove_generic_chip(struct irq_chip_generic *gc, u32 msk,
615 unsigned int clr, unsigned int set)
616 {
617 unsigned int i, virq;
618
619 raw_spin_lock(&gc_lock);
620 list_del(&gc->list);
621 raw_spin_unlock(&gc_lock);
622
623 for (i = 0; msk; msk >>= 1, i++) {
624 if (!(msk & 0x01))
625 continue;
626
627 /*
628 * Interrupt domain based chips store the base hardware
629 * interrupt number in gc::irq_base. Otherwise gc::irq_base
630 * contains the base Linux interrupt number.
631 */
632 if (gc->domain) {
633 virq = irq_find_mapping(gc->domain, gc->irq_base + i);
634 if (!virq)
635 continue;
636 } else {
637 virq = gc->irq_base + i;
638 }
639
640 /* Remove handler first. That will mask the irq line */
641 irq_set_handler(virq, NULL);
642 irq_set_chip(virq, &no_irq_chip);
643 irq_set_chip_data(virq, NULL);
644 irq_modify_status(virq, clr, set);
645 }
646 }
647 EXPORT_SYMBOL_GPL(irq_remove_generic_chip);
648
irq_gc_get_irq_data(struct irq_chip_generic * gc)649 static struct irq_data *irq_gc_get_irq_data(struct irq_chip_generic *gc)
650 {
651 unsigned int virq;
652
653 if (!gc->domain)
654 return irq_get_irq_data(gc->irq_base);
655
656 /*
657 * We don't know which of the irqs has been actually
658 * installed. Use the first one.
659 */
660 if (!gc->installed)
661 return NULL;
662
663 virq = irq_find_mapping(gc->domain, gc->irq_base + __ffs(gc->installed));
664 return virq ? irq_get_irq_data(virq) : NULL;
665 }
666
667 #ifdef CONFIG_PM
irq_gc_suspend(void)668 static int irq_gc_suspend(void)
669 {
670 struct irq_chip_generic *gc;
671
672 list_for_each_entry(gc, &gc_list, list) {
673 struct irq_chip_type *ct = gc->chip_types;
674
675 if (ct->chip.irq_suspend) {
676 struct irq_data *data = irq_gc_get_irq_data(gc);
677
678 if (data)
679 ct->chip.irq_suspend(data);
680 }
681
682 if (gc->suspend)
683 gc->suspend(gc);
684 }
685 return 0;
686 }
687
irq_gc_resume(void)688 static void irq_gc_resume(void)
689 {
690 struct irq_chip_generic *gc;
691
692 list_for_each_entry(gc, &gc_list, list) {
693 struct irq_chip_type *ct = gc->chip_types;
694
695 if (gc->resume)
696 gc->resume(gc);
697
698 if (ct->chip.irq_resume) {
699 struct irq_data *data = irq_gc_get_irq_data(gc);
700
701 if (data)
702 ct->chip.irq_resume(data);
703 }
704 }
705 }
706 #else
707 #define irq_gc_suspend NULL
708 #define irq_gc_resume NULL
709 #endif
710
irq_gc_shutdown(void)711 static void irq_gc_shutdown(void)
712 {
713 struct irq_chip_generic *gc;
714
715 list_for_each_entry(gc, &gc_list, list) {
716 struct irq_chip_type *ct = gc->chip_types;
717
718 if (ct->chip.irq_pm_shutdown) {
719 struct irq_data *data = irq_gc_get_irq_data(gc);
720
721 if (data)
722 ct->chip.irq_pm_shutdown(data);
723 }
724 }
725 }
726
727 static struct syscore_ops irq_gc_syscore_ops = {
728 .suspend = irq_gc_suspend,
729 .resume = irq_gc_resume,
730 .shutdown = irq_gc_shutdown,
731 };
732
irq_gc_init_ops(void)733 static int __init irq_gc_init_ops(void)
734 {
735 register_syscore_ops(&irq_gc_syscore_ops);
736 return 0;
737 }
738 device_initcall(irq_gc_init_ops);
739