generic-chip.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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/irqdomain.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/kernel_stat.h>
  13. #include <linux/syscore_ops.h>
  14. #include "internals.h"
  15. static LIST_HEAD(gc_list);
  16. static DEFINE_RAW_SPINLOCK(gc_lock);
  17. /**
  18. * irq_gc_noop - NOOP function
  19. * @d: irq_data
  20. */
  21. void irq_gc_noop(struct irq_data *d)
  22. {
  23. }
  24. /**
  25. * irq_gc_mask_disable_reg - Mask chip via disable register
  26. * @d: irq_data
  27. *
  28. * Chip has separate enable/disable registers instead of a single mask
  29. * register.
  30. */
  31. void irq_gc_mask_disable_reg(struct irq_data *d)
  32. {
  33. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  34. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  35. u32 mask = d->mask;
  36. irq_gc_lock(gc);
  37. irq_reg_writel(mask, gc->reg_base + ct->regs.disable);
  38. *ct->mask_cache &= ~mask;
  39. irq_gc_unlock(gc);
  40. }
  41. /**
  42. * irq_gc_mask_set_mask_bit - Mask chip via setting bit in mask register
  43. * @d: irq_data
  44. *
  45. * Chip has a single mask register. Values of this register are cached
  46. * and protected by gc->lock
  47. */
  48. void irq_gc_mask_set_bit(struct irq_data *d)
  49. {
  50. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  51. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  52. u32 mask = d->mask;
  53. irq_gc_lock(gc);
  54. *ct->mask_cache |= mask;
  55. irq_reg_writel(*ct->mask_cache, gc->reg_base + ct->regs.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. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  69. u32 mask = d->mask;
  70. irq_gc_lock(gc);
  71. *ct->mask_cache &= ~mask;
  72. irq_reg_writel(*ct->mask_cache, gc->reg_base + ct->regs.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. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  86. u32 mask = d->mask;
  87. irq_gc_lock(gc);
  88. irq_reg_writel(mask, gc->reg_base + ct->regs.enable);
  89. *ct->mask_cache |= mask;
  90. irq_gc_unlock(gc);
  91. }
  92. /**
  93. * irq_gc_ack_set_bit - Ack pending interrupt via setting bit
  94. * @d: irq_data
  95. */
  96. void irq_gc_ack_set_bit(struct irq_data *d)
  97. {
  98. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  99. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  100. u32 mask = d->mask;
  101. irq_gc_lock(gc);
  102. irq_reg_writel(mask, gc->reg_base + ct->regs.ack);
  103. irq_gc_unlock(gc);
  104. }
  105. /**
  106. * irq_gc_ack_clr_bit - Ack pending interrupt via clearing bit
  107. * @d: irq_data
  108. */
  109. void irq_gc_ack_clr_bit(struct irq_data *d)
  110. {
  111. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  112. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  113. u32 mask = ~d->mask;
  114. irq_gc_lock(gc);
  115. irq_reg_writel(mask, gc->reg_base + ct->regs.ack);
  116. irq_gc_unlock(gc);
  117. }
  118. /**
  119. * irq_gc_mask_disable_reg_and_ack- Mask and ack pending interrupt
  120. * @d: irq_data
  121. */
  122. void irq_gc_mask_disable_reg_and_ack(struct irq_data *d)
  123. {
  124. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  125. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  126. u32 mask = d->mask;
  127. irq_gc_lock(gc);
  128. irq_reg_writel(mask, gc->reg_base + ct->regs.mask);
  129. irq_reg_writel(mask, gc->reg_base + ct->regs.ack);
  130. irq_gc_unlock(gc);
  131. }
  132. /**
  133. * irq_gc_eoi - EOI interrupt
  134. * @d: irq_data
  135. */
  136. void irq_gc_eoi(struct irq_data *d)
  137. {
  138. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  139. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  140. u32 mask = d->mask;
  141. irq_gc_lock(gc);
  142. irq_reg_writel(mask, gc->reg_base + ct->regs.eoi);
  143. irq_gc_unlock(gc);
  144. }
  145. /**
  146. * irq_gc_set_wake - Set/clr wake bit for an interrupt
  147. * @d: irq_data
  148. *
  149. * For chips where the wake from suspend functionality is not
  150. * configured in a separate register and the wakeup active state is
  151. * just stored in a bitmask.
  152. */
  153. int irq_gc_set_wake(struct irq_data *d, unsigned int on)
  154. {
  155. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  156. u32 mask = d->mask;
  157. if (!(mask & gc->wake_enabled))
  158. return -EINVAL;
  159. irq_gc_lock(gc);
  160. if (on)
  161. gc->wake_active |= mask;
  162. else
  163. gc->wake_active &= ~mask;
  164. irq_gc_unlock(gc);
  165. return 0;
  166. }
  167. static void
  168. irq_init_generic_chip(struct irq_chip_generic *gc, const char *name,
  169. int num_ct, unsigned int irq_base,
  170. void __iomem *reg_base, irq_flow_handler_t handler)
  171. {
  172. raw_spin_lock_init(&gc->lock);
  173. gc->num_ct = num_ct;
  174. gc->irq_base = irq_base;
  175. gc->reg_base = reg_base;
  176. gc->chip_types->chip.name = name;
  177. gc->chip_types->handler = handler;
  178. }
  179. /**
  180. * irq_alloc_generic_chip - Allocate a generic chip and initialize it
  181. * @name: Name of the irq chip
  182. * @num_ct: Number of irq_chip_type instances associated with this
  183. * @irq_base: Interrupt base nr for this chip
  184. * @reg_base: Register base address (virtual)
  185. * @handler: Default flow handler associated with this chip
  186. *
  187. * Returns an initialized irq_chip_generic structure. The chip defaults
  188. * to the primary (index 0) irq_chip_type and @handler
  189. */
  190. struct irq_chip_generic *
  191. irq_alloc_generic_chip(const char *name, int num_ct, unsigned int irq_base,
  192. void __iomem *reg_base, irq_flow_handler_t handler)
  193. {
  194. struct irq_chip_generic *gc;
  195. unsigned long sz = sizeof(*gc) + num_ct * sizeof(struct irq_chip_type);
  196. gc = kzalloc(sz, GFP_KERNEL);
  197. if (gc) {
  198. irq_init_generic_chip(gc, name, num_ct, irq_base, reg_base,
  199. handler);
  200. }
  201. return gc;
  202. }
  203. EXPORT_SYMBOL_GPL(irq_alloc_generic_chip);
  204. static void
  205. irq_gc_init_mask_cache(struct irq_chip_generic *gc, enum irq_gc_flags flags)
  206. {
  207. struct irq_chip_type *ct = gc->chip_types;
  208. u32 *mskptr = &gc->mask_cache, mskreg = ct->regs.mask;
  209. int i;
  210. for (i = 0; i < gc->num_ct; i++) {
  211. if (flags & IRQ_GC_MASK_CACHE_PER_TYPE) {
  212. mskptr = &ct[i].mask_cache_priv;
  213. mskreg = ct[i].regs.mask;
  214. }
  215. ct[i].mask_cache = mskptr;
  216. if (flags & IRQ_GC_INIT_MASK_CACHE)
  217. *mskptr = irq_reg_readl(gc->reg_base + mskreg);
  218. }
  219. }
  220. /**
  221. * irq_alloc_domain_generic_chip - Allocate generic chips for an irq domain
  222. * @d: irq domain for which to allocate chips
  223. * @irqs_per_chip: Number of interrupts each chip handles
  224. * @num_ct: Number of irq_chip_type instances associated with this
  225. * @name: Name of the irq chip
  226. * @handler: Default flow handler associated with these chips
  227. * @clr: IRQ_* bits to clear in the mapping function
  228. * @set: IRQ_* bits to set in the mapping function
  229. */
  230. int irq_alloc_domain_generic_chips(struct irq_domain *d, int irqs_per_chip,
  231. int num_ct, const char *name,
  232. irq_flow_handler_t handler,
  233. unsigned int clr, unsigned int set,
  234. enum irq_gc_flags gcflags)
  235. {
  236. struct irq_domain_chip_generic *dgc;
  237. struct irq_chip_generic *gc;
  238. int numchips, sz, i;
  239. unsigned long flags;
  240. void *tmp;
  241. if (d->gc)
  242. return -EBUSY;
  243. if (d->revmap_type != IRQ_DOMAIN_MAP_LINEAR)
  244. return -EINVAL;
  245. numchips = d->revmap_data.linear.size / irqs_per_chip;
  246. if (!numchips)
  247. return -EINVAL;
  248. /* Allocate a pointer, generic chip and chiptypes for each chip */
  249. sz = sizeof(*dgc) + numchips * sizeof(gc);
  250. sz += numchips * (sizeof(*gc) + num_ct * sizeof(struct irq_chip_type));
  251. tmp = dgc = kzalloc(sz, GFP_KERNEL);
  252. if (!dgc)
  253. return -ENOMEM;
  254. dgc->irqs_per_chip = irqs_per_chip;
  255. dgc->num_chips = numchips;
  256. dgc->irq_flags_to_set = set;
  257. dgc->irq_flags_to_clear = clr;
  258. dgc->gc_flags = gcflags;
  259. d->gc = dgc;
  260. /* Calc pointer to the first generic chip */
  261. tmp += sizeof(*dgc) + numchips * sizeof(gc);
  262. for (i = 0; i < numchips; i++) {
  263. /* Store the pointer to the generic chip */
  264. dgc->gc[i] = gc = tmp;
  265. irq_init_generic_chip(gc, name, num_ct, i * irqs_per_chip,
  266. NULL, handler);
  267. gc->domain = d;
  268. raw_spin_lock_irqsave(&gc_lock, flags);
  269. list_add_tail(&gc->list, &gc_list);
  270. raw_spin_unlock_irqrestore(&gc_lock, flags);
  271. /* Calc pointer to the next generic chip */
  272. tmp += sizeof(*gc) + num_ct * sizeof(struct irq_chip_type);
  273. }
  274. d->name = name;
  275. return 0;
  276. }
  277. EXPORT_SYMBOL_GPL(irq_alloc_domain_generic_chips);
  278. /**
  279. * irq_get_domain_generic_chip - Get a pointer to the generic chip of a hw_irq
  280. * @d: irq domain pointer
  281. * @hw_irq: Hardware interrupt number
  282. */
  283. struct irq_chip_generic *
  284. irq_get_domain_generic_chip(struct irq_domain *d, unsigned int hw_irq)
  285. {
  286. struct irq_domain_chip_generic *dgc = d->gc;
  287. int idx;
  288. if (!dgc)
  289. return NULL;
  290. idx = hw_irq / dgc->irqs_per_chip;
  291. if (idx >= dgc->num_chips)
  292. return NULL;
  293. return dgc->gc[idx];
  294. }
  295. EXPORT_SYMBOL_GPL(irq_get_domain_generic_chip);
  296. /*
  297. * Separate lockdep class for interrupt chip which can nest irq_desc
  298. * lock.
  299. */
  300. static struct lock_class_key irq_nested_lock_class;
  301. /**
  302. * irq_map_generic_chip - Map a generic chip for an irq domain
  303. */
  304. static int irq_map_generic_chip(struct irq_domain *d, unsigned int virq,
  305. irq_hw_number_t hw_irq)
  306. {
  307. struct irq_data *data = irq_get_irq_data(virq);
  308. struct irq_domain_chip_generic *dgc = d->gc;
  309. struct irq_chip_generic *gc;
  310. struct irq_chip_type *ct;
  311. struct irq_chip *chip;
  312. unsigned long flags;
  313. int idx;
  314. if (!d->gc)
  315. return -ENODEV;
  316. idx = hw_irq / dgc->irqs_per_chip;
  317. if (idx >= dgc->num_chips)
  318. return -EINVAL;
  319. gc = dgc->gc[idx];
  320. idx = hw_irq % dgc->irqs_per_chip;
  321. if (test_bit(idx, &gc->unused))
  322. return -ENOTSUPP;
  323. if (test_bit(idx, &gc->installed))
  324. return -EBUSY;
  325. ct = gc->chip_types;
  326. chip = &ct->chip;
  327. /* We only init the cache for the first mapping of a generic chip */
  328. if (!gc->installed) {
  329. raw_spin_lock_irqsave(&gc->lock, flags);
  330. irq_gc_init_mask_cache(gc, dgc->gc_flags);
  331. raw_spin_unlock_irqrestore(&gc->lock, flags);
  332. }
  333. /* Mark the interrupt as installed */
  334. set_bit(idx, &gc->installed);
  335. if (dgc->gc_flags & IRQ_GC_INIT_NESTED_LOCK)
  336. irq_set_lockdep_class(virq, &irq_nested_lock_class);
  337. if (chip->irq_calc_mask)
  338. chip->irq_calc_mask(data);
  339. else
  340. data->mask = 1 << idx;
  341. irq_set_chip_and_handler(virq, chip, ct->handler);
  342. irq_set_chip_data(virq, gc);
  343. irq_modify_status(virq, dgc->irq_flags_to_clear, dgc->irq_flags_to_set);
  344. return 0;
  345. }
  346. struct irq_domain_ops irq_generic_chip_ops = {
  347. .map = irq_map_generic_chip,
  348. .xlate = irq_domain_xlate_onetwocell,
  349. };
  350. EXPORT_SYMBOL_GPL(irq_generic_chip_ops);
  351. /**
  352. * irq_setup_generic_chip - Setup a range of interrupts with a generic chip
  353. * @gc: Generic irq chip holding all data
  354. * @msk: Bitmask holding the irqs to initialize relative to gc->irq_base
  355. * @flags: Flags for initialization
  356. * @clr: IRQ_* bits to clear
  357. * @set: IRQ_* bits to set
  358. *
  359. * Set up max. 32 interrupts starting from gc->irq_base. Note, this
  360. * initializes all interrupts to the primary irq_chip_type and its
  361. * associated handler.
  362. */
  363. void irq_setup_generic_chip(struct irq_chip_generic *gc, u32 msk,
  364. enum irq_gc_flags flags, unsigned int clr,
  365. unsigned int set)
  366. {
  367. struct irq_chip_type *ct = gc->chip_types;
  368. struct irq_chip *chip = &ct->chip;
  369. unsigned int i;
  370. raw_spin_lock(&gc_lock);
  371. list_add_tail(&gc->list, &gc_list);
  372. raw_spin_unlock(&gc_lock);
  373. irq_gc_init_mask_cache(gc, flags);
  374. for (i = gc->irq_base; msk; msk >>= 1, i++) {
  375. if (!(msk & 0x01))
  376. continue;
  377. if (flags & IRQ_GC_INIT_NESTED_LOCK)
  378. irq_set_lockdep_class(i, &irq_nested_lock_class);
  379. if (!(flags & IRQ_GC_NO_MASK)) {
  380. struct irq_data *d = irq_get_irq_data(i);
  381. if (chip->irq_calc_mask)
  382. chip->irq_calc_mask(d);
  383. else
  384. d->mask = 1 << (i - gc->irq_base);
  385. }
  386. irq_set_chip_and_handler(i, chip, ct->handler);
  387. irq_set_chip_data(i, gc);
  388. irq_modify_status(i, clr, set);
  389. }
  390. gc->irq_cnt = i - gc->irq_base;
  391. }
  392. EXPORT_SYMBOL_GPL(irq_setup_generic_chip);
  393. /**
  394. * irq_setup_alt_chip - Switch to alternative chip
  395. * @d: irq_data for this interrupt
  396. * @type Flow type to be initialized
  397. *
  398. * Only to be called from chip->irq_set_type() callbacks.
  399. */
  400. int irq_setup_alt_chip(struct irq_data *d, unsigned int type)
  401. {
  402. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  403. struct irq_chip_type *ct = gc->chip_types;
  404. unsigned int i;
  405. for (i = 0; i < gc->num_ct; i++, ct++) {
  406. if (ct->type & type) {
  407. d->chip = &ct->chip;
  408. irq_data_to_desc(d)->handle_irq = ct->handler;
  409. return 0;
  410. }
  411. }
  412. return -EINVAL;
  413. }
  414. EXPORT_SYMBOL_GPL(irq_setup_alt_chip);
  415. /**
  416. * irq_remove_generic_chip - Remove a chip
  417. * @gc: Generic irq chip holding all data
  418. * @msk: Bitmask holding the irqs to initialize relative to gc->irq_base
  419. * @clr: IRQ_* bits to clear
  420. * @set: IRQ_* bits to set
  421. *
  422. * Remove up to 32 interrupts starting from gc->irq_base.
  423. */
  424. void irq_remove_generic_chip(struct irq_chip_generic *gc, u32 msk,
  425. unsigned int clr, unsigned int set)
  426. {
  427. unsigned int i = gc->irq_base;
  428. raw_spin_lock(&gc_lock);
  429. list_del(&gc->list);
  430. raw_spin_unlock(&gc_lock);
  431. for (; msk; msk >>= 1, i++) {
  432. if (!(msk & 0x01))
  433. continue;
  434. /* Remove handler first. That will mask the irq line */
  435. irq_set_handler(i, NULL);
  436. irq_set_chip(i, &no_irq_chip);
  437. irq_set_chip_data(i, NULL);
  438. irq_modify_status(i, clr, set);
  439. }
  440. }
  441. EXPORT_SYMBOL_GPL(irq_remove_generic_chip);
  442. static struct irq_data *irq_gc_get_irq_data(struct irq_chip_generic *gc)
  443. {
  444. unsigned int virq;
  445. if (!gc->domain)
  446. return irq_get_irq_data(gc->irq_base);
  447. /*
  448. * We don't know which of the irqs has been actually
  449. * installed. Use the first one.
  450. */
  451. if (!gc->installed)
  452. return NULL;
  453. virq = irq_find_mapping(gc->domain, gc->irq_base + __ffs(gc->installed));
  454. return virq ? irq_get_irq_data(virq) : NULL;
  455. }
  456. #ifdef CONFIG_PM
  457. static int irq_gc_suspend(void)
  458. {
  459. struct irq_chip_generic *gc;
  460. list_for_each_entry(gc, &gc_list, list) {
  461. struct irq_chip_type *ct = gc->chip_types;
  462. if (ct->chip.irq_suspend) {
  463. struct irq_data *data = irq_gc_get_irq_data(gc);
  464. if (data)
  465. ct->chip.irq_suspend(data);
  466. }
  467. }
  468. return 0;
  469. }
  470. static void irq_gc_resume(void)
  471. {
  472. struct irq_chip_generic *gc;
  473. list_for_each_entry(gc, &gc_list, list) {
  474. struct irq_chip_type *ct = gc->chip_types;
  475. if (ct->chip.irq_resume) {
  476. struct irq_data *data = irq_gc_get_irq_data(gc);
  477. if (data)
  478. ct->chip.irq_resume(data);
  479. }
  480. }
  481. }
  482. #else
  483. #define irq_gc_suspend NULL
  484. #define irq_gc_resume NULL
  485. #endif
  486. static void irq_gc_shutdown(void)
  487. {
  488. struct irq_chip_generic *gc;
  489. list_for_each_entry(gc, &gc_list, list) {
  490. struct irq_chip_type *ct = gc->chip_types;
  491. if (ct->chip.irq_pm_shutdown) {
  492. struct irq_data *data = irq_gc_get_irq_data(gc);
  493. if (data)
  494. ct->chip.irq_pm_shutdown(data);
  495. }
  496. }
  497. }
  498. static struct syscore_ops irq_gc_syscore_ops = {
  499. .suspend = irq_gc_suspend,
  500. .resume = irq_gc_resume,
  501. .shutdown = irq_gc_shutdown,
  502. };
  503. static int __init irq_gc_init_ops(void)
  504. {
  505. register_syscore_ops(&irq_gc_syscore_ops);
  506. return 0;
  507. }
  508. device_initcall(irq_gc_init_ops);