gh_rm_booster.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/kernel.h>
  7. #include <linux/notifier.h>
  8. #include <linux/cpufreq.h>
  9. #include <asm/gunyah/hcall.h>
  10. #include "gh_rm_drv_private.h"
  11. #include "gh_rm_booster.h"
  12. #define BOOSTER_TIMEOUT_MSEC 5000
  13. struct gh_rm_booster_dev {
  14. u32 boost_cnt;
  15. int vmid;
  16. int orig_cpu;
  17. struct device *dev;
  18. struct freq_qos_request gh_rm_boost_req;
  19. struct notifier_block gh_rm_boost_nb;
  20. struct delayed_work booster_release_work;
  21. gh_capid_t vcpu_cap_id;
  22. };
  23. static struct gh_rm_booster_dev *rm_status;
  24. static DEFINE_MUTEX(rm_booster_lock);
  25. static int rm_boost_target_cpu_set(const char *val, const struct kernel_param *kp)
  26. {
  27. unsigned int cpu;
  28. int ret;
  29. ret = kstrtou32(val, 10, &cpu);
  30. if (ret)
  31. return ret;
  32. if (cpu < num_possible_cpus())
  33. return param_set_int(val, kp);
  34. else
  35. return -EINVAL;
  36. }
  37. static const struct kernel_param_ops target_cpu_ops = {
  38. .set = rm_boost_target_cpu_set,
  39. .get = param_get_int,
  40. };
  41. static unsigned int target_cpu = UINT_MAX;
  42. module_param_cb(target_cpu, &target_cpu_ops, &target_cpu, 0644);
  43. MODULE_PARM_DESC(target_cpu, "Choose the target core for Resource Manager");
  44. static int gh_set_rm_affinity(int cpu)
  45. {
  46. int ret = -EINVAL;
  47. ret = gh_hcall_change_rm_affinity(rm_status->vcpu_cap_id, cpu);
  48. if (ret == GH_ERROR_OK)
  49. return 0;
  50. dev_err(rm_status->dev, "gh set RM affinity fail\n");
  51. return ret;
  52. }
  53. static void gh_boost_rmfreq(int cpu)
  54. {
  55. struct cpufreq_policy *policy;
  56. int ret;
  57. policy = cpufreq_cpu_get(cpu);
  58. if (!policy) {
  59. dev_err(rm_status->dev, "Failed to get RM cpufreq policy\n");
  60. return;
  61. }
  62. /* Always have target cpu's max freq as boosted freq. */
  63. ret = freq_qos_add_request(&policy->constraints,
  64. &rm_status->gh_rm_boost_req,
  65. FREQ_QOS_MIN, policy->max);
  66. if (ret < 0)
  67. dev_err(rm_status->dev, "Failed to boost RM freq\n");
  68. }
  69. static void gh_resume_rm_status(void)
  70. {
  71. int ret;
  72. ret = freq_qos_remove_request(&rm_status->gh_rm_boost_req);
  73. if (ret < 0)
  74. dev_err(rm_status->dev, "Failed to resume RM freq\n");
  75. if (rm_status->vcpu_cap_id == GH_CAPID_INVAL)
  76. return;
  77. ret = gh_set_rm_affinity(rm_status->orig_cpu);
  78. if (ret)
  79. dev_err(rm_status->dev, "Failed to resume RM affinity\n");
  80. }
  81. static void gh_configure_rm(struct gh_rm_notif_vm_status_payload *status)
  82. {
  83. int dest_cpu;
  84. switch (status->vm_status) {
  85. case GH_RM_VM_STATUS_AUTH:
  86. cancel_delayed_work_sync(&rm_status->booster_release_work);
  87. mutex_lock(&rm_booster_lock);
  88. rm_status->boost_cnt++;
  89. schedule_delayed_work(&rm_status->booster_release_work,
  90. msecs_to_jiffies(BOOSTER_TIMEOUT_MSEC));
  91. if (rm_status->boost_cnt > 1) {
  92. mutex_unlock(&rm_booster_lock);
  93. dev_dbg(rm_status->dev,
  94. "VM%d found Gunyah RM booster already activated\n",
  95. status->vmid);
  96. break;
  97. }
  98. dest_cpu = target_cpu;
  99. if (dest_cpu != rm_status->orig_cpu) {
  100. if (rm_status->vcpu_cap_id == GH_CAPID_INVAL ||
  101. gh_set_rm_affinity(dest_cpu)) {
  102. dest_cpu = rm_status->orig_cpu;
  103. dev_info(rm_status->dev,
  104. "Fallback to boost the frequency of RM current cpu - CPU%d\n",
  105. dest_cpu);
  106. }
  107. }
  108. gh_boost_rmfreq(dest_cpu);
  109. mutex_unlock(&rm_booster_lock);
  110. dev_dbg(rm_status->dev,
  111. "Gunyah RM booster activated by VM%d\n", status->vmid);
  112. break;
  113. case GH_RM_VM_STATUS_INIT_FAILED:
  114. fallthrough;
  115. case GH_RM_VM_STATUS_RUNNING:
  116. mutex_lock(&rm_booster_lock);
  117. if (!rm_status->boost_cnt) {
  118. mutex_unlock(&rm_booster_lock);
  119. break;
  120. }
  121. rm_status->boost_cnt--;
  122. if (rm_status->boost_cnt) {
  123. mutex_unlock(&rm_booster_lock);
  124. dev_dbg(rm_status->dev,
  125. "VM%d will not deactivate Gunyah RM booster\n",
  126. status->vmid);
  127. break;
  128. }
  129. gh_resume_rm_status();
  130. mutex_unlock(&rm_booster_lock);
  131. cancel_delayed_work_sync(&rm_status->booster_release_work);
  132. dev_dbg(rm_status->dev,
  133. "Gunyah RM booster deactivated by VM%d\n",
  134. status->vmid);
  135. break;
  136. default:
  137. break;
  138. }
  139. }
  140. static int gh_rm_boost_nb_handler(struct notifier_block *this,
  141. unsigned long cmd, void *data)
  142. {
  143. switch (cmd) {
  144. case GH_RM_NOTIF_VM_STATUS:
  145. gh_configure_rm(data);
  146. break;
  147. default:
  148. break;
  149. }
  150. return NOTIFY_DONE;
  151. }
  152. static void gh_rm_booster_release_func(struct work_struct *work)
  153. {
  154. mutex_lock(&rm_booster_lock);
  155. if (!rm_status || rm_status->boost_cnt == 0) {
  156. mutex_unlock(&rm_booster_lock);
  157. return;
  158. }
  159. dev_dbg(rm_status->dev, "Gunyah RM booster released\n");
  160. gh_resume_rm_status();
  161. rm_status->boost_cnt = 0;
  162. mutex_unlock(&rm_booster_lock);
  163. }
  164. static void gh_rm_booster_populate_res(void)
  165. {
  166. struct gh_vm_get_hyp_res_resp_entry *res_entries = NULL;
  167. u32 n_res, i;
  168. res_entries = gh_rm_vm_get_hyp_res(rm_status->vmid, &n_res);
  169. if (IS_ERR_OR_NULL(res_entries)) {
  170. dev_err(rm_status->dev, "Get hyp resources failed.\n");
  171. return;
  172. }
  173. for (i = 0; i < n_res; i++) {
  174. if (res_entries[i].res_type == GH_RM_RES_TYPE_VCPU) {
  175. rm_status->vcpu_cap_id = (u64) res_entries[i].cap_id_high << 32 |
  176. res_entries[i].cap_id_low;
  177. goto out;
  178. }
  179. }
  180. dev_err(rm_status->dev, "No vCPU resource type found.\n");
  181. out:
  182. kfree(res_entries);
  183. }
  184. static int gh_rm_booster_probe(struct platform_device *pdev)
  185. {
  186. struct device_node *np = pdev->dev.of_node;
  187. int ret = -ENODEV;
  188. rm_status = kzalloc(sizeof(*rm_status), GFP_KERNEL);
  189. if (!rm_status)
  190. return -ENOMEM;
  191. ret = of_property_read_u32(np, "qcom,rm-vmid", &rm_status->vmid);
  192. if (ret) {
  193. dev_err(&pdev->dev, "Failed to get RM vmid\n");
  194. goto out_free;
  195. }
  196. ret = of_property_read_u32(np, "qcom,rm-affinity-default",
  197. &rm_status->orig_cpu);
  198. if (ret) {
  199. dev_err(&pdev->dev, "Failed to get RM affinity\n");
  200. goto out_free;
  201. }
  202. rm_status->dev = &pdev->dev;
  203. platform_set_drvdata(pdev, rm_status);
  204. INIT_DELAYED_WORK(&rm_status->booster_release_work,
  205. gh_rm_booster_release_func);
  206. /*
  207. * By default target cpu will be the bigger number cpu,
  208. * and usually the most powerful cpu here.
  209. */
  210. if (target_cpu >= num_possible_cpus())
  211. target_cpu = num_possible_cpus() - 1;
  212. rm_status->boost_cnt = 0;
  213. rm_status->vcpu_cap_id = GH_CAPID_INVAL;
  214. gh_rm_booster_populate_res();
  215. rm_status->gh_rm_boost_nb.notifier_call = gh_rm_boost_nb_handler;
  216. rm_status->gh_rm_boost_nb.priority = INT_MAX;
  217. ret = gh_rm_register_notifier(&rm_status->gh_rm_boost_nb);
  218. if (!ret)
  219. return 0;
  220. dev_err(&pdev->dev, "Failed to register RM notifier\n");
  221. out_free:
  222. kfree(rm_status);
  223. return ret;
  224. }
  225. static int gh_rm_booster_remove(struct platform_device *pdev)
  226. {
  227. gh_rm_unregister_notifier(&rm_status->gh_rm_boost_nb);
  228. cancel_delayed_work_sync(&rm_status->booster_release_work);
  229. mutex_lock(&rm_booster_lock);
  230. if (rm_status->boost_cnt)
  231. gh_resume_rm_status();
  232. kfree(rm_status);
  233. rm_status = NULL;
  234. mutex_unlock(&rm_booster_lock);
  235. return 0;
  236. }
  237. static const struct of_device_id gh_rm_booster_match_table[] = {
  238. { .compatible = "qcom,gh-rm-booster" },
  239. {}
  240. };
  241. static struct platform_driver gh_rm_booster_driver = {
  242. .driver = {
  243. .name = "gh_rm_booster",
  244. .of_match_table = gh_rm_booster_match_table,
  245. },
  246. .probe = gh_rm_booster_probe,
  247. .remove = gh_rm_booster_remove,
  248. };
  249. module_platform_driver(gh_rm_booster_driver);
  250. MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Gunyah RM booster Driver");
  251. MODULE_LICENSE("GPL");