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