1 // SPDX-License-Identifier: GPL-2.0
3 * System Control and Management Interface (SCMI) Power Protocol
5 * Copyright (C) 2018-2022 ARM Ltd.
8 #define pr_fmt(fmt) "SCMI Notifications POWER - " fmt
10 #include <linux/module.h>
11 #include <linux/scmi_protocol.h>
13 #include "protocols.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,
21 POWER_DOMAIN_NAME_GET = 0x8,
24 struct scmi_msg_resp_power_attributes {
27 __le32 stats_addr_low;
28 __le32 stats_addr_high;
32 struct scmi_msg_resp_power_domain_attributes {
34 #define SUPPORTS_STATE_SET_NOTIFY(x) ((x) & BIT(31))
35 #define SUPPORTS_STATE_SET_ASYNC(x) ((x) & BIT(30))
36 #define SUPPORTS_STATE_SET_SYNC(x) ((x) & BIT(29))
37 #define SUPPORTS_EXTENDED_NAMES(x) ((x) & BIT(27))
38 u8 name[SCMI_SHORT_NAME_MAX_SIZE];
41 struct scmi_power_set_state {
43 #define STATE_SET_ASYNC BIT(0)
48 struct scmi_power_state_notify {
53 struct scmi_power_state_notify_payld {
59 struct power_dom_info {
62 bool state_set_notify;
63 char name[SCMI_MAX_STR_SIZE];
66 struct scmi_power_info {
71 struct power_dom_info *dom_info;
74 static int scmi_power_attributes_get(const struct scmi_protocol_handle *ph,
75 struct scmi_power_info *pi)
79 struct scmi_msg_resp_power_attributes *attr;
81 ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES,
82 0, sizeof(*attr), &t);
88 ret = ph->xops->do_xfer(ph, t);
90 pi->num_domains = le16_to_cpu(attr->num_domains);
91 pi->stats_addr = le32_to_cpu(attr->stats_addr_low) |
92 (u64)le32_to_cpu(attr->stats_addr_high) << 32;
93 pi->stats_size = le32_to_cpu(attr->stats_size);
96 ph->xops->xfer_put(ph, t);
101 scmi_power_domain_attributes_get(const struct scmi_protocol_handle *ph,
102 u32 domain, struct power_dom_info *dom_info,
108 struct scmi_msg_resp_power_domain_attributes *attr;
110 ret = ph->xops->xfer_get_init(ph, POWER_DOMAIN_ATTRIBUTES,
111 sizeof(domain), sizeof(*attr), &t);
115 put_unaligned_le32(domain, t->tx.buf);
118 ret = ph->xops->do_xfer(ph, t);
120 flags = le32_to_cpu(attr->flags);
122 dom_info->state_set_notify = SUPPORTS_STATE_SET_NOTIFY(flags);
123 dom_info->state_set_async = SUPPORTS_STATE_SET_ASYNC(flags);
124 dom_info->state_set_sync = SUPPORTS_STATE_SET_SYNC(flags);
125 strscpy(dom_info->name, attr->name, SCMI_SHORT_NAME_MAX_SIZE);
127 ph->xops->xfer_put(ph, t);
130 * If supported overwrite short name with the extended one;
131 * on error just carry on and use already provided short name.
133 if (!ret && PROTOCOL_REV_MAJOR(version) >= 0x3 &&
134 SUPPORTS_EXTENDED_NAMES(flags)) {
135 ph->hops->extended_name_get(ph, POWER_DOMAIN_NAME_GET,
136 domain, dom_info->name,
143 static int scmi_power_state_set(const struct scmi_protocol_handle *ph,
144 u32 domain, u32 state)
148 struct scmi_power_set_state *st;
150 ret = ph->xops->xfer_get_init(ph, POWER_STATE_SET, sizeof(*st), 0, &t);
155 st->flags = cpu_to_le32(0);
156 st->domain = cpu_to_le32(domain);
157 st->state = cpu_to_le32(state);
159 ret = ph->xops->do_xfer(ph, t);
161 ph->xops->xfer_put(ph, t);
165 static int scmi_power_state_get(const struct scmi_protocol_handle *ph,
166 u32 domain, u32 *state)
171 ret = ph->xops->xfer_get_init(ph, POWER_STATE_GET, sizeof(u32), sizeof(u32), &t);
175 put_unaligned_le32(domain, t->tx.buf);
177 ret = ph->xops->do_xfer(ph, t);
179 *state = get_unaligned_le32(t->rx.buf);
181 ph->xops->xfer_put(ph, t);
185 static int scmi_power_num_domains_get(const struct scmi_protocol_handle *ph)
187 struct scmi_power_info *pi = ph->get_priv(ph);
189 return pi->num_domains;
193 scmi_power_name_get(const struct scmi_protocol_handle *ph,
196 struct scmi_power_info *pi = ph->get_priv(ph);
197 struct power_dom_info *dom = pi->dom_info + domain;
202 static const struct scmi_power_proto_ops power_proto_ops = {
203 .num_domains_get = scmi_power_num_domains_get,
204 .name_get = scmi_power_name_get,
205 .state_set = scmi_power_state_set,
206 .state_get = scmi_power_state_get,
209 static int scmi_power_request_notify(const struct scmi_protocol_handle *ph,
210 u32 domain, bool enable)
214 struct scmi_power_state_notify *notify;
216 ret = ph->xops->xfer_get_init(ph, POWER_STATE_NOTIFY,
217 sizeof(*notify), 0, &t);
222 notify->domain = cpu_to_le32(domain);
223 notify->notify_enable = enable ? cpu_to_le32(BIT(0)) : 0;
225 ret = ph->xops->do_xfer(ph, t);
227 ph->xops->xfer_put(ph, t);
231 static int scmi_power_set_notify_enabled(const struct scmi_protocol_handle *ph,
232 u8 evt_id, u32 src_id, bool enable)
236 ret = scmi_power_request_notify(ph, src_id, enable);
238 pr_debug("FAIL_ENABLE - evt[%X] dom[%d] - ret:%d\n",
239 evt_id, src_id, ret);
245 scmi_power_fill_custom_report(const struct scmi_protocol_handle *ph,
246 u8 evt_id, ktime_t timestamp,
247 const void *payld, size_t payld_sz,
248 void *report, u32 *src_id)
250 const struct scmi_power_state_notify_payld *p = payld;
251 struct scmi_power_state_changed_report *r = report;
253 if (evt_id != SCMI_EVENT_POWER_STATE_CHANGED || sizeof(*p) != payld_sz)
256 r->timestamp = timestamp;
257 r->agent_id = le32_to_cpu(p->agent_id);
258 r->domain_id = le32_to_cpu(p->domain_id);
259 r->power_state = le32_to_cpu(p->power_state);
260 *src_id = r->domain_id;
265 static int scmi_power_get_num_sources(const struct scmi_protocol_handle *ph)
267 struct scmi_power_info *pinfo = ph->get_priv(ph);
272 return pinfo->num_domains;
275 static const struct scmi_event power_events[] = {
277 .id = SCMI_EVENT_POWER_STATE_CHANGED,
278 .max_payld_sz = sizeof(struct scmi_power_state_notify_payld),
280 sizeof(struct scmi_power_state_changed_report),
284 static const struct scmi_event_ops power_event_ops = {
285 .get_num_sources = scmi_power_get_num_sources,
286 .set_notify_enabled = scmi_power_set_notify_enabled,
287 .fill_custom_report = scmi_power_fill_custom_report,
290 static const struct scmi_protocol_events power_protocol_events = {
291 .queue_sz = SCMI_PROTO_QUEUE_SZ,
292 .ops = &power_event_ops,
293 .evts = power_events,
294 .num_events = ARRAY_SIZE(power_events),
297 static int scmi_power_protocol_init(const struct scmi_protocol_handle *ph)
301 struct scmi_power_info *pinfo;
303 ret = ph->xops->version_get(ph, &version);
307 dev_dbg(ph->dev, "Power Version %d.%d\n",
308 PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
310 pinfo = devm_kzalloc(ph->dev, sizeof(*pinfo), GFP_KERNEL);
314 ret = scmi_power_attributes_get(ph, pinfo);
318 pinfo->dom_info = devm_kcalloc(ph->dev, pinfo->num_domains,
319 sizeof(*pinfo->dom_info), GFP_KERNEL);
320 if (!pinfo->dom_info)
323 for (domain = 0; domain < pinfo->num_domains; domain++) {
324 struct power_dom_info *dom = pinfo->dom_info + domain;
326 scmi_power_domain_attributes_get(ph, domain, dom, version);
329 pinfo->version = version;
331 return ph->set_priv(ph, pinfo);
334 static const struct scmi_protocol scmi_power = {
335 .id = SCMI_PROTOCOL_POWER,
336 .owner = THIS_MODULE,
337 .instance_init = &scmi_power_protocol_init,
338 .ops = &power_proto_ops,
339 .events = &power_protocol_events,
342 DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(power, scmi_power)