apple-soc-cpufreq.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Apple SoC CPU cluster performance state driver
  4. *
  5. * Copyright The Asahi Linux Contributors
  6. *
  7. * Based on scpi-cpufreq.c
  8. */
  9. #include <linux/bitfield.h>
  10. #include <linux/bitops.h>
  11. #include <linux/cpu.h>
  12. #include <linux/cpufreq.h>
  13. #include <linux/cpumask.h>
  14. #include <linux/delay.h>
  15. #include <linux/err.h>
  16. #include <linux/io.h>
  17. #include <linux/iopoll.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/of_address.h>
  21. #include <linux/pm_opp.h>
  22. #include <linux/slab.h>
  23. #define APPLE_DVFS_CMD 0x20
  24. #define APPLE_DVFS_CMD_BUSY BIT(31)
  25. #define APPLE_DVFS_CMD_SET BIT(25)
  26. #define APPLE_DVFS_CMD_PS2 GENMASK(16, 12)
  27. #define APPLE_DVFS_CMD_PS1 GENMASK(4, 0)
  28. /* Same timebase as CPU counter (24MHz) */
  29. #define APPLE_DVFS_LAST_CHG_TIME 0x38
  30. /*
  31. * Apple ran out of bits and had to shift this in T8112...
  32. */
  33. #define APPLE_DVFS_STATUS 0x50
  34. #define APPLE_DVFS_STATUS_CUR_PS_T8103 GENMASK(7, 4)
  35. #define APPLE_DVFS_STATUS_CUR_PS_SHIFT_T8103 4
  36. #define APPLE_DVFS_STATUS_TGT_PS_T8103 GENMASK(3, 0)
  37. #define APPLE_DVFS_STATUS_CUR_PS_T8112 GENMASK(9, 5)
  38. #define APPLE_DVFS_STATUS_CUR_PS_SHIFT_T8112 5
  39. #define APPLE_DVFS_STATUS_TGT_PS_T8112 GENMASK(4, 0)
  40. /*
  41. * Div is +1, base clock is 12MHz on existing SoCs.
  42. * For documentation purposes. We use the OPP table to
  43. * get the frequency.
  44. */
  45. #define APPLE_DVFS_PLL_STATUS 0xc0
  46. #define APPLE_DVFS_PLL_FACTOR 0xc8
  47. #define APPLE_DVFS_PLL_FACTOR_MULT GENMASK(31, 16)
  48. #define APPLE_DVFS_PLL_FACTOR_DIV GENMASK(15, 0)
  49. #define APPLE_DVFS_TRANSITION_TIMEOUT 100
  50. struct apple_soc_cpufreq_info {
  51. u64 max_pstate;
  52. u64 cur_pstate_mask;
  53. u64 cur_pstate_shift;
  54. };
  55. struct apple_cpu_priv {
  56. struct device *cpu_dev;
  57. void __iomem *reg_base;
  58. const struct apple_soc_cpufreq_info *info;
  59. };
  60. static struct cpufreq_driver apple_soc_cpufreq_driver;
  61. static const struct apple_soc_cpufreq_info soc_t8103_info = {
  62. .max_pstate = 15,
  63. .cur_pstate_mask = APPLE_DVFS_STATUS_CUR_PS_T8103,
  64. .cur_pstate_shift = APPLE_DVFS_STATUS_CUR_PS_SHIFT_T8103,
  65. };
  66. static const struct apple_soc_cpufreq_info soc_t8112_info = {
  67. .max_pstate = 31,
  68. .cur_pstate_mask = APPLE_DVFS_STATUS_CUR_PS_T8112,
  69. .cur_pstate_shift = APPLE_DVFS_STATUS_CUR_PS_SHIFT_T8112,
  70. };
  71. static const struct apple_soc_cpufreq_info soc_default_info = {
  72. .max_pstate = 15,
  73. .cur_pstate_mask = 0, /* fallback */
  74. };
  75. static const struct of_device_id apple_soc_cpufreq_of_match[] __maybe_unused = {
  76. {
  77. .compatible = "apple,t8103-cluster-cpufreq",
  78. .data = &soc_t8103_info,
  79. },
  80. {
  81. .compatible = "apple,t8112-cluster-cpufreq",
  82. .data = &soc_t8112_info,
  83. },
  84. {
  85. .compatible = "apple,cluster-cpufreq",
  86. .data = &soc_default_info,
  87. },
  88. {}
  89. };
  90. static unsigned int apple_soc_cpufreq_get_rate(unsigned int cpu)
  91. {
  92. struct cpufreq_policy *policy;
  93. struct apple_cpu_priv *priv;
  94. struct cpufreq_frequency_table *p;
  95. unsigned int pstate;
  96. policy = cpufreq_cpu_get_raw(cpu);
  97. if (unlikely(!policy))
  98. return 0;
  99. priv = policy->driver_data;
  100. if (priv->info->cur_pstate_mask) {
  101. u64 reg = readq_relaxed(priv->reg_base + APPLE_DVFS_STATUS);
  102. pstate = (reg & priv->info->cur_pstate_mask) >> priv->info->cur_pstate_shift;
  103. } else {
  104. /*
  105. * For the fallback case we might not know the layout of DVFS_STATUS,
  106. * so just use the command register value (which ignores boost limitations).
  107. */
  108. u64 reg = readq_relaxed(priv->reg_base + APPLE_DVFS_CMD);
  109. pstate = FIELD_GET(APPLE_DVFS_CMD_PS1, reg);
  110. }
  111. cpufreq_for_each_valid_entry(p, policy->freq_table)
  112. if (p->driver_data == pstate)
  113. return p->frequency;
  114. dev_err(priv->cpu_dev, "could not find frequency for pstate %d\n",
  115. pstate);
  116. return 0;
  117. }
  118. static int apple_soc_cpufreq_set_target(struct cpufreq_policy *policy,
  119. unsigned int index)
  120. {
  121. struct apple_cpu_priv *priv = policy->driver_data;
  122. unsigned int pstate = policy->freq_table[index].driver_data;
  123. u64 reg;
  124. /* Fallback for newer SoCs */
  125. if (index > priv->info->max_pstate)
  126. index = priv->info->max_pstate;
  127. if (readq_poll_timeout_atomic(priv->reg_base + APPLE_DVFS_CMD, reg,
  128. !(reg & APPLE_DVFS_CMD_BUSY), 2,
  129. APPLE_DVFS_TRANSITION_TIMEOUT)) {
  130. return -EIO;
  131. }
  132. reg &= ~(APPLE_DVFS_CMD_PS1 | APPLE_DVFS_CMD_PS2);
  133. reg |= FIELD_PREP(APPLE_DVFS_CMD_PS1, pstate);
  134. reg |= FIELD_PREP(APPLE_DVFS_CMD_PS2, pstate);
  135. reg |= APPLE_DVFS_CMD_SET;
  136. writeq_relaxed(reg, priv->reg_base + APPLE_DVFS_CMD);
  137. return 0;
  138. }
  139. static unsigned int apple_soc_cpufreq_fast_switch(struct cpufreq_policy *policy,
  140. unsigned int target_freq)
  141. {
  142. if (apple_soc_cpufreq_set_target(policy, policy->cached_resolved_idx) < 0)
  143. return 0;
  144. return policy->freq_table[policy->cached_resolved_idx].frequency;
  145. }
  146. static int apple_soc_cpufreq_find_cluster(struct cpufreq_policy *policy,
  147. void __iomem **reg_base,
  148. const struct apple_soc_cpufreq_info **info)
  149. {
  150. struct of_phandle_args args;
  151. const struct of_device_id *match;
  152. int ret = 0;
  153. ret = of_perf_domain_get_sharing_cpumask(policy->cpu, "performance-domains",
  154. "#performance-domain-cells",
  155. policy->cpus, &args);
  156. if (ret < 0)
  157. return ret;
  158. match = of_match_node(apple_soc_cpufreq_of_match, args.np);
  159. of_node_put(args.np);
  160. if (!match)
  161. return -ENODEV;
  162. *info = match->data;
  163. *reg_base = of_iomap(args.np, 0);
  164. if (!*reg_base)
  165. return -ENOMEM;
  166. return 0;
  167. }
  168. static struct freq_attr *apple_soc_cpufreq_hw_attr[] = {
  169. &cpufreq_freq_attr_scaling_available_freqs,
  170. NULL, /* Filled in below if boost is enabled */
  171. NULL,
  172. };
  173. static int apple_soc_cpufreq_init(struct cpufreq_policy *policy)
  174. {
  175. int ret, i;
  176. unsigned int transition_latency;
  177. void __iomem *reg_base;
  178. struct device *cpu_dev;
  179. struct apple_cpu_priv *priv;
  180. const struct apple_soc_cpufreq_info *info;
  181. struct cpufreq_frequency_table *freq_table;
  182. cpu_dev = get_cpu_device(policy->cpu);
  183. if (!cpu_dev) {
  184. pr_err("failed to get cpu%d device\n", policy->cpu);
  185. return -ENODEV;
  186. }
  187. ret = dev_pm_opp_of_add_table(cpu_dev);
  188. if (ret < 0) {
  189. dev_err(cpu_dev, "%s: failed to add OPP table: %d\n", __func__, ret);
  190. return ret;
  191. }
  192. ret = apple_soc_cpufreq_find_cluster(policy, &reg_base, &info);
  193. if (ret) {
  194. dev_err(cpu_dev, "%s: failed to get cluster info: %d\n", __func__, ret);
  195. return ret;
  196. }
  197. ret = dev_pm_opp_set_sharing_cpus(cpu_dev, policy->cpus);
  198. if (ret) {
  199. dev_err(cpu_dev, "%s: failed to mark OPPs as shared: %d\n", __func__, ret);
  200. goto out_iounmap;
  201. }
  202. ret = dev_pm_opp_get_opp_count(cpu_dev);
  203. if (ret <= 0) {
  204. dev_dbg(cpu_dev, "OPP table is not ready, deferring probe\n");
  205. ret = -EPROBE_DEFER;
  206. goto out_free_opp;
  207. }
  208. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  209. if (!priv) {
  210. ret = -ENOMEM;
  211. goto out_free_opp;
  212. }
  213. ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table);
  214. if (ret) {
  215. dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
  216. goto out_free_priv;
  217. }
  218. /* Get OPP levels (p-state indexes) and stash them in driver_data */
  219. for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
  220. unsigned long rate = freq_table[i].frequency * 1000 + 999;
  221. struct dev_pm_opp *opp = dev_pm_opp_find_freq_floor(cpu_dev, &rate);
  222. if (IS_ERR(opp)) {
  223. ret = PTR_ERR(opp);
  224. goto out_free_cpufreq_table;
  225. }
  226. freq_table[i].driver_data = dev_pm_opp_get_level(opp);
  227. dev_pm_opp_put(opp);
  228. }
  229. priv->cpu_dev = cpu_dev;
  230. priv->reg_base = reg_base;
  231. priv->info = info;
  232. policy->driver_data = priv;
  233. policy->freq_table = freq_table;
  234. transition_latency = dev_pm_opp_get_max_transition_latency(cpu_dev);
  235. if (!transition_latency)
  236. transition_latency = CPUFREQ_ETERNAL;
  237. policy->cpuinfo.transition_latency = transition_latency;
  238. policy->dvfs_possible_from_any_cpu = true;
  239. policy->fast_switch_possible = true;
  240. policy->suspend_freq = freq_table[0].frequency;
  241. if (policy_has_boost_freq(policy)) {
  242. ret = cpufreq_enable_boost_support();
  243. if (ret) {
  244. dev_warn(cpu_dev, "failed to enable boost: %d\n", ret);
  245. } else {
  246. apple_soc_cpufreq_hw_attr[1] = &cpufreq_freq_attr_scaling_boost_freqs;
  247. apple_soc_cpufreq_driver.boost_enabled = true;
  248. }
  249. }
  250. return 0;
  251. out_free_cpufreq_table:
  252. dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
  253. out_free_priv:
  254. kfree(priv);
  255. out_free_opp:
  256. dev_pm_opp_remove_all_dynamic(cpu_dev);
  257. out_iounmap:
  258. iounmap(reg_base);
  259. return ret;
  260. }
  261. static void apple_soc_cpufreq_exit(struct cpufreq_policy *policy)
  262. {
  263. struct apple_cpu_priv *priv = policy->driver_data;
  264. dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &policy->freq_table);
  265. dev_pm_opp_remove_all_dynamic(priv->cpu_dev);
  266. iounmap(priv->reg_base);
  267. kfree(priv);
  268. }
  269. static struct cpufreq_driver apple_soc_cpufreq_driver = {
  270. .name = "apple-cpufreq",
  271. .flags = CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
  272. CPUFREQ_NEED_INITIAL_FREQ_CHECK | CPUFREQ_IS_COOLING_DEV,
  273. .verify = cpufreq_generic_frequency_table_verify,
  274. .get = apple_soc_cpufreq_get_rate,
  275. .init = apple_soc_cpufreq_init,
  276. .exit = apple_soc_cpufreq_exit,
  277. .target_index = apple_soc_cpufreq_set_target,
  278. .fast_switch = apple_soc_cpufreq_fast_switch,
  279. .register_em = cpufreq_register_em_with_opp,
  280. .attr = apple_soc_cpufreq_hw_attr,
  281. .suspend = cpufreq_generic_suspend,
  282. };
  283. static int __init apple_soc_cpufreq_module_init(void)
  284. {
  285. if (!of_machine_is_compatible("apple,arm-platform"))
  286. return -ENODEV;
  287. return cpufreq_register_driver(&apple_soc_cpufreq_driver);
  288. }
  289. module_init(apple_soc_cpufreq_module_init);
  290. static void __exit apple_soc_cpufreq_module_exit(void)
  291. {
  292. cpufreq_unregister_driver(&apple_soc_cpufreq_driver);
  293. }
  294. module_exit(apple_soc_cpufreq_module_exit);
  295. MODULE_DEVICE_TABLE(of, apple_soc_cpufreq_of_match);
  296. MODULE_AUTHOR("Hector Martin <marcan@marcan.st>");
  297. MODULE_DESCRIPTION("Apple SoC CPU cluster DVFS driver");
  298. MODULE_LICENSE("GPL");