system.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. u32 version;
  29. bool graceful_timeout_supported;
  30. bool power_state_notify_cmd;
  31. };
  32. static bool scmi_system_notify_supported(const struct scmi_protocol_handle *ph,
  33. u8 evt_id, u32 src_id)
  34. {
  35. struct scmi_system_info *pinfo = ph->get_priv(ph);
  36. if (evt_id != SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER)
  37. return false;
  38. return pinfo->power_state_notify_cmd;
  39. }
  40. static int scmi_system_request_notify(const struct scmi_protocol_handle *ph,
  41. bool enable)
  42. {
  43. int ret;
  44. struct scmi_xfer *t;
  45. struct scmi_system_power_state_notify *notify;
  46. ret = ph->xops->xfer_get_init(ph, SYSTEM_POWER_STATE_NOTIFY,
  47. sizeof(*notify), 0, &t);
  48. if (ret)
  49. return ret;
  50. notify = t->tx.buf;
  51. notify->notify_enable = enable ? cpu_to_le32(BIT(0)) : 0;
  52. ret = ph->xops->do_xfer(ph, t);
  53. ph->xops->xfer_put(ph, t);
  54. return ret;
  55. }
  56. static int scmi_system_set_notify_enabled(const struct scmi_protocol_handle *ph,
  57. u8 evt_id, u32 src_id, bool enable)
  58. {
  59. int ret;
  60. ret = scmi_system_request_notify(ph, enable);
  61. if (ret)
  62. pr_debug("FAIL_ENABLE - evt[%X] - ret:%d\n", evt_id, ret);
  63. return ret;
  64. }
  65. static void *
  66. scmi_system_fill_custom_report(const struct scmi_protocol_handle *ph,
  67. u8 evt_id, ktime_t timestamp,
  68. const void *payld, size_t payld_sz,
  69. void *report, u32 *src_id)
  70. {
  71. size_t expected_sz;
  72. const struct scmi_system_power_state_notifier_payld *p = payld;
  73. struct scmi_system_power_state_notifier_report *r = report;
  74. struct scmi_system_info *pinfo = ph->get_priv(ph);
  75. expected_sz = pinfo->graceful_timeout_supported ?
  76. sizeof(*p) : sizeof(*p) - sizeof(__le32);
  77. if (evt_id != SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER ||
  78. payld_sz != expected_sz)
  79. return NULL;
  80. r->timestamp = timestamp;
  81. r->agent_id = le32_to_cpu(p->agent_id);
  82. r->flags = le32_to_cpu(p->flags);
  83. r->system_state = le32_to_cpu(p->system_state);
  84. if (pinfo->graceful_timeout_supported &&
  85. r->system_state == SCMI_SYSTEM_SHUTDOWN &&
  86. SCMI_SYSPOWER_IS_REQUEST_GRACEFUL(r->flags))
  87. r->timeout = le32_to_cpu(p->timeout);
  88. else
  89. r->timeout = 0x00;
  90. *src_id = 0;
  91. return r;
  92. }
  93. static const struct scmi_event system_events[] = {
  94. {
  95. .id = SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER,
  96. .max_payld_sz =
  97. sizeof(struct scmi_system_power_state_notifier_payld),
  98. .max_report_sz =
  99. sizeof(struct scmi_system_power_state_notifier_report),
  100. },
  101. };
  102. static const struct scmi_event_ops system_event_ops = {
  103. .is_notify_supported = scmi_system_notify_supported,
  104. .set_notify_enabled = scmi_system_set_notify_enabled,
  105. .fill_custom_report = scmi_system_fill_custom_report,
  106. };
  107. static const struct scmi_protocol_events system_protocol_events = {
  108. .queue_sz = SCMI_PROTO_QUEUE_SZ,
  109. .ops = &system_event_ops,
  110. .evts = system_events,
  111. .num_events = ARRAY_SIZE(system_events),
  112. .num_sources = SCMI_SYSTEM_NUM_SOURCES,
  113. };
  114. static int scmi_system_protocol_init(const struct scmi_protocol_handle *ph)
  115. {
  116. int ret;
  117. u32 version;
  118. struct scmi_system_info *pinfo;
  119. ret = ph->xops->version_get(ph, &version);
  120. if (ret)
  121. return ret;
  122. dev_dbg(ph->dev, "System Power Version %d.%d\n",
  123. PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
  124. pinfo = devm_kzalloc(ph->dev, sizeof(*pinfo), GFP_KERNEL);
  125. if (!pinfo)
  126. return -ENOMEM;
  127. pinfo->version = version;
  128. if (PROTOCOL_REV_MAJOR(pinfo->version) >= 0x2)
  129. pinfo->graceful_timeout_supported = true;
  130. if (!ph->hops->protocol_msg_check(ph, SYSTEM_POWER_STATE_NOTIFY, NULL))
  131. pinfo->power_state_notify_cmd = true;
  132. return ph->set_priv(ph, pinfo, version);
  133. }
  134. static const struct scmi_protocol scmi_system = {
  135. .id = SCMI_PROTOCOL_SYSTEM,
  136. .owner = THIS_MODULE,
  137. .instance_init = &scmi_system_protocol_init,
  138. .ops = NULL,
  139. .events = &system_protocol_events,
  140. .supported_version = SCMI_PROTOCOL_SUPPORTED_VERSION,
  141. };
  142. DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(system, scmi_system)