ras.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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_fast(EPOW_SENSOR_TOKEN, EPOW_SENSOR_INDEX,
  159. &state);
  160. if (state > 3)
  161. critical = 1; /* Time Critical */
  162. else
  163. critical = 0;
  164. spin_lock(&ras_log_buf_lock);
  165. status = rtas_call(ras_check_exception_token, 6, 1, NULL,
  166. RTAS_VECTOR_EXTERNAL_INTERRUPT,
  167. virq_to_hw(irq),
  168. RTAS_EPOW_WARNING,
  169. critical, __pa(&ras_log_buf),
  170. rtas_get_error_log_max());
  171. log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, 0);
  172. rtas_parse_epow_errlog((struct rtas_error_log *)ras_log_buf);
  173. spin_unlock(&ras_log_buf_lock);
  174. return IRQ_HANDLED;
  175. }
  176. /*
  177. * Handle hardware error interrupts.
  178. *
  179. * RTAS check-exception is called to collect data on the exception. If
  180. * the error is deemed recoverable, we log a warning and return.
  181. * For nonrecoverable errors, an error is logged and we stop all processing
  182. * as quickly as possible in order to prevent propagation of the failure.
  183. */
  184. static irqreturn_t ras_error_interrupt(int irq, void *dev_id)
  185. {
  186. struct rtas_error_log *rtas_elog;
  187. int status;
  188. int fatal;
  189. spin_lock(&ras_log_buf_lock);
  190. status = rtas_call(ras_check_exception_token, 6, 1, NULL,
  191. RTAS_VECTOR_EXTERNAL_INTERRUPT,
  192. virq_to_hw(irq),
  193. RTAS_INTERNAL_ERROR, 1 /* Time Critical */,
  194. __pa(&ras_log_buf),
  195. rtas_get_error_log_max());
  196. rtas_elog = (struct rtas_error_log *)ras_log_buf;
  197. if (status == 0 &&
  198. rtas_error_severity(rtas_elog) >= RTAS_SEVERITY_ERROR_SYNC)
  199. fatal = 1;
  200. else
  201. fatal = 0;
  202. /* format and print the extended information */
  203. log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, fatal);
  204. if (fatal) {
  205. pr_emerg("Fatal hardware error reported by firmware");
  206. pr_emerg("Check RTAS error log for details");
  207. pr_emerg("Immediate power off");
  208. emergency_sync();
  209. kernel_power_off();
  210. } else {
  211. pr_err("Recoverable hardware error reported by firmware");
  212. }
  213. spin_unlock(&ras_log_buf_lock);
  214. return IRQ_HANDLED;
  215. }
  216. /*
  217. * Some versions of FWNMI place the buffer inside the 4kB page starting at
  218. * 0x7000. Other versions place it inside the rtas buffer. We check both.
  219. */
  220. #define VALID_FWNMI_BUFFER(A) \
  221. ((((A) >= 0x7000) && ((A) < 0x7ff0)) || \
  222. (((A) >= rtas.base) && ((A) < (rtas.base + rtas.size - 16))))
  223. /*
  224. * Get the error information for errors coming through the
  225. * FWNMI vectors. The pt_regs' r3 will be updated to reflect
  226. * the actual r3 if possible, and a ptr to the error log entry
  227. * will be returned if found.
  228. *
  229. * If the RTAS error is not of the extended type, then we put it in a per
  230. * cpu 64bit buffer. If it is the extended type we use global_mce_data_buf.
  231. *
  232. * The global_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 *savep;
  242. struct rtas_error_log *h, *errhdr = NULL;
  243. /* Mask top two bits */
  244. regs->gpr[3] &= ~(0x3UL << 62);
  245. if (!VALID_FWNMI_BUFFER(regs->gpr[3])) {
  246. printk(KERN_ERR "FWNMI: corrupt r3 0x%016lx\n", regs->gpr[3]);
  247. return NULL;
  248. }
  249. savep = __va(regs->gpr[3]);
  250. regs->gpr[3] = be64_to_cpu(savep[0]); /* restore original r3 */
  251. /* If it isn't an extended log we can use the per cpu 64bit buffer */
  252. h = (struct rtas_error_log *)&savep[1];
  253. if (!rtas_error_extended(h)) {
  254. memcpy(this_cpu_ptr(&mce_data_buf), h, sizeof(__u64));
  255. errhdr = (struct rtas_error_log *)this_cpu_ptr(&mce_data_buf);
  256. } else {
  257. int len, error_log_length;
  258. error_log_length = 8 + rtas_error_extended_log_length(h);
  259. len = min_t(int, error_log_length, RTAS_ERROR_LOG_MAX);
  260. memset(global_mce_data_buf, 0, RTAS_ERROR_LOG_MAX);
  261. memcpy(global_mce_data_buf, h, len);
  262. errhdr = (struct rtas_error_log *)global_mce_data_buf;
  263. }
  264. return errhdr;
  265. }
  266. /* Call this when done with the data returned by FWNMI_get_errinfo.
  267. * It will release the saved data area for other CPUs in the
  268. * partition to receive FWNMI errors.
  269. */
  270. static void fwnmi_release_errinfo(void)
  271. {
  272. int ret = rtas_call(rtas_token("ibm,nmi-interlock"), 0, 1, NULL);
  273. if (ret != 0)
  274. printk(KERN_ERR "FWNMI: nmi-interlock failed: %d\n", ret);
  275. }
  276. int pSeries_system_reset_exception(struct pt_regs *regs)
  277. {
  278. if (fwnmi_active) {
  279. struct rtas_error_log *errhdr = fwnmi_get_errinfo(regs);
  280. if (errhdr) {
  281. /* XXX Should look at FWNMI information */
  282. }
  283. fwnmi_release_errinfo();
  284. }
  285. return 0; /* need to perform reset */
  286. }
  287. /*
  288. * See if we can recover from a machine check exception.
  289. * This is only called on power4 (or above) and only via
  290. * the Firmware Non-Maskable Interrupts (fwnmi) handler
  291. * which provides the error analysis for us.
  292. *
  293. * Return 1 if corrected (or delivered a signal).
  294. * Return 0 if there is nothing we can do.
  295. */
  296. static int recover_mce(struct pt_regs *regs, struct rtas_error_log *err)
  297. {
  298. int recovered = 0;
  299. int disposition = rtas_error_disposition(err);
  300. if (!(regs->msr & MSR_RI)) {
  301. /* If MSR_RI isn't set, we cannot recover */
  302. recovered = 0;
  303. } else if (disposition == RTAS_DISP_FULLY_RECOVERED) {
  304. /* Platform corrected itself */
  305. recovered = 1;
  306. } else if (disposition == RTAS_DISP_LIMITED_RECOVERY) {
  307. /* Platform corrected itself but could be degraded */
  308. printk(KERN_ERR "MCE: limited recovery, system may "
  309. "be degraded\n");
  310. recovered = 1;
  311. } else if (user_mode(regs) && !is_global_init(current) &&
  312. rtas_error_severity(err) == RTAS_SEVERITY_ERROR_SYNC) {
  313. /*
  314. * If we received a synchronous error when in userspace
  315. * kill the task. Firmware may report details of the fail
  316. * asynchronously, so we can't rely on the target and type
  317. * fields being valid here.
  318. */
  319. printk(KERN_ERR "MCE: uncorrectable error, killing task "
  320. "%s:%d\n", current->comm, current->pid);
  321. _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
  322. recovered = 1;
  323. }
  324. log_error((char *)err, ERR_TYPE_RTAS_LOG, 0);
  325. return recovered;
  326. }
  327. /*
  328. * Handle a machine check.
  329. *
  330. * Note that on Power 4 and beyond Firmware Non-Maskable Interrupts (fwnmi)
  331. * should be present. If so the handler which called us tells us if the
  332. * error was recovered (never true if RI=0).
  333. *
  334. * On hardware prior to Power 4 these exceptions were asynchronous which
  335. * means we can't tell exactly where it occurred and so we can't recover.
  336. */
  337. int pSeries_machine_check_exception(struct pt_regs *regs)
  338. {
  339. struct rtas_error_log *errp;
  340. if (fwnmi_active) {
  341. errp = fwnmi_get_errinfo(regs);
  342. fwnmi_release_errinfo();
  343. if (errp && recover_mce(regs, errp))
  344. return 1;
  345. }
  346. return 0;
  347. }