generic-chip.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * Library implementing the most common irq chip callback functions
  3. *
  4. * Copyright (C) 2011, Thomas Gleixner
  5. */
  6. #include <linux/io.h>
  7. #include <linux/irq.h>
  8. #include <linux/slab.h>
  9. #include <linux/export.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/kernel_stat.h>
  12. #include <linux/syscore_ops.h>
  13. #include "internals.h"
  14. static LIST_HEAD(gc_list);
  15. static DEFINE_RAW_SPINLOCK(gc_lock);
  16. /**
  17. * irq_gc_noop - NOOP function
  18. * @d: irq_data
  19. */
  20. void irq_gc_noop(struct irq_data *d)
  21. {
  22. }
  23. /**
  24. * irq_gc_mask_disable_reg - Mask chip via disable register
  25. * @d: irq_data
  26. *
  27. * Chip has separate enable/disable registers instead of a single mask
  28. * register.
  29. */
  30. void irq_gc_mask_disable_reg(struct irq_data *d)
  31. {
  32. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  33. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  34. u32 mask = d->mask;
  35. irq_gc_lock(gc);
  36. irq_reg_writel(mask, gc->reg_base + ct->regs.disable);
  37. *ct->mask_cache &= ~mask;
  38. irq_gc_unlock(gc);
  39. }
  40. /**
  41. * irq_gc_mask_set_mask_bit - Mask chip via setting bit in mask register
  42. * @d: irq_data
  43. *
  44. * Chip has a single mask register. Values of this register are cached
  45. * and protected by gc->lock
  46. */
  47. void irq_gc_mask_set_bit(struct irq_data *d)
  48. {
  49. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  50. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  51. u32 mask = d->mask;
  52. irq_gc_lock(gc);
  53. *ct->mask_cache |= mask;
  54. irq_reg_writel(*ct->mask_cache, gc->reg_base + ct->regs.mask);
  55. irq_gc_unlock(gc);
  56. }
  57. /**
  58. * irq_gc_mask_set_mask_bit - Mask chip via clearing bit in mask register
  59. * @d: irq_data
  60. *
  61. * Chip has a single mask register. Values of this register are cached
  62. * and protected by gc->lock
  63. */
  64. void irq_gc_mask_clr_bit(struct irq_data *d)
  65. {
  66. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  67. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  68. u32 mask = d->mask;
  69. irq_gc_lock(gc);
  70. *ct->mask_cache &= ~mask;
  71. irq_reg_writel(*ct->mask_cache, gc->reg_base + ct->regs.mask);
  72. irq_gc_unlock(gc);
  73. }
  74. /**
  75. * irq_gc_unmask_enable_reg - Unmask chip via enable register
  76. * @d: irq_data
  77. *
  78. * Chip has separate enable/disable registers instead of a single mask
  79. * register.
  80. */
  81. void irq_gc_unmask_enable_reg(struct irq_data *d)
  82. {
  83. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  84. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  85. u32 mask = d->mask;
  86. irq_gc_lock(gc);
  87. irq_reg_writel(mask, gc->reg_base + ct->regs.enable);
  88. *ct->mask_cache |= mask;
  89. irq_gc_unlock(gc);
  90. }
  91. /**
  92. * irq_gc_ack_set_bit - Ack pending interrupt via setting bit
  93. * @d: irq_data
  94. */
  95. void irq_gc_ack_set_bit(struct irq_data *d)
  96. {
  97. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  98. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  99. u32 mask = d->mask;
  100. irq_gc_lock(gc);
  101. irq_reg_writel(mask, gc->reg_base + ct->regs.ack);
  102. irq_gc_unlock(gc);
  103. }
  104. /**
  105. * irq_gc_ack_clr_bit - Ack pending interrupt via clearing bit
  106. * @d: irq_data
  107. */
  108. void irq_gc_ack_clr_bit(struct irq_data *d)
  109. {
  110. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  111. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  112. u32 mask = ~d->mask;
  113. irq_gc_lock(gc);
  114. irq_reg_writel(mask, gc->reg_base + ct->regs.ack);
  115. irq_gc_unlock(gc);
  116. }
  117. /**
  118. * irq_gc_mask_disable_reg_and_ack- Mask and ack pending interrupt
  119. * @d: irq_data
  120. */
  121. void irq_gc_mask_disable_reg_and_ack(struct irq_data *d)
  122. {
  123. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  124. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  125. u32 mask = d->mask;
  126. irq_gc_lock(gc);
  127. irq_reg_writel(mask, gc->reg_base + ct->regs.mask);
  128. irq_reg_writel(mask, gc->reg_base + ct->regs.ack);
  129. irq_gc_unlock(gc);
  130. }
  131. /**
  132. * irq_gc_eoi - EOI interrupt
  133. * @d: irq_data
  134. */
  135. void irq_gc_eoi(struct irq_data *d)
  136. {
  137. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  138. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  139. u32 mask = d->mask;
  140. irq_gc_lock(gc);
  141. irq_reg_writel(mask, gc->reg_base + ct->regs.eoi);
  142. irq_gc_unlock(gc);
  143. }
  144. /**
  145. * irq_gc_set_wake - Set/clr wake bit for an interrupt
  146. * @d: irq_data
  147. *
  148. * For chips where the wake from suspend functionality is not
  149. * configured in a separate register and the wakeup active state is
  150. * just stored in a bitmask.
  151. */
  152. int irq_gc_set_wake(struct irq_data *d, unsigned int on)
  153. {
  154. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  155. u32 mask = d->mask;
  156. if (!(mask & gc->wake_enabled))
  157. return -EINVAL;
  158. irq_gc_lock(gc);
  159. if (on)
  160. gc->wake_active |= mask;
  161. else
  162. gc->wake_active &= ~mask;
  163. irq_gc_unlock(gc);
  164. return 0;
  165. }
  166. static void
  167. irq_init_generic_chip(struct irq_chip_generic *gc, const char *name,
  168. int num_ct, unsigned int irq_base,
  169. void __iomem *reg_base, irq_flow_handler_t handler)
  170. {
  171. raw_spin_lock_init(&gc->lock);
  172. gc->num_ct = num_ct;
  173. gc->irq_base = irq_base;
  174. gc->reg_base = reg_base;
  175. gc->chip_types->chip.name = name;
  176. gc->chip_types->handler = handler;
  177. }
  178. /**
  179. * irq_alloc_generic_chip - Allocate a generic chip and initialize it
  180. * @name: Name of the irq chip
  181. * @num_ct: Number of irq_chip_type instances associated with this
  182. * @irq_base: Interrupt base nr for this chip
  183. * @reg_base: Register base address (virtual)
  184. * @handler: Default flow handler associated with this chip
  185. *
  186. * Returns an initialized irq_chip_generic structure. The chip defaults
  187. * to the primary (index 0) irq_chip_type and @handler
  188. */
  189. struct irq_chip_generic *
  190. irq_alloc_generic_chip(const char *name, int num_ct, unsigned int irq_base,
  191. void __iomem *reg_base, irq_flow_handler_t handler)
  192. {
  193. struct irq_chip_generic *gc;
  194. unsigned long sz = sizeof(*gc) + num_ct * sizeof(struct irq_chip_type);
  195. gc = kzalloc(sz, GFP_KERNEL);
  196. if (gc) {
  197. irq_init_generic_chip(gc, name, num_ct, irq_base, reg_base,
  198. handler);
  199. }
  200. return gc;
  201. }
  202. EXPORT_SYMBOL_GPL(irq_alloc_generic_chip);
  203. static void
  204. irq_gc_init_mask_cache(struct irq_chip_generic *gc, enum irq_gc_flags flags)
  205. {
  206. struct irq_chip_type *ct = gc->chip_types;
  207. u32 *mskptr = &gc->mask_cache, mskreg = ct->regs.mask;
  208. int i;
  209. for (i = 0; i < gc->num_ct; i++) {
  210. if (flags & IRQ_GC_MASK_CACHE_PER_TYPE) {
  211. mskptr = &ct[i].mask_cache_priv;
  212. mskreg = ct[i].regs.mask;
  213. }
  214. ct[i].mask_cache = mskptr;
  215. if (flags & IRQ_GC_INIT_MASK_CACHE)
  216. *mskptr = irq_reg_readl(gc->reg_base + mskreg);
  217. }
  218. }
  219. /*
  220. * Separate lockdep class for interrupt chip which can nest irq_desc
  221. * lock.
  222. */
  223. static struct lock_class_key irq_nested_lock_class;
  224. /**
  225. * irq_setup_generic_chip - Setup a range of interrupts with a generic chip
  226. * @gc: Generic irq chip holding all data
  227. * @msk: Bitmask holding the irqs to initialize relative to gc->irq_base
  228. * @flags: Flags for initialization
  229. * @clr: IRQ_* bits to clear
  230. * @set: IRQ_* bits to set
  231. *
  232. * Set up max. 32 interrupts starting from gc->irq_base. Note, this
  233. * initializes all interrupts to the primary irq_chip_type and its
  234. * associated handler.
  235. */
  236. void irq_setup_generic_chip(struct irq_chip_generic *gc, u32 msk,
  237. enum irq_gc_flags flags, unsigned int clr,
  238. unsigned int set)
  239. {
  240. struct irq_chip_type *ct = gc->chip_types;
  241. struct irq_chip *chip = &ct->chip;
  242. unsigned int i;
  243. raw_spin_lock(&gc_lock);
  244. list_add_tail(&gc->list, &gc_list);
  245. raw_spin_unlock(&gc_lock);
  246. irq_gc_init_mask_cache(gc, flags);
  247. for (i = gc->irq_base; msk; msk >>= 1, i++) {
  248. if (!(msk & 0x01))
  249. continue;
  250. if (flags & IRQ_GC_INIT_NESTED_LOCK)
  251. irq_set_lockdep_class(i, &irq_nested_lock_class);
  252. if (!(flags & IRQ_GC_NO_MASK)) {
  253. struct irq_data *d = irq_get_irq_data(i);
  254. if (chip->irq_calc_mask)
  255. chip->irq_calc_mask(d);
  256. else
  257. d->mask = 1 << (i - gc->irq_base);
  258. }
  259. irq_set_chip_and_handler(i, chip, ct->handler);
  260. irq_set_chip_data(i, gc);
  261. irq_modify_status(i, clr, set);
  262. }
  263. gc->irq_cnt = i - gc->irq_base;
  264. }
  265. EXPORT_SYMBOL_GPL(irq_setup_generic_chip);
  266. /**
  267. * irq_setup_alt_chip - Switch to alternative chip
  268. * @d: irq_data for this interrupt
  269. * @type Flow type to be initialized
  270. *
  271. * Only to be called from chip->irq_set_type() callbacks.
  272. */
  273. int irq_setup_alt_chip(struct irq_data *d, unsigned int type)
  274. {
  275. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  276. struct irq_chip_type *ct = gc->chip_types;
  277. unsigned int i;
  278. for (i = 0; i < gc->num_ct; i++, ct++) {
  279. if (ct->type & type) {
  280. d->chip = &ct->chip;
  281. irq_data_to_desc(d)->handle_irq = ct->handler;
  282. return 0;
  283. }
  284. }
  285. return -EINVAL;
  286. }
  287. EXPORT_SYMBOL_GPL(irq_setup_alt_chip);
  288. /**
  289. * irq_remove_generic_chip - Remove a chip
  290. * @gc: Generic irq chip holding all data
  291. * @msk: Bitmask holding the irqs to initialize relative to gc->irq_base
  292. * @clr: IRQ_* bits to clear
  293. * @set: IRQ_* bits to set
  294. *
  295. * Remove up to 32 interrupts starting from gc->irq_base.
  296. */
  297. void irq_remove_generic_chip(struct irq_chip_generic *gc, u32 msk,
  298. unsigned int clr, unsigned int set)
  299. {
  300. unsigned int i = gc->irq_base;
  301. raw_spin_lock(&gc_lock);
  302. list_del(&gc->list);
  303. raw_spin_unlock(&gc_lock);
  304. for (; msk; msk >>= 1, i++) {
  305. if (!(msk & 0x01))
  306. continue;
  307. /* Remove handler first. That will mask the irq line */
  308. irq_set_handler(i, NULL);
  309. irq_set_chip(i, &no_irq_chip);
  310. irq_set_chip_data(i, NULL);
  311. irq_modify_status(i, clr, set);
  312. }
  313. }
  314. EXPORT_SYMBOL_GPL(irq_remove_generic_chip);
  315. #ifdef CONFIG_PM
  316. static int irq_gc_suspend(void)
  317. {
  318. struct irq_chip_generic *gc;
  319. list_for_each_entry(gc, &gc_list, list) {
  320. struct irq_chip_type *ct = gc->chip_types;
  321. if (ct->chip.irq_suspend)
  322. ct->chip.irq_suspend(irq_get_irq_data(gc->irq_base));
  323. }
  324. return 0;
  325. }
  326. static void irq_gc_resume(void)
  327. {
  328. struct irq_chip_generic *gc;
  329. list_for_each_entry(gc, &gc_list, list) {
  330. struct irq_chip_type *ct = gc->chip_types;
  331. if (ct->chip.irq_resume)
  332. ct->chip.irq_resume(irq_get_irq_data(gc->irq_base));
  333. }
  334. }
  335. #else
  336. #define irq_gc_suspend NULL
  337. #define irq_gc_resume NULL
  338. #endif
  339. static void irq_gc_shutdown(void)
  340. {
  341. struct irq_chip_generic *gc;
  342. list_for_each_entry(gc, &gc_list, list) {
  343. struct irq_chip_type *ct = gc->chip_types;
  344. if (ct->chip.irq_pm_shutdown)
  345. ct->chip.irq_pm_shutdown(irq_get_irq_data(gc->irq_base));
  346. }
  347. }
  348. static struct syscore_ops irq_gc_syscore_ops = {
  349. .suspend = irq_gc_suspend,
  350. .resume = irq_gc_resume,
  351. .shutdown = irq_gc_shutdown,
  352. };
  353. static int __init irq_gc_init_ops(void)
  354. {
  355. register_syscore_ops(&irq_gc_syscore_ops);
  356. return 0;
  357. }
  358. device_initcall(irq_gc_init_ops);