therm_throt.c 5.2 KB

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