GNU Linux-libre 6.7.9-gnu
[releases.git] / drivers / firmware / 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 #define MAX_OPPS                16
28
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,
34         PERF_LEVEL_SET = 0x7,
35         PERF_LEVEL_GET = 0x8,
36         PERF_NOTIFY_LIMITS = 0x9,
37         PERF_NOTIFY_LEVEL = 0xa,
38         PERF_DESCRIBE_FASTCHANNEL = 0xb,
39         PERF_DOMAIN_NAME_GET = 0xc,
40 };
41
42 enum {
43         PERF_FC_LEVEL,
44         PERF_FC_LIMIT,
45         PERF_FC_MAX,
46 };
47
48 struct scmi_opp {
49         u32 perf;
50         u32 power;
51         u32 trans_latency_us;
52         u32 indicative_freq;
53         u32 level_index;
54         struct hlist_node hash;
55 };
56
57 struct scmi_msg_resp_perf_attributes {
58         __le16 num_domains;
59         __le16 flags;
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;
64         __le32 stats_size;
65 };
66
67 struct scmi_msg_resp_perf_domain_attributes {
68         __le32 flags;
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))
76         __le32 rate_limit_us;
77         __le32 sustained_freq_khz;
78         __le32 sustained_perf_level;
79             u8 name[SCMI_SHORT_NAME_MAX_SIZE];
80 };
81
82 struct scmi_msg_perf_describe_levels {
83         __le32 domain;
84         __le32 level_index;
85 };
86
87 struct scmi_perf_set_limits {
88         __le32 domain;
89         __le32 max_level;
90         __le32 min_level;
91 };
92
93 struct scmi_perf_get_limits {
94         __le32 max_level;
95         __le32 min_level;
96 };
97
98 struct scmi_perf_set_level {
99         __le32 domain;
100         __le32 level;
101 };
102
103 struct scmi_perf_notify_level_or_limits {
104         __le32 domain;
105         __le32 notify_enable;
106 };
107
108 struct scmi_perf_limits_notify_payld {
109         __le32 agent_id;
110         __le32 domain_id;
111         __le32 range_max;
112         __le32 range_min;
113 };
114
115 struct scmi_perf_level_notify_payld {
116         __le32 agent_id;
117         __le32 domain_id;
118         __le32 performance_level;
119 };
120
121 struct scmi_msg_resp_perf_describe_levels {
122         __le16 num_returned;
123         __le16 num_remaining;
124         struct {
125                 __le32 perf_val;
126                 __le32 power;
127                 __le16 transition_latency_us;
128                 __le16 reserved;
129         } opp[];
130 };
131
132 struct scmi_msg_resp_perf_describe_levels_v4 {
133         __le16 num_returned;
134         __le16 num_remaining;
135         struct {
136                 __le32 perf_val;
137                 __le32 power;
138                 __le16 transition_latency_us;
139                 __le16 reserved;
140                 __le32 indicative_freq;
141                 __le32 level_index;
142         } opp[];
143 };
144
145 struct perf_dom_info {
146         u32 id;
147         bool set_limits;
148         bool perf_limit_notify;
149         bool perf_level_notify;
150         bool perf_fastchannels;
151         bool level_indexing_mode;
152         u32 opp_count;
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));
162 };
163
164 #define LOOKUP_BY_FREQ(__htp, __freq)                                   \
165 ({                                                                      \
166                 /* u32 cast is needed to pick right hash func */        \
167                 u32 f_ = (u32)(__freq);                                 \
168                 struct scmi_opp *_opp;                                  \
169                                                                         \
170                 hash_for_each_possible((__htp), _opp, hash, f_)         \
171                         if (_opp->indicative_freq == f_)                \
172                                 break;                                  \
173                 _opp;                                                   \
174 })
175
176 struct scmi_perf_info {
177         u32 version;
178         u16 num_domains;
179         enum scmi_power_scale power_scale;
180         u64 stats_addr;
181         u32 stats_size;
182         struct perf_dom_info *dom_info;
183 };
184
185 static enum scmi_performance_protocol_cmd evt_2_cmd[] = {
186         PERF_NOTIFY_LIMITS,
187         PERF_NOTIFY_LEVEL,
188 };
189
190 static int scmi_perf_attributes_get(const struct scmi_protocol_handle *ph,
191                                     struct scmi_perf_info *pi)
192 {
193         int ret;
194         struct scmi_xfer *t;
195         struct scmi_msg_resp_perf_attributes *attr;
196
197         ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES, 0,
198                                       sizeof(*attr), &t);
199         if (ret)
200                 return ret;
201
202         attr = t->rx.buf;
203
204         ret = ph->xops->do_xfer(ph, t);
205         if (!ret) {
206                 u16 flags = le16_to_cpu(attr->flags);
207
208                 pi->num_domains = le16_to_cpu(attr->num_domains);
209
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;
215
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);
219         }
220
221         ph->xops->xfer_put(ph, t);
222         return ret;
223 }
224
225 static void scmi_perf_xa_destroy(void *data)
226 {
227         int domain;
228         struct scmi_perf_info *pinfo = data;
229
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));
233         }
234 }
235
236 static int
237 scmi_perf_domain_attributes_get(const struct scmi_protocol_handle *ph,
238                                 struct perf_dom_info *dom_info,
239                                 u32 version)
240 {
241         int ret;
242         u32 flags;
243         struct scmi_xfer *t;
244         struct scmi_msg_resp_perf_domain_attributes *attr;
245
246         ret = ph->xops->xfer_get_init(ph, PERF_DOMAIN_ATTRIBUTES,
247                                       sizeof(dom_info->id), sizeof(*attr), &t);
248         if (ret)
249                 return ret;
250
251         put_unaligned_le32(dom_info->id, t->tx.buf);
252         attr = t->rx.buf;
253
254         ret = ph->xops->do_xfer(ph, t);
255         if (!ret) {
256                 flags = le32_to_cpu(attr->flags);
257
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;
275                 else
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);
281         }
282
283         ph->xops->xfer_put(ph, t);
284
285         /*
286          * If supported overwrite short name with the extended one;
287          * on error just carry on and use already provided short name.
288          */
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,
293                                             SCMI_MAX_STR_SIZE);
294
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);
299         }
300
301         return ret;
302 }
303
304 static int opp_cmp_func(const void *opp1, const void *opp2)
305 {
306         const struct scmi_opp *t1 = opp1, *t2 = opp2;
307
308         return t1->perf - t2->perf;
309 }
310
311 struct scmi_perf_ipriv {
312         u32 version;
313         struct perf_dom_info *perf_dom;
314 };
315
316 static void iter_perf_levels_prepare_message(void *message,
317                                              unsigned int desc_index,
318                                              const void *priv)
319 {
320         struct scmi_msg_perf_describe_levels *msg = message;
321         const struct scmi_perf_ipriv *p = priv;
322
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);
326 }
327
328 static int iter_perf_levels_update_state(struct scmi_iterator_state *st,
329                                          const void *response, void *priv)
330 {
331         const struct scmi_msg_resp_perf_describe_levels *r = response;
332
333         st->num_returned = le16_to_cpu(r->num_returned);
334         st->num_remaining = le16_to_cpu(r->num_remaining);
335
336         return 0;
337 }
338
339 static inline void
340 process_response_opp(struct scmi_opp *opp, unsigned int loop_idx,
341                      const struct scmi_msg_resp_perf_describe_levels *r)
342 {
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);
347 }
348
349 static inline void
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)
353 {
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);
358
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) {
362                 int ret;
363
364                 opp->level_index = le32_to_cpu(r->opp[loop_idx].level_index);
365
366                 ret = xa_insert(&dom->opps_by_idx, opp->level_index, opp,
367                                 GFP_KERNEL);
368                 if (ret)
369                         dev_warn(dev,
370                                  "Failed to add opps_by_idx at %d - ret:%d\n",
371                                  opp->level_index, ret);
372
373                 ret = xa_insert(&dom->opps_by_lvl, opp->perf, opp, GFP_KERNEL);
374                 if (ret)
375                         dev_warn(dev,
376                                  "Failed to add opps_by_lvl at %d - ret:%d\n",
377                                  opp->perf, ret);
378
379                 hash_add(dom->opps_by_freq, &opp->hash, opp->indicative_freq);
380         }
381 }
382
383 static int
384 iter_perf_levels_process_response(const struct scmi_protocol_handle *ph,
385                                   const void *response,
386                                   struct scmi_iterator_state *st, void *priv)
387 {
388         struct scmi_opp *opp;
389         struct scmi_perf_ipriv *p = priv;
390
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);
394         else
395                 process_response_opp_v4(ph->dev, p->perf_dom, opp, st->loop_idx,
396                                         response);
397         p->perf_dom->opp_count++;
398
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);
402
403         return 0;
404 }
405
406 static int
407 scmi_perf_describe_levels_get(const struct scmi_protocol_handle *ph,
408                               struct perf_dom_info *perf_dom, u32 version)
409 {
410         int ret;
411         void *iter;
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,
416         };
417         struct scmi_perf_ipriv ppriv = {
418                 .version = version,
419                 .perf_dom = perf_dom,
420         };
421
422         iter = ph->hops->iter_response_init(ph, &ops, MAX_OPPS,
423                                             PERF_DESCRIBE_LEVELS,
424                                             sizeof(struct scmi_msg_perf_describe_levels),
425                                             &ppriv);
426         if (IS_ERR(iter))
427                 return PTR_ERR(iter);
428
429         ret = ph->hops->iter_response_run(iter);
430         if (ret)
431                 return ret;
432
433         if (perf_dom->opp_count)
434                 sort(perf_dom->opp, perf_dom->opp_count,
435                      sizeof(struct scmi_opp), opp_cmp_func, NULL);
436
437         return ret;
438 }
439
440 static int scmi_perf_num_domains_get(const struct scmi_protocol_handle *ph)
441 {
442         struct scmi_perf_info *pi = ph->get_priv(ph);
443
444         return pi->num_domains;
445 }
446
447 static inline struct perf_dom_info *
448 scmi_perf_domain_lookup(const struct scmi_protocol_handle *ph, u32 domain)
449 {
450         struct scmi_perf_info *pi = ph->get_priv(ph);
451
452         if (domain >= pi->num_domains)
453                 return ERR_PTR(-EINVAL);
454
455         return pi->dom_info + domain;
456 }
457
458 static const struct scmi_perf_domain_info *
459 scmi_perf_info_get(const struct scmi_protocol_handle *ph, u32 domain)
460 {
461         struct perf_dom_info *dom;
462
463         dom = scmi_perf_domain_lookup(ph, domain);
464         if (IS_ERR(dom))
465                 return ERR_PTR(-EINVAL);
466
467         return &dom->info;
468 }
469
470 static int scmi_perf_msg_limits_set(const struct scmi_protocol_handle *ph,
471                                     u32 domain, u32 max_perf, u32 min_perf)
472 {
473         int ret;
474         struct scmi_xfer *t;
475         struct scmi_perf_set_limits *limits;
476
477         ret = ph->xops->xfer_get_init(ph, PERF_LIMITS_SET,
478                                       sizeof(*limits), 0, &t);
479         if (ret)
480                 return ret;
481
482         limits = t->tx.buf;
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);
486
487         ret = ph->xops->do_xfer(ph, t);
488
489         ph->xops->xfer_put(ph, t);
490         return ret;
491 }
492
493 static int __scmi_perf_limits_set(const struct scmi_protocol_handle *ph,
494                                   struct perf_dom_info *dom, u32 max_perf,
495                                   u32 min_perf)
496 {
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];
499
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);
505                 return 0;
506         }
507
508         return scmi_perf_msg_limits_set(ph, dom->id, max_perf, min_perf);
509 }
510
511 static int scmi_perf_limits_set(const struct scmi_protocol_handle *ph,
512                                 u32 domain, u32 max_perf, u32 min_perf)
513 {
514         struct scmi_perf_info *pi = ph->get_priv(ph);
515         struct perf_dom_info *dom;
516
517         dom = scmi_perf_domain_lookup(ph, domain);
518         if (IS_ERR(dom))
519                 return PTR_ERR(dom);
520
521         if (PROTOCOL_REV_MAJOR(pi->version) >= 0x3 && !max_perf && !min_perf)
522                 return -EINVAL;
523
524         if (dom->level_indexing_mode) {
525                 struct scmi_opp *opp;
526
527                 if (min_perf) {
528                         opp = xa_load(&dom->opps_by_lvl, min_perf);
529                         if (!opp)
530                                 return -EIO;
531
532                         min_perf = opp->level_index;
533                 }
534
535                 if (max_perf) {
536                         opp = xa_load(&dom->opps_by_lvl, max_perf);
537                         if (!opp)
538                                 return -EIO;
539
540                         max_perf = opp->level_index;
541                 }
542         }
543
544         return __scmi_perf_limits_set(ph, dom, max_perf, min_perf);
545 }
546
547 static int scmi_perf_msg_limits_get(const struct scmi_protocol_handle *ph,
548                                     u32 domain, u32 *max_perf, u32 *min_perf)
549 {
550         int ret;
551         struct scmi_xfer *t;
552         struct scmi_perf_get_limits *limits;
553
554         ret = ph->xops->xfer_get_init(ph, PERF_LIMITS_GET,
555                                       sizeof(__le32), 0, &t);
556         if (ret)
557                 return ret;
558
559         put_unaligned_le32(domain, t->tx.buf);
560
561         ret = ph->xops->do_xfer(ph, t);
562         if (!ret) {
563                 limits = t->rx.buf;
564
565                 *max_perf = le32_to_cpu(limits->max_level);
566                 *min_perf = le32_to_cpu(limits->min_level);
567         }
568
569         ph->xops->xfer_put(ph, t);
570         return ret;
571 }
572
573 static int __scmi_perf_limits_get(const struct scmi_protocol_handle *ph,
574                                   struct perf_dom_info *dom, u32 *max_perf,
575                                   u32 *min_perf)
576 {
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];
579
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);
584                 return 0;
585         }
586
587         return scmi_perf_msg_limits_get(ph, dom->id, max_perf, min_perf);
588 }
589
590 static int scmi_perf_limits_get(const struct scmi_protocol_handle *ph,
591                                 u32 domain, u32 *max_perf, u32 *min_perf)
592 {
593         int ret;
594         struct perf_dom_info *dom;
595
596         dom = scmi_perf_domain_lookup(ph, domain);
597         if (IS_ERR(dom))
598                 return PTR_ERR(dom);
599
600         ret = __scmi_perf_limits_get(ph, dom, max_perf, min_perf);
601         if (ret)
602                 return ret;
603
604         if (dom->level_indexing_mode) {
605                 struct scmi_opp *opp;
606
607                 opp = xa_load(&dom->opps_by_idx, *min_perf);
608                 if (!opp)
609                         return -EIO;
610
611                 *min_perf = opp->perf;
612
613                 opp = xa_load(&dom->opps_by_idx, *max_perf);
614                 if (!opp)
615                         return -EIO;
616
617                 *max_perf = opp->perf;
618         }
619
620         return 0;
621 }
622
623 static int scmi_perf_msg_level_set(const struct scmi_protocol_handle *ph,
624                                    u32 domain, u32 level, bool poll)
625 {
626         int ret;
627         struct scmi_xfer *t;
628         struct scmi_perf_set_level *lvl;
629
630         ret = ph->xops->xfer_get_init(ph, PERF_LEVEL_SET, sizeof(*lvl), 0, &t);
631         if (ret)
632                 return ret;
633
634         t->hdr.poll_completion = poll;
635         lvl = t->tx.buf;
636         lvl->domain = cpu_to_le32(domain);
637         lvl->level = cpu_to_le32(level);
638
639         ret = ph->xops->do_xfer(ph, t);
640
641         ph->xops->xfer_put(ph, t);
642         return ret;
643 }
644
645 static int __scmi_perf_level_set(const struct scmi_protocol_handle *ph,
646                                  struct perf_dom_info *dom, u32 level,
647                                  bool poll)
648 {
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];
651
652                 trace_scmi_fc_call(SCMI_PROTOCOL_PERF, PERF_LEVEL_SET,
653                                    dom->id, level, 0);
654                 iowrite32(level, fci->set_addr);
655                 ph->hops->fastchannel_db_ring(fci->set_db);
656                 return 0;
657         }
658
659         return scmi_perf_msg_level_set(ph, dom->id, level, poll);
660 }
661
662 static int scmi_perf_level_set(const struct scmi_protocol_handle *ph,
663                                u32 domain, u32 level, bool poll)
664 {
665         struct perf_dom_info *dom;
666
667         dom = scmi_perf_domain_lookup(ph, domain);
668         if (IS_ERR(dom))
669                 return PTR_ERR(dom);
670
671         if (dom->level_indexing_mode) {
672                 struct scmi_opp *opp;
673
674                 opp = xa_load(&dom->opps_by_lvl, level);
675                 if (!opp)
676                         return -EIO;
677
678                 level = opp->level_index;
679         }
680
681         return __scmi_perf_level_set(ph, dom, level, poll);
682 }
683
684 static int scmi_perf_msg_level_get(const struct scmi_protocol_handle *ph,
685                                    u32 domain, u32 *level, bool poll)
686 {
687         int ret;
688         struct scmi_xfer *t;
689
690         ret = ph->xops->xfer_get_init(ph, PERF_LEVEL_GET,
691                                      sizeof(u32), sizeof(u32), &t);
692         if (ret)
693                 return ret;
694
695         t->hdr.poll_completion = poll;
696         put_unaligned_le32(domain, t->tx.buf);
697
698         ret = ph->xops->do_xfer(ph, t);
699         if (!ret)
700                 *level = get_unaligned_le32(t->rx.buf);
701
702         ph->xops->xfer_put(ph, t);
703         return ret;
704 }
705
706 static int __scmi_perf_level_get(const struct scmi_protocol_handle *ph,
707                                  struct perf_dom_info *dom, u32 *level,
708                                  bool poll)
709 {
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,
713                                    dom->id, *level, 0);
714                 return 0;
715         }
716
717         return scmi_perf_msg_level_get(ph, dom->id, level, poll);
718 }
719
720 static int scmi_perf_level_get(const struct scmi_protocol_handle *ph,
721                                u32 domain, u32 *level, bool poll)
722 {
723         int ret;
724         struct perf_dom_info *dom;
725
726         dom = scmi_perf_domain_lookup(ph, domain);
727         if (IS_ERR(dom))
728                 return PTR_ERR(dom);
729
730         ret = __scmi_perf_level_get(ph, dom, level, poll);
731         if (ret)
732                 return ret;
733
734         if (dom->level_indexing_mode) {
735                 struct scmi_opp *opp;
736
737                 opp = xa_load(&dom->opps_by_idx, *level);
738                 if (!opp)
739                         return -EIO;
740
741                 *level = opp->perf;
742         }
743
744         return 0;
745 }
746
747 static int scmi_perf_level_limits_notify(const struct scmi_protocol_handle *ph,
748                                          u32 domain, int message_id,
749                                          bool enable)
750 {
751         int ret;
752         struct scmi_xfer *t;
753         struct scmi_perf_notify_level_or_limits *notify;
754
755         ret = ph->xops->xfer_get_init(ph, message_id, sizeof(*notify), 0, &t);
756         if (ret)
757                 return ret;
758
759         notify = t->tx.buf;
760         notify->domain = cpu_to_le32(domain);
761         notify->notify_enable = enable ? cpu_to_le32(BIT(0)) : 0;
762
763         ret = ph->xops->do_xfer(ph, t);
764
765         ph->xops->xfer_put(ph, t);
766         return ret;
767 }
768
769 static void scmi_perf_domain_init_fc(const struct scmi_protocol_handle *ph,
770                                      u32 domain, struct scmi_fc_info **p_fc)
771 {
772         struct scmi_fc_info *fc;
773
774         fc = devm_kcalloc(ph->dev, PERF_FC_MAX, sizeof(*fc), GFP_KERNEL);
775         if (!fc)
776                 return;
777
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);
782
783         ph->hops->fastchannel_init(ph, PERF_DESCRIBE_FASTCHANNEL,
784                                    PERF_LEVEL_GET, 4, domain,
785                                    &fc[PERF_FC_LEVEL].get_addr, NULL);
786
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);
791
792         ph->hops->fastchannel_init(ph, PERF_DESCRIBE_FASTCHANNEL,
793                                    PERF_LIMITS_GET, 8, domain,
794                                    &fc[PERF_FC_LIMIT].get_addr, NULL);
795
796         *p_fc = fc;
797 }
798
799 static int scmi_dvfs_device_opps_add(const struct scmi_protocol_handle *ph,
800                                      struct device *dev, u32 domain)
801 {
802         int idx, ret;
803         unsigned long freq;
804         struct dev_pm_opp_data data = {};
805         struct perf_dom_info *dom;
806
807         dom = scmi_perf_domain_lookup(ph, domain);
808         if (IS_ERR(dom))
809                 return PTR_ERR(dom);
810
811         for (idx = 0; idx < dom->opp_count; idx++) {
812                 if (!dom->level_indexing_mode)
813                         freq = dom->opp[idx].perf * dom->mult_factor;
814                 else
815                         freq = dom->opp[idx].indicative_freq * dom->mult_factor;
816
817                 data.level = dom->opp[idx].perf;
818                 data.freq = freq;
819
820                 ret = dev_pm_opp_add_dynamic(dev, &data);
821                 if (ret) {
822                         dev_warn(dev, "failed to add opp %luHz\n", freq);
823                         dev_pm_opp_remove_all_dynamic(dev);
824                         return ret;
825                 }
826
827                 dev_dbg(dev, "[%d][%s]:: Registered OPP[%d] %lu\n",
828                         domain, dom->info.name, idx, freq);
829         }
830         return 0;
831 }
832
833 static int
834 scmi_dvfs_transition_latency_get(const struct scmi_protocol_handle *ph,
835                                  u32 domain)
836 {
837         struct perf_dom_info *dom;
838
839         dom = scmi_perf_domain_lookup(ph, domain);
840         if (IS_ERR(dom))
841                 return PTR_ERR(dom);
842
843         /* uS to nS */
844         return dom->opp[dom->opp_count - 1].trans_latency_us * 1000;
845 }
846
847 static int scmi_dvfs_freq_set(const struct scmi_protocol_handle *ph, u32 domain,
848                               unsigned long freq, bool poll)
849 {
850         unsigned int level;
851         struct perf_dom_info *dom;
852
853         dom = scmi_perf_domain_lookup(ph, domain);
854         if (IS_ERR(dom))
855                 return PTR_ERR(dom);
856
857         if (!dom->level_indexing_mode) {
858                 level = freq / dom->mult_factor;
859         } else {
860                 struct scmi_opp *opp;
861
862                 opp = LOOKUP_BY_FREQ(dom->opps_by_freq,
863                                      freq / dom->mult_factor);
864                 if (!opp)
865                         return -EIO;
866
867                 level = opp->level_index;
868         }
869
870         return __scmi_perf_level_set(ph, dom, level, poll);
871 }
872
873 static int scmi_dvfs_freq_get(const struct scmi_protocol_handle *ph, u32 domain,
874                               unsigned long *freq, bool poll)
875 {
876         int ret;
877         u32 level;
878         struct perf_dom_info *dom;
879
880         dom = scmi_perf_domain_lookup(ph, domain);
881         if (IS_ERR(dom))
882                 return PTR_ERR(dom);
883
884         ret = __scmi_perf_level_get(ph, dom, &level, poll);
885         if (ret)
886                 return ret;
887
888         if (!dom->level_indexing_mode) {
889                 *freq = level * dom->mult_factor;
890         } else {
891                 struct scmi_opp *opp;
892
893                 opp = xa_load(&dom->opps_by_idx, level);
894                 if (!opp)
895                         return -EIO;
896
897                 *freq = opp->indicative_freq * dom->mult_factor;
898         }
899
900         return ret;
901 }
902
903 static int scmi_dvfs_est_power_get(const struct scmi_protocol_handle *ph,
904                                    u32 domain, unsigned long *freq,
905                                    unsigned long *power)
906 {
907         struct perf_dom_info *dom;
908         unsigned long opp_freq;
909         int idx, ret = -EINVAL;
910         struct scmi_opp *opp;
911
912         dom = scmi_perf_domain_lookup(ph, domain);
913         if (IS_ERR(dom))
914                 return PTR_ERR(dom);
915
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;
919                 else
920                         opp_freq = opp->indicative_freq * dom->mult_factor;
921
922                 if (opp_freq < *freq)
923                         continue;
924
925                 *freq = opp_freq;
926                 *power = opp->power;
927                 ret = 0;
928                 break;
929         }
930
931         return ret;
932 }
933
934 static bool scmi_fast_switch_possible(const struct scmi_protocol_handle *ph,
935                                       u32 domain)
936 {
937         struct perf_dom_info *dom;
938
939         dom = scmi_perf_domain_lookup(ph, domain);
940         if (IS_ERR(dom))
941                 return false;
942
943         return dom->fc_info && dom->fc_info[PERF_FC_LEVEL].set_addr;
944 }
945
946 static enum scmi_power_scale
947 scmi_power_scale_get(const struct scmi_protocol_handle *ph)
948 {
949         struct scmi_perf_info *pi = ph->get_priv(ph);
950
951         return pi->power_scale;
952 }
953
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,
968 };
969
970 static int scmi_perf_set_notify_enabled(const struct scmi_protocol_handle *ph,
971                                         u8 evt_id, u32 src_id, bool enable)
972 {
973         int ret, cmd_id;
974
975         if (evt_id >= ARRAY_SIZE(evt_2_cmd))
976                 return -EINVAL;
977
978         cmd_id = evt_2_cmd[evt_id];
979         ret = scmi_perf_level_limits_notify(ph, src_id, cmd_id, enable);
980         if (ret)
981                 pr_debug("FAIL_ENABLED - evt[%X] dom[%d] - ret:%d\n",
982                          evt_id, src_id, ret);
983
984         return ret;
985 }
986
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)
991 {
992         void *rep = NULL;
993
994         switch (evt_id) {
995         case SCMI_EVENT_PERFORMANCE_LIMITS_CHANGED:
996         {
997                 const struct scmi_perf_limits_notify_payld *p = payld;
998                 struct scmi_perf_limits_report *r = report;
999
1000                 if (sizeof(*p) != payld_sz)
1001                         break;
1002
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;
1009                 rep = r;
1010                 break;
1011         }
1012         case SCMI_EVENT_PERFORMANCE_LEVEL_CHANGED:
1013         {
1014                 const struct scmi_perf_level_notify_payld *p = payld;
1015                 struct scmi_perf_level_report *r = report;
1016
1017                 if (sizeof(*p) != payld_sz)
1018                         break;
1019
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;
1025                 rep = r;
1026                 break;
1027         }
1028         default:
1029                 break;
1030         }
1031
1032         return rep;
1033 }
1034
1035 static int scmi_perf_get_num_sources(const struct scmi_protocol_handle *ph)
1036 {
1037         struct scmi_perf_info *pi = ph->get_priv(ph);
1038
1039         if (!pi)
1040                 return -EINVAL;
1041
1042         return pi->num_domains;
1043 }
1044
1045 static const struct scmi_event perf_events[] = {
1046         {
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),
1050         },
1051         {
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),
1055         },
1056 };
1057
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,
1062 };
1063
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),
1069 };
1070
1071 static int scmi_perf_protocol_init(const struct scmi_protocol_handle *ph)
1072 {
1073         int domain, ret;
1074         u32 version;
1075         struct scmi_perf_info *pinfo;
1076
1077         ret = ph->xops->version_get(ph, &version);
1078         if (ret)
1079                 return ret;
1080
1081         dev_dbg(ph->dev, "Performance Version %d.%d\n",
1082                 PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
1083
1084         pinfo = devm_kzalloc(ph->dev, sizeof(*pinfo), GFP_KERNEL);
1085         if (!pinfo)
1086                 return -ENOMEM;
1087
1088         pinfo->version = version;
1089
1090         ret = scmi_perf_attributes_get(ph, pinfo);
1091         if (ret)
1092                 return ret;
1093
1094         pinfo->dom_info = devm_kcalloc(ph->dev, pinfo->num_domains,
1095                                        sizeof(*pinfo->dom_info), GFP_KERNEL);
1096         if (!pinfo->dom_info)
1097                 return -ENOMEM;
1098
1099         for (domain = 0; domain < pinfo->num_domains; domain++) {
1100                 struct perf_dom_info *dom = pinfo->dom_info + domain;
1101
1102                 dom->id = domain;
1103                 scmi_perf_domain_attributes_get(ph, dom, version);
1104                 scmi_perf_describe_levels_get(ph, dom, version);
1105
1106                 if (dom->perf_fastchannels)
1107                         scmi_perf_domain_init_fc(ph, dom->id, &dom->fc_info);
1108         }
1109
1110         ret = devm_add_action_or_reset(ph->dev, scmi_perf_xa_destroy, pinfo);
1111         if (ret)
1112                 return ret;
1113
1114         return ph->set_priv(ph, pinfo);
1115 }
1116
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,
1123 };
1124
1125 DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(perf, scmi_perf)