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