io_event_irq.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright 2010 2011 Mark Nelson and Tseng-Hui (Frank) Lin, IBM Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #include <linux/errno.h>
  10. #include <linux/slab.h>
  11. #include <linux/export.h>
  12. #include <linux/irq.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/of.h>
  15. #include <linux/list.h>
  16. #include <linux/notifier.h>
  17. #include <asm/machdep.h>
  18. #include <asm/rtas.h>
  19. #include <asm/irq.h>
  20. #include <asm/io_event_irq.h>
  21. #include "pseries.h"
  22. /*
  23. * IO event interrupt is a mechanism provided by RTAS to return
  24. * information about hardware error and non-error events. Device
  25. * drivers can register their event handlers to receive events.
  26. * Device drivers are expected to use atomic_notifier_chain_register()
  27. * and atomic_notifier_chain_unregister() to register and unregister
  28. * their event handlers. Since multiple IO event types and scopes
  29. * share an IO event interrupt, the event handlers are called one
  30. * by one until the IO event is claimed by one of the handlers.
  31. * The event handlers are expected to return NOTIFY_OK if the
  32. * event is handled by the event handler or NOTIFY_DONE if the
  33. * event does not belong to the handler.
  34. *
  35. * Usage:
  36. *
  37. * Notifier function:
  38. * #include <asm/io_event_irq.h>
  39. * int event_handler(struct notifier_block *nb, unsigned long val, void *data) {
  40. * p = (struct pseries_io_event_sect_data *) data;
  41. * if (! is_my_event(p->scope, p->event_type)) return NOTIFY_DONE;
  42. * :
  43. * :
  44. * return NOTIFY_OK;
  45. * }
  46. * struct notifier_block event_nb = {
  47. * .notifier_call = event_handler,
  48. * }
  49. *
  50. * Registration:
  51. * atomic_notifier_chain_register(&pseries_ioei_notifier_list, &event_nb);
  52. *
  53. * Unregistration:
  54. * atomic_notifier_chain_unregister(&pseries_ioei_notifier_list, &event_nb);
  55. */
  56. ATOMIC_NOTIFIER_HEAD(pseries_ioei_notifier_list);
  57. EXPORT_SYMBOL_GPL(pseries_ioei_notifier_list);
  58. static int ioei_check_exception_token;
  59. static char ioei_rtas_buf[RTAS_DATA_BUF_SIZE] __cacheline_aligned;
  60. /**
  61. * Find the data portion of an IO Event section from event log.
  62. * @elog: RTAS error/event log.
  63. *
  64. * Return:
  65. * pointer to a valid IO event section data. NULL if not found.
  66. */
  67. static struct pseries_io_event * ioei_find_event(struct rtas_error_log *elog)
  68. {
  69. struct pseries_errorlog *sect;
  70. /* We should only ever get called for io-event interrupts, but if
  71. * we do get called for another type then something went wrong so
  72. * make some noise about it.
  73. * RTAS_TYPE_IO only exists in extended event log version 6 or later.
  74. * No need to check event log version.
  75. */
  76. if (unlikely(rtas_error_type(elog) != RTAS_TYPE_IO)) {
  77. printk_once(KERN_WARNING"io_event_irq: Unexpected event type %d",
  78. rtas_error_type(elog));
  79. return NULL;
  80. }
  81. sect = get_pseries_errorlog(elog, PSERIES_ELOG_SECT_ID_IO_EVENT);
  82. if (unlikely(!sect)) {
  83. printk_once(KERN_WARNING "io_event_irq: RTAS extended event "
  84. "log does not contain an IO Event section. "
  85. "Could be a bug in system firmware!\n");
  86. return NULL;
  87. }
  88. return (struct pseries_io_event *) &sect->data;
  89. }
  90. /*
  91. * PAPR:
  92. * - check-exception returns the first found error or event and clear that
  93. * error or event so it is reported once.
  94. * - Each interrupt returns one event. If a plateform chooses to report
  95. * multiple events through a single interrupt, it must ensure that the
  96. * interrupt remains asserted until check-exception has been used to
  97. * process all out-standing events for that interrupt.
  98. *
  99. * Implementation notes:
  100. * - Events must be processed in the order they are returned. Hence,
  101. * sequential in nature.
  102. * - The owner of an event is determined by combinations of scope,
  103. * event type, and sub-type. There is no easy way to pre-sort clients
  104. * by scope or event type alone. For example, Torrent ISR route change
  105. * event is reported with scope 0x00 (Not Applicatable) rather than
  106. * 0x3B (Torrent-hub). It is better to let the clients to identify
  107. * who owns the event.
  108. */
  109. static irqreturn_t ioei_interrupt(int irq, void *dev_id)
  110. {
  111. struct pseries_io_event *event;
  112. int rtas_rc;
  113. for (;;) {
  114. rtas_rc = rtas_call(ioei_check_exception_token, 6, 1, NULL,
  115. RTAS_VECTOR_EXTERNAL_INTERRUPT,
  116. virq_to_hw(irq),
  117. RTAS_IO_EVENTS, 1 /* Time Critical */,
  118. __pa(ioei_rtas_buf),
  119. RTAS_DATA_BUF_SIZE);
  120. if (rtas_rc != 0)
  121. break;
  122. event = ioei_find_event((struct rtas_error_log *)ioei_rtas_buf);
  123. if (!event)
  124. continue;
  125. atomic_notifier_call_chain(&pseries_ioei_notifier_list,
  126. 0, event);
  127. }
  128. return IRQ_HANDLED;
  129. }
  130. static int __init ioei_init(void)
  131. {
  132. struct device_node *np;
  133. ioei_check_exception_token = rtas_token("check-exception");
  134. if (ioei_check_exception_token == RTAS_UNKNOWN_SERVICE)
  135. return -ENODEV;
  136. np = of_find_node_by_path("/event-sources/ibm,io-events");
  137. if (np) {
  138. request_event_sources_irqs(np, ioei_interrupt, "IO_EVENT");
  139. pr_info("IBM I/O event interrupts enabled\n");
  140. of_node_put(np);
  141. } else {
  142. return -ENODEV;
  143. }
  144. return 0;
  145. }
  146. machine_subsys_initcall(pseries, ioei_init);