1 // SPDX-License-Identifier: GPL-2.0
3 * System Control and Management Interface (SCMI) Powercap Protocol
5 * Copyright (C) 2022 ARM Ltd.
8 #define pr_fmt(fmt) "SCMI Notifications POWERCAP - " fmt
10 #include <linux/bitfield.h>
12 #include <linux/module.h>
13 #include <linux/scmi_protocol.h>
15 #include <trace/events/scmi.h>
17 #include "protocols.h"
20 enum scmi_powercap_protocol_cmd {
21 POWERCAP_DOMAIN_ATTRIBUTES = 0x3,
22 POWERCAP_CAP_GET = 0x4,
23 POWERCAP_CAP_SET = 0x5,
24 POWERCAP_PAI_GET = 0x6,
25 POWERCAP_PAI_SET = 0x7,
26 POWERCAP_DOMAIN_NAME_GET = 0x8,
27 POWERCAP_MEASUREMENTS_GET = 0x9,
28 POWERCAP_CAP_NOTIFY = 0xa,
29 POWERCAP_MEASUREMENTS_NOTIFY = 0xb,
30 POWERCAP_DESCRIBE_FASTCHANNEL = 0xc,
39 struct scmi_msg_resp_powercap_domain_attributes {
41 #define SUPPORTS_POWERCAP_CAP_CHANGE_NOTIFY(x) ((x) & BIT(31))
42 #define SUPPORTS_POWERCAP_MEASUREMENTS_CHANGE_NOTIFY(x) ((x) & BIT(30))
43 #define SUPPORTS_ASYNC_POWERCAP_CAP_SET(x) ((x) & BIT(29))
44 #define SUPPORTS_EXTENDED_NAMES(x) ((x) & BIT(28))
45 #define SUPPORTS_POWERCAP_CAP_CONFIGURATION(x) ((x) & BIT(27))
46 #define SUPPORTS_POWERCAP_MONITORING(x) ((x) & BIT(26))
47 #define SUPPORTS_POWERCAP_PAI_CONFIGURATION(x) ((x) & BIT(25))
48 #define SUPPORTS_POWERCAP_FASTCHANNELS(x) ((x) & BIT(22))
49 #define POWERCAP_POWER_UNIT(x) \
50 (FIELD_GET(GENMASK(24, 23), (x)))
51 #define SUPPORTS_POWER_UNITS_MW(x) \
52 (POWERCAP_POWER_UNIT(x) == 0x2)
53 #define SUPPORTS_POWER_UNITS_UW(x) \
54 (POWERCAP_POWER_UNIT(x) == 0x1)
55 u8 name[SCMI_SHORT_NAME_MAX_SIZE];
61 __le32 power_cap_step;
62 __le32 sustainable_power;
67 struct scmi_msg_powercap_set_cap_or_pai {
70 #define CAP_SET_ASYNC BIT(1)
71 #define CAP_SET_IGNORE_DRESP BIT(0)
75 struct scmi_msg_resp_powercap_cap_set_complete {
80 struct scmi_msg_resp_powercap_meas_get {
85 struct scmi_msg_powercap_notify_cap {
90 struct scmi_msg_powercap_notify_thresh {
93 __le32 power_thresh_low;
94 __le32 power_thresh_high;
97 struct scmi_powercap_cap_changed_notify_payld {
104 struct scmi_powercap_meas_changed_notify_payld {
110 struct scmi_powercap_state {
113 bool meas_notif_enabled;
115 #define THRESH_LOW(p, id) \
116 (lower_32_bits((p)->states[(id)].thresholds))
117 #define THRESH_HIGH(p, id) \
118 (upper_32_bits((p)->states[(id)].thresholds))
121 struct powercap_info {
124 struct scmi_powercap_state *states;
125 struct scmi_powercap_info *powercaps;
128 static enum scmi_powercap_protocol_cmd evt_2_cmd[] = {
130 POWERCAP_MEASUREMENTS_NOTIFY,
133 static int scmi_powercap_notify(const struct scmi_protocol_handle *ph,
134 u32 domain, int message_id, bool enable);
137 scmi_powercap_attributes_get(const struct scmi_protocol_handle *ph,
138 struct powercap_info *pi)
143 ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES, 0,
148 ret = ph->xops->do_xfer(ph, t);
152 attributes = get_unaligned_le32(t->rx.buf);
153 pi->num_domains = FIELD_GET(GENMASK(15, 0), attributes);
156 ph->xops->xfer_put(ph, t);
161 scmi_powercap_validate(unsigned int min_val, unsigned int max_val,
162 unsigned int step_val, bool configurable)
164 if (!min_val || !max_val)
167 if ((configurable && min_val == max_val) ||
168 (!configurable && min_val != max_val))
171 if (min_val != max_val && !step_val)
178 scmi_powercap_domain_attributes_get(const struct scmi_protocol_handle *ph,
179 struct powercap_info *pinfo, u32 domain)
184 struct scmi_powercap_info *dom_info = pinfo->powercaps + domain;
185 struct scmi_msg_resp_powercap_domain_attributes *resp;
187 ret = ph->xops->xfer_get_init(ph, POWERCAP_DOMAIN_ATTRIBUTES,
188 sizeof(domain), sizeof(*resp), &t);
192 put_unaligned_le32(domain, t->tx.buf);
195 ret = ph->xops->do_xfer(ph, t);
197 flags = le32_to_cpu(resp->attributes);
199 dom_info->id = domain;
200 dom_info->notify_powercap_cap_change =
201 SUPPORTS_POWERCAP_CAP_CHANGE_NOTIFY(flags);
202 dom_info->notify_powercap_measurement_change =
203 SUPPORTS_POWERCAP_MEASUREMENTS_CHANGE_NOTIFY(flags);
204 dom_info->async_powercap_cap_set =
205 SUPPORTS_ASYNC_POWERCAP_CAP_SET(flags);
206 dom_info->powercap_cap_config =
207 SUPPORTS_POWERCAP_CAP_CONFIGURATION(flags);
208 dom_info->powercap_monitoring =
209 SUPPORTS_POWERCAP_MONITORING(flags);
210 dom_info->powercap_pai_config =
211 SUPPORTS_POWERCAP_PAI_CONFIGURATION(flags);
212 dom_info->powercap_scale_mw =
213 SUPPORTS_POWER_UNITS_MW(flags);
214 dom_info->powercap_scale_uw =
215 SUPPORTS_POWER_UNITS_UW(flags);
216 dom_info->fastchannels =
217 SUPPORTS_POWERCAP_FASTCHANNELS(flags);
219 strscpy(dom_info->name, resp->name, SCMI_SHORT_NAME_MAX_SIZE);
221 dom_info->min_pai = le32_to_cpu(resp->min_pai);
222 dom_info->max_pai = le32_to_cpu(resp->max_pai);
223 dom_info->pai_step = le32_to_cpu(resp->pai_step);
224 ret = scmi_powercap_validate(dom_info->min_pai,
227 dom_info->powercap_pai_config);
230 "Platform reported inconsistent PAI config for domain %d - %s\n",
231 dom_info->id, dom_info->name);
235 dom_info->min_power_cap = le32_to_cpu(resp->min_power_cap);
236 dom_info->max_power_cap = le32_to_cpu(resp->max_power_cap);
237 dom_info->power_cap_step = le32_to_cpu(resp->power_cap_step);
238 ret = scmi_powercap_validate(dom_info->min_power_cap,
239 dom_info->max_power_cap,
240 dom_info->power_cap_step,
241 dom_info->powercap_cap_config);
244 "Platform reported inconsistent CAP config for domain %d - %s\n",
245 dom_info->id, dom_info->name);
249 dom_info->sustainable_power =
250 le32_to_cpu(resp->sustainable_power);
251 dom_info->accuracy = le32_to_cpu(resp->accuracy);
253 dom_info->parent_id = le32_to_cpu(resp->parent_id);
254 if (dom_info->parent_id != SCMI_POWERCAP_ROOT_ZONE_ID &&
255 (dom_info->parent_id >= pinfo->num_domains ||
256 dom_info->parent_id == dom_info->id)) {
258 "Platform reported inconsistent parent ID for domain %d - %s\n",
259 dom_info->id, dom_info->name);
265 ph->xops->xfer_put(ph, t);
268 * If supported overwrite short name with the extended one;
269 * on error just carry on and use already provided short name.
271 if (!ret && SUPPORTS_EXTENDED_NAMES(flags))
272 ph->hops->extended_name_get(ph, POWERCAP_DOMAIN_NAME_GET,
273 domain, dom_info->name,
279 static int scmi_powercap_num_domains_get(const struct scmi_protocol_handle *ph)
281 struct powercap_info *pi = ph->get_priv(ph);
283 return pi->num_domains;
286 static const struct scmi_powercap_info *
287 scmi_powercap_dom_info_get(const struct scmi_protocol_handle *ph, u32 domain_id)
289 struct powercap_info *pi = ph->get_priv(ph);
291 if (domain_id >= pi->num_domains)
294 return pi->powercaps + domain_id;
297 static int scmi_powercap_xfer_cap_get(const struct scmi_protocol_handle *ph,
298 u32 domain_id, u32 *power_cap)
303 ret = ph->xops->xfer_get_init(ph, POWERCAP_CAP_GET, sizeof(u32),
308 put_unaligned_le32(domain_id, t->tx.buf);
309 ret = ph->xops->do_xfer(ph, t);
311 *power_cap = get_unaligned_le32(t->rx.buf);
313 ph->xops->xfer_put(ph, t);
318 static int __scmi_powercap_cap_get(const struct scmi_protocol_handle *ph,
319 const struct scmi_powercap_info *dom,
322 if (dom->fc_info && dom->fc_info[POWERCAP_FC_CAP].get_addr) {
323 *power_cap = ioread32(dom->fc_info[POWERCAP_FC_CAP].get_addr);
324 trace_scmi_fc_call(SCMI_PROTOCOL_POWERCAP, POWERCAP_CAP_GET,
325 dom->id, *power_cap, 0);
329 return scmi_powercap_xfer_cap_get(ph, dom->id, power_cap);
332 static int scmi_powercap_cap_get(const struct scmi_protocol_handle *ph,
333 u32 domain_id, u32 *power_cap)
335 const struct scmi_powercap_info *dom;
340 dom = scmi_powercap_dom_info_get(ph, domain_id);
344 return __scmi_powercap_cap_get(ph, dom, power_cap);
347 static int scmi_powercap_xfer_cap_set(const struct scmi_protocol_handle *ph,
348 const struct scmi_powercap_info *pc,
349 u32 power_cap, bool ignore_dresp)
353 struct scmi_msg_powercap_set_cap_or_pai *msg;
355 ret = ph->xops->xfer_get_init(ph, POWERCAP_CAP_SET,
356 sizeof(*msg), 0, &t);
361 msg->domain = cpu_to_le32(pc->id);
363 cpu_to_le32(FIELD_PREP(CAP_SET_ASYNC, !!pc->async_powercap_cap_set) |
364 FIELD_PREP(CAP_SET_IGNORE_DRESP, !!ignore_dresp));
365 msg->value = cpu_to_le32(power_cap);
367 if (!pc->async_powercap_cap_set || ignore_dresp) {
368 ret = ph->xops->do_xfer(ph, t);
370 ret = ph->xops->do_xfer_with_response(ph, t);
372 struct scmi_msg_resp_powercap_cap_set_complete *resp;
375 if (le32_to_cpu(resp->domain) == pc->id)
377 "Powercap ID %d CAP set async to %u\n",
379 get_unaligned_le32(&resp->power_cap));
385 ph->xops->xfer_put(ph, t);
389 static int __scmi_powercap_cap_set(const struct scmi_protocol_handle *ph,
390 struct powercap_info *pi, u32 domain_id,
391 u32 power_cap, bool ignore_dresp)
394 const struct scmi_powercap_info *pc;
396 pc = scmi_powercap_dom_info_get(ph, domain_id);
397 if (!pc || !pc->powercap_cap_config)
401 (power_cap < pc->min_power_cap || power_cap > pc->max_power_cap))
404 if (pc->fc_info && pc->fc_info[POWERCAP_FC_CAP].set_addr) {
405 struct scmi_fc_info *fci = &pc->fc_info[POWERCAP_FC_CAP];
407 iowrite32(power_cap, fci->set_addr);
408 ph->hops->fastchannel_db_ring(fci->set_db);
409 trace_scmi_fc_call(SCMI_PROTOCOL_POWERCAP, POWERCAP_CAP_SET,
410 domain_id, power_cap, 0);
413 ret = scmi_powercap_xfer_cap_set(ph, pc, power_cap,
417 /* Save the last explicitly set non-zero powercap value */
418 if (PROTOCOL_REV_MAJOR(pi->version) >= 0x2 && !ret && power_cap)
419 pi->states[domain_id].last_pcap = power_cap;
424 static int scmi_powercap_cap_set(const struct scmi_protocol_handle *ph,
425 u32 domain_id, u32 power_cap,
428 struct powercap_info *pi = ph->get_priv(ph);
431 * Disallow zero as a possible explicitly requested powercap:
432 * there are enable/disable operations for this.
437 /* Just log the last set request if acting on a disabled domain */
438 if (PROTOCOL_REV_MAJOR(pi->version) >= 0x2 &&
439 !pi->states[domain_id].enabled) {
440 pi->states[domain_id].last_pcap = power_cap;
444 return __scmi_powercap_cap_set(ph, pi, domain_id,
445 power_cap, ignore_dresp);
448 static int scmi_powercap_xfer_pai_get(const struct scmi_protocol_handle *ph,
449 u32 domain_id, u32 *pai)
454 ret = ph->xops->xfer_get_init(ph, POWERCAP_PAI_GET, sizeof(u32),
459 put_unaligned_le32(domain_id, t->tx.buf);
460 ret = ph->xops->do_xfer(ph, t);
462 *pai = get_unaligned_le32(t->rx.buf);
464 ph->xops->xfer_put(ph, t);
469 static int scmi_powercap_pai_get(const struct scmi_protocol_handle *ph,
470 u32 domain_id, u32 *pai)
472 struct scmi_powercap_info *dom;
473 struct powercap_info *pi = ph->get_priv(ph);
475 if (!pai || domain_id >= pi->num_domains)
478 dom = pi->powercaps + domain_id;
479 if (dom->fc_info && dom->fc_info[POWERCAP_FC_PAI].get_addr) {
480 *pai = ioread32(dom->fc_info[POWERCAP_FC_PAI].get_addr);
481 trace_scmi_fc_call(SCMI_PROTOCOL_POWERCAP, POWERCAP_PAI_GET,
486 return scmi_powercap_xfer_pai_get(ph, domain_id, pai);
489 static int scmi_powercap_xfer_pai_set(const struct scmi_protocol_handle *ph,
490 u32 domain_id, u32 pai)
494 struct scmi_msg_powercap_set_cap_or_pai *msg;
496 ret = ph->xops->xfer_get_init(ph, POWERCAP_PAI_SET,
497 sizeof(*msg), 0, &t);
502 msg->domain = cpu_to_le32(domain_id);
503 msg->flags = cpu_to_le32(0);
504 msg->value = cpu_to_le32(pai);
506 ret = ph->xops->do_xfer(ph, t);
508 ph->xops->xfer_put(ph, t);
512 static int scmi_powercap_pai_set(const struct scmi_protocol_handle *ph,
513 u32 domain_id, u32 pai)
515 const struct scmi_powercap_info *pc;
517 pc = scmi_powercap_dom_info_get(ph, domain_id);
518 if (!pc || !pc->powercap_pai_config || !pai ||
519 pai < pc->min_pai || pai > pc->max_pai)
522 if (pc->fc_info && pc->fc_info[POWERCAP_FC_PAI].set_addr) {
523 struct scmi_fc_info *fci = &pc->fc_info[POWERCAP_FC_PAI];
525 trace_scmi_fc_call(SCMI_PROTOCOL_POWERCAP, POWERCAP_PAI_SET,
527 iowrite32(pai, fci->set_addr);
528 ph->hops->fastchannel_db_ring(fci->set_db);
532 return scmi_powercap_xfer_pai_set(ph, domain_id, pai);
535 static int scmi_powercap_measurements_get(const struct scmi_protocol_handle *ph,
536 u32 domain_id, u32 *average_power,
541 struct scmi_msg_resp_powercap_meas_get *resp;
542 const struct scmi_powercap_info *pc;
544 pc = scmi_powercap_dom_info_get(ph, domain_id);
545 if (!pc || !pc->powercap_monitoring || !pai || !average_power)
548 ret = ph->xops->xfer_get_init(ph, POWERCAP_MEASUREMENTS_GET,
549 sizeof(u32), sizeof(*resp), &t);
554 put_unaligned_le32(domain_id, t->tx.buf);
555 ret = ph->xops->do_xfer(ph, t);
557 *average_power = le32_to_cpu(resp->power);
558 *pai = le32_to_cpu(resp->pai);
561 ph->xops->xfer_put(ph, t);
566 scmi_powercap_measurements_threshold_get(const struct scmi_protocol_handle *ph,
567 u32 domain_id, u32 *power_thresh_low,
568 u32 *power_thresh_high)
570 struct powercap_info *pi = ph->get_priv(ph);
572 if (!power_thresh_low || !power_thresh_high ||
573 domain_id >= pi->num_domains)
576 *power_thresh_low = THRESH_LOW(pi, domain_id);
577 *power_thresh_high = THRESH_HIGH(pi, domain_id);
583 scmi_powercap_measurements_threshold_set(const struct scmi_protocol_handle *ph,
584 u32 domain_id, u32 power_thresh_low,
585 u32 power_thresh_high)
588 struct powercap_info *pi = ph->get_priv(ph);
590 if (domain_id >= pi->num_domains ||
591 power_thresh_low > power_thresh_high)
594 /* Anything to do ? */
595 if (THRESH_LOW(pi, domain_id) == power_thresh_low &&
596 THRESH_HIGH(pi, domain_id) == power_thresh_high)
599 pi->states[domain_id].thresholds =
600 (FIELD_PREP(GENMASK_ULL(31, 0), power_thresh_low) |
601 FIELD_PREP(GENMASK_ULL(63, 32), power_thresh_high));
603 /* Update thresholds if notification already enabled */
604 if (pi->states[domain_id].meas_notif_enabled)
605 ret = scmi_powercap_notify(ph, domain_id,
606 POWERCAP_MEASUREMENTS_NOTIFY,
612 static int scmi_powercap_cap_enable_set(const struct scmi_protocol_handle *ph,
613 u32 domain_id, bool enable)
617 struct powercap_info *pi = ph->get_priv(ph);
619 if (PROTOCOL_REV_MAJOR(pi->version) < 0x2)
622 if (enable == pi->states[domain_id].enabled)
626 /* Cannot enable with a zero powercap. */
627 if (!pi->states[domain_id].last_pcap)
630 ret = __scmi_powercap_cap_set(ph, pi, domain_id,
631 pi->states[domain_id].last_pcap,
634 ret = __scmi_powercap_cap_set(ph, pi, domain_id, 0, true);
641 * Update our internal state to reflect final platform state: the SCMI
642 * server could have ignored a disable request and kept enforcing some
643 * powercap limit requested by other agents.
645 ret = scmi_powercap_cap_get(ph, domain_id, &power_cap);
647 pi->states[domain_id].enabled = !!power_cap;
652 static int scmi_powercap_cap_enable_get(const struct scmi_protocol_handle *ph,
653 u32 domain_id, bool *enable)
657 struct powercap_info *pi = ph->get_priv(ph);
660 if (PROTOCOL_REV_MAJOR(pi->version) < 0x2)
664 * Report always real platform state; platform could have ignored
665 * a previous disable request. Default true on any error.
667 ret = scmi_powercap_cap_get(ph, domain_id, &power_cap);
669 *enable = !!power_cap;
671 /* Update internal state with current real platform state */
672 pi->states[domain_id].enabled = *enable;
677 static const struct scmi_powercap_proto_ops powercap_proto_ops = {
678 .num_domains_get = scmi_powercap_num_domains_get,
679 .info_get = scmi_powercap_dom_info_get,
680 .cap_get = scmi_powercap_cap_get,
681 .cap_set = scmi_powercap_cap_set,
682 .cap_enable_set = scmi_powercap_cap_enable_set,
683 .cap_enable_get = scmi_powercap_cap_enable_get,
684 .pai_get = scmi_powercap_pai_get,
685 .pai_set = scmi_powercap_pai_set,
686 .measurements_get = scmi_powercap_measurements_get,
687 .measurements_threshold_set = scmi_powercap_measurements_threshold_set,
688 .measurements_threshold_get = scmi_powercap_measurements_threshold_get,
691 static void scmi_powercap_domain_init_fc(const struct scmi_protocol_handle *ph,
692 u32 domain, struct scmi_fc_info **p_fc)
694 struct scmi_fc_info *fc;
696 fc = devm_kcalloc(ph->dev, POWERCAP_FC_MAX, sizeof(*fc), GFP_KERNEL);
700 ph->hops->fastchannel_init(ph, POWERCAP_DESCRIBE_FASTCHANNEL,
701 POWERCAP_CAP_SET, 4, domain,
702 &fc[POWERCAP_FC_CAP].set_addr,
703 &fc[POWERCAP_FC_CAP].set_db);
705 ph->hops->fastchannel_init(ph, POWERCAP_DESCRIBE_FASTCHANNEL,
706 POWERCAP_CAP_GET, 4, domain,
707 &fc[POWERCAP_FC_CAP].get_addr, NULL);
709 ph->hops->fastchannel_init(ph, POWERCAP_DESCRIBE_FASTCHANNEL,
710 POWERCAP_PAI_SET, 4, domain,
711 &fc[POWERCAP_FC_PAI].set_addr,
712 &fc[POWERCAP_FC_PAI].set_db);
714 ph->hops->fastchannel_init(ph, POWERCAP_DESCRIBE_FASTCHANNEL,
715 POWERCAP_PAI_GET, 4, domain,
716 &fc[POWERCAP_FC_PAI].get_addr, NULL);
721 static int scmi_powercap_notify(const struct scmi_protocol_handle *ph,
722 u32 domain, int message_id, bool enable)
727 switch (message_id) {
728 case POWERCAP_CAP_NOTIFY:
730 struct scmi_msg_powercap_notify_cap *notify;
732 ret = ph->xops->xfer_get_init(ph, message_id,
733 sizeof(*notify), 0, &t);
738 notify->domain = cpu_to_le32(domain);
739 notify->notify_enable = cpu_to_le32(enable ? BIT(0) : 0);
742 case POWERCAP_MEASUREMENTS_NOTIFY:
745 struct scmi_msg_powercap_notify_thresh *notify;
748 * Note that we have to pick the most recently configured
749 * thresholds to build a proper POWERCAP_MEASUREMENTS_NOTIFY
750 * enable request and we fail, complaining, if no thresholds
751 * were ever set, since this is an indication the API has been
754 ret = scmi_powercap_measurements_threshold_get(ph, domain,
759 if (enable && !low && !high) {
761 "Invalid Measurements Notify thresholds: %u/%u\n",
766 ret = ph->xops->xfer_get_init(ph, message_id,
767 sizeof(*notify), 0, &t);
772 notify->domain = cpu_to_le32(domain);
773 notify->notify_enable = cpu_to_le32(enable ? BIT(0) : 0);
774 notify->power_thresh_low = cpu_to_le32(low);
775 notify->power_thresh_high = cpu_to_le32(high);
782 ret = ph->xops->do_xfer(ph, t);
784 ph->xops->xfer_put(ph, t);
789 scmi_powercap_set_notify_enabled(const struct scmi_protocol_handle *ph,
790 u8 evt_id, u32 src_id, bool enable)
793 struct powercap_info *pi = ph->get_priv(ph);
795 if (evt_id >= ARRAY_SIZE(evt_2_cmd) || src_id >= pi->num_domains)
798 cmd_id = evt_2_cmd[evt_id];
799 ret = scmi_powercap_notify(ph, src_id, cmd_id, enable);
801 pr_debug("FAIL_ENABLED - evt[%X] dom[%d] - ret:%d\n",
802 evt_id, src_id, ret);
803 else if (cmd_id == POWERCAP_MEASUREMENTS_NOTIFY)
805 * On success save the current notification enabled state, so
806 * as to be able to properly update the notification thresholds
807 * when they are modified on a domain for which measurement
808 * notifications were currently enabled.
810 * This is needed because the SCMI Notification core machinery
811 * and API does not support passing per-notification custom
812 * arguments at callback registration time.
814 * Note that this can be done here with a simple flag since the
815 * SCMI core Notifications code takes care of keeping proper
816 * per-domain enables refcounting, so that this helper function
817 * will be called only once (for enables) when the first user
818 * registers a callback on this domain and once more (disable)
819 * when the last user de-registers its callback.
821 pi->states[src_id].meas_notif_enabled = enable;
827 scmi_powercap_fill_custom_report(const struct scmi_protocol_handle *ph,
828 u8 evt_id, ktime_t timestamp,
829 const void *payld, size_t payld_sz,
830 void *report, u32 *src_id)
835 case SCMI_EVENT_POWERCAP_CAP_CHANGED:
837 const struct scmi_powercap_cap_changed_notify_payld *p = payld;
838 struct scmi_powercap_cap_changed_report *r = report;
840 if (sizeof(*p) != payld_sz)
843 r->timestamp = timestamp;
844 r->agent_id = le32_to_cpu(p->agent_id);
845 r->domain_id = le32_to_cpu(p->domain_id);
846 r->power_cap = le32_to_cpu(p->power_cap);
847 r->pai = le32_to_cpu(p->pai);
848 *src_id = r->domain_id;
852 case SCMI_EVENT_POWERCAP_MEASUREMENTS_CHANGED:
854 const struct scmi_powercap_meas_changed_notify_payld *p = payld;
855 struct scmi_powercap_meas_changed_report *r = report;
857 if (sizeof(*p) != payld_sz)
860 r->timestamp = timestamp;
861 r->agent_id = le32_to_cpu(p->agent_id);
862 r->domain_id = le32_to_cpu(p->domain_id);
863 r->power = le32_to_cpu(p->power);
864 *src_id = r->domain_id;
876 scmi_powercap_get_num_sources(const struct scmi_protocol_handle *ph)
878 struct powercap_info *pi = ph->get_priv(ph);
883 return pi->num_domains;
886 static const struct scmi_event powercap_events[] = {
888 .id = SCMI_EVENT_POWERCAP_CAP_CHANGED,
890 sizeof(struct scmi_powercap_cap_changed_notify_payld),
892 sizeof(struct scmi_powercap_cap_changed_report),
895 .id = SCMI_EVENT_POWERCAP_MEASUREMENTS_CHANGED,
897 sizeof(struct scmi_powercap_meas_changed_notify_payld),
899 sizeof(struct scmi_powercap_meas_changed_report),
903 static const struct scmi_event_ops powercap_event_ops = {
904 .get_num_sources = scmi_powercap_get_num_sources,
905 .set_notify_enabled = scmi_powercap_set_notify_enabled,
906 .fill_custom_report = scmi_powercap_fill_custom_report,
909 static const struct scmi_protocol_events powercap_protocol_events = {
910 .queue_sz = SCMI_PROTO_QUEUE_SZ,
911 .ops = &powercap_event_ops,
912 .evts = powercap_events,
913 .num_events = ARRAY_SIZE(powercap_events),
917 scmi_powercap_protocol_init(const struct scmi_protocol_handle *ph)
921 struct powercap_info *pinfo;
923 ret = ph->xops->version_get(ph, &version);
927 dev_dbg(ph->dev, "Powercap Version %d.%d\n",
928 PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
930 pinfo = devm_kzalloc(ph->dev, sizeof(*pinfo), GFP_KERNEL);
934 ret = scmi_powercap_attributes_get(ph, pinfo);
938 pinfo->powercaps = devm_kcalloc(ph->dev, pinfo->num_domains,
939 sizeof(*pinfo->powercaps),
941 if (!pinfo->powercaps)
944 pinfo->states = devm_kcalloc(ph->dev, pinfo->num_domains,
945 sizeof(*pinfo->states), GFP_KERNEL);
950 * Note that any failure in retrieving any domain attribute leads to
951 * the whole Powercap protocol initialization failure: this way the
952 * reported Powercap domains are all assured, when accessed, to be well
953 * formed and correlated by sane parent-child relationship (if any).
955 for (domain = 0; domain < pinfo->num_domains; domain++) {
956 ret = scmi_powercap_domain_attributes_get(ph, pinfo, domain);
960 if (pinfo->powercaps[domain].fastchannels)
961 scmi_powercap_domain_init_fc(ph, domain,
962 &pinfo->powercaps[domain].fc_info);
964 /* Grab initial state when disable is supported. */
965 if (PROTOCOL_REV_MAJOR(version) >= 0x2) {
966 ret = __scmi_powercap_cap_get(ph,
967 &pinfo->powercaps[domain],
968 &pinfo->states[domain].last_pcap);
972 pinfo->states[domain].enabled =
973 !!pinfo->states[domain].last_pcap;
977 pinfo->version = version;
978 return ph->set_priv(ph, pinfo);
981 static const struct scmi_protocol scmi_powercap = {
982 .id = SCMI_PROTOCOL_POWERCAP,
983 .owner = THIS_MODULE,
984 .instance_init = &scmi_powercap_protocol_init,
985 .ops = &powercap_proto_ops,
986 .events = &powercap_protocol_events,
989 DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(powercap, scmi_powercap)