generic-chip.c 18 KB

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