ras.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 <linux/fs.h>
  37. #include <linux/reboot.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 "pseries.h"
  51. static unsigned char ras_log_buf[RTAS_ERROR_LOG_MAX];
  52. static DEFINE_SPINLOCK(ras_log_buf_lock);
  53. static char global_mce_data_buf[RTAS_ERROR_LOG_MAX];
  54. static DEFINE_PER_CPU(__u64, mce_data_buf);
  55. static int ras_get_sensor_state_token;
  56. static int ras_check_exception_token;
  57. #define EPOW_SENSOR_TOKEN 9
  58. #define EPOW_SENSOR_INDEX 0
  59. static irqreturn_t ras_epow_interrupt(int irq, void *dev_id);
  60. static irqreturn_t ras_error_interrupt(int irq, void *dev_id);
  61. /*
  62. * Initialize handlers for the set of interrupts caused by hardware errors
  63. * and power system events.
  64. */
  65. static int __init init_ras_IRQ(void)
  66. {
  67. struct device_node *np;
  68. ras_get_sensor_state_token = rtas_token("get-sensor-state");
  69. ras_check_exception_token = rtas_token("check-exception");
  70. /* Internal Errors */
  71. np = of_find_node_by_path("/event-sources/internal-errors");
  72. if (np != NULL) {
  73. request_event_sources_irqs(np, ras_error_interrupt,
  74. "RAS_ERROR");
  75. of_node_put(np);
  76. }
  77. /* EPOW Events */
  78. np = of_find_node_by_path("/event-sources/epow-events");
  79. if (np != NULL) {
  80. request_event_sources_irqs(np, ras_epow_interrupt, "RAS_EPOW");
  81. of_node_put(np);
  82. }
  83. return 0;
  84. }
  85. subsys_initcall(init_ras_IRQ);
  86. #define EPOW_SHUTDOWN_NORMAL 1
  87. #define EPOW_SHUTDOWN_ON_UPS 2
  88. #define EPOW_SHUTDOWN_LOSS_OF_CRITICAL_FUNCTIONS 3
  89. #define EPOW_SHUTDOWN_AMBIENT_TEMPERATURE_TOO_HIGH 4
  90. static void handle_system_shutdown(char event_modifier)
  91. {
  92. switch (event_modifier) {
  93. case EPOW_SHUTDOWN_NORMAL:
  94. pr_emerg("Firmware initiated power off");
  95. orderly_poweroff(1);
  96. break;
  97. case EPOW_SHUTDOWN_ON_UPS:
  98. pr_emerg("Loss of power reported by firmware, system is "
  99. "running on UPS/battery");
  100. break;
  101. case EPOW_SHUTDOWN_LOSS_OF_CRITICAL_FUNCTIONS:
  102. pr_emerg("Loss of system critical functions reported by "
  103. "firmware");
  104. pr_emerg("Check RTAS error log for details");
  105. orderly_poweroff(1);
  106. break;
  107. case EPOW_SHUTDOWN_AMBIENT_TEMPERATURE_TOO_HIGH:
  108. pr_emerg("Ambient temperature too high reported by firmware");
  109. pr_emerg("Check RTAS error log for details");
  110. orderly_poweroff(1);
  111. break;
  112. default:
  113. pr_err("Unknown power/cooling shutdown event (modifier %d)",
  114. event_modifier);
  115. }
  116. }
  117. struct epow_errorlog {
  118. unsigned char sensor_value;
  119. unsigned char event_modifier;
  120. unsigned char extended_modifier;
  121. unsigned char reserved;
  122. unsigned char platform_reason;
  123. };
  124. #define EPOW_RESET 0
  125. #define EPOW_WARN_COOLING 1
  126. #define EPOW_WARN_POWER 2
  127. #define EPOW_SYSTEM_SHUTDOWN 3
  128. #define EPOW_SYSTEM_HALT 4
  129. #define EPOW_MAIN_ENCLOSURE 5
  130. #define EPOW_POWER_OFF 7
  131. void rtas_parse_epow_errlog(struct rtas_error_log *log)
  132. {
  133. struct pseries_errorlog *pseries_log;
  134. struct epow_errorlog *epow_log;
  135. char action_code;
  136. char modifier;
  137. pseries_log = get_pseries_errorlog(log, PSERIES_ELOG_SECT_ID_EPOW);
  138. if (pseries_log == NULL)
  139. return;
  140. epow_log = (struct epow_errorlog *)pseries_log->data;
  141. action_code = epow_log->sensor_value & 0xF; /* bottom 4 bits */
  142. modifier = epow_log->event_modifier & 0xF; /* bottom 4 bits */
  143. switch (action_code) {
  144. case EPOW_RESET:
  145. pr_err("Non critical power or cooling issue cleared");
  146. break;
  147. case EPOW_WARN_COOLING:
  148. pr_err("Non critical cooling issue reported by firmware");
  149. pr_err("Check RTAS error log for details");
  150. break;
  151. case EPOW_WARN_POWER:
  152. pr_err("Non critical power issue reported by firmware");
  153. pr_err("Check RTAS error log for details");
  154. break;
  155. case EPOW_SYSTEM_SHUTDOWN:
  156. handle_system_shutdown(epow_log->event_modifier);
  157. break;
  158. case EPOW_SYSTEM_HALT:
  159. pr_emerg("Firmware initiated power off");
  160. orderly_poweroff(1);
  161. break;
  162. case EPOW_MAIN_ENCLOSURE:
  163. case EPOW_POWER_OFF:
  164. pr_emerg("Critical power/cooling issue reported by firmware");
  165. pr_emerg("Check RTAS error log for details");
  166. pr_emerg("Immediate power off");
  167. emergency_sync();
  168. kernel_power_off();
  169. break;
  170. default:
  171. pr_err("Unknown power/cooling event (action code %d)",
  172. action_code);
  173. }
  174. }
  175. /* Handle environmental and power warning (EPOW) interrupts. */
  176. static irqreturn_t ras_epow_interrupt(int irq, void *dev_id)
  177. {
  178. int status;
  179. int state;
  180. int critical;
  181. status = rtas_call(ras_get_sensor_state_token, 2, 2, &state,
  182. EPOW_SENSOR_TOKEN, EPOW_SENSOR_INDEX);
  183. if (state > 3)
  184. critical = 1; /* Time Critical */
  185. else
  186. critical = 0;
  187. spin_lock(&ras_log_buf_lock);
  188. status = rtas_call(ras_check_exception_token, 6, 1, NULL,
  189. RTAS_VECTOR_EXTERNAL_INTERRUPT,
  190. virq_to_hw(irq),
  191. RTAS_EPOW_WARNING | RTAS_POWERMGM_EVENTS,
  192. critical, __pa(&ras_log_buf),
  193. rtas_get_error_log_max());
  194. log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, 0);
  195. rtas_parse_epow_errlog((struct rtas_error_log *)ras_log_buf);
  196. spin_unlock(&ras_log_buf_lock);
  197. return IRQ_HANDLED;
  198. }
  199. /*
  200. * Handle hardware error interrupts.
  201. *
  202. * RTAS check-exception is called to collect data on the exception. If
  203. * the error is deemed recoverable, we log a warning and return.
  204. * For nonrecoverable errors, an error is logged and we stop all processing
  205. * as quickly as possible in order to prevent propagation of the failure.
  206. */
  207. static irqreturn_t ras_error_interrupt(int irq, void *dev_id)
  208. {
  209. struct rtas_error_log *rtas_elog;
  210. int status = 0xdeadbeef;
  211. int fatal;
  212. spin_lock(&ras_log_buf_lock);
  213. status = rtas_call(ras_check_exception_token, 6, 1, NULL,
  214. RTAS_VECTOR_EXTERNAL_INTERRUPT,
  215. virq_to_hw(irq),
  216. RTAS_INTERNAL_ERROR, 1 /*Time Critical */,
  217. __pa(&ras_log_buf),
  218. rtas_get_error_log_max());
  219. rtas_elog = (struct rtas_error_log *)ras_log_buf;
  220. if ((status == 0) && (rtas_elog->severity >= RTAS_SEVERITY_ERROR_SYNC))
  221. fatal = 1;
  222. else
  223. fatal = 0;
  224. /* format and print the extended information */
  225. log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, fatal);
  226. if (fatal) {
  227. udbg_printf("Fatal HW Error <0x%lx 0x%x>\n",
  228. *((unsigned long *)&ras_log_buf), status);
  229. printk(KERN_EMERG "Error: Fatal hardware error <0x%lx 0x%x>\n",
  230. *((unsigned long *)&ras_log_buf), status);
  231. #ifndef DEBUG_RTAS_POWER_OFF
  232. /* Don't actually power off when debugging so we can test
  233. * without actually failing while injecting errors.
  234. * Error data will not be logged to syslog.
  235. */
  236. ppc_md.power_off();
  237. #endif
  238. } else {
  239. udbg_printf("Recoverable HW Error <0x%lx 0x%x>\n",
  240. *((unsigned long *)&ras_log_buf), status);
  241. printk(KERN_WARNING
  242. "Warning: Recoverable hardware error <0x%lx 0x%x>\n",
  243. *((unsigned long *)&ras_log_buf), status);
  244. }
  245. spin_unlock(&ras_log_buf_lock);
  246. return IRQ_HANDLED;
  247. }
  248. /*
  249. * Some versions of FWNMI place the buffer inside the 4kB page starting at
  250. * 0x7000. Other versions place it inside the rtas buffer. We check both.
  251. */
  252. #define VALID_FWNMI_BUFFER(A) \
  253. ((((A) >= 0x7000) && ((A) < 0x7ff0)) || \
  254. (((A) >= rtas.base) && ((A) < (rtas.base + rtas.size - 16))))
  255. /*
  256. * Get the error information for errors coming through the
  257. * FWNMI vectors. The pt_regs' r3 will be updated to reflect
  258. * the actual r3 if possible, and a ptr to the error log entry
  259. * will be returned if found.
  260. *
  261. * If the RTAS error is not of the extended type, then we put it in a per
  262. * cpu 64bit buffer. If it is the extended type we use global_mce_data_buf.
  263. *
  264. * The global_mce_data_buf does not have any locks or protection around it,
  265. * if a second machine check comes in, or a system reset is done
  266. * before we have logged the error, then we will get corruption in the
  267. * error log. This is preferable over holding off on calling
  268. * ibm,nmi-interlock which would result in us checkstopping if a
  269. * second machine check did come in.
  270. */
  271. static struct rtas_error_log *fwnmi_get_errinfo(struct pt_regs *regs)
  272. {
  273. unsigned long *savep;
  274. struct rtas_error_log *h, *errhdr = NULL;
  275. if (!VALID_FWNMI_BUFFER(regs->gpr[3])) {
  276. printk(KERN_ERR "FWNMI: corrupt r3 0x%016lx\n", regs->gpr[3]);
  277. return NULL;
  278. }
  279. savep = __va(regs->gpr[3]);
  280. regs->gpr[3] = savep[0]; /* restore original r3 */
  281. /* If it isn't an extended log we can use the per cpu 64bit buffer */
  282. h = (struct rtas_error_log *)&savep[1];
  283. if (!h->extended) {
  284. memcpy(&__get_cpu_var(mce_data_buf), h, sizeof(__u64));
  285. errhdr = (struct rtas_error_log *)&__get_cpu_var(mce_data_buf);
  286. } else {
  287. int len;
  288. len = max_t(int, 8+h->extended_log_length, RTAS_ERROR_LOG_MAX);
  289. memset(global_mce_data_buf, 0, RTAS_ERROR_LOG_MAX);
  290. memcpy(global_mce_data_buf, h, len);
  291. errhdr = (struct rtas_error_log *)global_mce_data_buf;
  292. }
  293. return errhdr;
  294. }
  295. /* Call this when done with the data returned by FWNMI_get_errinfo.
  296. * It will release the saved data area for other CPUs in the
  297. * partition to receive FWNMI errors.
  298. */
  299. static void fwnmi_release_errinfo(void)
  300. {
  301. int ret = rtas_call(rtas_token("ibm,nmi-interlock"), 0, 1, NULL);
  302. if (ret != 0)
  303. printk(KERN_ERR "FWNMI: nmi-interlock failed: %d\n", ret);
  304. }
  305. int pSeries_system_reset_exception(struct pt_regs *regs)
  306. {
  307. if (fwnmi_active) {
  308. struct rtas_error_log *errhdr = fwnmi_get_errinfo(regs);
  309. if (errhdr) {
  310. /* XXX Should look at FWNMI information */
  311. }
  312. fwnmi_release_errinfo();
  313. }
  314. return 0; /* need to perform reset */
  315. }
  316. /*
  317. * See if we can recover from a machine check exception.
  318. * This is only called on power4 (or above) and only via
  319. * the Firmware Non-Maskable Interrupts (fwnmi) handler
  320. * which provides the error analysis for us.
  321. *
  322. * Return 1 if corrected (or delivered a signal).
  323. * Return 0 if there is nothing we can do.
  324. */
  325. static int recover_mce(struct pt_regs *regs, struct rtas_error_log *err)
  326. {
  327. int recovered = 0;
  328. if (!(regs->msr & MSR_RI)) {
  329. /* If MSR_RI isn't set, we cannot recover */
  330. recovered = 0;
  331. } else if (err->disposition == RTAS_DISP_FULLY_RECOVERED) {
  332. /* Platform corrected itself */
  333. recovered = 1;
  334. } else if (err->disposition == RTAS_DISP_LIMITED_RECOVERY) {
  335. /* Platform corrected itself but could be degraded */
  336. printk(KERN_ERR "MCE: limited recovery, system may "
  337. "be degraded\n");
  338. recovered = 1;
  339. } else if (user_mode(regs) && !is_global_init(current) &&
  340. err->severity == RTAS_SEVERITY_ERROR_SYNC) {
  341. /*
  342. * If we received a synchronous error when in userspace
  343. * kill the task. Firmware may report details of the fail
  344. * asynchronously, so we can't rely on the target and type
  345. * fields being valid here.
  346. */
  347. printk(KERN_ERR "MCE: uncorrectable error, killing task "
  348. "%s:%d\n", current->comm, current->pid);
  349. _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
  350. recovered = 1;
  351. }
  352. log_error((char *)err, ERR_TYPE_RTAS_LOG, 0);
  353. return recovered;
  354. }
  355. /*
  356. * Handle a machine check.
  357. *
  358. * Note that on Power 4 and beyond Firmware Non-Maskable Interrupts (fwnmi)
  359. * should be present. If so the handler which called us tells us if the
  360. * error was recovered (never true if RI=0).
  361. *
  362. * On hardware prior to Power 4 these exceptions were asynchronous which
  363. * means we can't tell exactly where it occurred and so we can't recover.
  364. */
  365. int pSeries_machine_check_exception(struct pt_regs *regs)
  366. {
  367. struct rtas_error_log *errp;
  368. if (fwnmi_active) {
  369. errp = fwnmi_get_errinfo(regs);
  370. fwnmi_release_errinfo();
  371. if (errp && recover_mce(regs, errp))
  372. return 1;
  373. }
  374. return 0;
  375. }