• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, The Linux Foundation. All rights reserved.*/
3 
4 #include <linux/err.h>
5 #include <linux/init.h>
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/mutex.h>
9 #include <linux/pm_domain.h>
10 #include <linux/slab.h>
11 #include <linux/of.h>
12 #include <linux/of_device.h>
13 #include <linux/platform_device.h>
14 #include <linux/pm_opp.h>
15 #include <soc/qcom/cmd-db.h>
16 #include <soc/qcom/rpmh.h>
17 #include <dt-bindings/power/qcom-rpmpd.h>
18 
19 #define domain_to_rpmhpd(domain) container_of(domain, struct rpmhpd, pd)
20 
21 #define RPMH_ARC_MAX_LEVELS	16
22 
23 /**
24  * struct rpmhpd - top level RPMh power domain resource data structure
25  * @dev:		rpmh power domain controller device
26  * @pd:			generic_pm_domain corrresponding to the power domain
27  * @parent:		generic_pm_domain corrresponding to the parent's power domain
28  * @peer:		A peer power domain in case Active only Voting is
29  *			supported
30  * @active_only:	True if it represents an Active only peer
31  * @corner:		current corner
32  * @active_corner:	current active corner
33  * @enable_corner:	lowest non-zero corner
34  * @level:		An array of level (vlvl) to corner (hlvl) mappings
35  *			derived from cmd-db
36  * @level_count:	Number of levels supported by the power domain. max
37  *			being 16 (0 - 15)
38  * @enabled:		true if the power domain is enabled
39  * @res_name:		Resource name used for cmd-db lookup
40  * @addr:		Resource address as looped up using resource name from
41  *			cmd-db
42  */
43 struct rpmhpd {
44 	struct device	*dev;
45 	struct generic_pm_domain pd;
46 	struct generic_pm_domain *parent;
47 	struct rpmhpd	*peer;
48 	const bool	active_only;
49 	unsigned int	corner;
50 	unsigned int	active_corner;
51 	unsigned int	enable_corner;
52 	u32		level[RPMH_ARC_MAX_LEVELS];
53 	size_t		level_count;
54 	bool		enabled;
55 	const char	*res_name;
56 	u32		addr;
57 };
58 
59 struct rpmhpd_desc {
60 	struct rpmhpd **rpmhpds;
61 	size_t num_pds;
62 };
63 
64 static DEFINE_MUTEX(rpmhpd_lock);
65 
66 /* SDM845 RPMH powerdomains */
67 
68 static struct rpmhpd sdm845_ebi = {
69 	.pd = { .name = "ebi", },
70 	.res_name = "ebi.lvl",
71 };
72 
73 static struct rpmhpd sdm845_lmx = {
74 	.pd = { .name = "lmx", },
75 	.res_name = "lmx.lvl",
76 };
77 
78 static struct rpmhpd sdm845_lcx = {
79 	.pd = { .name = "lcx", },
80 	.res_name = "lcx.lvl",
81 };
82 
83 static struct rpmhpd sdm845_gfx = {
84 	.pd = { .name = "gfx", },
85 	.res_name = "gfx.lvl",
86 };
87 
88 static struct rpmhpd sdm845_mss = {
89 	.pd = { .name = "mss", },
90 	.res_name = "mss.lvl",
91 };
92 
93 static struct rpmhpd sdm845_mx_ao;
94 static struct rpmhpd sdm845_mx = {
95 	.pd = { .name = "mx", },
96 	.peer = &sdm845_mx_ao,
97 	.res_name = "mx.lvl",
98 };
99 
100 static struct rpmhpd sdm845_mx_ao = {
101 	.pd = { .name = "mx_ao", },
102 	.active_only = true,
103 	.peer = &sdm845_mx,
104 	.res_name = "mx.lvl",
105 };
106 
107 static struct rpmhpd sdm845_cx_ao;
108 static struct rpmhpd sdm845_cx = {
109 	.pd = { .name = "cx", },
110 	.peer = &sdm845_cx_ao,
111 	.parent = &sdm845_mx.pd,
112 	.res_name = "cx.lvl",
113 };
114 
115 static struct rpmhpd sdm845_cx_ao = {
116 	.pd = { .name = "cx_ao", },
117 	.active_only = true,
118 	.peer = &sdm845_cx,
119 	.parent = &sdm845_mx_ao.pd,
120 	.res_name = "cx.lvl",
121 };
122 
123 static struct rpmhpd *sdm845_rpmhpds[] = {
124 	[SDM845_EBI] = &sdm845_ebi,
125 	[SDM845_MX] = &sdm845_mx,
126 	[SDM845_MX_AO] = &sdm845_mx_ao,
127 	[SDM845_CX] = &sdm845_cx,
128 	[SDM845_CX_AO] = &sdm845_cx_ao,
129 	[SDM845_LMX] = &sdm845_lmx,
130 	[SDM845_LCX] = &sdm845_lcx,
131 	[SDM845_GFX] = &sdm845_gfx,
132 	[SDM845_MSS] = &sdm845_mss,
133 };
134 
135 static const struct rpmhpd_desc sdm845_desc = {
136 	.rpmhpds = sdm845_rpmhpds,
137 	.num_pds = ARRAY_SIZE(sdm845_rpmhpds),
138 };
139 
140 /* SDX55 RPMH powerdomains */
141 static struct rpmhpd *sdx55_rpmhpds[] = {
142 	[SDX55_MSS] = &sdm845_mss,
143 	[SDX55_MX] = &sdm845_mx,
144 	[SDX55_CX] = &sdm845_cx,
145 };
146 
147 static const struct rpmhpd_desc sdx55_desc = {
148 	.rpmhpds = sdx55_rpmhpds,
149 	.num_pds = ARRAY_SIZE(sdx55_rpmhpds),
150 };
151 
152 /* SM8150 RPMH powerdomains */
153 
154 static struct rpmhpd sm8150_mmcx_ao;
155 static struct rpmhpd sm8150_mmcx = {
156 	.pd = { .name = "mmcx", },
157 	.peer = &sm8150_mmcx_ao,
158 	.res_name = "mmcx.lvl",
159 };
160 
161 static struct rpmhpd sm8150_mmcx_ao = {
162 	.pd = { .name = "mmcx_ao", },
163 	.active_only = true,
164 	.peer = &sm8150_mmcx,
165 	.res_name = "mmcx.lvl",
166 };
167 
168 static struct rpmhpd *sm8150_rpmhpds[] = {
169 	[SM8150_MSS] = &sdm845_mss,
170 	[SM8150_EBI] = &sdm845_ebi,
171 	[SM8150_LMX] = &sdm845_lmx,
172 	[SM8150_LCX] = &sdm845_lcx,
173 	[SM8150_GFX] = &sdm845_gfx,
174 	[SM8150_MX] = &sdm845_mx,
175 	[SM8150_MX_AO] = &sdm845_mx_ao,
176 	[SM8150_CX] = &sdm845_cx,
177 	[SM8150_CX_AO] = &sdm845_cx_ao,
178 	[SM8150_MMCX] = &sm8150_mmcx,
179 	[SM8150_MMCX_AO] = &sm8150_mmcx_ao,
180 };
181 
182 static const struct rpmhpd_desc sm8150_desc = {
183 	.rpmhpds = sm8150_rpmhpds,
184 	.num_pds = ARRAY_SIZE(sm8150_rpmhpds),
185 };
186 
187 static struct rpmhpd *sm8250_rpmhpds[] = {
188 	[SM8250_CX] = &sdm845_cx,
189 	[SM8250_CX_AO] = &sdm845_cx_ao,
190 	[SM8250_EBI] = &sdm845_ebi,
191 	[SM8250_GFX] = &sdm845_gfx,
192 	[SM8250_LCX] = &sdm845_lcx,
193 	[SM8250_LMX] = &sdm845_lmx,
194 	[SM8250_MMCX] = &sm8150_mmcx,
195 	[SM8250_MMCX_AO] = &sm8150_mmcx_ao,
196 	[SM8250_MX] = &sdm845_mx,
197 	[SM8250_MX_AO] = &sdm845_mx_ao,
198 };
199 
200 static const struct rpmhpd_desc sm8250_desc = {
201 	.rpmhpds = sm8250_rpmhpds,
202 	.num_pds = ARRAY_SIZE(sm8250_rpmhpds),
203 };
204 
205 /* SM8350 Power domains */
206 static struct rpmhpd sm8350_mxc_ao;
207 static struct rpmhpd sm8350_mxc = {
208 	.pd = { .name = "mxc", },
209 	.peer = &sm8350_mxc_ao,
210 	.res_name = "mxc.lvl",
211 };
212 
213 static struct rpmhpd sm8350_mxc_ao = {
214 	.pd = { .name = "mxc_ao", },
215 	.active_only = true,
216 	.peer = &sm8350_mxc,
217 	.res_name = "mxc.lvl",
218 };
219 
220 static struct rpmhpd *sm8350_rpmhpds[] = {
221 	[SM8350_CX] = &sdm845_cx,
222 	[SM8350_CX_AO] = &sdm845_cx_ao,
223 	[SM8350_EBI] = &sdm845_ebi,
224 	[SM8350_GFX] = &sdm845_gfx,
225 	[SM8350_LCX] = &sdm845_lcx,
226 	[SM8350_LMX] = &sdm845_lmx,
227 	[SM8350_MMCX] = &sm8150_mmcx,
228 	[SM8350_MMCX_AO] = &sm8150_mmcx_ao,
229 	[SM8350_MX] = &sdm845_mx,
230 	[SM8350_MX_AO] = &sdm845_mx_ao,
231 	[SM8350_MXC] = &sm8350_mxc,
232 	[SM8350_MXC_AO] = &sm8350_mxc_ao,
233 	[SM8350_MSS] = &sdm845_mss,
234 };
235 
236 static const struct rpmhpd_desc sm8350_desc = {
237 	.rpmhpds = sm8350_rpmhpds,
238 	.num_pds = ARRAY_SIZE(sm8350_rpmhpds),
239 };
240 
241 /* SC7180 RPMH powerdomains */
242 static struct rpmhpd *sc7180_rpmhpds[] = {
243 	[SC7180_CX] = &sdm845_cx,
244 	[SC7180_CX_AO] = &sdm845_cx_ao,
245 	[SC7180_GFX] = &sdm845_gfx,
246 	[SC7180_MX] = &sdm845_mx,
247 	[SC7180_MX_AO] = &sdm845_mx_ao,
248 	[SC7180_LMX] = &sdm845_lmx,
249 	[SC7180_LCX] = &sdm845_lcx,
250 	[SC7180_MSS] = &sdm845_mss,
251 };
252 
253 static const struct rpmhpd_desc sc7180_desc = {
254 	.rpmhpds = sc7180_rpmhpds,
255 	.num_pds = ARRAY_SIZE(sc7180_rpmhpds),
256 };
257 
258 /* SC7280 RPMH powerdomains */
259 static struct rpmhpd *sc7280_rpmhpds[] = {
260 	[SC7280_CX] = &sdm845_cx,
261 	[SC7280_CX_AO] = &sdm845_cx_ao,
262 	[SC7280_EBI] = &sdm845_ebi,
263 	[SC7280_GFX] = &sdm845_gfx,
264 	[SC7280_MX] = &sdm845_mx,
265 	[SC7280_MX_AO] = &sdm845_mx_ao,
266 	[SC7280_LMX] = &sdm845_lmx,
267 	[SC7280_LCX] = &sdm845_lcx,
268 	[SC7280_MSS] = &sdm845_mss,
269 };
270 
271 static const struct rpmhpd_desc sc7280_desc = {
272 	.rpmhpds = sc7280_rpmhpds,
273 	.num_pds = ARRAY_SIZE(sc7280_rpmhpds),
274 };
275 
276 /* SC8180x RPMH powerdomains */
277 static struct rpmhpd *sc8180x_rpmhpds[] = {
278 	[SC8180X_CX] = &sdm845_cx,
279 	[SC8180X_CX_AO] = &sdm845_cx_ao,
280 	[SC8180X_EBI] = &sdm845_ebi,
281 	[SC8180X_GFX] = &sdm845_gfx,
282 	[SC8180X_LCX] = &sdm845_lcx,
283 	[SC8180X_LMX] = &sdm845_lmx,
284 	[SC8180X_MMCX] = &sm8150_mmcx,
285 	[SC8180X_MMCX_AO] = &sm8150_mmcx_ao,
286 	[SC8180X_MSS] = &sdm845_mss,
287 	[SC8180X_MX] = &sdm845_mx,
288 	[SC8180X_MX_AO] = &sdm845_mx_ao,
289 };
290 
291 static const struct rpmhpd_desc sc8180x_desc = {
292 	.rpmhpds = sc8180x_rpmhpds,
293 	.num_pds = ARRAY_SIZE(sc8180x_rpmhpds),
294 };
295 
296 static const struct of_device_id rpmhpd_match_table[] = {
297 	{ .compatible = "qcom,sc7180-rpmhpd", .data = &sc7180_desc },
298 	{ .compatible = "qcom,sc7280-rpmhpd", .data = &sc7280_desc },
299 	{ .compatible = "qcom,sc8180x-rpmhpd", .data = &sc8180x_desc },
300 	{ .compatible = "qcom,sdm845-rpmhpd", .data = &sdm845_desc },
301 	{ .compatible = "qcom,sdx55-rpmhpd", .data = &sdx55_desc},
302 	{ .compatible = "qcom,sm8150-rpmhpd", .data = &sm8150_desc },
303 	{ .compatible = "qcom,sm8250-rpmhpd", .data = &sm8250_desc },
304 	{ .compatible = "qcom,sm8350-rpmhpd", .data = &sm8350_desc },
305 	{ }
306 };
307 MODULE_DEVICE_TABLE(of, rpmhpd_match_table);
308 
rpmhpd_send_corner(struct rpmhpd * pd,int state,unsigned int corner,bool sync)309 static int rpmhpd_send_corner(struct rpmhpd *pd, int state,
310 			      unsigned int corner, bool sync)
311 {
312 	struct tcs_cmd cmd = {
313 		.addr = pd->addr,
314 		.data = corner,
315 	};
316 
317 	/*
318 	 * Wait for an ack only when we are increasing the
319 	 * perf state of the power domain
320 	 */
321 	if (sync)
322 		return rpmh_write(pd->dev, state, &cmd, 1);
323 	else
324 		return rpmh_write_async(pd->dev, state, &cmd, 1);
325 }
326 
to_active_sleep(struct rpmhpd * pd,unsigned int corner,unsigned int * active,unsigned int * sleep)327 static void to_active_sleep(struct rpmhpd *pd, unsigned int corner,
328 			    unsigned int *active, unsigned int *sleep)
329 {
330 	*active = corner;
331 
332 	if (pd->active_only)
333 		*sleep = 0;
334 	else
335 		*sleep = *active;
336 }
337 
338 /*
339  * This function is used to aggregate the votes across the active only
340  * resources and its peers. The aggregated votes are sent to RPMh as
341  * ACTIVE_ONLY votes (which take effect immediately), as WAKE_ONLY votes
342  * (applied by RPMh on system wakeup) and as SLEEP votes (applied by RPMh
343  * on system sleep).
344  * We send ACTIVE_ONLY votes for resources without any peers. For others,
345  * which have an active only peer, all 3 votes are sent.
346  */
rpmhpd_aggregate_corner(struct rpmhpd * pd,unsigned int corner)347 static int rpmhpd_aggregate_corner(struct rpmhpd *pd, unsigned int corner)
348 {
349 	int ret;
350 	struct rpmhpd *peer = pd->peer;
351 	unsigned int active_corner, sleep_corner;
352 	unsigned int this_active_corner = 0, this_sleep_corner = 0;
353 	unsigned int peer_active_corner = 0, peer_sleep_corner = 0;
354 	unsigned int peer_enabled_corner;
355 
356 	to_active_sleep(pd, corner, &this_active_corner, &this_sleep_corner);
357 
358 	if (peer && peer->enabled) {
359 		peer_enabled_corner = max(peer->corner, peer->enable_corner);
360 		to_active_sleep(peer, peer_enabled_corner, &peer_active_corner,
361 				&peer_sleep_corner);
362 	}
363 
364 	active_corner = max(this_active_corner, peer_active_corner);
365 
366 	ret = rpmhpd_send_corner(pd, RPMH_ACTIVE_ONLY_STATE, active_corner,
367 				 active_corner > pd->active_corner);
368 	if (ret)
369 		return ret;
370 
371 	pd->active_corner = active_corner;
372 
373 	if (peer) {
374 		peer->active_corner = active_corner;
375 
376 		ret = rpmhpd_send_corner(pd, RPMH_WAKE_ONLY_STATE,
377 					 active_corner, false);
378 		if (ret)
379 			return ret;
380 
381 		sleep_corner = max(this_sleep_corner, peer_sleep_corner);
382 
383 		return rpmhpd_send_corner(pd, RPMH_SLEEP_STATE, sleep_corner,
384 					  false);
385 	}
386 
387 	return ret;
388 }
389 
rpmhpd_power_on(struct generic_pm_domain * domain)390 static int rpmhpd_power_on(struct generic_pm_domain *domain)
391 {
392 	struct rpmhpd *pd = domain_to_rpmhpd(domain);
393 	unsigned int corner;
394 	int ret;
395 
396 	mutex_lock(&rpmhpd_lock);
397 
398 	corner = max(pd->corner, pd->enable_corner);
399 	ret = rpmhpd_aggregate_corner(pd, corner);
400 	if (!ret)
401 		pd->enabled = true;
402 
403 	mutex_unlock(&rpmhpd_lock);
404 
405 	return ret;
406 }
407 
rpmhpd_power_off(struct generic_pm_domain * domain)408 static int rpmhpd_power_off(struct generic_pm_domain *domain)
409 {
410 	struct rpmhpd *pd = domain_to_rpmhpd(domain);
411 	int ret;
412 
413 	mutex_lock(&rpmhpd_lock);
414 
415 	ret = rpmhpd_aggregate_corner(pd, 0);
416 	if (!ret)
417 		pd->enabled = false;
418 
419 	mutex_unlock(&rpmhpd_lock);
420 
421 	return ret;
422 }
423 
rpmhpd_set_performance_state(struct generic_pm_domain * domain,unsigned int level)424 static int rpmhpd_set_performance_state(struct generic_pm_domain *domain,
425 					unsigned int level)
426 {
427 	struct rpmhpd *pd = domain_to_rpmhpd(domain);
428 	int ret = 0, i;
429 
430 	mutex_lock(&rpmhpd_lock);
431 
432 	for (i = 0; i < pd->level_count; i++)
433 		if (level <= pd->level[i])
434 			break;
435 
436 	/*
437 	 * If the level requested is more than that supported by the
438 	 * max corner, just set it to max anyway.
439 	 */
440 	if (i == pd->level_count)
441 		i--;
442 
443 	if (pd->enabled) {
444 		/* Ensure that the domain isn't turn off */
445 		if (i < pd->enable_corner)
446 			i = pd->enable_corner;
447 
448 		ret = rpmhpd_aggregate_corner(pd, i);
449 		if (ret)
450 			goto out;
451 	}
452 
453 	pd->corner = i;
454 out:
455 	mutex_unlock(&rpmhpd_lock);
456 
457 	return ret;
458 }
459 
rpmhpd_get_performance_state(struct generic_pm_domain * genpd,struct dev_pm_opp * opp)460 static unsigned int rpmhpd_get_performance_state(struct generic_pm_domain *genpd,
461 						 struct dev_pm_opp *opp)
462 {
463 	return dev_pm_opp_get_level(opp);
464 }
465 
rpmhpd_update_level_mapping(struct rpmhpd * rpmhpd)466 static int rpmhpd_update_level_mapping(struct rpmhpd *rpmhpd)
467 {
468 	int i;
469 	const u16 *buf;
470 
471 	buf = cmd_db_read_aux_data(rpmhpd->res_name, &rpmhpd->level_count);
472 	if (IS_ERR(buf))
473 		return PTR_ERR(buf);
474 
475 	/* 2 bytes used for each command DB aux data entry */
476 	rpmhpd->level_count >>= 1;
477 
478 	if (rpmhpd->level_count > RPMH_ARC_MAX_LEVELS)
479 		return -EINVAL;
480 
481 	for (i = 0; i < rpmhpd->level_count; i++) {
482 		rpmhpd->level[i] = buf[i];
483 
484 		/* Remember the first corner with non-zero level */
485 		if (!rpmhpd->level[rpmhpd->enable_corner] && rpmhpd->level[i])
486 			rpmhpd->enable_corner = i;
487 
488 		/*
489 		 * The AUX data may be zero padded.  These 0 valued entries at
490 		 * the end of the map must be ignored.
491 		 */
492 		if (i > 0 && rpmhpd->level[i] == 0) {
493 			rpmhpd->level_count = i;
494 			break;
495 		}
496 		pr_debug("%s: ARC hlvl=%2d --> vlvl=%4u\n", rpmhpd->res_name, i,
497 			 rpmhpd->level[i]);
498 	}
499 
500 	return 0;
501 }
502 
rpmhpd_probe(struct platform_device * pdev)503 static int rpmhpd_probe(struct platform_device *pdev)
504 {
505 	int i, ret;
506 	size_t num_pds;
507 	struct device *dev = &pdev->dev;
508 	struct genpd_onecell_data *data;
509 	struct rpmhpd **rpmhpds;
510 	const struct rpmhpd_desc *desc;
511 
512 	desc = of_device_get_match_data(dev);
513 	if (!desc)
514 		return -EINVAL;
515 
516 	rpmhpds = desc->rpmhpds;
517 	num_pds = desc->num_pds;
518 
519 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
520 	if (!data)
521 		return -ENOMEM;
522 
523 	data->domains = devm_kcalloc(dev, num_pds, sizeof(*data->domains),
524 				     GFP_KERNEL);
525 	if (!data->domains)
526 		return -ENOMEM;
527 
528 	data->num_domains = num_pds;
529 
530 	for (i = 0; i < num_pds; i++) {
531 		if (!rpmhpds[i]) {
532 			dev_warn(dev, "rpmhpds[%d] is empty\n", i);
533 			continue;
534 		}
535 
536 		rpmhpds[i]->dev = dev;
537 		rpmhpds[i]->addr = cmd_db_read_addr(rpmhpds[i]->res_name);
538 		if (!rpmhpds[i]->addr) {
539 			dev_err(dev, "Could not find RPMh address for resource %s\n",
540 				rpmhpds[i]->res_name);
541 			return -ENODEV;
542 		}
543 
544 		ret = cmd_db_read_slave_id(rpmhpds[i]->res_name);
545 		if (ret != CMD_DB_HW_ARC) {
546 			dev_err(dev, "RPMh slave ID mismatch\n");
547 			return -EINVAL;
548 		}
549 
550 		ret = rpmhpd_update_level_mapping(rpmhpds[i]);
551 		if (ret)
552 			return ret;
553 
554 		rpmhpds[i]->pd.power_off = rpmhpd_power_off;
555 		rpmhpds[i]->pd.power_on = rpmhpd_power_on;
556 		rpmhpds[i]->pd.set_performance_state = rpmhpd_set_performance_state;
557 		rpmhpds[i]->pd.opp_to_performance_state = rpmhpd_get_performance_state;
558 		pm_genpd_init(&rpmhpds[i]->pd, NULL, true);
559 
560 		data->domains[i] = &rpmhpds[i]->pd;
561 	}
562 
563 	/* Add subdomains */
564 	for (i = 0; i < num_pds; i++) {
565 		if (!rpmhpds[i])
566 			continue;
567 		if (rpmhpds[i]->parent)
568 			pm_genpd_add_subdomain(rpmhpds[i]->parent,
569 					       &rpmhpds[i]->pd);
570 	}
571 
572 	return of_genpd_add_provider_onecell(pdev->dev.of_node, data);
573 }
574 
575 static struct platform_driver rpmhpd_driver = {
576 	.driver = {
577 		.name = "qcom-rpmhpd",
578 		.of_match_table = rpmhpd_match_table,
579 		.suppress_bind_attrs = true,
580 	},
581 	.probe = rpmhpd_probe,
582 };
583 
rpmhpd_init(void)584 static int __init rpmhpd_init(void)
585 {
586 	return platform_driver_register(&rpmhpd_driver);
587 }
588 core_initcall(rpmhpd_init);
589 
590 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. RPMh Power Domain Driver");
591 MODULE_LICENSE("GPL v2");
592