ras.c 12 KB

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