system.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * System Control and Management Interface (SCMI) System Power Protocol
  4. *
  5. * Copyright (C) 2020-2022 ARM Ltd.
  6. */
  7. #define pr_fmt(fmt) "SCMI Notifications SYSTEM - " fmt
  8. #include <linux/module.h>
  9. #include <linux/scmi_protocol.h>
  10. #include "protocols.h"
  11. #include "notify.h"
  12. /* Updated only after ALL the mandatory features for that version are merged */
  13. #define SCMI_PROTOCOL_SUPPORTED_VERSION 0x20001
  14. #define SCMI_SYSTEM_NUM_SOURCES 1
  15. enum scmi_system_protocol_cmd {
  16. SYSTEM_POWER_STATE_NOTIFY = 0x5,
  17. };
  18. struct scmi_system_power_state_notify {
  19. __le32 notify_enable;
  20. };
  21. struct scmi_system_power_state_notifier_payld {
  22. __le32 agent_id;
  23. __le32 flags;
  24. __le32 system_state;
  25. __le32 timeout;
  26. };
  27. struct scmi_system_info {
  28. bool graceful_timeout_supported;
  29. bool power_state_notify_cmd;
  30. };
  31. static bool scmi_system_notify_supported(const struct scmi_protocol_handle *ph,
  32. u8 evt_id, u32 src_id)
  33. {
  34. struct scmi_system_info *pinfo = ph->get_priv(ph);
  35. if (evt_id != SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER)
  36. return false;
  37. return pinfo->power_state_notify_cmd;
  38. }
  39. static int scmi_system_request_notify(const struct scmi_protocol_handle *ph,
  40. bool enable)
  41. {
  42. int ret;
  43. struct scmi_xfer *t;
  44. struct scmi_system_power_state_notify *notify;
  45. ret = ph->xops->xfer_get_init(ph, SYSTEM_POWER_STATE_NOTIFY,
  46. sizeof(*notify), 0, &t);
  47. if (ret)
  48. return ret;
  49. notify = t->tx.buf;
  50. notify->notify_enable = enable ? cpu_to_le32(BIT(0)) : 0;
  51. ret = ph->xops->do_xfer(ph, t);
  52. ph->xops->xfer_put(ph, t);
  53. return ret;
  54. }
  55. static int scmi_system_set_notify_enabled(const struct scmi_protocol_handle *ph,
  56. u8 evt_id, u32 src_id, bool enable)
  57. {
  58. int ret;
  59. ret = scmi_system_request_notify(ph, enable);
  60. if (ret)
  61. pr_debug("FAIL_ENABLE - evt[%X] - ret:%d\n", evt_id, ret);
  62. return ret;
  63. }
  64. static void *
  65. scmi_system_fill_custom_report(const struct scmi_protocol_handle *ph,
  66. u8 evt_id, ktime_t timestamp,
  67. const void *payld, size_t payld_sz,
  68. void *report, u32 *src_id)
  69. {
  70. size_t expected_sz;
  71. const struct scmi_system_power_state_notifier_payld *p = payld;
  72. struct scmi_system_power_state_notifier_report *r = report;
  73. struct scmi_system_info *pinfo = ph->get_priv(ph);
  74. expected_sz = pinfo->graceful_timeout_supported ?
  75. sizeof(*p) : sizeof(*p) - sizeof(__le32);
  76. if (evt_id != SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER ||
  77. payld_sz != expected_sz)
  78. return NULL;
  79. r->timestamp = timestamp;
  80. r->agent_id = le32_to_cpu(p->agent_id);
  81. r->flags = le32_to_cpu(p->flags);
  82. r->system_state = le32_to_cpu(p->system_state);
  83. if (pinfo->graceful_timeout_supported &&
  84. r->system_state == SCMI_SYSTEM_SHUTDOWN &&
  85. SCMI_SYSPOWER_IS_REQUEST_GRACEFUL(r->flags))
  86. r->timeout = le32_to_cpu(p->timeout);
  87. else
  88. r->timeout = 0x00;
  89. *src_id = 0;
  90. return r;
  91. }
  92. static const struct scmi_event system_events[] = {
  93. {
  94. .id = SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER,
  95. .max_payld_sz =
  96. sizeof(struct scmi_system_power_state_notifier_payld),
  97. .max_report_sz =
  98. sizeof(struct scmi_system_power_state_notifier_report),
  99. },
  100. };
  101. static const struct scmi_event_ops system_event_ops = {
  102. .is_notify_supported = scmi_system_notify_supported,
  103. .set_notify_enabled = scmi_system_set_notify_enabled,
  104. .fill_custom_report = scmi_system_fill_custom_report,
  105. };
  106. static const struct scmi_protocol_events system_protocol_events = {
  107. .queue_sz = SCMI_PROTO_QUEUE_SZ,
  108. .ops = &system_event_ops,
  109. .evts = system_events,
  110. .num_events = ARRAY_SIZE(system_events),
  111. .num_sources = SCMI_SYSTEM_NUM_SOURCES,
  112. };
  113. static int scmi_system_protocol_init(const struct scmi_protocol_handle *ph)
  114. {
  115. struct scmi_system_info *pinfo;
  116. dev_dbg(ph->dev, "System Power Version %d.%d\n",
  117. PROTOCOL_REV_MAJOR(ph->version), PROTOCOL_REV_MINOR(ph->version));
  118. pinfo = devm_kzalloc(ph->dev, sizeof(*pinfo), GFP_KERNEL);
  119. if (!pinfo)
  120. return -ENOMEM;
  121. if (PROTOCOL_REV_MAJOR(ph->version) >= 0x2)
  122. pinfo->graceful_timeout_supported = true;
  123. if (!ph->hops->protocol_msg_check(ph, SYSTEM_POWER_STATE_NOTIFY, NULL))
  124. pinfo->power_state_notify_cmd = true;
  125. return ph->set_priv(ph, pinfo);
  126. }
  127. static const struct scmi_protocol scmi_system = {
  128. .id = SCMI_PROTOCOL_SYSTEM,
  129. .owner = THIS_MODULE,
  130. .instance_init = &scmi_system_protocol_init,
  131. .ops = NULL,
  132. .events = &system_protocol_events,
  133. .supported_version = SCMI_PROTOCOL_SUPPORTED_VERSION,
  134. };
  135. DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(system, scmi_system)