blob: 70d366ed37e6bef2fa91a8593c7dd2435614dd17 [file] [log] [blame]
Sudeep Holla76a65502017-06-06 11:32:24 +01001// SPDX-License-Identifier: GPL-2.0
2/*
3 * System Control and Management Interface (SCMI) Power Protocol
4 *
Cristian Marussicf516fb2021-02-02 22:15:19 +00005 * Copyright (C) 2018-2020 ARM Ltd.
Sudeep Holla76a65502017-06-06 11:32:24 +01006 */
7
Cristian Marussie27077b2020-07-01 16:53:44 +01008#define pr_fmt(fmt) "SCMI Notifications POWER - " fmt
9
Cristian Marussidb15cd22021-02-02 22:15:54 +000010#include <linux/module.h>
Cristian Marussie27077b2020-07-01 16:53:44 +010011#include <linux/scmi_protocol.h>
12
Sudeep Holla76a65502017-06-06 11:32:24 +010013#include "common.h"
Cristian Marussie27077b2020-07-01 16:53:44 +010014#include "notify.h"
Sudeep Holla76a65502017-06-06 11:32:24 +010015
16enum 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};
22
23struct scmi_msg_resp_power_attributes {
24 __le16 num_domains;
25 __le16 reserved;
26 __le32 stats_addr_low;
27 __le32 stats_addr_high;
28 __le32 stats_size;
29};
30
31struct scmi_msg_resp_power_domain_attributes {
32 __le32 flags;
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];
37};
38
39struct scmi_power_set_state {
40 __le32 flags;
41#define STATE_SET_ASYNC BIT(0)
42 __le32 domain;
43 __le32 state;
44};
45
46struct scmi_power_state_notify {
47 __le32 domain;
48 __le32 notify_enable;
49};
50
Cristian Marussie27077b2020-07-01 16:53:44 +010051struct scmi_power_state_notify_payld {
52 __le32 agent_id;
53 __le32 domain_id;
54 __le32 power_state;
55};
56
Sudeep Holla76a65502017-06-06 11:32:24 +010057struct power_dom_info {
58 bool state_set_sync;
59 bool state_set_async;
60 bool state_set_notify;
61 char name[SCMI_MAX_STR_SIZE];
62};
63
64struct scmi_power_info {
Sudeep Hollab55b06b2019-11-22 14:48:40 +000065 u32 version;
Sudeep Holla76a65502017-06-06 11:32:24 +010066 int num_domains;
67 u64 stats_addr;
68 u32 stats_size;
69 struct power_dom_info *dom_info;
70};
71
Cristian Marussiec2e97852021-02-02 22:15:33 +000072static int scmi_power_attributes_get(const struct scmi_protocol_handle *ph,
Sudeep Holla76a65502017-06-06 11:32:24 +010073 struct scmi_power_info *pi)
74{
75 int ret;
76 struct scmi_xfer *t;
77 struct scmi_msg_resp_power_attributes *attr;
78
Cristian Marussiec2e97852021-02-02 22:15:33 +000079 ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES,
80 0, sizeof(*attr), &t);
Sudeep Holla76a65502017-06-06 11:32:24 +010081 if (ret)
82 return ret;
83
84 attr = t->rx.buf;
85
Cristian Marussiec2e97852021-02-02 22:15:33 +000086 ret = ph->xops->do_xfer(ph, t);
Sudeep Holla76a65502017-06-06 11:32:24 +010087 if (!ret) {
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);
92 }
93
Cristian Marussiec2e97852021-02-02 22:15:33 +000094 ph->xops->xfer_put(ph, t);
Sudeep Holla76a65502017-06-06 11:32:24 +010095 return ret;
96}
97
98static int
Cristian Marussiec2e97852021-02-02 22:15:33 +000099scmi_power_domain_attributes_get(const struct scmi_protocol_handle *ph,
100 u32 domain, struct power_dom_info *dom_info)
Sudeep Holla76a65502017-06-06 11:32:24 +0100101{
102 int ret;
103 struct scmi_xfer *t;
104 struct scmi_msg_resp_power_domain_attributes *attr;
105
Cristian Marussiec2e97852021-02-02 22:15:33 +0000106 ret = ph->xops->xfer_get_init(ph, POWER_DOMAIN_ATTRIBUTES,
107 sizeof(domain), sizeof(*attr), &t);
Sudeep Holla76a65502017-06-06 11:32:24 +0100108 if (ret)
109 return ret;
110
Sudeep Hollaaa90ac42019-08-07 13:46:27 +0100111 put_unaligned_le32(domain, t->tx.buf);
Sudeep Holla76a65502017-06-06 11:32:24 +0100112 attr = t->rx.buf;
113
Cristian Marussiec2e97852021-02-02 22:15:33 +0000114 ret = ph->xops->do_xfer(ph, t);
Sudeep Holla76a65502017-06-06 11:32:24 +0100115 if (!ret) {
116 u32 flags = le32_to_cpu(attr->flags);
117
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);
Sudeep Hollaca64b712018-09-07 17:03:25 +0100121 strlcpy(dom_info->name, attr->name, SCMI_MAX_STR_SIZE);
Sudeep Holla76a65502017-06-06 11:32:24 +0100122 }
123
Cristian Marussiec2e97852021-02-02 22:15:33 +0000124 ph->xops->xfer_put(ph, t);
Sudeep Holla76a65502017-06-06 11:32:24 +0100125 return ret;
126}
127
Cristian Marussiec2e97852021-02-02 22:15:33 +0000128static int scmi_power_state_set(const struct scmi_protocol_handle *ph,
129 u32 domain, u32 state)
Sudeep Holla76a65502017-06-06 11:32:24 +0100130{
131 int ret;
132 struct scmi_xfer *t;
133 struct scmi_power_set_state *st;
134
Cristian Marussiec2e97852021-02-02 22:15:33 +0000135 ret = ph->xops->xfer_get_init(ph, POWER_STATE_SET, sizeof(*st), 0, &t);
Sudeep Holla76a65502017-06-06 11:32:24 +0100136 if (ret)
137 return ret;
138
139 st = t->tx.buf;
140 st->flags = cpu_to_le32(0);
141 st->domain = cpu_to_le32(domain);
142 st->state = cpu_to_le32(state);
143
Cristian Marussiec2e97852021-02-02 22:15:33 +0000144 ret = ph->xops->do_xfer(ph, t);
Sudeep Holla76a65502017-06-06 11:32:24 +0100145
Cristian Marussiec2e97852021-02-02 22:15:33 +0000146 ph->xops->xfer_put(ph, t);
Sudeep Holla76a65502017-06-06 11:32:24 +0100147 return ret;
148}
149
Cristian Marussiec2e97852021-02-02 22:15:33 +0000150static int scmi_power_state_get(const struct scmi_protocol_handle *ph,
151 u32 domain, u32 *state)
Sudeep Holla76a65502017-06-06 11:32:24 +0100152{
153 int ret;
154 struct scmi_xfer *t;
155
Cristian Marussiec2e97852021-02-02 22:15:33 +0000156 ret = ph->xops->xfer_get_init(ph, POWER_STATE_GET, sizeof(u32), sizeof(u32), &t);
Sudeep Holla76a65502017-06-06 11:32:24 +0100157 if (ret)
158 return ret;
159
Sudeep Hollaaa90ac42019-08-07 13:46:27 +0100160 put_unaligned_le32(domain, t->tx.buf);
Sudeep Holla76a65502017-06-06 11:32:24 +0100161
Cristian Marussiec2e97852021-02-02 22:15:33 +0000162 ret = ph->xops->do_xfer(ph, t);
Sudeep Holla76a65502017-06-06 11:32:24 +0100163 if (!ret)
Sudeep Hollaaa90ac42019-08-07 13:46:27 +0100164 *state = get_unaligned_le32(t->rx.buf);
Sudeep Holla76a65502017-06-06 11:32:24 +0100165
Cristian Marussiec2e97852021-02-02 22:15:33 +0000166 ph->xops->xfer_put(ph, t);
Sudeep Holla76a65502017-06-06 11:32:24 +0100167 return ret;
168}
169
Cristian Marussiec2e97852021-02-02 22:15:33 +0000170static int scmi_power_num_domains_get(const struct scmi_protocol_handle *ph)
171{
172 struct scmi_power_info *pi = ph->get_priv(ph);
Sudeep Holla76a65502017-06-06 11:32:24 +0100173
174 return pi->num_domains;
175}
176
Cristian Marussiec2e97852021-02-02 22:15:33 +0000177static char *scmi_power_name_get(const struct scmi_protocol_handle *ph,
178 u32 domain)
179{
180 struct scmi_power_info *pi = ph->get_priv(ph);
Sudeep Holla76a65502017-06-06 11:32:24 +0100181 struct power_dom_info *dom = pi->dom_info + domain;
182
183 return dom->name;
184}
185
Cristian Marussiec2e97852021-02-02 22:15:33 +0000186static const struct scmi_power_proto_ops power_proto_ops = {
Sudeep Holla76a65502017-06-06 11:32:24 +0100187 .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,
191};
192
Cristian Marussiec2e97852021-02-02 22:15:33 +0000193static int scmi_power_request_notify(const struct scmi_protocol_handle *ph,
Cristian Marussie27077b2020-07-01 16:53:44 +0100194 u32 domain, bool enable)
195{
196 int ret;
197 struct scmi_xfer *t;
198 struct scmi_power_state_notify *notify;
199
Cristian Marussiec2e97852021-02-02 22:15:33 +0000200 ret = ph->xops->xfer_get_init(ph, POWER_STATE_NOTIFY,
201 sizeof(*notify), 0, &t);
Cristian Marussie27077b2020-07-01 16:53:44 +0100202 if (ret)
203 return ret;
204
205 notify = t->tx.buf;
206 notify->domain = cpu_to_le32(domain);
207 notify->notify_enable = enable ? cpu_to_le32(BIT(0)) : 0;
208
Cristian Marussiec2e97852021-02-02 22:15:33 +0000209 ret = ph->xops->do_xfer(ph, t);
Cristian Marussie27077b2020-07-01 16:53:44 +0100210
Cristian Marussiec2e97852021-02-02 22:15:33 +0000211 ph->xops->xfer_put(ph, t);
Cristian Marussie27077b2020-07-01 16:53:44 +0100212 return ret;
213}
214
Cristian Marussibb35ff42021-02-02 22:15:52 +0000215static int scmi_power_set_notify_enabled(const struct scmi_protocol_handle *ph,
Cristian Marussie27077b2020-07-01 16:53:44 +0100216 u8 evt_id, u32 src_id, bool enable)
217{
218 int ret;
219
Cristian Marussiec2e97852021-02-02 22:15:33 +0000220 ret = scmi_power_request_notify(ph, src_id, enable);
Cristian Marussie27077b2020-07-01 16:53:44 +0100221 if (ret)
222 pr_debug("FAIL_ENABLE - evt[%X] dom[%d] - ret:%d\n",
223 evt_id, src_id, ret);
224
225 return ret;
226}
227
Cristian Marussibb35ff42021-02-02 22:15:52 +0000228static void *
229scmi_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)
Cristian Marussie27077b2020-07-01 16:53:44 +0100233{
234 const struct scmi_power_state_notify_payld *p = payld;
235 struct scmi_power_state_changed_report *r = report;
236
237 if (evt_id != SCMI_EVENT_POWER_STATE_CHANGED || sizeof(*p) != payld_sz)
238 return NULL;
239
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;
245
246 return r;
247}
248
Cristian Marussibb35ff42021-02-02 22:15:52 +0000249static int scmi_power_get_num_sources(const struct scmi_protocol_handle *ph)
Cristian Marussi834134a2021-02-02 22:15:25 +0000250{
Cristian Marussibb35ff42021-02-02 22:15:52 +0000251 struct scmi_power_info *pinfo = ph->get_priv(ph);
Cristian Marussi834134a2021-02-02 22:15:25 +0000252
253 if (!pinfo)
254 return -EINVAL;
255
256 return pinfo->num_domains;
257}
258
Cristian Marussie27077b2020-07-01 16:53:44 +0100259static const struct scmi_event power_events[] = {
260 {
261 .id = SCMI_EVENT_POWER_STATE_CHANGED,
262 .max_payld_sz = sizeof(struct scmi_power_state_notify_payld),
263 .max_report_sz =
264 sizeof(struct scmi_power_state_changed_report),
265 },
266};
267
268static const struct scmi_event_ops power_event_ops = {
Cristian Marussi834134a2021-02-02 22:15:25 +0000269 .get_num_sources = scmi_power_get_num_sources,
Cristian Marussie27077b2020-07-01 16:53:44 +0100270 .set_notify_enabled = scmi_power_set_notify_enabled,
271 .fill_custom_report = scmi_power_fill_custom_report,
272};
273
Cristian Marussi834134a2021-02-02 22:15:25 +0000274static 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),
279};
280
Cristian Marussiec2e97852021-02-02 22:15:33 +0000281static int scmi_power_protocol_init(const struct scmi_protocol_handle *ph)
Sudeep Holla76a65502017-06-06 11:32:24 +0100282{
283 int domain;
284 u32 version;
285 struct scmi_power_info *pinfo;
286
Cristian Marussiec2e97852021-02-02 22:15:33 +0000287 ph->xops->version_get(ph, &version);
Sudeep Holla76a65502017-06-06 11:32:24 +0100288
Cristian Marussiec2e97852021-02-02 22:15:33 +0000289 dev_dbg(ph->dev, "Power Version %d.%d\n",
Sudeep Holla76a65502017-06-06 11:32:24 +0100290 PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
291
Cristian Marussiec2e97852021-02-02 22:15:33 +0000292 pinfo = devm_kzalloc(ph->dev, sizeof(*pinfo), GFP_KERNEL);
Sudeep Holla76a65502017-06-06 11:32:24 +0100293 if (!pinfo)
294 return -ENOMEM;
295
Cristian Marussiec2e97852021-02-02 22:15:33 +0000296 scmi_power_attributes_get(ph, pinfo);
Sudeep Holla76a65502017-06-06 11:32:24 +0100297
Cristian Marussiec2e97852021-02-02 22:15:33 +0000298 pinfo->dom_info = devm_kcalloc(ph->dev, pinfo->num_domains,
Sudeep Holla76a65502017-06-06 11:32:24 +0100299 sizeof(*pinfo->dom_info), GFP_KERNEL);
300 if (!pinfo->dom_info)
301 return -ENOMEM;
302
303 for (domain = 0; domain < pinfo->num_domains; domain++) {
304 struct power_dom_info *dom = pinfo->dom_info + domain;
305
Cristian Marussiec2e97852021-02-02 22:15:33 +0000306 scmi_power_domain_attributes_get(ph, domain, dom);
Sudeep Holla76a65502017-06-06 11:32:24 +0100307 }
308
Sudeep Hollab55b06b2019-11-22 14:48:40 +0000309 pinfo->version = version;
Sudeep Holla76a65502017-06-06 11:32:24 +0100310
Cristian Marussiec2e97852021-02-02 22:15:33 +0000311 return ph->set_priv(ph, pinfo);
Sudeep Holla76a65502017-06-06 11:32:24 +0100312}
313
Cristian Marussicf516fb2021-02-02 22:15:19 +0000314static const struct scmi_protocol scmi_power = {
315 .id = SCMI_PROTOCOL_POWER,
Cristian Marussidb15cd22021-02-02 22:15:54 +0000316 .owner = THIS_MODULE,
Cristian Marussiec2e97852021-02-02 22:15:33 +0000317 .init_instance = &scmi_power_protocol_init,
318 .ops = &power_proto_ops,
Cristian Marussi834134a2021-02-02 22:15:25 +0000319 .events = &power_protocol_events,
Cristian Marussicf516fb2021-02-02 22:15:19 +0000320};
321
322DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(power, scmi_power)