irqdomain.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include <linux/irq.h>
  2. #include <linux/irqdomain.h>
  3. #include <linux/module.h>
  4. #include <linux/mutex.h>
  5. #include <linux/of.h>
  6. #include <linux/of_address.h>
  7. #include <linux/slab.h>
  8. static LIST_HEAD(irq_domain_list);
  9. static DEFINE_MUTEX(irq_domain_mutex);
  10. /**
  11. * irq_domain_add() - Register an irq_domain
  12. * @domain: ptr to initialized irq_domain structure
  13. *
  14. * Registers an irq_domain structure. The irq_domain must at a minimum be
  15. * initialized with an ops structure pointer, and either a ->to_irq hook or
  16. * a valid irq_base value. Everything else is optional.
  17. */
  18. void irq_domain_add(struct irq_domain *domain)
  19. {
  20. struct irq_data *d;
  21. int hwirq;
  22. /*
  23. * This assumes that the irq_domain owner has already allocated
  24. * the irq_descs. This block will be removed when support for dynamic
  25. * allocation of irq_descs is added to irq_domain.
  26. */
  27. for (hwirq = 0; hwirq < domain->nr_irq; hwirq++) {
  28. d = irq_get_irq_data(irq_domain_to_irq(domain, hwirq));
  29. if (d || d->domain) {
  30. /* things are broken; just report, don't clean up */
  31. WARN(1, "error: irq_desc already assigned to a domain");
  32. return;
  33. }
  34. d->domain = domain;
  35. d->hwirq = hwirq;
  36. }
  37. mutex_lock(&irq_domain_mutex);
  38. list_add(&domain->list, &irq_domain_list);
  39. mutex_unlock(&irq_domain_mutex);
  40. }
  41. /**
  42. * irq_domain_del() - Unregister an irq_domain
  43. * @domain: ptr to registered irq_domain.
  44. */
  45. void irq_domain_del(struct irq_domain *domain)
  46. {
  47. struct irq_data *d;
  48. int hwirq;
  49. mutex_lock(&irq_domain_mutex);
  50. list_del(&domain->list);
  51. mutex_unlock(&irq_domain_mutex);
  52. /* Clear the irq_domain assignments */
  53. for (hwirq = 0; hwirq < domain->nr_irq; hwirq++) {
  54. d = irq_get_irq_data(irq_domain_to_irq(domain, hwirq));
  55. d->domain = NULL;
  56. }
  57. }
  58. #if defined(CONFIG_OF_IRQ)
  59. /**
  60. * irq_create_of_mapping() - Map a linux irq number from a DT interrupt spec
  61. *
  62. * Used by the device tree interrupt mapping code to translate a device tree
  63. * interrupt specifier to a valid linux irq number. Returns either a valid
  64. * linux IRQ number or 0.
  65. *
  66. * When the caller no longer need the irq number returned by this function it
  67. * should arrange to call irq_dispose_mapping().
  68. */
  69. unsigned int irq_create_of_mapping(struct device_node *controller,
  70. const u32 *intspec, unsigned int intsize)
  71. {
  72. struct irq_domain *domain;
  73. unsigned long hwirq;
  74. unsigned int irq, type;
  75. int rc = -EINVAL;
  76. /* Find a domain which can translate the irq spec */
  77. mutex_lock(&irq_domain_mutex);
  78. list_for_each_entry(domain, &irq_domain_list, list) {
  79. if (!domain->ops->dt_translate)
  80. continue;
  81. rc = domain->ops->dt_translate(domain, controller,
  82. intspec, intsize, &hwirq, &type);
  83. if (rc == 0)
  84. break;
  85. }
  86. mutex_unlock(&irq_domain_mutex);
  87. if (rc != 0)
  88. return 0;
  89. irq = irq_domain_to_irq(domain, hwirq);
  90. if (type != IRQ_TYPE_NONE)
  91. irq_set_irq_type(irq, type);
  92. pr_debug("%s: mapped hwirq=%i to irq=%i, flags=%x\n",
  93. controller->full_name, (int)hwirq, irq, type);
  94. return irq;
  95. }
  96. EXPORT_SYMBOL_GPL(irq_create_of_mapping);
  97. /**
  98. * irq_dispose_mapping() - Discard a mapping created by irq_create_of_mapping()
  99. * @irq: linux irq number to be discarded
  100. *
  101. * Calling this function indicates the caller no longer needs a reference to
  102. * the linux irq number returned by a prior call to irq_create_of_mapping().
  103. */
  104. void irq_dispose_mapping(unsigned int irq)
  105. {
  106. /*
  107. * nothing yet; will be filled when support for dynamic allocation of
  108. * irq_descs is added to irq_domain
  109. */
  110. }
  111. EXPORT_SYMBOL_GPL(irq_dispose_mapping);
  112. int irq_domain_simple_dt_translate(struct irq_domain *d,
  113. struct device_node *controller,
  114. const u32 *intspec, unsigned int intsize,
  115. unsigned long *out_hwirq, unsigned int *out_type)
  116. {
  117. if (d->of_node != controller)
  118. return -EINVAL;
  119. if (intsize < 1)
  120. return -EINVAL;
  121. *out_hwirq = intspec[0];
  122. *out_type = IRQ_TYPE_NONE;
  123. if (intsize > 1)
  124. *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
  125. return 0;
  126. }
  127. struct irq_domain_ops irq_domain_simple_ops = {
  128. .dt_translate = irq_domain_simple_dt_translate,
  129. };
  130. EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
  131. /**
  132. * irq_domain_create_simple() - Set up a 'simple' translation range
  133. */
  134. void irq_domain_add_simple(struct device_node *controller, int irq_base)
  135. {
  136. struct irq_domain *domain;
  137. domain = kzalloc(sizeof(*domain), GFP_KERNEL);
  138. if (!domain) {
  139. WARN_ON(1);
  140. return;
  141. }
  142. domain->irq_base = irq_base;
  143. domain->of_node = of_node_get(controller);
  144. domain->ops = &irq_domain_simple_ops;
  145. irq_domain_add(domain);
  146. }
  147. EXPORT_SYMBOL_GPL(irq_domain_add_simple);
  148. void irq_domain_generate_simple(const struct of_device_id *match,
  149. u64 phys_base, unsigned int irq_start)
  150. {
  151. struct device_node *node;
  152. pr_info("looking for phys_base=%llx, irq_start=%i\n",
  153. (unsigned long long) phys_base, (int) irq_start);
  154. node = of_find_matching_node_by_address(NULL, match, phys_base);
  155. if (node)
  156. irq_domain_add_simple(node, irq_start);
  157. else
  158. pr_info("no node found\n");
  159. }
  160. EXPORT_SYMBOL_GPL(irq_domain_generate_simple);
  161. #endif /* CONFIG_OF_IRQ */