generic-chip.c 9.0 KB

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