rpmhpd.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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/mutex.h>
  7. #include <linux/pm_domain.h>
  8. #include <linux/slab.h>
  9. #include <linux/of.h>
  10. #include <linux/of_device.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/pm_opp.h>
  13. #include <soc/qcom/cmd-db.h>
  14. #include <soc/qcom/rpmh.h>
  15. #include <dt-bindings/power/qcom-rpmpd.h>
  16. #define domain_to_rpmhpd(domain) container_of(domain, struct rpmhpd, pd)
  17. #define RPMH_ARC_MAX_LEVELS 16
  18. /**
  19. * struct rpmhpd - top level RPMh power domain resource data structure
  20. * @dev: rpmh power domain controller device
  21. * @pd: generic_pm_domain corrresponding to the power domain
  22. * @peer: A peer power domain in case Active only Voting is
  23. * supported
  24. * @active_only: True if it represents an Active only peer
  25. * @level: An array of level (vlvl) to corner (hlvl) mappings
  26. * derived from cmd-db
  27. * @level_count: Number of levels supported by the power domain. max
  28. * being 16 (0 - 15)
  29. * @enabled: true if the power domain is enabled
  30. * @res_name: Resource name used for cmd-db lookup
  31. * @addr: Resource address as looped up using resource name from
  32. * cmd-db
  33. */
  34. struct rpmhpd {
  35. struct device *dev;
  36. struct generic_pm_domain pd;
  37. struct generic_pm_domain *parent;
  38. struct rpmhpd *peer;
  39. const bool active_only;
  40. unsigned int corner;
  41. unsigned int active_corner;
  42. u32 level[RPMH_ARC_MAX_LEVELS];
  43. size_t level_count;
  44. bool enabled;
  45. const char *res_name;
  46. u32 addr;
  47. };
  48. struct rpmhpd_desc {
  49. struct rpmhpd **rpmhpds;
  50. size_t num_pds;
  51. };
  52. static DEFINE_MUTEX(rpmhpd_lock);
  53. /* SDM845 RPMH powerdomains */
  54. static struct rpmhpd sdm845_ebi = {
  55. .pd = { .name = "ebi", },
  56. .res_name = "ebi.lvl",
  57. };
  58. static struct rpmhpd sdm845_lmx = {
  59. .pd = { .name = "lmx", },
  60. .res_name = "lmx.lvl",
  61. };
  62. static struct rpmhpd sdm845_lcx = {
  63. .pd = { .name = "lcx", },
  64. .res_name = "lcx.lvl",
  65. };
  66. static struct rpmhpd sdm845_gfx = {
  67. .pd = { .name = "gfx", },
  68. .res_name = "gfx.lvl",
  69. };
  70. static struct rpmhpd sdm845_mss = {
  71. .pd = { .name = "mss", },
  72. .res_name = "mss.lvl",
  73. };
  74. static struct rpmhpd sdm845_mx_ao;
  75. static struct rpmhpd sdm845_mx = {
  76. .pd = { .name = "mx", },
  77. .peer = &sdm845_mx_ao,
  78. .res_name = "mx.lvl",
  79. };
  80. static struct rpmhpd sdm845_mx_ao = {
  81. .pd = { .name = "mx_ao", },
  82. .active_only = true,
  83. .peer = &sdm845_mx,
  84. .res_name = "mx.lvl",
  85. };
  86. static struct rpmhpd sdm845_cx_ao;
  87. static struct rpmhpd sdm845_cx = {
  88. .pd = { .name = "cx", },
  89. .peer = &sdm845_cx_ao,
  90. .parent = &sdm845_mx.pd,
  91. .res_name = "cx.lvl",
  92. };
  93. static struct rpmhpd sdm845_cx_ao = {
  94. .pd = { .name = "cx_ao", },
  95. .active_only = true,
  96. .peer = &sdm845_cx,
  97. .parent = &sdm845_mx_ao.pd,
  98. .res_name = "cx.lvl",
  99. };
  100. static struct rpmhpd *sdm845_rpmhpds[] = {
  101. [SDM845_EBI] = &sdm845_ebi,
  102. [SDM845_MX] = &sdm845_mx,
  103. [SDM845_MX_AO] = &sdm845_mx_ao,
  104. [SDM845_CX] = &sdm845_cx,
  105. [SDM845_CX_AO] = &sdm845_cx_ao,
  106. [SDM845_LMX] = &sdm845_lmx,
  107. [SDM845_LCX] = &sdm845_lcx,
  108. [SDM845_GFX] = &sdm845_gfx,
  109. [SDM845_MSS] = &sdm845_mss,
  110. };
  111. static const struct rpmhpd_desc sdm845_desc = {
  112. .rpmhpds = sdm845_rpmhpds,
  113. .num_pds = ARRAY_SIZE(sdm845_rpmhpds),
  114. };
  115. static const struct of_device_id rpmhpd_match_table[] = {
  116. { .compatible = "qcom,sdm845-rpmhpd", .data = &sdm845_desc },
  117. { }
  118. };
  119. static int rpmhpd_send_corner(struct rpmhpd *pd, int state,
  120. unsigned int corner, bool sync)
  121. {
  122. struct tcs_cmd cmd = {
  123. .addr = pd->addr,
  124. .data = corner,
  125. };
  126. /*
  127. * Wait for an ack only when we are increasing the
  128. * perf state of the power domain
  129. */
  130. if (sync)
  131. return rpmh_write(pd->dev, state, &cmd, 1);
  132. else
  133. return rpmh_write_async(pd->dev, state, &cmd, 1);
  134. }
  135. static void to_active_sleep(struct rpmhpd *pd, unsigned int corner,
  136. unsigned int *active, unsigned int *sleep)
  137. {
  138. *active = corner;
  139. if (pd->active_only)
  140. *sleep = 0;
  141. else
  142. *sleep = *active;
  143. }
  144. /*
  145. * This function is used to aggregate the votes across the active only
  146. * resources and its peers. The aggregated votes are sent to RPMh as
  147. * ACTIVE_ONLY votes (which take effect immediately), as WAKE_ONLY votes
  148. * (applied by RPMh on system wakeup) and as SLEEP votes (applied by RPMh
  149. * on system sleep).
  150. * We send ACTIVE_ONLY votes for resources without any peers. For others,
  151. * which have an active only peer, all 3 votes are sent.
  152. */
  153. static int rpmhpd_aggregate_corner(struct rpmhpd *pd, unsigned int corner)
  154. {
  155. int ret;
  156. struct rpmhpd *peer = pd->peer;
  157. unsigned int active_corner, sleep_corner;
  158. unsigned int this_active_corner = 0, this_sleep_corner = 0;
  159. unsigned int peer_active_corner = 0, peer_sleep_corner = 0;
  160. to_active_sleep(pd, corner, &this_active_corner, &this_sleep_corner);
  161. if (peer && peer->enabled)
  162. to_active_sleep(peer, peer->corner, &peer_active_corner,
  163. &peer_sleep_corner);
  164. active_corner = max(this_active_corner, peer_active_corner);
  165. ret = rpmhpd_send_corner(pd, RPMH_ACTIVE_ONLY_STATE, active_corner,
  166. active_corner > pd->active_corner);
  167. if (ret)
  168. return ret;
  169. pd->active_corner = active_corner;
  170. if (peer) {
  171. peer->active_corner = active_corner;
  172. ret = rpmhpd_send_corner(pd, RPMH_WAKE_ONLY_STATE,
  173. active_corner, false);
  174. if (ret)
  175. return ret;
  176. sleep_corner = max(this_sleep_corner, peer_sleep_corner);
  177. return rpmhpd_send_corner(pd, RPMH_SLEEP_STATE, sleep_corner,
  178. false);
  179. }
  180. return ret;
  181. }
  182. static int rpmhpd_power_on(struct generic_pm_domain *domain)
  183. {
  184. struct rpmhpd *pd = domain_to_rpmhpd(domain);
  185. int ret = 0;
  186. mutex_lock(&rpmhpd_lock);
  187. if (pd->corner)
  188. ret = rpmhpd_aggregate_corner(pd, pd->corner);
  189. if (!ret)
  190. pd->enabled = true;
  191. mutex_unlock(&rpmhpd_lock);
  192. return ret;
  193. }
  194. static int rpmhpd_power_off(struct generic_pm_domain *domain)
  195. {
  196. struct rpmhpd *pd = domain_to_rpmhpd(domain);
  197. int ret;
  198. mutex_lock(&rpmhpd_lock);
  199. ret = rpmhpd_aggregate_corner(pd, 0);
  200. if (!ret)
  201. pd->enabled = false;
  202. mutex_unlock(&rpmhpd_lock);
  203. return ret;
  204. }
  205. static int rpmhpd_set_performance_state(struct generic_pm_domain *domain,
  206. unsigned int level)
  207. {
  208. struct rpmhpd *pd = domain_to_rpmhpd(domain);
  209. int ret = 0, i;
  210. mutex_lock(&rpmhpd_lock);
  211. for (i = 0; i < pd->level_count; i++)
  212. if (level <= pd->level[i])
  213. break;
  214. /*
  215. * If the level requested is more than that supported by the
  216. * max corner, just set it to max anyway.
  217. */
  218. if (i == pd->level_count)
  219. i--;
  220. if (pd->enabled) {
  221. ret = rpmhpd_aggregate_corner(pd, i);
  222. if (ret)
  223. goto out;
  224. }
  225. pd->corner = i;
  226. out:
  227. mutex_unlock(&rpmhpd_lock);
  228. return ret;
  229. }
  230. static unsigned int rpmhpd_get_performance_state(struct generic_pm_domain *genpd,
  231. struct dev_pm_opp *opp)
  232. {
  233. return dev_pm_opp_get_level(opp);
  234. }
  235. static int rpmhpd_update_level_mapping(struct rpmhpd *rpmhpd)
  236. {
  237. int i;
  238. const u16 *buf;
  239. buf = cmd_db_read_aux_data(rpmhpd->res_name, &rpmhpd->level_count);
  240. if (IS_ERR(buf))
  241. return PTR_ERR(buf);
  242. /* 2 bytes used for each command DB aux data entry */
  243. rpmhpd->level_count >>= 1;
  244. if (rpmhpd->level_count > RPMH_ARC_MAX_LEVELS)
  245. return -EINVAL;
  246. for (i = 0; i < rpmhpd->level_count; i++) {
  247. rpmhpd->level[i] = buf[i];
  248. /*
  249. * The AUX data may be zero padded. These 0 valued entries at
  250. * the end of the map must be ignored.
  251. */
  252. if (i > 0 && rpmhpd->level[i] == 0) {
  253. rpmhpd->level_count = i;
  254. break;
  255. }
  256. pr_debug("%s: ARC hlvl=%2d --> vlvl=%4u\n", rpmhpd->res_name, i,
  257. rpmhpd->level[i]);
  258. }
  259. return 0;
  260. }
  261. static int rpmhpd_probe(struct platform_device *pdev)
  262. {
  263. int i, ret;
  264. size_t num_pds;
  265. struct device *dev = &pdev->dev;
  266. struct genpd_onecell_data *data;
  267. struct rpmhpd **rpmhpds;
  268. const struct rpmhpd_desc *desc;
  269. desc = of_device_get_match_data(dev);
  270. if (!desc)
  271. return -EINVAL;
  272. rpmhpds = desc->rpmhpds;
  273. num_pds = desc->num_pds;
  274. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  275. if (!data)
  276. return -ENOMEM;
  277. data->domains = devm_kcalloc(dev, num_pds, sizeof(*data->domains),
  278. GFP_KERNEL);
  279. if (!data->domains)
  280. return -ENOMEM;
  281. data->num_domains = num_pds;
  282. for (i = 0; i < num_pds; i++) {
  283. if (!rpmhpds[i]) {
  284. dev_warn(dev, "rpmhpds[%d] is empty\n", i);
  285. continue;
  286. }
  287. rpmhpds[i]->dev = dev;
  288. rpmhpds[i]->addr = cmd_db_read_addr(rpmhpds[i]->res_name);
  289. if (!rpmhpds[i]->addr) {
  290. dev_err(dev, "Could not find RPMh address for resource %s\n",
  291. rpmhpds[i]->res_name);
  292. return -ENODEV;
  293. }
  294. ret = cmd_db_read_slave_id(rpmhpds[i]->res_name);
  295. if (ret != CMD_DB_HW_ARC) {
  296. dev_err(dev, "RPMh slave ID mismatch\n");
  297. return -EINVAL;
  298. }
  299. ret = rpmhpd_update_level_mapping(rpmhpds[i]);
  300. if (ret)
  301. return ret;
  302. rpmhpds[i]->pd.power_off = rpmhpd_power_off;
  303. rpmhpds[i]->pd.power_on = rpmhpd_power_on;
  304. rpmhpds[i]->pd.set_performance_state = rpmhpd_set_performance_state;
  305. rpmhpds[i]->pd.opp_to_performance_state = rpmhpd_get_performance_state;
  306. pm_genpd_init(&rpmhpds[i]->pd, NULL, true);
  307. data->domains[i] = &rpmhpds[i]->pd;
  308. }
  309. /* Add subdomains */
  310. for (i = 0; i < num_pds; i++) {
  311. if (!rpmhpds[i])
  312. continue;
  313. if (rpmhpds[i]->parent)
  314. pm_genpd_add_subdomain(rpmhpds[i]->parent,
  315. &rpmhpds[i]->pd);
  316. }
  317. return of_genpd_add_provider_onecell(pdev->dev.of_node, data);
  318. }
  319. static struct platform_driver rpmhpd_driver = {
  320. .driver = {
  321. .name = "qcom-rpmhpd",
  322. .of_match_table = rpmhpd_match_table,
  323. .suppress_bind_attrs = true,
  324. },
  325. .probe = rpmhpd_probe,
  326. };
  327. static int __init rpmhpd_init(void)
  328. {
  329. return platform_driver_register(&rpmhpd_driver);
  330. }
  331. core_initcall(rpmhpd_init);