cpu_hwmon.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/err.h>
  3. #include <linux/module.h>
  4. #include <linux/reboot.h>
  5. #include <linux/jiffies.h>
  6. #include <linux/hwmon.h>
  7. #include <linux/hwmon-sysfs.h>
  8. #include <loongson.h>
  9. #include <boot_param.h>
  10. #include <loongson_hwmon.h>
  11. #include <loongson_regs.h>
  12. static int csr_temp_enable;
  13. /*
  14. * Loongson-3 series cpu has two sensors inside,
  15. * each of them from 0 to 255,
  16. * if more than 127, that is dangerous.
  17. * here only provide sensor1 data, because it always hot than sensor0
  18. */
  19. int loongson3_cpu_temp(int cpu)
  20. {
  21. u32 reg, prid_rev;
  22. if (csr_temp_enable) {
  23. reg = (csr_readl(LOONGSON_CSR_CPUTEMP) & 0xff);
  24. goto out;
  25. }
  26. reg = LOONGSON_CHIPTEMP(cpu);
  27. prid_rev = read_c0_prid() & PRID_REV_MASK;
  28. switch (prid_rev) {
  29. case PRID_REV_LOONGSON3A_R1:
  30. reg = (reg >> 8) & 0xff;
  31. break;
  32. case PRID_REV_LOONGSON3B_R1:
  33. case PRID_REV_LOONGSON3B_R2:
  34. case PRID_REV_LOONGSON3A_R2_0:
  35. case PRID_REV_LOONGSON3A_R2_1:
  36. reg = ((reg >> 8) & 0xff) - 100;
  37. break;
  38. case PRID_REV_LOONGSON3A_R3_0:
  39. case PRID_REV_LOONGSON3A_R3_1:
  40. default:
  41. reg = (reg & 0xffff) * 731 / 0x4000 - 273;
  42. break;
  43. }
  44. out:
  45. return (int)reg * 1000;
  46. }
  47. static int nr_packages;
  48. static struct device *cpu_hwmon_dev;
  49. static ssize_t cpu_temp_label(struct device *dev,
  50. struct device_attribute *attr, char *buf)
  51. {
  52. int id = (to_sensor_dev_attr(attr))->index - 1;
  53. return sprintf(buf, "CPU %d Temperature\n", id);
  54. }
  55. static ssize_t get_cpu_temp(struct device *dev,
  56. struct device_attribute *attr, char *buf)
  57. {
  58. int id = (to_sensor_dev_attr(attr))->index - 1;
  59. int value = loongson3_cpu_temp(id);
  60. return sprintf(buf, "%d\n", value);
  61. }
  62. static SENSOR_DEVICE_ATTR(temp1_input, 0444, get_cpu_temp, NULL, 1);
  63. static SENSOR_DEVICE_ATTR(temp1_label, 0444, cpu_temp_label, NULL, 1);
  64. static SENSOR_DEVICE_ATTR(temp2_input, 0444, get_cpu_temp, NULL, 2);
  65. static SENSOR_DEVICE_ATTR(temp2_label, 0444, cpu_temp_label, NULL, 2);
  66. static SENSOR_DEVICE_ATTR(temp3_input, 0444, get_cpu_temp, NULL, 3);
  67. static SENSOR_DEVICE_ATTR(temp3_label, 0444, cpu_temp_label, NULL, 3);
  68. static SENSOR_DEVICE_ATTR(temp4_input, 0444, get_cpu_temp, NULL, 4);
  69. static SENSOR_DEVICE_ATTR(temp4_label, 0444, cpu_temp_label, NULL, 4);
  70. static struct attribute *cpu_hwmon_attributes[] = {
  71. &sensor_dev_attr_temp1_input.dev_attr.attr,
  72. &sensor_dev_attr_temp1_label.dev_attr.attr,
  73. &sensor_dev_attr_temp2_input.dev_attr.attr,
  74. &sensor_dev_attr_temp2_label.dev_attr.attr,
  75. &sensor_dev_attr_temp3_input.dev_attr.attr,
  76. &sensor_dev_attr_temp3_label.dev_attr.attr,
  77. &sensor_dev_attr_temp4_input.dev_attr.attr,
  78. &sensor_dev_attr_temp4_label.dev_attr.attr,
  79. NULL
  80. };
  81. static umode_t cpu_hwmon_is_visible(struct kobject *kobj,
  82. struct attribute *attr, int i)
  83. {
  84. int id = i / 2;
  85. if (id < nr_packages)
  86. return attr->mode;
  87. return 0;
  88. }
  89. static struct attribute_group cpu_hwmon_group = {
  90. .attrs = cpu_hwmon_attributes,
  91. .is_visible = cpu_hwmon_is_visible,
  92. };
  93. static const struct attribute_group *cpu_hwmon_groups[] = {
  94. &cpu_hwmon_group,
  95. NULL
  96. };
  97. #define CPU_THERMAL_THRESHOLD 90000
  98. static struct delayed_work thermal_work;
  99. static void do_thermal_timer(struct work_struct *work)
  100. {
  101. int i, value;
  102. for (i = 0; i < nr_packages; i++) {
  103. value = loongson3_cpu_temp(i);
  104. if (value > CPU_THERMAL_THRESHOLD) {
  105. pr_emerg("Power off due to high temp: %d\n", value);
  106. orderly_poweroff(true);
  107. }
  108. }
  109. schedule_delayed_work(&thermal_work, msecs_to_jiffies(5000));
  110. }
  111. static int __init loongson_hwmon_init(void)
  112. {
  113. pr_info("Loongson Hwmon Enter...\n");
  114. if (cpu_has_csr())
  115. csr_temp_enable = csr_readl(LOONGSON_CSR_FEATURES) &
  116. LOONGSON_CSRF_TEMP;
  117. nr_packages = loongson_sysconf.nr_cpus /
  118. loongson_sysconf.cores_per_package;
  119. cpu_hwmon_dev = hwmon_device_register_with_groups(NULL, "cpu_hwmon",
  120. NULL, cpu_hwmon_groups);
  121. if (IS_ERR(cpu_hwmon_dev)) {
  122. pr_err("hwmon_device_register fail!\n");
  123. return PTR_ERR(cpu_hwmon_dev);
  124. }
  125. INIT_DEFERRABLE_WORK(&thermal_work, do_thermal_timer);
  126. schedule_delayed_work(&thermal_work, msecs_to_jiffies(20000));
  127. return 0;
  128. }
  129. static void __exit loongson_hwmon_exit(void)
  130. {
  131. cancel_delayed_work_sync(&thermal_work);
  132. hwmon_device_unregister(cpu_hwmon_dev);
  133. }
  134. module_init(loongson_hwmon_init);
  135. module_exit(loongson_hwmon_exit);
  136. MODULE_AUTHOR("Yu Xiang <[email protected]>");
  137. MODULE_AUTHOR("Huacai Chen <[email protected]>");
  138. MODULE_DESCRIPTION("Loongson CPU Hwmon driver");
  139. MODULE_LICENSE("GPL");