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
scmi_perf_attributes_get(const struct scmi_protocol_handle * ph,struct scmi_perf_info * pi)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
scmi_perf_xa_destroy(void * data)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
scmi_perf_domain_attributes_get(const struct scmi_protocol_handle * ph,struct perf_dom_info * dom_info,u32 version)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
opp_cmp_func(const void * opp1,const void * opp2)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
iter_perf_levels_prepare_message(void * message,unsigned int desc_index,const void * priv)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
iter_perf_levels_update_state(struct scmi_iterator_state * st,const void * response,void * priv)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
process_response_opp(struct scmi_opp * opp,unsigned int loop_idx,const struct scmi_msg_resp_perf_describe_levels * r)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
process_response_opp_v4(struct device * dev,struct perf_dom_info * dom,struct scmi_opp * opp,unsigned int loop_idx,const struct scmi_msg_resp_perf_describe_levels_v4 * r)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
iter_perf_levels_process_response(const struct scmi_protocol_handle * ph,const void * response,struct scmi_iterator_state * st,void * priv)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
scmi_perf_describe_levels_get(const struct scmi_protocol_handle * ph,struct perf_dom_info * perf_dom,u32 version)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
scmi_perf_num_domains_get(const struct scmi_protocol_handle * ph)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 *
scmi_perf_domain_lookup(const struct scmi_protocol_handle * ph,u32 domain)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 *
scmi_perf_info_get(const struct scmi_protocol_handle * ph,u32 domain)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
scmi_perf_msg_limits_set(const struct scmi_protocol_handle * ph,u32 domain,u32 max_perf,u32 min_perf)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
__scmi_perf_limits_set(const struct scmi_protocol_handle * ph,struct perf_dom_info * dom,u32 max_perf,u32 min_perf)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
scmi_perf_limits_set(const struct scmi_protocol_handle * ph,u32 domain,u32 max_perf,u32 min_perf)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
scmi_perf_msg_limits_get(const struct scmi_protocol_handle * ph,u32 domain,u32 * max_perf,u32 * min_perf)547 static int scmi_perf_msg_limits_get(const struct scmi_protocol_handle *ph,
548 u32 domain, u32 *max_perf, u32 *min_perf)
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
__scmi_perf_limits_get(const struct scmi_protocol_handle * ph,struct perf_dom_info * dom,u32 * max_perf,u32 * min_perf)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
scmi_perf_limits_get(const struct scmi_protocol_handle * ph,u32 domain,u32 * max_perf,u32 * min_perf)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
scmi_perf_msg_level_set(const struct scmi_protocol_handle * ph,u32 domain,u32 level,bool poll)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
__scmi_perf_level_set(const struct scmi_protocol_handle * ph,struct perf_dom_info * dom,u32 level,bool poll)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
scmi_perf_level_set(const struct scmi_protocol_handle * ph,u32 domain,u32 level,bool poll)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
scmi_perf_msg_level_get(const struct scmi_protocol_handle * ph,u32 domain,u32 * level,bool poll)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
__scmi_perf_level_get(const struct scmi_protocol_handle * ph,struct perf_dom_info * dom,u32 * level,bool poll)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
scmi_perf_level_get(const struct scmi_protocol_handle * ph,u32 domain,u32 * level,bool poll)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
scmi_perf_level_limits_notify(const struct scmi_protocol_handle * ph,u32 domain,int message_id,bool enable)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
scmi_perf_domain_init_fc(const struct scmi_protocol_handle * ph,u32 domain,struct scmi_fc_info ** p_fc)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 /* Device specific ops */
scmi_dev_domain_id(struct device * dev)800 static int scmi_dev_domain_id(struct device *dev)
801 {
802 struct of_phandle_args clkspec;
803
804 if (of_parse_phandle_with_args(dev->of_node, "clocks", "#clock-cells",
805 0, &clkspec))
806 return -EINVAL;
807
808 return clkspec.args[0];
809 }
810
scmi_dvfs_device_opps_add(const struct scmi_protocol_handle * ph,struct device * dev)811 static int scmi_dvfs_device_opps_add(const struct scmi_protocol_handle *ph,
812 struct device *dev)
813 {
814 int idx, ret, domain;
815 unsigned long freq;
816 struct perf_dom_info *dom;
817
818 domain = scmi_dev_domain_id(dev);
819 if (domain < 0)
820 return -EINVAL;
821
822 dom = scmi_perf_domain_lookup(ph, domain);
823 if (IS_ERR(dom))
824 return PTR_ERR(dom);
825
826 for (idx = 0; idx < dom->opp_count; idx++) {
827 if (!dom->level_indexing_mode)
828 freq = dom->opp[idx].perf * dom->mult_factor;
829 else
830 freq = dom->opp[idx].indicative_freq * dom->mult_factor;
831
832 ret = dev_pm_opp_add(dev, freq, 0);
833 if (ret) {
834 dev_warn(dev, "failed to add opp %luHz\n", freq);
835 dev_pm_opp_remove_all_dynamic(dev);
836 return ret;
837 }
838
839 dev_dbg(dev, "[%d][%s]:: Registered OPP[%d] %lu\n",
840 domain, dom->info.name, idx, freq);
841 }
842 return 0;
843 }
844
845 static int
scmi_dvfs_transition_latency_get(const struct scmi_protocol_handle * ph,struct device * dev)846 scmi_dvfs_transition_latency_get(const struct scmi_protocol_handle *ph,
847 struct device *dev)
848 {
849 int domain;
850 struct perf_dom_info *dom;
851
852 domain = scmi_dev_domain_id(dev);
853 if (domain < 0)
854 return -EINVAL;
855
856 dom = scmi_perf_domain_lookup(ph, domain);
857 if (IS_ERR(dom))
858 return PTR_ERR(dom);
859
860 /* uS to nS */
861 return dom->opp[dom->opp_count - 1].trans_latency_us * 1000;
862 }
863
scmi_dvfs_freq_set(const struct scmi_protocol_handle * ph,u32 domain,unsigned long freq,bool poll)864 static int scmi_dvfs_freq_set(const struct scmi_protocol_handle *ph, u32 domain,
865 unsigned long freq, bool poll)
866 {
867 unsigned int level;
868 struct perf_dom_info *dom;
869
870 dom = scmi_perf_domain_lookup(ph, domain);
871 if (IS_ERR(dom))
872 return PTR_ERR(dom);
873
874 if (!dom->level_indexing_mode) {
875 level = freq / dom->mult_factor;
876 } else {
877 struct scmi_opp *opp;
878
879 opp = LOOKUP_BY_FREQ(dom->opps_by_freq,
880 freq / dom->mult_factor);
881 if (!opp)
882 return -EIO;
883
884 level = opp->level_index;
885 }
886
887 return __scmi_perf_level_set(ph, dom, level, poll);
888 }
889
scmi_dvfs_freq_get(const struct scmi_protocol_handle * ph,u32 domain,unsigned long * freq,bool poll)890 static int scmi_dvfs_freq_get(const struct scmi_protocol_handle *ph, u32 domain,
891 unsigned long *freq, bool poll)
892 {
893 int ret;
894 u32 level;
895 struct perf_dom_info *dom;
896
897 dom = scmi_perf_domain_lookup(ph, domain);
898 if (IS_ERR(dom))
899 return PTR_ERR(dom);
900
901 ret = __scmi_perf_level_get(ph, dom, &level, poll);
902 if (ret)
903 return ret;
904
905 if (!dom->level_indexing_mode) {
906 *freq = level * dom->mult_factor;
907 } else {
908 struct scmi_opp *opp;
909
910 opp = xa_load(&dom->opps_by_idx, level);
911 if (!opp)
912 return -EIO;
913
914 *freq = opp->indicative_freq * dom->mult_factor;
915 }
916
917 return ret;
918 }
919
scmi_dvfs_est_power_get(const struct scmi_protocol_handle * ph,u32 domain,unsigned long * freq,unsigned long * power)920 static int scmi_dvfs_est_power_get(const struct scmi_protocol_handle *ph,
921 u32 domain, unsigned long *freq,
922 unsigned long *power)
923 {
924 struct perf_dom_info *dom;
925 unsigned long opp_freq;
926 int idx, ret = -EINVAL;
927 struct scmi_opp *opp;
928
929 dom = scmi_perf_domain_lookup(ph, domain);
930 if (IS_ERR(dom))
931 return PTR_ERR(dom);
932
933 for (opp = dom->opp, idx = 0; idx < dom->opp_count; idx++, opp++) {
934 if (!dom->level_indexing_mode)
935 opp_freq = opp->perf * dom->mult_factor;
936 else
937 opp_freq = opp->indicative_freq * dom->mult_factor;
938
939 if (opp_freq < *freq)
940 continue;
941
942 *freq = opp_freq;
943 *power = opp->power;
944 ret = 0;
945 break;
946 }
947
948 return ret;
949 }
950
scmi_fast_switch_possible(const struct scmi_protocol_handle * ph,struct device * dev)951 static bool scmi_fast_switch_possible(const struct scmi_protocol_handle *ph,
952 struct device *dev)
953 {
954 int domain;
955 struct perf_dom_info *dom;
956
957 domain = scmi_dev_domain_id(dev);
958 if (domain < 0)
959 return false;
960
961 dom = scmi_perf_domain_lookup(ph, domain);
962 if (IS_ERR(dom))
963 return false;
964
965 return dom->fc_info && dom->fc_info[PERF_FC_LEVEL].set_addr;
966 }
967
968 static enum scmi_power_scale
scmi_power_scale_get(const struct scmi_protocol_handle * ph)969 scmi_power_scale_get(const struct scmi_protocol_handle *ph)
970 {
971 struct scmi_perf_info *pi = ph->get_priv(ph);
972
973 return pi->power_scale;
974 }
975
976 static const struct scmi_perf_proto_ops perf_proto_ops = {
977 .num_domains_get = scmi_perf_num_domains_get,
978 .info_get = scmi_perf_info_get,
979 .limits_set = scmi_perf_limits_set,
980 .limits_get = scmi_perf_limits_get,
981 .level_set = scmi_perf_level_set,
982 .level_get = scmi_perf_level_get,
983 .device_domain_id = scmi_dev_domain_id,
984 .transition_latency_get = scmi_dvfs_transition_latency_get,
985 .device_opps_add = scmi_dvfs_device_opps_add,
986 .freq_set = scmi_dvfs_freq_set,
987 .freq_get = scmi_dvfs_freq_get,
988 .est_power_get = scmi_dvfs_est_power_get,
989 .fast_switch_possible = scmi_fast_switch_possible,
990 .power_scale_get = scmi_power_scale_get,
991 };
992
scmi_perf_set_notify_enabled(const struct scmi_protocol_handle * ph,u8 evt_id,u32 src_id,bool enable)993 static int scmi_perf_set_notify_enabled(const struct scmi_protocol_handle *ph,
994 u8 evt_id, u32 src_id, bool enable)
995 {
996 int ret, cmd_id;
997
998 if (evt_id >= ARRAY_SIZE(evt_2_cmd))
999 return -EINVAL;
1000
1001 cmd_id = evt_2_cmd[evt_id];
1002 ret = scmi_perf_level_limits_notify(ph, src_id, cmd_id, enable);
1003 if (ret)
1004 pr_debug("FAIL_ENABLED - evt[%X] dom[%d] - ret:%d\n",
1005 evt_id, src_id, ret);
1006
1007 return ret;
1008 }
1009
scmi_perf_fill_custom_report(const struct scmi_protocol_handle * ph,u8 evt_id,ktime_t timestamp,const void * payld,size_t payld_sz,void * report,u32 * src_id)1010 static void *scmi_perf_fill_custom_report(const struct scmi_protocol_handle *ph,
1011 u8 evt_id, ktime_t timestamp,
1012 const void *payld, size_t payld_sz,
1013 void *report, u32 *src_id)
1014 {
1015 void *rep = NULL;
1016
1017 switch (evt_id) {
1018 case SCMI_EVENT_PERFORMANCE_LIMITS_CHANGED:
1019 {
1020 const struct scmi_perf_limits_notify_payld *p = payld;
1021 struct scmi_perf_limits_report *r = report;
1022
1023 if (sizeof(*p) != payld_sz)
1024 break;
1025
1026 r->timestamp = timestamp;
1027 r->agent_id = le32_to_cpu(p->agent_id);
1028 r->domain_id = le32_to_cpu(p->domain_id);
1029 r->range_max = le32_to_cpu(p->range_max);
1030 r->range_min = le32_to_cpu(p->range_min);
1031 *src_id = r->domain_id;
1032 rep = r;
1033 break;
1034 }
1035 case SCMI_EVENT_PERFORMANCE_LEVEL_CHANGED:
1036 {
1037 const struct scmi_perf_level_notify_payld *p = payld;
1038 struct scmi_perf_level_report *r = report;
1039
1040 if (sizeof(*p) != payld_sz)
1041 break;
1042
1043 r->timestamp = timestamp;
1044 r->agent_id = le32_to_cpu(p->agent_id);
1045 r->domain_id = le32_to_cpu(p->domain_id);
1046 r->performance_level = le32_to_cpu(p->performance_level);
1047 *src_id = r->domain_id;
1048 rep = r;
1049 break;
1050 }
1051 default:
1052 break;
1053 }
1054
1055 return rep;
1056 }
1057
scmi_perf_get_num_sources(const struct scmi_protocol_handle * ph)1058 static int scmi_perf_get_num_sources(const struct scmi_protocol_handle *ph)
1059 {
1060 struct scmi_perf_info *pi = ph->get_priv(ph);
1061
1062 if (!pi)
1063 return -EINVAL;
1064
1065 return pi->num_domains;
1066 }
1067
1068 static const struct scmi_event perf_events[] = {
1069 {
1070 .id = SCMI_EVENT_PERFORMANCE_LIMITS_CHANGED,
1071 .max_payld_sz = sizeof(struct scmi_perf_limits_notify_payld),
1072 .max_report_sz = sizeof(struct scmi_perf_limits_report),
1073 },
1074 {
1075 .id = SCMI_EVENT_PERFORMANCE_LEVEL_CHANGED,
1076 .max_payld_sz = sizeof(struct scmi_perf_level_notify_payld),
1077 .max_report_sz = sizeof(struct scmi_perf_level_report),
1078 },
1079 };
1080
1081 static const struct scmi_event_ops perf_event_ops = {
1082 .get_num_sources = scmi_perf_get_num_sources,
1083 .set_notify_enabled = scmi_perf_set_notify_enabled,
1084 .fill_custom_report = scmi_perf_fill_custom_report,
1085 };
1086
1087 static const struct scmi_protocol_events perf_protocol_events = {
1088 .queue_sz = SCMI_PROTO_QUEUE_SZ,
1089 .ops = &perf_event_ops,
1090 .evts = perf_events,
1091 .num_events = ARRAY_SIZE(perf_events),
1092 };
1093
scmi_perf_protocol_init(const struct scmi_protocol_handle * ph)1094 static int scmi_perf_protocol_init(const struct scmi_protocol_handle *ph)
1095 {
1096 int domain, ret;
1097 u32 version;
1098 struct scmi_perf_info *pinfo;
1099
1100 ret = ph->xops->version_get(ph, &version);
1101 if (ret)
1102 return ret;
1103
1104 dev_dbg(ph->dev, "Performance Version %d.%d\n",
1105 PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
1106
1107 pinfo = devm_kzalloc(ph->dev, sizeof(*pinfo), GFP_KERNEL);
1108 if (!pinfo)
1109 return -ENOMEM;
1110
1111 pinfo->version = version;
1112
1113 ret = scmi_perf_attributes_get(ph, pinfo);
1114 if (ret)
1115 return ret;
1116
1117 pinfo->dom_info = devm_kcalloc(ph->dev, pinfo->num_domains,
1118 sizeof(*pinfo->dom_info), GFP_KERNEL);
1119 if (!pinfo->dom_info)
1120 return -ENOMEM;
1121
1122 for (domain = 0; domain < pinfo->num_domains; domain++) {
1123 struct perf_dom_info *dom = pinfo->dom_info + domain;
1124
1125 dom->id = domain;
1126 scmi_perf_domain_attributes_get(ph, dom, version);
1127 scmi_perf_describe_levels_get(ph, dom, version);
1128
1129 if (dom->perf_fastchannels)
1130 scmi_perf_domain_init_fc(ph, dom->id, &dom->fc_info);
1131 }
1132
1133 ret = devm_add_action_or_reset(ph->dev, scmi_perf_xa_destroy, pinfo);
1134 if (ret)
1135 return ret;
1136
1137 return ph->set_priv(ph, pinfo);
1138 }
1139
1140 static const struct scmi_protocol scmi_perf = {
1141 .id = SCMI_PROTOCOL_PERF,
1142 .owner = THIS_MODULE,
1143 .instance_init = &scmi_perf_protocol_init,
1144 .ops = &perf_proto_ops,
1145 .events = &perf_protocol_events,
1146 };
1147
1148 DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(perf, scmi_perf)
1149