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"
29 enum scmi_performance_protocol_cmd {
30 PERF_DOMAIN_ATTRIBUTES = 0x3,
31 PERF_DESCRIBE_LEVELS = 0x4,
32 PERF_LIMITS_SET = 0x5,
33 PERF_LIMITS_GET = 0x6,
36 PERF_NOTIFY_LIMITS = 0x9,
37 PERF_NOTIFY_LEVEL = 0xa,
38 PERF_DESCRIBE_FASTCHANNEL = 0xb,
39 PERF_DOMAIN_NAME_GET = 0xc,
54 struct hlist_node hash;
57 struct scmi_msg_resp_perf_attributes {
60 #define POWER_SCALE_IN_MILLIWATT(x) ((x) & BIT(0))
61 #define POWER_SCALE_IN_MICROWATT(x) ((x) & BIT(1))
62 __le32 stats_addr_low;
63 __le32 stats_addr_high;
67 struct scmi_msg_resp_perf_domain_attributes {
69 #define SUPPORTS_SET_LIMITS(x) ((x) & BIT(31))
70 #define SUPPORTS_SET_PERF_LVL(x) ((x) & BIT(30))
71 #define SUPPORTS_PERF_LIMIT_NOTIFY(x) ((x) & BIT(29))
72 #define SUPPORTS_PERF_LEVEL_NOTIFY(x) ((x) & BIT(28))
73 #define SUPPORTS_PERF_FASTCHANNELS(x) ((x) & BIT(27))
74 #define SUPPORTS_EXTENDED_NAMES(x) ((x) & BIT(26))
75 #define SUPPORTS_LEVEL_INDEXING(x) ((x) & BIT(25))
77 __le32 sustained_freq_khz;
78 __le32 sustained_perf_level;
79 u8 name[SCMI_SHORT_NAME_MAX_SIZE];
82 struct scmi_msg_perf_describe_levels {
87 struct scmi_perf_set_limits {
93 struct scmi_perf_get_limits {
98 struct scmi_perf_set_level {
103 struct scmi_perf_notify_level_or_limits {
105 __le32 notify_enable;
108 struct scmi_perf_limits_notify_payld {
115 struct scmi_perf_level_notify_payld {
118 __le32 performance_level;
121 struct scmi_msg_resp_perf_describe_levels {
123 __le16 num_remaining;
127 __le16 transition_latency_us;
132 struct scmi_msg_resp_perf_describe_levels_v4 {
134 __le16 num_remaining;
138 __le16 transition_latency_us;
140 __le32 indicative_freq;
145 struct perf_dom_info {
148 bool perf_limit_notify;
149 bool perf_level_notify;
150 bool perf_fastchannels;
151 bool level_indexing_mode;
153 u32 sustained_freq_khz;
154 u32 sustained_perf_level;
155 unsigned long mult_factor;
156 struct scmi_perf_domain_info info;
157 struct scmi_opp opp[MAX_OPPS];
158 struct scmi_fc_info *fc_info;
159 struct xarray opps_by_idx;
160 struct xarray opps_by_lvl;
161 DECLARE_HASHTABLE(opps_by_freq, ilog2(MAX_OPPS));
164 #define LOOKUP_BY_FREQ(__htp, __freq) \
166 /* u32 cast is needed to pick right hash func */ \
167 u32 f_ = (u32)(__freq); \
168 struct scmi_opp *_opp; \
170 hash_for_each_possible((__htp), _opp, hash, f_) \
171 if (_opp->indicative_freq == f_) \
176 struct scmi_perf_info {
179 enum scmi_power_scale power_scale;
182 struct perf_dom_info *dom_info;
185 static enum scmi_performance_protocol_cmd evt_2_cmd[] = {
190 static int scmi_perf_attributes_get(const struct scmi_protocol_handle *ph,
191 struct scmi_perf_info *pi)
195 struct scmi_msg_resp_perf_attributes *attr;
197 ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES, 0,
204 ret = ph->xops->do_xfer(ph, t);
206 u16 flags = le16_to_cpu(attr->flags);
208 pi->num_domains = le16_to_cpu(attr->num_domains);
210 if (POWER_SCALE_IN_MILLIWATT(flags))
211 pi->power_scale = SCMI_POWER_MILLIWATTS;
212 if (PROTOCOL_REV_MAJOR(pi->version) >= 0x3)
213 if (POWER_SCALE_IN_MICROWATT(flags))
214 pi->power_scale = SCMI_POWER_MICROWATTS;
216 pi->stats_addr = le32_to_cpu(attr->stats_addr_low) |
217 (u64)le32_to_cpu(attr->stats_addr_high) << 32;
218 pi->stats_size = le32_to_cpu(attr->stats_size);
221 ph->xops->xfer_put(ph, t);
225 static void scmi_perf_xa_destroy(void *data)
228 struct scmi_perf_info *pinfo = data;
230 for (domain = 0; domain < pinfo->num_domains; domain++) {
231 xa_destroy(&((pinfo->dom_info + domain)->opps_by_idx));
232 xa_destroy(&((pinfo->dom_info + domain)->opps_by_lvl));
237 scmi_perf_domain_attributes_get(const struct scmi_protocol_handle *ph,
238 struct perf_dom_info *dom_info,
244 struct scmi_msg_resp_perf_domain_attributes *attr;
246 ret = ph->xops->xfer_get_init(ph, PERF_DOMAIN_ATTRIBUTES,
247 sizeof(dom_info->id), sizeof(*attr), &t);
251 put_unaligned_le32(dom_info->id, t->tx.buf);
254 ret = ph->xops->do_xfer(ph, t);
256 flags = le32_to_cpu(attr->flags);
258 dom_info->set_limits = SUPPORTS_SET_LIMITS(flags);
259 dom_info->info.set_perf = SUPPORTS_SET_PERF_LVL(flags);
260 dom_info->perf_limit_notify = SUPPORTS_PERF_LIMIT_NOTIFY(flags);
261 dom_info->perf_level_notify = SUPPORTS_PERF_LEVEL_NOTIFY(flags);
262 dom_info->perf_fastchannels = SUPPORTS_PERF_FASTCHANNELS(flags);
263 if (PROTOCOL_REV_MAJOR(version) >= 0x4)
264 dom_info->level_indexing_mode =
265 SUPPORTS_LEVEL_INDEXING(flags);
266 dom_info->sustained_freq_khz =
267 le32_to_cpu(attr->sustained_freq_khz);
268 dom_info->sustained_perf_level =
269 le32_to_cpu(attr->sustained_perf_level);
270 if (!dom_info->sustained_freq_khz ||
271 !dom_info->sustained_perf_level ||
272 dom_info->level_indexing_mode)
273 /* CPUFreq converts to kHz, hence default 1000 */
274 dom_info->mult_factor = 1000;
276 dom_info->mult_factor =
277 (dom_info->sustained_freq_khz * 1000UL)
278 / dom_info->sustained_perf_level;
279 strscpy(dom_info->info.name, attr->name,
280 SCMI_SHORT_NAME_MAX_SIZE);
283 ph->xops->xfer_put(ph, t);
286 * If supported overwrite short name with the extended one;
287 * on error just carry on and use already provided short name.
289 if (!ret && PROTOCOL_REV_MAJOR(version) >= 0x3 &&
290 SUPPORTS_EXTENDED_NAMES(flags))
291 ph->hops->extended_name_get(ph, PERF_DOMAIN_NAME_GET,
292 dom_info->id, dom_info->info.name,
295 if (dom_info->level_indexing_mode) {
296 xa_init(&dom_info->opps_by_idx);
297 xa_init(&dom_info->opps_by_lvl);
298 hash_init(dom_info->opps_by_freq);
304 static int opp_cmp_func(const void *opp1, const void *opp2)
306 const struct scmi_opp *t1 = opp1, *t2 = opp2;
308 return t1->perf - t2->perf;
311 struct scmi_perf_ipriv {
313 struct perf_dom_info *perf_dom;
316 static void iter_perf_levels_prepare_message(void *message,
317 unsigned int desc_index,
320 struct scmi_msg_perf_describe_levels *msg = message;
321 const struct scmi_perf_ipriv *p = priv;
323 msg->domain = cpu_to_le32(p->perf_dom->id);
324 /* Set the number of OPPs to be skipped/already read */
325 msg->level_index = cpu_to_le32(desc_index);
328 static int iter_perf_levels_update_state(struct scmi_iterator_state *st,
329 const void *response, void *priv)
331 const struct scmi_msg_resp_perf_describe_levels *r = response;
333 st->num_returned = le16_to_cpu(r->num_returned);
334 st->num_remaining = le16_to_cpu(r->num_remaining);
340 process_response_opp(struct scmi_opp *opp, unsigned int loop_idx,
341 const struct scmi_msg_resp_perf_describe_levels *r)
343 opp->perf = le32_to_cpu(r->opp[loop_idx].perf_val);
344 opp->power = le32_to_cpu(r->opp[loop_idx].power);
345 opp->trans_latency_us =
346 le16_to_cpu(r->opp[loop_idx].transition_latency_us);
350 process_response_opp_v4(struct device *dev, struct perf_dom_info *dom,
351 struct scmi_opp *opp, unsigned int loop_idx,
352 const struct scmi_msg_resp_perf_describe_levels_v4 *r)
354 opp->perf = le32_to_cpu(r->opp[loop_idx].perf_val);
355 opp->power = le32_to_cpu(r->opp[loop_idx].power);
356 opp->trans_latency_us =
357 le16_to_cpu(r->opp[loop_idx].transition_latency_us);
359 /* Note that PERF v4 reports always five 32-bit words */
360 opp->indicative_freq = le32_to_cpu(r->opp[loop_idx].indicative_freq);
361 if (dom->level_indexing_mode) {
364 opp->level_index = le32_to_cpu(r->opp[loop_idx].level_index);
366 ret = xa_insert(&dom->opps_by_idx, opp->level_index, opp,
370 "Failed to add opps_by_idx at %d - ret:%d\n",
371 opp->level_index, ret);
373 ret = xa_insert(&dom->opps_by_lvl, opp->perf, opp, GFP_KERNEL);
376 "Failed to add opps_by_lvl at %d - ret:%d\n",
379 hash_add(dom->opps_by_freq, &opp->hash, opp->indicative_freq);
384 iter_perf_levels_process_response(const struct scmi_protocol_handle *ph,
385 const void *response,
386 struct scmi_iterator_state *st, void *priv)
388 struct scmi_opp *opp;
389 struct scmi_perf_ipriv *p = priv;
391 opp = &p->perf_dom->opp[st->desc_index + st->loop_idx];
392 if (PROTOCOL_REV_MAJOR(p->version) <= 0x3)
393 process_response_opp(opp, st->loop_idx, response);
395 process_response_opp_v4(ph->dev, p->perf_dom, opp, st->loop_idx,
397 p->perf_dom->opp_count++;
399 dev_dbg(ph->dev, "Level %d Power %d Latency %dus Ifreq %d Index %d\n",
400 opp->perf, opp->power, opp->trans_latency_us,
401 opp->indicative_freq, opp->level_index);
407 scmi_perf_describe_levels_get(const struct scmi_protocol_handle *ph,
408 struct perf_dom_info *perf_dom, u32 version)
412 struct scmi_iterator_ops ops = {
413 .prepare_message = iter_perf_levels_prepare_message,
414 .update_state = iter_perf_levels_update_state,
415 .process_response = iter_perf_levels_process_response,
417 struct scmi_perf_ipriv ppriv = {
419 .perf_dom = perf_dom,
422 iter = ph->hops->iter_response_init(ph, &ops, MAX_OPPS,
423 PERF_DESCRIBE_LEVELS,
424 sizeof(struct scmi_msg_perf_describe_levels),
427 return PTR_ERR(iter);
429 ret = ph->hops->iter_response_run(iter);
433 if (perf_dom->opp_count)
434 sort(perf_dom->opp, perf_dom->opp_count,
435 sizeof(struct scmi_opp), opp_cmp_func, NULL);
440 static int scmi_perf_num_domains_get(const struct scmi_protocol_handle *ph)
442 struct scmi_perf_info *pi = ph->get_priv(ph);
444 return pi->num_domains;
447 static inline struct perf_dom_info *
448 scmi_perf_domain_lookup(const struct scmi_protocol_handle *ph, u32 domain)
450 struct scmi_perf_info *pi = ph->get_priv(ph);
452 if (domain >= pi->num_domains)
453 return ERR_PTR(-EINVAL);
455 return pi->dom_info + domain;
458 static const struct scmi_perf_domain_info *
459 scmi_perf_info_get(const struct scmi_protocol_handle *ph, u32 domain)
461 struct perf_dom_info *dom;
463 dom = scmi_perf_domain_lookup(ph, domain);
465 return ERR_PTR(-EINVAL);
470 static int scmi_perf_msg_limits_set(const struct scmi_protocol_handle *ph,
471 u32 domain, u32 max_perf, u32 min_perf)
475 struct scmi_perf_set_limits *limits;
477 ret = ph->xops->xfer_get_init(ph, PERF_LIMITS_SET,
478 sizeof(*limits), 0, &t);
483 limits->domain = cpu_to_le32(domain);
484 limits->max_level = cpu_to_le32(max_perf);
485 limits->min_level = cpu_to_le32(min_perf);
487 ret = ph->xops->do_xfer(ph, t);
489 ph->xops->xfer_put(ph, t);
493 static int __scmi_perf_limits_set(const struct scmi_protocol_handle *ph,
494 struct perf_dom_info *dom, u32 max_perf,
497 if (dom->fc_info && dom->fc_info[PERF_FC_LIMIT].set_addr) {
498 struct scmi_fc_info *fci = &dom->fc_info[PERF_FC_LIMIT];
500 trace_scmi_fc_call(SCMI_PROTOCOL_PERF, PERF_LIMITS_SET,
501 dom->id, min_perf, max_perf);
502 iowrite32(max_perf, fci->set_addr);
503 iowrite32(min_perf, fci->set_addr + 4);
504 ph->hops->fastchannel_db_ring(fci->set_db);
508 return scmi_perf_msg_limits_set(ph, dom->id, max_perf, min_perf);
511 static int scmi_perf_limits_set(const struct scmi_protocol_handle *ph,
512 u32 domain, u32 max_perf, u32 min_perf)
514 struct scmi_perf_info *pi = ph->get_priv(ph);
515 struct perf_dom_info *dom;
517 dom = scmi_perf_domain_lookup(ph, domain);
521 if (PROTOCOL_REV_MAJOR(pi->version) >= 0x3 && !max_perf && !min_perf)
524 if (dom->level_indexing_mode) {
525 struct scmi_opp *opp;
528 opp = xa_load(&dom->opps_by_lvl, min_perf);
532 min_perf = opp->level_index;
536 opp = xa_load(&dom->opps_by_lvl, max_perf);
540 max_perf = opp->level_index;
544 return __scmi_perf_limits_set(ph, dom, max_perf, min_perf);
547 static int scmi_perf_msg_limits_get(const struct scmi_protocol_handle *ph,
548 u32 domain, u32 *max_perf, u32 *min_perf)
552 struct scmi_perf_get_limits *limits;
554 ret = ph->xops->xfer_get_init(ph, PERF_LIMITS_GET,
555 sizeof(__le32), 0, &t);
559 put_unaligned_le32(domain, t->tx.buf);
561 ret = ph->xops->do_xfer(ph, t);
565 *max_perf = le32_to_cpu(limits->max_level);
566 *min_perf = le32_to_cpu(limits->min_level);
569 ph->xops->xfer_put(ph, t);
573 static int __scmi_perf_limits_get(const struct scmi_protocol_handle *ph,
574 struct perf_dom_info *dom, u32 *max_perf,
577 if (dom->fc_info && dom->fc_info[PERF_FC_LIMIT].get_addr) {
578 struct scmi_fc_info *fci = &dom->fc_info[PERF_FC_LIMIT];
580 *max_perf = ioread32(fci->get_addr);
581 *min_perf = ioread32(fci->get_addr + 4);
582 trace_scmi_fc_call(SCMI_PROTOCOL_PERF, PERF_LIMITS_GET,
583 dom->id, *min_perf, *max_perf);
587 return scmi_perf_msg_limits_get(ph, dom->id, max_perf, min_perf);
590 static int scmi_perf_limits_get(const struct scmi_protocol_handle *ph,
591 u32 domain, u32 *max_perf, u32 *min_perf)
594 struct perf_dom_info *dom;
596 dom = scmi_perf_domain_lookup(ph, domain);
600 ret = __scmi_perf_limits_get(ph, dom, max_perf, min_perf);
604 if (dom->level_indexing_mode) {
605 struct scmi_opp *opp;
607 opp = xa_load(&dom->opps_by_idx, *min_perf);
611 *min_perf = opp->perf;
613 opp = xa_load(&dom->opps_by_idx, *max_perf);
617 *max_perf = opp->perf;
623 static int scmi_perf_msg_level_set(const struct scmi_protocol_handle *ph,
624 u32 domain, u32 level, bool poll)
628 struct scmi_perf_set_level *lvl;
630 ret = ph->xops->xfer_get_init(ph, PERF_LEVEL_SET, sizeof(*lvl), 0, &t);
634 t->hdr.poll_completion = poll;
636 lvl->domain = cpu_to_le32(domain);
637 lvl->level = cpu_to_le32(level);
639 ret = ph->xops->do_xfer(ph, t);
641 ph->xops->xfer_put(ph, t);
645 static int __scmi_perf_level_set(const struct scmi_protocol_handle *ph,
646 struct perf_dom_info *dom, u32 level,
649 if (dom->fc_info && dom->fc_info[PERF_FC_LEVEL].set_addr) {
650 struct scmi_fc_info *fci = &dom->fc_info[PERF_FC_LEVEL];
652 trace_scmi_fc_call(SCMI_PROTOCOL_PERF, PERF_LEVEL_SET,
654 iowrite32(level, fci->set_addr);
655 ph->hops->fastchannel_db_ring(fci->set_db);
659 return scmi_perf_msg_level_set(ph, dom->id, level, poll);
662 static int scmi_perf_level_set(const struct scmi_protocol_handle *ph,
663 u32 domain, u32 level, bool poll)
665 struct perf_dom_info *dom;
667 dom = scmi_perf_domain_lookup(ph, domain);
671 if (dom->level_indexing_mode) {
672 struct scmi_opp *opp;
674 opp = xa_load(&dom->opps_by_lvl, level);
678 level = opp->level_index;
681 return __scmi_perf_level_set(ph, dom, level, poll);
684 static int scmi_perf_msg_level_get(const struct scmi_protocol_handle *ph,
685 u32 domain, u32 *level, bool poll)
690 ret = ph->xops->xfer_get_init(ph, PERF_LEVEL_GET,
691 sizeof(u32), sizeof(u32), &t);
695 t->hdr.poll_completion = poll;
696 put_unaligned_le32(domain, t->tx.buf);
698 ret = ph->xops->do_xfer(ph, t);
700 *level = get_unaligned_le32(t->rx.buf);
702 ph->xops->xfer_put(ph, t);
706 static int __scmi_perf_level_get(const struct scmi_protocol_handle *ph,
707 struct perf_dom_info *dom, u32 *level,
710 if (dom->fc_info && dom->fc_info[PERF_FC_LEVEL].get_addr) {
711 *level = ioread32(dom->fc_info[PERF_FC_LEVEL].get_addr);
712 trace_scmi_fc_call(SCMI_PROTOCOL_PERF, PERF_LEVEL_GET,
717 return scmi_perf_msg_level_get(ph, dom->id, level, poll);
720 static int scmi_perf_level_get(const struct scmi_protocol_handle *ph,
721 u32 domain, u32 *level, bool poll)
724 struct perf_dom_info *dom;
726 dom = scmi_perf_domain_lookup(ph, domain);
730 ret = __scmi_perf_level_get(ph, dom, level, poll);
734 if (dom->level_indexing_mode) {
735 struct scmi_opp *opp;
737 opp = xa_load(&dom->opps_by_idx, *level);
747 static int scmi_perf_level_limits_notify(const struct scmi_protocol_handle *ph,
748 u32 domain, int message_id,
753 struct scmi_perf_notify_level_or_limits *notify;
755 ret = ph->xops->xfer_get_init(ph, message_id, sizeof(*notify), 0, &t);
760 notify->domain = cpu_to_le32(domain);
761 notify->notify_enable = enable ? cpu_to_le32(BIT(0)) : 0;
763 ret = ph->xops->do_xfer(ph, t);
765 ph->xops->xfer_put(ph, t);
769 static void scmi_perf_domain_init_fc(const struct scmi_protocol_handle *ph,
770 u32 domain, struct scmi_fc_info **p_fc)
772 struct scmi_fc_info *fc;
774 fc = devm_kcalloc(ph->dev, PERF_FC_MAX, sizeof(*fc), GFP_KERNEL);
778 ph->hops->fastchannel_init(ph, PERF_DESCRIBE_FASTCHANNEL,
779 PERF_LEVEL_SET, 4, domain,
780 &fc[PERF_FC_LEVEL].set_addr,
781 &fc[PERF_FC_LEVEL].set_db);
783 ph->hops->fastchannel_init(ph, PERF_DESCRIBE_FASTCHANNEL,
784 PERF_LEVEL_GET, 4, domain,
785 &fc[PERF_FC_LEVEL].get_addr, NULL);
787 ph->hops->fastchannel_init(ph, PERF_DESCRIBE_FASTCHANNEL,
788 PERF_LIMITS_SET, 8, domain,
789 &fc[PERF_FC_LIMIT].set_addr,
790 &fc[PERF_FC_LIMIT].set_db);
792 ph->hops->fastchannel_init(ph, PERF_DESCRIBE_FASTCHANNEL,
793 PERF_LIMITS_GET, 8, domain,
794 &fc[PERF_FC_LIMIT].get_addr, NULL);
799 static int scmi_dvfs_device_opps_add(const struct scmi_protocol_handle *ph,
800 struct device *dev, u32 domain)
804 struct dev_pm_opp_data data = {};
805 struct perf_dom_info *dom;
807 dom = scmi_perf_domain_lookup(ph, domain);
811 for (idx = 0; idx < dom->opp_count; idx++) {
812 if (!dom->level_indexing_mode)
813 freq = dom->opp[idx].perf * dom->mult_factor;
815 freq = dom->opp[idx].indicative_freq * dom->mult_factor;
817 data.level = dom->opp[idx].perf;
820 ret = dev_pm_opp_add_dynamic(dev, &data);
822 dev_warn(dev, "failed to add opp %luHz\n", freq);
823 dev_pm_opp_remove_all_dynamic(dev);
827 dev_dbg(dev, "[%d][%s]:: Registered OPP[%d] %lu\n",
828 domain, dom->info.name, idx, freq);
834 scmi_dvfs_transition_latency_get(const struct scmi_protocol_handle *ph,
837 struct perf_dom_info *dom;
839 dom = scmi_perf_domain_lookup(ph, domain);
844 return dom->opp[dom->opp_count - 1].trans_latency_us * 1000;
847 static int scmi_dvfs_freq_set(const struct scmi_protocol_handle *ph, u32 domain,
848 unsigned long freq, bool poll)
851 struct perf_dom_info *dom;
853 dom = scmi_perf_domain_lookup(ph, domain);
857 if (!dom->level_indexing_mode) {
858 level = freq / dom->mult_factor;
860 struct scmi_opp *opp;
862 opp = LOOKUP_BY_FREQ(dom->opps_by_freq,
863 freq / dom->mult_factor);
867 level = opp->level_index;
870 return __scmi_perf_level_set(ph, dom, level, poll);
873 static int scmi_dvfs_freq_get(const struct scmi_protocol_handle *ph, u32 domain,
874 unsigned long *freq, bool poll)
878 struct perf_dom_info *dom;
880 dom = scmi_perf_domain_lookup(ph, domain);
884 ret = __scmi_perf_level_get(ph, dom, &level, poll);
888 if (!dom->level_indexing_mode) {
889 *freq = level * dom->mult_factor;
891 struct scmi_opp *opp;
893 opp = xa_load(&dom->opps_by_idx, level);
897 *freq = opp->indicative_freq * dom->mult_factor;
903 static int scmi_dvfs_est_power_get(const struct scmi_protocol_handle *ph,
904 u32 domain, unsigned long *freq,
905 unsigned long *power)
907 struct perf_dom_info *dom;
908 unsigned long opp_freq;
909 int idx, ret = -EINVAL;
910 struct scmi_opp *opp;
912 dom = scmi_perf_domain_lookup(ph, domain);
916 for (opp = dom->opp, idx = 0; idx < dom->opp_count; idx++, opp++) {
917 if (!dom->level_indexing_mode)
918 opp_freq = opp->perf * dom->mult_factor;
920 opp_freq = opp->indicative_freq * dom->mult_factor;
922 if (opp_freq < *freq)
934 static bool scmi_fast_switch_possible(const struct scmi_protocol_handle *ph,
937 struct perf_dom_info *dom;
939 dom = scmi_perf_domain_lookup(ph, domain);
943 return dom->fc_info && dom->fc_info[PERF_FC_LEVEL].set_addr;
946 static enum scmi_power_scale
947 scmi_power_scale_get(const struct scmi_protocol_handle *ph)
949 struct scmi_perf_info *pi = ph->get_priv(ph);
951 return pi->power_scale;
954 static const struct scmi_perf_proto_ops perf_proto_ops = {
955 .num_domains_get = scmi_perf_num_domains_get,
956 .info_get = scmi_perf_info_get,
957 .limits_set = scmi_perf_limits_set,
958 .limits_get = scmi_perf_limits_get,
959 .level_set = scmi_perf_level_set,
960 .level_get = scmi_perf_level_get,
961 .transition_latency_get = scmi_dvfs_transition_latency_get,
962 .device_opps_add = scmi_dvfs_device_opps_add,
963 .freq_set = scmi_dvfs_freq_set,
964 .freq_get = scmi_dvfs_freq_get,
965 .est_power_get = scmi_dvfs_est_power_get,
966 .fast_switch_possible = scmi_fast_switch_possible,
967 .power_scale_get = scmi_power_scale_get,
970 static int scmi_perf_set_notify_enabled(const struct scmi_protocol_handle *ph,
971 u8 evt_id, u32 src_id, bool enable)
975 if (evt_id >= ARRAY_SIZE(evt_2_cmd))
978 cmd_id = evt_2_cmd[evt_id];
979 ret = scmi_perf_level_limits_notify(ph, src_id, cmd_id, enable);
981 pr_debug("FAIL_ENABLED - evt[%X] dom[%d] - ret:%d\n",
982 evt_id, src_id, ret);
987 static void *scmi_perf_fill_custom_report(const struct scmi_protocol_handle *ph,
988 u8 evt_id, ktime_t timestamp,
989 const void *payld, size_t payld_sz,
990 void *report, u32 *src_id)
995 case SCMI_EVENT_PERFORMANCE_LIMITS_CHANGED:
997 const struct scmi_perf_limits_notify_payld *p = payld;
998 struct scmi_perf_limits_report *r = report;
1000 if (sizeof(*p) != payld_sz)
1003 r->timestamp = timestamp;
1004 r->agent_id = le32_to_cpu(p->agent_id);
1005 r->domain_id = le32_to_cpu(p->domain_id);
1006 r->range_max = le32_to_cpu(p->range_max);
1007 r->range_min = le32_to_cpu(p->range_min);
1008 *src_id = r->domain_id;
1012 case SCMI_EVENT_PERFORMANCE_LEVEL_CHANGED:
1014 const struct scmi_perf_level_notify_payld *p = payld;
1015 struct scmi_perf_level_report *r = report;
1017 if (sizeof(*p) != payld_sz)
1020 r->timestamp = timestamp;
1021 r->agent_id = le32_to_cpu(p->agent_id);
1022 r->domain_id = le32_to_cpu(p->domain_id);
1023 r->performance_level = le32_to_cpu(p->performance_level);
1024 *src_id = r->domain_id;
1035 static int scmi_perf_get_num_sources(const struct scmi_protocol_handle *ph)
1037 struct scmi_perf_info *pi = ph->get_priv(ph);
1042 return pi->num_domains;
1045 static const struct scmi_event perf_events[] = {
1047 .id = SCMI_EVENT_PERFORMANCE_LIMITS_CHANGED,
1048 .max_payld_sz = sizeof(struct scmi_perf_limits_notify_payld),
1049 .max_report_sz = sizeof(struct scmi_perf_limits_report),
1052 .id = SCMI_EVENT_PERFORMANCE_LEVEL_CHANGED,
1053 .max_payld_sz = sizeof(struct scmi_perf_level_notify_payld),
1054 .max_report_sz = sizeof(struct scmi_perf_level_report),
1058 static const struct scmi_event_ops perf_event_ops = {
1059 .get_num_sources = scmi_perf_get_num_sources,
1060 .set_notify_enabled = scmi_perf_set_notify_enabled,
1061 .fill_custom_report = scmi_perf_fill_custom_report,
1064 static const struct scmi_protocol_events perf_protocol_events = {
1065 .queue_sz = SCMI_PROTO_QUEUE_SZ,
1066 .ops = &perf_event_ops,
1067 .evts = perf_events,
1068 .num_events = ARRAY_SIZE(perf_events),
1071 static int scmi_perf_protocol_init(const struct scmi_protocol_handle *ph)
1075 struct scmi_perf_info *pinfo;
1077 ret = ph->xops->version_get(ph, &version);
1081 dev_dbg(ph->dev, "Performance Version %d.%d\n",
1082 PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
1084 pinfo = devm_kzalloc(ph->dev, sizeof(*pinfo), GFP_KERNEL);
1088 pinfo->version = version;
1090 ret = scmi_perf_attributes_get(ph, pinfo);
1094 pinfo->dom_info = devm_kcalloc(ph->dev, pinfo->num_domains,
1095 sizeof(*pinfo->dom_info), GFP_KERNEL);
1096 if (!pinfo->dom_info)
1099 for (domain = 0; domain < pinfo->num_domains; domain++) {
1100 struct perf_dom_info *dom = pinfo->dom_info + domain;
1103 scmi_perf_domain_attributes_get(ph, dom, version);
1104 scmi_perf_describe_levels_get(ph, dom, version);
1106 if (dom->perf_fastchannels)
1107 scmi_perf_domain_init_fc(ph, dom->id, &dom->fc_info);
1110 ret = devm_add_action_or_reset(ph->dev, scmi_perf_xa_destroy, pinfo);
1114 return ph->set_priv(ph, pinfo);
1117 static const struct scmi_protocol scmi_perf = {
1118 .id = SCMI_PROTOCOL_PERF,
1119 .owner = THIS_MODULE,
1120 .instance_init = &scmi_perf_protocol_init,
1121 .ops = &perf_proto_ops,
1122 .events = &perf_protocol_events,
1125 DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(perf, scmi_perf)