bcm-voter.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include <asm/div64.h>
  6. #include <linux/interconnect-provider.h>
  7. #include <linux/list_sort.h>
  8. #include <linux/module.h>
  9. #include <linux/of.h>
  10. #include <linux/platform_device.h>
  11. #include <soc/qcom/rpmh.h>
  12. #include <soc/qcom/tcs.h>
  13. #include "bcm-voter.h"
  14. #include "icc-rpmh.h"
  15. static LIST_HEAD(bcm_voters);
  16. static DEFINE_MUTEX(bcm_voter_lock);
  17. /**
  18. * struct bcm_voter - Bus Clock Manager voter
  19. * @dev: reference to the device that communicates with the BCM
  20. * @np: reference to the device node to match bcm voters
  21. * @lock: mutex to protect commit and wake/sleep lists in the voter
  22. * @commit_list: list containing bcms to be committed to hardware
  23. * @ws_list: list containing bcms that have different wake/sleep votes
  24. * @voter_node: list of bcm voters
  25. * @tcs_wait: mask for which buckets require TCS completion
  26. */
  27. struct bcm_voter {
  28. struct device *dev;
  29. struct device_node *np;
  30. struct mutex lock;
  31. struct list_head commit_list;
  32. struct list_head ws_list;
  33. struct list_head voter_node;
  34. u32 tcs_wait;
  35. };
  36. static int cmp_vcd(void *priv, struct list_head *a, struct list_head *b)
  37. {
  38. const struct qcom_icc_bcm *bcm_a =
  39. list_entry(a, struct qcom_icc_bcm, list);
  40. const struct qcom_icc_bcm *bcm_b =
  41. list_entry(b, struct qcom_icc_bcm, list);
  42. if (bcm_a->aux_data.vcd < bcm_b->aux_data.vcd)
  43. return -1;
  44. else if (bcm_a->aux_data.vcd == bcm_b->aux_data.vcd)
  45. return 0;
  46. else
  47. return 1;
  48. }
  49. static u64 bcm_div(u64 num, u32 base)
  50. {
  51. /* Ensure that small votes aren't lost. */
  52. if (num && num < base)
  53. return 1;
  54. do_div(num, base);
  55. return num;
  56. }
  57. static void bcm_aggregate(struct qcom_icc_bcm *bcm)
  58. {
  59. struct qcom_icc_node *node;
  60. size_t i, bucket;
  61. u64 agg_avg[QCOM_ICC_NUM_BUCKETS] = {0};
  62. u64 agg_peak[QCOM_ICC_NUM_BUCKETS] = {0};
  63. u64 temp;
  64. for (bucket = 0; bucket < QCOM_ICC_NUM_BUCKETS; bucket++) {
  65. for (i = 0; i < bcm->num_nodes; i++) {
  66. node = bcm->nodes[i];
  67. temp = bcm_div(node->sum_avg[bucket] * bcm->aux_data.width,
  68. node->buswidth * node->channels);
  69. agg_avg[bucket] = max(agg_avg[bucket], temp);
  70. temp = bcm_div(node->max_peak[bucket] * bcm->aux_data.width,
  71. node->buswidth);
  72. agg_peak[bucket] = max(agg_peak[bucket], temp);
  73. }
  74. temp = agg_avg[bucket] * bcm->vote_scale;
  75. bcm->vote_x[bucket] = bcm_div(temp, bcm->aux_data.unit);
  76. temp = agg_peak[bucket] * bcm->vote_scale;
  77. bcm->vote_y[bucket] = bcm_div(temp, bcm->aux_data.unit);
  78. }
  79. if (bcm->keepalive && bcm->vote_x[QCOM_ICC_BUCKET_AMC] == 0 &&
  80. bcm->vote_y[QCOM_ICC_BUCKET_AMC] == 0) {
  81. bcm->vote_x[QCOM_ICC_BUCKET_AMC] = 1;
  82. bcm->vote_x[QCOM_ICC_BUCKET_WAKE] = 1;
  83. bcm->vote_y[QCOM_ICC_BUCKET_AMC] = 1;
  84. bcm->vote_y[QCOM_ICC_BUCKET_WAKE] = 1;
  85. }
  86. }
  87. static inline void tcs_cmd_gen(struct tcs_cmd *cmd, u64 vote_x, u64 vote_y,
  88. u32 addr, bool commit, bool wait)
  89. {
  90. bool valid = true;
  91. if (!cmd)
  92. return;
  93. memset(cmd, 0, sizeof(*cmd));
  94. if (vote_x == 0 && vote_y == 0)
  95. valid = false;
  96. if (vote_x > BCM_TCS_CMD_VOTE_MASK)
  97. vote_x = BCM_TCS_CMD_VOTE_MASK;
  98. if (vote_y > BCM_TCS_CMD_VOTE_MASK)
  99. vote_y = BCM_TCS_CMD_VOTE_MASK;
  100. cmd->addr = addr;
  101. cmd->data = BCM_TCS_CMD(commit, valid, vote_x, vote_y);
  102. /*
  103. * Set the wait for completion flag on command that need to be completed
  104. * before the next command.
  105. */
  106. cmd->wait = wait;
  107. }
  108. static void tcs_list_gen(struct bcm_voter *voter, int bucket,
  109. struct tcs_cmd tcs_list[MAX_VCD],
  110. int n[MAX_VCD + 1])
  111. {
  112. struct list_head *bcm_list = &voter->commit_list;
  113. struct qcom_icc_bcm *bcm;
  114. bool commit, wait;
  115. size_t idx = 0, batch = 0, cur_vcd_size = 0;
  116. memset(n, 0, sizeof(int) * (MAX_VCD + 1));
  117. list_for_each_entry(bcm, bcm_list, list) {
  118. commit = false;
  119. cur_vcd_size++;
  120. if ((list_is_last(&bcm->list, bcm_list)) ||
  121. bcm->aux_data.vcd != list_next_entry(bcm, list)->aux_data.vcd) {
  122. commit = true;
  123. cur_vcd_size = 0;
  124. }
  125. wait = commit && (voter->tcs_wait & BIT(bucket));
  126. tcs_cmd_gen(&tcs_list[idx], bcm->vote_x[bucket],
  127. bcm->vote_y[bucket], bcm->addr, commit, wait);
  128. idx++;
  129. n[batch]++;
  130. /*
  131. * Batch the BCMs in such a way that we do not split them in
  132. * multiple payloads when they are under the same VCD. This is
  133. * to ensure that every BCM is committed since we only set the
  134. * commit bit on the last BCM request of every VCD.
  135. */
  136. if (n[batch] >= MAX_RPMH_PAYLOAD) {
  137. if (!commit) {
  138. n[batch] -= cur_vcd_size;
  139. n[batch + 1] = cur_vcd_size;
  140. }
  141. batch++;
  142. }
  143. }
  144. }
  145. /**
  146. * of_bcm_voter_get - gets a bcm voter handle from DT node
  147. * @dev: device pointer for the consumer device
  148. * @name: name for the bcm voter device
  149. *
  150. * This function will match a device_node pointer for the phandle
  151. * specified in the device DT and return a bcm_voter handle on success.
  152. *
  153. * Returns bcm_voter pointer or ERR_PTR() on error. EPROBE_DEFER is returned
  154. * when matching bcm voter is yet to be found.
  155. */
  156. struct bcm_voter *of_bcm_voter_get(struct device *dev, const char *name)
  157. {
  158. struct bcm_voter *voter = ERR_PTR(-EPROBE_DEFER);
  159. struct bcm_voter *temp;
  160. struct device_node *np, *node;
  161. int idx = 0;
  162. if (!dev || !dev->of_node)
  163. return ERR_PTR(-ENODEV);
  164. np = dev->of_node;
  165. if (name) {
  166. idx = of_property_match_string(np, "qcom,bcm-voter-names", name);
  167. if (idx < 0)
  168. return ERR_PTR(idx);
  169. }
  170. node = of_parse_phandle(np, "qcom,bcm-voters", idx);
  171. mutex_lock(&bcm_voter_lock);
  172. list_for_each_entry(temp, &bcm_voters, voter_node) {
  173. if (temp->np == node) {
  174. voter = temp;
  175. break;
  176. }
  177. }
  178. mutex_unlock(&bcm_voter_lock);
  179. of_node_put(node);
  180. return voter;
  181. }
  182. EXPORT_SYMBOL_GPL(of_bcm_voter_get);
  183. /**
  184. * qcom_icc_bcm_voter_add - queues up the bcm nodes that require updates
  185. * @voter: voter that the bcms are being added to
  186. * @bcm: bcm to add to the commit and wake sleep list
  187. */
  188. void qcom_icc_bcm_voter_add(struct bcm_voter *voter, struct qcom_icc_bcm *bcm)
  189. {
  190. if (!voter)
  191. return;
  192. mutex_lock(&voter->lock);
  193. if (list_empty(&bcm->list))
  194. list_add_tail(&bcm->list, &voter->commit_list);
  195. if (list_empty(&bcm->ws_list))
  196. list_add_tail(&bcm->ws_list, &voter->ws_list);
  197. mutex_unlock(&voter->lock);
  198. }
  199. EXPORT_SYMBOL_GPL(qcom_icc_bcm_voter_add);
  200. /**
  201. * qcom_icc_bcm_voter_commit - generates and commits tcs cmds based on bcms
  202. * @voter: voter that needs flushing
  203. *
  204. * This function generates a set of AMC commands and flushes to the BCM device
  205. * associated with the voter. It conditionally generate WAKE and SLEEP commands
  206. * based on deltas between WAKE/SLEEP requirements. The ws_list persists
  207. * through multiple commit requests and bcm nodes are removed only when the
  208. * requirements for WAKE matches SLEEP.
  209. *
  210. * Returns 0 on success, or an appropriate error code otherwise.
  211. */
  212. int qcom_icc_bcm_voter_commit(struct bcm_voter *voter)
  213. {
  214. struct qcom_icc_bcm *bcm;
  215. struct qcom_icc_bcm *bcm_tmp;
  216. int commit_idx[MAX_VCD + 1];
  217. struct tcs_cmd cmds[MAX_BCMS];
  218. int ret = 0;
  219. if (!voter)
  220. return 0;
  221. mutex_lock(&voter->lock);
  222. list_for_each_entry(bcm, &voter->commit_list, list)
  223. bcm_aggregate(bcm);
  224. /*
  225. * Pre sort the BCMs based on VCD for ease of generating a command list
  226. * that groups the BCMs with the same VCD together. VCDs are numbered
  227. * with lowest being the most expensive time wise, ensuring that
  228. * those commands are being sent the earliest in the queue. This needs
  229. * to be sorted every commit since we can't guarantee the order in which
  230. * the BCMs are added to the list.
  231. */
  232. list_sort(NULL, &voter->commit_list, cmp_vcd);
  233. /*
  234. * Construct the command list based on a pre ordered list of BCMs
  235. * based on VCD.
  236. */
  237. tcs_list_gen(voter, QCOM_ICC_BUCKET_AMC, cmds, commit_idx);
  238. if (!commit_idx[0])
  239. goto out;
  240. rpmh_invalidate(voter->dev);
  241. ret = rpmh_write_batch(voter->dev, RPMH_ACTIVE_ONLY_STATE,
  242. cmds, commit_idx);
  243. if (ret) {
  244. pr_err("Error sending AMC RPMH requests (%d)\n", ret);
  245. goto out;
  246. }
  247. list_for_each_entry_safe(bcm, bcm_tmp, &voter->commit_list, list)
  248. list_del_init(&bcm->list);
  249. list_for_each_entry_safe(bcm, bcm_tmp, &voter->ws_list, ws_list) {
  250. /*
  251. * Only generate WAKE and SLEEP commands if a resource's
  252. * requirements change as the execution environment transitions
  253. * between different power states.
  254. */
  255. if (bcm->vote_x[QCOM_ICC_BUCKET_WAKE] !=
  256. bcm->vote_x[QCOM_ICC_BUCKET_SLEEP] ||
  257. bcm->vote_y[QCOM_ICC_BUCKET_WAKE] !=
  258. bcm->vote_y[QCOM_ICC_BUCKET_SLEEP])
  259. list_add_tail(&bcm->list, &voter->commit_list);
  260. else
  261. list_del_init(&bcm->ws_list);
  262. }
  263. if (list_empty(&voter->commit_list))
  264. goto out;
  265. list_sort(NULL, &voter->commit_list, cmp_vcd);
  266. tcs_list_gen(voter, QCOM_ICC_BUCKET_WAKE, cmds, commit_idx);
  267. ret = rpmh_write_batch(voter->dev, RPMH_WAKE_ONLY_STATE, cmds, commit_idx);
  268. if (ret) {
  269. pr_err("Error sending WAKE RPMH requests (%d)\n", ret);
  270. goto out;
  271. }
  272. tcs_list_gen(voter, QCOM_ICC_BUCKET_SLEEP, cmds, commit_idx);
  273. ret = rpmh_write_batch(voter->dev, RPMH_SLEEP_STATE, cmds, commit_idx);
  274. if (ret) {
  275. pr_err("Error sending SLEEP RPMH requests (%d)\n", ret);
  276. goto out;
  277. }
  278. out:
  279. list_for_each_entry_safe(bcm, bcm_tmp, &voter->commit_list, list)
  280. list_del_init(&bcm->list);
  281. mutex_unlock(&voter->lock);
  282. return ret;
  283. }
  284. EXPORT_SYMBOL_GPL(qcom_icc_bcm_voter_commit);
  285. static int qcom_icc_bcm_voter_probe(struct platform_device *pdev)
  286. {
  287. struct device_node *np = pdev->dev.of_node;
  288. struct bcm_voter *voter;
  289. voter = devm_kzalloc(&pdev->dev, sizeof(*voter), GFP_KERNEL);
  290. if (!voter)
  291. return -ENOMEM;
  292. voter->dev = &pdev->dev;
  293. voter->np = np;
  294. if (of_property_read_u32(np, "qcom,tcs-wait", &voter->tcs_wait))
  295. voter->tcs_wait = QCOM_ICC_TAG_ACTIVE_ONLY;
  296. mutex_init(&voter->lock);
  297. INIT_LIST_HEAD(&voter->commit_list);
  298. INIT_LIST_HEAD(&voter->ws_list);
  299. mutex_lock(&bcm_voter_lock);
  300. list_add_tail(&voter->voter_node, &bcm_voters);
  301. mutex_unlock(&bcm_voter_lock);
  302. return 0;
  303. }
  304. static const struct of_device_id bcm_voter_of_match[] = {
  305. { .compatible = "qcom,bcm-voter" },
  306. { }
  307. };
  308. MODULE_DEVICE_TABLE(of, bcm_voter_of_match);
  309. static struct platform_driver qcom_icc_bcm_voter_driver = {
  310. .probe = qcom_icc_bcm_voter_probe,
  311. .driver = {
  312. .name = "bcm_voter",
  313. .of_match_table = bcm_voter_of_match,
  314. },
  315. };
  316. module_platform_driver(qcom_icc_bcm_voter_driver);
  317. MODULE_AUTHOR("David Dai <daidavid1@codeaurora.org>");
  318. MODULE_DESCRIPTION("Qualcomm BCM Voter interconnect driver");
  319. MODULE_LICENSE("GPL v2");