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