generic-chip.c 9.2 KB

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