ras.c 12 KB

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