scmi-cpufreq.c 13 KB

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