generic-chip.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. EXPORT_SYMBOL_GPL(irq_alloc_generic_chip);
  190. /*
  191. * Separate lockdep class for interrupt chip which can nest irq_desc
  192. * lock.
  193. */
  194. static struct lock_class_key irq_nested_lock_class;
  195. /**
  196. * irq_setup_generic_chip - Setup a range of interrupts with a generic chip
  197. * @gc: Generic irq chip holding all data
  198. * @msk: Bitmask holding the irqs to initialize relative to gc->irq_base
  199. * @flags: Flags for initialization
  200. * @clr: IRQ_* bits to clear
  201. * @set: IRQ_* bits to set
  202. *
  203. * Set up max. 32 interrupts starting from gc->irq_base. Note, this
  204. * initializes all interrupts to the primary irq_chip_type and its
  205. * associated handler.
  206. */
  207. void irq_setup_generic_chip(struct irq_chip_generic *gc, u32 msk,
  208. enum irq_gc_flags flags, unsigned int clr,
  209. unsigned int set)
  210. {
  211. struct irq_chip_type *ct = gc->chip_types;
  212. unsigned int i;
  213. raw_spin_lock(&gc_lock);
  214. list_add_tail(&gc->list, &gc_list);
  215. raw_spin_unlock(&gc_lock);
  216. /* Init mask cache ? */
  217. if (flags & IRQ_GC_INIT_MASK_CACHE)
  218. gc->mask_cache = irq_reg_readl(gc->reg_base + ct->regs.mask);
  219. for (i = gc->irq_base; msk; msk >>= 1, i++) {
  220. if (!(msk & 0x01))
  221. continue;
  222. if (flags & IRQ_GC_INIT_NESTED_LOCK)
  223. irq_set_lockdep_class(i, &irq_nested_lock_class);
  224. irq_set_chip_and_handler(i, &ct->chip, ct->handler);
  225. irq_set_chip_data(i, gc);
  226. irq_modify_status(i, clr, set);
  227. }
  228. gc->irq_cnt = i - gc->irq_base;
  229. }
  230. EXPORT_SYMBOL_GPL(irq_setup_generic_chip);
  231. /**
  232. * irq_setup_alt_chip - Switch to alternative chip
  233. * @d: irq_data for this interrupt
  234. * @type Flow type to be initialized
  235. *
  236. * Only to be called from chip->irq_set_type() callbacks.
  237. */
  238. int irq_setup_alt_chip(struct irq_data *d, unsigned int type)
  239. {
  240. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  241. struct irq_chip_type *ct = gc->chip_types;
  242. unsigned int i;
  243. for (i = 0; i < gc->num_ct; i++, ct++) {
  244. if (ct->type & type) {
  245. d->chip = &ct->chip;
  246. irq_data_to_desc(d)->handle_irq = ct->handler;
  247. return 0;
  248. }
  249. }
  250. return -EINVAL;
  251. }
  252. EXPORT_SYMBOL_GPL(irq_setup_alt_chip);
  253. /**
  254. * irq_remove_generic_chip - Remove a chip
  255. * @gc: Generic irq chip holding all data
  256. * @msk: Bitmask holding the irqs to initialize relative to gc->irq_base
  257. * @clr: IRQ_* bits to clear
  258. * @set: IRQ_* bits to set
  259. *
  260. * Remove up to 32 interrupts starting from gc->irq_base.
  261. */
  262. void irq_remove_generic_chip(struct irq_chip_generic *gc, u32 msk,
  263. unsigned int clr, unsigned int set)
  264. {
  265. unsigned int i = gc->irq_base;
  266. raw_spin_lock(&gc_lock);
  267. list_del(&gc->list);
  268. raw_spin_unlock(&gc_lock);
  269. for (; msk; msk >>= 1, i++) {
  270. if (!(msk & 0x01))
  271. continue;
  272. /* Remove handler first. That will mask the irq line */
  273. irq_set_handler(i, NULL);
  274. irq_set_chip(i, &no_irq_chip);
  275. irq_set_chip_data(i, NULL);
  276. irq_modify_status(i, clr, set);
  277. }
  278. }
  279. EXPORT_SYMBOL_GPL(irq_remove_generic_chip);
  280. #ifdef CONFIG_PM
  281. static int irq_gc_suspend(void)
  282. {
  283. struct irq_chip_generic *gc;
  284. list_for_each_entry(gc, &gc_list, list) {
  285. struct irq_chip_type *ct = gc->chip_types;
  286. if (ct->chip.irq_suspend)
  287. ct->chip.irq_suspend(irq_get_irq_data(gc->irq_base));
  288. }
  289. return 0;
  290. }
  291. static void irq_gc_resume(void)
  292. {
  293. struct irq_chip_generic *gc;
  294. list_for_each_entry(gc, &gc_list, list) {
  295. struct irq_chip_type *ct = gc->chip_types;
  296. if (ct->chip.irq_resume)
  297. ct->chip.irq_resume(irq_get_irq_data(gc->irq_base));
  298. }
  299. }
  300. #else
  301. #define irq_gc_suspend NULL
  302. #define irq_gc_resume NULL
  303. #endif
  304. static void irq_gc_shutdown(void)
  305. {
  306. struct irq_chip_generic *gc;
  307. list_for_each_entry(gc, &gc_list, list) {
  308. struct irq_chip_type *ct = gc->chip_types;
  309. if (ct->chip.irq_pm_shutdown)
  310. ct->chip.irq_pm_shutdown(irq_get_irq_data(gc->irq_base));
  311. }
  312. }
  313. static struct syscore_ops irq_gc_syscore_ops = {
  314. .suspend = irq_gc_suspend,
  315. .resume = irq_gc_resume,
  316. .shutdown = irq_gc_shutdown,
  317. };
  318. static int __init irq_gc_init_ops(void)
  319. {
  320. register_syscore_ops(&irq_gc_syscore_ops);
  321. return 0;
  322. }
  323. device_initcall(irq_gc_init_ops);