1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * System Control and Management Interface (SCMI) Power Protocol 4 * 5 * Copyright (C) 2018-2022 ARM Ltd. 6 */ 7 8 #define pr_fmt(fmt) "SCMI Notifications POWER - " fmt 9 10 #include <linux/module.h> 11 #include <linux/scmi_protocol.h> 12 13 #include "protocols.h" 14 #include "notify.h" 15 16 /* Updated only after ALL the mandatory features for that version are merged */ 17 #define SCMI_PROTOCOL_SUPPORTED_VERSION 0x30000 18 19 enum scmi_power_protocol_cmd { 20 POWER_DOMAIN_ATTRIBUTES = 0x3, 21 POWER_STATE_SET = 0x4, 22 POWER_STATE_GET = 0x5, 23 POWER_STATE_NOTIFY = 0x6, 24 POWER_DOMAIN_NAME_GET = 0x8, 25 }; 26 27 struct scmi_msg_resp_power_attributes { 28 __le16 num_domains; 29 __le16 reserved; 30 __le32 stats_addr_low; 31 __le32 stats_addr_high; 32 __le32 stats_size; 33 }; 34 35 struct scmi_msg_resp_power_domain_attributes { 36 __le32 flags; 37 #define SUPPORTS_STATE_SET_NOTIFY(x) ((x) & BIT(31)) 38 #define SUPPORTS_STATE_SET_ASYNC(x) ((x) & BIT(30)) 39 #define SUPPORTS_STATE_SET_SYNC(x) ((x) & BIT(29)) 40 #define SUPPORTS_EXTENDED_NAMES(x) ((x) & BIT(27)) 41 u8 name[SCMI_SHORT_NAME_MAX_SIZE]; 42 }; 43 44 struct scmi_power_set_state { 45 __le32 flags; 46 #define STATE_SET_ASYNC BIT(0) 47 __le32 domain; 48 __le32 state; 49 }; 50 51 struct scmi_power_state_notify { 52 __le32 domain; 53 __le32 notify_enable; 54 }; 55 56 struct scmi_power_state_notify_payld { 57 __le32 agent_id; 58 __le32 domain_id; 59 __le32 power_state; 60 }; 61 62 struct power_dom_info { 63 bool state_set_sync; 64 bool state_set_async; 65 bool state_set_notify; 66 char name[SCMI_MAX_STR_SIZE]; 67 }; 68 69 struct scmi_power_info { 70 u32 version; 71 int num_domains; 72 u64 stats_addr; 73 u32 stats_size; 74 struct power_dom_info *dom_info; 75 }; 76 77 static int scmi_power_attributes_get(const struct scmi_protocol_handle *ph, 78 struct scmi_power_info *pi) 79 { 80 int ret; 81 struct scmi_xfer *t; 82 struct scmi_msg_resp_power_attributes *attr; 83 84 ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES, 85 0, sizeof(*attr), &t); 86 if (ret) 87 return ret; 88 89 attr = t->rx.buf; 90 91 ret = ph->xops->do_xfer(ph, t); 92 if (!ret) { 93 pi->num_domains = le16_to_cpu(attr->num_domains); 94 pi->stats_addr = le32_to_cpu(attr->stats_addr_low) | 95 (u64)le32_to_cpu(attr->stats_addr_high) << 32; 96 pi->stats_size = le32_to_cpu(attr->stats_size); 97 } 98 99 ph->xops->xfer_put(ph, t); 100 return ret; 101 } 102 103 static int 104 scmi_power_domain_attributes_get(const struct scmi_protocol_handle *ph, 105 u32 domain, struct power_dom_info *dom_info, 106 u32 version) 107 { 108 int ret; 109 u32 flags; 110 struct scmi_xfer *t; 111 struct scmi_msg_resp_power_domain_attributes *attr; 112 113 ret = ph->xops->xfer_get_init(ph, POWER_DOMAIN_ATTRIBUTES, 114 sizeof(domain), sizeof(*attr), &t); 115 if (ret) 116 return ret; 117 118 put_unaligned_le32(domain, t->tx.buf); 119 attr = t->rx.buf; 120 121 ret = ph->xops->do_xfer(ph, t); 122 if (!ret) { 123 flags = le32_to_cpu(attr->flags); 124 125 dom_info->state_set_notify = SUPPORTS_STATE_SET_NOTIFY(flags); 126 dom_info->state_set_async = SUPPORTS_STATE_SET_ASYNC(flags); 127 dom_info->state_set_sync = SUPPORTS_STATE_SET_SYNC(flags); 128 strscpy(dom_info->name, attr->name, SCMI_SHORT_NAME_MAX_SIZE); 129 } 130 ph->xops->xfer_put(ph, t); 131 132 /* 133 * If supported overwrite short name with the extended one; 134 * on error just carry on and use already provided short name. 135 */ 136 if (!ret && PROTOCOL_REV_MAJOR(version) >= 0x3 && 137 SUPPORTS_EXTENDED_NAMES(flags)) { 138 ph->hops->extended_name_get(ph, POWER_DOMAIN_NAME_GET, 139 domain, NULL, dom_info->name, 140 SCMI_MAX_STR_SIZE); 141 } 142 143 return ret; 144 } 145 146 static int scmi_power_state_set(const struct scmi_protocol_handle *ph, 147 u32 domain, u32 state) 148 { 149 int ret; 150 struct scmi_xfer *t; 151 struct scmi_power_set_state *st; 152 153 ret = ph->xops->xfer_get_init(ph, POWER_STATE_SET, sizeof(*st), 0, &t); 154 if (ret) 155 return ret; 156 157 st = t->tx.buf; 158 st->flags = cpu_to_le32(0); 159 st->domain = cpu_to_le32(domain); 160 st->state = cpu_to_le32(state); 161 162 ret = ph->xops->do_xfer(ph, t); 163 164 ph->xops->xfer_put(ph, t); 165 return ret; 166 } 167 168 static int scmi_power_state_get(const struct scmi_protocol_handle *ph, 169 u32 domain, u32 *state) 170 { 171 int ret; 172 struct scmi_xfer *t; 173 174 ret = ph->xops->xfer_get_init(ph, POWER_STATE_GET, sizeof(u32), sizeof(u32), &t); 175 if (ret) 176 return ret; 177 178 put_unaligned_le32(domain, t->tx.buf); 179 180 ret = ph->xops->do_xfer(ph, t); 181 if (!ret) 182 *state = get_unaligned_le32(t->rx.buf); 183 184 ph->xops->xfer_put(ph, t); 185 return ret; 186 } 187 188 static int scmi_power_num_domains_get(const struct scmi_protocol_handle *ph) 189 { 190 struct scmi_power_info *pi = ph->get_priv(ph); 191 192 return pi->num_domains; 193 } 194 195 static const char * 196 scmi_power_name_get(const struct scmi_protocol_handle *ph, 197 u32 domain) 198 { 199 struct scmi_power_info *pi = ph->get_priv(ph); 200 struct power_dom_info *dom = pi->dom_info + domain; 201 202 return dom->name; 203 } 204 205 static const struct scmi_power_proto_ops power_proto_ops = { 206 .num_domains_get = scmi_power_num_domains_get, 207 .name_get = scmi_power_name_get, 208 .state_set = scmi_power_state_set, 209 .state_get = scmi_power_state_get, 210 }; 211 212 static int scmi_power_request_notify(const struct scmi_protocol_handle *ph, 213 u32 domain, bool enable) 214 { 215 int ret; 216 struct scmi_xfer *t; 217 struct scmi_power_state_notify *notify; 218 219 ret = ph->xops->xfer_get_init(ph, POWER_STATE_NOTIFY, 220 sizeof(*notify), 0, &t); 221 if (ret) 222 return ret; 223 224 notify = t->tx.buf; 225 notify->domain = cpu_to_le32(domain); 226 notify->notify_enable = enable ? cpu_to_le32(BIT(0)) : 0; 227 228 ret = ph->xops->do_xfer(ph, t); 229 230 ph->xops->xfer_put(ph, t); 231 return ret; 232 } 233 234 static int scmi_power_set_notify_enabled(const struct scmi_protocol_handle *ph, 235 u8 evt_id, u32 src_id, bool enable) 236 { 237 int ret; 238 239 ret = scmi_power_request_notify(ph, src_id, enable); 240 if (ret) 241 pr_debug("FAIL_ENABLE - evt[%X] dom[%d] - ret:%d\n", 242 evt_id, src_id, ret); 243 244 return ret; 245 } 246 247 static void * 248 scmi_power_fill_custom_report(const struct scmi_protocol_handle *ph, 249 u8 evt_id, ktime_t timestamp, 250 const void *payld, size_t payld_sz, 251 void *report, u32 *src_id) 252 { 253 const struct scmi_power_state_notify_payld *p = payld; 254 struct scmi_power_state_changed_report *r = report; 255 256 if (evt_id != SCMI_EVENT_POWER_STATE_CHANGED || sizeof(*p) != payld_sz) 257 return NULL; 258 259 r->timestamp = timestamp; 260 r->agent_id = le32_to_cpu(p->agent_id); 261 r->domain_id = le32_to_cpu(p->domain_id); 262 r->power_state = le32_to_cpu(p->power_state); 263 *src_id = r->domain_id; 264 265 return r; 266 } 267 268 static int scmi_power_get_num_sources(const struct scmi_protocol_handle *ph) 269 { 270 struct scmi_power_info *pinfo = ph->get_priv(ph); 271 272 if (!pinfo) 273 return -EINVAL; 274 275 return pinfo->num_domains; 276 } 277 278 static const struct scmi_event power_events[] = { 279 { 280 .id = SCMI_EVENT_POWER_STATE_CHANGED, 281 .max_payld_sz = sizeof(struct scmi_power_state_notify_payld), 282 .max_report_sz = 283 sizeof(struct scmi_power_state_changed_report), 284 }, 285 }; 286 287 static const struct scmi_event_ops power_event_ops = { 288 .get_num_sources = scmi_power_get_num_sources, 289 .set_notify_enabled = scmi_power_set_notify_enabled, 290 .fill_custom_report = scmi_power_fill_custom_report, 291 }; 292 293 static const struct scmi_protocol_events power_protocol_events = { 294 .queue_sz = SCMI_PROTO_QUEUE_SZ, 295 .ops = &power_event_ops, 296 .evts = power_events, 297 .num_events = ARRAY_SIZE(power_events), 298 }; 299 300 static int scmi_power_protocol_init(const struct scmi_protocol_handle *ph) 301 { 302 int domain, ret; 303 u32 version; 304 struct scmi_power_info *pinfo; 305 306 ret = ph->xops->version_get(ph, &version); 307 if (ret) 308 return ret; 309 310 dev_dbg(ph->dev, "Power Version %d.%d\n", 311 PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version)); 312 313 pinfo = devm_kzalloc(ph->dev, sizeof(*pinfo), GFP_KERNEL); 314 if (!pinfo) 315 return -ENOMEM; 316 317 ret = scmi_power_attributes_get(ph, pinfo); 318 if (ret) 319 return ret; 320 321 pinfo->dom_info = devm_kcalloc(ph->dev, pinfo->num_domains, 322 sizeof(*pinfo->dom_info), GFP_KERNEL); 323 if (!pinfo->dom_info) 324 return -ENOMEM; 325 326 for (domain = 0; domain < pinfo->num_domains; domain++) { 327 struct power_dom_info *dom = pinfo->dom_info + domain; 328 329 scmi_power_domain_attributes_get(ph, domain, dom, version); 330 } 331 332 pinfo->version = version; 333 334 return ph->set_priv(ph, pinfo, version); 335 } 336 337 static const struct scmi_protocol scmi_power = { 338 .id = SCMI_PROTOCOL_POWER, 339 .owner = THIS_MODULE, 340 .instance_init = &scmi_power_protocol_init, 341 .ops = &power_proto_ops, 342 .events = &power_protocol_events, 343 .supported_version = SCMI_PROTOCOL_SUPPORTED_VERSION, 344 }; 345 346 DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(power, scmi_power) 347