therm_throt.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * linux/arch/i386/kernel/cpu/mcheck/therm_throt.c
  3. *
  4. * Thermal throttle event support code (such as syslog messaging and rate
  5. * limiting) that was factored out from x86_64 (mce_intel.c) and i386 (p4.c).
  6. * This allows consistent reporting of CPU thermal throttle events.
  7. *
  8. * Maintains a counter in /sys that keeps track of the number of thermal
  9. * events, such that the user knows how bad the thermal problem might be
  10. * (since the logging to syslog and mcelog is rate limited).
  11. *
  12. * Author: Dmitriy Zavin (dmitriyz@google.com)
  13. *
  14. * Credits: Adapted from Zwane Mwaikambo's original code in mce_intel.c.
  15. * Inspired by Ross Biro's and Al Borchers' counter code.
  16. */
  17. #include <linux/percpu.h>
  18. #include <linux/sysdev.h>
  19. #include <linux/cpu.h>
  20. #include <asm/cpu.h>
  21. #include <linux/notifier.h>
  22. #include <linux/jiffies.h>
  23. #include <asm/therm_throt.h>
  24. /* How long to wait between reporting thermal events */
  25. #define CHECK_INTERVAL (300 * HZ)
  26. static DEFINE_PER_CPU(__u64, next_check) = INITIAL_JIFFIES;
  27. static DEFINE_PER_CPU(unsigned long, thermal_throttle_count);
  28. atomic_t therm_throt_en = ATOMIC_INIT(0);
  29. #ifdef CONFIG_SYSFS
  30. #define define_therm_throt_sysdev_one_ro(_name) \
  31. static SYSDEV_ATTR(_name, 0444, therm_throt_sysdev_show_##_name, NULL)
  32. #define define_therm_throt_sysdev_show_func(name) \
  33. static ssize_t therm_throt_sysdev_show_##name(struct sys_device *dev, \
  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. return 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;
  116. sys_dev = get_cpu_sysdev(cpu);
  117. switch (action) {
  118. case CPU_ONLINE:
  119. case CPU_ONLINE_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_DEAD:
  126. case CPU_DEAD_FROZEN:
  127. mutex_lock(&therm_cpu_lock);
  128. thermal_throttle_remove_dev(sys_dev);
  129. mutex_unlock(&therm_cpu_lock);
  130. break;
  131. }
  132. return NOTIFY_OK;
  133. }
  134. static struct notifier_block thermal_throttle_cpu_notifier =
  135. {
  136. .notifier_call = thermal_throttle_cpu_callback,
  137. };
  138. static __init int thermal_throttle_init_device(void)
  139. {
  140. unsigned int cpu = 0;
  141. int err;
  142. if (!atomic_read(&therm_throt_en))
  143. return 0;
  144. register_hotcpu_notifier(&thermal_throttle_cpu_notifier);
  145. #ifdef CONFIG_HOTPLUG_CPU
  146. mutex_lock(&therm_cpu_lock);
  147. #endif
  148. /* connect live CPUs to sysfs */
  149. for_each_online_cpu(cpu) {
  150. err = thermal_throttle_add_dev(get_cpu_sysdev(cpu));
  151. WARN_ON(err);
  152. }
  153. #ifdef CONFIG_HOTPLUG_CPU
  154. mutex_unlock(&therm_cpu_lock);
  155. #endif
  156. return 0;
  157. }
  158. device_initcall(thermal_throttle_init_device);
  159. #endif /* CONFIG_SYSFS */