opal-irqchip.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * This file implements an irqchip for OPAL events. Whenever there is
  3. * an interrupt that is handled by OPAL we get passed a list of events
  4. * that Linux needs to do something about. These basically look like
  5. * interrupts to Linux so we implement an irqchip to handle them.
  6. *
  7. * Copyright Alistair Popple, IBM Corporation 2014.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. */
  14. #include <linux/bitops.h>
  15. #include <linux/irq.h>
  16. #include <linux/irqchip.h>
  17. #include <linux/irqdomain.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/kthread.h>
  23. #include <linux/delay.h>
  24. #include <linux/slab.h>
  25. #include <asm/machdep.h>
  26. #include <asm/opal.h>
  27. #include "powernv.h"
  28. /* Maximum number of events supported by OPAL firmware */
  29. #define MAX_NUM_EVENTS 64
  30. struct opal_event_irqchip {
  31. struct irq_chip irqchip;
  32. struct irq_domain *domain;
  33. unsigned long mask;
  34. };
  35. static struct opal_event_irqchip opal_event_irqchip;
  36. static u64 last_outstanding_events;
  37. static unsigned int opal_irq_count;
  38. static unsigned int *opal_irqs;
  39. void opal_handle_events(void)
  40. {
  41. __be64 events = 0;
  42. u64 e;
  43. e = READ_ONCE(last_outstanding_events) & opal_event_irqchip.mask;
  44. again:
  45. while (e) {
  46. int virq, hwirq;
  47. hwirq = fls64(e) - 1;
  48. e &= ~BIT_ULL(hwirq);
  49. local_irq_disable();
  50. virq = irq_find_mapping(opal_event_irqchip.domain, hwirq);
  51. if (virq) {
  52. irq_enter();
  53. generic_handle_irq(virq);
  54. irq_exit();
  55. }
  56. local_irq_enable();
  57. cond_resched();
  58. }
  59. last_outstanding_events = 0;
  60. if (opal_poll_events(&events) != OPAL_SUCCESS)
  61. return;
  62. e = be64_to_cpu(events) & opal_event_irqchip.mask;
  63. if (e)
  64. goto again;
  65. }
  66. bool opal_have_pending_events(void)
  67. {
  68. if (last_outstanding_events & opal_event_irqchip.mask)
  69. return true;
  70. return false;
  71. }
  72. static void opal_event_mask(struct irq_data *d)
  73. {
  74. clear_bit(d->hwirq, &opal_event_irqchip.mask);
  75. }
  76. static void opal_event_unmask(struct irq_data *d)
  77. {
  78. set_bit(d->hwirq, &opal_event_irqchip.mask);
  79. if (opal_have_pending_events())
  80. opal_wake_poller();
  81. }
  82. static int opal_event_set_type(struct irq_data *d, unsigned int flow_type)
  83. {
  84. /*
  85. * For now we only support level triggered events. The irq
  86. * handler will be called continuously until the event has
  87. * been cleared in OPAL.
  88. */
  89. if (flow_type != IRQ_TYPE_LEVEL_HIGH)
  90. return -EINVAL;
  91. return 0;
  92. }
  93. static struct opal_event_irqchip opal_event_irqchip = {
  94. .irqchip = {
  95. .name = "OPAL EVT",
  96. .irq_mask = opal_event_mask,
  97. .irq_unmask = opal_event_unmask,
  98. .irq_set_type = opal_event_set_type,
  99. },
  100. .mask = 0,
  101. };
  102. static int opal_event_map(struct irq_domain *d, unsigned int irq,
  103. irq_hw_number_t hwirq)
  104. {
  105. irq_set_chip_data(irq, &opal_event_irqchip);
  106. irq_set_chip_and_handler(irq, &opal_event_irqchip.irqchip,
  107. handle_level_irq);
  108. return 0;
  109. }
  110. static irqreturn_t opal_interrupt(int irq, void *data)
  111. {
  112. __be64 events;
  113. opal_handle_interrupt(virq_to_hw(irq), &events);
  114. last_outstanding_events = be64_to_cpu(events);
  115. if (opal_have_pending_events())
  116. opal_wake_poller();
  117. return IRQ_HANDLED;
  118. }
  119. static int opal_event_match(struct irq_domain *h, struct device_node *node,
  120. enum irq_domain_bus_token bus_token)
  121. {
  122. return irq_domain_get_of_node(h) == node;
  123. }
  124. static int opal_event_xlate(struct irq_domain *h, struct device_node *np,
  125. const u32 *intspec, unsigned int intsize,
  126. irq_hw_number_t *out_hwirq, unsigned int *out_flags)
  127. {
  128. *out_hwirq = intspec[0];
  129. *out_flags = IRQ_TYPE_LEVEL_HIGH;
  130. return 0;
  131. }
  132. static const struct irq_domain_ops opal_event_domain_ops = {
  133. .match = opal_event_match,
  134. .map = opal_event_map,
  135. .xlate = opal_event_xlate,
  136. };
  137. void opal_event_shutdown(void)
  138. {
  139. unsigned int i;
  140. /* First free interrupts, which will also mask them */
  141. for (i = 0; i < opal_irq_count; i++) {
  142. if (!opal_irqs[i])
  143. continue;
  144. if (in_interrupt() || irqs_disabled())
  145. disable_irq_nosync(opal_irqs[i]);
  146. else
  147. free_irq(opal_irqs[i], NULL);
  148. opal_irqs[i] = 0;
  149. }
  150. }
  151. int __init opal_event_init(void)
  152. {
  153. struct device_node *dn, *opal_node;
  154. const char **names;
  155. u32 *irqs;
  156. int i, rc;
  157. opal_node = of_find_node_by_path("/ibm,opal");
  158. if (!opal_node) {
  159. pr_warn("opal: Node not found\n");
  160. return -ENODEV;
  161. }
  162. /* If dn is NULL it means the domain won't be linked to a DT
  163. * node so therefore irq_of_parse_and_map(...) wont work. But
  164. * that shouldn't be problem because if we're running a
  165. * version of skiboot that doesn't have the dn then the
  166. * devices won't have the correct properties and will have to
  167. * fall back to the legacy method (opal_event_request(...))
  168. * anyway. */
  169. dn = of_find_compatible_node(NULL, NULL, "ibm,opal-event");
  170. opal_event_irqchip.domain = irq_domain_add_linear(dn, MAX_NUM_EVENTS,
  171. &opal_event_domain_ops, &opal_event_irqchip);
  172. of_node_put(dn);
  173. if (!opal_event_irqchip.domain) {
  174. pr_warn("opal: Unable to create irq domain\n");
  175. rc = -ENOMEM;
  176. goto out;
  177. }
  178. /* Get opal-interrupts property and names if present */
  179. rc = of_property_count_u32_elems(opal_node, "opal-interrupts");
  180. if (rc < 0)
  181. goto out;
  182. opal_irq_count = rc;
  183. pr_debug("Found %d interrupts reserved for OPAL\n", opal_irq_count);
  184. irqs = kcalloc(opal_irq_count, sizeof(*irqs), GFP_KERNEL);
  185. names = kcalloc(opal_irq_count, sizeof(*names), GFP_KERNEL);
  186. opal_irqs = kcalloc(opal_irq_count, sizeof(*opal_irqs), GFP_KERNEL);
  187. if (WARN_ON(!irqs || !names || !opal_irqs))
  188. goto out_free;
  189. rc = of_property_read_u32_array(opal_node, "opal-interrupts",
  190. irqs, opal_irq_count);
  191. if (rc < 0) {
  192. pr_err("Error %d reading opal-interrupts array\n", rc);
  193. goto out_free;
  194. }
  195. /* It's not an error for the names to be missing */
  196. of_property_read_string_array(opal_node, "opal-interrupts-names",
  197. names, opal_irq_count);
  198. /* Install interrupt handlers */
  199. for (i = 0; i < opal_irq_count; i++) {
  200. unsigned int virq;
  201. char *name;
  202. /* Get hardware and virtual IRQ */
  203. virq = irq_create_mapping(NULL, irqs[i]);
  204. if (!virq) {
  205. pr_warn("Failed to map irq 0x%x\n", irqs[i]);
  206. continue;
  207. }
  208. if (names[i] && strlen(names[i]))
  209. name = kasprintf(GFP_KERNEL, "opal-%s", names[i]);
  210. else
  211. name = kasprintf(GFP_KERNEL, "opal");
  212. /* Install interrupt handler */
  213. rc = request_irq(virq, opal_interrupt, IRQF_TRIGGER_LOW,
  214. name, NULL);
  215. if (rc) {
  216. irq_dispose_mapping(virq);
  217. pr_warn("Error %d requesting irq %d (0x%x)\n",
  218. rc, virq, irqs[i]);
  219. continue;
  220. }
  221. /* Cache IRQ */
  222. opal_irqs[i] = virq;
  223. }
  224. out_free:
  225. kfree(irqs);
  226. kfree(names);
  227. out:
  228. of_node_put(opal_node);
  229. return rc;
  230. }
  231. machine_arch_initcall(powernv, opal_event_init);
  232. /**
  233. * opal_event_request(unsigned int opal_event_nr) - Request an event
  234. * @opal_event_nr: the opal event number to request
  235. *
  236. * This routine can be used to find the linux virq number which can
  237. * then be passed to request_irq to assign a handler for a particular
  238. * opal event. This should only be used by legacy devices which don't
  239. * have proper device tree bindings. Most devices should use
  240. * irq_of_parse_and_map() instead.
  241. */
  242. int opal_event_request(unsigned int opal_event_nr)
  243. {
  244. if (WARN_ON_ONCE(!opal_event_irqchip.domain))
  245. return 0;
  246. return irq_create_mapping(opal_event_irqchip.domain, opal_event_nr);
  247. }
  248. EXPORT_SYMBOL(opal_event_request);