spapr_watchdog.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * This library is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU Lesser General Public
  4. * License as published by the Free Software Foundation; either
  5. * version 2.1 of the License, or (at your option) any later version.
  6. *
  7. * This library is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. * Lesser General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU Lesser General Public
  13. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "qemu/osdep.h"
  16. #include "qapi/error.h"
  17. #include "target/ppc/cpu.h"
  18. #include "migration/vmstate.h"
  19. #include "trace.h"
  20. #include "hw/ppc/spapr.h"
  21. #define FIELD_BE(reg, field, start, len) \
  22. FIELD(reg, field, 64 - (start + len), len)
  23. /*
  24. * Bits 47: "leaveOtherWatchdogsRunningOnTimeout", specified on
  25. * the "Start watchdog" operation,
  26. * 0 - stop out-standing watchdogs on timeout,
  27. * 1 - leave outstanding watchdogs running on timeout
  28. */
  29. FIELD_BE(PSERIES_WDTF, LEAVE_OTHER, 47, 1)
  30. /* Bits 48-55: "operation" */
  31. FIELD_BE(PSERIES_WDTF, OP, 48, 8)
  32. #define PSERIES_WDTF_OP_START 0x1
  33. #define PSERIES_WDTF_OP_STOP 0x2
  34. #define PSERIES_WDTF_OP_QUERY 0x3
  35. #define PSERIES_WDTF_OP_QUERY_LPM 0x4
  36. /* Bits 56-63: "timeoutAction" */
  37. FIELD_BE(PSERIES_WDTF, ACTION, 56, 8)
  38. #define PSERIES_WDTF_ACTION_HARD_POWER_OFF 0x1
  39. #define PSERIES_WDTF_ACTION_HARD_RESTART 0x2
  40. #define PSERIES_WDTF_ACTION_DUMP_RESTART 0x3
  41. FIELD_BE(PSERIES_WDTF, RESERVED, 0, 47)
  42. /* Special watchdogNumber for the "stop all watchdogs" operation */
  43. #define PSERIES_WDT_STOP_ALL ((uint64_t)~0)
  44. /*
  45. * For the "Query watchdog capabilities" operation, a uint64 structure
  46. * defined as:
  47. * Bits 0-15: The minimum supported timeout in milliseconds
  48. * Bits 16-31: The number of watchdogs supported
  49. * Bits 32-63: Reserved
  50. */
  51. FIELD_BE(PSERIES_WDTQ, MIN_TIMEOUT, 0, 16)
  52. FIELD_BE(PSERIES_WDTQ, NUM, 16, 16)
  53. /*
  54. * For the "Query watchdog LPM requirement" operation:
  55. * 1 = The given "watchdogNumber" must be stopped prior to suspending
  56. * 2 = The given "watchdogNumber" does not have to be stopped prior to
  57. * suspending
  58. */
  59. #define PSERIES_WDTQL_STOPPED 1
  60. #define PSERIES_WDTQL_QUERY_NOT_STOPPED 2
  61. #define WDT_MIN_TIMEOUT 1 /* 1ms */
  62. static target_ulong watchdog_stop(unsigned watchdogNumber, SpaprWatchdog *w)
  63. {
  64. target_ulong ret = H_NOOP;
  65. if (timer_pending(&w->timer)) {
  66. timer_del(&w->timer);
  67. ret = H_SUCCESS;
  68. }
  69. trace_spapr_watchdog_stop(watchdogNumber, ret);
  70. return ret;
  71. }
  72. static target_ulong watchdog_stop_all(SpaprMachineState *spapr)
  73. {
  74. target_ulong ret = H_NOOP;
  75. int i;
  76. for (i = 1; i <= ARRAY_SIZE(spapr->wds); ++i) {
  77. target_ulong r = watchdog_stop(i, &spapr->wds[i - 1]);
  78. if (r != H_NOOP && r != H_SUCCESS) {
  79. ret = r;
  80. }
  81. }
  82. return ret;
  83. }
  84. static void watchdog_expired(void *pw)
  85. {
  86. SpaprWatchdog *w = pw;
  87. CPUState *cs;
  88. SpaprMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
  89. unsigned num = w - spapr->wds;
  90. g_assert(num < ARRAY_SIZE(spapr->wds));
  91. trace_spapr_watchdog_expired(num, w->action);
  92. switch (w->action) {
  93. case PSERIES_WDTF_ACTION_HARD_POWER_OFF:
  94. qemu_system_vmstop_request(RUN_STATE_SHUTDOWN);
  95. break;
  96. case PSERIES_WDTF_ACTION_HARD_RESTART:
  97. qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
  98. break;
  99. case PSERIES_WDTF_ACTION_DUMP_RESTART:
  100. CPU_FOREACH(cs) {
  101. async_run_on_cpu(cs, spapr_do_system_reset_on_cpu, RUN_ON_CPU_NULL);
  102. }
  103. break;
  104. }
  105. if (!w->leave_others) {
  106. watchdog_stop_all(spapr);
  107. }
  108. }
  109. static target_ulong h_watchdog(PowerPCCPU *cpu,
  110. SpaprMachineState *spapr,
  111. target_ulong opcode, target_ulong *args)
  112. {
  113. target_ulong ret = H_SUCCESS;
  114. target_ulong flags = args[0];
  115. target_ulong watchdogNumber = args[1]; /* 1-Based per PAPR */
  116. target_ulong timeoutInMs = args[2];
  117. unsigned operation = FIELD_EX64(flags, PSERIES_WDTF, OP);
  118. unsigned timeoutAction = FIELD_EX64(flags, PSERIES_WDTF, ACTION);
  119. SpaprWatchdog *w;
  120. if (FIELD_EX64(flags, PSERIES_WDTF, RESERVED)) {
  121. return H_PARAMETER;
  122. }
  123. switch (operation) {
  124. case PSERIES_WDTF_OP_START:
  125. if (watchdogNumber > ARRAY_SIZE(spapr->wds)) {
  126. return H_P2;
  127. }
  128. if (timeoutInMs <= WDT_MIN_TIMEOUT) {
  129. return H_P3;
  130. }
  131. w = &spapr->wds[watchdogNumber - 1];
  132. switch (timeoutAction) {
  133. case PSERIES_WDTF_ACTION_HARD_POWER_OFF:
  134. case PSERIES_WDTF_ACTION_HARD_RESTART:
  135. case PSERIES_WDTF_ACTION_DUMP_RESTART:
  136. w->action = timeoutAction;
  137. break;
  138. default:
  139. return H_PARAMETER;
  140. }
  141. w->leave_others = FIELD_EX64(flags, PSERIES_WDTF, LEAVE_OTHER);
  142. timer_mod(&w->timer,
  143. qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + timeoutInMs);
  144. trace_spapr_watchdog_start(flags, watchdogNumber, timeoutInMs);
  145. break;
  146. case PSERIES_WDTF_OP_STOP:
  147. if (watchdogNumber == PSERIES_WDT_STOP_ALL) {
  148. ret = watchdog_stop_all(spapr);
  149. } else if (watchdogNumber <= ARRAY_SIZE(spapr->wds)) {
  150. ret = watchdog_stop(watchdogNumber,
  151. &spapr->wds[watchdogNumber - 1]);
  152. } else {
  153. return H_P2;
  154. }
  155. break;
  156. case PSERIES_WDTF_OP_QUERY:
  157. args[0] = FIELD_DP64(0, PSERIES_WDTQ, MIN_TIMEOUT, WDT_MIN_TIMEOUT);
  158. args[0] = FIELD_DP64(args[0], PSERIES_WDTQ, NUM,
  159. ARRAY_SIZE(spapr->wds));
  160. trace_spapr_watchdog_query(args[0]);
  161. break;
  162. case PSERIES_WDTF_OP_QUERY_LPM:
  163. if (watchdogNumber > ARRAY_SIZE(spapr->wds)) {
  164. return H_P2;
  165. }
  166. args[0] = PSERIES_WDTQL_QUERY_NOT_STOPPED;
  167. trace_spapr_watchdog_query_lpm(args[0]);
  168. break;
  169. default:
  170. return H_PARAMETER;
  171. }
  172. return ret;
  173. }
  174. void spapr_watchdog_init(SpaprMachineState *spapr)
  175. {
  176. int i;
  177. for (i = 0; i < ARRAY_SIZE(spapr->wds); ++i) {
  178. char name[16];
  179. SpaprWatchdog *w = &spapr->wds[i];
  180. snprintf(name, sizeof(name) - 1, "wdt%d", i + 1);
  181. object_initialize_child_with_props(OBJECT(spapr), name, w,
  182. sizeof(SpaprWatchdog),
  183. TYPE_SPAPR_WDT,
  184. &error_fatal, NULL);
  185. qdev_realize(DEVICE(w), NULL, &error_fatal);
  186. }
  187. }
  188. static bool watchdog_needed(void *opaque)
  189. {
  190. SpaprWatchdog *w = opaque;
  191. return timer_pending(&w->timer);
  192. }
  193. static const VMStateDescription vmstate_wdt = {
  194. .name = "spapr_watchdog",
  195. .version_id = 1,
  196. .minimum_version_id = 1,
  197. .needed = watchdog_needed,
  198. .fields = (const VMStateField[]) {
  199. VMSTATE_TIMER(timer, SpaprWatchdog),
  200. VMSTATE_UINT8(action, SpaprWatchdog),
  201. VMSTATE_UINT8(leave_others, SpaprWatchdog),
  202. VMSTATE_END_OF_LIST()
  203. }
  204. };
  205. static void spapr_wdt_realize(DeviceState *dev, Error **errp)
  206. {
  207. SpaprWatchdog *w = SPAPR_WDT(dev);
  208. Object *o = OBJECT(dev);
  209. timer_init_ms(&w->timer, QEMU_CLOCK_VIRTUAL, watchdog_expired, w);
  210. object_property_add_uint64_ptr(o, "expire",
  211. (uint64_t *)&w->timer.expire_time,
  212. OBJ_PROP_FLAG_READ);
  213. object_property_add_uint8_ptr(o, "action", &w->action, OBJ_PROP_FLAG_READ);
  214. object_property_add_uint8_ptr(o, "leaveOtherWatchdogsRunningOnTimeout",
  215. &w->leave_others, OBJ_PROP_FLAG_READ);
  216. }
  217. static void spapr_wdt_class_init(ObjectClass *oc, void *data)
  218. {
  219. DeviceClass *dc = DEVICE_CLASS(oc);
  220. dc->realize = spapr_wdt_realize;
  221. dc->vmsd = &vmstate_wdt;
  222. dc->user_creatable = false;
  223. }
  224. static const TypeInfo spapr_wdt_info = {
  225. .name = TYPE_SPAPR_WDT,
  226. .parent = TYPE_DEVICE,
  227. .instance_size = sizeof(SpaprWatchdog),
  228. .class_init = spapr_wdt_class_init,
  229. };
  230. static void spapr_watchdog_register_types(void)
  231. {
  232. spapr_register_hypercall(H_WATCHDOG, h_watchdog);
  233. type_register_static(&spapr_wdt_info);
  234. }
  235. type_init(spapr_watchdog_register_types)