tegra186-cpufreq.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. struct tegra186_cpufreq_cluster *clusters;
  62. const struct tegra186_cpufreq_cpu *cpus;
  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 = data->cpus[policy->cpu].edvd_offset;
  79. u32 edvd_val = tbl->driver_data;
  80. writel(edvd_val, data->regs + edvd_offset);
  81. return 0;
  82. }
  83. static unsigned int tegra186_cpufreq_get(unsigned int cpu)
  84. {
  85. struct tegra186_cpufreq_data *data = cpufreq_get_driver_data();
  86. struct tegra186_cpufreq_cluster *cluster;
  87. struct cpufreq_policy *policy;
  88. unsigned int edvd_offset, cluster_id;
  89. u32 ndiv;
  90. policy = cpufreq_cpu_get(cpu);
  91. if (!policy)
  92. return 0;
  93. edvd_offset = data->cpus[policy->cpu].edvd_offset;
  94. ndiv = readl(data->regs + edvd_offset) & EDVD_CORE_VOLT_FREQ_F_MASK;
  95. cluster_id = data->cpus[policy->cpu].bpmp_cluster_id;
  96. cluster = &data->clusters[cluster_id];
  97. cpufreq_cpu_put(policy);
  98. return (cluster->ref_clk_khz * ndiv) / cluster->div;
  99. }
  100. static struct cpufreq_driver tegra186_cpufreq_driver = {
  101. .name = "tegra186",
  102. .flags = CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
  103. CPUFREQ_NEED_INITIAL_FREQ_CHECK,
  104. .get = tegra186_cpufreq_get,
  105. .verify = cpufreq_generic_frequency_table_verify,
  106. .target_index = tegra186_cpufreq_set_target,
  107. .init = tegra186_cpufreq_init,
  108. .attr = cpufreq_generic_attr,
  109. };
  110. static struct cpufreq_frequency_table *init_vhint_table(
  111. struct platform_device *pdev, struct tegra_bpmp *bpmp,
  112. struct tegra186_cpufreq_cluster *cluster, unsigned int cluster_id)
  113. {
  114. struct cpufreq_frequency_table *table;
  115. struct mrq_cpu_vhint_request req;
  116. struct tegra_bpmp_message msg;
  117. struct cpu_vhint_data *data;
  118. int err, i, j, num_rates = 0;
  119. dma_addr_t phys;
  120. void *virt;
  121. virt = dma_alloc_coherent(bpmp->dev, sizeof(*data), &phys,
  122. GFP_KERNEL);
  123. if (!virt)
  124. return ERR_PTR(-ENOMEM);
  125. data = (struct cpu_vhint_data *)virt;
  126. memset(&req, 0, sizeof(req));
  127. req.addr = phys;
  128. req.cluster_id = cluster_id;
  129. memset(&msg, 0, sizeof(msg));
  130. msg.mrq = MRQ_CPU_VHINT;
  131. msg.tx.data = &req;
  132. msg.tx.size = sizeof(req);
  133. err = tegra_bpmp_transfer(bpmp, &msg);
  134. if (err) {
  135. table = ERR_PTR(err);
  136. goto free;
  137. }
  138. if (msg.rx.ret) {
  139. table = ERR_PTR(-EINVAL);
  140. goto free;
  141. }
  142. for (i = data->vfloor; i <= data->vceil; i++) {
  143. u16 ndiv = data->ndiv[i];
  144. if (ndiv < data->ndiv_min || ndiv > data->ndiv_max)
  145. continue;
  146. /* Only store lowest voltage index for each rate */
  147. if (i > 0 && ndiv == data->ndiv[i - 1])
  148. continue;
  149. num_rates++;
  150. }
  151. table = devm_kcalloc(&pdev->dev, num_rates + 1, sizeof(*table),
  152. GFP_KERNEL);
  153. if (!table) {
  154. table = ERR_PTR(-ENOMEM);
  155. goto free;
  156. }
  157. cluster->ref_clk_khz = data->ref_clk_hz / 1000;
  158. cluster->div = data->pdiv * data->mdiv;
  159. for (i = data->vfloor, j = 0; i <= data->vceil; i++) {
  160. struct cpufreq_frequency_table *point;
  161. u16 ndiv = data->ndiv[i];
  162. u32 edvd_val = 0;
  163. if (ndiv < data->ndiv_min || ndiv > data->ndiv_max)
  164. continue;
  165. /* Only store lowest voltage index for each rate */
  166. if (i > 0 && ndiv == data->ndiv[i - 1])
  167. continue;
  168. edvd_val |= i << EDVD_CORE_VOLT_FREQ_V_SHIFT;
  169. edvd_val |= ndiv << EDVD_CORE_VOLT_FREQ_F_SHIFT;
  170. point = &table[j++];
  171. point->driver_data = edvd_val;
  172. point->frequency = (cluster->ref_clk_khz * ndiv) / cluster->div;
  173. }
  174. table[j].frequency = CPUFREQ_TABLE_END;
  175. free:
  176. dma_free_coherent(bpmp->dev, sizeof(*data), virt, phys);
  177. return table;
  178. }
  179. static int tegra186_cpufreq_probe(struct platform_device *pdev)
  180. {
  181. struct tegra186_cpufreq_data *data;
  182. struct tegra_bpmp *bpmp;
  183. unsigned int i = 0, err;
  184. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  185. if (!data)
  186. return -ENOMEM;
  187. data->clusters = devm_kcalloc(&pdev->dev, TEGRA186_NUM_CLUSTERS,
  188. sizeof(*data->clusters), GFP_KERNEL);
  189. if (!data->clusters)
  190. return -ENOMEM;
  191. data->cpus = tegra186_cpus;
  192. bpmp = tegra_bpmp_get(&pdev->dev);
  193. if (IS_ERR(bpmp))
  194. return PTR_ERR(bpmp);
  195. data->regs = devm_platform_ioremap_resource(pdev, 0);
  196. if (IS_ERR(data->regs)) {
  197. err = PTR_ERR(data->regs);
  198. goto put_bpmp;
  199. }
  200. for (i = 0; i < TEGRA186_NUM_CLUSTERS; i++) {
  201. struct tegra186_cpufreq_cluster *cluster = &data->clusters[i];
  202. cluster->table = init_vhint_table(pdev, bpmp, cluster, i);
  203. if (IS_ERR(cluster->table)) {
  204. err = PTR_ERR(cluster->table);
  205. goto put_bpmp;
  206. }
  207. }
  208. tegra186_cpufreq_driver.driver_data = data;
  209. err = cpufreq_register_driver(&tegra186_cpufreq_driver);
  210. put_bpmp:
  211. tegra_bpmp_put(bpmp);
  212. return err;
  213. }
  214. static int tegra186_cpufreq_remove(struct platform_device *pdev)
  215. {
  216. cpufreq_unregister_driver(&tegra186_cpufreq_driver);
  217. return 0;
  218. }
  219. static const struct of_device_id tegra186_cpufreq_of_match[] = {
  220. { .compatible = "nvidia,tegra186-ccplex-cluster", },
  221. { }
  222. };
  223. MODULE_DEVICE_TABLE(of, tegra186_cpufreq_of_match);
  224. static struct platform_driver tegra186_cpufreq_platform_driver = {
  225. .driver = {
  226. .name = "tegra186-cpufreq",
  227. .of_match_table = tegra186_cpufreq_of_match,
  228. },
  229. .probe = tegra186_cpufreq_probe,
  230. .remove = tegra186_cpufreq_remove,
  231. };
  232. module_platform_driver(tegra186_cpufreq_platform_driver);
  233. MODULE_AUTHOR("Mikko Perttunen <[email protected]>");
  234. MODULE_DESCRIPTION("NVIDIA Tegra186 cpufreq driver");
  235. MODULE_LICENSE("GPL v2");