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