Mention branches and keyring.
[releases.git] / arm_scmi / perf.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * System Control and Management Interface (SCMI) Performance Protocol
4  *
5  * Copyright (C) 2018-2023 ARM Ltd.
6  */
7
8 #define pr_fmt(fmt) "SCMI Notifications PERF - " fmt
9
10 #include <linux/bits.h>
11 #include <linux/hashtable.h>
12 #include <linux/io.h>
13 #include <linux/log2.h>
14 #include <linux/module.h>
15 #include <linux/of.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>
21
22 #include <trace/events/scmi.h>
23
24 #include "protocols.h"
25 #include "notify.h"
26
27 /* Updated only after ALL the mandatory features for that version are merged */
28 #define SCMI_PROTOCOL_SUPPORTED_VERSION         0x40000
29
30 #define MAX_OPPS                32
31
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,
37         PERF_LEVEL_SET = 0x7,
38         PERF_LEVEL_GET = 0x8,
39         PERF_NOTIFY_LIMITS = 0x9,
40         PERF_NOTIFY_LEVEL = 0xa,
41         PERF_DESCRIBE_FASTCHANNEL = 0xb,
42         PERF_DOMAIN_NAME_GET = 0xc,
43 };
44
45 enum {
46         PERF_FC_LEVEL,
47         PERF_FC_LIMIT,
48         PERF_FC_MAX,
49 };
50
51 struct scmi_opp {
52         u32 perf;
53         u32 power;
54         u32 trans_latency_us;
55         u32 indicative_freq;
56         u32 level_index;
57         struct hlist_node hash;
58 };
59
60 struct scmi_msg_resp_perf_attributes {
61         __le16 num_domains;
62         __le16 flags;
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;
67         __le32 stats_size;
68 };
69
70 struct scmi_msg_resp_perf_domain_attributes {
71         __le32 flags;
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))
79         __le32 rate_limit_us;
80         __le32 sustained_freq_khz;
81         __le32 sustained_perf_level;
82             u8 name[SCMI_SHORT_NAME_MAX_SIZE];
83 };
84
85 struct scmi_msg_perf_describe_levels {
86         __le32 domain;
87         __le32 level_index;
88 };
89
90 struct scmi_perf_set_limits {
91         __le32 domain;
92         __le32 max_level;
93         __le32 min_level;
94 };
95
96 struct scmi_perf_get_limits {
97         __le32 max_level;
98         __le32 min_level;
99 };
100
101 struct scmi_perf_set_level {
102         __le32 domain;
103         __le32 level;
104 };
105
106 struct scmi_perf_notify_level_or_limits {
107         __le32 domain;
108         __le32 notify_enable;
109 };
110
111 struct scmi_perf_limits_notify_payld {
112         __le32 agent_id;
113         __le32 domain_id;
114         __le32 range_max;
115         __le32 range_min;
116 };
117
118 struct scmi_perf_level_notify_payld {
119         __le32 agent_id;
120         __le32 domain_id;
121         __le32 performance_level;
122 };
123
124 struct scmi_msg_resp_perf_describe_levels {
125         __le16 num_returned;
126         __le16 num_remaining;
127         struct {
128                 __le32 perf_val;
129                 __le32 power;
130                 __le16 transition_latency_us;
131                 __le16 reserved;
132         } opp[];
133 };
134
135 struct scmi_msg_resp_perf_describe_levels_v4 {
136         __le16 num_returned;
137         __le16 num_remaining;
138         struct {
139                 __le32 perf_val;
140                 __le32 power;
141                 __le16 transition_latency_us;
142                 __le16 reserved;
143                 __le32 indicative_freq;
144                 __le32 level_index;
145         } opp[];
146 };
147
148 struct perf_dom_info {
149         u32 id;
150         bool set_limits;
151         bool perf_limit_notify;
152         bool perf_level_notify;
153         bool perf_fastchannels;
154         bool level_indexing_mode;
155         u32 opp_count;
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));
165 };
166
167 #define LOOKUP_BY_FREQ(__htp, __freq)                                   \
168 ({                                                                      \
169                 /* u32 cast is needed to pick right hash func */        \
170                 u32 f_ = (u32)(__freq);                                 \
171                 struct scmi_opp *_opp;                                  \
172                                                                         \
173                 hash_for_each_possible((__htp), _opp, hash, f_)         \
174                         if (_opp->indicative_freq == f_)                \
175                                 break;                                  \
176                 _opp;                                                   \
177 })
178
179 struct scmi_perf_info {
180         u32 version;
181         u16 num_domains;
182         enum scmi_power_scale power_scale;
183         u64 stats_addr;
184         u32 stats_size;
185         struct perf_dom_info *dom_info;
186 };
187
188 static enum scmi_performance_protocol_cmd evt_2_cmd[] = {
189         PERF_NOTIFY_LIMITS,
190         PERF_NOTIFY_LEVEL,
191 };
192
193 static int scmi_perf_attributes_get(const struct scmi_protocol_handle *ph,
194                                     struct scmi_perf_info *pi)
195 {
196         int ret;
197         struct scmi_xfer *t;
198         struct scmi_msg_resp_perf_attributes *attr;
199
200         ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES, 0,
201                                       sizeof(*attr), &t);
202         if (ret)
203                 return ret;
204
205         attr = t->rx.buf;
206
207         ret = ph->xops->do_xfer(ph, t);
208         if (!ret) {
209                 u16 flags = le16_to_cpu(attr->flags);
210
211                 pi->num_domains = le16_to_cpu(attr->num_domains);
212
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;
218
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);
222         }
223
224         ph->xops->xfer_put(ph, t);
225         return ret;
226 }
227
228 static void scmi_perf_xa_destroy(void *data)
229 {
230         int domain;
231         struct scmi_perf_info *pinfo = data;
232
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));
236         }
237 }
238
239 static int
240 scmi_perf_domain_attributes_get(const struct scmi_protocol_handle *ph,
241                                 struct perf_dom_info *dom_info,
242                                 u32 version)
243 {
244         int ret;
245         u32 flags;
246         struct scmi_xfer *t;
247         struct scmi_msg_resp_perf_domain_attributes *attr;
248
249         ret = ph->xops->xfer_get_init(ph, PERF_DOMAIN_ATTRIBUTES,
250                                       sizeof(dom_info->id), sizeof(*attr), &t);
251         if (ret)
252                 return ret;
253
254         put_unaligned_le32(dom_info->id, t->tx.buf);
255         attr = t->rx.buf;
256
257         ret = ph->xops->do_xfer(ph, t);
258         if (!ret) {
259                 flags = le32_to_cpu(attr->flags);
260
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;
278                 else
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);
284         }
285
286         ph->xops->xfer_put(ph, t);
287
288         /*
289          * If supported overwrite short name with the extended one;
290          * on error just carry on and use already provided short name.
291          */
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,
296                                             SCMI_MAX_STR_SIZE);
297
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);
302         }
303
304         return ret;
305 }
306
307 static int opp_cmp_func(const void *opp1, const void *opp2)
308 {
309         const struct scmi_opp *t1 = opp1, *t2 = opp2;
310
311         return t1->perf - t2->perf;
312 }
313
314 struct scmi_perf_ipriv {
315         u32 version;
316         struct perf_dom_info *perf_dom;
317 };
318
319 static void iter_perf_levels_prepare_message(void *message,
320                                              unsigned int desc_index,
321                                              const void *priv)
322 {
323         struct scmi_msg_perf_describe_levels *msg = message;
324         const struct scmi_perf_ipriv *p = priv;
325
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);
329 }
330
331 static int iter_perf_levels_update_state(struct scmi_iterator_state *st,
332                                          const void *response, void *priv)
333 {
334         const struct scmi_msg_resp_perf_describe_levels *r = response;
335
336         st->num_returned = le16_to_cpu(r->num_returned);
337         st->num_remaining = le16_to_cpu(r->num_remaining);
338
339         return 0;
340 }
341
342 static inline void
343 process_response_opp(struct scmi_opp *opp, unsigned int loop_idx,
344                      const struct scmi_msg_resp_perf_describe_levels *r)
345 {
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);
350 }
351
352 static inline void
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)
356 {
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);
361
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) {
365                 int ret;
366
367                 opp->level_index = le32_to_cpu(r->opp[loop_idx].level_index);
368
369                 ret = xa_insert(&dom->opps_by_idx, opp->level_index, opp,
370                                 GFP_KERNEL);
371                 if (ret)
372                         dev_warn(dev,
373                                  "Failed to add opps_by_idx at %d - ret:%d\n",
374                                  opp->level_index, ret);
375
376                 ret = xa_insert(&dom->opps_by_lvl, opp->perf, opp, GFP_KERNEL);
377                 if (ret)
378                         dev_warn(dev,
379                                  "Failed to add opps_by_lvl at %d - ret:%d\n",
380                                  opp->perf, ret);
381
382                 hash_add(dom->opps_by_freq, &opp->hash, opp->indicative_freq);
383         }
384 }
385
386 static int
387 iter_perf_levels_process_response(const struct scmi_protocol_handle *ph,
388                                   const void *response,
389                                   struct scmi_iterator_state *st, void *priv)
390 {
391         struct scmi_opp *opp;
392         struct scmi_perf_ipriv *p = priv;
393
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);
397         else
398                 process_response_opp_v4(ph->dev, p->perf_dom, opp, st->loop_idx,
399                                         response);
400         p->perf_dom->opp_count++;
401
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);
405
406         return 0;
407 }
408
409 static int
410 scmi_perf_describe_levels_get(const struct scmi_protocol_handle *ph,
411                               struct perf_dom_info *perf_dom, u32 version)
412 {
413         int ret;
414         void *iter;
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,
419         };
420         struct scmi_perf_ipriv ppriv = {
421                 .version = version,
422                 .perf_dom = perf_dom,
423         };
424
425         iter = ph->hops->iter_response_init(ph, &ops, MAX_OPPS,
426                                             PERF_DESCRIBE_LEVELS,
427                                             sizeof(struct scmi_msg_perf_describe_levels),
428                                             &ppriv);
429         if (IS_ERR(iter))
430                 return PTR_ERR(iter);
431
432         ret = ph->hops->iter_response_run(iter);
433         if (ret)
434                 return ret;
435
436         if (perf_dom->opp_count)
437                 sort(perf_dom->opp, perf_dom->opp_count,
438                      sizeof(struct scmi_opp), opp_cmp_func, NULL);
439
440         return ret;
441 }
442
443 static int scmi_perf_num_domains_get(const struct scmi_protocol_handle *ph)
444 {
445         struct scmi_perf_info *pi = ph->get_priv(ph);
446
447         return pi->num_domains;
448 }
449
450 static inline struct perf_dom_info *
451 scmi_perf_domain_lookup(const struct scmi_protocol_handle *ph, u32 domain)
452 {
453         struct scmi_perf_info *pi = ph->get_priv(ph);
454
455         if (domain >= pi->num_domains)
456                 return ERR_PTR(-EINVAL);
457
458         return pi->dom_info + domain;
459 }
460
461 static const struct scmi_perf_domain_info *
462 scmi_perf_info_get(const struct scmi_protocol_handle *ph, u32 domain)
463 {
464         struct perf_dom_info *dom;
465
466         dom = scmi_perf_domain_lookup(ph, domain);
467         if (IS_ERR(dom))
468                 return ERR_PTR(-EINVAL);
469
470         return &dom->info;
471 }
472
473 static int scmi_perf_msg_limits_set(const struct scmi_protocol_handle *ph,
474                                     u32 domain, u32 max_perf, u32 min_perf)
475 {
476         int ret;
477         struct scmi_xfer *t;
478         struct scmi_perf_set_limits *limits;
479
480         ret = ph->xops->xfer_get_init(ph, PERF_LIMITS_SET,
481                                       sizeof(*limits), 0, &t);
482         if (ret)
483                 return ret;
484
485         limits = t->tx.buf;
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);
489
490         ret = ph->xops->do_xfer(ph, t);
491
492         ph->xops->xfer_put(ph, t);
493         return ret;
494 }
495
496 static int __scmi_perf_limits_set(const struct scmi_protocol_handle *ph,
497                                   struct perf_dom_info *dom, u32 max_perf,
498                                   u32 min_perf)
499 {
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];
502
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);
508                 return 0;
509         }
510
511         return scmi_perf_msg_limits_set(ph, dom->id, max_perf, min_perf);
512 }
513
514 static int scmi_perf_limits_set(const struct scmi_protocol_handle *ph,
515                                 u32 domain, u32 max_perf, u32 min_perf)
516 {
517         struct scmi_perf_info *pi = ph->get_priv(ph);
518         struct perf_dom_info *dom;
519
520         dom = scmi_perf_domain_lookup(ph, domain);
521         if (IS_ERR(dom))
522                 return PTR_ERR(dom);
523
524         if (!dom->set_limits)
525                 return -EOPNOTSUPP;
526
527         if (PROTOCOL_REV_MAJOR(pi->version) >= 0x3 && !max_perf && !min_perf)
528                 return -EINVAL;
529
530         if (dom->level_indexing_mode) {
531                 struct scmi_opp *opp;
532
533                 if (min_perf) {
534                         opp = xa_load(&dom->opps_by_lvl, min_perf);
535                         if (!opp)
536                                 return -EIO;
537
538                         min_perf = opp->level_index;
539                 }
540
541                 if (max_perf) {
542                         opp = xa_load(&dom->opps_by_lvl, max_perf);
543                         if (!opp)
544                                 return -EIO;
545
546                         max_perf = opp->level_index;
547                 }
548         }
549
550         return __scmi_perf_limits_set(ph, dom, max_perf, min_perf);
551 }
552
553 static int scmi_perf_msg_limits_get(const struct scmi_protocol_handle *ph,
554                                     u32 domain, u32 *max_perf, u32 *min_perf)
555 {
556         int ret;
557         struct scmi_xfer *t;
558         struct scmi_perf_get_limits *limits;
559
560         ret = ph->xops->xfer_get_init(ph, PERF_LIMITS_GET,
561                                       sizeof(__le32), 0, &t);
562         if (ret)
563                 return ret;
564
565         put_unaligned_le32(domain, t->tx.buf);
566
567         ret = ph->xops->do_xfer(ph, t);
568         if (!ret) {
569                 limits = t->rx.buf;
570
571                 *max_perf = le32_to_cpu(limits->max_level);
572                 *min_perf = le32_to_cpu(limits->min_level);
573         }
574
575         ph->xops->xfer_put(ph, t);
576         return ret;
577 }
578
579 static int __scmi_perf_limits_get(const struct scmi_protocol_handle *ph,
580                                   struct perf_dom_info *dom, u32 *max_perf,
581                                   u32 *min_perf)
582 {
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];
585
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);
590                 return 0;
591         }
592
593         return scmi_perf_msg_limits_get(ph, dom->id, max_perf, min_perf);
594 }
595
596 static int scmi_perf_limits_get(const struct scmi_protocol_handle *ph,
597                                 u32 domain, u32 *max_perf, u32 *min_perf)
598 {
599         int ret;
600         struct perf_dom_info *dom;
601
602         dom = scmi_perf_domain_lookup(ph, domain);
603         if (IS_ERR(dom))
604                 return PTR_ERR(dom);
605
606         ret = __scmi_perf_limits_get(ph, dom, max_perf, min_perf);
607         if (ret)
608                 return ret;
609
610         if (dom->level_indexing_mode) {
611                 struct scmi_opp *opp;
612
613                 opp = xa_load(&dom->opps_by_idx, *min_perf);
614                 if (!opp)
615                         return -EIO;
616
617                 *min_perf = opp->perf;
618
619                 opp = xa_load(&dom->opps_by_idx, *max_perf);
620                 if (!opp)
621                         return -EIO;
622
623                 *max_perf = opp->perf;
624         }
625
626         return 0;
627 }
628
629 static int scmi_perf_msg_level_set(const struct scmi_protocol_handle *ph,
630                                    u32 domain, u32 level, bool poll)
631 {
632         int ret;
633         struct scmi_xfer *t;
634         struct scmi_perf_set_level *lvl;
635
636         ret = ph->xops->xfer_get_init(ph, PERF_LEVEL_SET, sizeof(*lvl), 0, &t);
637         if (ret)
638                 return ret;
639
640         t->hdr.poll_completion = poll;
641         lvl = t->tx.buf;
642         lvl->domain = cpu_to_le32(domain);
643         lvl->level = cpu_to_le32(level);
644
645         ret = ph->xops->do_xfer(ph, t);
646
647         ph->xops->xfer_put(ph, t);
648         return ret;
649 }
650
651 static int __scmi_perf_level_set(const struct scmi_protocol_handle *ph,
652                                  struct perf_dom_info *dom, u32 level,
653                                  bool poll)
654 {
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];
657
658                 trace_scmi_fc_call(SCMI_PROTOCOL_PERF, PERF_LEVEL_SET,
659                                    dom->id, level, 0);
660                 iowrite32(level, fci->set_addr);
661                 ph->hops->fastchannel_db_ring(fci->set_db);
662                 return 0;
663         }
664
665         return scmi_perf_msg_level_set(ph, dom->id, level, poll);
666 }
667
668 static int scmi_perf_level_set(const struct scmi_protocol_handle *ph,
669                                u32 domain, u32 level, bool poll)
670 {
671         struct perf_dom_info *dom;
672
673         dom = scmi_perf_domain_lookup(ph, domain);
674         if (IS_ERR(dom))
675                 return PTR_ERR(dom);
676
677         if (!dom->info.set_perf)
678                 return -EOPNOTSUPP;
679
680         if (dom->level_indexing_mode) {
681                 struct scmi_opp *opp;
682
683                 opp = xa_load(&dom->opps_by_lvl, level);
684                 if (!opp)
685                         return -EIO;
686
687                 level = opp->level_index;
688         }
689
690         return __scmi_perf_level_set(ph, dom, level, poll);
691 }
692
693 static int scmi_perf_msg_level_get(const struct scmi_protocol_handle *ph,
694                                    u32 domain, u32 *level, bool poll)
695 {
696         int ret;
697         struct scmi_xfer *t;
698
699         ret = ph->xops->xfer_get_init(ph, PERF_LEVEL_GET,
700                                      sizeof(u32), sizeof(u32), &t);
701         if (ret)
702                 return ret;
703
704         t->hdr.poll_completion = poll;
705         put_unaligned_le32(domain, t->tx.buf);
706
707         ret = ph->xops->do_xfer(ph, t);
708         if (!ret)
709                 *level = get_unaligned_le32(t->rx.buf);
710
711         ph->xops->xfer_put(ph, t);
712         return ret;
713 }
714
715 static int __scmi_perf_level_get(const struct scmi_protocol_handle *ph,
716                                  struct perf_dom_info *dom, u32 *level,
717                                  bool poll)
718 {
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,
722                                    dom->id, *level, 0);
723                 return 0;
724         }
725
726         return scmi_perf_msg_level_get(ph, dom->id, level, poll);
727 }
728
729 static int scmi_perf_level_get(const struct scmi_protocol_handle *ph,
730                                u32 domain, u32 *level, bool poll)
731 {
732         int ret;
733         struct perf_dom_info *dom;
734
735         dom = scmi_perf_domain_lookup(ph, domain);
736         if (IS_ERR(dom))
737                 return PTR_ERR(dom);
738
739         ret = __scmi_perf_level_get(ph, dom, level, poll);
740         if (ret)
741                 return ret;
742
743         if (dom->level_indexing_mode) {
744                 struct scmi_opp *opp;
745
746                 opp = xa_load(&dom->opps_by_idx, *level);
747                 if (!opp)
748                         return -EIO;
749
750                 *level = opp->perf;
751         }
752
753         return 0;
754 }
755
756 static int scmi_perf_level_limits_notify(const struct scmi_protocol_handle *ph,
757                                          u32 domain, int message_id,
758                                          bool enable)
759 {
760         int ret;
761         struct scmi_xfer *t;
762         struct scmi_perf_notify_level_or_limits *notify;
763
764         ret = ph->xops->xfer_get_init(ph, message_id, sizeof(*notify), 0, &t);
765         if (ret)
766                 return ret;
767
768         notify = t->tx.buf;
769         notify->domain = cpu_to_le32(domain);
770         notify->notify_enable = enable ? cpu_to_le32(BIT(0)) : 0;
771
772         ret = ph->xops->do_xfer(ph, t);
773
774         ph->xops->xfer_put(ph, t);
775         return ret;
776 }
777
778 static void scmi_perf_domain_init_fc(const struct scmi_protocol_handle *ph,
779                                      struct perf_dom_info *dom)
780 {
781         struct scmi_fc_info *fc;
782
783         fc = devm_kcalloc(ph->dev, PERF_FC_MAX, sizeof(*fc), GFP_KERNEL);
784         if (!fc)
785                 return;
786
787         ph->hops->fastchannel_init(ph, PERF_DESCRIBE_FASTCHANNEL,
788                                    PERF_LEVEL_GET, 4, dom->id,
789                                    &fc[PERF_FC_LEVEL].get_addr, NULL);
790
791         ph->hops->fastchannel_init(ph, PERF_DESCRIBE_FASTCHANNEL,
792                                    PERF_LIMITS_GET, 8, dom->id,
793                                    &fc[PERF_FC_LIMIT].get_addr, NULL);
794
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);
800
801         if (dom->set_limits)
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);
806
807         dom->fc_info = fc;
808 }
809
810 static int scmi_dvfs_device_opps_add(const struct scmi_protocol_handle *ph,
811                                      struct device *dev, u32 domain)
812 {
813         int idx, ret;
814         unsigned long freq;
815         struct dev_pm_opp_data data = {};
816         struct perf_dom_info *dom;
817
818         dom = scmi_perf_domain_lookup(ph, domain);
819         if (IS_ERR(dom))
820                 return PTR_ERR(dom);
821
822         for (idx = 0; idx < dom->opp_count; idx++) {
823                 if (!dom->level_indexing_mode)
824                         freq = dom->opp[idx].perf * dom->mult_factor;
825                 else
826                         freq = dom->opp[idx].indicative_freq * dom->mult_factor;
827
828                 data.level = dom->opp[idx].perf;
829                 data.freq = freq;
830
831                 ret = dev_pm_opp_add_dynamic(dev, &data);
832                 if (ret) {
833                         dev_warn(dev, "failed to add opp %luHz\n", freq);
834                         dev_pm_opp_remove_all_dynamic(dev);
835                         return ret;
836                 }
837
838                 dev_dbg(dev, "[%d][%s]:: Registered OPP[%d] %lu\n",
839                         domain, dom->info.name, idx, freq);
840         }
841         return 0;
842 }
843
844 static int
845 scmi_dvfs_transition_latency_get(const struct scmi_protocol_handle *ph,
846                                  u32 domain)
847 {
848         struct perf_dom_info *dom;
849
850         dom = scmi_perf_domain_lookup(ph, domain);
851         if (IS_ERR(dom))
852                 return PTR_ERR(dom);
853
854         /* uS to nS */
855         return dom->opp[dom->opp_count - 1].trans_latency_us * 1000;
856 }
857
858 static int scmi_dvfs_freq_set(const struct scmi_protocol_handle *ph, u32 domain,
859                               unsigned long freq, bool poll)
860 {
861         unsigned int level;
862         struct perf_dom_info *dom;
863
864         dom = scmi_perf_domain_lookup(ph, domain);
865         if (IS_ERR(dom))
866                 return PTR_ERR(dom);
867
868         if (!dom->level_indexing_mode) {
869                 level = freq / dom->mult_factor;
870         } else {
871                 struct scmi_opp *opp;
872
873                 opp = LOOKUP_BY_FREQ(dom->opps_by_freq,
874                                      freq / dom->mult_factor);
875                 if (!opp)
876                         return -EIO;
877
878                 level = opp->level_index;
879         }
880
881         return __scmi_perf_level_set(ph, dom, level, poll);
882 }
883
884 static int scmi_dvfs_freq_get(const struct scmi_protocol_handle *ph, u32 domain,
885                               unsigned long *freq, bool poll)
886 {
887         int ret;
888         u32 level;
889         struct perf_dom_info *dom;
890
891         dom = scmi_perf_domain_lookup(ph, domain);
892         if (IS_ERR(dom))
893                 return PTR_ERR(dom);
894
895         ret = __scmi_perf_level_get(ph, dom, &level, poll);
896         if (ret)
897                 return ret;
898
899         if (!dom->level_indexing_mode) {
900                 *freq = level * dom->mult_factor;
901         } else {
902                 struct scmi_opp *opp;
903
904                 opp = xa_load(&dom->opps_by_idx, level);
905                 if (!opp)
906                         return -EIO;
907
908                 *freq = opp->indicative_freq * dom->mult_factor;
909         }
910
911         return ret;
912 }
913
914 static int scmi_dvfs_est_power_get(const struct scmi_protocol_handle *ph,
915                                    u32 domain, unsigned long *freq,
916                                    unsigned long *power)
917 {
918         struct perf_dom_info *dom;
919         unsigned long opp_freq;
920         int idx, ret = -EINVAL;
921         struct scmi_opp *opp;
922
923         dom = scmi_perf_domain_lookup(ph, domain);
924         if (IS_ERR(dom))
925                 return PTR_ERR(dom);
926
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;
930                 else
931                         opp_freq = opp->indicative_freq * dom->mult_factor;
932
933                 if (opp_freq < *freq)
934                         continue;
935
936                 *freq = opp_freq;
937                 *power = opp->power;
938                 ret = 0;
939                 break;
940         }
941
942         return ret;
943 }
944
945 static bool scmi_fast_switch_possible(const struct scmi_protocol_handle *ph,
946                                       u32 domain)
947 {
948         struct perf_dom_info *dom;
949
950         dom = scmi_perf_domain_lookup(ph, domain);
951         if (IS_ERR(dom))
952                 return false;
953
954         return dom->fc_info && dom->fc_info[PERF_FC_LEVEL].set_addr;
955 }
956
957 static enum scmi_power_scale
958 scmi_power_scale_get(const struct scmi_protocol_handle *ph)
959 {
960         struct scmi_perf_info *pi = ph->get_priv(ph);
961
962         return pi->power_scale;
963 }
964
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,
979 };
980
981 static int scmi_perf_set_notify_enabled(const struct scmi_protocol_handle *ph,
982                                         u8 evt_id, u32 src_id, bool enable)
983 {
984         int ret, cmd_id;
985
986         if (evt_id >= ARRAY_SIZE(evt_2_cmd))
987                 return -EINVAL;
988
989         cmd_id = evt_2_cmd[evt_id];
990         ret = scmi_perf_level_limits_notify(ph, src_id, cmd_id, enable);
991         if (ret)
992                 pr_debug("FAIL_ENABLED - evt[%X] dom[%d] - ret:%d\n",
993                          evt_id, src_id, ret);
994
995         return ret;
996 }
997
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)
1002 {
1003         void *rep = NULL;
1004
1005         switch (evt_id) {
1006         case SCMI_EVENT_PERFORMANCE_LIMITS_CHANGED:
1007         {
1008                 const struct scmi_perf_limits_notify_payld *p = payld;
1009                 struct scmi_perf_limits_report *r = report;
1010
1011                 if (sizeof(*p) != payld_sz)
1012                         break;
1013
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;
1020                 rep = r;
1021                 break;
1022         }
1023         case SCMI_EVENT_PERFORMANCE_LEVEL_CHANGED:
1024         {
1025                 const struct scmi_perf_level_notify_payld *p = payld;
1026                 struct scmi_perf_level_report *r = report;
1027
1028                 if (sizeof(*p) != payld_sz)
1029                         break;
1030
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;
1036                 rep = r;
1037                 break;
1038         }
1039         default:
1040                 break;
1041         }
1042
1043         return rep;
1044 }
1045
1046 static int scmi_perf_get_num_sources(const struct scmi_protocol_handle *ph)
1047 {
1048         struct scmi_perf_info *pi = ph->get_priv(ph);
1049
1050         if (!pi)
1051                 return -EINVAL;
1052
1053         return pi->num_domains;
1054 }
1055
1056 static const struct scmi_event perf_events[] = {
1057         {
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),
1061         },
1062         {
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),
1066         },
1067 };
1068
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,
1073 };
1074
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),
1080 };
1081
1082 static int scmi_perf_protocol_init(const struct scmi_protocol_handle *ph)
1083 {
1084         int domain, ret;
1085         u32 version;
1086         struct scmi_perf_info *pinfo;
1087
1088         ret = ph->xops->version_get(ph, &version);
1089         if (ret)
1090                 return ret;
1091
1092         dev_dbg(ph->dev, "Performance Version %d.%d\n",
1093                 PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
1094
1095         pinfo = devm_kzalloc(ph->dev, sizeof(*pinfo), GFP_KERNEL);
1096         if (!pinfo)
1097                 return -ENOMEM;
1098
1099         pinfo->version = version;
1100
1101         ret = scmi_perf_attributes_get(ph, pinfo);
1102         if (ret)
1103                 return ret;
1104
1105         pinfo->dom_info = devm_kcalloc(ph->dev, pinfo->num_domains,
1106                                        sizeof(*pinfo->dom_info), GFP_KERNEL);
1107         if (!pinfo->dom_info)
1108                 return -ENOMEM;
1109
1110         for (domain = 0; domain < pinfo->num_domains; domain++) {
1111                 struct perf_dom_info *dom = pinfo->dom_info + domain;
1112
1113                 dom->id = domain;
1114                 scmi_perf_domain_attributes_get(ph, dom, version);
1115                 scmi_perf_describe_levels_get(ph, dom, version);
1116
1117                 if (dom->perf_fastchannels)
1118                         scmi_perf_domain_init_fc(ph, dom);
1119         }
1120
1121         ret = devm_add_action_or_reset(ph->dev, scmi_perf_xa_destroy, pinfo);
1122         if (ret)
1123                 return ret;
1124
1125         return ph->set_priv(ph, pinfo, version);
1126 }
1127
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,
1135 };
1136
1137 DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(perf, scmi_perf)