Mention branches and keyring.
[releases.git] / qcom / rpmhpd.c
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 /* SM8150 RPMH powerdomains */
141
142 static struct rpmhpd sm8150_mmcx_ao;
143 static struct rpmhpd sm8150_mmcx = {
144         .pd = { .name = "mmcx", },
145         .peer = &sm8150_mmcx_ao,
146         .res_name = "mmcx.lvl",
147 };
148
149 static struct rpmhpd sm8150_mmcx_ao = {
150         .pd = { .name = "mmcx_ao", },
151         .active_only = true,
152         .peer = &sm8150_mmcx,
153         .res_name = "mmcx.lvl",
154 };
155
156 static struct rpmhpd *sm8150_rpmhpds[] = {
157         [SM8150_MSS] = &sdm845_mss,
158         [SM8150_EBI] = &sdm845_ebi,
159         [SM8150_LMX] = &sdm845_lmx,
160         [SM8150_LCX] = &sdm845_lcx,
161         [SM8150_GFX] = &sdm845_gfx,
162         [SM8150_MX] = &sdm845_mx,
163         [SM8150_MX_AO] = &sdm845_mx_ao,
164         [SM8150_CX] = &sdm845_cx,
165         [SM8150_CX_AO] = &sdm845_cx_ao,
166         [SM8150_MMCX] = &sm8150_mmcx,
167         [SM8150_MMCX_AO] = &sm8150_mmcx_ao,
168 };
169
170 static const struct rpmhpd_desc sm8150_desc = {
171         .rpmhpds = sm8150_rpmhpds,
172         .num_pds = ARRAY_SIZE(sm8150_rpmhpds),
173 };
174
175 static struct rpmhpd *sm8250_rpmhpds[] = {
176         [SM8250_CX] = &sdm845_cx,
177         [SM8250_CX_AO] = &sdm845_cx_ao,
178         [SM8250_EBI] = &sdm845_ebi,
179         [SM8250_GFX] = &sdm845_gfx,
180         [SM8250_LCX] = &sdm845_lcx,
181         [SM8250_LMX] = &sdm845_lmx,
182         [SM8250_MMCX] = &sm8150_mmcx,
183         [SM8250_MMCX_AO] = &sm8150_mmcx_ao,
184         [SM8250_MX] = &sdm845_mx,
185         [SM8250_MX_AO] = &sdm845_mx_ao,
186 };
187
188 static const struct rpmhpd_desc sm8250_desc = {
189         .rpmhpds = sm8250_rpmhpds,
190         .num_pds = ARRAY_SIZE(sm8250_rpmhpds),
191 };
192
193 /* SC7180 RPMH powerdomains */
194 static struct rpmhpd *sc7180_rpmhpds[] = {
195         [SC7180_CX] = &sdm845_cx,
196         [SC7180_CX_AO] = &sdm845_cx_ao,
197         [SC7180_GFX] = &sdm845_gfx,
198         [SC7180_MX] = &sdm845_mx,
199         [SC7180_MX_AO] = &sdm845_mx_ao,
200         [SC7180_LMX] = &sdm845_lmx,
201         [SC7180_LCX] = &sdm845_lcx,
202         [SC7180_MSS] = &sdm845_mss,
203 };
204
205 static const struct rpmhpd_desc sc7180_desc = {
206         .rpmhpds = sc7180_rpmhpds,
207         .num_pds = ARRAY_SIZE(sc7180_rpmhpds),
208 };
209
210 static const struct of_device_id rpmhpd_match_table[] = {
211         { .compatible = "qcom,sc7180-rpmhpd", .data = &sc7180_desc },
212         { .compatible = "qcom,sdm845-rpmhpd", .data = &sdm845_desc },
213         { .compatible = "qcom,sm8150-rpmhpd", .data = &sm8150_desc },
214         { .compatible = "qcom,sm8250-rpmhpd", .data = &sm8250_desc },
215         { }
216 };
217 MODULE_DEVICE_TABLE(of, rpmhpd_match_table);
218
219 static int rpmhpd_send_corner(struct rpmhpd *pd, int state,
220                               unsigned int corner, bool sync)
221 {
222         struct tcs_cmd cmd = {
223                 .addr = pd->addr,
224                 .data = corner,
225         };
226
227         /*
228          * Wait for an ack only when we are increasing the
229          * perf state of the power domain
230          */
231         if (sync)
232                 return rpmh_write(pd->dev, state, &cmd, 1);
233         else
234                 return rpmh_write_async(pd->dev, state, &cmd, 1);
235 }
236
237 static void to_active_sleep(struct rpmhpd *pd, unsigned int corner,
238                             unsigned int *active, unsigned int *sleep)
239 {
240         *active = corner;
241
242         if (pd->active_only)
243                 *sleep = 0;
244         else
245                 *sleep = *active;
246 }
247
248 /*
249  * This function is used to aggregate the votes across the active only
250  * resources and its peers. The aggregated votes are sent to RPMh as
251  * ACTIVE_ONLY votes (which take effect immediately), as WAKE_ONLY votes
252  * (applied by RPMh on system wakeup) and as SLEEP votes (applied by RPMh
253  * on system sleep).
254  * We send ACTIVE_ONLY votes for resources without any peers. For others,
255  * which have an active only peer, all 3 votes are sent.
256  */
257 static int rpmhpd_aggregate_corner(struct rpmhpd *pd, unsigned int corner)
258 {
259         int ret;
260         struct rpmhpd *peer = pd->peer;
261         unsigned int active_corner, sleep_corner;
262         unsigned int this_active_corner = 0, this_sleep_corner = 0;
263         unsigned int peer_active_corner = 0, peer_sleep_corner = 0;
264         unsigned int peer_enabled_corner;
265
266         to_active_sleep(pd, corner, &this_active_corner, &this_sleep_corner);
267
268         if (peer && peer->enabled) {
269                 peer_enabled_corner = max(peer->corner, peer->enable_corner);
270                 to_active_sleep(peer, peer_enabled_corner, &peer_active_corner,
271                                 &peer_sleep_corner);
272         }
273
274         active_corner = max(this_active_corner, peer_active_corner);
275
276         ret = rpmhpd_send_corner(pd, RPMH_ACTIVE_ONLY_STATE, active_corner,
277                                  active_corner > pd->active_corner);
278         if (ret)
279                 return ret;
280
281         pd->active_corner = active_corner;
282
283         if (peer) {
284                 peer->active_corner = active_corner;
285
286                 ret = rpmhpd_send_corner(pd, RPMH_WAKE_ONLY_STATE,
287                                          active_corner, false);
288                 if (ret)
289                         return ret;
290
291                 sleep_corner = max(this_sleep_corner, peer_sleep_corner);
292
293                 return rpmhpd_send_corner(pd, RPMH_SLEEP_STATE, sleep_corner,
294                                           false);
295         }
296
297         return ret;
298 }
299
300 static int rpmhpd_power_on(struct generic_pm_domain *domain)
301 {
302         struct rpmhpd *pd = domain_to_rpmhpd(domain);
303         unsigned int corner;
304         int ret;
305
306         mutex_lock(&rpmhpd_lock);
307
308         corner = max(pd->corner, pd->enable_corner);
309         ret = rpmhpd_aggregate_corner(pd, corner);
310         if (!ret)
311                 pd->enabled = true;
312
313         mutex_unlock(&rpmhpd_lock);
314
315         return ret;
316 }
317
318 static int rpmhpd_power_off(struct generic_pm_domain *domain)
319 {
320         struct rpmhpd *pd = domain_to_rpmhpd(domain);
321         int ret;
322
323         mutex_lock(&rpmhpd_lock);
324
325         ret = rpmhpd_aggregate_corner(pd, 0);
326         if (!ret)
327                 pd->enabled = false;
328
329         mutex_unlock(&rpmhpd_lock);
330
331         return ret;
332 }
333
334 static int rpmhpd_set_performance_state(struct generic_pm_domain *domain,
335                                         unsigned int level)
336 {
337         struct rpmhpd *pd = domain_to_rpmhpd(domain);
338         int ret = 0, i;
339
340         mutex_lock(&rpmhpd_lock);
341
342         for (i = 0; i < pd->level_count; i++)
343                 if (level <= pd->level[i])
344                         break;
345
346         /*
347          * If the level requested is more than that supported by the
348          * max corner, just set it to max anyway.
349          */
350         if (i == pd->level_count)
351                 i--;
352
353         if (pd->enabled) {
354                 /* Ensure that the domain isn't turn off */
355                 if (i < pd->enable_corner)
356                         i = pd->enable_corner;
357
358                 ret = rpmhpd_aggregate_corner(pd, i);
359                 if (ret)
360                         goto out;
361         }
362
363         pd->corner = i;
364 out:
365         mutex_unlock(&rpmhpd_lock);
366
367         return ret;
368 }
369
370 static unsigned int rpmhpd_get_performance_state(struct generic_pm_domain *genpd,
371                                                  struct dev_pm_opp *opp)
372 {
373         return dev_pm_opp_get_level(opp);
374 }
375
376 static int rpmhpd_update_level_mapping(struct rpmhpd *rpmhpd)
377 {
378         int i;
379         const u16 *buf;
380
381         buf = cmd_db_read_aux_data(rpmhpd->res_name, &rpmhpd->level_count);
382         if (IS_ERR(buf))
383                 return PTR_ERR(buf);
384
385         /* 2 bytes used for each command DB aux data entry */
386         rpmhpd->level_count >>= 1;
387
388         if (rpmhpd->level_count > RPMH_ARC_MAX_LEVELS)
389                 return -EINVAL;
390
391         for (i = 0; i < rpmhpd->level_count; i++) {
392                 rpmhpd->level[i] = buf[i];
393
394                 /* Remember the first corner with non-zero level */
395                 if (!rpmhpd->level[rpmhpd->enable_corner] && rpmhpd->level[i])
396                         rpmhpd->enable_corner = i;
397
398                 /*
399                  * The AUX data may be zero padded.  These 0 valued entries at
400                  * the end of the map must be ignored.
401                  */
402                 if (i > 0 && rpmhpd->level[i] == 0) {
403                         rpmhpd->level_count = i;
404                         break;
405                 }
406                 pr_debug("%s: ARC hlvl=%2d --> vlvl=%4u\n", rpmhpd->res_name, i,
407                          rpmhpd->level[i]);
408         }
409
410         return 0;
411 }
412
413 static int rpmhpd_probe(struct platform_device *pdev)
414 {
415         int i, ret;
416         size_t num_pds;
417         struct device *dev = &pdev->dev;
418         struct genpd_onecell_data *data;
419         struct rpmhpd **rpmhpds;
420         const struct rpmhpd_desc *desc;
421
422         desc = of_device_get_match_data(dev);
423         if (!desc)
424                 return -EINVAL;
425
426         rpmhpds = desc->rpmhpds;
427         num_pds = desc->num_pds;
428
429         data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
430         if (!data)
431                 return -ENOMEM;
432
433         data->domains = devm_kcalloc(dev, num_pds, sizeof(*data->domains),
434                                      GFP_KERNEL);
435         if (!data->domains)
436                 return -ENOMEM;
437
438         data->num_domains = num_pds;
439
440         for (i = 0; i < num_pds; i++) {
441                 if (!rpmhpds[i]) {
442                         dev_warn(dev, "rpmhpds[%d] is empty\n", i);
443                         continue;
444                 }
445
446                 rpmhpds[i]->dev = dev;
447                 rpmhpds[i]->addr = cmd_db_read_addr(rpmhpds[i]->res_name);
448                 if (!rpmhpds[i]->addr) {
449                         dev_err(dev, "Could not find RPMh address for resource %s\n",
450                                 rpmhpds[i]->res_name);
451                         return -ENODEV;
452                 }
453
454                 ret = cmd_db_read_slave_id(rpmhpds[i]->res_name);
455                 if (ret != CMD_DB_HW_ARC) {
456                         dev_err(dev, "RPMh slave ID mismatch\n");
457                         return -EINVAL;
458                 }
459
460                 ret = rpmhpd_update_level_mapping(rpmhpds[i]);
461                 if (ret)
462                         return ret;
463
464                 rpmhpds[i]->pd.power_off = rpmhpd_power_off;
465                 rpmhpds[i]->pd.power_on = rpmhpd_power_on;
466                 rpmhpds[i]->pd.set_performance_state = rpmhpd_set_performance_state;
467                 rpmhpds[i]->pd.opp_to_performance_state = rpmhpd_get_performance_state;
468                 pm_genpd_init(&rpmhpds[i]->pd, NULL, true);
469
470                 data->domains[i] = &rpmhpds[i]->pd;
471         }
472
473         /* Add subdomains */
474         for (i = 0; i < num_pds; i++) {
475                 if (!rpmhpds[i])
476                         continue;
477                 if (rpmhpds[i]->parent)
478                         pm_genpd_add_subdomain(rpmhpds[i]->parent,
479                                                &rpmhpds[i]->pd);
480         }
481
482         return of_genpd_add_provider_onecell(pdev->dev.of_node, data);
483 }
484
485 static struct platform_driver rpmhpd_driver = {
486         .driver = {
487                 .name = "qcom-rpmhpd",
488                 .of_match_table = rpmhpd_match_table,
489                 .suppress_bind_attrs = true,
490         },
491         .probe = rpmhpd_probe,
492 };
493
494 static int __init rpmhpd_init(void)
495 {
496         return platform_driver_register(&rpmhpd_driver);
497 }
498 core_initcall(rpmhpd_init);
499
500 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. RPMh Power Domain Driver");
501 MODULE_LICENSE("GPL v2");