• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
scmi_perf_attributes_get(const struct scmi_protocol_handle * ph,struct scmi_perf_info * pi)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 
scmi_perf_xa_destroy(void * data)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
scmi_perf_domain_attributes_get(const struct scmi_protocol_handle * ph,struct perf_dom_info * dom_info,u32 version)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 
opp_cmp_func(const void * opp1,const void * opp2)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 
iter_perf_levels_prepare_message(void * message,unsigned int desc_index,const void * priv)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 
iter_perf_levels_update_state(struct scmi_iterator_state * st,const void * response,void * priv)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
process_response_opp(struct scmi_opp * opp,unsigned int loop_idx,const struct scmi_msg_resp_perf_describe_levels * r)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
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)353 process_response_opp_v4(struct device *dev, struct perf_dom_info *dom,
354 			struct scmi_opp *opp, 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 		int ret;
366 
367 		opp->level_index = le32_to_cpu(r->opp[loop_idx].level_index);
368 
369 		ret = xa_insert(&dom->opps_by_idx, opp->level_index, opp,
370 				GFP_KERNEL);
371 		if (ret)
372 			dev_warn(dev,
373 				 "Failed to add opps_by_idx at %d - ret:%d\n",
374 				 opp->level_index, ret);
375 
376 		ret = xa_insert(&dom->opps_by_lvl, opp->perf, opp, GFP_KERNEL);
377 		if (ret)
378 			dev_warn(dev,
379 				 "Failed to add opps_by_lvl at %d - ret:%d\n",
380 				 opp->perf, ret);
381 
382 		hash_add(dom->opps_by_freq, &opp->hash, opp->indicative_freq);
383 	}
384 }
385 
386 static int
iter_perf_levels_process_response(const struct scmi_protocol_handle * ph,const void * response,struct scmi_iterator_state * st,void * priv)387 iter_perf_levels_process_response(const struct scmi_protocol_handle *ph,
388 				  const void *response,
389 				  struct scmi_iterator_state *st, void *priv)
390 {
391 	struct scmi_opp *opp;
392 	struct scmi_perf_ipriv *p = priv;
393 
394 	opp = &p->perf_dom->opp[st->desc_index + st->loop_idx];
395 	if (PROTOCOL_REV_MAJOR(p->version) <= 0x3)
396 		process_response_opp(opp, st->loop_idx, response);
397 	else
398 		process_response_opp_v4(ph->dev, p->perf_dom, opp, st->loop_idx,
399 					response);
400 	p->perf_dom->opp_count++;
401 
402 	dev_dbg(ph->dev, "Level %d Power %d Latency %dus Ifreq %d Index %d\n",
403 		opp->perf, opp->power, opp->trans_latency_us,
404 		opp->indicative_freq, opp->level_index);
405 
406 	return 0;
407 }
408 
409 static int
scmi_perf_describe_levels_get(const struct scmi_protocol_handle * ph,struct perf_dom_info * perf_dom,u32 version)410 scmi_perf_describe_levels_get(const struct scmi_protocol_handle *ph,
411 			      struct perf_dom_info *perf_dom, u32 version)
412 {
413 	int ret;
414 	void *iter;
415 	struct scmi_iterator_ops ops = {
416 		.prepare_message = iter_perf_levels_prepare_message,
417 		.update_state = iter_perf_levels_update_state,
418 		.process_response = iter_perf_levels_process_response,
419 	};
420 	struct scmi_perf_ipriv ppriv = {
421 		.version = version,
422 		.perf_dom = perf_dom,
423 	};
424 
425 	iter = ph->hops->iter_response_init(ph, &ops, MAX_OPPS,
426 					    PERF_DESCRIBE_LEVELS,
427 					    sizeof(struct scmi_msg_perf_describe_levels),
428 					    &ppriv);
429 	if (IS_ERR(iter))
430 		return PTR_ERR(iter);
431 
432 	ret = ph->hops->iter_response_run(iter);
433 	if (ret)
434 		return ret;
435 
436 	if (perf_dom->opp_count)
437 		sort(perf_dom->opp, perf_dom->opp_count,
438 		     sizeof(struct scmi_opp), opp_cmp_func, NULL);
439 
440 	return ret;
441 }
442 
scmi_perf_num_domains_get(const struct scmi_protocol_handle * ph)443 static int scmi_perf_num_domains_get(const struct scmi_protocol_handle *ph)
444 {
445 	struct scmi_perf_info *pi = ph->get_priv(ph);
446 
447 	return pi->num_domains;
448 }
449 
450 static inline struct perf_dom_info *
scmi_perf_domain_lookup(const struct scmi_protocol_handle * ph,u32 domain)451 scmi_perf_domain_lookup(const struct scmi_protocol_handle *ph, u32 domain)
452 {
453 	struct scmi_perf_info *pi = ph->get_priv(ph);
454 
455 	if (domain >= pi->num_domains)
456 		return ERR_PTR(-EINVAL);
457 
458 	return pi->dom_info + domain;
459 }
460 
461 static const struct scmi_perf_domain_info *
scmi_perf_info_get(const struct scmi_protocol_handle * ph,u32 domain)462 scmi_perf_info_get(const struct scmi_protocol_handle *ph, u32 domain)
463 {
464 	struct perf_dom_info *dom;
465 
466 	dom = scmi_perf_domain_lookup(ph, domain);
467 	if (IS_ERR(dom))
468 		return ERR_PTR(-EINVAL);
469 
470 	return &dom->info;
471 }
472 
scmi_perf_msg_limits_set(const struct scmi_protocol_handle * ph,u32 domain,u32 max_perf,u32 min_perf)473 static int scmi_perf_msg_limits_set(const struct scmi_protocol_handle *ph,
474 				    u32 domain, u32 max_perf, u32 min_perf)
475 {
476 	int ret;
477 	struct scmi_xfer *t;
478 	struct scmi_perf_set_limits *limits;
479 
480 	ret = ph->xops->xfer_get_init(ph, PERF_LIMITS_SET,
481 				      sizeof(*limits), 0, &t);
482 	if (ret)
483 		return ret;
484 
485 	limits = t->tx.buf;
486 	limits->domain = cpu_to_le32(domain);
487 	limits->max_level = cpu_to_le32(max_perf);
488 	limits->min_level = cpu_to_le32(min_perf);
489 
490 	ret = ph->xops->do_xfer(ph, t);
491 
492 	ph->xops->xfer_put(ph, t);
493 	return ret;
494 }
495 
__scmi_perf_limits_set(const struct scmi_protocol_handle * ph,struct perf_dom_info * dom,u32 max_perf,u32 min_perf)496 static int __scmi_perf_limits_set(const struct scmi_protocol_handle *ph,
497 				  struct perf_dom_info *dom, u32 max_perf,
498 				  u32 min_perf)
499 {
500 	if (dom->fc_info && dom->fc_info[PERF_FC_LIMIT].set_addr) {
501 		struct scmi_fc_info *fci = &dom->fc_info[PERF_FC_LIMIT];
502 
503 		trace_scmi_fc_call(SCMI_PROTOCOL_PERF, PERF_LIMITS_SET,
504 				   dom->id, min_perf, max_perf);
505 		iowrite32(max_perf, fci->set_addr);
506 		iowrite32(min_perf, fci->set_addr + 4);
507 		ph->hops->fastchannel_db_ring(fci->set_db);
508 		return 0;
509 	}
510 
511 	return scmi_perf_msg_limits_set(ph, dom->id, max_perf, min_perf);
512 }
513 
scmi_perf_limits_set(const struct scmi_protocol_handle * ph,u32 domain,u32 max_perf,u32 min_perf)514 static int scmi_perf_limits_set(const struct scmi_protocol_handle *ph,
515 				u32 domain, u32 max_perf, u32 min_perf)
516 {
517 	struct scmi_perf_info *pi = ph->get_priv(ph);
518 	struct perf_dom_info *dom;
519 
520 	dom = scmi_perf_domain_lookup(ph, domain);
521 	if (IS_ERR(dom))
522 		return PTR_ERR(dom);
523 
524 	if (PROTOCOL_REV_MAJOR(pi->version) >= 0x3 && !max_perf && !min_perf)
525 		return -EINVAL;
526 
527 	if (dom->level_indexing_mode) {
528 		struct scmi_opp *opp;
529 
530 		if (min_perf) {
531 			opp = xa_load(&dom->opps_by_lvl, min_perf);
532 			if (!opp)
533 				return -EIO;
534 
535 			min_perf = opp->level_index;
536 		}
537 
538 		if (max_perf) {
539 			opp = xa_load(&dom->opps_by_lvl, max_perf);
540 			if (!opp)
541 				return -EIO;
542 
543 			max_perf = opp->level_index;
544 		}
545 	}
546 
547 	return __scmi_perf_limits_set(ph, dom, max_perf, min_perf);
548 }
549 
scmi_perf_msg_limits_get(const struct scmi_protocol_handle * ph,u32 domain,u32 * max_perf,u32 * min_perf)550 static int scmi_perf_msg_limits_get(const struct scmi_protocol_handle *ph,
551 				    u32 domain, u32 *max_perf, u32 *min_perf)
552 {
553 	int ret;
554 	struct scmi_xfer *t;
555 	struct scmi_perf_get_limits *limits;
556 
557 	ret = ph->xops->xfer_get_init(ph, PERF_LIMITS_GET,
558 				      sizeof(__le32), 0, &t);
559 	if (ret)
560 		return ret;
561 
562 	put_unaligned_le32(domain, t->tx.buf);
563 
564 	ret = ph->xops->do_xfer(ph, t);
565 	if (!ret) {
566 		limits = t->rx.buf;
567 
568 		*max_perf = le32_to_cpu(limits->max_level);
569 		*min_perf = le32_to_cpu(limits->min_level);
570 	}
571 
572 	ph->xops->xfer_put(ph, t);
573 	return ret;
574 }
575 
__scmi_perf_limits_get(const struct scmi_protocol_handle * ph,struct perf_dom_info * dom,u32 * max_perf,u32 * min_perf)576 static int __scmi_perf_limits_get(const struct scmi_protocol_handle *ph,
577 				  struct perf_dom_info *dom, u32 *max_perf,
578 				  u32 *min_perf)
579 {
580 	if (dom->fc_info && dom->fc_info[PERF_FC_LIMIT].get_addr) {
581 		struct scmi_fc_info *fci = &dom->fc_info[PERF_FC_LIMIT];
582 
583 		*max_perf = ioread32(fci->get_addr);
584 		*min_perf = ioread32(fci->get_addr + 4);
585 		trace_scmi_fc_call(SCMI_PROTOCOL_PERF, PERF_LIMITS_GET,
586 				   dom->id, *min_perf, *max_perf);
587 		return 0;
588 	}
589 
590 	return scmi_perf_msg_limits_get(ph, dom->id, max_perf, min_perf);
591 }
592 
scmi_perf_limits_get(const struct scmi_protocol_handle * ph,u32 domain,u32 * max_perf,u32 * min_perf)593 static int scmi_perf_limits_get(const struct scmi_protocol_handle *ph,
594 				u32 domain, u32 *max_perf, u32 *min_perf)
595 {
596 	int ret;
597 	struct perf_dom_info *dom;
598 
599 	dom = scmi_perf_domain_lookup(ph, domain);
600 	if (IS_ERR(dom))
601 		return PTR_ERR(dom);
602 
603 	ret = __scmi_perf_limits_get(ph, dom, max_perf, min_perf);
604 	if (ret)
605 		return ret;
606 
607 	if (dom->level_indexing_mode) {
608 		struct scmi_opp *opp;
609 
610 		opp = xa_load(&dom->opps_by_idx, *min_perf);
611 		if (!opp)
612 			return -EIO;
613 
614 		*min_perf = opp->perf;
615 
616 		opp = xa_load(&dom->opps_by_idx, *max_perf);
617 		if (!opp)
618 			return -EIO;
619 
620 		*max_perf = opp->perf;
621 	}
622 
623 	return 0;
624 }
625 
scmi_perf_msg_level_set(const struct scmi_protocol_handle * ph,u32 domain,u32 level,bool poll)626 static int scmi_perf_msg_level_set(const struct scmi_protocol_handle *ph,
627 				   u32 domain, u32 level, bool poll)
628 {
629 	int ret;
630 	struct scmi_xfer *t;
631 	struct scmi_perf_set_level *lvl;
632 
633 	ret = ph->xops->xfer_get_init(ph, PERF_LEVEL_SET, sizeof(*lvl), 0, &t);
634 	if (ret)
635 		return ret;
636 
637 	t->hdr.poll_completion = poll;
638 	lvl = t->tx.buf;
639 	lvl->domain = cpu_to_le32(domain);
640 	lvl->level = cpu_to_le32(level);
641 
642 	ret = ph->xops->do_xfer(ph, t);
643 
644 	ph->xops->xfer_put(ph, t);
645 	return ret;
646 }
647 
__scmi_perf_level_set(const struct scmi_protocol_handle * ph,struct perf_dom_info * dom,u32 level,bool poll)648 static int __scmi_perf_level_set(const struct scmi_protocol_handle *ph,
649 				 struct perf_dom_info *dom, u32 level,
650 				 bool poll)
651 {
652 	if (dom->fc_info && dom->fc_info[PERF_FC_LEVEL].set_addr) {
653 		struct scmi_fc_info *fci = &dom->fc_info[PERF_FC_LEVEL];
654 
655 		trace_scmi_fc_call(SCMI_PROTOCOL_PERF, PERF_LEVEL_SET,
656 				   dom->id, level, 0);
657 		iowrite32(level, fci->set_addr);
658 		ph->hops->fastchannel_db_ring(fci->set_db);
659 		return 0;
660 	}
661 
662 	return scmi_perf_msg_level_set(ph, dom->id, level, poll);
663 }
664 
scmi_perf_level_set(const struct scmi_protocol_handle * ph,u32 domain,u32 level,bool poll)665 static int scmi_perf_level_set(const struct scmi_protocol_handle *ph,
666 			       u32 domain, u32 level, bool poll)
667 {
668 	struct perf_dom_info *dom;
669 
670 	dom = scmi_perf_domain_lookup(ph, domain);
671 	if (IS_ERR(dom))
672 		return PTR_ERR(dom);
673 
674 	if (dom->level_indexing_mode) {
675 		struct scmi_opp *opp;
676 
677 		opp = xa_load(&dom->opps_by_lvl, level);
678 		if (!opp)
679 			return -EIO;
680 
681 		level = opp->level_index;
682 	}
683 
684 	return __scmi_perf_level_set(ph, dom, level, poll);
685 }
686 
scmi_perf_msg_level_get(const struct scmi_protocol_handle * ph,u32 domain,u32 * level,bool poll)687 static int scmi_perf_msg_level_get(const struct scmi_protocol_handle *ph,
688 				   u32 domain, u32 *level, bool poll)
689 {
690 	int ret;
691 	struct scmi_xfer *t;
692 
693 	ret = ph->xops->xfer_get_init(ph, PERF_LEVEL_GET,
694 				     sizeof(u32), sizeof(u32), &t);
695 	if (ret)
696 		return ret;
697 
698 	t->hdr.poll_completion = poll;
699 	put_unaligned_le32(domain, t->tx.buf);
700 
701 	ret = ph->xops->do_xfer(ph, t);
702 	if (!ret)
703 		*level = get_unaligned_le32(t->rx.buf);
704 
705 	ph->xops->xfer_put(ph, t);
706 	return ret;
707 }
708 
__scmi_perf_level_get(const struct scmi_protocol_handle * ph,struct perf_dom_info * dom,u32 * level,bool poll)709 static int __scmi_perf_level_get(const struct scmi_protocol_handle *ph,
710 				 struct perf_dom_info *dom, u32 *level,
711 				 bool poll)
712 {
713 	if (dom->fc_info && dom->fc_info[PERF_FC_LEVEL].get_addr) {
714 		*level = ioread32(dom->fc_info[PERF_FC_LEVEL].get_addr);
715 		trace_scmi_fc_call(SCMI_PROTOCOL_PERF, PERF_LEVEL_GET,
716 				   dom->id, *level, 0);
717 		return 0;
718 	}
719 
720 	return scmi_perf_msg_level_get(ph, dom->id, level, poll);
721 }
722 
scmi_perf_level_get(const struct scmi_protocol_handle * ph,u32 domain,u32 * level,bool poll)723 static int scmi_perf_level_get(const struct scmi_protocol_handle *ph,
724 			       u32 domain, u32 *level, bool poll)
725 {
726 	int ret;
727 	struct perf_dom_info *dom;
728 
729 	dom = scmi_perf_domain_lookup(ph, domain);
730 	if (IS_ERR(dom))
731 		return PTR_ERR(dom);
732 
733 	ret = __scmi_perf_level_get(ph, dom, level, poll);
734 	if (ret)
735 		return ret;
736 
737 	if (dom->level_indexing_mode) {
738 		struct scmi_opp *opp;
739 
740 		opp = xa_load(&dom->opps_by_idx, *level);
741 		if (!opp)
742 			return -EIO;
743 
744 		*level = opp->perf;
745 	}
746 
747 	return 0;
748 }
749 
scmi_perf_level_limits_notify(const struct scmi_protocol_handle * ph,u32 domain,int message_id,bool enable)750 static int scmi_perf_level_limits_notify(const struct scmi_protocol_handle *ph,
751 					 u32 domain, int message_id,
752 					 bool enable)
753 {
754 	int ret;
755 	struct scmi_xfer *t;
756 	struct scmi_perf_notify_level_or_limits *notify;
757 
758 	ret = ph->xops->xfer_get_init(ph, message_id, sizeof(*notify), 0, &t);
759 	if (ret)
760 		return ret;
761 
762 	notify = t->tx.buf;
763 	notify->domain = cpu_to_le32(domain);
764 	notify->notify_enable = enable ? cpu_to_le32(BIT(0)) : 0;
765 
766 	ret = ph->xops->do_xfer(ph, t);
767 
768 	ph->xops->xfer_put(ph, t);
769 	return ret;
770 }
771 
scmi_perf_domain_init_fc(const struct scmi_protocol_handle * ph,u32 domain,struct scmi_fc_info ** p_fc)772 static void scmi_perf_domain_init_fc(const struct scmi_protocol_handle *ph,
773 				     u32 domain, struct scmi_fc_info **p_fc)
774 {
775 	struct scmi_fc_info *fc;
776 
777 	fc = devm_kcalloc(ph->dev, PERF_FC_MAX, sizeof(*fc), GFP_KERNEL);
778 	if (!fc)
779 		return;
780 
781 	ph->hops->fastchannel_init(ph, PERF_DESCRIBE_FASTCHANNEL,
782 				   PERF_LEVEL_SET, 4, domain,
783 				   &fc[PERF_FC_LEVEL].set_addr,
784 				   &fc[PERF_FC_LEVEL].set_db);
785 
786 	ph->hops->fastchannel_init(ph, PERF_DESCRIBE_FASTCHANNEL,
787 				   PERF_LEVEL_GET, 4, domain,
788 				   &fc[PERF_FC_LEVEL].get_addr, NULL);
789 
790 	ph->hops->fastchannel_init(ph, PERF_DESCRIBE_FASTCHANNEL,
791 				   PERF_LIMITS_SET, 8, domain,
792 				   &fc[PERF_FC_LIMIT].set_addr,
793 				   &fc[PERF_FC_LIMIT].set_db);
794 
795 	ph->hops->fastchannel_init(ph, PERF_DESCRIBE_FASTCHANNEL,
796 				   PERF_LIMITS_GET, 8, domain,
797 				   &fc[PERF_FC_LIMIT].get_addr, NULL);
798 
799 	*p_fc = fc;
800 }
801 
scmi_dvfs_device_opps_add(const struct scmi_protocol_handle * ph,struct device * dev,u32 domain)802 static int scmi_dvfs_device_opps_add(const struct scmi_protocol_handle *ph,
803 				     struct device *dev, u32 domain)
804 {
805 	int idx, ret;
806 	unsigned long freq;
807 	struct dev_pm_opp_data data = {};
808 	struct perf_dom_info *dom;
809 
810 	dom = scmi_perf_domain_lookup(ph, domain);
811 	if (IS_ERR(dom))
812 		return PTR_ERR(dom);
813 
814 	for (idx = 0; idx < dom->opp_count; idx++) {
815 		if (!dom->level_indexing_mode)
816 			freq = dom->opp[idx].perf * dom->mult_factor;
817 		else
818 			freq = dom->opp[idx].indicative_freq * dom->mult_factor;
819 
820 		data.level = dom->opp[idx].perf;
821 		data.freq = freq;
822 
823 		ret = dev_pm_opp_add_dynamic(dev, &data);
824 		if (ret) {
825 			dev_warn(dev, "failed to add opp %luHz\n", freq);
826 			dev_pm_opp_remove_all_dynamic(dev);
827 			return ret;
828 		}
829 
830 		dev_dbg(dev, "[%d][%s]:: Registered OPP[%d] %lu\n",
831 			domain, dom->info.name, idx, freq);
832 	}
833 	return 0;
834 }
835 
836 static int
scmi_dvfs_transition_latency_get(const struct scmi_protocol_handle * ph,u32 domain)837 scmi_dvfs_transition_latency_get(const struct scmi_protocol_handle *ph,
838 				 u32 domain)
839 {
840 	struct perf_dom_info *dom;
841 
842 	dom = scmi_perf_domain_lookup(ph, domain);
843 	if (IS_ERR(dom))
844 		return PTR_ERR(dom);
845 
846 	/* uS to nS */
847 	return dom->opp[dom->opp_count - 1].trans_latency_us * 1000;
848 }
849 
scmi_dvfs_freq_set(const struct scmi_protocol_handle * ph,u32 domain,unsigned long freq,bool poll)850 static int scmi_dvfs_freq_set(const struct scmi_protocol_handle *ph, u32 domain,
851 			      unsigned long freq, bool poll)
852 {
853 	unsigned int level;
854 	struct perf_dom_info *dom;
855 
856 	dom = scmi_perf_domain_lookup(ph, domain);
857 	if (IS_ERR(dom))
858 		return PTR_ERR(dom);
859 
860 	if (!dom->level_indexing_mode) {
861 		level = freq / dom->mult_factor;
862 	} else {
863 		struct scmi_opp *opp;
864 
865 		opp = LOOKUP_BY_FREQ(dom->opps_by_freq,
866 				     freq / dom->mult_factor);
867 		if (!opp)
868 			return -EIO;
869 
870 		level = opp->level_index;
871 	}
872 
873 	return __scmi_perf_level_set(ph, dom, level, poll);
874 }
875 
scmi_dvfs_freq_get(const struct scmi_protocol_handle * ph,u32 domain,unsigned long * freq,bool poll)876 static int scmi_dvfs_freq_get(const struct scmi_protocol_handle *ph, u32 domain,
877 			      unsigned long *freq, bool poll)
878 {
879 	int ret;
880 	u32 level;
881 	struct perf_dom_info *dom;
882 
883 	dom = scmi_perf_domain_lookup(ph, domain);
884 	if (IS_ERR(dom))
885 		return PTR_ERR(dom);
886 
887 	ret = __scmi_perf_level_get(ph, dom, &level, poll);
888 	if (ret)
889 		return ret;
890 
891 	if (!dom->level_indexing_mode) {
892 		*freq = level * dom->mult_factor;
893 	} else {
894 		struct scmi_opp *opp;
895 
896 		opp = xa_load(&dom->opps_by_idx, level);
897 		if (!opp)
898 			return -EIO;
899 
900 		*freq = opp->indicative_freq * dom->mult_factor;
901 	}
902 
903 	return ret;
904 }
905 
scmi_dvfs_est_power_get(const struct scmi_protocol_handle * ph,u32 domain,unsigned long * freq,unsigned long * power)906 static int scmi_dvfs_est_power_get(const struct scmi_protocol_handle *ph,
907 				   u32 domain, unsigned long *freq,
908 				   unsigned long *power)
909 {
910 	struct perf_dom_info *dom;
911 	unsigned long opp_freq;
912 	int idx, ret = -EINVAL;
913 	struct scmi_opp *opp;
914 
915 	dom = scmi_perf_domain_lookup(ph, domain);
916 	if (IS_ERR(dom))
917 		return PTR_ERR(dom);
918 
919 	for (opp = dom->opp, idx = 0; idx < dom->opp_count; idx++, opp++) {
920 		if (!dom->level_indexing_mode)
921 			opp_freq = opp->perf * dom->mult_factor;
922 		else
923 			opp_freq = opp->indicative_freq * dom->mult_factor;
924 
925 		if (opp_freq < *freq)
926 			continue;
927 
928 		*freq = opp_freq;
929 		*power = opp->power;
930 		ret = 0;
931 		break;
932 	}
933 
934 	return ret;
935 }
936 
scmi_fast_switch_possible(const struct scmi_protocol_handle * ph,u32 domain)937 static bool scmi_fast_switch_possible(const struct scmi_protocol_handle *ph,
938 				      u32 domain)
939 {
940 	struct perf_dom_info *dom;
941 
942 	dom = scmi_perf_domain_lookup(ph, domain);
943 	if (IS_ERR(dom))
944 		return false;
945 
946 	return dom->fc_info && dom->fc_info[PERF_FC_LEVEL].set_addr;
947 }
948 
949 static enum scmi_power_scale
scmi_power_scale_get(const struct scmi_protocol_handle * ph)950 scmi_power_scale_get(const struct scmi_protocol_handle *ph)
951 {
952 	struct scmi_perf_info *pi = ph->get_priv(ph);
953 
954 	return pi->power_scale;
955 }
956 
957 static const struct scmi_perf_proto_ops perf_proto_ops = {
958 	.num_domains_get = scmi_perf_num_domains_get,
959 	.info_get = scmi_perf_info_get,
960 	.limits_set = scmi_perf_limits_set,
961 	.limits_get = scmi_perf_limits_get,
962 	.level_set = scmi_perf_level_set,
963 	.level_get = scmi_perf_level_get,
964 	.transition_latency_get = scmi_dvfs_transition_latency_get,
965 	.device_opps_add = scmi_dvfs_device_opps_add,
966 	.freq_set = scmi_dvfs_freq_set,
967 	.freq_get = scmi_dvfs_freq_get,
968 	.est_power_get = scmi_dvfs_est_power_get,
969 	.fast_switch_possible = scmi_fast_switch_possible,
970 	.power_scale_get = scmi_power_scale_get,
971 };
972 
scmi_perf_set_notify_enabled(const struct scmi_protocol_handle * ph,u8 evt_id,u32 src_id,bool enable)973 static int scmi_perf_set_notify_enabled(const struct scmi_protocol_handle *ph,
974 					u8 evt_id, u32 src_id, bool enable)
975 {
976 	int ret, cmd_id;
977 
978 	if (evt_id >= ARRAY_SIZE(evt_2_cmd))
979 		return -EINVAL;
980 
981 	cmd_id = evt_2_cmd[evt_id];
982 	ret = scmi_perf_level_limits_notify(ph, src_id, cmd_id, enable);
983 	if (ret)
984 		pr_debug("FAIL_ENABLED - evt[%X] dom[%d] - ret:%d\n",
985 			 evt_id, src_id, ret);
986 
987 	return ret;
988 }
989 
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)990 static void *scmi_perf_fill_custom_report(const struct scmi_protocol_handle *ph,
991 					  u8 evt_id, ktime_t timestamp,
992 					  const void *payld, size_t payld_sz,
993 					  void *report, u32 *src_id)
994 {
995 	void *rep = NULL;
996 
997 	switch (evt_id) {
998 	case SCMI_EVENT_PERFORMANCE_LIMITS_CHANGED:
999 	{
1000 		const struct scmi_perf_limits_notify_payld *p = payld;
1001 		struct scmi_perf_limits_report *r = report;
1002 
1003 		if (sizeof(*p) != payld_sz)
1004 			break;
1005 
1006 		r->timestamp = timestamp;
1007 		r->agent_id = le32_to_cpu(p->agent_id);
1008 		r->domain_id = le32_to_cpu(p->domain_id);
1009 		r->range_max = le32_to_cpu(p->range_max);
1010 		r->range_min = le32_to_cpu(p->range_min);
1011 		*src_id = r->domain_id;
1012 		rep = r;
1013 		break;
1014 	}
1015 	case SCMI_EVENT_PERFORMANCE_LEVEL_CHANGED:
1016 	{
1017 		const struct scmi_perf_level_notify_payld *p = payld;
1018 		struct scmi_perf_level_report *r = report;
1019 
1020 		if (sizeof(*p) != payld_sz)
1021 			break;
1022 
1023 		r->timestamp = timestamp;
1024 		r->agent_id = le32_to_cpu(p->agent_id);
1025 		r->domain_id = le32_to_cpu(p->domain_id);
1026 		r->performance_level = le32_to_cpu(p->performance_level);
1027 		*src_id = r->domain_id;
1028 		rep = r;
1029 		break;
1030 	}
1031 	default:
1032 		break;
1033 	}
1034 
1035 	return rep;
1036 }
1037 
scmi_perf_get_num_sources(const struct scmi_protocol_handle * ph)1038 static int scmi_perf_get_num_sources(const struct scmi_protocol_handle *ph)
1039 {
1040 	struct scmi_perf_info *pi = ph->get_priv(ph);
1041 
1042 	if (!pi)
1043 		return -EINVAL;
1044 
1045 	return pi->num_domains;
1046 }
1047 
1048 static const struct scmi_event perf_events[] = {
1049 	{
1050 		.id = SCMI_EVENT_PERFORMANCE_LIMITS_CHANGED,
1051 		.max_payld_sz = sizeof(struct scmi_perf_limits_notify_payld),
1052 		.max_report_sz = sizeof(struct scmi_perf_limits_report),
1053 	},
1054 	{
1055 		.id = SCMI_EVENT_PERFORMANCE_LEVEL_CHANGED,
1056 		.max_payld_sz = sizeof(struct scmi_perf_level_notify_payld),
1057 		.max_report_sz = sizeof(struct scmi_perf_level_report),
1058 	},
1059 };
1060 
1061 static const struct scmi_event_ops perf_event_ops = {
1062 	.get_num_sources = scmi_perf_get_num_sources,
1063 	.set_notify_enabled = scmi_perf_set_notify_enabled,
1064 	.fill_custom_report = scmi_perf_fill_custom_report,
1065 };
1066 
1067 static const struct scmi_protocol_events perf_protocol_events = {
1068 	.queue_sz = SCMI_PROTO_QUEUE_SZ,
1069 	.ops = &perf_event_ops,
1070 	.evts = perf_events,
1071 	.num_events = ARRAY_SIZE(perf_events),
1072 };
1073 
scmi_perf_protocol_init(const struct scmi_protocol_handle * ph)1074 static int scmi_perf_protocol_init(const struct scmi_protocol_handle *ph)
1075 {
1076 	int domain, ret;
1077 	u32 version;
1078 	struct scmi_perf_info *pinfo;
1079 
1080 	ret = ph->xops->version_get(ph, &version);
1081 	if (ret)
1082 		return ret;
1083 
1084 	dev_dbg(ph->dev, "Performance Version %d.%d\n",
1085 		PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
1086 
1087 	pinfo = devm_kzalloc(ph->dev, sizeof(*pinfo), GFP_KERNEL);
1088 	if (!pinfo)
1089 		return -ENOMEM;
1090 
1091 	pinfo->version = version;
1092 
1093 	ret = scmi_perf_attributes_get(ph, pinfo);
1094 	if (ret)
1095 		return ret;
1096 
1097 	pinfo->dom_info = devm_kcalloc(ph->dev, pinfo->num_domains,
1098 				       sizeof(*pinfo->dom_info), GFP_KERNEL);
1099 	if (!pinfo->dom_info)
1100 		return -ENOMEM;
1101 
1102 	for (domain = 0; domain < pinfo->num_domains; domain++) {
1103 		struct perf_dom_info *dom = pinfo->dom_info + domain;
1104 
1105 		dom->id = domain;
1106 		scmi_perf_domain_attributes_get(ph, dom, version);
1107 		scmi_perf_describe_levels_get(ph, dom, version);
1108 
1109 		if (dom->perf_fastchannels)
1110 			scmi_perf_domain_init_fc(ph, dom->id, &dom->fc_info);
1111 	}
1112 
1113 	ret = devm_add_action_or_reset(ph->dev, scmi_perf_xa_destroy, pinfo);
1114 	if (ret)
1115 		return ret;
1116 
1117 	return ph->set_priv(ph, pinfo, version);
1118 }
1119 
1120 static const struct scmi_protocol scmi_perf = {
1121 	.id = SCMI_PROTOCOL_PERF,
1122 	.owner = THIS_MODULE,
1123 	.instance_init = &scmi_perf_protocol_init,
1124 	.ops = &perf_proto_ops,
1125 	.events = &perf_protocol_events,
1126 	.supported_version = SCMI_PROTOCOL_SUPPORTED_VERSION,
1127 };
1128 
1129 DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(perf, scmi_perf)
1130