ras.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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/slab.h>
  32. #include <linux/pci.h>
  33. #include <linux/delay.h>
  34. #include <linux/irq.h>
  35. #include <linux/random.h>
  36. #include <linux/sysrq.h>
  37. #include <linux/bitops.h>
  38. #include <asm/uaccess.h>
  39. #include <asm/system.h>
  40. #include <asm/io.h>
  41. #include <asm/pgtable.h>
  42. #include <asm/irq.h>
  43. #include <asm/cache.h>
  44. #include <asm/prom.h>
  45. #include <asm/ptrace.h>
  46. #include <asm/machdep.h>
  47. #include <asm/rtas.h>
  48. #include <asm/udbg.h>
  49. #include <asm/firmware.h>
  50. #include "ras.h"
  51. static unsigned char ras_log_buf[RTAS_ERROR_LOG_MAX];
  52. static DEFINE_SPINLOCK(ras_log_buf_lock);
  53. char mce_data_buf[RTAS_ERROR_LOG_MAX];
  54. static int ras_get_sensor_state_token;
  55. static int ras_check_exception_token;
  56. #define EPOW_SENSOR_TOKEN 9
  57. #define EPOW_SENSOR_INDEX 0
  58. #define RAS_VECTOR_OFFSET 0x500
  59. static irqreturn_t ras_epow_interrupt(int irq, void *dev_id,
  60. struct pt_regs * regs);
  61. static irqreturn_t ras_error_interrupt(int irq, void *dev_id,
  62. struct pt_regs * regs);
  63. /* #define DEBUG */
  64. static void request_ras_irqs(struct device_node *np,
  65. irqreturn_t (*handler)(int, void *, struct pt_regs *),
  66. const char *name)
  67. {
  68. int i, index, count = 0;
  69. struct of_irq oirq;
  70. u32 *opicprop;
  71. unsigned int opicplen;
  72. unsigned int virqs[16];
  73. /* Check for obsolete "open-pic-interrupt" property. If present, then
  74. * map those interrupts using the default interrupt host and default
  75. * trigger
  76. */
  77. opicprop = (u32 *)get_property(np, "open-pic-interrupt", &opicplen);
  78. if (opicprop) {
  79. opicplen /= sizeof(u32);
  80. for (i = 0; i < opicplen; i++) {
  81. if (count > 15)
  82. break;
  83. virqs[count] = irq_create_mapping(NULL, *(opicprop++),
  84. IRQ_TYPE_NONE);
  85. if (virqs[count] == NO_IRQ)
  86. printk(KERN_ERR "Unable to allocate interrupt "
  87. "number for %s\n", np->full_name);
  88. else
  89. count++;
  90. }
  91. }
  92. /* Else use normal interrupt tree parsing */
  93. else {
  94. /* First try to do a proper OF tree parsing */
  95. for (index = 0; of_irq_map_one(np, index, &oirq) == 0;
  96. index++) {
  97. if (count > 15)
  98. break;
  99. virqs[count] = irq_create_of_mapping(oirq.controller,
  100. oirq.specifier,
  101. oirq.size);
  102. if (virqs[count] == NO_IRQ)
  103. printk(KERN_ERR "Unable to allocate interrupt "
  104. "number for %s\n", np->full_name);
  105. else
  106. count++;
  107. }
  108. }
  109. /* Now request them */
  110. for (i = 0; i < count; i++) {
  111. if (request_irq(virqs[i], handler, 0, name, NULL)) {
  112. printk(KERN_ERR "Unable to request interrupt %d for "
  113. "%s\n", virqs[i], np->full_name);
  114. return;
  115. }
  116. }
  117. }
  118. /*
  119. * Initialize handlers for the set of interrupts caused by hardware errors
  120. * and power system events.
  121. */
  122. static int __init init_ras_IRQ(void)
  123. {
  124. struct device_node *np;
  125. ras_get_sensor_state_token = rtas_token("get-sensor-state");
  126. ras_check_exception_token = rtas_token("check-exception");
  127. /* Internal Errors */
  128. np = of_find_node_by_path("/event-sources/internal-errors");
  129. if (np != NULL) {
  130. request_ras_irqs(np, ras_error_interrupt, "RAS_ERROR");
  131. of_node_put(np);
  132. }
  133. /* EPOW Events */
  134. np = of_find_node_by_path("/event-sources/epow-events");
  135. if (np != NULL) {
  136. request_ras_irqs(np, ras_epow_interrupt, "RAS_EPOW");
  137. of_node_put(np);
  138. }
  139. return 0;
  140. }
  141. __initcall(init_ras_IRQ);
  142. /*
  143. * Handle power subsystem events (EPOW).
  144. *
  145. * Presently we just log the event has occurred. This should be fixed
  146. * to examine the type of power failure and take appropriate action where
  147. * the time horizon permits something useful to be done.
  148. */
  149. static irqreturn_t
  150. ras_epow_interrupt(int irq, void *dev_id, struct pt_regs * regs)
  151. {
  152. int status = 0xdeadbeef;
  153. int state = 0;
  154. int critical;
  155. status = rtas_call(ras_get_sensor_state_token, 2, 2, &state,
  156. EPOW_SENSOR_TOKEN, EPOW_SENSOR_INDEX);
  157. if (state > 3)
  158. critical = 1; /* Time Critical */
  159. else
  160. critical = 0;
  161. spin_lock(&ras_log_buf_lock);
  162. status = rtas_call(ras_check_exception_token, 6, 1, NULL,
  163. RAS_VECTOR_OFFSET,
  164. irq_map[irq].hwirq,
  165. RTAS_EPOW_WARNING | RTAS_POWERMGM_EVENTS,
  166. critical, __pa(&ras_log_buf),
  167. rtas_get_error_log_max());
  168. udbg_printf("EPOW <0x%lx 0x%x 0x%x>\n",
  169. *((unsigned long *)&ras_log_buf), status, state);
  170. printk(KERN_WARNING "EPOW <0x%lx 0x%x 0x%x>\n",
  171. *((unsigned long *)&ras_log_buf), status, state);
  172. /* format and print the extended information */
  173. log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, 0);
  174. spin_unlock(&ras_log_buf_lock);
  175. return IRQ_HANDLED;
  176. }
  177. /*
  178. * Handle hardware error interrupts.
  179. *
  180. * RTAS check-exception is called to collect data on the exception. If
  181. * the error is deemed recoverable, we log a warning and return.
  182. * For nonrecoverable errors, an error is logged and we stop all processing
  183. * as quickly as possible in order to prevent propagation of the failure.
  184. */
  185. static irqreturn_t
  186. ras_error_interrupt(int irq, void *dev_id, struct pt_regs * regs)
  187. {
  188. struct rtas_error_log *rtas_elog;
  189. int status = 0xdeadbeef;
  190. int fatal;
  191. spin_lock(&ras_log_buf_lock);
  192. status = rtas_call(ras_check_exception_token, 6, 1, NULL,
  193. RAS_VECTOR_OFFSET,
  194. irq_map[irq].hwirq,
  195. RTAS_INTERNAL_ERROR, 1 /*Time Critical */,
  196. __pa(&ras_log_buf),
  197. rtas_get_error_log_max());
  198. rtas_elog = (struct rtas_error_log *)ras_log_buf;
  199. if ((status == 0) && (rtas_elog->severity >= RTAS_SEVERITY_ERROR_SYNC))
  200. fatal = 1;
  201. else
  202. fatal = 0;
  203. /* format and print the extended information */
  204. log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, fatal);
  205. if (fatal) {
  206. udbg_printf("Fatal HW Error <0x%lx 0x%x>\n",
  207. *((unsigned long *)&ras_log_buf), status);
  208. printk(KERN_EMERG "Error: Fatal hardware error <0x%lx 0x%x>\n",
  209. *((unsigned long *)&ras_log_buf), status);
  210. #ifndef DEBUG
  211. /* Don't actually power off when debugging so we can test
  212. * without actually failing while injecting errors.
  213. * Error data will not be logged to syslog.
  214. */
  215. ppc_md.power_off();
  216. #endif
  217. } else {
  218. udbg_printf("Recoverable HW Error <0x%lx 0x%x>\n",
  219. *((unsigned long *)&ras_log_buf), status);
  220. printk(KERN_WARNING
  221. "Warning: Recoverable hardware error <0x%lx 0x%x>\n",
  222. *((unsigned long *)&ras_log_buf), status);
  223. }
  224. spin_unlock(&ras_log_buf_lock);
  225. return IRQ_HANDLED;
  226. }
  227. /* Get the error information for errors coming through the
  228. * FWNMI vectors. The pt_regs' r3 will be updated to reflect
  229. * the actual r3 if possible, and a ptr to the error log entry
  230. * will be returned if found.
  231. *
  232. * The mce_data_buf does not have any locks or protection around it,
  233. * if a second machine check comes in, or a system reset is done
  234. * before we have logged the error, then we will get corruption in the
  235. * error log. This is preferable over holding off on calling
  236. * ibm,nmi-interlock which would result in us checkstopping if a
  237. * second machine check did come in.
  238. */
  239. static struct rtas_error_log *fwnmi_get_errinfo(struct pt_regs *regs)
  240. {
  241. unsigned long errdata = regs->gpr[3];
  242. struct rtas_error_log *errhdr = NULL;
  243. unsigned long *savep;
  244. if ((errdata >= 0x7000 && errdata < 0x7fff0) ||
  245. (errdata >= rtas.base && errdata < rtas.base + rtas.size - 16)) {
  246. savep = __va(errdata);
  247. regs->gpr[3] = savep[0]; /* restore original r3 */
  248. memset(mce_data_buf, 0, RTAS_ERROR_LOG_MAX);
  249. memcpy(mce_data_buf, (char *)(savep + 1), RTAS_ERROR_LOG_MAX);
  250. errhdr = (struct rtas_error_log *)mce_data_buf;
  251. } else {
  252. printk("FWNMI: corrupt r3\n");
  253. }
  254. return errhdr;
  255. }
  256. /* Call this when done with the data returned by FWNMI_get_errinfo.
  257. * It will release the saved data area for other CPUs in the
  258. * partition to receive FWNMI errors.
  259. */
  260. static void fwnmi_release_errinfo(void)
  261. {
  262. int ret = rtas_call(rtas_token("ibm,nmi-interlock"), 0, 1, NULL);
  263. if (ret != 0)
  264. printk("FWNMI: nmi-interlock failed: %d\n", ret);
  265. }
  266. int pSeries_system_reset_exception(struct pt_regs *regs)
  267. {
  268. if (fwnmi_active) {
  269. struct rtas_error_log *errhdr = fwnmi_get_errinfo(regs);
  270. if (errhdr) {
  271. /* XXX Should look at FWNMI information */
  272. }
  273. fwnmi_release_errinfo();
  274. }
  275. return 0; /* need to perform reset */
  276. }
  277. /*
  278. * See if we can recover from a machine check exception.
  279. * This is only called on power4 (or above) and only via
  280. * the Firmware Non-Maskable Interrupts (fwnmi) handler
  281. * which provides the error analysis for us.
  282. *
  283. * Return 1 if corrected (or delivered a signal).
  284. * Return 0 if there is nothing we can do.
  285. */
  286. static int recover_mce(struct pt_regs *regs, struct rtas_error_log * err)
  287. {
  288. int nonfatal = 0;
  289. if (err->disposition == RTAS_DISP_FULLY_RECOVERED) {
  290. /* Platform corrected itself */
  291. nonfatal = 1;
  292. } else if ((regs->msr & MSR_RI) &&
  293. user_mode(regs) &&
  294. err->severity == RTAS_SEVERITY_ERROR_SYNC &&
  295. err->disposition == RTAS_DISP_NOT_RECOVERED &&
  296. err->target == RTAS_TARGET_MEMORY &&
  297. err->type == RTAS_TYPE_ECC_UNCORR &&
  298. !(current->pid == 0 || current->pid == 1)) {
  299. /* Kill off a user process with an ECC error */
  300. printk(KERN_ERR "MCE: uncorrectable ecc error for pid %d\n",
  301. current->pid);
  302. /* XXX something better for ECC error? */
  303. _exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
  304. nonfatal = 1;
  305. }
  306. log_error((char *)err, ERR_TYPE_RTAS_LOG, !nonfatal);
  307. return nonfatal;
  308. }
  309. /*
  310. * Handle a machine check.
  311. *
  312. * Note that on Power 4 and beyond Firmware Non-Maskable Interrupts (fwnmi)
  313. * should be present. If so the handler which called us tells us if the
  314. * error was recovered (never true if RI=0).
  315. *
  316. * On hardware prior to Power 4 these exceptions were asynchronous which
  317. * means we can't tell exactly where it occurred and so we can't recover.
  318. */
  319. int pSeries_machine_check_exception(struct pt_regs *regs)
  320. {
  321. struct rtas_error_log *errp;
  322. if (fwnmi_active) {
  323. errp = fwnmi_get_errinfo(regs);
  324. fwnmi_release_errinfo();
  325. if (errp && recover_mce(regs, errp))
  326. return 1;
  327. }
  328. return 0;
  329. }