osm-l3.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/bitfield.h>
  6. #include <linux/clk.h>
  7. #include <linux/interconnect-provider.h>
  8. #include <linux/io.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/of_device.h>
  12. #include <linux/platform_device.h>
  13. #include <dt-bindings/interconnect/qcom,osm-l3.h>
  14. #include "sc7180.h"
  15. #include "sc7280.h"
  16. #include "sc8180x.h"
  17. #include "sdm845.h"
  18. #include "sm8150.h"
  19. #include "sm8250.h"
  20. #define LUT_MAX_ENTRIES 40U
  21. #define LUT_SRC GENMASK(31, 30)
  22. #define LUT_L_VAL GENMASK(7, 0)
  23. #define CLK_HW_DIV 2
  24. /* OSM Register offsets */
  25. #define REG_ENABLE 0x0
  26. #define OSM_LUT_ROW_SIZE 32
  27. #define OSM_REG_FREQ_LUT 0x110
  28. #define OSM_REG_PERF_STATE 0x920
  29. /* EPSS Register offsets */
  30. #define EPSS_LUT_ROW_SIZE 4
  31. #define EPSS_REG_FREQ_LUT 0x100
  32. #define EPSS_REG_PERF_STATE 0x320
  33. #define OSM_L3_MAX_LINKS 1
  34. #define to_osm_l3_provider(_provider) \
  35. container_of(_provider, struct qcom_osm_l3_icc_provider, provider)
  36. struct qcom_osm_l3_icc_provider {
  37. void __iomem *base;
  38. unsigned int max_state;
  39. unsigned int reg_perf_state;
  40. unsigned long lut_tables[LUT_MAX_ENTRIES];
  41. struct icc_provider provider;
  42. };
  43. /**
  44. * struct qcom_osm_l3_node - Qualcomm specific interconnect nodes
  45. * @name: the node name used in debugfs
  46. * @links: an array of nodes where we can go next while traversing
  47. * @id: a unique node identifier
  48. * @num_links: the total number of @links
  49. * @buswidth: width of the interconnect between a node and the bus
  50. */
  51. struct qcom_osm_l3_node {
  52. const char *name;
  53. u16 links[OSM_L3_MAX_LINKS];
  54. u16 id;
  55. u16 num_links;
  56. u16 buswidth;
  57. };
  58. struct qcom_osm_l3_desc {
  59. const struct qcom_osm_l3_node * const *nodes;
  60. size_t num_nodes;
  61. unsigned int lut_row_size;
  62. unsigned int reg_freq_lut;
  63. unsigned int reg_perf_state;
  64. };
  65. #define DEFINE_QNODE(_name, _id, _buswidth, ...) \
  66. static const struct qcom_osm_l3_node _name = { \
  67. .name = #_name, \
  68. .id = _id, \
  69. .buswidth = _buswidth, \
  70. .num_links = ARRAY_SIZE(((int[]){ __VA_ARGS__ })), \
  71. .links = { __VA_ARGS__ }, \
  72. }
  73. DEFINE_QNODE(sdm845_osm_apps_l3, SDM845_MASTER_OSM_L3_APPS, 16, SDM845_SLAVE_OSM_L3);
  74. DEFINE_QNODE(sdm845_osm_l3, SDM845_SLAVE_OSM_L3, 16);
  75. static const struct qcom_osm_l3_node * const sdm845_osm_l3_nodes[] = {
  76. [MASTER_OSM_L3_APPS] = &sdm845_osm_apps_l3,
  77. [SLAVE_OSM_L3] = &sdm845_osm_l3,
  78. };
  79. static const struct qcom_osm_l3_desc sdm845_icc_osm_l3 = {
  80. .nodes = sdm845_osm_l3_nodes,
  81. .num_nodes = ARRAY_SIZE(sdm845_osm_l3_nodes),
  82. .lut_row_size = OSM_LUT_ROW_SIZE,
  83. .reg_freq_lut = OSM_REG_FREQ_LUT,
  84. .reg_perf_state = OSM_REG_PERF_STATE,
  85. };
  86. DEFINE_QNODE(sc7180_osm_apps_l3, SC7180_MASTER_OSM_L3_APPS, 16, SC7180_SLAVE_OSM_L3);
  87. DEFINE_QNODE(sc7180_osm_l3, SC7180_SLAVE_OSM_L3, 16);
  88. static const struct qcom_osm_l3_node * const sc7180_osm_l3_nodes[] = {
  89. [MASTER_OSM_L3_APPS] = &sc7180_osm_apps_l3,
  90. [SLAVE_OSM_L3] = &sc7180_osm_l3,
  91. };
  92. static const struct qcom_osm_l3_desc sc7180_icc_osm_l3 = {
  93. .nodes = sc7180_osm_l3_nodes,
  94. .num_nodes = ARRAY_SIZE(sc7180_osm_l3_nodes),
  95. .lut_row_size = OSM_LUT_ROW_SIZE,
  96. .reg_freq_lut = OSM_REG_FREQ_LUT,
  97. .reg_perf_state = OSM_REG_PERF_STATE,
  98. };
  99. DEFINE_QNODE(sc7280_epss_apps_l3, SC7280_MASTER_EPSS_L3_APPS, 32, SC7280_SLAVE_EPSS_L3);
  100. DEFINE_QNODE(sc7280_epss_l3, SC7280_SLAVE_EPSS_L3, 32);
  101. static const struct qcom_osm_l3_node * const sc7280_epss_l3_nodes[] = {
  102. [MASTER_EPSS_L3_APPS] = &sc7280_epss_apps_l3,
  103. [SLAVE_EPSS_L3_SHARED] = &sc7280_epss_l3,
  104. };
  105. static const struct qcom_osm_l3_desc sc7280_icc_epss_l3 = {
  106. .nodes = sc7280_epss_l3_nodes,
  107. .num_nodes = ARRAY_SIZE(sc7280_epss_l3_nodes),
  108. .lut_row_size = EPSS_LUT_ROW_SIZE,
  109. .reg_freq_lut = EPSS_REG_FREQ_LUT,
  110. .reg_perf_state = EPSS_REG_PERF_STATE,
  111. };
  112. DEFINE_QNODE(sc8180x_osm_apps_l3, SC8180X_MASTER_OSM_L3_APPS, 32, SC8180X_SLAVE_OSM_L3);
  113. DEFINE_QNODE(sc8180x_osm_l3, SC8180X_SLAVE_OSM_L3, 32);
  114. static const struct qcom_osm_l3_node * const sc8180x_osm_l3_nodes[] = {
  115. [MASTER_OSM_L3_APPS] = &sc8180x_osm_apps_l3,
  116. [SLAVE_OSM_L3] = &sc8180x_osm_l3,
  117. };
  118. static const struct qcom_osm_l3_desc sc8180x_icc_osm_l3 = {
  119. .nodes = sc8180x_osm_l3_nodes,
  120. .num_nodes = ARRAY_SIZE(sc8180x_osm_l3_nodes),
  121. .lut_row_size = OSM_LUT_ROW_SIZE,
  122. .reg_freq_lut = OSM_REG_FREQ_LUT,
  123. .reg_perf_state = OSM_REG_PERF_STATE,
  124. };
  125. DEFINE_QNODE(sm8150_osm_apps_l3, SM8150_MASTER_OSM_L3_APPS, 32, SM8150_SLAVE_OSM_L3);
  126. DEFINE_QNODE(sm8150_osm_l3, SM8150_SLAVE_OSM_L3, 32);
  127. static const struct qcom_osm_l3_node * const sm8150_osm_l3_nodes[] = {
  128. [MASTER_OSM_L3_APPS] = &sm8150_osm_apps_l3,
  129. [SLAVE_OSM_L3] = &sm8150_osm_l3,
  130. };
  131. static const struct qcom_osm_l3_desc sm8150_icc_osm_l3 = {
  132. .nodes = sm8150_osm_l3_nodes,
  133. .num_nodes = ARRAY_SIZE(sm8150_osm_l3_nodes),
  134. .lut_row_size = OSM_LUT_ROW_SIZE,
  135. .reg_freq_lut = OSM_REG_FREQ_LUT,
  136. .reg_perf_state = OSM_REG_PERF_STATE,
  137. };
  138. DEFINE_QNODE(sm8250_epss_apps_l3, SM8250_MASTER_EPSS_L3_APPS, 32, SM8250_SLAVE_EPSS_L3);
  139. DEFINE_QNODE(sm8250_epss_l3, SM8250_SLAVE_EPSS_L3, 32);
  140. static const struct qcom_osm_l3_node * const sm8250_epss_l3_nodes[] = {
  141. [MASTER_EPSS_L3_APPS] = &sm8250_epss_apps_l3,
  142. [SLAVE_EPSS_L3_SHARED] = &sm8250_epss_l3,
  143. };
  144. static const struct qcom_osm_l3_desc sm8250_icc_epss_l3 = {
  145. .nodes = sm8250_epss_l3_nodes,
  146. .num_nodes = ARRAY_SIZE(sm8250_epss_l3_nodes),
  147. .lut_row_size = EPSS_LUT_ROW_SIZE,
  148. .reg_freq_lut = EPSS_REG_FREQ_LUT,
  149. .reg_perf_state = EPSS_REG_PERF_STATE,
  150. };
  151. static int qcom_osm_l3_set(struct icc_node *src, struct icc_node *dst)
  152. {
  153. struct qcom_osm_l3_icc_provider *qp;
  154. struct icc_provider *provider;
  155. const struct qcom_osm_l3_node *qn;
  156. struct icc_node *n;
  157. unsigned int index;
  158. u32 agg_peak = 0;
  159. u32 agg_avg = 0;
  160. u64 rate;
  161. qn = src->data;
  162. provider = src->provider;
  163. qp = to_osm_l3_provider(provider);
  164. list_for_each_entry(n, &provider->nodes, node_list)
  165. provider->aggregate(n, 0, n->avg_bw, n->peak_bw,
  166. &agg_avg, &agg_peak);
  167. rate = max(agg_avg, agg_peak);
  168. rate = icc_units_to_bps(rate);
  169. do_div(rate, qn->buswidth);
  170. for (index = 0; index < qp->max_state - 1; index++) {
  171. if (qp->lut_tables[index] >= rate)
  172. break;
  173. }
  174. writel_relaxed(index, qp->base + qp->reg_perf_state);
  175. return 0;
  176. }
  177. static int qcom_osm_l3_remove(struct platform_device *pdev)
  178. {
  179. struct qcom_osm_l3_icc_provider *qp = platform_get_drvdata(pdev);
  180. icc_provider_deregister(&qp->provider);
  181. icc_nodes_remove(&qp->provider);
  182. return 0;
  183. }
  184. static int qcom_osm_l3_probe(struct platform_device *pdev)
  185. {
  186. u32 info, src, lval, i, prev_freq = 0, freq;
  187. static unsigned long hw_rate, xo_rate;
  188. struct qcom_osm_l3_icc_provider *qp;
  189. const struct qcom_osm_l3_desc *desc;
  190. struct icc_onecell_data *data;
  191. struct icc_provider *provider;
  192. const struct qcom_osm_l3_node * const *qnodes;
  193. struct icc_node *node;
  194. size_t num_nodes;
  195. struct clk *clk;
  196. int ret;
  197. clk = clk_get(&pdev->dev, "xo");
  198. if (IS_ERR(clk))
  199. return PTR_ERR(clk);
  200. xo_rate = clk_get_rate(clk);
  201. clk_put(clk);
  202. clk = clk_get(&pdev->dev, "alternate");
  203. if (IS_ERR(clk))
  204. return PTR_ERR(clk);
  205. hw_rate = clk_get_rate(clk) / CLK_HW_DIV;
  206. clk_put(clk);
  207. qp = devm_kzalloc(&pdev->dev, sizeof(*qp), GFP_KERNEL);
  208. if (!qp)
  209. return -ENOMEM;
  210. qp->base = devm_platform_ioremap_resource(pdev, 0);
  211. if (IS_ERR(qp->base))
  212. return PTR_ERR(qp->base);
  213. /* HW should be in enabled state to proceed */
  214. if (!(readl_relaxed(qp->base + REG_ENABLE) & 0x1)) {
  215. dev_err(&pdev->dev, "error hardware not enabled\n");
  216. return -ENODEV;
  217. }
  218. desc = device_get_match_data(&pdev->dev);
  219. if (!desc)
  220. return -EINVAL;
  221. qp->reg_perf_state = desc->reg_perf_state;
  222. for (i = 0; i < LUT_MAX_ENTRIES; i++) {
  223. info = readl_relaxed(qp->base + desc->reg_freq_lut +
  224. i * desc->lut_row_size);
  225. src = FIELD_GET(LUT_SRC, info);
  226. lval = FIELD_GET(LUT_L_VAL, info);
  227. if (src)
  228. freq = xo_rate * lval;
  229. else
  230. freq = hw_rate;
  231. /* Two of the same frequencies signify end of table */
  232. if (i > 0 && prev_freq == freq)
  233. break;
  234. dev_dbg(&pdev->dev, "index=%d freq=%d\n", i, freq);
  235. qp->lut_tables[i] = freq;
  236. prev_freq = freq;
  237. }
  238. qp->max_state = i;
  239. qnodes = desc->nodes;
  240. num_nodes = desc->num_nodes;
  241. data = devm_kzalloc(&pdev->dev, struct_size(data, nodes, num_nodes), GFP_KERNEL);
  242. if (!data)
  243. return -ENOMEM;
  244. provider = &qp->provider;
  245. provider->dev = &pdev->dev;
  246. provider->set = qcom_osm_l3_set;
  247. provider->aggregate = icc_std_aggregate;
  248. provider->xlate = of_icc_xlate_onecell;
  249. provider->data = data;
  250. icc_provider_init(provider);
  251. for (i = 0; i < num_nodes; i++) {
  252. size_t j;
  253. node = icc_node_create(qnodes[i]->id);
  254. if (IS_ERR(node)) {
  255. ret = PTR_ERR(node);
  256. goto err;
  257. }
  258. node->name = qnodes[i]->name;
  259. /* Cast away const and add it back in qcom_osm_l3_set() */
  260. node->data = (void *)qnodes[i];
  261. icc_node_add(node, provider);
  262. for (j = 0; j < qnodes[i]->num_links; j++)
  263. icc_link_create(node, qnodes[i]->links[j]);
  264. data->nodes[i] = node;
  265. }
  266. data->num_nodes = num_nodes;
  267. ret = icc_provider_register(provider);
  268. if (ret)
  269. goto err;
  270. platform_set_drvdata(pdev, qp);
  271. return 0;
  272. err:
  273. icc_nodes_remove(provider);
  274. return ret;
  275. }
  276. static const struct of_device_id osm_l3_of_match[] = {
  277. { .compatible = "qcom,sc7180-osm-l3", .data = &sc7180_icc_osm_l3 },
  278. { .compatible = "qcom,sc7280-epss-l3", .data = &sc7280_icc_epss_l3 },
  279. { .compatible = "qcom,sdm845-osm-l3", .data = &sdm845_icc_osm_l3 },
  280. { .compatible = "qcom,sm8150-osm-l3", .data = &sm8150_icc_osm_l3 },
  281. { .compatible = "qcom,sc8180x-osm-l3", .data = &sc8180x_icc_osm_l3 },
  282. { .compatible = "qcom,sm8250-epss-l3", .data = &sm8250_icc_epss_l3 },
  283. { }
  284. };
  285. MODULE_DEVICE_TABLE(of, osm_l3_of_match);
  286. static struct platform_driver osm_l3_driver = {
  287. .probe = qcom_osm_l3_probe,
  288. .remove = qcom_osm_l3_remove,
  289. .driver = {
  290. .name = "osm-l3",
  291. .of_match_table = osm_l3_of_match,
  292. .sync_state = icc_sync_state,
  293. },
  294. };
  295. module_platform_driver(osm_l3_driver);
  296. MODULE_DESCRIPTION("Qualcomm OSM L3 interconnect driver");
  297. MODULE_LICENSE("GPL v2");