scmi-cpufreq.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * System Control and Power Interface (SCMI) based CPUFreq Interface driver
  4. *
  5. * Copyright (C) 2018-2021 ARM Ltd.
  6. * Sudeep Holla <sudeep.holla@arm.com>
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/clk-provider.h>
  10. #include <linux/cpu.h>
  11. #include <linux/cpufreq.h>
  12. #include <linux/cpumask.h>
  13. #include <linux/energy_model.h>
  14. #include <linux/export.h>
  15. #include <linux/module.h>
  16. #include <linux/pm_opp.h>
  17. #include <linux/slab.h>
  18. #include <linux/scmi_protocol.h>
  19. #include <linux/types.h>
  20. #include <linux/units.h>
  21. struct scmi_data {
  22. int domain_id;
  23. int nr_opp;
  24. struct device *cpu_dev;
  25. cpumask_var_t opp_shared_cpus;
  26. };
  27. static struct scmi_protocol_handle *ph;
  28. static const struct scmi_perf_proto_ops *perf_ops;
  29. static struct cpufreq_driver scmi_cpufreq_driver;
  30. static unsigned int scmi_cpufreq_get_rate(unsigned int cpu)
  31. {
  32. struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
  33. struct scmi_data *priv = policy->driver_data;
  34. unsigned long rate;
  35. int ret;
  36. ret = perf_ops->freq_get(ph, priv->domain_id, &rate, false);
  37. if (ret)
  38. return 0;
  39. return rate / 1000;
  40. }
  41. /*
  42. * perf_ops->freq_set is not a synchronous, the actual OPP change will
  43. * happen asynchronously and can get notified if the events are
  44. * subscribed for by the SCMI firmware
  45. */
  46. static int
  47. scmi_cpufreq_set_target(struct cpufreq_policy *policy, unsigned int index)
  48. {
  49. struct scmi_data *priv = policy->driver_data;
  50. u64 freq = policy->freq_table[index].frequency;
  51. return perf_ops->freq_set(ph, priv->domain_id, freq * 1000, false);
  52. }
  53. static unsigned int scmi_cpufreq_fast_switch(struct cpufreq_policy *policy,
  54. unsigned int target_freq)
  55. {
  56. struct scmi_data *priv = policy->driver_data;
  57. unsigned long freq = target_freq;
  58. if (!perf_ops->freq_set(ph, priv->domain_id, freq * 1000, true))
  59. return target_freq;
  60. return 0;
  61. }
  62. static int scmi_cpu_domain_id(struct device *cpu_dev)
  63. {
  64. struct device_node *np = cpu_dev->of_node;
  65. struct of_phandle_args domain_id;
  66. int index;
  67. if (of_parse_phandle_with_args(np, "clocks", "#clock-cells", 0,
  68. &domain_id)) {
  69. /* Find the corresponding index for power-domain "perf". */
  70. index = of_property_match_string(np, "power-domain-names",
  71. "perf");
  72. if (index < 0)
  73. return -EINVAL;
  74. if (of_parse_phandle_with_args(np, "power-domains",
  75. "#power-domain-cells", index,
  76. &domain_id))
  77. return -EINVAL;
  78. }
  79. return domain_id.args[0];
  80. }
  81. static int
  82. scmi_get_sharing_cpus(struct device *cpu_dev, int domain,
  83. struct cpumask *cpumask)
  84. {
  85. int cpu, tdomain;
  86. struct device *tcpu_dev;
  87. for_each_possible_cpu(cpu) {
  88. if (cpu == cpu_dev->id)
  89. continue;
  90. tcpu_dev = get_cpu_device(cpu);
  91. if (!tcpu_dev)
  92. continue;
  93. tdomain = scmi_cpu_domain_id(tcpu_dev);
  94. if (tdomain == domain)
  95. cpumask_set_cpu(cpu, cpumask);
  96. }
  97. return 0;
  98. }
  99. static int __maybe_unused
  100. scmi_get_cpu_power(struct device *cpu_dev, unsigned long *power,
  101. unsigned long *KHz)
  102. {
  103. enum scmi_power_scale power_scale = perf_ops->power_scale_get(ph);
  104. unsigned long Hz;
  105. int ret, domain;
  106. domain = scmi_cpu_domain_id(cpu_dev);
  107. if (domain < 0)
  108. return domain;
  109. /* Get the power cost of the performance domain. */
  110. Hz = *KHz * 1000;
  111. ret = perf_ops->est_power_get(ph, domain, &Hz, power);
  112. if (ret)
  113. return ret;
  114. /* Convert the power to uW if it is mW (ignore bogoW) */
  115. if (power_scale == SCMI_POWER_MILLIWATTS)
  116. *power *= MICROWATT_PER_MILLIWATT;
  117. /* The EM framework specifies the frequency in KHz. */
  118. *KHz = Hz / 1000;
  119. return 0;
  120. }
  121. static int
  122. scmi_get_rate_limit(u32 domain, bool has_fast_switch)
  123. {
  124. int ret, rate_limit;
  125. if (has_fast_switch) {
  126. /*
  127. * Fast channels are used whenever available,
  128. * so use their rate_limit value if populated.
  129. */
  130. ret = perf_ops->fast_switch_rate_limit(ph, domain,
  131. &rate_limit);
  132. if (!ret && rate_limit)
  133. return rate_limit;
  134. }
  135. ret = perf_ops->rate_limit_get(ph, domain, &rate_limit);
  136. if (ret)
  137. return 0;
  138. return rate_limit;
  139. }
  140. static struct freq_attr *scmi_cpufreq_hw_attr[] = {
  141. &cpufreq_freq_attr_scaling_available_freqs,
  142. NULL,
  143. NULL,
  144. };
  145. static int scmi_cpufreq_init(struct cpufreq_policy *policy)
  146. {
  147. int ret, nr_opp, domain;
  148. unsigned int latency;
  149. struct device *cpu_dev;
  150. struct scmi_data *priv;
  151. struct cpufreq_frequency_table *freq_table;
  152. cpu_dev = get_cpu_device(policy->cpu);
  153. if (!cpu_dev) {
  154. pr_err("failed to get cpu%d device\n", policy->cpu);
  155. return -ENODEV;
  156. }
  157. domain = scmi_cpu_domain_id(cpu_dev);
  158. if (domain < 0)
  159. return domain;
  160. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  161. if (!priv)
  162. return -ENOMEM;
  163. if (!zalloc_cpumask_var(&priv->opp_shared_cpus, GFP_KERNEL)) {
  164. ret = -ENOMEM;
  165. goto out_free_priv;
  166. }
  167. /* Obtain CPUs that share SCMI performance controls */
  168. ret = scmi_get_sharing_cpus(cpu_dev, domain, policy->cpus);
  169. if (ret) {
  170. dev_warn(cpu_dev, "failed to get sharing cpumask\n");
  171. goto out_free_cpumask;
  172. }
  173. /*
  174. * Obtain CPUs that share performance levels.
  175. * The OPP 'sharing cpus' info may come from DT through an empty opp
  176. * table and opp-shared.
  177. */
  178. ret = dev_pm_opp_of_get_sharing_cpus(cpu_dev, priv->opp_shared_cpus);
  179. if (ret || cpumask_empty(priv->opp_shared_cpus)) {
  180. /*
  181. * Either opp-table is not set or no opp-shared was found.
  182. * Use the CPU mask from SCMI to designate CPUs sharing an OPP
  183. * table.
  184. */
  185. cpumask_copy(priv->opp_shared_cpus, policy->cpus);
  186. }
  187. /*
  188. * A previous CPU may have marked OPPs as shared for a few CPUs, based on
  189. * what OPP core provided. If the current CPU is part of those few, then
  190. * there is no need to add OPPs again.
  191. */
  192. nr_opp = dev_pm_opp_get_opp_count(cpu_dev);
  193. if (nr_opp <= 0) {
  194. ret = perf_ops->device_opps_add(ph, cpu_dev, domain);
  195. if (ret) {
  196. dev_warn(cpu_dev, "failed to add opps to the device\n");
  197. goto out_free_cpumask;
  198. }
  199. nr_opp = dev_pm_opp_get_opp_count(cpu_dev);
  200. if (nr_opp <= 0) {
  201. dev_err(cpu_dev, "%s: No OPPs for this device: %d\n",
  202. __func__, nr_opp);
  203. ret = -ENODEV;
  204. goto out_free_opp;
  205. }
  206. ret = dev_pm_opp_set_sharing_cpus(cpu_dev, priv->opp_shared_cpus);
  207. if (ret) {
  208. dev_err(cpu_dev, "%s: failed to mark OPPs as shared: %d\n",
  209. __func__, ret);
  210. goto out_free_opp;
  211. }
  212. priv->nr_opp = nr_opp;
  213. }
  214. ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table);
  215. if (ret) {
  216. dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
  217. goto out_free_opp;
  218. }
  219. priv->cpu_dev = cpu_dev;
  220. priv->domain_id = domain;
  221. policy->driver_data = priv;
  222. policy->freq_table = freq_table;
  223. /* SCMI allows DVFS request for any domain from any CPU */
  224. policy->dvfs_possible_from_any_cpu = true;
  225. latency = perf_ops->transition_latency_get(ph, domain);
  226. if (!latency)
  227. latency = CPUFREQ_ETERNAL;
  228. policy->cpuinfo.transition_latency = latency;
  229. policy->fast_switch_possible =
  230. perf_ops->fast_switch_possible(ph, domain);
  231. policy->transition_delay_us =
  232. scmi_get_rate_limit(domain, policy->fast_switch_possible);
  233. if (policy_has_boost_freq(policy)) {
  234. ret = cpufreq_enable_boost_support();
  235. if (ret) {
  236. dev_warn(cpu_dev, "failed to enable boost: %d\n", ret);
  237. goto out_free_table;
  238. } else {
  239. scmi_cpufreq_hw_attr[1] = &cpufreq_freq_attr_scaling_boost_freqs;
  240. scmi_cpufreq_driver.boost_enabled = true;
  241. }
  242. }
  243. return 0;
  244. out_free_table:
  245. dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
  246. out_free_opp:
  247. dev_pm_opp_remove_all_dynamic(cpu_dev);
  248. out_free_cpumask:
  249. free_cpumask_var(priv->opp_shared_cpus);
  250. out_free_priv:
  251. kfree(priv);
  252. return ret;
  253. }
  254. static void scmi_cpufreq_exit(struct cpufreq_policy *policy)
  255. {
  256. struct scmi_data *priv = policy->driver_data;
  257. dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &policy->freq_table);
  258. dev_pm_opp_remove_all_dynamic(priv->cpu_dev);
  259. free_cpumask_var(priv->opp_shared_cpus);
  260. kfree(priv);
  261. }
  262. static void scmi_cpufreq_register_em(struct cpufreq_policy *policy)
  263. {
  264. struct em_data_callback em_cb = EM_DATA_CB(scmi_get_cpu_power);
  265. enum scmi_power_scale power_scale = perf_ops->power_scale_get(ph);
  266. struct scmi_data *priv = policy->driver_data;
  267. bool em_power_scale = false;
  268. /*
  269. * This callback will be called for each policy, but we don't need to
  270. * register with EM every time. Despite not being part of the same
  271. * policy, some CPUs may still share their perf-domains, and a CPU from
  272. * another policy may already have registered with EM on behalf of CPUs
  273. * of this policy.
  274. */
  275. if (!priv->nr_opp)
  276. return;
  277. if (power_scale == SCMI_POWER_MILLIWATTS
  278. || power_scale == SCMI_POWER_MICROWATTS)
  279. em_power_scale = true;
  280. em_dev_register_perf_domain(get_cpu_device(policy->cpu), priv->nr_opp,
  281. &em_cb, priv->opp_shared_cpus,
  282. em_power_scale);
  283. }
  284. static struct cpufreq_driver scmi_cpufreq_driver = {
  285. .name = "scmi",
  286. .flags = CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
  287. CPUFREQ_NEED_INITIAL_FREQ_CHECK |
  288. CPUFREQ_IS_COOLING_DEV,
  289. .verify = cpufreq_generic_frequency_table_verify,
  290. .attr = scmi_cpufreq_hw_attr,
  291. .target_index = scmi_cpufreq_set_target,
  292. .fast_switch = scmi_cpufreq_fast_switch,
  293. .get = scmi_cpufreq_get_rate,
  294. .init = scmi_cpufreq_init,
  295. .exit = scmi_cpufreq_exit,
  296. .register_em = scmi_cpufreq_register_em,
  297. };
  298. static int scmi_cpufreq_probe(struct scmi_device *sdev)
  299. {
  300. int ret;
  301. struct device *dev = &sdev->dev;
  302. const struct scmi_handle *handle;
  303. handle = sdev->handle;
  304. if (!handle)
  305. return -ENODEV;
  306. perf_ops = handle->devm_protocol_get(sdev, SCMI_PROTOCOL_PERF, &ph);
  307. if (IS_ERR(perf_ops))
  308. return PTR_ERR(perf_ops);
  309. #ifdef CONFIG_COMMON_CLK
  310. /* dummy clock provider as needed by OPP if clocks property is used */
  311. if (of_property_present(dev->of_node, "#clock-cells")) {
  312. ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get, NULL);
  313. if (ret)
  314. return dev_err_probe(dev, ret, "%s: registering clock provider failed\n", __func__);
  315. }
  316. #endif
  317. ret = cpufreq_register_driver(&scmi_cpufreq_driver);
  318. if (ret) {
  319. dev_err(dev, "%s: registering cpufreq failed, err: %d\n",
  320. __func__, ret);
  321. }
  322. return ret;
  323. }
  324. static void scmi_cpufreq_remove(struct scmi_device *sdev)
  325. {
  326. cpufreq_unregister_driver(&scmi_cpufreq_driver);
  327. }
  328. static const struct scmi_device_id scmi_id_table[] = {
  329. { SCMI_PROTOCOL_PERF, "cpufreq" },
  330. { },
  331. };
  332. MODULE_DEVICE_TABLE(scmi, scmi_id_table);
  333. static struct scmi_driver scmi_cpufreq_drv = {
  334. .name = "scmi-cpufreq",
  335. .probe = scmi_cpufreq_probe,
  336. .remove = scmi_cpufreq_remove,
  337. .id_table = scmi_id_table,
  338. };
  339. module_scmi_driver(scmi_cpufreq_drv);
  340. MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
  341. MODULE_DESCRIPTION("ARM SCMI CPUFreq interface driver");
  342. MODULE_LICENSE("GPL v2");