Merge tag 'nolibc.2022.07.27a' of git://git.kernel.org/pub/scm/linux/kernel/git/paulm...
[linux-block.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-2022 ARM Ltd.
6  */
7
8 #define pr_fmt(fmt) "SCMI Notifications PERF - " fmt
9
10 #include <linux/bits.h>
11 #include <linux/of.h>
12 #include <linux/io.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_opp.h>
16 #include <linux/scmi_protocol.h>
17 #include <linux/sort.h>
18
19 #include <trace/events/scmi.h>
20
21 #include "protocols.h"
22 #include "notify.h"
23
24 #define MAX_OPPS                16
25
26 enum scmi_performance_protocol_cmd {
27         PERF_DOMAIN_ATTRIBUTES = 0x3,
28         PERF_DESCRIBE_LEVELS = 0x4,
29         PERF_LIMITS_SET = 0x5,
30         PERF_LIMITS_GET = 0x6,
31         PERF_LEVEL_SET = 0x7,
32         PERF_LEVEL_GET = 0x8,
33         PERF_NOTIFY_LIMITS = 0x9,
34         PERF_NOTIFY_LEVEL = 0xa,
35         PERF_DESCRIBE_FASTCHANNEL = 0xb,
36         PERF_DOMAIN_NAME_GET = 0xc,
37 };
38
39 enum {
40         PERF_FC_LEVEL,
41         PERF_FC_LIMIT,
42         PERF_FC_MAX,
43 };
44
45 struct scmi_opp {
46         u32 perf;
47         u32 power;
48         u32 trans_latency_us;
49 };
50
51 struct scmi_msg_resp_perf_attributes {
52         __le16 num_domains;
53         __le16 flags;
54 #define POWER_SCALE_IN_MILLIWATT(x)     ((x) & BIT(0))
55 #define POWER_SCALE_IN_MICROWATT(x)     ((x) & BIT(1))
56         __le32 stats_addr_low;
57         __le32 stats_addr_high;
58         __le32 stats_size;
59 };
60
61 struct scmi_msg_resp_perf_domain_attributes {
62         __le32 flags;
63 #define SUPPORTS_SET_LIMITS(x)          ((x) & BIT(31))
64 #define SUPPORTS_SET_PERF_LVL(x)        ((x) & BIT(30))
65 #define SUPPORTS_PERF_LIMIT_NOTIFY(x)   ((x) & BIT(29))
66 #define SUPPORTS_PERF_LEVEL_NOTIFY(x)   ((x) & BIT(28))
67 #define SUPPORTS_PERF_FASTCHANNELS(x)   ((x) & BIT(27))
68 #define SUPPORTS_EXTENDED_NAMES(x)      ((x) & BIT(26))
69         __le32 rate_limit_us;
70         __le32 sustained_freq_khz;
71         __le32 sustained_perf_level;
72             u8 name[SCMI_SHORT_NAME_MAX_SIZE];
73 };
74
75 struct scmi_msg_perf_describe_levels {
76         __le32 domain;
77         __le32 level_index;
78 };
79
80 struct scmi_perf_set_limits {
81         __le32 domain;
82         __le32 max_level;
83         __le32 min_level;
84 };
85
86 struct scmi_perf_get_limits {
87         __le32 max_level;
88         __le32 min_level;
89 };
90
91 struct scmi_perf_set_level {
92         __le32 domain;
93         __le32 level;
94 };
95
96 struct scmi_perf_notify_level_or_limits {
97         __le32 domain;
98         __le32 notify_enable;
99 };
100
101 struct scmi_perf_limits_notify_payld {
102         __le32 agent_id;
103         __le32 domain_id;
104         __le32 range_max;
105         __le32 range_min;
106 };
107
108 struct scmi_perf_level_notify_payld {
109         __le32 agent_id;
110         __le32 domain_id;
111         __le32 performance_level;
112 };
113
114 struct scmi_msg_resp_perf_describe_levels {
115         __le16 num_returned;
116         __le16 num_remaining;
117         struct {
118                 __le32 perf_val;
119                 __le32 power;
120                 __le16 transition_latency_us;
121                 __le16 reserved;
122         } opp[];
123 };
124
125 struct perf_dom_info {
126         bool set_limits;
127         bool set_perf;
128         bool perf_limit_notify;
129         bool perf_level_notify;
130         bool perf_fastchannels;
131         u32 opp_count;
132         u32 sustained_freq_khz;
133         u32 sustained_perf_level;
134         u32 mult_factor;
135         char name[SCMI_MAX_STR_SIZE];
136         struct scmi_opp opp[MAX_OPPS];
137         struct scmi_fc_info *fc_info;
138 };
139
140 struct scmi_perf_info {
141         u32 version;
142         int num_domains;
143         enum scmi_power_scale power_scale;
144         u64 stats_addr;
145         u32 stats_size;
146         struct perf_dom_info *dom_info;
147 };
148
149 static enum scmi_performance_protocol_cmd evt_2_cmd[] = {
150         PERF_NOTIFY_LIMITS,
151         PERF_NOTIFY_LEVEL,
152 };
153
154 static int scmi_perf_attributes_get(const struct scmi_protocol_handle *ph,
155                                     struct scmi_perf_info *pi)
156 {
157         int ret;
158         struct scmi_xfer *t;
159         struct scmi_msg_resp_perf_attributes *attr;
160
161         ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES, 0,
162                                       sizeof(*attr), &t);
163         if (ret)
164                 return ret;
165
166         attr = t->rx.buf;
167
168         ret = ph->xops->do_xfer(ph, t);
169         if (!ret) {
170                 u16 flags = le16_to_cpu(attr->flags);
171
172                 pi->num_domains = le16_to_cpu(attr->num_domains);
173
174                 if (POWER_SCALE_IN_MILLIWATT(flags))
175                         pi->power_scale = SCMI_POWER_MILLIWATTS;
176                 if (PROTOCOL_REV_MAJOR(pi->version) >= 0x3)
177                         if (POWER_SCALE_IN_MICROWATT(flags))
178                                 pi->power_scale = SCMI_POWER_MICROWATTS;
179
180                 pi->stats_addr = le32_to_cpu(attr->stats_addr_low) |
181                                 (u64)le32_to_cpu(attr->stats_addr_high) << 32;
182                 pi->stats_size = le32_to_cpu(attr->stats_size);
183         }
184
185         ph->xops->xfer_put(ph, t);
186         return ret;
187 }
188
189 static int
190 scmi_perf_domain_attributes_get(const struct scmi_protocol_handle *ph,
191                                 u32 domain, struct perf_dom_info *dom_info,
192                                 u32 version)
193 {
194         int ret;
195         u32 flags;
196         struct scmi_xfer *t;
197         struct scmi_msg_resp_perf_domain_attributes *attr;
198
199         ret = ph->xops->xfer_get_init(ph, PERF_DOMAIN_ATTRIBUTES,
200                                      sizeof(domain), sizeof(*attr), &t);
201         if (ret)
202                 return ret;
203
204         put_unaligned_le32(domain, t->tx.buf);
205         attr = t->rx.buf;
206
207         ret = ph->xops->do_xfer(ph, t);
208         if (!ret) {
209                 flags = le32_to_cpu(attr->flags);
210
211                 dom_info->set_limits = SUPPORTS_SET_LIMITS(flags);
212                 dom_info->set_perf = SUPPORTS_SET_PERF_LVL(flags);
213                 dom_info->perf_limit_notify = SUPPORTS_PERF_LIMIT_NOTIFY(flags);
214                 dom_info->perf_level_notify = SUPPORTS_PERF_LEVEL_NOTIFY(flags);
215                 dom_info->perf_fastchannels = SUPPORTS_PERF_FASTCHANNELS(flags);
216                 dom_info->sustained_freq_khz =
217                                         le32_to_cpu(attr->sustained_freq_khz);
218                 dom_info->sustained_perf_level =
219                                         le32_to_cpu(attr->sustained_perf_level);
220                 if (!dom_info->sustained_freq_khz ||
221                     !dom_info->sustained_perf_level)
222                         /* CPUFreq converts to kHz, hence default 1000 */
223                         dom_info->mult_factor = 1000;
224                 else
225                         dom_info->mult_factor =
226                                         (dom_info->sustained_freq_khz * 1000) /
227                                         dom_info->sustained_perf_level;
228                 strscpy(dom_info->name, attr->name, SCMI_SHORT_NAME_MAX_SIZE);
229         }
230
231         ph->xops->xfer_put(ph, t);
232
233         /*
234          * If supported overwrite short name with the extended one;
235          * on error just carry on and use already provided short name.
236          */
237         if (!ret && PROTOCOL_REV_MAJOR(version) >= 0x3 &&
238             SUPPORTS_EXTENDED_NAMES(flags))
239                 ph->hops->extended_name_get(ph, PERF_DOMAIN_NAME_GET, domain,
240                                             dom_info->name, SCMI_MAX_STR_SIZE);
241
242         return ret;
243 }
244
245 static int opp_cmp_func(const void *opp1, const void *opp2)
246 {
247         const struct scmi_opp *t1 = opp1, *t2 = opp2;
248
249         return t1->perf - t2->perf;
250 }
251
252 struct scmi_perf_ipriv {
253         u32 domain;
254         struct perf_dom_info *perf_dom;
255 };
256
257 static void iter_perf_levels_prepare_message(void *message,
258                                              unsigned int desc_index,
259                                              const void *priv)
260 {
261         struct scmi_msg_perf_describe_levels *msg = message;
262         const struct scmi_perf_ipriv *p = priv;
263
264         msg->domain = cpu_to_le32(p->domain);
265         /* Set the number of OPPs to be skipped/already read */
266         msg->level_index = cpu_to_le32(desc_index);
267 }
268
269 static int iter_perf_levels_update_state(struct scmi_iterator_state *st,
270                                          const void *response, void *priv)
271 {
272         const struct scmi_msg_resp_perf_describe_levels *r = response;
273
274         st->num_returned = le16_to_cpu(r->num_returned);
275         st->num_remaining = le16_to_cpu(r->num_remaining);
276
277         return 0;
278 }
279
280 static int
281 iter_perf_levels_process_response(const struct scmi_protocol_handle *ph,
282                                   const void *response,
283                                   struct scmi_iterator_state *st, void *priv)
284 {
285         struct scmi_opp *opp;
286         const struct scmi_msg_resp_perf_describe_levels *r = response;
287         struct scmi_perf_ipriv *p = priv;
288
289         opp = &p->perf_dom->opp[st->desc_index + st->loop_idx];
290         opp->perf = le32_to_cpu(r->opp[st->loop_idx].perf_val);
291         opp->power = le32_to_cpu(r->opp[st->loop_idx].power);
292         opp->trans_latency_us =
293                 le16_to_cpu(r->opp[st->loop_idx].transition_latency_us);
294         p->perf_dom->opp_count++;
295
296         dev_dbg(ph->dev, "Level %d Power %d Latency %dus\n",
297                 opp->perf, opp->power, opp->trans_latency_us);
298
299         return 0;
300 }
301
302 static int
303 scmi_perf_describe_levels_get(const struct scmi_protocol_handle *ph, u32 domain,
304                               struct perf_dom_info *perf_dom)
305 {
306         int ret;
307         void *iter;
308         struct scmi_iterator_ops ops = {
309                 .prepare_message = iter_perf_levels_prepare_message,
310                 .update_state = iter_perf_levels_update_state,
311                 .process_response = iter_perf_levels_process_response,
312         };
313         struct scmi_perf_ipriv ppriv = {
314                 .domain = domain,
315                 .perf_dom = perf_dom,
316         };
317
318         iter = ph->hops->iter_response_init(ph, &ops, MAX_OPPS,
319                                             PERF_DESCRIBE_LEVELS,
320                                             sizeof(struct scmi_msg_perf_describe_levels),
321                                             &ppriv);
322         if (IS_ERR(iter))
323                 return PTR_ERR(iter);
324
325         ret = ph->hops->iter_response_run(iter);
326         if (ret)
327                 return ret;
328
329         if (perf_dom->opp_count)
330                 sort(perf_dom->opp, perf_dom->opp_count,
331                      sizeof(struct scmi_opp), opp_cmp_func, NULL);
332
333         return ret;
334 }
335
336 static int scmi_perf_mb_limits_set(const struct scmi_protocol_handle *ph,
337                                    u32 domain, u32 max_perf, u32 min_perf)
338 {
339         int ret;
340         struct scmi_xfer *t;
341         struct scmi_perf_set_limits *limits;
342
343         ret = ph->xops->xfer_get_init(ph, PERF_LIMITS_SET,
344                                       sizeof(*limits), 0, &t);
345         if (ret)
346                 return ret;
347
348         limits = t->tx.buf;
349         limits->domain = cpu_to_le32(domain);
350         limits->max_level = cpu_to_le32(max_perf);
351         limits->min_level = cpu_to_le32(min_perf);
352
353         ret = ph->xops->do_xfer(ph, t);
354
355         ph->xops->xfer_put(ph, t);
356         return ret;
357 }
358
359 static int scmi_perf_limits_set(const struct scmi_protocol_handle *ph,
360                                 u32 domain, u32 max_perf, u32 min_perf)
361 {
362         struct scmi_perf_info *pi = ph->get_priv(ph);
363         struct perf_dom_info *dom = pi->dom_info + domain;
364
365         if (PROTOCOL_REV_MAJOR(pi->version) >= 0x3 && !max_perf && !min_perf)
366                 return -EINVAL;
367
368         if (dom->fc_info && dom->fc_info[PERF_FC_LIMIT].set_addr) {
369                 struct scmi_fc_info *fci = &dom->fc_info[PERF_FC_LIMIT];
370
371                 trace_scmi_fc_call(SCMI_PROTOCOL_PERF, PERF_LIMITS_SET,
372                                    domain, min_perf, max_perf);
373                 iowrite32(max_perf, fci->set_addr);
374                 iowrite32(min_perf, fci->set_addr + 4);
375                 ph->hops->fastchannel_db_ring(fci->set_db);
376                 return 0;
377         }
378
379         return scmi_perf_mb_limits_set(ph, domain, max_perf, min_perf);
380 }
381
382 static int scmi_perf_mb_limits_get(const struct scmi_protocol_handle *ph,
383                                    u32 domain, u32 *max_perf, u32 *min_perf)
384 {
385         int ret;
386         struct scmi_xfer *t;
387         struct scmi_perf_get_limits *limits;
388
389         ret = ph->xops->xfer_get_init(ph, PERF_LIMITS_GET,
390                                       sizeof(__le32), 0, &t);
391         if (ret)
392                 return ret;
393
394         put_unaligned_le32(domain, t->tx.buf);
395
396         ret = ph->xops->do_xfer(ph, t);
397         if (!ret) {
398                 limits = t->rx.buf;
399
400                 *max_perf = le32_to_cpu(limits->max_level);
401                 *min_perf = le32_to_cpu(limits->min_level);
402         }
403
404         ph->xops->xfer_put(ph, t);
405         return ret;
406 }
407
408 static int scmi_perf_limits_get(const struct scmi_protocol_handle *ph,
409                                 u32 domain, u32 *max_perf, u32 *min_perf)
410 {
411         struct scmi_perf_info *pi = ph->get_priv(ph);
412         struct perf_dom_info *dom = pi->dom_info + domain;
413
414         if (dom->fc_info && dom->fc_info[PERF_FC_LIMIT].get_addr) {
415                 struct scmi_fc_info *fci = &dom->fc_info[PERF_FC_LIMIT];
416
417                 *max_perf = ioread32(fci->get_addr);
418                 *min_perf = ioread32(fci->get_addr + 4);
419                 trace_scmi_fc_call(SCMI_PROTOCOL_PERF, PERF_LIMITS_GET,
420                                    domain, *min_perf, *max_perf);
421                 return 0;
422         }
423
424         return scmi_perf_mb_limits_get(ph, domain, max_perf, min_perf);
425 }
426
427 static int scmi_perf_mb_level_set(const struct scmi_protocol_handle *ph,
428                                   u32 domain, u32 level, bool poll)
429 {
430         int ret;
431         struct scmi_xfer *t;
432         struct scmi_perf_set_level *lvl;
433
434         ret = ph->xops->xfer_get_init(ph, PERF_LEVEL_SET, sizeof(*lvl), 0, &t);
435         if (ret)
436                 return ret;
437
438         t->hdr.poll_completion = poll;
439         lvl = t->tx.buf;
440         lvl->domain = cpu_to_le32(domain);
441         lvl->level = cpu_to_le32(level);
442
443         ret = ph->xops->do_xfer(ph, t);
444
445         ph->xops->xfer_put(ph, t);
446         return ret;
447 }
448
449 static int scmi_perf_level_set(const struct scmi_protocol_handle *ph,
450                                u32 domain, u32 level, bool poll)
451 {
452         struct scmi_perf_info *pi = ph->get_priv(ph);
453         struct perf_dom_info *dom = pi->dom_info + domain;
454
455         if (dom->fc_info && dom->fc_info[PERF_FC_LEVEL].set_addr) {
456                 struct scmi_fc_info *fci = &dom->fc_info[PERF_FC_LEVEL];
457
458                 trace_scmi_fc_call(SCMI_PROTOCOL_PERF, PERF_LEVEL_SET,
459                                    domain, level, 0);
460                 iowrite32(level, fci->set_addr);
461                 ph->hops->fastchannel_db_ring(fci->set_db);
462                 return 0;
463         }
464
465         return scmi_perf_mb_level_set(ph, domain, level, poll);
466 }
467
468 static int scmi_perf_mb_level_get(const struct scmi_protocol_handle *ph,
469                                   u32 domain, u32 *level, bool poll)
470 {
471         int ret;
472         struct scmi_xfer *t;
473
474         ret = ph->xops->xfer_get_init(ph, PERF_LEVEL_GET,
475                                      sizeof(u32), sizeof(u32), &t);
476         if (ret)
477                 return ret;
478
479         t->hdr.poll_completion = poll;
480         put_unaligned_le32(domain, t->tx.buf);
481
482         ret = ph->xops->do_xfer(ph, t);
483         if (!ret)
484                 *level = get_unaligned_le32(t->rx.buf);
485
486         ph->xops->xfer_put(ph, t);
487         return ret;
488 }
489
490 static int scmi_perf_level_get(const struct scmi_protocol_handle *ph,
491                                u32 domain, u32 *level, bool poll)
492 {
493         struct scmi_perf_info *pi = ph->get_priv(ph);
494         struct perf_dom_info *dom = pi->dom_info + domain;
495
496         if (dom->fc_info && dom->fc_info[PERF_FC_LEVEL].get_addr) {
497                 *level = ioread32(dom->fc_info[PERF_FC_LEVEL].get_addr);
498                 trace_scmi_fc_call(SCMI_PROTOCOL_PERF, PERF_LEVEL_GET,
499                                    domain, *level, 0);
500                 return 0;
501         }
502
503         return scmi_perf_mb_level_get(ph, domain, level, poll);
504 }
505
506 static int scmi_perf_level_limits_notify(const struct scmi_protocol_handle *ph,
507                                          u32 domain, int message_id,
508                                          bool enable)
509 {
510         int ret;
511         struct scmi_xfer *t;
512         struct scmi_perf_notify_level_or_limits *notify;
513
514         ret = ph->xops->xfer_get_init(ph, message_id, sizeof(*notify), 0, &t);
515         if (ret)
516                 return ret;
517
518         notify = t->tx.buf;
519         notify->domain = cpu_to_le32(domain);
520         notify->notify_enable = enable ? cpu_to_le32(BIT(0)) : 0;
521
522         ret = ph->xops->do_xfer(ph, t);
523
524         ph->xops->xfer_put(ph, t);
525         return ret;
526 }
527
528 static void scmi_perf_domain_init_fc(const struct scmi_protocol_handle *ph,
529                                      u32 domain, struct scmi_fc_info **p_fc)
530 {
531         struct scmi_fc_info *fc;
532
533         fc = devm_kcalloc(ph->dev, PERF_FC_MAX, sizeof(*fc), GFP_KERNEL);
534         if (!fc)
535                 return;
536
537         ph->hops->fastchannel_init(ph, PERF_DESCRIBE_FASTCHANNEL,
538                                    PERF_LEVEL_SET, 4, domain,
539                                    &fc[PERF_FC_LEVEL].set_addr,
540                                    &fc[PERF_FC_LEVEL].set_db);
541
542         ph->hops->fastchannel_init(ph, PERF_DESCRIBE_FASTCHANNEL,
543                                    PERF_LEVEL_GET, 4, domain,
544                                    &fc[PERF_FC_LEVEL].get_addr, NULL);
545
546         ph->hops->fastchannel_init(ph, PERF_DESCRIBE_FASTCHANNEL,
547                                    PERF_LIMITS_SET, 8, domain,
548                                    &fc[PERF_FC_LIMIT].set_addr,
549                                    &fc[PERF_FC_LIMIT].set_db);
550
551         ph->hops->fastchannel_init(ph, PERF_DESCRIBE_FASTCHANNEL,
552                                    PERF_LIMITS_GET, 8, domain,
553                                    &fc[PERF_FC_LIMIT].get_addr, NULL);
554
555         *p_fc = fc;
556 }
557
558 /* Device specific ops */
559 static int scmi_dev_domain_id(struct device *dev)
560 {
561         struct of_phandle_args clkspec;
562
563         if (of_parse_phandle_with_args(dev->of_node, "clocks", "#clock-cells",
564                                        0, &clkspec))
565                 return -EINVAL;
566
567         return clkspec.args[0];
568 }
569
570 static int scmi_dvfs_device_opps_add(const struct scmi_protocol_handle *ph,
571                                      struct device *dev)
572 {
573         int idx, ret, domain;
574         unsigned long freq;
575         struct scmi_opp *opp;
576         struct perf_dom_info *dom;
577         struct scmi_perf_info *pi = ph->get_priv(ph);
578
579         domain = scmi_dev_domain_id(dev);
580         if (domain < 0)
581                 return domain;
582
583         dom = pi->dom_info + domain;
584
585         for (opp = dom->opp, idx = 0; idx < dom->opp_count; idx++, opp++) {
586                 freq = opp->perf * dom->mult_factor;
587
588                 ret = dev_pm_opp_add(dev, freq, 0);
589                 if (ret) {
590                         dev_warn(dev, "failed to add opp %luHz\n", freq);
591
592                         while (idx-- > 0) {
593                                 freq = (--opp)->perf * dom->mult_factor;
594                                 dev_pm_opp_remove(dev, freq);
595                         }
596                         return ret;
597                 }
598         }
599         return 0;
600 }
601
602 static int
603 scmi_dvfs_transition_latency_get(const struct scmi_protocol_handle *ph,
604                                  struct device *dev)
605 {
606         struct perf_dom_info *dom;
607         struct scmi_perf_info *pi = ph->get_priv(ph);
608         int domain = scmi_dev_domain_id(dev);
609
610         if (domain < 0)
611                 return domain;
612
613         dom = pi->dom_info + domain;
614         /* uS to nS */
615         return dom->opp[dom->opp_count - 1].trans_latency_us * 1000;
616 }
617
618 static int scmi_dvfs_freq_set(const struct scmi_protocol_handle *ph, u32 domain,
619                               unsigned long freq, bool poll)
620 {
621         struct scmi_perf_info *pi = ph->get_priv(ph);
622         struct perf_dom_info *dom = pi->dom_info + domain;
623
624         return scmi_perf_level_set(ph, domain, freq / dom->mult_factor, poll);
625 }
626
627 static int scmi_dvfs_freq_get(const struct scmi_protocol_handle *ph, u32 domain,
628                               unsigned long *freq, bool poll)
629 {
630         int ret;
631         u32 level;
632         struct scmi_perf_info *pi = ph->get_priv(ph);
633         struct perf_dom_info *dom = pi->dom_info + domain;
634
635         ret = scmi_perf_level_get(ph, domain, &level, poll);
636         if (!ret)
637                 *freq = level * dom->mult_factor;
638
639         return ret;
640 }
641
642 static int scmi_dvfs_est_power_get(const struct scmi_protocol_handle *ph,
643                                    u32 domain, unsigned long *freq,
644                                    unsigned long *power)
645 {
646         struct scmi_perf_info *pi = ph->get_priv(ph);
647         struct perf_dom_info *dom;
648         unsigned long opp_freq;
649         int idx, ret = -EINVAL;
650         struct scmi_opp *opp;
651
652         dom = pi->dom_info + domain;
653         if (!dom)
654                 return -EIO;
655
656         for (opp = dom->opp, idx = 0; idx < dom->opp_count; idx++, opp++) {
657                 opp_freq = opp->perf * dom->mult_factor;
658                 if (opp_freq < *freq)
659                         continue;
660
661                 *freq = opp_freq;
662                 *power = opp->power;
663                 ret = 0;
664                 break;
665         }
666
667         return ret;
668 }
669
670 static bool scmi_fast_switch_possible(const struct scmi_protocol_handle *ph,
671                                       struct device *dev)
672 {
673         struct perf_dom_info *dom;
674         struct scmi_perf_info *pi = ph->get_priv(ph);
675
676         dom = pi->dom_info + scmi_dev_domain_id(dev);
677
678         return dom->fc_info && dom->fc_info[PERF_FC_LEVEL].set_addr;
679 }
680
681 static enum scmi_power_scale
682 scmi_power_scale_get(const struct scmi_protocol_handle *ph)
683 {
684         struct scmi_perf_info *pi = ph->get_priv(ph);
685
686         return pi->power_scale;
687 }
688
689 static const struct scmi_perf_proto_ops perf_proto_ops = {
690         .limits_set = scmi_perf_limits_set,
691         .limits_get = scmi_perf_limits_get,
692         .level_set = scmi_perf_level_set,
693         .level_get = scmi_perf_level_get,
694         .device_domain_id = scmi_dev_domain_id,
695         .transition_latency_get = scmi_dvfs_transition_latency_get,
696         .device_opps_add = scmi_dvfs_device_opps_add,
697         .freq_set = scmi_dvfs_freq_set,
698         .freq_get = scmi_dvfs_freq_get,
699         .est_power_get = scmi_dvfs_est_power_get,
700         .fast_switch_possible = scmi_fast_switch_possible,
701         .power_scale_get = scmi_power_scale_get,
702 };
703
704 static int scmi_perf_set_notify_enabled(const struct scmi_protocol_handle *ph,
705                                         u8 evt_id, u32 src_id, bool enable)
706 {
707         int ret, cmd_id;
708
709         if (evt_id >= ARRAY_SIZE(evt_2_cmd))
710                 return -EINVAL;
711
712         cmd_id = evt_2_cmd[evt_id];
713         ret = scmi_perf_level_limits_notify(ph, src_id, cmd_id, enable);
714         if (ret)
715                 pr_debug("FAIL_ENABLED - evt[%X] dom[%d] - ret:%d\n",
716                          evt_id, src_id, ret);
717
718         return ret;
719 }
720
721 static void *scmi_perf_fill_custom_report(const struct scmi_protocol_handle *ph,
722                                           u8 evt_id, ktime_t timestamp,
723                                           const void *payld, size_t payld_sz,
724                                           void *report, u32 *src_id)
725 {
726         void *rep = NULL;
727
728         switch (evt_id) {
729         case SCMI_EVENT_PERFORMANCE_LIMITS_CHANGED:
730         {
731                 const struct scmi_perf_limits_notify_payld *p = payld;
732                 struct scmi_perf_limits_report *r = report;
733
734                 if (sizeof(*p) != payld_sz)
735                         break;
736
737                 r->timestamp = timestamp;
738                 r->agent_id = le32_to_cpu(p->agent_id);
739                 r->domain_id = le32_to_cpu(p->domain_id);
740                 r->range_max = le32_to_cpu(p->range_max);
741                 r->range_min = le32_to_cpu(p->range_min);
742                 *src_id = r->domain_id;
743                 rep = r;
744                 break;
745         }
746         case SCMI_EVENT_PERFORMANCE_LEVEL_CHANGED:
747         {
748                 const struct scmi_perf_level_notify_payld *p = payld;
749                 struct scmi_perf_level_report *r = report;
750
751                 if (sizeof(*p) != payld_sz)
752                         break;
753
754                 r->timestamp = timestamp;
755                 r->agent_id = le32_to_cpu(p->agent_id);
756                 r->domain_id = le32_to_cpu(p->domain_id);
757                 r->performance_level = le32_to_cpu(p->performance_level);
758                 *src_id = r->domain_id;
759                 rep = r;
760                 break;
761         }
762         default:
763                 break;
764         }
765
766         return rep;
767 }
768
769 static int scmi_perf_get_num_sources(const struct scmi_protocol_handle *ph)
770 {
771         struct scmi_perf_info *pi = ph->get_priv(ph);
772
773         if (!pi)
774                 return -EINVAL;
775
776         return pi->num_domains;
777 }
778
779 static const struct scmi_event perf_events[] = {
780         {
781                 .id = SCMI_EVENT_PERFORMANCE_LIMITS_CHANGED,
782                 .max_payld_sz = sizeof(struct scmi_perf_limits_notify_payld),
783                 .max_report_sz = sizeof(struct scmi_perf_limits_report),
784         },
785         {
786                 .id = SCMI_EVENT_PERFORMANCE_LEVEL_CHANGED,
787                 .max_payld_sz = sizeof(struct scmi_perf_level_notify_payld),
788                 .max_report_sz = sizeof(struct scmi_perf_level_report),
789         },
790 };
791
792 static const struct scmi_event_ops perf_event_ops = {
793         .get_num_sources = scmi_perf_get_num_sources,
794         .set_notify_enabled = scmi_perf_set_notify_enabled,
795         .fill_custom_report = scmi_perf_fill_custom_report,
796 };
797
798 static const struct scmi_protocol_events perf_protocol_events = {
799         .queue_sz = SCMI_PROTO_QUEUE_SZ,
800         .ops = &perf_event_ops,
801         .evts = perf_events,
802         .num_events = ARRAY_SIZE(perf_events),
803 };
804
805 static int scmi_perf_protocol_init(const struct scmi_protocol_handle *ph)
806 {
807         int domain, ret;
808         u32 version;
809         struct scmi_perf_info *pinfo;
810
811         ret = ph->xops->version_get(ph, &version);
812         if (ret)
813                 return ret;
814
815         dev_dbg(ph->dev, "Performance Version %d.%d\n",
816                 PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
817
818         pinfo = devm_kzalloc(ph->dev, sizeof(*pinfo), GFP_KERNEL);
819         if (!pinfo)
820                 return -ENOMEM;
821
822         ret = scmi_perf_attributes_get(ph, pinfo);
823         if (ret)
824                 return ret;
825
826         pinfo->dom_info = devm_kcalloc(ph->dev, pinfo->num_domains,
827                                        sizeof(*pinfo->dom_info), GFP_KERNEL);
828         if (!pinfo->dom_info)
829                 return -ENOMEM;
830
831         for (domain = 0; domain < pinfo->num_domains; domain++) {
832                 struct perf_dom_info *dom = pinfo->dom_info + domain;
833
834                 scmi_perf_domain_attributes_get(ph, domain, dom, version);
835                 scmi_perf_describe_levels_get(ph, domain, dom);
836
837                 if (dom->perf_fastchannels)
838                         scmi_perf_domain_init_fc(ph, domain, &dom->fc_info);
839         }
840
841         pinfo->version = version;
842
843         return ph->set_priv(ph, pinfo);
844 }
845
846 static const struct scmi_protocol scmi_perf = {
847         .id = SCMI_PROTOCOL_PERF,
848         .owner = THIS_MODULE,
849         .instance_init = &scmi_perf_protocol_init,
850         .ops = &perf_proto_ops,
851         .events = &perf_protocol_events,
852 };
853
854 DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(perf, scmi_perf)