ras.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Copyright (C) 2001 Dave Engebretsen IBM Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. /* Change Activity:
  19. * 2001/09/21 : engebret : Created with minimal EPOW and HW exception support.
  20. * End Change Activity
  21. */
  22. #include <linux/errno.h>
  23. #include <linux/threads.h>
  24. #include <linux/kernel_stat.h>
  25. #include <linux/signal.h>
  26. #include <linux/sched.h>
  27. #include <linux/ioport.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/timex.h>
  30. #include <linux/init.h>
  31. #include <linux/delay.h>
  32. #include <linux/irq.h>
  33. #include <linux/random.h>
  34. #include <linux/sysrq.h>
  35. #include <linux/bitops.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/system.h>
  38. #include <asm/io.h>
  39. #include <asm/pgtable.h>
  40. #include <asm/irq.h>
  41. #include <asm/cache.h>
  42. #include <asm/prom.h>
  43. #include <asm/ptrace.h>
  44. #include <asm/machdep.h>
  45. #include <asm/rtas.h>
  46. #include <asm/udbg.h>
  47. #include <asm/firmware.h>
  48. #include "pseries.h"
  49. static unsigned char ras_log_buf[RTAS_ERROR_LOG_MAX];
  50. static DEFINE_SPINLOCK(ras_log_buf_lock);
  51. static char global_mce_data_buf[RTAS_ERROR_LOG_MAX];
  52. static DEFINE_PER_CPU(__u64, mce_data_buf);
  53. static int ras_get_sensor_state_token;
  54. static int ras_check_exception_token;
  55. #define EPOW_SENSOR_TOKEN 9
  56. #define EPOW_SENSOR_INDEX 0
  57. static irqreturn_t ras_epow_interrupt(int irq, void *dev_id);
  58. static irqreturn_t ras_error_interrupt(int irq, void *dev_id);
  59. /*
  60. * Initialize handlers for the set of interrupts caused by hardware errors
  61. * and power system events.
  62. */
  63. static int __init init_ras_IRQ(void)
  64. {
  65. struct device_node *np;
  66. ras_get_sensor_state_token = rtas_token("get-sensor-state");
  67. ras_check_exception_token = rtas_token("check-exception");
  68. /* Internal Errors */
  69. np = of_find_node_by_path("/event-sources/internal-errors");
  70. if (np != NULL) {
  71. request_event_sources_irqs(np, ras_error_interrupt,
  72. "RAS_ERROR");
  73. of_node_put(np);
  74. }
  75. /* EPOW Events */
  76. np = of_find_node_by_path("/event-sources/epow-events");
  77. if (np != NULL) {
  78. request_event_sources_irqs(np, ras_epow_interrupt, "RAS_EPOW");
  79. of_node_put(np);
  80. }
  81. return 0;
  82. }
  83. __initcall(init_ras_IRQ);
  84. /*
  85. * Handle power subsystem events (EPOW).
  86. *
  87. * Presently we just log the event has occurred. This should be fixed
  88. * to examine the type of power failure and take appropriate action where
  89. * the time horizon permits something useful to be done.
  90. */
  91. static irqreturn_t ras_epow_interrupt(int irq, void *dev_id)
  92. {
  93. int status = 0xdeadbeef;
  94. int state = 0;
  95. int critical;
  96. status = rtas_call(ras_get_sensor_state_token, 2, 2, &state,
  97. EPOW_SENSOR_TOKEN, EPOW_SENSOR_INDEX);
  98. if (state > 3)
  99. critical = 1; /* Time Critical */
  100. else
  101. critical = 0;
  102. spin_lock(&ras_log_buf_lock);
  103. status = rtas_call(ras_check_exception_token, 6, 1, NULL,
  104. RTAS_VECTOR_EXTERNAL_INTERRUPT,
  105. virq_to_hw(irq),
  106. RTAS_EPOW_WARNING | RTAS_POWERMGM_EVENTS,
  107. critical, __pa(&ras_log_buf),
  108. rtas_get_error_log_max());
  109. udbg_printf("EPOW <0x%lx 0x%x 0x%x>\n",
  110. *((unsigned long *)&ras_log_buf), status, state);
  111. printk(KERN_WARNING "EPOW <0x%lx 0x%x 0x%x>\n",
  112. *((unsigned long *)&ras_log_buf), status, state);
  113. /* format and print the extended information */
  114. log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, 0);
  115. spin_unlock(&ras_log_buf_lock);
  116. return IRQ_HANDLED;
  117. }
  118. /*
  119. * Handle hardware error interrupts.
  120. *
  121. * RTAS check-exception is called to collect data on the exception. If
  122. * the error is deemed recoverable, we log a warning and return.
  123. * For nonrecoverable errors, an error is logged and we stop all processing
  124. * as quickly as possible in order to prevent propagation of the failure.
  125. */
  126. static irqreturn_t ras_error_interrupt(int irq, void *dev_id)
  127. {
  128. struct rtas_error_log *rtas_elog;
  129. int status = 0xdeadbeef;
  130. int fatal;
  131. spin_lock(&ras_log_buf_lock);
  132. status = rtas_call(ras_check_exception_token, 6, 1, NULL,
  133. RTAS_VECTOR_EXTERNAL_INTERRUPT,
  134. virq_to_hw(irq),
  135. RTAS_INTERNAL_ERROR, 1 /*Time Critical */,
  136. __pa(&ras_log_buf),
  137. rtas_get_error_log_max());
  138. rtas_elog = (struct rtas_error_log *)ras_log_buf;
  139. if ((status == 0) && (rtas_elog->severity >= RTAS_SEVERITY_ERROR_SYNC))
  140. fatal = 1;
  141. else
  142. fatal = 0;
  143. /* format and print the extended information */
  144. log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, fatal);
  145. if (fatal) {
  146. udbg_printf("Fatal HW Error <0x%lx 0x%x>\n",
  147. *((unsigned long *)&ras_log_buf), status);
  148. printk(KERN_EMERG "Error: Fatal hardware error <0x%lx 0x%x>\n",
  149. *((unsigned long *)&ras_log_buf), status);
  150. #ifndef DEBUG_RTAS_POWER_OFF
  151. /* Don't actually power off when debugging so we can test
  152. * without actually failing while injecting errors.
  153. * Error data will not be logged to syslog.
  154. */
  155. ppc_md.power_off();
  156. #endif
  157. } else {
  158. udbg_printf("Recoverable HW Error <0x%lx 0x%x>\n",
  159. *((unsigned long *)&ras_log_buf), status);
  160. printk(KERN_WARNING
  161. "Warning: Recoverable hardware error <0x%lx 0x%x>\n",
  162. *((unsigned long *)&ras_log_buf), status);
  163. }
  164. spin_unlock(&ras_log_buf_lock);
  165. return IRQ_HANDLED;
  166. }
  167. /*
  168. * Some versions of FWNMI place the buffer inside the 4kB page starting at
  169. * 0x7000. Other versions place it inside the rtas buffer. We check both.
  170. */
  171. #define VALID_FWNMI_BUFFER(A) \
  172. ((((A) >= 0x7000) && ((A) < 0x7ff0)) || \
  173. (((A) >= rtas.base) && ((A) < (rtas.base + rtas.size - 16))))
  174. /*
  175. * Get the error information for errors coming through the
  176. * FWNMI vectors. The pt_regs' r3 will be updated to reflect
  177. * the actual r3 if possible, and a ptr to the error log entry
  178. * will be returned if found.
  179. *
  180. * If the RTAS error is not of the extended type, then we put it in a per
  181. * cpu 64bit buffer. If it is the extended type we use global_mce_data_buf.
  182. *
  183. * The global_mce_data_buf does not have any locks or protection around it,
  184. * if a second machine check comes in, or a system reset is done
  185. * before we have logged the error, then we will get corruption in the
  186. * error log. This is preferable over holding off on calling
  187. * ibm,nmi-interlock which would result in us checkstopping if a
  188. * second machine check did come in.
  189. */
  190. static struct rtas_error_log *fwnmi_get_errinfo(struct pt_regs *regs)
  191. {
  192. unsigned long *savep;
  193. struct rtas_error_log *h, *errhdr = NULL;
  194. if (!VALID_FWNMI_BUFFER(regs->gpr[3])) {
  195. printk(KERN_ERR "FWNMI: corrupt r3 0x%016lx\n", regs->gpr[3]);
  196. return NULL;
  197. }
  198. savep = __va(regs->gpr[3]);
  199. regs->gpr[3] = savep[0]; /* restore original r3 */
  200. /* If it isn't an extended log we can use the per cpu 64bit buffer */
  201. h = (struct rtas_error_log *)&savep[1];
  202. if (!h->extended) {
  203. memcpy(&__get_cpu_var(mce_data_buf), h, sizeof(__u64));
  204. errhdr = (struct rtas_error_log *)&__get_cpu_var(mce_data_buf);
  205. } else {
  206. int len;
  207. len = max_t(int, 8+h->extended_log_length, RTAS_ERROR_LOG_MAX);
  208. memset(global_mce_data_buf, 0, RTAS_ERROR_LOG_MAX);
  209. memcpy(global_mce_data_buf, h, len);
  210. errhdr = (struct rtas_error_log *)global_mce_data_buf;
  211. }
  212. return errhdr;
  213. }
  214. /* Call this when done with the data returned by FWNMI_get_errinfo.
  215. * It will release the saved data area for other CPUs in the
  216. * partition to receive FWNMI errors.
  217. */
  218. static void fwnmi_release_errinfo(void)
  219. {
  220. int ret = rtas_call(rtas_token("ibm,nmi-interlock"), 0, 1, NULL);
  221. if (ret != 0)
  222. printk(KERN_ERR "FWNMI: nmi-interlock failed: %d\n", ret);
  223. }
  224. int pSeries_system_reset_exception(struct pt_regs *regs)
  225. {
  226. if (fwnmi_active) {
  227. struct rtas_error_log *errhdr = fwnmi_get_errinfo(regs);
  228. if (errhdr) {
  229. /* XXX Should look at FWNMI information */
  230. }
  231. fwnmi_release_errinfo();
  232. }
  233. return 0; /* need to perform reset */
  234. }
  235. /*
  236. * See if we can recover from a machine check exception.
  237. * This is only called on power4 (or above) and only via
  238. * the Firmware Non-Maskable Interrupts (fwnmi) handler
  239. * which provides the error analysis for us.
  240. *
  241. * Return 1 if corrected (or delivered a signal).
  242. * Return 0 if there is nothing we can do.
  243. */
  244. static int recover_mce(struct pt_regs *regs, struct rtas_error_log *err)
  245. {
  246. int recovered = 0;
  247. if (!(regs->msr & MSR_RI)) {
  248. /* If MSR_RI isn't set, we cannot recover */
  249. recovered = 0;
  250. } else if (err->disposition == RTAS_DISP_FULLY_RECOVERED) {
  251. /* Platform corrected itself */
  252. recovered = 1;
  253. } else if (err->disposition == RTAS_DISP_LIMITED_RECOVERY) {
  254. /* Platform corrected itself but could be degraded */
  255. printk(KERN_ERR "MCE: limited recovery, system may "
  256. "be degraded\n");
  257. recovered = 1;
  258. } else if (user_mode(regs) && !is_global_init(current) &&
  259. err->severity == RTAS_SEVERITY_ERROR_SYNC) {
  260. /*
  261. * If we received a synchronous error when in userspace
  262. * kill the task. Firmware may report details of the fail
  263. * asynchronously, so we can't rely on the target and type
  264. * fields being valid here.
  265. */
  266. printk(KERN_ERR "MCE: uncorrectable error, killing task "
  267. "%s:%d\n", current->comm, current->pid);
  268. _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
  269. recovered = 1;
  270. }
  271. log_error((char *)err, ERR_TYPE_RTAS_LOG, 0);
  272. return recovered;
  273. }
  274. /*
  275. * Handle a machine check.
  276. *
  277. * Note that on Power 4 and beyond Firmware Non-Maskable Interrupts (fwnmi)
  278. * should be present. If so the handler which called us tells us if the
  279. * error was recovered (never true if RI=0).
  280. *
  281. * On hardware prior to Power 4 these exceptions were asynchronous which
  282. * means we can't tell exactly where it occurred and so we can't recover.
  283. */
  284. int pSeries_machine_check_exception(struct pt_regs *regs)
  285. {
  286. struct rtas_error_log *errp;
  287. if (fwnmi_active) {
  288. errp = fwnmi_get_errinfo(regs);
  289. fwnmi_release_errinfo();
  290. if (errp && recover_mce(regs, errp))
  291. return 1;
  292. }
  293. return 0;
  294. }