1 // SPDX-License-Identifier: GPL-2.0
3 * System Control and Management Interface (SCMI) Power Protocol
5 * Copyright (C) 2018-2021 ARM Ltd.
8 #define pr_fmt(fmt) "SCMI Notifications POWER - " fmt
10 #include <linux/module.h>
11 #include <linux/scmi_protocol.h>
16 enum scmi_power_protocol_cmd {
17 POWER_DOMAIN_ATTRIBUTES = 0x3,
18 POWER_STATE_SET = 0x4,
19 POWER_STATE_GET = 0x5,
20 POWER_STATE_NOTIFY = 0x6,
23 struct scmi_msg_resp_power_attributes {
26 __le32 stats_addr_low;
27 __le32 stats_addr_high;
31 struct scmi_msg_resp_power_domain_attributes {
33 #define SUPPORTS_STATE_SET_NOTIFY(x) ((x) & BIT(31))
34 #define SUPPORTS_STATE_SET_ASYNC(x) ((x) & BIT(30))
35 #define SUPPORTS_STATE_SET_SYNC(x) ((x) & BIT(29))
36 u8 name[SCMI_MAX_STR_SIZE];
39 struct scmi_power_set_state {
41 #define STATE_SET_ASYNC BIT(0)
46 struct scmi_power_state_notify {
51 struct scmi_power_state_notify_payld {
57 struct power_dom_info {
60 bool state_set_notify;
61 char name[SCMI_MAX_STR_SIZE];
64 struct scmi_power_info {
69 struct power_dom_info *dom_info;
72 static int scmi_power_attributes_get(const struct scmi_protocol_handle *ph,
73 struct scmi_power_info *pi)
77 struct scmi_msg_resp_power_attributes *attr;
79 ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES,
80 0, sizeof(*attr), &t);
86 ret = ph->xops->do_xfer(ph, t);
88 pi->num_domains = le16_to_cpu(attr->num_domains);
89 pi->stats_addr = le32_to_cpu(attr->stats_addr_low) |
90 (u64)le32_to_cpu(attr->stats_addr_high) << 32;
91 pi->stats_size = le32_to_cpu(attr->stats_size);
94 ph->xops->xfer_put(ph, t);
99 scmi_power_domain_attributes_get(const struct scmi_protocol_handle *ph,
100 u32 domain, struct power_dom_info *dom_info)
104 struct scmi_msg_resp_power_domain_attributes *attr;
106 ret = ph->xops->xfer_get_init(ph, POWER_DOMAIN_ATTRIBUTES,
107 sizeof(domain), sizeof(*attr), &t);
111 put_unaligned_le32(domain, t->tx.buf);
114 ret = ph->xops->do_xfer(ph, t);
116 u32 flags = le32_to_cpu(attr->flags);
118 dom_info->state_set_notify = SUPPORTS_STATE_SET_NOTIFY(flags);
119 dom_info->state_set_async = SUPPORTS_STATE_SET_ASYNC(flags);
120 dom_info->state_set_sync = SUPPORTS_STATE_SET_SYNC(flags);
121 strlcpy(dom_info->name, attr->name, SCMI_MAX_STR_SIZE);
124 ph->xops->xfer_put(ph, t);
128 static int scmi_power_state_set(const struct scmi_protocol_handle *ph,
129 u32 domain, u32 state)
133 struct scmi_power_set_state *st;
135 ret = ph->xops->xfer_get_init(ph, POWER_STATE_SET, sizeof(*st), 0, &t);
140 st->flags = cpu_to_le32(0);
141 st->domain = cpu_to_le32(domain);
142 st->state = cpu_to_le32(state);
144 ret = ph->xops->do_xfer(ph, t);
146 ph->xops->xfer_put(ph, t);
150 static int scmi_power_state_get(const struct scmi_protocol_handle *ph,
151 u32 domain, u32 *state)
156 ret = ph->xops->xfer_get_init(ph, POWER_STATE_GET, sizeof(u32), sizeof(u32), &t);
160 put_unaligned_le32(domain, t->tx.buf);
162 ret = ph->xops->do_xfer(ph, t);
164 *state = get_unaligned_le32(t->rx.buf);
166 ph->xops->xfer_put(ph, t);
170 static int scmi_power_num_domains_get(const struct scmi_protocol_handle *ph)
172 struct scmi_power_info *pi = ph->get_priv(ph);
174 return pi->num_domains;
177 static char *scmi_power_name_get(const struct scmi_protocol_handle *ph,
180 struct scmi_power_info *pi = ph->get_priv(ph);
181 struct power_dom_info *dom = pi->dom_info + domain;
186 static const struct scmi_power_proto_ops power_proto_ops = {
187 .num_domains_get = scmi_power_num_domains_get,
188 .name_get = scmi_power_name_get,
189 .state_set = scmi_power_state_set,
190 .state_get = scmi_power_state_get,
193 static int scmi_power_request_notify(const struct scmi_protocol_handle *ph,
194 u32 domain, bool enable)
198 struct scmi_power_state_notify *notify;
200 ret = ph->xops->xfer_get_init(ph, POWER_STATE_NOTIFY,
201 sizeof(*notify), 0, &t);
206 notify->domain = cpu_to_le32(domain);
207 notify->notify_enable = enable ? cpu_to_le32(BIT(0)) : 0;
209 ret = ph->xops->do_xfer(ph, t);
211 ph->xops->xfer_put(ph, t);
215 static int scmi_power_set_notify_enabled(const struct scmi_protocol_handle *ph,
216 u8 evt_id, u32 src_id, bool enable)
220 ret = scmi_power_request_notify(ph, src_id, enable);
222 pr_debug("FAIL_ENABLE - evt[%X] dom[%d] - ret:%d\n",
223 evt_id, src_id, ret);
229 scmi_power_fill_custom_report(const struct scmi_protocol_handle *ph,
230 u8 evt_id, ktime_t timestamp,
231 const void *payld, size_t payld_sz,
232 void *report, u32 *src_id)
234 const struct scmi_power_state_notify_payld *p = payld;
235 struct scmi_power_state_changed_report *r = report;
237 if (evt_id != SCMI_EVENT_POWER_STATE_CHANGED || sizeof(*p) != payld_sz)
240 r->timestamp = timestamp;
241 r->agent_id = le32_to_cpu(p->agent_id);
242 r->domain_id = le32_to_cpu(p->domain_id);
243 r->power_state = le32_to_cpu(p->power_state);
244 *src_id = r->domain_id;
249 static int scmi_power_get_num_sources(const struct scmi_protocol_handle *ph)
251 struct scmi_power_info *pinfo = ph->get_priv(ph);
256 return pinfo->num_domains;
259 static const struct scmi_event power_events[] = {
261 .id = SCMI_EVENT_POWER_STATE_CHANGED,
262 .max_payld_sz = sizeof(struct scmi_power_state_notify_payld),
264 sizeof(struct scmi_power_state_changed_report),
268 static const struct scmi_event_ops power_event_ops = {
269 .get_num_sources = scmi_power_get_num_sources,
270 .set_notify_enabled = scmi_power_set_notify_enabled,
271 .fill_custom_report = scmi_power_fill_custom_report,
274 static const struct scmi_protocol_events power_protocol_events = {
275 .queue_sz = SCMI_PROTO_QUEUE_SZ,
276 .ops = &power_event_ops,
277 .evts = power_events,
278 .num_events = ARRAY_SIZE(power_events),
281 static int scmi_power_protocol_init(const struct scmi_protocol_handle *ph)
285 struct scmi_power_info *pinfo;
287 ph->xops->version_get(ph, &version);
289 dev_dbg(ph->dev, "Power Version %d.%d\n",
290 PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
292 pinfo = devm_kzalloc(ph->dev, sizeof(*pinfo), GFP_KERNEL);
296 scmi_power_attributes_get(ph, pinfo);
298 pinfo->dom_info = devm_kcalloc(ph->dev, pinfo->num_domains,
299 sizeof(*pinfo->dom_info), GFP_KERNEL);
300 if (!pinfo->dom_info)
303 for (domain = 0; domain < pinfo->num_domains; domain++) {
304 struct power_dom_info *dom = pinfo->dom_info + domain;
306 scmi_power_domain_attributes_get(ph, domain, dom);
309 pinfo->version = version;
311 return ph->set_priv(ph, pinfo);
314 static const struct scmi_protocol scmi_power = {
315 .id = SCMI_PROTOCOL_POWER,
316 .owner = THIS_MODULE,
317 .instance_init = &scmi_power_protocol_init,
318 .ops = &power_proto_ops,
319 .events = &power_protocol_events,
322 DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(power, scmi_power)