virq.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Support for virtual IRQ subgroups.
  3. *
  4. * Copyright (C) 2010 Paul Mundt
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. */
  10. #define pr_fmt(fmt) "intc: " fmt
  11. #include <linux/slab.h>
  12. #include <linux/irq.h>
  13. #include <linux/list.h>
  14. #include <linux/radix-tree.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/export.h>
  17. #include "internals.h"
  18. static struct intc_map_entry intc_irq_xlate[INTC_NR_IRQS];
  19. struct intc_virq_list {
  20. unsigned int irq;
  21. struct intc_virq_list *next;
  22. };
  23. #define for_each_virq(entry, head) \
  24. for (entry = head; entry; entry = entry->next)
  25. /*
  26. * Tags for the radix tree
  27. */
  28. #define INTC_TAG_VIRQ_NEEDS_ALLOC 0
  29. void intc_irq_xlate_set(unsigned int irq, intc_enum id, struct intc_desc_int *d)
  30. {
  31. unsigned long flags;
  32. raw_spin_lock_irqsave(&intc_big_lock, flags);
  33. intc_irq_xlate[irq].enum_id = id;
  34. intc_irq_xlate[irq].desc = d;
  35. raw_spin_unlock_irqrestore(&intc_big_lock, flags);
  36. }
  37. struct intc_map_entry *intc_irq_xlate_get(unsigned int irq)
  38. {
  39. return intc_irq_xlate + irq;
  40. }
  41. int intc_irq_lookup(const char *chipname, intc_enum enum_id)
  42. {
  43. struct intc_map_entry *ptr;
  44. struct intc_desc_int *d;
  45. int irq = -1;
  46. list_for_each_entry(d, &intc_list, list) {
  47. int tagged;
  48. if (strcmp(d->chip.name, chipname) != 0)
  49. continue;
  50. /*
  51. * Catch early lookups for subgroup VIRQs that have not
  52. * yet been allocated an IRQ. This already includes a
  53. * fast-path out if the tree is untagged, so there is no
  54. * need to explicitly test the root tree.
  55. */
  56. tagged = radix_tree_tag_get(&d->tree, enum_id,
  57. INTC_TAG_VIRQ_NEEDS_ALLOC);
  58. if (unlikely(tagged))
  59. break;
  60. ptr = radix_tree_lookup(&d->tree, enum_id);
  61. if (ptr) {
  62. irq = ptr - intc_irq_xlate;
  63. break;
  64. }
  65. }
  66. return irq;
  67. }
  68. EXPORT_SYMBOL_GPL(intc_irq_lookup);
  69. static int add_virq_to_pirq(unsigned int irq, unsigned int virq)
  70. {
  71. struct intc_virq_list **last, *entry;
  72. struct irq_data *data = irq_get_irq_data(irq);
  73. /* scan for duplicates */
  74. last = (struct intc_virq_list **)&data->handler_data;
  75. for_each_virq(entry, data->handler_data) {
  76. if (entry->irq == virq)
  77. return 0;
  78. last = &entry->next;
  79. }
  80. entry = kzalloc(sizeof(struct intc_virq_list), GFP_ATOMIC);
  81. if (!entry) {
  82. pr_err("can't allocate VIRQ mapping for %d\n", virq);
  83. return -ENOMEM;
  84. }
  85. entry->irq = virq;
  86. *last = entry;
  87. return 0;
  88. }
  89. static void intc_virq_handler(unsigned int irq, struct irq_desc *desc)
  90. {
  91. struct irq_data *data = irq_get_irq_data(irq);
  92. struct irq_chip *chip = irq_data_get_irq_chip(data);
  93. struct intc_virq_list *entry, *vlist = irq_data_get_irq_handler_data(data);
  94. struct intc_desc_int *d = get_intc_desc(irq);
  95. chip->irq_mask_ack(data);
  96. for_each_virq(entry, vlist) {
  97. unsigned long addr, handle;
  98. handle = (unsigned long)irq_get_handler_data(entry->irq);
  99. addr = INTC_REG(d, _INTC_ADDR_E(handle), 0);
  100. if (intc_reg_fns[_INTC_FN(handle)](addr, handle, 0))
  101. generic_handle_irq(entry->irq);
  102. }
  103. chip->irq_unmask(data);
  104. }
  105. static unsigned long __init intc_subgroup_data(struct intc_subgroup *subgroup,
  106. struct intc_desc_int *d,
  107. unsigned int index)
  108. {
  109. unsigned int fn = REG_FN_TEST_BASE + (subgroup->reg_width >> 3) - 1;
  110. return _INTC_MK(fn, MODE_ENABLE_REG, intc_get_reg(d, subgroup->reg),
  111. 0, 1, (subgroup->reg_width - 1) - index);
  112. }
  113. static void __init intc_subgroup_init_one(struct intc_desc *desc,
  114. struct intc_desc_int *d,
  115. struct intc_subgroup *subgroup)
  116. {
  117. struct intc_map_entry *mapped;
  118. unsigned int pirq;
  119. unsigned long flags;
  120. int i;
  121. mapped = radix_tree_lookup(&d->tree, subgroup->parent_id);
  122. if (!mapped) {
  123. WARN_ON(1);
  124. return;
  125. }
  126. pirq = mapped - intc_irq_xlate;
  127. raw_spin_lock_irqsave(&d->lock, flags);
  128. for (i = 0; i < ARRAY_SIZE(subgroup->enum_ids); i++) {
  129. struct intc_subgroup_entry *entry;
  130. int err;
  131. if (!subgroup->enum_ids[i])
  132. continue;
  133. entry = kmalloc(sizeof(*entry), GFP_NOWAIT);
  134. if (!entry)
  135. break;
  136. entry->pirq = pirq;
  137. entry->enum_id = subgroup->enum_ids[i];
  138. entry->handle = intc_subgroup_data(subgroup, d, i);
  139. err = radix_tree_insert(&d->tree, entry->enum_id, entry);
  140. if (unlikely(err < 0))
  141. break;
  142. radix_tree_tag_set(&d->tree, entry->enum_id,
  143. INTC_TAG_VIRQ_NEEDS_ALLOC);
  144. }
  145. raw_spin_unlock_irqrestore(&d->lock, flags);
  146. }
  147. void __init intc_subgroup_init(struct intc_desc *desc, struct intc_desc_int *d)
  148. {
  149. int i;
  150. if (!desc->hw.subgroups)
  151. return;
  152. for (i = 0; i < desc->hw.nr_subgroups; i++)
  153. intc_subgroup_init_one(desc, d, desc->hw.subgroups + i);
  154. }
  155. static void __init intc_subgroup_map(struct intc_desc_int *d)
  156. {
  157. struct intc_subgroup_entry *entries[32];
  158. unsigned long flags;
  159. unsigned int nr_found;
  160. int i;
  161. raw_spin_lock_irqsave(&d->lock, flags);
  162. restart:
  163. nr_found = radix_tree_gang_lookup_tag_slot(&d->tree,
  164. (void ***)entries, 0, ARRAY_SIZE(entries),
  165. INTC_TAG_VIRQ_NEEDS_ALLOC);
  166. for (i = 0; i < nr_found; i++) {
  167. struct intc_subgroup_entry *entry;
  168. int irq;
  169. entry = radix_tree_deref_slot((void **)entries[i]);
  170. if (unlikely(!entry))
  171. continue;
  172. if (radix_tree_deref_retry(entry))
  173. goto restart;
  174. irq = create_irq();
  175. if (unlikely(irq < 0)) {
  176. pr_err("no more free IRQs, bailing..\n");
  177. break;
  178. }
  179. pr_info("Setting up a chained VIRQ from %d -> %d\n",
  180. irq, entry->pirq);
  181. intc_irq_xlate_set(irq, entry->enum_id, d);
  182. irq_set_chip_and_handler_name(irq, irq_get_chip(entry->pirq),
  183. handle_simple_irq, "virq");
  184. irq_set_chip_data(irq, irq_get_chip_data(entry->pirq));
  185. irq_set_handler_data(irq, (void *)entry->handle);
  186. /*
  187. * Set the virtual IRQ as non-threadable.
  188. */
  189. irq_set_nothread(irq);
  190. irq_set_chained_handler(entry->pirq, intc_virq_handler);
  191. add_virq_to_pirq(entry->pirq, irq);
  192. radix_tree_tag_clear(&d->tree, entry->enum_id,
  193. INTC_TAG_VIRQ_NEEDS_ALLOC);
  194. radix_tree_replace_slot((void **)entries[i],
  195. &intc_irq_xlate[irq]);
  196. }
  197. raw_spin_unlock_irqrestore(&d->lock, flags);
  198. }
  199. void __init intc_finalize(void)
  200. {
  201. struct intc_desc_int *d;
  202. list_for_each_entry(d, &intc_list, list)
  203. if (radix_tree_tagged(&d->tree, INTC_TAG_VIRQ_NEEDS_ALLOC))
  204. intc_subgroup_map(d);
  205. }