1 // SPDX-License-Identifier: GPL-2.0
3 * System Control and Management Interface (SCMI) Performance Protocol
5 * Copyright (C) 2018-2023 ARM Ltd.
8 #define pr_fmt(fmt) "SCMI Notifications PERF - " fmt
10 #include <linux/bits.h>
11 #include <linux/hashtable.h>
13 #include <linux/log2.h>
14 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_opp.h>
18 #include <linux/scmi_protocol.h>
19 #include <linux/sort.h>
20 #include <linux/xarray.h>
22 #include <trace/events/scmi.h>
24 #include "protocols.h"
27 /* Updated only after ALL the mandatory features for that version are merged */
28 #define SCMI_PROTOCOL_SUPPORTED_VERSION 0x40000
32 enum scmi_performance_protocol_cmd {
33 PERF_DOMAIN_ATTRIBUTES = 0x3,
34 PERF_DESCRIBE_LEVELS = 0x4,
35 PERF_LIMITS_SET = 0x5,
36 PERF_LIMITS_GET = 0x6,
39 PERF_NOTIFY_LIMITS = 0x9,
40 PERF_NOTIFY_LEVEL = 0xa,
41 PERF_DESCRIBE_FASTCHANNEL = 0xb,
42 PERF_DOMAIN_NAME_GET = 0xc,
57 struct hlist_node hash;
60 struct scmi_msg_resp_perf_attributes {
63 #define POWER_SCALE_IN_MILLIWATT(x) ((x) & BIT(0))
64 #define POWER_SCALE_IN_MICROWATT(x) ((x) & BIT(1))
65 __le32 stats_addr_low;
66 __le32 stats_addr_high;
70 struct scmi_msg_resp_perf_domain_attributes {
72 #define SUPPORTS_SET_LIMITS(x) ((x) & BIT(31))
73 #define SUPPORTS_SET_PERF_LVL(x) ((x) & BIT(30))
74 #define SUPPORTS_PERF_LIMIT_NOTIFY(x) ((x) & BIT(29))
75 #define SUPPORTS_PERF_LEVEL_NOTIFY(x) ((x) & BIT(28))
76 #define SUPPORTS_PERF_FASTCHANNELS(x) ((x) & BIT(27))
77 #define SUPPORTS_EXTENDED_NAMES(x) ((x) & BIT(26))
78 #define SUPPORTS_LEVEL_INDEXING(x) ((x) & BIT(25))
80 __le32 sustained_freq_khz;
81 __le32 sustained_perf_level;
82 u8 name[SCMI_SHORT_NAME_MAX_SIZE];
85 struct scmi_msg_perf_describe_levels {
90 struct scmi_perf_set_limits {
96 struct scmi_perf_get_limits {
101 struct scmi_perf_set_level {
106 struct scmi_perf_notify_level_or_limits {
108 __le32 notify_enable;
111 struct scmi_perf_limits_notify_payld {
118 struct scmi_perf_level_notify_payld {
121 __le32 performance_level;
124 struct scmi_msg_resp_perf_describe_levels {
126 __le16 num_remaining;
130 __le16 transition_latency_us;
135 struct scmi_msg_resp_perf_describe_levels_v4 {
137 __le16 num_remaining;
141 __le16 transition_latency_us;
143 __le32 indicative_freq;
148 struct perf_dom_info {
151 bool perf_limit_notify;
152 bool perf_level_notify;
153 bool perf_fastchannels;
154 bool level_indexing_mode;
156 u32 sustained_freq_khz;
157 u32 sustained_perf_level;
158 unsigned long mult_factor;
159 struct scmi_perf_domain_info info;
160 struct scmi_opp opp[MAX_OPPS];
161 struct scmi_fc_info *fc_info;
162 struct xarray opps_by_idx;
163 struct xarray opps_by_lvl;
164 DECLARE_HASHTABLE(opps_by_freq, ilog2(MAX_OPPS));
167 #define LOOKUP_BY_FREQ(__htp, __freq) \
169 /* u32 cast is needed to pick right hash func */ \
170 u32 f_ = (u32)(__freq); \
171 struct scmi_opp *_opp; \
173 hash_for_each_possible((__htp), _opp, hash, f_) \
174 if (_opp->indicative_freq == f_) \
179 struct scmi_perf_info {
182 enum scmi_power_scale power_scale;
185 struct perf_dom_info *dom_info;
188 static enum scmi_performance_protocol_cmd evt_2_cmd[] = {
193 static int scmi_perf_attributes_get(const struct scmi_protocol_handle *ph,
194 struct scmi_perf_info *pi)
198 struct scmi_msg_resp_perf_attributes *attr;
200 ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES, 0,
207 ret = ph->xops->do_xfer(ph, t);
209 u16 flags = le16_to_cpu(attr->flags);
211 pi->num_domains = le16_to_cpu(attr->num_domains);
213 if (POWER_SCALE_IN_MILLIWATT(flags))
214 pi->power_scale = SCMI_POWER_MILLIWATTS;
215 if (PROTOCOL_REV_MAJOR(pi->version) >= 0x3)
216 if (POWER_SCALE_IN_MICROWATT(flags))
217 pi->power_scale = SCMI_POWER_MICROWATTS;
219 pi->stats_addr = le32_to_cpu(attr->stats_addr_low) |
220 (u64)le32_to_cpu(attr->stats_addr_high) << 32;
221 pi->stats_size = le32_to_cpu(attr->stats_size);
224 ph->xops->xfer_put(ph, t);
228 static void scmi_perf_xa_destroy(void *data)
231 struct scmi_perf_info *pinfo = data;
233 for (domain = 0; domain < pinfo->num_domains; domain++) {
234 xa_destroy(&((pinfo->dom_info + domain)->opps_by_idx));
235 xa_destroy(&((pinfo->dom_info + domain)->opps_by_lvl));
240 scmi_perf_domain_attributes_get(const struct scmi_protocol_handle *ph,
241 struct perf_dom_info *dom_info,
247 struct scmi_msg_resp_perf_domain_attributes *attr;
249 ret = ph->xops->xfer_get_init(ph, PERF_DOMAIN_ATTRIBUTES,
250 sizeof(dom_info->id), sizeof(*attr), &t);
254 put_unaligned_le32(dom_info->id, t->tx.buf);
257 ret = ph->xops->do_xfer(ph, t);
259 flags = le32_to_cpu(attr->flags);
261 dom_info->set_limits = SUPPORTS_SET_LIMITS(flags);
262 dom_info->info.set_perf = SUPPORTS_SET_PERF_LVL(flags);
263 dom_info->perf_limit_notify = SUPPORTS_PERF_LIMIT_NOTIFY(flags);
264 dom_info->perf_level_notify = SUPPORTS_PERF_LEVEL_NOTIFY(flags);
265 dom_info->perf_fastchannels = SUPPORTS_PERF_FASTCHANNELS(flags);
266 if (PROTOCOL_REV_MAJOR(version) >= 0x4)
267 dom_info->level_indexing_mode =
268 SUPPORTS_LEVEL_INDEXING(flags);
269 dom_info->sustained_freq_khz =
270 le32_to_cpu(attr->sustained_freq_khz);
271 dom_info->sustained_perf_level =
272 le32_to_cpu(attr->sustained_perf_level);
273 if (!dom_info->sustained_freq_khz ||
274 !dom_info->sustained_perf_level ||
275 dom_info->level_indexing_mode)
276 /* CPUFreq converts to kHz, hence default 1000 */
277 dom_info->mult_factor = 1000;
279 dom_info->mult_factor =
280 (dom_info->sustained_freq_khz * 1000UL)
281 / dom_info->sustained_perf_level;
282 strscpy(dom_info->info.name, attr->name,
283 SCMI_SHORT_NAME_MAX_SIZE);
286 ph->xops->xfer_put(ph, t);
289 * If supported overwrite short name with the extended one;
290 * on error just carry on and use already provided short name.
292 if (!ret && PROTOCOL_REV_MAJOR(version) >= 0x3 &&
293 SUPPORTS_EXTENDED_NAMES(flags))
294 ph->hops->extended_name_get(ph, PERF_DOMAIN_NAME_GET,
295 dom_info->id, NULL, dom_info->info.name,
298 if (dom_info->level_indexing_mode) {
299 xa_init(&dom_info->opps_by_idx);
300 xa_init(&dom_info->opps_by_lvl);
301 hash_init(dom_info->opps_by_freq);
307 static int opp_cmp_func(const void *opp1, const void *opp2)
309 const struct scmi_opp *t1 = opp1, *t2 = opp2;
311 return t1->perf - t2->perf;
314 struct scmi_perf_ipriv {
316 struct perf_dom_info *perf_dom;
319 static void iter_perf_levels_prepare_message(void *message,
320 unsigned int desc_index,
323 struct scmi_msg_perf_describe_levels *msg = message;
324 const struct scmi_perf_ipriv *p = priv;
326 msg->domain = cpu_to_le32(p->perf_dom->id);
327 /* Set the number of OPPs to be skipped/already read */
328 msg->level_index = cpu_to_le32(desc_index);
331 static int iter_perf_levels_update_state(struct scmi_iterator_state *st,
332 const void *response, void *priv)
334 const struct scmi_msg_resp_perf_describe_levels *r = response;
336 st->num_returned = le16_to_cpu(r->num_returned);
337 st->num_remaining = le16_to_cpu(r->num_remaining);
343 process_response_opp(struct scmi_opp *opp, unsigned int loop_idx,
344 const struct scmi_msg_resp_perf_describe_levels *r)
346 opp->perf = le32_to_cpu(r->opp[loop_idx].perf_val);
347 opp->power = le32_to_cpu(r->opp[loop_idx].power);
348 opp->trans_latency_us =
349 le16_to_cpu(r->opp[loop_idx].transition_latency_us);
353 process_response_opp_v4(struct device *dev, struct perf_dom_info *dom,
354 struct scmi_opp *opp, unsigned int loop_idx,
355 const struct scmi_msg_resp_perf_describe_levels_v4 *r)
357 opp->perf = le32_to_cpu(r->opp[loop_idx].perf_val);
358 opp->power = le32_to_cpu(r->opp[loop_idx].power);
359 opp->trans_latency_us =
360 le16_to_cpu(r->opp[loop_idx].transition_latency_us);
362 /* Note that PERF v4 reports always five 32-bit words */
363 opp->indicative_freq = le32_to_cpu(r->opp[loop_idx].indicative_freq);
364 if (dom->level_indexing_mode) {
367 opp->level_index = le32_to_cpu(r->opp[loop_idx].level_index);
369 ret = xa_insert(&dom->opps_by_idx, opp->level_index, opp,
373 "Failed to add opps_by_idx at %d - ret:%d\n",
374 opp->level_index, ret);
376 ret = xa_insert(&dom->opps_by_lvl, opp->perf, opp, GFP_KERNEL);
379 "Failed to add opps_by_lvl at %d - ret:%d\n",
382 hash_add(dom->opps_by_freq, &opp->hash, opp->indicative_freq);
387 iter_perf_levels_process_response(const struct scmi_protocol_handle *ph,
388 const void *response,
389 struct scmi_iterator_state *st, void *priv)
391 struct scmi_opp *opp;
392 struct scmi_perf_ipriv *p = priv;
394 opp = &p->perf_dom->opp[st->desc_index + st->loop_idx];
395 if (PROTOCOL_REV_MAJOR(p->version) <= 0x3)
396 process_response_opp(opp, st->loop_idx, response);
398 process_response_opp_v4(ph->dev, p->perf_dom, opp, st->loop_idx,
400 p->perf_dom->opp_count++;
402 dev_dbg(ph->dev, "Level %d Power %d Latency %dus Ifreq %d Index %d\n",
403 opp->perf, opp->power, opp->trans_latency_us,
404 opp->indicative_freq, opp->level_index);
410 scmi_perf_describe_levels_get(const struct scmi_protocol_handle *ph,
411 struct perf_dom_info *perf_dom, u32 version)
415 struct scmi_iterator_ops ops = {
416 .prepare_message = iter_perf_levels_prepare_message,
417 .update_state = iter_perf_levels_update_state,
418 .process_response = iter_perf_levels_process_response,
420 struct scmi_perf_ipriv ppriv = {
422 .perf_dom = perf_dom,
425 iter = ph->hops->iter_response_init(ph, &ops, MAX_OPPS,
426 PERF_DESCRIBE_LEVELS,
427 sizeof(struct scmi_msg_perf_describe_levels),
430 return PTR_ERR(iter);
432 ret = ph->hops->iter_response_run(iter);
436 if (perf_dom->opp_count)
437 sort(perf_dom->opp, perf_dom->opp_count,
438 sizeof(struct scmi_opp), opp_cmp_func, NULL);
443 static int scmi_perf_num_domains_get(const struct scmi_protocol_handle *ph)
445 struct scmi_perf_info *pi = ph->get_priv(ph);
447 return pi->num_domains;
450 static inline struct perf_dom_info *
451 scmi_perf_domain_lookup(const struct scmi_protocol_handle *ph, u32 domain)
453 struct scmi_perf_info *pi = ph->get_priv(ph);
455 if (domain >= pi->num_domains)
456 return ERR_PTR(-EINVAL);
458 return pi->dom_info + domain;
461 static const struct scmi_perf_domain_info *
462 scmi_perf_info_get(const struct scmi_protocol_handle *ph, u32 domain)
464 struct perf_dom_info *dom;
466 dom = scmi_perf_domain_lookup(ph, domain);
468 return ERR_PTR(-EINVAL);
473 static int scmi_perf_msg_limits_set(const struct scmi_protocol_handle *ph,
474 u32 domain, u32 max_perf, u32 min_perf)
478 struct scmi_perf_set_limits *limits;
480 ret = ph->xops->xfer_get_init(ph, PERF_LIMITS_SET,
481 sizeof(*limits), 0, &t);
486 limits->domain = cpu_to_le32(domain);
487 limits->max_level = cpu_to_le32(max_perf);
488 limits->min_level = cpu_to_le32(min_perf);
490 ret = ph->xops->do_xfer(ph, t);
492 ph->xops->xfer_put(ph, t);
496 static int __scmi_perf_limits_set(const struct scmi_protocol_handle *ph,
497 struct perf_dom_info *dom, u32 max_perf,
500 if (dom->fc_info && dom->fc_info[PERF_FC_LIMIT].set_addr) {
501 struct scmi_fc_info *fci = &dom->fc_info[PERF_FC_LIMIT];
503 trace_scmi_fc_call(SCMI_PROTOCOL_PERF, PERF_LIMITS_SET,
504 dom->id, min_perf, max_perf);
505 iowrite32(max_perf, fci->set_addr);
506 iowrite32(min_perf, fci->set_addr + 4);
507 ph->hops->fastchannel_db_ring(fci->set_db);
511 return scmi_perf_msg_limits_set(ph, dom->id, max_perf, min_perf);
514 static int scmi_perf_limits_set(const struct scmi_protocol_handle *ph,
515 u32 domain, u32 max_perf, u32 min_perf)
517 struct scmi_perf_info *pi = ph->get_priv(ph);
518 struct perf_dom_info *dom;
520 dom = scmi_perf_domain_lookup(ph, domain);
524 if (!dom->set_limits)
527 if (PROTOCOL_REV_MAJOR(pi->version) >= 0x3 && !max_perf && !min_perf)
530 if (dom->level_indexing_mode) {
531 struct scmi_opp *opp;
534 opp = xa_load(&dom->opps_by_lvl, min_perf);
538 min_perf = opp->level_index;
542 opp = xa_load(&dom->opps_by_lvl, max_perf);
546 max_perf = opp->level_index;
550 return __scmi_perf_limits_set(ph, dom, max_perf, min_perf);
553 static int scmi_perf_msg_limits_get(const struct scmi_protocol_handle *ph,
554 u32 domain, u32 *max_perf, u32 *min_perf)
558 struct scmi_perf_get_limits *limits;
560 ret = ph->xops->xfer_get_init(ph, PERF_LIMITS_GET,
561 sizeof(__le32), 0, &t);
565 put_unaligned_le32(domain, t->tx.buf);
567 ret = ph->xops->do_xfer(ph, t);
571 *max_perf = le32_to_cpu(limits->max_level);
572 *min_perf = le32_to_cpu(limits->min_level);
575 ph->xops->xfer_put(ph, t);
579 static int __scmi_perf_limits_get(const struct scmi_protocol_handle *ph,
580 struct perf_dom_info *dom, u32 *max_perf,
583 if (dom->fc_info && dom->fc_info[PERF_FC_LIMIT].get_addr) {
584 struct scmi_fc_info *fci = &dom->fc_info[PERF_FC_LIMIT];
586 *max_perf = ioread32(fci->get_addr);
587 *min_perf = ioread32(fci->get_addr + 4);
588 trace_scmi_fc_call(SCMI_PROTOCOL_PERF, PERF_LIMITS_GET,
589 dom->id, *min_perf, *max_perf);
593 return scmi_perf_msg_limits_get(ph, dom->id, max_perf, min_perf);
596 static int scmi_perf_limits_get(const struct scmi_protocol_handle *ph,
597 u32 domain, u32 *max_perf, u32 *min_perf)
600 struct perf_dom_info *dom;
602 dom = scmi_perf_domain_lookup(ph, domain);
606 ret = __scmi_perf_limits_get(ph, dom, max_perf, min_perf);
610 if (dom->level_indexing_mode) {
611 struct scmi_opp *opp;
613 opp = xa_load(&dom->opps_by_idx, *min_perf);
617 *min_perf = opp->perf;
619 opp = xa_load(&dom->opps_by_idx, *max_perf);
623 *max_perf = opp->perf;
629 static int scmi_perf_msg_level_set(const struct scmi_protocol_handle *ph,
630 u32 domain, u32 level, bool poll)
634 struct scmi_perf_set_level *lvl;
636 ret = ph->xops->xfer_get_init(ph, PERF_LEVEL_SET, sizeof(*lvl), 0, &t);
640 t->hdr.poll_completion = poll;
642 lvl->domain = cpu_to_le32(domain);
643 lvl->level = cpu_to_le32(level);
645 ret = ph->xops->do_xfer(ph, t);
647 ph->xops->xfer_put(ph, t);
651 static int __scmi_perf_level_set(const struct scmi_protocol_handle *ph,
652 struct perf_dom_info *dom, u32 level,
655 if (dom->fc_info && dom->fc_info[PERF_FC_LEVEL].set_addr) {
656 struct scmi_fc_info *fci = &dom->fc_info[PERF_FC_LEVEL];
658 trace_scmi_fc_call(SCMI_PROTOCOL_PERF, PERF_LEVEL_SET,
660 iowrite32(level, fci->set_addr);
661 ph->hops->fastchannel_db_ring(fci->set_db);
665 return scmi_perf_msg_level_set(ph, dom->id, level, poll);
668 static int scmi_perf_level_set(const struct scmi_protocol_handle *ph,
669 u32 domain, u32 level, bool poll)
671 struct perf_dom_info *dom;
673 dom = scmi_perf_domain_lookup(ph, domain);
677 if (!dom->info.set_perf)
680 if (dom->level_indexing_mode) {
681 struct scmi_opp *opp;
683 opp = xa_load(&dom->opps_by_lvl, level);
687 level = opp->level_index;
690 return __scmi_perf_level_set(ph, dom, level, poll);
693 static int scmi_perf_msg_level_get(const struct scmi_protocol_handle *ph,
694 u32 domain, u32 *level, bool poll)
699 ret = ph->xops->xfer_get_init(ph, PERF_LEVEL_GET,
700 sizeof(u32), sizeof(u32), &t);
704 t->hdr.poll_completion = poll;
705 put_unaligned_le32(domain, t->tx.buf);
707 ret = ph->xops->do_xfer(ph, t);
709 *level = get_unaligned_le32(t->rx.buf);
711 ph->xops->xfer_put(ph, t);
715 static int __scmi_perf_level_get(const struct scmi_protocol_handle *ph,
716 struct perf_dom_info *dom, u32 *level,
719 if (dom->fc_info && dom->fc_info[PERF_FC_LEVEL].get_addr) {
720 *level = ioread32(dom->fc_info[PERF_FC_LEVEL].get_addr);
721 trace_scmi_fc_call(SCMI_PROTOCOL_PERF, PERF_LEVEL_GET,
726 return scmi_perf_msg_level_get(ph, dom->id, level, poll);
729 static int scmi_perf_level_get(const struct scmi_protocol_handle *ph,
730 u32 domain, u32 *level, bool poll)
733 struct perf_dom_info *dom;
735 dom = scmi_perf_domain_lookup(ph, domain);
739 ret = __scmi_perf_level_get(ph, dom, level, poll);
743 if (dom->level_indexing_mode) {
744 struct scmi_opp *opp;
746 opp = xa_load(&dom->opps_by_idx, *level);
756 static int scmi_perf_level_limits_notify(const struct scmi_protocol_handle *ph,
757 u32 domain, int message_id,
762 struct scmi_perf_notify_level_or_limits *notify;
764 ret = ph->xops->xfer_get_init(ph, message_id, sizeof(*notify), 0, &t);
769 notify->domain = cpu_to_le32(domain);
770 notify->notify_enable = enable ? cpu_to_le32(BIT(0)) : 0;
772 ret = ph->xops->do_xfer(ph, t);
774 ph->xops->xfer_put(ph, t);
778 static void scmi_perf_domain_init_fc(const struct scmi_protocol_handle *ph,
779 struct perf_dom_info *dom)
781 struct scmi_fc_info *fc;
783 fc = devm_kcalloc(ph->dev, PERF_FC_MAX, sizeof(*fc), GFP_KERNEL);
787 ph->hops->fastchannel_init(ph, PERF_DESCRIBE_FASTCHANNEL,
788 PERF_LEVEL_GET, 4, dom->id,
789 &fc[PERF_FC_LEVEL].get_addr, NULL);
791 ph->hops->fastchannel_init(ph, PERF_DESCRIBE_FASTCHANNEL,
792 PERF_LIMITS_GET, 8, dom->id,
793 &fc[PERF_FC_LIMIT].get_addr, NULL);
795 if (dom->info.set_perf)
796 ph->hops->fastchannel_init(ph, PERF_DESCRIBE_FASTCHANNEL,
797 PERF_LEVEL_SET, 4, dom->id,
798 &fc[PERF_FC_LEVEL].set_addr,
799 &fc[PERF_FC_LEVEL].set_db);
802 ph->hops->fastchannel_init(ph, PERF_DESCRIBE_FASTCHANNEL,
803 PERF_LIMITS_SET, 8, dom->id,
804 &fc[PERF_FC_LIMIT].set_addr,
805 &fc[PERF_FC_LIMIT].set_db);
810 static int scmi_dvfs_device_opps_add(const struct scmi_protocol_handle *ph,
811 struct device *dev, u32 domain)
815 struct dev_pm_opp_data data = {};
816 struct perf_dom_info *dom;
818 dom = scmi_perf_domain_lookup(ph, domain);
822 for (idx = 0; idx < dom->opp_count; idx++) {
823 if (!dom->level_indexing_mode)
824 freq = dom->opp[idx].perf * dom->mult_factor;
826 freq = dom->opp[idx].indicative_freq * dom->mult_factor;
828 data.level = dom->opp[idx].perf;
831 ret = dev_pm_opp_add_dynamic(dev, &data);
833 dev_warn(dev, "failed to add opp %luHz\n", freq);
834 dev_pm_opp_remove_all_dynamic(dev);
838 dev_dbg(dev, "[%d][%s]:: Registered OPP[%d] %lu\n",
839 domain, dom->info.name, idx, freq);
845 scmi_dvfs_transition_latency_get(const struct scmi_protocol_handle *ph,
848 struct perf_dom_info *dom;
850 dom = scmi_perf_domain_lookup(ph, domain);
855 return dom->opp[dom->opp_count - 1].trans_latency_us * 1000;
858 static int scmi_dvfs_freq_set(const struct scmi_protocol_handle *ph, u32 domain,
859 unsigned long freq, bool poll)
862 struct perf_dom_info *dom;
864 dom = scmi_perf_domain_lookup(ph, domain);
868 if (!dom->level_indexing_mode) {
869 level = freq / dom->mult_factor;
871 struct scmi_opp *opp;
873 opp = LOOKUP_BY_FREQ(dom->opps_by_freq,
874 freq / dom->mult_factor);
878 level = opp->level_index;
881 return __scmi_perf_level_set(ph, dom, level, poll);
884 static int scmi_dvfs_freq_get(const struct scmi_protocol_handle *ph, u32 domain,
885 unsigned long *freq, bool poll)
889 struct perf_dom_info *dom;
891 dom = scmi_perf_domain_lookup(ph, domain);
895 ret = __scmi_perf_level_get(ph, dom, &level, poll);
899 if (!dom->level_indexing_mode) {
900 *freq = level * dom->mult_factor;
902 struct scmi_opp *opp;
904 opp = xa_load(&dom->opps_by_idx, level);
908 *freq = opp->indicative_freq * dom->mult_factor;
914 static int scmi_dvfs_est_power_get(const struct scmi_protocol_handle *ph,
915 u32 domain, unsigned long *freq,
916 unsigned long *power)
918 struct perf_dom_info *dom;
919 unsigned long opp_freq;
920 int idx, ret = -EINVAL;
921 struct scmi_opp *opp;
923 dom = scmi_perf_domain_lookup(ph, domain);
927 for (opp = dom->opp, idx = 0; idx < dom->opp_count; idx++, opp++) {
928 if (!dom->level_indexing_mode)
929 opp_freq = opp->perf * dom->mult_factor;
931 opp_freq = opp->indicative_freq * dom->mult_factor;
933 if (opp_freq < *freq)
945 static bool scmi_fast_switch_possible(const struct scmi_protocol_handle *ph,
948 struct perf_dom_info *dom;
950 dom = scmi_perf_domain_lookup(ph, domain);
954 return dom->fc_info && dom->fc_info[PERF_FC_LEVEL].set_addr;
957 static enum scmi_power_scale
958 scmi_power_scale_get(const struct scmi_protocol_handle *ph)
960 struct scmi_perf_info *pi = ph->get_priv(ph);
962 return pi->power_scale;
965 static const struct scmi_perf_proto_ops perf_proto_ops = {
966 .num_domains_get = scmi_perf_num_domains_get,
967 .info_get = scmi_perf_info_get,
968 .limits_set = scmi_perf_limits_set,
969 .limits_get = scmi_perf_limits_get,
970 .level_set = scmi_perf_level_set,
971 .level_get = scmi_perf_level_get,
972 .transition_latency_get = scmi_dvfs_transition_latency_get,
973 .device_opps_add = scmi_dvfs_device_opps_add,
974 .freq_set = scmi_dvfs_freq_set,
975 .freq_get = scmi_dvfs_freq_get,
976 .est_power_get = scmi_dvfs_est_power_get,
977 .fast_switch_possible = scmi_fast_switch_possible,
978 .power_scale_get = scmi_power_scale_get,
981 static int scmi_perf_set_notify_enabled(const struct scmi_protocol_handle *ph,
982 u8 evt_id, u32 src_id, bool enable)
986 if (evt_id >= ARRAY_SIZE(evt_2_cmd))
989 cmd_id = evt_2_cmd[evt_id];
990 ret = scmi_perf_level_limits_notify(ph, src_id, cmd_id, enable);
992 pr_debug("FAIL_ENABLED - evt[%X] dom[%d] - ret:%d\n",
993 evt_id, src_id, ret);
998 static void *scmi_perf_fill_custom_report(const struct scmi_protocol_handle *ph,
999 u8 evt_id, ktime_t timestamp,
1000 const void *payld, size_t payld_sz,
1001 void *report, u32 *src_id)
1006 case SCMI_EVENT_PERFORMANCE_LIMITS_CHANGED:
1008 const struct scmi_perf_limits_notify_payld *p = payld;
1009 struct scmi_perf_limits_report *r = report;
1011 if (sizeof(*p) != payld_sz)
1014 r->timestamp = timestamp;
1015 r->agent_id = le32_to_cpu(p->agent_id);
1016 r->domain_id = le32_to_cpu(p->domain_id);
1017 r->range_max = le32_to_cpu(p->range_max);
1018 r->range_min = le32_to_cpu(p->range_min);
1019 *src_id = r->domain_id;
1023 case SCMI_EVENT_PERFORMANCE_LEVEL_CHANGED:
1025 const struct scmi_perf_level_notify_payld *p = payld;
1026 struct scmi_perf_level_report *r = report;
1028 if (sizeof(*p) != payld_sz)
1031 r->timestamp = timestamp;
1032 r->agent_id = le32_to_cpu(p->agent_id);
1033 r->domain_id = le32_to_cpu(p->domain_id);
1034 r->performance_level = le32_to_cpu(p->performance_level);
1035 *src_id = r->domain_id;
1046 static int scmi_perf_get_num_sources(const struct scmi_protocol_handle *ph)
1048 struct scmi_perf_info *pi = ph->get_priv(ph);
1053 return pi->num_domains;
1056 static const struct scmi_event perf_events[] = {
1058 .id = SCMI_EVENT_PERFORMANCE_LIMITS_CHANGED,
1059 .max_payld_sz = sizeof(struct scmi_perf_limits_notify_payld),
1060 .max_report_sz = sizeof(struct scmi_perf_limits_report),
1063 .id = SCMI_EVENT_PERFORMANCE_LEVEL_CHANGED,
1064 .max_payld_sz = sizeof(struct scmi_perf_level_notify_payld),
1065 .max_report_sz = sizeof(struct scmi_perf_level_report),
1069 static const struct scmi_event_ops perf_event_ops = {
1070 .get_num_sources = scmi_perf_get_num_sources,
1071 .set_notify_enabled = scmi_perf_set_notify_enabled,
1072 .fill_custom_report = scmi_perf_fill_custom_report,
1075 static const struct scmi_protocol_events perf_protocol_events = {
1076 .queue_sz = SCMI_PROTO_QUEUE_SZ,
1077 .ops = &perf_event_ops,
1078 .evts = perf_events,
1079 .num_events = ARRAY_SIZE(perf_events),
1082 static int scmi_perf_protocol_init(const struct scmi_protocol_handle *ph)
1086 struct scmi_perf_info *pinfo;
1088 ret = ph->xops->version_get(ph, &version);
1092 dev_dbg(ph->dev, "Performance Version %d.%d\n",
1093 PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
1095 pinfo = devm_kzalloc(ph->dev, sizeof(*pinfo), GFP_KERNEL);
1099 pinfo->version = version;
1101 ret = scmi_perf_attributes_get(ph, pinfo);
1105 pinfo->dom_info = devm_kcalloc(ph->dev, pinfo->num_domains,
1106 sizeof(*pinfo->dom_info), GFP_KERNEL);
1107 if (!pinfo->dom_info)
1110 for (domain = 0; domain < pinfo->num_domains; domain++) {
1111 struct perf_dom_info *dom = pinfo->dom_info + domain;
1114 scmi_perf_domain_attributes_get(ph, dom, version);
1115 scmi_perf_describe_levels_get(ph, dom, version);
1117 if (dom->perf_fastchannels)
1118 scmi_perf_domain_init_fc(ph, dom);
1121 ret = devm_add_action_or_reset(ph->dev, scmi_perf_xa_destroy, pinfo);
1125 return ph->set_priv(ph, pinfo, version);
1128 static const struct scmi_protocol scmi_perf = {
1129 .id = SCMI_PROTOCOL_PERF,
1130 .owner = THIS_MODULE,
1131 .instance_init = &scmi_perf_protocol_init,
1132 .ops = &perf_proto_ops,
1133 .events = &perf_protocol_events,
1134 .supported_version = SCMI_PROTOCOL_SUPPORTED_VERSION,
1137 DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(perf, scmi_perf)