opal-irqchip.c 7.7 KB

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