0001
0002
0003
0004
0005
0006
0007
0008 #define pr_fmt(fmt) "SCMI Notifications POWER - " fmt
0009
0010 #include <linux/module.h>
0011 #include <linux/scmi_protocol.h>
0012
0013 #include "protocols.h"
0014 #include "notify.h"
0015
0016 enum scmi_power_protocol_cmd {
0017 POWER_DOMAIN_ATTRIBUTES = 0x3,
0018 POWER_STATE_SET = 0x4,
0019 POWER_STATE_GET = 0x5,
0020 POWER_STATE_NOTIFY = 0x6,
0021 POWER_DOMAIN_NAME_GET = 0x8,
0022 };
0023
0024 struct scmi_msg_resp_power_attributes {
0025 __le16 num_domains;
0026 __le16 reserved;
0027 __le32 stats_addr_low;
0028 __le32 stats_addr_high;
0029 __le32 stats_size;
0030 };
0031
0032 struct scmi_msg_resp_power_domain_attributes {
0033 __le32 flags;
0034 #define SUPPORTS_STATE_SET_NOTIFY(x) ((x) & BIT(31))
0035 #define SUPPORTS_STATE_SET_ASYNC(x) ((x) & BIT(30))
0036 #define SUPPORTS_STATE_SET_SYNC(x) ((x) & BIT(29))
0037 #define SUPPORTS_EXTENDED_NAMES(x) ((x) & BIT(27))
0038 u8 name[SCMI_SHORT_NAME_MAX_SIZE];
0039 };
0040
0041 struct scmi_power_set_state {
0042 __le32 flags;
0043 #define STATE_SET_ASYNC BIT(0)
0044 __le32 domain;
0045 __le32 state;
0046 };
0047
0048 struct scmi_power_state_notify {
0049 __le32 domain;
0050 __le32 notify_enable;
0051 };
0052
0053 struct scmi_power_state_notify_payld {
0054 __le32 agent_id;
0055 __le32 domain_id;
0056 __le32 power_state;
0057 };
0058
0059 struct power_dom_info {
0060 bool state_set_sync;
0061 bool state_set_async;
0062 bool state_set_notify;
0063 char name[SCMI_MAX_STR_SIZE];
0064 };
0065
0066 struct scmi_power_info {
0067 u32 version;
0068 int num_domains;
0069 u64 stats_addr;
0070 u32 stats_size;
0071 struct power_dom_info *dom_info;
0072 };
0073
0074 static int scmi_power_attributes_get(const struct scmi_protocol_handle *ph,
0075 struct scmi_power_info *pi)
0076 {
0077 int ret;
0078 struct scmi_xfer *t;
0079 struct scmi_msg_resp_power_attributes *attr;
0080
0081 ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES,
0082 0, sizeof(*attr), &t);
0083 if (ret)
0084 return ret;
0085
0086 attr = t->rx.buf;
0087
0088 ret = ph->xops->do_xfer(ph, t);
0089 if (!ret) {
0090 pi->num_domains = le16_to_cpu(attr->num_domains);
0091 pi->stats_addr = le32_to_cpu(attr->stats_addr_low) |
0092 (u64)le32_to_cpu(attr->stats_addr_high) << 32;
0093 pi->stats_size = le32_to_cpu(attr->stats_size);
0094 }
0095
0096 ph->xops->xfer_put(ph, t);
0097 return ret;
0098 }
0099
0100 static int
0101 scmi_power_domain_attributes_get(const struct scmi_protocol_handle *ph,
0102 u32 domain, struct power_dom_info *dom_info,
0103 u32 version)
0104 {
0105 int ret;
0106 u32 flags;
0107 struct scmi_xfer *t;
0108 struct scmi_msg_resp_power_domain_attributes *attr;
0109
0110 ret = ph->xops->xfer_get_init(ph, POWER_DOMAIN_ATTRIBUTES,
0111 sizeof(domain), sizeof(*attr), &t);
0112 if (ret)
0113 return ret;
0114
0115 put_unaligned_le32(domain, t->tx.buf);
0116 attr = t->rx.buf;
0117
0118 ret = ph->xops->do_xfer(ph, t);
0119 if (!ret) {
0120 flags = le32_to_cpu(attr->flags);
0121
0122 dom_info->state_set_notify = SUPPORTS_STATE_SET_NOTIFY(flags);
0123 dom_info->state_set_async = SUPPORTS_STATE_SET_ASYNC(flags);
0124 dom_info->state_set_sync = SUPPORTS_STATE_SET_SYNC(flags);
0125 strscpy(dom_info->name, attr->name, SCMI_SHORT_NAME_MAX_SIZE);
0126 }
0127 ph->xops->xfer_put(ph, t);
0128
0129
0130
0131
0132
0133 if (!ret && PROTOCOL_REV_MAJOR(version) >= 0x3 &&
0134 SUPPORTS_EXTENDED_NAMES(flags)) {
0135 ph->hops->extended_name_get(ph, POWER_DOMAIN_NAME_GET,
0136 domain, dom_info->name,
0137 SCMI_MAX_STR_SIZE);
0138 }
0139
0140 return ret;
0141 }
0142
0143 static int scmi_power_state_set(const struct scmi_protocol_handle *ph,
0144 u32 domain, u32 state)
0145 {
0146 int ret;
0147 struct scmi_xfer *t;
0148 struct scmi_power_set_state *st;
0149
0150 ret = ph->xops->xfer_get_init(ph, POWER_STATE_SET, sizeof(*st), 0, &t);
0151 if (ret)
0152 return ret;
0153
0154 st = t->tx.buf;
0155 st->flags = cpu_to_le32(0);
0156 st->domain = cpu_to_le32(domain);
0157 st->state = cpu_to_le32(state);
0158
0159 ret = ph->xops->do_xfer(ph, t);
0160
0161 ph->xops->xfer_put(ph, t);
0162 return ret;
0163 }
0164
0165 static int scmi_power_state_get(const struct scmi_protocol_handle *ph,
0166 u32 domain, u32 *state)
0167 {
0168 int ret;
0169 struct scmi_xfer *t;
0170
0171 ret = ph->xops->xfer_get_init(ph, POWER_STATE_GET, sizeof(u32), sizeof(u32), &t);
0172 if (ret)
0173 return ret;
0174
0175 put_unaligned_le32(domain, t->tx.buf);
0176
0177 ret = ph->xops->do_xfer(ph, t);
0178 if (!ret)
0179 *state = get_unaligned_le32(t->rx.buf);
0180
0181 ph->xops->xfer_put(ph, t);
0182 return ret;
0183 }
0184
0185 static int scmi_power_num_domains_get(const struct scmi_protocol_handle *ph)
0186 {
0187 struct scmi_power_info *pi = ph->get_priv(ph);
0188
0189 return pi->num_domains;
0190 }
0191
0192 static const char *
0193 scmi_power_name_get(const struct scmi_protocol_handle *ph,
0194 u32 domain)
0195 {
0196 struct scmi_power_info *pi = ph->get_priv(ph);
0197 struct power_dom_info *dom = pi->dom_info + domain;
0198
0199 return dom->name;
0200 }
0201
0202 static const struct scmi_power_proto_ops power_proto_ops = {
0203 .num_domains_get = scmi_power_num_domains_get,
0204 .name_get = scmi_power_name_get,
0205 .state_set = scmi_power_state_set,
0206 .state_get = scmi_power_state_get,
0207 };
0208
0209 static int scmi_power_request_notify(const struct scmi_protocol_handle *ph,
0210 u32 domain, bool enable)
0211 {
0212 int ret;
0213 struct scmi_xfer *t;
0214 struct scmi_power_state_notify *notify;
0215
0216 ret = ph->xops->xfer_get_init(ph, POWER_STATE_NOTIFY,
0217 sizeof(*notify), 0, &t);
0218 if (ret)
0219 return ret;
0220
0221 notify = t->tx.buf;
0222 notify->domain = cpu_to_le32(domain);
0223 notify->notify_enable = enable ? cpu_to_le32(BIT(0)) : 0;
0224
0225 ret = ph->xops->do_xfer(ph, t);
0226
0227 ph->xops->xfer_put(ph, t);
0228 return ret;
0229 }
0230
0231 static int scmi_power_set_notify_enabled(const struct scmi_protocol_handle *ph,
0232 u8 evt_id, u32 src_id, bool enable)
0233 {
0234 int ret;
0235
0236 ret = scmi_power_request_notify(ph, src_id, enable);
0237 if (ret)
0238 pr_debug("FAIL_ENABLE - evt[%X] dom[%d] - ret:%d\n",
0239 evt_id, src_id, ret);
0240
0241 return ret;
0242 }
0243
0244 static void *
0245 scmi_power_fill_custom_report(const struct scmi_protocol_handle *ph,
0246 u8 evt_id, ktime_t timestamp,
0247 const void *payld, size_t payld_sz,
0248 void *report, u32 *src_id)
0249 {
0250 const struct scmi_power_state_notify_payld *p = payld;
0251 struct scmi_power_state_changed_report *r = report;
0252
0253 if (evt_id != SCMI_EVENT_POWER_STATE_CHANGED || sizeof(*p) != payld_sz)
0254 return NULL;
0255
0256 r->timestamp = timestamp;
0257 r->agent_id = le32_to_cpu(p->agent_id);
0258 r->domain_id = le32_to_cpu(p->domain_id);
0259 r->power_state = le32_to_cpu(p->power_state);
0260 *src_id = r->domain_id;
0261
0262 return r;
0263 }
0264
0265 static int scmi_power_get_num_sources(const struct scmi_protocol_handle *ph)
0266 {
0267 struct scmi_power_info *pinfo = ph->get_priv(ph);
0268
0269 if (!pinfo)
0270 return -EINVAL;
0271
0272 return pinfo->num_domains;
0273 }
0274
0275 static const struct scmi_event power_events[] = {
0276 {
0277 .id = SCMI_EVENT_POWER_STATE_CHANGED,
0278 .max_payld_sz = sizeof(struct scmi_power_state_notify_payld),
0279 .max_report_sz =
0280 sizeof(struct scmi_power_state_changed_report),
0281 },
0282 };
0283
0284 static const struct scmi_event_ops power_event_ops = {
0285 .get_num_sources = scmi_power_get_num_sources,
0286 .set_notify_enabled = scmi_power_set_notify_enabled,
0287 .fill_custom_report = scmi_power_fill_custom_report,
0288 };
0289
0290 static const struct scmi_protocol_events power_protocol_events = {
0291 .queue_sz = SCMI_PROTO_QUEUE_SZ,
0292 .ops = &power_event_ops,
0293 .evts = power_events,
0294 .num_events = ARRAY_SIZE(power_events),
0295 };
0296
0297 static int scmi_power_protocol_init(const struct scmi_protocol_handle *ph)
0298 {
0299 int domain, ret;
0300 u32 version;
0301 struct scmi_power_info *pinfo;
0302
0303 ret = ph->xops->version_get(ph, &version);
0304 if (ret)
0305 return ret;
0306
0307 dev_dbg(ph->dev, "Power Version %d.%d\n",
0308 PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
0309
0310 pinfo = devm_kzalloc(ph->dev, sizeof(*pinfo), GFP_KERNEL);
0311 if (!pinfo)
0312 return -ENOMEM;
0313
0314 ret = scmi_power_attributes_get(ph, pinfo);
0315 if (ret)
0316 return ret;
0317
0318 pinfo->dom_info = devm_kcalloc(ph->dev, pinfo->num_domains,
0319 sizeof(*pinfo->dom_info), GFP_KERNEL);
0320 if (!pinfo->dom_info)
0321 return -ENOMEM;
0322
0323 for (domain = 0; domain < pinfo->num_domains; domain++) {
0324 struct power_dom_info *dom = pinfo->dom_info + domain;
0325
0326 scmi_power_domain_attributes_get(ph, domain, dom, version);
0327 }
0328
0329 pinfo->version = version;
0330
0331 return ph->set_priv(ph, pinfo);
0332 }
0333
0334 static const struct scmi_protocol scmi_power = {
0335 .id = SCMI_PROTOCOL_POWER,
0336 .owner = THIS_MODULE,
0337 .instance_init = &scmi_power_protocol_init,
0338 .ops = &power_proto_ops,
0339 .events = &power_protocol_events,
0340 };
0341
0342 DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(power, scmi_power)