io_event_irq.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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/module.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. /* pSeries event log format */
  60. /* Two bytes ASCII section IDs */
  61. #define PSERIES_ELOG_SECT_ID_PRIV_HDR (('P' << 8) | 'H')
  62. #define PSERIES_ELOG_SECT_ID_USER_HDR (('U' << 8) | 'H')
  63. #define PSERIES_ELOG_SECT_ID_PRIMARY_SRC (('P' << 8) | 'S')
  64. #define PSERIES_ELOG_SECT_ID_EXTENDED_UH (('E' << 8) | 'H')
  65. #define PSERIES_ELOG_SECT_ID_FAILING_MTMS (('M' << 8) | 'T')
  66. #define PSERIES_ELOG_SECT_ID_SECONDARY_SRC (('S' << 8) | 'S')
  67. #define PSERIES_ELOG_SECT_ID_DUMP_LOCATOR (('D' << 8) | 'H')
  68. #define PSERIES_ELOG_SECT_ID_FW_ERROR (('S' << 8) | 'W')
  69. #define PSERIES_ELOG_SECT_ID_IMPACT_PART_ID (('L' << 8) | 'P')
  70. #define PSERIES_ELOG_SECT_ID_LOGIC_RESOURCE_ID (('L' << 8) | 'R')
  71. #define PSERIES_ELOG_SECT_ID_HMC_ID (('H' << 8) | 'M')
  72. #define PSERIES_ELOG_SECT_ID_EPOW (('E' << 8) | 'P')
  73. #define PSERIES_ELOG_SECT_ID_IO_EVENT (('I' << 8) | 'E')
  74. #define PSERIES_ELOG_SECT_ID_MANUFACT_INFO (('M' << 8) | 'I')
  75. #define PSERIES_ELOG_SECT_ID_CALL_HOME (('C' << 8) | 'H')
  76. #define PSERIES_ELOG_SECT_ID_USER_DEF (('U' << 8) | 'D')
  77. /* Vendor specific Platform Event Log Format, Version 6, section header */
  78. struct pseries_elog_section {
  79. uint16_t id; /* 0x00 2-byte ASCII section ID */
  80. uint16_t length; /* 0x02 Section length in bytes */
  81. uint8_t version; /* 0x04 Section version */
  82. uint8_t subtype; /* 0x05 Section subtype */
  83. uint16_t creator_component; /* 0x06 Creator component ID */
  84. uint8_t data[]; /* 0x08 Start of section data */
  85. };
  86. static char ioei_rtas_buf[RTAS_DATA_BUF_SIZE] __cacheline_aligned;
  87. /**
  88. * Find data portion of a specific section in RTAS extended event log.
  89. * @elog: RTAS error/event log.
  90. * @sect_id: secsion ID.
  91. *
  92. * Return:
  93. * pointer to the section data of the specified section
  94. * NULL if not found
  95. */
  96. static struct pseries_elog_section *find_xelog_section(struct rtas_error_log *elog,
  97. uint16_t sect_id)
  98. {
  99. struct rtas_ext_event_log_v6 *xelog =
  100. (struct rtas_ext_event_log_v6 *) elog->buffer;
  101. struct pseries_elog_section *sect;
  102. unsigned char *p, *log_end;
  103. /* Check that we understand the format */
  104. if (elog->extended_log_length < sizeof(struct rtas_ext_event_log_v6) ||
  105. xelog->log_format != RTAS_V6EXT_LOG_FORMAT_EVENT_LOG ||
  106. xelog->company_id != RTAS_V6EXT_COMPANY_ID_IBM)
  107. return NULL;
  108. log_end = elog->buffer + elog->extended_log_length;
  109. p = xelog->vendor_log;
  110. while (p < log_end) {
  111. sect = (struct pseries_elog_section *)p;
  112. if (sect->id == sect_id)
  113. return sect;
  114. p += sect->length;
  115. }
  116. return NULL;
  117. }
  118. /**
  119. * Find the data portion of an IO Event section from event log.
  120. * @elog: RTAS error/event log.
  121. *
  122. * Return:
  123. * pointer to a valid IO event section data. NULL if not found.
  124. */
  125. static struct pseries_io_event * ioei_find_event(struct rtas_error_log *elog)
  126. {
  127. struct pseries_elog_section *sect;
  128. /* We should only ever get called for io-event interrupts, but if
  129. * we do get called for another type then something went wrong so
  130. * make some noise about it.
  131. * RTAS_TYPE_IO only exists in extended event log version 6 or later.
  132. * No need to check event log version.
  133. */
  134. if (unlikely(elog->type != RTAS_TYPE_IO)) {
  135. printk_once(KERN_WARNING "io_event_irq: Unexpected event type %d",
  136. elog->type);
  137. return NULL;
  138. }
  139. sect = find_xelog_section(elog, PSERIES_ELOG_SECT_ID_IO_EVENT);
  140. if (unlikely(!sect)) {
  141. printk_once(KERN_WARNING "io_event_irq: RTAS extended event "
  142. "log does not contain an IO Event section. "
  143. "Could be a bug in system firmware!\n");
  144. return NULL;
  145. }
  146. return (struct pseries_io_event *) &sect->data;
  147. }
  148. /*
  149. * PAPR:
  150. * - check-exception returns the first found error or event and clear that
  151. * error or event so it is reported once.
  152. * - Each interrupt returns one event. If a plateform chooses to report
  153. * multiple events through a single interrupt, it must ensure that the
  154. * interrupt remains asserted until check-exception has been used to
  155. * process all out-standing events for that interrupt.
  156. *
  157. * Implementation notes:
  158. * - Events must be processed in the order they are returned. Hence,
  159. * sequential in nature.
  160. * - The owner of an event is determined by combinations of scope,
  161. * event type, and sub-type. There is no easy way to pre-sort clients
  162. * by scope or event type alone. For example, Torrent ISR route change
  163. * event is reported with scope 0x00 (Not Applicatable) rather than
  164. * 0x3B (Torrent-hub). It is better to let the clients to identify
  165. * who owns the the event.
  166. */
  167. static irqreturn_t ioei_interrupt(int irq, void *dev_id)
  168. {
  169. struct pseries_io_event *event;
  170. int rtas_rc;
  171. for (;;) {
  172. rtas_rc = rtas_call(ioei_check_exception_token, 6, 1, NULL,
  173. RTAS_VECTOR_EXTERNAL_INTERRUPT,
  174. virq_to_hw(irq),
  175. RTAS_IO_EVENTS, 1 /* Time Critical */,
  176. __pa(ioei_rtas_buf),
  177. RTAS_DATA_BUF_SIZE);
  178. if (rtas_rc != 0)
  179. break;
  180. event = ioei_find_event((struct rtas_error_log *)ioei_rtas_buf);
  181. if (!event)
  182. continue;
  183. atomic_notifier_call_chain(&pseries_ioei_notifier_list,
  184. 0, event);
  185. }
  186. return IRQ_HANDLED;
  187. }
  188. static int __init ioei_init(void)
  189. {
  190. struct device_node *np;
  191. ioei_check_exception_token = rtas_token("check-exception");
  192. if (ioei_check_exception_token == RTAS_UNKNOWN_SERVICE) {
  193. pr_warning("IO Event IRQ not supported on this system !\n");
  194. return -ENODEV;
  195. }
  196. np = of_find_node_by_path("/event-sources/ibm,io-events");
  197. if (np) {
  198. request_event_sources_irqs(np, ioei_interrupt, "IO_EVENT");
  199. of_node_put(np);
  200. } else {
  201. pr_err("io_event_irq: No ibm,io-events on system! "
  202. "IO Event interrupt disabled.\n");
  203. return -ENODEV;
  204. }
  205. return 0;
  206. }
  207. machine_subsys_initcall(pseries, ioei_init);