qcom-cpufreq-hw-debug.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2023, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #define pr_fmt(fmt) "cpufreq_hw_debug: %s: " fmt, __func__
  7. #include <linux/device.h>
  8. #include <linux/kernel.h>
  9. #include <linux/panic_notifier.h>
  10. #include <linux/mfd/syscon.h>
  11. #include <linux/module.h>
  12. #include <linux/of_address.h>
  13. #include <linux/of_platform.h>
  14. enum debug_hw_regs_data {
  15. REG_PERF_STATE,
  16. REG_CYCLE_CNTR,
  17. REG_PSTATE_STATUS,
  18. REG_EPSS_DEBUG_STATUS,
  19. REG_EPSS_DEBUG_SRB,
  20. REG_EPSS_DEBUG_LUT,
  21. REG_ARRAY_SIZE,
  22. };
  23. struct cpufreq_hwregs {
  24. void * __iomem *base;
  25. int domain_cnt;
  26. };
  27. struct cpufreq_register_data {
  28. char *name;
  29. u16 offset;
  30. };
  31. static struct cpufreq_hwregs *hw_regs;
  32. static const u16 *offsets;
  33. static struct kobj_attribute cpufreq_hwregs_attr;
  34. static struct kobject *cpufreqhw_kobj;
  35. static const u16 cpufreq_qcom_std_data[] = {
  36. [REG_PERF_STATE] = 0x920,
  37. [REG_CYCLE_CNTR] = 0x9c0,
  38. [REG_PSTATE_STATUS] = 0x700,
  39. };
  40. static const u16 cpufreq_qcom_std_epss_data[] = {
  41. [REG_PERF_STATE] = 0x320,
  42. [REG_CYCLE_CNTR] = 0x3c4,
  43. [REG_PSTATE_STATUS] = 0x020,
  44. [REG_EPSS_DEBUG_STATUS] = 0x01c,
  45. [REG_EPSS_DEBUG_SRB] = 0x0bc,
  46. [REG_EPSS_DEBUG_LUT] = 0x100,
  47. };
  48. static ssize_t cpufreq_hwregs_show(struct kobject *kobj,
  49. struct kobj_attribute *attr, char *buf)
  50. {
  51. int i, j, size = ARRAY_SIZE(cpufreq_qcom_std_data);
  52. u32 regval, count = 0;
  53. static struct cpufreq_register_data data[] = {
  54. {"PERF_STATE_DESIRED", REG_PERF_STATE},
  55. {"CYCLE_CNTR_VAL", REG_CYCLE_CNTR},
  56. {"PSTATE_STATUS", REG_PSTATE_STATUS},
  57. {"EPSS_DEBUG_STATUS", REG_EPSS_DEBUG_STATUS},
  58. {"EPSS_DEBUG_SRB", REG_EPSS_DEBUG_SRB},
  59. {"EPSS_DEBUG_LUT", REG_EPSS_DEBUG_LUT},
  60. };
  61. if (offsets == cpufreq_qcom_std_epss_data)
  62. size = ARRAY_SIZE(cpufreq_qcom_std_epss_data);
  63. for (i = 0; i < hw_regs->domain_cnt; i++) {
  64. count += scnprintf(buf + count, PAGE_SIZE,
  65. "FREQUENCY DOMAIN %d\n", i);
  66. for (j = 0; j < size; j++) {
  67. regval = readl_relaxed(hw_regs->base[i] +
  68. offsets[data[j].offset]);
  69. count += scnprintf(buf + count, PAGE_SIZE,
  70. "%25s: 0x%.8x\n", data[j].name, regval);
  71. }
  72. }
  73. return count;
  74. }
  75. static int cpufreq_panic_callback(struct notifier_block *nfb,
  76. unsigned long event, void *unused)
  77. {
  78. int i, j, size = ARRAY_SIZE(cpufreq_qcom_std_data);
  79. u32 regval;
  80. static struct cpufreq_register_data data[] = {
  81. {"PERF_STATE_DESIRED", REG_PERF_STATE},
  82. {"CYCLE_CNTR_VAL", REG_CYCLE_CNTR},
  83. {"PSTATE_STATUS", REG_PSTATE_STATUS},
  84. {"EPSS_DEBUG_STATUS", REG_EPSS_DEBUG_STATUS},
  85. {"EPSS_DEBUG_SRB", REG_EPSS_DEBUG_SRB},
  86. {"EPSS_DEBUG_LUT", REG_EPSS_DEBUG_LUT},
  87. };
  88. if (offsets == cpufreq_qcom_std_epss_data)
  89. size = ARRAY_SIZE(cpufreq_qcom_std_epss_data);
  90. for (i = 0; i < hw_regs->domain_cnt; i++) {
  91. pr_err("FREQUENCY DOMAIN %d\n", i);
  92. for (j = 0; j < size; j++) {
  93. regval = readl_relaxed(hw_regs->base[i] +
  94. offsets[data[j].offset]);
  95. pr_err("%25s: 0x%.8x\n", data[j].name, regval);
  96. }
  97. }
  98. return NOTIFY_OK;
  99. }
  100. static struct notifier_block cpufreq_panic_notifier = {
  101. .notifier_call = cpufreq_panic_callback,
  102. .priority = 1,
  103. };
  104. static int cpufreq_get_hwregs(struct platform_device *pdev)
  105. {
  106. struct of_phandle_args args;
  107. struct property *prop;
  108. struct resource res;
  109. void __iomem *base;
  110. int i, ret;
  111. offsets = of_device_get_match_data(&pdev->dev);
  112. if (!offsets)
  113. return -EINVAL;
  114. hw_regs = devm_kzalloc(&pdev->dev, sizeof(*hw_regs), GFP_KERNEL);
  115. if (!hw_regs)
  116. return -ENOMEM;
  117. prop = of_find_property(pdev->dev.of_node, "qcom,freq-hw-domain", NULL);
  118. if (!prop)
  119. return -EINVAL;
  120. hw_regs->domain_cnt = prop->length / (2 * sizeof(prop->length));
  121. hw_regs->base = devm_kzalloc(&pdev->dev,
  122. hw_regs->domain_cnt * sizeof(base), GFP_KERNEL);
  123. if (!hw_regs->base)
  124. return -ENOMEM;
  125. for (i = 0; i < hw_regs->domain_cnt; i++) {
  126. ret = of_parse_phandle_with_fixed_args(pdev->dev.of_node,
  127. "qcom,freq-hw-domain", 1, i, &args);
  128. of_node_put(pdev->dev.of_node);
  129. if (ret)
  130. return ret;
  131. ret = of_address_to_resource(args.np, args.args[0], &res);
  132. if (ret)
  133. return ret;
  134. base = devm_ioremap(&pdev->dev, res.start, resource_size(&res));
  135. if (!base)
  136. return -ENOMEM;
  137. hw_regs->base[i] = base;
  138. }
  139. atomic_notifier_chain_register(&panic_notifier_list,
  140. &cpufreq_panic_notifier);
  141. return 0;
  142. }
  143. static int enable_cpufreq_hw_debug(struct platform_device *pdev)
  144. {
  145. int ret;
  146. ret = cpufreq_get_hwregs(pdev);
  147. if (ret < 0) {
  148. dev_err(&pdev->dev, "Failed to map cpufreq hw regs\n");
  149. return ret;
  150. }
  151. cpufreqhw_kobj = kobject_create_and_add("qcom-cpufreq-hw",
  152. kernel_kobj);
  153. if (!cpufreqhw_kobj)
  154. return -ENOMEM;
  155. sysfs_attr_init(&cpufreq_hwregs_attr.attr);
  156. cpufreq_hwregs_attr.attr.name = "print_cpufreq_debug_regs";
  157. cpufreq_hwregs_attr.show = cpufreq_hwregs_show;
  158. cpufreq_hwregs_attr.attr.mode = 0444;
  159. ret = sysfs_create_file(cpufreqhw_kobj, &cpufreq_hwregs_attr.attr);
  160. if (ret) {
  161. dev_err(&pdev->dev, "Failed to create sysfs entry\n");
  162. kobject_put(cpufreqhw_kobj);
  163. }
  164. return ret;
  165. }
  166. static int qcom_cpufreq_hw_debug_probe(struct platform_device *pdev)
  167. {
  168. return enable_cpufreq_hw_debug(pdev);
  169. }
  170. static int qcom_cpufreq_hw_debug_remove(struct platform_device *pdev)
  171. {
  172. sysfs_remove_file(kernel_kobj, &cpufreq_hwregs_attr.attr);
  173. kobject_put(cpufreqhw_kobj);
  174. return 0;
  175. }
  176. static const struct of_device_id qcom_cpufreq_hw_debug_match[] = {
  177. { .compatible = "qcom,cpufreq-hw-debug",
  178. .data = &cpufreq_qcom_std_data },
  179. { .compatible = "qcom,cpufreq-hw-epss-debug",
  180. .data = &cpufreq_qcom_std_epss_data },
  181. {}
  182. };
  183. static struct platform_driver qcom_cpufreq_hw_debug = {
  184. .probe = qcom_cpufreq_hw_debug_probe,
  185. .remove = qcom_cpufreq_hw_debug_remove,
  186. .driver = {
  187. .name = "qcom-cpufreq-hw-debug",
  188. .of_match_table = qcom_cpufreq_hw_debug_match,
  189. },
  190. };
  191. static int __init qcom_cpufreq_hw_debug_init(void)
  192. {
  193. return platform_driver_register(&qcom_cpufreq_hw_debug);
  194. }
  195. fs_initcall(qcom_cpufreq_hw_debug_init);
  196. static void __exit qcom_cpufreq_hw_debug_exit(void)
  197. {
  198. return platform_driver_unregister(&qcom_cpufreq_hw_debug);
  199. }
  200. module_exit(qcom_cpufreq_hw_debug_exit);
  201. MODULE_DESCRIPTION("QTI clock driver for CPUFREQ HW debug");
  202. MODULE_LICENSE("GPL");