scmi-cpufreq.c 11 KB

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