system.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * System Control and Management Interface (SCMI) System Power Protocol
  4. *
  5. * Copyright (C) 2020 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 "common.h"
  11. #include "notify.h"
  12. #define SCMI_SYSTEM_NUM_SOURCES 1
  13. enum scmi_system_protocol_cmd {
  14. SYSTEM_POWER_STATE_NOTIFY = 0x5,
  15. };
  16. struct scmi_system_power_state_notify {
  17. __le32 notify_enable;
  18. };
  19. struct scmi_system_power_state_notifier_payld {
  20. __le32 agent_id;
  21. __le32 flags;
  22. __le32 system_state;
  23. };
  24. struct scmi_system_info {
  25. u32 version;
  26. };
  27. static int scmi_system_request_notify(const struct scmi_protocol_handle *ph,
  28. bool enable)
  29. {
  30. int ret;
  31. struct scmi_xfer *t;
  32. struct scmi_system_power_state_notify *notify;
  33. ret = ph->xops->xfer_get_init(ph, SYSTEM_POWER_STATE_NOTIFY,
  34. sizeof(*notify), 0, &t);
  35. if (ret)
  36. return ret;
  37. notify = t->tx.buf;
  38. notify->notify_enable = enable ? cpu_to_le32(BIT(0)) : 0;
  39. ret = ph->xops->do_xfer(ph, t);
  40. ph->xops->xfer_put(ph, t);
  41. return ret;
  42. }
  43. static int scmi_system_set_notify_enabled(const struct scmi_protocol_handle *ph,
  44. u8 evt_id, u32 src_id, bool enable)
  45. {
  46. int ret;
  47. ret = scmi_system_request_notify(ph, enable);
  48. if (ret)
  49. pr_debug("FAIL_ENABLE - evt[%X] - ret:%d\n", evt_id, ret);
  50. return ret;
  51. }
  52. static void *
  53. scmi_system_fill_custom_report(const struct scmi_protocol_handle *ph,
  54. u8 evt_id, ktime_t timestamp,
  55. const void *payld, size_t payld_sz,
  56. void *report, u32 *src_id)
  57. {
  58. const struct scmi_system_power_state_notifier_payld *p = payld;
  59. struct scmi_system_power_state_notifier_report *r = report;
  60. if (evt_id != SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER ||
  61. sizeof(*p) != payld_sz)
  62. return NULL;
  63. r->timestamp = timestamp;
  64. r->agent_id = le32_to_cpu(p->agent_id);
  65. r->flags = le32_to_cpu(p->flags);
  66. r->system_state = le32_to_cpu(p->system_state);
  67. *src_id = 0;
  68. return r;
  69. }
  70. static const struct scmi_event system_events[] = {
  71. {
  72. .id = SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER,
  73. .max_payld_sz =
  74. sizeof(struct scmi_system_power_state_notifier_payld),
  75. .max_report_sz =
  76. sizeof(struct scmi_system_power_state_notifier_report),
  77. },
  78. };
  79. static const struct scmi_event_ops system_event_ops = {
  80. .set_notify_enabled = scmi_system_set_notify_enabled,
  81. .fill_custom_report = scmi_system_fill_custom_report,
  82. };
  83. static const struct scmi_protocol_events system_protocol_events = {
  84. .queue_sz = SCMI_PROTO_QUEUE_SZ,
  85. .ops = &system_event_ops,
  86. .evts = system_events,
  87. .num_events = ARRAY_SIZE(system_events),
  88. .num_sources = SCMI_SYSTEM_NUM_SOURCES,
  89. };
  90. static int scmi_system_protocol_init(const struct scmi_protocol_handle *ph)
  91. {
  92. u32 version;
  93. struct scmi_system_info *pinfo;
  94. ph->xops->version_get(ph, &version);
  95. dev_dbg(ph->dev, "System Power Version %d.%d\n",
  96. PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
  97. pinfo = devm_kzalloc(ph->dev, sizeof(*pinfo), GFP_KERNEL);
  98. if (!pinfo)
  99. return -ENOMEM;
  100. pinfo->version = version;
  101. return ph->set_priv(ph, pinfo);
  102. }
  103. static const struct scmi_protocol scmi_system = {
  104. .id = SCMI_PROTOCOL_SYSTEM,
  105. .owner = THIS_MODULE,
  106. .init_instance = &scmi_system_protocol_init,
  107. .ops = NULL,
  108. .events = &system_protocol_events,
  109. };
  110. DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(system, scmi_system)