tegra186-cpufreq.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved
  4. */
  5. #include <linux/cpufreq.h>
  6. #include <linux/dma-mapping.h>
  7. #include <linux/module.h>
  8. #include <linux/of.h>
  9. #include <linux/platform_device.h>
  10. #include <soc/tegra/bpmp.h>
  11. #include <soc/tegra/bpmp-abi.h>
  12. #define TEGRA186_NUM_CLUSTERS 2
  13. #define EDVD_OFFSET_A57(core) ((SZ_64K * 6) + (0x20 + (core) * 0x4))
  14. #define EDVD_OFFSET_DENVER(core) ((SZ_64K * 7) + (0x20 + (core) * 0x4))
  15. #define EDVD_CORE_VOLT_FREQ_F_SHIFT 0
  16. #define EDVD_CORE_VOLT_FREQ_F_MASK 0xffff
  17. #define EDVD_CORE_VOLT_FREQ_V_SHIFT 16
  18. struct tegra186_cpufreq_cpu {
  19. unsigned int bpmp_cluster_id;
  20. unsigned int edvd_offset;
  21. };
  22. static const struct tegra186_cpufreq_cpu tegra186_cpus[] = {
  23. /* CPU0 - A57 Cluster */
  24. {
  25. .bpmp_cluster_id = 1,
  26. .edvd_offset = EDVD_OFFSET_A57(0)
  27. },
  28. /* CPU1 - Denver Cluster */
  29. {
  30. .bpmp_cluster_id = 0,
  31. .edvd_offset = EDVD_OFFSET_DENVER(0)
  32. },
  33. /* CPU2 - Denver Cluster */
  34. {
  35. .bpmp_cluster_id = 0,
  36. .edvd_offset = EDVD_OFFSET_DENVER(1)
  37. },
  38. /* CPU3 - A57 Cluster */
  39. {
  40. .bpmp_cluster_id = 1,
  41. .edvd_offset = EDVD_OFFSET_A57(1)
  42. },
  43. /* CPU4 - A57 Cluster */
  44. {
  45. .bpmp_cluster_id = 1,
  46. .edvd_offset = EDVD_OFFSET_A57(2)
  47. },
  48. /* CPU5 - A57 Cluster */
  49. {
  50. .bpmp_cluster_id = 1,
  51. .edvd_offset = EDVD_OFFSET_A57(3)
  52. },
  53. };
  54. struct tegra186_cpufreq_cluster {
  55. struct cpufreq_frequency_table *table;
  56. u32 ref_clk_khz;
  57. u32 div;
  58. };
  59. struct tegra186_cpufreq_data {
  60. void __iomem *regs;
  61. const struct tegra186_cpufreq_cpu *cpus;
  62. struct tegra186_cpufreq_cluster clusters[];
  63. };
  64. static int tegra186_cpufreq_init(struct cpufreq_policy *policy)
  65. {
  66. struct tegra186_cpufreq_data *data = cpufreq_get_driver_data();
  67. unsigned int cluster = data->cpus[policy->cpu].bpmp_cluster_id;
  68. policy->freq_table = data->clusters[cluster].table;
  69. policy->cpuinfo.transition_latency = 300 * 1000;
  70. policy->driver_data = NULL;
  71. return 0;
  72. }
  73. static int tegra186_cpufreq_set_target(struct cpufreq_policy *policy,
  74. unsigned int index)
  75. {
  76. struct tegra186_cpufreq_data *data = cpufreq_get_driver_data();
  77. struct cpufreq_frequency_table *tbl = policy->freq_table + index;
  78. unsigned int edvd_offset;
  79. u32 edvd_val = tbl->driver_data;
  80. u32 cpu;
  81. for_each_cpu(cpu, policy->cpus) {
  82. edvd_offset = data->cpus[cpu].edvd_offset;
  83. writel(edvd_val, data->regs + edvd_offset);
  84. }
  85. return 0;
  86. }
  87. static unsigned int tegra186_cpufreq_get(unsigned int cpu)
  88. {
  89. struct tegra186_cpufreq_data *data = cpufreq_get_driver_data();
  90. struct tegra186_cpufreq_cluster *cluster;
  91. struct cpufreq_policy *policy;
  92. unsigned int edvd_offset, cluster_id;
  93. u32 ndiv;
  94. policy = cpufreq_cpu_get(cpu);
  95. if (!policy)
  96. return 0;
  97. edvd_offset = data->cpus[policy->cpu].edvd_offset;
  98. ndiv = readl(data->regs + edvd_offset) & EDVD_CORE_VOLT_FREQ_F_MASK;
  99. cluster_id = data->cpus[policy->cpu].bpmp_cluster_id;
  100. cluster = &data->clusters[cluster_id];
  101. cpufreq_cpu_put(policy);
  102. return (cluster->ref_clk_khz * ndiv) / cluster->div;
  103. }
  104. static struct cpufreq_driver tegra186_cpufreq_driver = {
  105. .name = "tegra186",
  106. .flags = CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
  107. CPUFREQ_NEED_INITIAL_FREQ_CHECK,
  108. .get = tegra186_cpufreq_get,
  109. .verify = cpufreq_generic_frequency_table_verify,
  110. .target_index = tegra186_cpufreq_set_target,
  111. .init = tegra186_cpufreq_init,
  112. .attr = cpufreq_generic_attr,
  113. };
  114. static struct cpufreq_frequency_table *init_vhint_table(
  115. struct platform_device *pdev, struct tegra_bpmp *bpmp,
  116. struct tegra186_cpufreq_cluster *cluster, unsigned int cluster_id)
  117. {
  118. struct cpufreq_frequency_table *table;
  119. struct mrq_cpu_vhint_request req;
  120. struct tegra_bpmp_message msg;
  121. struct cpu_vhint_data *data;
  122. int err, i, j, num_rates = 0;
  123. dma_addr_t phys;
  124. void *virt;
  125. virt = dma_alloc_coherent(bpmp->dev, sizeof(*data), &phys,
  126. GFP_KERNEL);
  127. if (!virt)
  128. return ERR_PTR(-ENOMEM);
  129. data = (struct cpu_vhint_data *)virt;
  130. memset(&req, 0, sizeof(req));
  131. req.addr = phys;
  132. req.cluster_id = cluster_id;
  133. memset(&msg, 0, sizeof(msg));
  134. msg.mrq = MRQ_CPU_VHINT;
  135. msg.tx.data = &req;
  136. msg.tx.size = sizeof(req);
  137. err = tegra_bpmp_transfer(bpmp, &msg);
  138. if (err) {
  139. table = ERR_PTR(err);
  140. goto free;
  141. }
  142. if (msg.rx.ret) {
  143. table = ERR_PTR(-EINVAL);
  144. goto free;
  145. }
  146. for (i = data->vfloor; i <= data->vceil; i++) {
  147. u16 ndiv = data->ndiv[i];
  148. if (ndiv < data->ndiv_min || ndiv > data->ndiv_max)
  149. continue;
  150. /* Only store lowest voltage index for each rate */
  151. if (i > 0 && ndiv == data->ndiv[i - 1])
  152. continue;
  153. num_rates++;
  154. }
  155. table = devm_kcalloc(&pdev->dev, num_rates + 1, sizeof(*table),
  156. GFP_KERNEL);
  157. if (!table) {
  158. table = ERR_PTR(-ENOMEM);
  159. goto free;
  160. }
  161. cluster->ref_clk_khz = data->ref_clk_hz / 1000;
  162. cluster->div = data->pdiv * data->mdiv;
  163. for (i = data->vfloor, j = 0; i <= data->vceil; i++) {
  164. struct cpufreq_frequency_table *point;
  165. u16 ndiv = data->ndiv[i];
  166. u32 edvd_val = 0;
  167. if (ndiv < data->ndiv_min || ndiv > data->ndiv_max)
  168. continue;
  169. /* Only store lowest voltage index for each rate */
  170. if (i > 0 && ndiv == data->ndiv[i - 1])
  171. continue;
  172. edvd_val |= i << EDVD_CORE_VOLT_FREQ_V_SHIFT;
  173. edvd_val |= ndiv << EDVD_CORE_VOLT_FREQ_F_SHIFT;
  174. point = &table[j++];
  175. point->driver_data = edvd_val;
  176. point->frequency = (cluster->ref_clk_khz * ndiv) / cluster->div;
  177. }
  178. table[j].frequency = CPUFREQ_TABLE_END;
  179. free:
  180. dma_free_coherent(bpmp->dev, sizeof(*data), virt, phys);
  181. return table;
  182. }
  183. static int tegra186_cpufreq_probe(struct platform_device *pdev)
  184. {
  185. struct tegra186_cpufreq_data *data;
  186. struct tegra_bpmp *bpmp;
  187. unsigned int i = 0, err;
  188. data = devm_kzalloc(&pdev->dev,
  189. struct_size(data, clusters, TEGRA186_NUM_CLUSTERS),
  190. GFP_KERNEL);
  191. if (!data)
  192. return -ENOMEM;
  193. data->cpus = tegra186_cpus;
  194. bpmp = tegra_bpmp_get(&pdev->dev);
  195. if (IS_ERR(bpmp))
  196. return PTR_ERR(bpmp);
  197. data->regs = devm_platform_ioremap_resource(pdev, 0);
  198. if (IS_ERR(data->regs)) {
  199. err = PTR_ERR(data->regs);
  200. goto put_bpmp;
  201. }
  202. for (i = 0; i < TEGRA186_NUM_CLUSTERS; i++) {
  203. struct tegra186_cpufreq_cluster *cluster = &data->clusters[i];
  204. cluster->table = init_vhint_table(pdev, bpmp, cluster, i);
  205. if (IS_ERR(cluster->table)) {
  206. err = PTR_ERR(cluster->table);
  207. goto put_bpmp;
  208. }
  209. }
  210. tegra186_cpufreq_driver.driver_data = data;
  211. err = cpufreq_register_driver(&tegra186_cpufreq_driver);
  212. put_bpmp:
  213. tegra_bpmp_put(bpmp);
  214. return err;
  215. }
  216. static void tegra186_cpufreq_remove(struct platform_device *pdev)
  217. {
  218. cpufreq_unregister_driver(&tegra186_cpufreq_driver);
  219. }
  220. static const struct of_device_id tegra186_cpufreq_of_match[] = {
  221. { .compatible = "nvidia,tegra186-ccplex-cluster", },
  222. { }
  223. };
  224. MODULE_DEVICE_TABLE(of, tegra186_cpufreq_of_match);
  225. static struct platform_driver tegra186_cpufreq_platform_driver = {
  226. .driver = {
  227. .name = "tegra186-cpufreq",
  228. .of_match_table = tegra186_cpufreq_of_match,
  229. },
  230. .probe = tegra186_cpufreq_probe,
  231. .remove_new = tegra186_cpufreq_remove,
  232. };
  233. module_platform_driver(tegra186_cpufreq_platform_driver);
  234. MODULE_AUTHOR("Mikko Perttunen <mperttunen@nvidia.com>");
  235. MODULE_DESCRIPTION("NVIDIA Tegra186 cpufreq driver");
  236. MODULE_LICENSE("GPL v2");