opal-power.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * PowerNV OPAL power control for graceful shutdown handling
  3. *
  4. * Copyright 2015 IBM Corp.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #define pr_fmt(fmt) "opal-power: " fmt
  12. #include <linux/kernel.h>
  13. #include <linux/reboot.h>
  14. #include <linux/notifier.h>
  15. #include <linux/of.h>
  16. #include <asm/opal.h>
  17. #include <asm/machdep.h>
  18. #define SOFT_OFF 0x00
  19. #define SOFT_REBOOT 0x01
  20. /* Detect EPOW event */
  21. static bool detect_epow(void)
  22. {
  23. u16 epow;
  24. int i, rc;
  25. __be16 epow_classes;
  26. __be16 opal_epow_status[OPAL_SYSEPOW_MAX] = {0};
  27. /*
  28. * Check for EPOW event. Kernel sends supported EPOW classes info
  29. * to OPAL. OPAL returns EPOW info along with classes present.
  30. */
  31. epow_classes = cpu_to_be16(OPAL_SYSEPOW_MAX);
  32. rc = opal_get_epow_status(opal_epow_status, &epow_classes);
  33. if (rc != OPAL_SUCCESS) {
  34. pr_err("Failed to get EPOW event information\n");
  35. return false;
  36. }
  37. /* Look for EPOW events present */
  38. for (i = 0; i < be16_to_cpu(epow_classes); i++) {
  39. epow = be16_to_cpu(opal_epow_status[i]);
  40. /* Filter events which do not need shutdown. */
  41. if (i == OPAL_SYSEPOW_POWER)
  42. epow &= ~(OPAL_SYSPOWER_CHNG | OPAL_SYSPOWER_FAIL |
  43. OPAL_SYSPOWER_INCL);
  44. if (epow)
  45. return true;
  46. }
  47. return false;
  48. }
  49. /* Check for existing EPOW, DPO events */
  50. static bool poweroff_pending(void)
  51. {
  52. int rc;
  53. __be64 opal_dpo_timeout;
  54. /* Check for DPO event */
  55. rc = opal_get_dpo_status(&opal_dpo_timeout);
  56. if (rc == OPAL_SUCCESS) {
  57. pr_info("Existing DPO event detected.\n");
  58. return true;
  59. }
  60. /* Check for EPOW event */
  61. if (detect_epow()) {
  62. pr_info("Existing EPOW event detected.\n");
  63. return true;
  64. }
  65. return false;
  66. }
  67. /* OPAL power-control events notifier */
  68. static int opal_power_control_event(struct notifier_block *nb,
  69. unsigned long msg_type, void *msg)
  70. {
  71. uint64_t type;
  72. switch (msg_type) {
  73. case OPAL_MSG_EPOW:
  74. if (detect_epow()) {
  75. pr_info("EPOW msg received. Powering off system\n");
  76. orderly_poweroff(true);
  77. }
  78. break;
  79. case OPAL_MSG_DPO:
  80. pr_info("DPO msg received. Powering off system\n");
  81. orderly_poweroff(true);
  82. break;
  83. case OPAL_MSG_SHUTDOWN:
  84. type = be64_to_cpu(((struct opal_msg *)msg)->params[0]);
  85. switch (type) {
  86. case SOFT_REBOOT:
  87. pr_info("Reboot requested\n");
  88. orderly_reboot();
  89. break;
  90. case SOFT_OFF:
  91. pr_info("Poweroff requested\n");
  92. orderly_poweroff(true);
  93. break;
  94. default:
  95. pr_err("Unknown power-control type %llu\n", type);
  96. }
  97. break;
  98. default:
  99. pr_err("Unknown OPAL message type %lu\n", msg_type);
  100. }
  101. return 0;
  102. }
  103. /* OPAL EPOW event notifier block */
  104. static struct notifier_block opal_epow_nb = {
  105. .notifier_call = opal_power_control_event,
  106. .next = NULL,
  107. .priority = 0,
  108. };
  109. /* OPAL DPO event notifier block */
  110. static struct notifier_block opal_dpo_nb = {
  111. .notifier_call = opal_power_control_event,
  112. .next = NULL,
  113. .priority = 0,
  114. };
  115. /* OPAL power-control event notifier block */
  116. static struct notifier_block opal_power_control_nb = {
  117. .notifier_call = opal_power_control_event,
  118. .next = NULL,
  119. .priority = 0,
  120. };
  121. static int __init opal_power_control_init(void)
  122. {
  123. int ret, supported = 0;
  124. struct device_node *np;
  125. /* Register OPAL power-control events notifier */
  126. ret = opal_message_notifier_register(OPAL_MSG_SHUTDOWN,
  127. &opal_power_control_nb);
  128. if (ret)
  129. pr_err("Failed to register SHUTDOWN notifier, ret = %d\n", ret);
  130. /* Determine OPAL EPOW, DPO support */
  131. np = of_find_node_by_path("/ibm,opal/epow");
  132. if (np) {
  133. supported = of_device_is_compatible(np, "ibm,opal-v3-epow");
  134. of_node_put(np);
  135. }
  136. if (!supported)
  137. return 0;
  138. pr_info("OPAL EPOW, DPO support detected.\n");
  139. /* Register EPOW event notifier */
  140. ret = opal_message_notifier_register(OPAL_MSG_EPOW, &opal_epow_nb);
  141. if (ret)
  142. pr_err("Failed to register EPOW notifier, ret = %d\n", ret);
  143. /* Register DPO event notifier */
  144. ret = opal_message_notifier_register(OPAL_MSG_DPO, &opal_dpo_nb);
  145. if (ret)
  146. pr_err("Failed to register DPO notifier, ret = %d\n", ret);
  147. /* Check for any pending EPOW or DPO events. */
  148. if (poweroff_pending())
  149. orderly_poweroff(true);
  150. return 0;
  151. }
  152. machine_subsys_initcall(powernv, opal_power_control_init);