rpmhpd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2018, The Linux Foundation. All rights reserved.*/
  3. #include <linux/err.h>
  4. #include <linux/init.h>
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/mutex.h>
  8. #include <linux/pm_domain.h>
  9. #include <linux/slab.h>
  10. #include <linux/of.h>
  11. #include <linux/of_device.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/pm_opp.h>
  14. #include <soc/qcom/cmd-db.h>
  15. #include <soc/qcom/rpmh.h>
  16. #include <dt-bindings/power/qcom-rpmpd.h>
  17. #define domain_to_rpmhpd(domain) container_of(domain, struct rpmhpd, pd)
  18. #define RPMH_ARC_MAX_LEVELS 16
  19. /**
  20. * struct rpmhpd - top level RPMh power domain resource data structure
  21. * @dev: rpmh power domain controller device
  22. * @pd: generic_pm_domain corrresponding to the power domain
  23. * @parent: generic_pm_domain corrresponding to the parent's power domain
  24. * @peer: A peer power domain in case Active only Voting is
  25. * supported
  26. * @active_only: True if it represents an Active only peer
  27. * @corner: current corner
  28. * @active_corner: current active corner
  29. * @enable_corner: lowest non-zero corner
  30. * @level: An array of level (vlvl) to corner (hlvl) mappings
  31. * derived from cmd-db
  32. * @level_count: Number of levels supported by the power domain. max
  33. * being 16 (0 - 15)
  34. * @enabled: true if the power domain is enabled
  35. * @res_name: Resource name used for cmd-db lookup
  36. * @addr: Resource address as looped up using resource name from
  37. * cmd-db
  38. */
  39. struct rpmhpd {
  40. struct device *dev;
  41. struct generic_pm_domain pd;
  42. struct generic_pm_domain *parent;
  43. struct rpmhpd *peer;
  44. const bool active_only;
  45. unsigned int corner;
  46. unsigned int active_corner;
  47. unsigned int enable_corner;
  48. u32 level[RPMH_ARC_MAX_LEVELS];
  49. size_t level_count;
  50. bool enabled;
  51. const char *res_name;
  52. u32 addr;
  53. };
  54. struct rpmhpd_desc {
  55. struct rpmhpd **rpmhpds;
  56. size_t num_pds;
  57. };
  58. static DEFINE_MUTEX(rpmhpd_lock);
  59. /* SDM845 RPMH powerdomains */
  60. static struct rpmhpd sdm845_ebi = {
  61. .pd = { .name = "ebi", },
  62. .res_name = "ebi.lvl",
  63. };
  64. static struct rpmhpd sdm845_lmx = {
  65. .pd = { .name = "lmx", },
  66. .res_name = "lmx.lvl",
  67. };
  68. static struct rpmhpd sdm845_lcx = {
  69. .pd = { .name = "lcx", },
  70. .res_name = "lcx.lvl",
  71. };
  72. static struct rpmhpd sdm845_gfx = {
  73. .pd = { .name = "gfx", },
  74. .res_name = "gfx.lvl",
  75. };
  76. static struct rpmhpd sdm845_mss = {
  77. .pd = { .name = "mss", },
  78. .res_name = "mss.lvl",
  79. };
  80. static struct rpmhpd sdm845_mx_ao;
  81. static struct rpmhpd sdm845_mx = {
  82. .pd = { .name = "mx", },
  83. .peer = &sdm845_mx_ao,
  84. .res_name = "mx.lvl",
  85. };
  86. static struct rpmhpd sdm845_mx_ao = {
  87. .pd = { .name = "mx_ao", },
  88. .active_only = true,
  89. .peer = &sdm845_mx,
  90. .res_name = "mx.lvl",
  91. };
  92. static struct rpmhpd sdm845_cx_ao;
  93. static struct rpmhpd sdm845_cx = {
  94. .pd = { .name = "cx", },
  95. .peer = &sdm845_cx_ao,
  96. .parent = &sdm845_mx.pd,
  97. .res_name = "cx.lvl",
  98. };
  99. static struct rpmhpd sdm845_cx_ao = {
  100. .pd = { .name = "cx_ao", },
  101. .active_only = true,
  102. .peer = &sdm845_cx,
  103. .parent = &sdm845_mx_ao.pd,
  104. .res_name = "cx.lvl",
  105. };
  106. static struct rpmhpd *sdm845_rpmhpds[] = {
  107. [SDM845_EBI] = &sdm845_ebi,
  108. [SDM845_MX] = &sdm845_mx,
  109. [SDM845_MX_AO] = &sdm845_mx_ao,
  110. [SDM845_CX] = &sdm845_cx,
  111. [SDM845_CX_AO] = &sdm845_cx_ao,
  112. [SDM845_LMX] = &sdm845_lmx,
  113. [SDM845_LCX] = &sdm845_lcx,
  114. [SDM845_GFX] = &sdm845_gfx,
  115. [SDM845_MSS] = &sdm845_mss,
  116. };
  117. static const struct rpmhpd_desc sdm845_desc = {
  118. .rpmhpds = sdm845_rpmhpds,
  119. .num_pds = ARRAY_SIZE(sdm845_rpmhpds),
  120. };
  121. /* SM8150 RPMH powerdomains */
  122. static struct rpmhpd sm8150_mmcx_ao;
  123. static struct rpmhpd sm8150_mmcx = {
  124. .pd = { .name = "mmcx", },
  125. .peer = &sm8150_mmcx_ao,
  126. .res_name = "mmcx.lvl",
  127. };
  128. static struct rpmhpd sm8150_mmcx_ao = {
  129. .pd = { .name = "mmcx_ao", },
  130. .active_only = true,
  131. .peer = &sm8150_mmcx,
  132. .res_name = "mmcx.lvl",
  133. };
  134. static struct rpmhpd *sm8150_rpmhpds[] = {
  135. [SM8150_MSS] = &sdm845_mss,
  136. [SM8150_EBI] = &sdm845_ebi,
  137. [SM8150_LMX] = &sdm845_lmx,
  138. [SM8150_LCX] = &sdm845_lcx,
  139. [SM8150_GFX] = &sdm845_gfx,
  140. [SM8150_MX] = &sdm845_mx,
  141. [SM8150_MX_AO] = &sdm845_mx_ao,
  142. [SM8150_CX] = &sdm845_cx,
  143. [SM8150_CX_AO] = &sdm845_cx_ao,
  144. [SM8150_MMCX] = &sm8150_mmcx,
  145. [SM8150_MMCX_AO] = &sm8150_mmcx_ao,
  146. };
  147. static const struct rpmhpd_desc sm8150_desc = {
  148. .rpmhpds = sm8150_rpmhpds,
  149. .num_pds = ARRAY_SIZE(sm8150_rpmhpds),
  150. };
  151. static struct rpmhpd *sm8250_rpmhpds[] = {
  152. [SM8250_CX] = &sdm845_cx,
  153. [SM8250_CX_AO] = &sdm845_cx_ao,
  154. [SM8250_EBI] = &sdm845_ebi,
  155. [SM8250_GFX] = &sdm845_gfx,
  156. [SM8250_LCX] = &sdm845_lcx,
  157. [SM8250_LMX] = &sdm845_lmx,
  158. [SM8250_MMCX] = &sm8150_mmcx,
  159. [SM8250_MMCX_AO] = &sm8150_mmcx_ao,
  160. [SM8250_MX] = &sdm845_mx,
  161. [SM8250_MX_AO] = &sdm845_mx_ao,
  162. };
  163. static const struct rpmhpd_desc sm8250_desc = {
  164. .rpmhpds = sm8250_rpmhpds,
  165. .num_pds = ARRAY_SIZE(sm8250_rpmhpds),
  166. };
  167. /* SC7180 RPMH powerdomains */
  168. static struct rpmhpd *sc7180_rpmhpds[] = {
  169. [SC7180_CX] = &sdm845_cx,
  170. [SC7180_CX_AO] = &sdm845_cx_ao,
  171. [SC7180_GFX] = &sdm845_gfx,
  172. [SC7180_MX] = &sdm845_mx,
  173. [SC7180_MX_AO] = &sdm845_mx_ao,
  174. [SC7180_LMX] = &sdm845_lmx,
  175. [SC7180_LCX] = &sdm845_lcx,
  176. [SC7180_MSS] = &sdm845_mss,
  177. };
  178. static const struct rpmhpd_desc sc7180_desc = {
  179. .rpmhpds = sc7180_rpmhpds,
  180. .num_pds = ARRAY_SIZE(sc7180_rpmhpds),
  181. };
  182. static const struct of_device_id rpmhpd_match_table[] = {
  183. { .compatible = "qcom,sc7180-rpmhpd", .data = &sc7180_desc },
  184. { .compatible = "qcom,sdm845-rpmhpd", .data = &sdm845_desc },
  185. { .compatible = "qcom,sm8150-rpmhpd", .data = &sm8150_desc },
  186. { .compatible = "qcom,sm8250-rpmhpd", .data = &sm8250_desc },
  187. { }
  188. };
  189. MODULE_DEVICE_TABLE(of, rpmhpd_match_table);
  190. static int rpmhpd_send_corner(struct rpmhpd *pd, int state,
  191. unsigned int corner, bool sync)
  192. {
  193. struct tcs_cmd cmd = {
  194. .addr = pd->addr,
  195. .data = corner,
  196. };
  197. /*
  198. * Wait for an ack only when we are increasing the
  199. * perf state of the power domain
  200. */
  201. if (sync)
  202. return rpmh_write(pd->dev, state, &cmd, 1);
  203. else
  204. return rpmh_write_async(pd->dev, state, &cmd, 1);
  205. }
  206. static void to_active_sleep(struct rpmhpd *pd, unsigned int corner,
  207. unsigned int *active, unsigned int *sleep)
  208. {
  209. *active = corner;
  210. if (pd->active_only)
  211. *sleep = 0;
  212. else
  213. *sleep = *active;
  214. }
  215. /*
  216. * This function is used to aggregate the votes across the active only
  217. * resources and its peers. The aggregated votes are sent to RPMh as
  218. * ACTIVE_ONLY votes (which take effect immediately), as WAKE_ONLY votes
  219. * (applied by RPMh on system wakeup) and as SLEEP votes (applied by RPMh
  220. * on system sleep).
  221. * We send ACTIVE_ONLY votes for resources without any peers. For others,
  222. * which have an active only peer, all 3 votes are sent.
  223. */
  224. static int rpmhpd_aggregate_corner(struct rpmhpd *pd, unsigned int corner)
  225. {
  226. int ret;
  227. struct rpmhpd *peer = pd->peer;
  228. unsigned int active_corner, sleep_corner;
  229. unsigned int this_active_corner = 0, this_sleep_corner = 0;
  230. unsigned int peer_active_corner = 0, peer_sleep_corner = 0;
  231. to_active_sleep(pd, corner, &this_active_corner, &this_sleep_corner);
  232. if (peer && peer->enabled)
  233. to_active_sleep(peer, peer->corner, &peer_active_corner,
  234. &peer_sleep_corner);
  235. active_corner = max(this_active_corner, peer_active_corner);
  236. ret = rpmhpd_send_corner(pd, RPMH_ACTIVE_ONLY_STATE, active_corner,
  237. active_corner > pd->active_corner);
  238. if (ret)
  239. return ret;
  240. pd->active_corner = active_corner;
  241. if (peer) {
  242. peer->active_corner = active_corner;
  243. ret = rpmhpd_send_corner(pd, RPMH_WAKE_ONLY_STATE,
  244. active_corner, false);
  245. if (ret)
  246. return ret;
  247. sleep_corner = max(this_sleep_corner, peer_sleep_corner);
  248. return rpmhpd_send_corner(pd, RPMH_SLEEP_STATE, sleep_corner,
  249. false);
  250. }
  251. return ret;
  252. }
  253. static int rpmhpd_power_on(struct generic_pm_domain *domain)
  254. {
  255. struct rpmhpd *pd = domain_to_rpmhpd(domain);
  256. unsigned int corner;
  257. int ret;
  258. mutex_lock(&rpmhpd_lock);
  259. corner = max(pd->corner, pd->enable_corner);
  260. ret = rpmhpd_aggregate_corner(pd, corner);
  261. if (!ret)
  262. pd->enabled = true;
  263. mutex_unlock(&rpmhpd_lock);
  264. return ret;
  265. }
  266. static int rpmhpd_power_off(struct generic_pm_domain *domain)
  267. {
  268. struct rpmhpd *pd = domain_to_rpmhpd(domain);
  269. int ret;
  270. mutex_lock(&rpmhpd_lock);
  271. ret = rpmhpd_aggregate_corner(pd, 0);
  272. if (!ret)
  273. pd->enabled = false;
  274. mutex_unlock(&rpmhpd_lock);
  275. return ret;
  276. }
  277. static int rpmhpd_set_performance_state(struct generic_pm_domain *domain,
  278. unsigned int level)
  279. {
  280. struct rpmhpd *pd = domain_to_rpmhpd(domain);
  281. int ret = 0, i;
  282. mutex_lock(&rpmhpd_lock);
  283. for (i = 0; i < pd->level_count; i++)
  284. if (level <= pd->level[i])
  285. break;
  286. /*
  287. * If the level requested is more than that supported by the
  288. * max corner, just set it to max anyway.
  289. */
  290. if (i == pd->level_count)
  291. i--;
  292. if (pd->enabled) {
  293. /* Ensure that the domain isn't turn off */
  294. if (i < pd->enable_corner)
  295. i = pd->enable_corner;
  296. ret = rpmhpd_aggregate_corner(pd, i);
  297. if (ret)
  298. goto out;
  299. }
  300. pd->corner = i;
  301. out:
  302. mutex_unlock(&rpmhpd_lock);
  303. return ret;
  304. }
  305. static unsigned int rpmhpd_get_performance_state(struct generic_pm_domain *genpd,
  306. struct dev_pm_opp *opp)
  307. {
  308. return dev_pm_opp_get_level(opp);
  309. }
  310. static int rpmhpd_update_level_mapping(struct rpmhpd *rpmhpd)
  311. {
  312. int i;
  313. const u16 *buf;
  314. buf = cmd_db_read_aux_data(rpmhpd->res_name, &rpmhpd->level_count);
  315. if (IS_ERR(buf))
  316. return PTR_ERR(buf);
  317. /* 2 bytes used for each command DB aux data entry */
  318. rpmhpd->level_count >>= 1;
  319. if (rpmhpd->level_count > RPMH_ARC_MAX_LEVELS)
  320. return -EINVAL;
  321. for (i = 0; i < rpmhpd->level_count; i++) {
  322. rpmhpd->level[i] = buf[i];
  323. /* Remember the first corner with non-zero level */
  324. if (!rpmhpd->level[rpmhpd->enable_corner] && rpmhpd->level[i])
  325. rpmhpd->enable_corner = i;
  326. /*
  327. * The AUX data may be zero padded. These 0 valued entries at
  328. * the end of the map must be ignored.
  329. */
  330. if (i > 0 && rpmhpd->level[i] == 0) {
  331. rpmhpd->level_count = i;
  332. break;
  333. }
  334. pr_debug("%s: ARC hlvl=%2d --> vlvl=%4u\n", rpmhpd->res_name, i,
  335. rpmhpd->level[i]);
  336. }
  337. return 0;
  338. }
  339. static int rpmhpd_probe(struct platform_device *pdev)
  340. {
  341. int i, ret;
  342. size_t num_pds;
  343. struct device *dev = &pdev->dev;
  344. struct genpd_onecell_data *data;
  345. struct rpmhpd **rpmhpds;
  346. const struct rpmhpd_desc *desc;
  347. desc = of_device_get_match_data(dev);
  348. if (!desc)
  349. return -EINVAL;
  350. rpmhpds = desc->rpmhpds;
  351. num_pds = desc->num_pds;
  352. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  353. if (!data)
  354. return -ENOMEM;
  355. data->domains = devm_kcalloc(dev, num_pds, sizeof(*data->domains),
  356. GFP_KERNEL);
  357. if (!data->domains)
  358. return -ENOMEM;
  359. data->num_domains = num_pds;
  360. for (i = 0; i < num_pds; i++) {
  361. if (!rpmhpds[i]) {
  362. dev_warn(dev, "rpmhpds[%d] is empty\n", i);
  363. continue;
  364. }
  365. rpmhpds[i]->dev = dev;
  366. rpmhpds[i]->addr = cmd_db_read_addr(rpmhpds[i]->res_name);
  367. if (!rpmhpds[i]->addr) {
  368. dev_err(dev, "Could not find RPMh address for resource %s\n",
  369. rpmhpds[i]->res_name);
  370. return -ENODEV;
  371. }
  372. ret = cmd_db_read_slave_id(rpmhpds[i]->res_name);
  373. if (ret != CMD_DB_HW_ARC) {
  374. dev_err(dev, "RPMh slave ID mismatch\n");
  375. return -EINVAL;
  376. }
  377. ret = rpmhpd_update_level_mapping(rpmhpds[i]);
  378. if (ret)
  379. return ret;
  380. rpmhpds[i]->pd.power_off = rpmhpd_power_off;
  381. rpmhpds[i]->pd.power_on = rpmhpd_power_on;
  382. rpmhpds[i]->pd.set_performance_state = rpmhpd_set_performance_state;
  383. rpmhpds[i]->pd.opp_to_performance_state = rpmhpd_get_performance_state;
  384. pm_genpd_init(&rpmhpds[i]->pd, NULL, true);
  385. data->domains[i] = &rpmhpds[i]->pd;
  386. }
  387. /* Add subdomains */
  388. for (i = 0; i < num_pds; i++) {
  389. if (!rpmhpds[i])
  390. continue;
  391. if (rpmhpds[i]->parent)
  392. pm_genpd_add_subdomain(rpmhpds[i]->parent,
  393. &rpmhpds[i]->pd);
  394. }
  395. return of_genpd_add_provider_onecell(pdev->dev.of_node, data);
  396. }
  397. static struct platform_driver rpmhpd_driver = {
  398. .driver = {
  399. .name = "qcom-rpmhpd",
  400. .of_match_table = rpmhpd_match_table,
  401. .suppress_bind_attrs = true,
  402. },
  403. .probe = rpmhpd_probe,
  404. };
  405. static int __init rpmhpd_init(void)
  406. {
  407. return platform_driver_register(&rpmhpd_driver);
  408. }
  409. core_initcall(rpmhpd_init);
  410. MODULE_DESCRIPTION("Qualcomm Technologies, Inc. RPMh Power Domain Driver");
  411. MODULE_LICENSE("GPL v2");