ras.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. /* EPOW events counter variable */
  36. static int num_epow_events;
  37. static irqreturn_t ras_hotplug_interrupt(int irq, void *dev_id);
  38. static irqreturn_t ras_epow_interrupt(int irq, void *dev_id);
  39. static irqreturn_t ras_error_interrupt(int irq, void *dev_id);
  40. /*
  41. * Enable the hotplug interrupt late because processing them may touch other
  42. * devices or systems (e.g. hugepages) that have not been initialized at the
  43. * subsys stage.
  44. */
  45. int __init init_ras_hotplug_IRQ(void)
  46. {
  47. struct device_node *np;
  48. /* Hotplug Events */
  49. np = of_find_node_by_path("/event-sources/hot-plug-events");
  50. if (np != NULL) {
  51. if (dlpar_workqueue_init() == 0)
  52. request_event_sources_irqs(np, ras_hotplug_interrupt,
  53. "RAS_HOTPLUG");
  54. of_node_put(np);
  55. }
  56. return 0;
  57. }
  58. machine_late_initcall(pseries, init_ras_hotplug_IRQ);
  59. /*
  60. * Initialize handlers for the set of interrupts caused by hardware errors
  61. * and power system events.
  62. */
  63. static int __init init_ras_IRQ(void)
  64. {
  65. struct device_node *np;
  66. ras_check_exception_token = rtas_token("check-exception");
  67. /* Internal Errors */
  68. np = of_find_node_by_path("/event-sources/internal-errors");
  69. if (np != NULL) {
  70. request_event_sources_irqs(np, ras_error_interrupt,
  71. "RAS_ERROR");
  72. of_node_put(np);
  73. }
  74. /* EPOW Events */
  75. np = of_find_node_by_path("/event-sources/epow-events");
  76. if (np != NULL) {
  77. request_event_sources_irqs(np, ras_epow_interrupt, "RAS_EPOW");
  78. of_node_put(np);
  79. }
  80. return 0;
  81. }
  82. machine_subsys_initcall(pseries, init_ras_IRQ);
  83. #define EPOW_SHUTDOWN_NORMAL 1
  84. #define EPOW_SHUTDOWN_ON_UPS 2
  85. #define EPOW_SHUTDOWN_LOSS_OF_CRITICAL_FUNCTIONS 3
  86. #define EPOW_SHUTDOWN_AMBIENT_TEMPERATURE_TOO_HIGH 4
  87. static void handle_system_shutdown(char event_modifier)
  88. {
  89. switch (event_modifier) {
  90. case EPOW_SHUTDOWN_NORMAL:
  91. pr_emerg("Power off requested\n");
  92. orderly_poweroff(true);
  93. break;
  94. case EPOW_SHUTDOWN_ON_UPS:
  95. pr_emerg("Loss of system power detected. System is running on"
  96. " UPS/battery. Check RTAS error log for details\n");
  97. orderly_poweroff(true);
  98. break;
  99. case EPOW_SHUTDOWN_LOSS_OF_CRITICAL_FUNCTIONS:
  100. pr_emerg("Loss of system critical functions detected. Check"
  101. " RTAS error log for details\n");
  102. orderly_poweroff(true);
  103. break;
  104. case EPOW_SHUTDOWN_AMBIENT_TEMPERATURE_TOO_HIGH:
  105. pr_emerg("High ambient temperature detected. Check RTAS"
  106. " error log for details\n");
  107. orderly_poweroff(true);
  108. break;
  109. default:
  110. pr_err("Unknown power/cooling shutdown event (modifier = %d)\n",
  111. event_modifier);
  112. }
  113. }
  114. struct epow_errorlog {
  115. unsigned char sensor_value;
  116. unsigned char event_modifier;
  117. unsigned char extended_modifier;
  118. unsigned char reserved;
  119. unsigned char platform_reason;
  120. };
  121. #define EPOW_RESET 0
  122. #define EPOW_WARN_COOLING 1
  123. #define EPOW_WARN_POWER 2
  124. #define EPOW_SYSTEM_SHUTDOWN 3
  125. #define EPOW_SYSTEM_HALT 4
  126. #define EPOW_MAIN_ENCLOSURE 5
  127. #define EPOW_POWER_OFF 7
  128. static void rtas_parse_epow_errlog(struct rtas_error_log *log)
  129. {
  130. struct pseries_errorlog *pseries_log;
  131. struct epow_errorlog *epow_log;
  132. char action_code;
  133. char modifier;
  134. pseries_log = get_pseries_errorlog(log, PSERIES_ELOG_SECT_ID_EPOW);
  135. if (pseries_log == NULL)
  136. return;
  137. epow_log = (struct epow_errorlog *)pseries_log->data;
  138. action_code = epow_log->sensor_value & 0xF; /* bottom 4 bits */
  139. modifier = epow_log->event_modifier & 0xF; /* bottom 4 bits */
  140. switch (action_code) {
  141. case EPOW_RESET:
  142. if (num_epow_events) {
  143. pr_info("Non critical power/cooling issue cleared\n");
  144. num_epow_events--;
  145. }
  146. break;
  147. case EPOW_WARN_COOLING:
  148. pr_info("Non-critical cooling issue detected. Check RTAS error"
  149. " log for details\n");
  150. break;
  151. case EPOW_WARN_POWER:
  152. pr_info("Non-critical power issue detected. Check RTAS error"
  153. " log for details\n");
  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("Critical power/cooling issue detected. Check RTAS"
  160. " error log for details. Powering off.\n");
  161. orderly_poweroff(true);
  162. break;
  163. case EPOW_MAIN_ENCLOSURE:
  164. case EPOW_POWER_OFF:
  165. pr_emerg("System about to lose power. Check RTAS error log "
  166. " for details. Powering off immediately.\n");
  167. emergency_sync();
  168. kernel_power_off();
  169. break;
  170. default:
  171. pr_err("Unknown power/cooling event (action code = %d)\n",
  172. action_code);
  173. }
  174. /* Increment epow events counter variable */
  175. if (action_code != EPOW_RESET)
  176. num_epow_events++;
  177. }
  178. static irqreturn_t ras_hotplug_interrupt(int irq, void *dev_id)
  179. {
  180. struct pseries_errorlog *pseries_log;
  181. struct pseries_hp_errorlog *hp_elog;
  182. spin_lock(&ras_log_buf_lock);
  183. rtas_call(ras_check_exception_token, 6, 1, NULL,
  184. RTAS_VECTOR_EXTERNAL_INTERRUPT, virq_to_hw(irq),
  185. RTAS_HOTPLUG_EVENTS, 0, __pa(&ras_log_buf),
  186. rtas_get_error_log_max());
  187. pseries_log = get_pseries_errorlog((struct rtas_error_log *)ras_log_buf,
  188. PSERIES_ELOG_SECT_ID_HOTPLUG);
  189. hp_elog = (struct pseries_hp_errorlog *)pseries_log->data;
  190. /*
  191. * Since PCI hotplug is not currently supported on pseries, put PCI
  192. * hotplug events on the ras_log_buf to be handled by rtas_errd.
  193. */
  194. if (hp_elog->resource == PSERIES_HP_ELOG_RESOURCE_MEM ||
  195. hp_elog->resource == PSERIES_HP_ELOG_RESOURCE_CPU)
  196. queue_hotplug_event(hp_elog, NULL, NULL);
  197. else
  198. log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, 0);
  199. spin_unlock(&ras_log_buf_lock);
  200. return IRQ_HANDLED;
  201. }
  202. /* Handle environmental and power warning (EPOW) interrupts. */
  203. static irqreturn_t ras_epow_interrupt(int irq, void *dev_id)
  204. {
  205. int status;
  206. int state;
  207. int critical;
  208. status = rtas_get_sensor_fast(EPOW_SENSOR_TOKEN, EPOW_SENSOR_INDEX,
  209. &state);
  210. if (state > 3)
  211. critical = 1; /* Time Critical */
  212. else
  213. critical = 0;
  214. spin_lock(&ras_log_buf_lock);
  215. status = rtas_call(ras_check_exception_token, 6, 1, NULL,
  216. RTAS_VECTOR_EXTERNAL_INTERRUPT,
  217. virq_to_hw(irq),
  218. RTAS_EPOW_WARNING,
  219. critical, __pa(&ras_log_buf),
  220. rtas_get_error_log_max());
  221. log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, 0);
  222. rtas_parse_epow_errlog((struct rtas_error_log *)ras_log_buf);
  223. spin_unlock(&ras_log_buf_lock);
  224. return IRQ_HANDLED;
  225. }
  226. /*
  227. * Handle hardware error interrupts.
  228. *
  229. * RTAS check-exception is called to collect data on the exception. If
  230. * the error is deemed recoverable, we log a warning and return.
  231. * For nonrecoverable errors, an error is logged and we stop all processing
  232. * as quickly as possible in order to prevent propagation of the failure.
  233. */
  234. static irqreturn_t ras_error_interrupt(int irq, void *dev_id)
  235. {
  236. struct rtas_error_log *rtas_elog;
  237. int status;
  238. int fatal;
  239. spin_lock(&ras_log_buf_lock);
  240. status = rtas_call(ras_check_exception_token, 6, 1, NULL,
  241. RTAS_VECTOR_EXTERNAL_INTERRUPT,
  242. virq_to_hw(irq),
  243. RTAS_INTERNAL_ERROR, 1 /* Time Critical */,
  244. __pa(&ras_log_buf),
  245. rtas_get_error_log_max());
  246. rtas_elog = (struct rtas_error_log *)ras_log_buf;
  247. if (status == 0 &&
  248. rtas_error_severity(rtas_elog) >= RTAS_SEVERITY_ERROR_SYNC)
  249. fatal = 1;
  250. else
  251. fatal = 0;
  252. /* format and print the extended information */
  253. log_error(ras_log_buf, ERR_TYPE_RTAS_LOG, fatal);
  254. if (fatal) {
  255. pr_emerg("Fatal hardware error detected. Check RTAS error"
  256. " log for details. Powering off immediately\n");
  257. emergency_sync();
  258. kernel_power_off();
  259. } else {
  260. pr_err("Recoverable hardware error detected\n");
  261. }
  262. spin_unlock(&ras_log_buf_lock);
  263. return IRQ_HANDLED;
  264. }
  265. /*
  266. * Some versions of FWNMI place the buffer inside the 4kB page starting at
  267. * 0x7000. Other versions place it inside the rtas buffer. We check both.
  268. */
  269. #define VALID_FWNMI_BUFFER(A) \
  270. ((((A) >= 0x7000) && ((A) < 0x7ff0)) || \
  271. (((A) >= rtas.base) && ((A) < (rtas.base + rtas.size - 16))))
  272. /*
  273. * Get the error information for errors coming through the
  274. * FWNMI vectors. The pt_regs' r3 will be updated to reflect
  275. * the actual r3 if possible, and a ptr to the error log entry
  276. * will be returned if found.
  277. *
  278. * If the RTAS error is not of the extended type, then we put it in a per
  279. * cpu 64bit buffer. If it is the extended type we use global_mce_data_buf.
  280. *
  281. * The global_mce_data_buf does not have any locks or protection around it,
  282. * if a second machine check comes in, or a system reset is done
  283. * before we have logged the error, then we will get corruption in the
  284. * error log. This is preferable over holding off on calling
  285. * ibm,nmi-interlock which would result in us checkstopping if a
  286. * second machine check did come in.
  287. */
  288. static struct rtas_error_log *fwnmi_get_errinfo(struct pt_regs *regs)
  289. {
  290. unsigned long *savep;
  291. struct rtas_error_log *h, *errhdr = NULL;
  292. /* Mask top two bits */
  293. regs->gpr[3] &= ~(0x3UL << 62);
  294. if (!VALID_FWNMI_BUFFER(regs->gpr[3])) {
  295. printk(KERN_ERR "FWNMI: corrupt r3 0x%016lx\n", regs->gpr[3]);
  296. return NULL;
  297. }
  298. savep = __va(regs->gpr[3]);
  299. regs->gpr[3] = savep[0]; /* restore original r3 */
  300. /* If it isn't an extended log we can use the per cpu 64bit buffer */
  301. h = (struct rtas_error_log *)&savep[1];
  302. if (!rtas_error_extended(h)) {
  303. memcpy(this_cpu_ptr(&mce_data_buf), h, sizeof(__u64));
  304. errhdr = (struct rtas_error_log *)this_cpu_ptr(&mce_data_buf);
  305. } else {
  306. int len, error_log_length;
  307. error_log_length = 8 + rtas_error_extended_log_length(h);
  308. len = max_t(int, error_log_length, RTAS_ERROR_LOG_MAX);
  309. memset(global_mce_data_buf, 0, RTAS_ERROR_LOG_MAX);
  310. memcpy(global_mce_data_buf, h, len);
  311. errhdr = (struct rtas_error_log *)global_mce_data_buf;
  312. }
  313. return errhdr;
  314. }
  315. /* Call this when done with the data returned by FWNMI_get_errinfo.
  316. * It will release the saved data area for other CPUs in the
  317. * partition to receive FWNMI errors.
  318. */
  319. static void fwnmi_release_errinfo(void)
  320. {
  321. int ret = rtas_call(rtas_token("ibm,nmi-interlock"), 0, 1, NULL);
  322. if (ret != 0)
  323. printk(KERN_ERR "FWNMI: nmi-interlock failed: %d\n", ret);
  324. }
  325. int pSeries_system_reset_exception(struct pt_regs *regs)
  326. {
  327. #ifdef __LITTLE_ENDIAN__
  328. /*
  329. * Some firmware byteswaps SRR registers and gives incorrect SRR1. Try
  330. * to detect the bad SRR1 pattern here. Flip the NIP back to correct
  331. * endian for reporting purposes. Unfortunately the MSR can't be fixed,
  332. * so clear it. It will be missing MSR_RI so we won't try to recover.
  333. */
  334. if ((be64_to_cpu(regs->msr) &
  335. (MSR_LE|MSR_RI|MSR_DR|MSR_IR|MSR_ME|MSR_PR|
  336. MSR_ILE|MSR_HV|MSR_SF)) == (MSR_DR|MSR_SF)) {
  337. regs->nip = be64_to_cpu((__be64)regs->nip);
  338. regs->msr = 0;
  339. }
  340. #endif
  341. if (fwnmi_active) {
  342. struct rtas_error_log *errhdr = fwnmi_get_errinfo(regs);
  343. if (errhdr) {
  344. /* XXX Should look at FWNMI information */
  345. }
  346. fwnmi_release_errinfo();
  347. }
  348. if (smp_handle_nmi_ipi(regs))
  349. return 1;
  350. return 0; /* need to perform reset */
  351. }
  352. /*
  353. * See if we can recover from a machine check exception.
  354. * This is only called on power4 (or above) and only via
  355. * the Firmware Non-Maskable Interrupts (fwnmi) handler
  356. * which provides the error analysis for us.
  357. *
  358. * Return 1 if corrected (or delivered a signal).
  359. * Return 0 if there is nothing we can do.
  360. */
  361. static int recover_mce(struct pt_regs *regs, struct rtas_error_log *err)
  362. {
  363. int recovered = 0;
  364. int disposition = rtas_error_disposition(err);
  365. if (!(regs->msr & MSR_RI)) {
  366. /* If MSR_RI isn't set, we cannot recover */
  367. recovered = 0;
  368. } else if (disposition == RTAS_DISP_FULLY_RECOVERED) {
  369. /* Platform corrected itself */
  370. recovered = 1;
  371. } else if (disposition == RTAS_DISP_LIMITED_RECOVERY) {
  372. /* Platform corrected itself but could be degraded */
  373. printk(KERN_ERR "MCE: limited recovery, system may "
  374. "be degraded\n");
  375. recovered = 1;
  376. } else if (user_mode(regs) && !is_global_init(current) &&
  377. rtas_error_severity(err) == RTAS_SEVERITY_ERROR_SYNC) {
  378. /*
  379. * If we received a synchronous error when in userspace
  380. * kill the task. Firmware may report details of the fail
  381. * asynchronously, so we can't rely on the target and type
  382. * fields being valid here.
  383. */
  384. printk(KERN_ERR "MCE: uncorrectable error, killing task "
  385. "%s:%d\n", current->comm, current->pid);
  386. _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
  387. recovered = 1;
  388. }
  389. log_error((char *)err, ERR_TYPE_RTAS_LOG, 0);
  390. return recovered;
  391. }
  392. /*
  393. * Handle a machine check.
  394. *
  395. * Note that on Power 4 and beyond Firmware Non-Maskable Interrupts (fwnmi)
  396. * should be present. If so the handler which called us tells us if the
  397. * error was recovered (never true if RI=0).
  398. *
  399. * On hardware prior to Power 4 these exceptions were asynchronous which
  400. * means we can't tell exactly where it occurred and so we can't recover.
  401. */
  402. int pSeries_machine_check_exception(struct pt_regs *regs)
  403. {
  404. struct rtas_error_log *errp;
  405. if (fwnmi_active) {
  406. errp = fwnmi_get_errinfo(regs);
  407. fwnmi_release_errinfo();
  408. if (errp && recover_mce(regs, errp))
  409. return 1;
  410. }
  411. return 0;
  412. }