PageRenderTime 57ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/arch/powerpc/platforms/pseries/ras.c

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