0001
0002
0003
0004
0005
0006
0007
0008 #define pr_fmt(fmt) "SCMI Notifications SYSTEM - " fmt
0009
0010 #include <linux/module.h>
0011 #include <linux/scmi_protocol.h>
0012
0013 #include "protocols.h"
0014 #include "notify.h"
0015
0016 #define SCMI_SYSTEM_NUM_SOURCES 1
0017
0018 enum scmi_system_protocol_cmd {
0019 SYSTEM_POWER_STATE_NOTIFY = 0x5,
0020 };
0021
0022 struct scmi_system_power_state_notify {
0023 __le32 notify_enable;
0024 };
0025
0026 struct scmi_system_power_state_notifier_payld {
0027 __le32 agent_id;
0028 __le32 flags;
0029 __le32 system_state;
0030 __le32 timeout;
0031 };
0032
0033 struct scmi_system_info {
0034 u32 version;
0035 bool graceful_timeout_supported;
0036 };
0037
0038 static int scmi_system_request_notify(const struct scmi_protocol_handle *ph,
0039 bool enable)
0040 {
0041 int ret;
0042 struct scmi_xfer *t;
0043 struct scmi_system_power_state_notify *notify;
0044
0045 ret = ph->xops->xfer_get_init(ph, SYSTEM_POWER_STATE_NOTIFY,
0046 sizeof(*notify), 0, &t);
0047 if (ret)
0048 return ret;
0049
0050 notify = t->tx.buf;
0051 notify->notify_enable = enable ? cpu_to_le32(BIT(0)) : 0;
0052
0053 ret = ph->xops->do_xfer(ph, t);
0054
0055 ph->xops->xfer_put(ph, t);
0056 return ret;
0057 }
0058
0059 static int scmi_system_set_notify_enabled(const struct scmi_protocol_handle *ph,
0060 u8 evt_id, u32 src_id, bool enable)
0061 {
0062 int ret;
0063
0064 ret = scmi_system_request_notify(ph, enable);
0065 if (ret)
0066 pr_debug("FAIL_ENABLE - evt[%X] - ret:%d\n", evt_id, ret);
0067
0068 return ret;
0069 }
0070
0071 static void *
0072 scmi_system_fill_custom_report(const struct scmi_protocol_handle *ph,
0073 u8 evt_id, ktime_t timestamp,
0074 const void *payld, size_t payld_sz,
0075 void *report, u32 *src_id)
0076 {
0077 size_t expected_sz;
0078 const struct scmi_system_power_state_notifier_payld *p = payld;
0079 struct scmi_system_power_state_notifier_report *r = report;
0080 struct scmi_system_info *pinfo = ph->get_priv(ph);
0081
0082 expected_sz = pinfo->graceful_timeout_supported ?
0083 sizeof(*p) : sizeof(*p) - sizeof(__le32);
0084 if (evt_id != SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER ||
0085 payld_sz != expected_sz)
0086 return NULL;
0087
0088 r->timestamp = timestamp;
0089 r->agent_id = le32_to_cpu(p->agent_id);
0090 r->flags = le32_to_cpu(p->flags);
0091 r->system_state = le32_to_cpu(p->system_state);
0092 if (pinfo->graceful_timeout_supported &&
0093 r->system_state == SCMI_SYSTEM_SHUTDOWN &&
0094 SCMI_SYSPOWER_IS_REQUEST_GRACEFUL(r->flags))
0095 r->timeout = le32_to_cpu(p->timeout);
0096 else
0097 r->timeout = 0x00;
0098 *src_id = 0;
0099
0100 return r;
0101 }
0102
0103 static const struct scmi_event system_events[] = {
0104 {
0105 .id = SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER,
0106 .max_payld_sz =
0107 sizeof(struct scmi_system_power_state_notifier_payld),
0108 .max_report_sz =
0109 sizeof(struct scmi_system_power_state_notifier_report),
0110 },
0111 };
0112
0113 static const struct scmi_event_ops system_event_ops = {
0114 .set_notify_enabled = scmi_system_set_notify_enabled,
0115 .fill_custom_report = scmi_system_fill_custom_report,
0116 };
0117
0118 static const struct scmi_protocol_events system_protocol_events = {
0119 .queue_sz = SCMI_PROTO_QUEUE_SZ,
0120 .ops = &system_event_ops,
0121 .evts = system_events,
0122 .num_events = ARRAY_SIZE(system_events),
0123 .num_sources = SCMI_SYSTEM_NUM_SOURCES,
0124 };
0125
0126 static int scmi_system_protocol_init(const struct scmi_protocol_handle *ph)
0127 {
0128 int ret;
0129 u32 version;
0130 struct scmi_system_info *pinfo;
0131
0132 ret = ph->xops->version_get(ph, &version);
0133 if (ret)
0134 return ret;
0135
0136 dev_dbg(ph->dev, "System Power Version %d.%d\n",
0137 PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
0138
0139 pinfo = devm_kzalloc(ph->dev, sizeof(*pinfo), GFP_KERNEL);
0140 if (!pinfo)
0141 return -ENOMEM;
0142
0143 pinfo->version = version;
0144 if (PROTOCOL_REV_MAJOR(pinfo->version) >= 0x2)
0145 pinfo->graceful_timeout_supported = true;
0146
0147 return ph->set_priv(ph, pinfo);
0148 }
0149
0150 static const struct scmi_protocol scmi_system = {
0151 .id = SCMI_PROTOCOL_SYSTEM,
0152 .owner = THIS_MODULE,
0153 .instance_init = &scmi_system_protocol_init,
0154 .ops = NULL,
0155 .events = &system_protocol_events,
0156 };
0157
0158 DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(system, scmi_system)