virq.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 *entry;
  72. struct intc_virq_list **last = NULL;
  73. /* scan for duplicates */
  74. for_each_virq(entry, irq_get_handler_data(irq)) {
  75. if (entry->irq == virq)
  76. return 0;
  77. last = &entry->next;
  78. }
  79. entry = kzalloc(sizeof(struct intc_virq_list), GFP_ATOMIC);
  80. if (!entry)
  81. return -ENOMEM;
  82. entry->irq = virq;
  83. if (last)
  84. *last = entry;
  85. else
  86. irq_set_handler_data(irq, entry);
  87. return 0;
  88. }
  89. static void intc_virq_handler(struct irq_desc *desc)
  90. {
  91. unsigned int irq = irq_desc_get_irq(desc);
  92. struct irq_data *data = irq_desc_get_irq_data(desc);
  93. struct irq_chip *chip = irq_data_get_irq_chip(data);
  94. struct intc_virq_list *entry, *vlist = irq_data_get_irq_handler_data(data);
  95. struct intc_desc_int *d = get_intc_desc(irq);
  96. chip->irq_mask_ack(data);
  97. for_each_virq(entry, vlist) {
  98. unsigned long addr, handle;
  99. struct irq_desc *vdesc = irq_to_desc(entry->irq);
  100. if (vdesc) {
  101. handle = (unsigned long)irq_desc_get_handler_data(vdesc);
  102. addr = INTC_REG(d, _INTC_ADDR_E(handle), 0);
  103. if (intc_reg_fns[_INTC_FN(handle)](addr, handle, 0))
  104. generic_handle_irq_desc(vdesc);
  105. }
  106. }
  107. chip->irq_unmask(data);
  108. }
  109. static unsigned long __init intc_subgroup_data(struct intc_subgroup *subgroup,
  110. struct intc_desc_int *d,
  111. unsigned int index)
  112. {
  113. unsigned int fn = REG_FN_TEST_BASE + (subgroup->reg_width >> 3) - 1;
  114. return _INTC_MK(fn, MODE_ENABLE_REG, intc_get_reg(d, subgroup->reg),
  115. 0, 1, (subgroup->reg_width - 1) - index);
  116. }
  117. static void __init intc_subgroup_init_one(struct intc_desc *desc,
  118. struct intc_desc_int *d,
  119. struct intc_subgroup *subgroup)
  120. {
  121. struct intc_map_entry *mapped;
  122. unsigned int pirq;
  123. unsigned long flags;
  124. int i;
  125. mapped = radix_tree_lookup(&d->tree, subgroup->parent_id);
  126. if (!mapped) {
  127. WARN_ON(1);
  128. return;
  129. }
  130. pirq = mapped - intc_irq_xlate;
  131. raw_spin_lock_irqsave(&d->lock, flags);
  132. for (i = 0; i < ARRAY_SIZE(subgroup->enum_ids); i++) {
  133. struct intc_subgroup_entry *entry;
  134. int err;
  135. if (!subgroup->enum_ids[i])
  136. continue;
  137. entry = kmalloc(sizeof(*entry), GFP_NOWAIT);
  138. if (!entry)
  139. break;
  140. entry->pirq = pirq;
  141. entry->enum_id = subgroup->enum_ids[i];
  142. entry->handle = intc_subgroup_data(subgroup, d, i);
  143. err = radix_tree_insert(&d->tree, entry->enum_id, entry);
  144. if (unlikely(err < 0))
  145. break;
  146. radix_tree_tag_set(&d->tree, entry->enum_id,
  147. INTC_TAG_VIRQ_NEEDS_ALLOC);
  148. }
  149. raw_spin_unlock_irqrestore(&d->lock, flags);
  150. }
  151. void __init intc_subgroup_init(struct intc_desc *desc, struct intc_desc_int *d)
  152. {
  153. int i;
  154. if (!desc->hw.subgroups)
  155. return;
  156. for (i = 0; i < desc->hw.nr_subgroups; i++)
  157. intc_subgroup_init_one(desc, d, desc->hw.subgroups + i);
  158. }
  159. static void __init intc_subgroup_map(struct intc_desc_int *d)
  160. {
  161. struct intc_subgroup_entry *entries[32];
  162. unsigned long flags;
  163. unsigned int nr_found;
  164. int i;
  165. raw_spin_lock_irqsave(&d->lock, flags);
  166. restart:
  167. nr_found = radix_tree_gang_lookup_tag_slot(&d->tree,
  168. (void ***)entries, 0, ARRAY_SIZE(entries),
  169. INTC_TAG_VIRQ_NEEDS_ALLOC);
  170. for (i = 0; i < nr_found; i++) {
  171. struct intc_subgroup_entry *entry;
  172. int irq;
  173. entry = radix_tree_deref_slot((void **)entries[i]);
  174. if (unlikely(!entry))
  175. continue;
  176. if (radix_tree_deref_retry(entry))
  177. goto restart;
  178. irq = irq_alloc_desc(numa_node_id());
  179. if (unlikely(irq < 0)) {
  180. pr_err("no more free IRQs, bailing..\n");
  181. break;
  182. }
  183. activate_irq(irq);
  184. pr_info("Setting up a chained VIRQ from %d -> %d\n",
  185. irq, entry->pirq);
  186. intc_irq_xlate_set(irq, entry->enum_id, d);
  187. irq_set_chip_and_handler_name(irq, irq_get_chip(entry->pirq),
  188. handle_simple_irq, "virq");
  189. irq_set_chip_data(irq, irq_get_chip_data(entry->pirq));
  190. irq_set_handler_data(irq, (void *)entry->handle);
  191. /*
  192. * Set the virtual IRQ as non-threadable.
  193. */
  194. irq_set_nothread(irq);
  195. /* Set handler data before installing the handler */
  196. add_virq_to_pirq(entry->pirq, irq);
  197. irq_set_chained_handler(entry->pirq, intc_virq_handler);
  198. radix_tree_tag_clear(&d->tree, entry->enum_id,
  199. INTC_TAG_VIRQ_NEEDS_ALLOC);
  200. radix_tree_replace_slot(&d->tree, (void **)entries[i],
  201. &intc_irq_xlate[irq]);
  202. }
  203. raw_spin_unlock_irqrestore(&d->lock, flags);
  204. }
  205. void __init intc_finalize(void)
  206. {
  207. struct intc_desc_int *d;
  208. list_for_each_entry(d, &intc_list, list)
  209. if (radix_tree_tagged(&d->tree, INTC_TAG_VIRQ_NEEDS_ALLOC))
  210. intc_subgroup_map(d);
  211. }