generic-chip.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 - Ack pending interrupt
  92. * @d: irq_data
  93. */
  94. void irq_gc_ack(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_mask_disable_reg_and_ack- Mask and ack pending interrupt
  104. * @d: irq_data
  105. */
  106. void irq_gc_mask_disable_reg_and_ack(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)->mask);
  112. irq_reg_writel(mask, gc->reg_base + cur_regs(d)->ack);
  113. irq_gc_unlock(gc);
  114. }
  115. /**
  116. * irq_gc_eoi - EOI interrupt
  117. * @d: irq_data
  118. */
  119. void irq_gc_eoi(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)->eoi);
  125. irq_gc_unlock(gc);
  126. }
  127. /**
  128. * irq_gc_set_wake - Set/clr wake bit for an interrupt
  129. * @d: irq_data
  130. *
  131. * For chips where the wake from suspend functionality is not
  132. * configured in a separate register and the wakeup active state is
  133. * just stored in a bitmask.
  134. */
  135. int irq_gc_set_wake(struct irq_data *d, unsigned int on)
  136. {
  137. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  138. u32 mask = 1 << (d->irq - gc->irq_base);
  139. if (!(mask & gc->wake_enabled))
  140. return -EINVAL;
  141. irq_gc_lock(gc);
  142. if (on)
  143. gc->wake_active |= mask;
  144. else
  145. gc->wake_active &= ~mask;
  146. irq_gc_unlock(gc);
  147. return 0;
  148. }
  149. /**
  150. * irq_alloc_generic_chip - Allocate a generic chip and initialize it
  151. * @name: Name of the irq chip
  152. * @num_ct: Number of irq_chip_type instances associated with this
  153. * @irq_base: Interrupt base nr for this chip
  154. * @reg_base: Register base address (virtual)
  155. * @handler: Default flow handler associated with this chip
  156. *
  157. * Returns an initialized irq_chip_generic structure. The chip defaults
  158. * to the primary (index 0) irq_chip_type and @handler
  159. */
  160. struct irq_chip_generic *
  161. irq_alloc_generic_chip(const char *name, int num_ct, unsigned int irq_base,
  162. void __iomem *reg_base, irq_flow_handler_t handler)
  163. {
  164. struct irq_chip_generic *gc;
  165. unsigned long sz = sizeof(*gc) + num_ct * sizeof(struct irq_chip_type);
  166. gc = kzalloc(sz, GFP_KERNEL);
  167. if (gc) {
  168. raw_spin_lock_init(&gc->lock);
  169. gc->num_ct = num_ct;
  170. gc->irq_base = irq_base;
  171. gc->reg_base = reg_base;
  172. gc->chip_types->chip.name = name;
  173. gc->chip_types->handler = handler;
  174. }
  175. return gc;
  176. }
  177. /*
  178. * Separate lockdep class for interrupt chip which can nest irq_desc
  179. * lock.
  180. */
  181. static struct lock_class_key irq_nested_lock_class;
  182. /**
  183. * irq_setup_generic_chip - Setup a range of interrupts with a generic chip
  184. * @gc: Generic irq chip holding all data
  185. * @msk: Bitmask holding the irqs to initialize relative to gc->irq_base
  186. * @flags: Flags for initialization
  187. * @clr: IRQ_* bits to clear
  188. * @set: IRQ_* bits to set
  189. *
  190. * Set up max. 32 interrupts starting from gc->irq_base. Note, this
  191. * initializes all interrupts to the primary irq_chip_type and its
  192. * associated handler.
  193. */
  194. void irq_setup_generic_chip(struct irq_chip_generic *gc, u32 msk,
  195. enum irq_gc_flags flags, unsigned int clr,
  196. unsigned int set)
  197. {
  198. struct irq_chip_type *ct = gc->chip_types;
  199. unsigned int i;
  200. raw_spin_lock(&gc_lock);
  201. list_add_tail(&gc->list, &gc_list);
  202. raw_spin_unlock(&gc_lock);
  203. /* Init mask cache ? */
  204. if (flags & IRQ_GC_INIT_MASK_CACHE)
  205. gc->mask_cache = irq_reg_readl(gc->reg_base + ct->regs.mask);
  206. for (i = gc->irq_base; msk; msk >>= 1, i++) {
  207. if (!msk & 0x01)
  208. continue;
  209. if (flags & IRQ_GC_INIT_NESTED_LOCK)
  210. irq_set_lockdep_class(i, &irq_nested_lock_class);
  211. irq_set_chip_and_handler(i, &ct->chip, ct->handler);
  212. irq_set_chip_data(i, gc);
  213. irq_modify_status(i, clr, set);
  214. }
  215. gc->irq_cnt = i - gc->irq_base;
  216. }
  217. /**
  218. * irq_setup_alt_chip - Switch to alternative chip
  219. * @d: irq_data for this interrupt
  220. * @type Flow type to be initialized
  221. *
  222. * Only to be called from chip->irq_set_type() callbacks.
  223. */
  224. int irq_setup_alt_chip(struct irq_data *d, unsigned int type)
  225. {
  226. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  227. struct irq_chip_type *ct = gc->chip_types;
  228. unsigned int i;
  229. for (i = 0; i < gc->num_ct; i++, ct++) {
  230. if (ct->type & type) {
  231. d->chip = &ct->chip;
  232. irq_data_to_desc(d)->handle_irq = ct->handler;
  233. return 0;
  234. }
  235. }
  236. return -EINVAL;
  237. }
  238. /**
  239. * irq_remove_generic_chip - Remove a chip
  240. * @gc: Generic irq chip holding all data
  241. * @msk: Bitmask holding the irqs to initialize relative to gc->irq_base
  242. * @clr: IRQ_* bits to clear
  243. * @set: IRQ_* bits to set
  244. *
  245. * Remove up to 32 interrupts starting from gc->irq_base.
  246. */
  247. void irq_remove_generic_chip(struct irq_chip_generic *gc, u32 msk,
  248. unsigned int clr, unsigned int set)
  249. {
  250. unsigned int i = gc->irq_base;
  251. raw_spin_lock(&gc_lock);
  252. list_del(&gc->list);
  253. raw_spin_unlock(&gc_lock);
  254. for (; msk; msk >>= 1, i++) {
  255. if (!msk & 0x01)
  256. continue;
  257. /* Remove handler first. That will mask the irq line */
  258. irq_set_handler(i, NULL);
  259. irq_set_chip(i, &no_irq_chip);
  260. irq_set_chip_data(i, NULL);
  261. irq_modify_status(i, clr, set);
  262. }
  263. }
  264. #ifdef CONFIG_PM
  265. static int irq_gc_suspend(void)
  266. {
  267. struct irq_chip_generic *gc;
  268. list_for_each_entry(gc, &gc_list, list) {
  269. struct irq_chip_type *ct = gc->chip_types;
  270. if (ct->chip.irq_suspend)
  271. ct->chip.irq_suspend(irq_get_irq_data(gc->irq_base));
  272. }
  273. return 0;
  274. }
  275. static void irq_gc_resume(void)
  276. {
  277. struct irq_chip_generic *gc;
  278. list_for_each_entry(gc, &gc_list, list) {
  279. struct irq_chip_type *ct = gc->chip_types;
  280. if (ct->chip.irq_resume)
  281. ct->chip.irq_resume(irq_get_irq_data(gc->irq_base));
  282. }
  283. }
  284. #else
  285. #define irq_gc_suspend NULL
  286. #define irq_gc_resume NULL
  287. #endif
  288. static void irq_gc_shutdown(void)
  289. {
  290. struct irq_chip_generic *gc;
  291. list_for_each_entry(gc, &gc_list, list) {
  292. struct irq_chip_type *ct = gc->chip_types;
  293. if (ct->chip.irq_pm_shutdown)
  294. ct->chip.irq_pm_shutdown(irq_get_irq_data(gc->irq_base));
  295. }
  296. }
  297. static struct syscore_ops irq_gc_syscore_ops = {
  298. .suspend = irq_gc_suspend,
  299. .resume = irq_gc_resume,
  300. .shutdown = irq_gc_shutdown,
  301. };
  302. static int __init irq_gc_init_ops(void)
  303. {
  304. register_syscore_ops(&irq_gc_syscore_ops);
  305. return 0;
  306. }
  307. device_initcall(irq_gc_init_ops);