therm_throt.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. *
  3. * Thermal throttle event support code (such as syslog messaging and rate
  4. * limiting) that was factored out from x86_64 (mce_intel.c) and i386 (p4.c).
  5. * This allows consistent reporting of CPU thermal throttle events.
  6. *
  7. * Maintains a counter in /sys that keeps track of the number of thermal
  8. * events, such that the user knows how bad the thermal problem might be
  9. * (since the logging to syslog and mcelog is rate limited).
  10. *
  11. * Author: Dmitriy Zavin (dmitriyz@google.com)
  12. *
  13. * Credits: Adapted from Zwane Mwaikambo's original code in mce_intel.c.
  14. * Inspired by Ross Biro's and Al Borchers' counter code.
  15. */
  16. #include <linux/percpu.h>
  17. #include <linux/sysdev.h>
  18. #include <linux/cpu.h>
  19. #include <asm/cpu.h>
  20. #include <linux/notifier.h>
  21. #include <linux/jiffies.h>
  22. #include <asm/therm_throt.h>
  23. /* How long to wait between reporting thermal events */
  24. #define CHECK_INTERVAL (300 * HZ)
  25. static DEFINE_PER_CPU(__u64, next_check) = INITIAL_JIFFIES;
  26. static DEFINE_PER_CPU(unsigned long, thermal_throttle_count);
  27. atomic_t therm_throt_en = ATOMIC_INIT(0);
  28. #ifdef CONFIG_SYSFS
  29. #define define_therm_throt_sysdev_one_ro(_name) \
  30. static SYSDEV_ATTR(_name, 0444, therm_throt_sysdev_show_##_name, NULL)
  31. #define define_therm_throt_sysdev_show_func(name) \
  32. static ssize_t therm_throt_sysdev_show_##name(struct sys_device *dev, \
  33. struct sysdev_attribute *attr, \
  34. char *buf) \
  35. { \
  36. unsigned int cpu = dev->id; \
  37. ssize_t ret; \
  38. \
  39. preempt_disable(); /* CPU hotplug */ \
  40. if (cpu_online(cpu)) \
  41. ret = sprintf(buf, "%lu\n", \
  42. per_cpu(thermal_throttle_##name, cpu)); \
  43. else \
  44. ret = 0; \
  45. preempt_enable(); \
  46. \
  47. return ret; \
  48. }
  49. define_therm_throt_sysdev_show_func(count);
  50. define_therm_throt_sysdev_one_ro(count);
  51. static struct attribute *thermal_throttle_attrs[] = {
  52. &attr_count.attr,
  53. NULL
  54. };
  55. static struct attribute_group thermal_throttle_attr_group = {
  56. .attrs = thermal_throttle_attrs,
  57. .name = "thermal_throttle"
  58. };
  59. #endif /* CONFIG_SYSFS */
  60. /***
  61. * therm_throt_process - Process thermal throttling event from interrupt
  62. * @curr: Whether the condition is current or not (boolean), since the
  63. * thermal interrupt normally gets called both when the thermal
  64. * event begins and once the event has ended.
  65. *
  66. * This function is called by the thermal interrupt after the
  67. * IRQ has been acknowledged.
  68. *
  69. * It will take care of rate limiting and printing messages to the syslog.
  70. *
  71. * Returns: 0 : Event should NOT be further logged, i.e. still in
  72. * "timeout" from previous log message.
  73. * 1 : Event should be logged further, and a message has been
  74. * printed to the syslog.
  75. */
  76. int therm_throt_process(int curr)
  77. {
  78. unsigned int cpu = smp_processor_id();
  79. __u64 tmp_jiffs = get_jiffies_64();
  80. if (curr)
  81. __get_cpu_var(thermal_throttle_count)++;
  82. if (time_before64(tmp_jiffs, __get_cpu_var(next_check)))
  83. return 0;
  84. __get_cpu_var(next_check) = tmp_jiffs + CHECK_INTERVAL;
  85. /* if we just entered the thermal event */
  86. if (curr) {
  87. printk(KERN_CRIT "CPU%d: Temperature above threshold, "
  88. "cpu clock throttled (total events = %lu)\n", cpu,
  89. __get_cpu_var(thermal_throttle_count));
  90. add_taint(TAINT_MACHINE_CHECK);
  91. } else {
  92. printk(KERN_CRIT "CPU%d: Temperature/speed normal\n", cpu);
  93. }
  94. return 1;
  95. }
  96. #ifdef CONFIG_SYSFS
  97. /* Add/Remove thermal_throttle interface for CPU device */
  98. static __cpuinit int thermal_throttle_add_dev(struct sys_device *sys_dev)
  99. {
  100. return sysfs_create_group(&sys_dev->kobj, &thermal_throttle_attr_group);
  101. }
  102. static __cpuinit void thermal_throttle_remove_dev(struct sys_device *sys_dev)
  103. {
  104. sysfs_remove_group(&sys_dev->kobj, &thermal_throttle_attr_group);
  105. }
  106. /* Mutex protecting device creation against CPU hotplug */
  107. static DEFINE_MUTEX(therm_cpu_lock);
  108. /* Get notified when a cpu comes on/off. Be hotplug friendly. */
  109. static __cpuinit int thermal_throttle_cpu_callback(struct notifier_block *nfb,
  110. unsigned long action,
  111. void *hcpu)
  112. {
  113. unsigned int cpu = (unsigned long)hcpu;
  114. struct sys_device *sys_dev;
  115. int err = 0;
  116. sys_dev = get_cpu_sysdev(cpu);
  117. switch (action) {
  118. case CPU_UP_PREPARE:
  119. case CPU_UP_PREPARE_FROZEN:
  120. mutex_lock(&therm_cpu_lock);
  121. err = thermal_throttle_add_dev(sys_dev);
  122. mutex_unlock(&therm_cpu_lock);
  123. WARN_ON(err);
  124. break;
  125. case CPU_UP_CANCELED:
  126. case CPU_UP_CANCELED_FROZEN:
  127. case CPU_DEAD:
  128. case CPU_DEAD_FROZEN:
  129. mutex_lock(&therm_cpu_lock);
  130. thermal_throttle_remove_dev(sys_dev);
  131. mutex_unlock(&therm_cpu_lock);
  132. break;
  133. }
  134. return err ? NOTIFY_BAD : NOTIFY_OK;
  135. }
  136. static struct notifier_block thermal_throttle_cpu_notifier __cpuinitdata =
  137. {
  138. .notifier_call = thermal_throttle_cpu_callback,
  139. };
  140. static __init int thermal_throttle_init_device(void)
  141. {
  142. unsigned int cpu = 0;
  143. int err;
  144. if (!atomic_read(&therm_throt_en))
  145. return 0;
  146. register_hotcpu_notifier(&thermal_throttle_cpu_notifier);
  147. #ifdef CONFIG_HOTPLUG_CPU
  148. mutex_lock(&therm_cpu_lock);
  149. #endif
  150. /* connect live CPUs to sysfs */
  151. for_each_online_cpu(cpu) {
  152. err = thermal_throttle_add_dev(get_cpu_sysdev(cpu));
  153. WARN_ON(err);
  154. }
  155. #ifdef CONFIG_HOTPLUG_CPU
  156. mutex_unlock(&therm_cpu_lock);
  157. #endif
  158. return 0;
  159. }
  160. device_initcall(thermal_throttle_init_device);
  161. #endif /* CONFIG_SYSFS */