therm_throt.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * linux/arch/i386/kerne/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 <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. 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, &thermal_throttle_attr_group);
  100. }
  101. #ifdef CONFIG_HOTPLUG_CPU
  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. mutex_lock(&therm_cpu_lock);
  118. switch (action) {
  119. case CPU_ONLINE:
  120. err = thermal_throttle_add_dev(sys_dev);
  121. WARN_ON(err);
  122. break;
  123. case CPU_DEAD:
  124. thermal_throttle_remove_dev(sys_dev);
  125. break;
  126. }
  127. mutex_unlock(&therm_cpu_lock);
  128. return NOTIFY_OK;
  129. }
  130. static struct notifier_block thermal_throttle_cpu_notifier =
  131. {
  132. .notifier_call = thermal_throttle_cpu_callback,
  133. };
  134. #endif /* CONFIG_HOTPLUG_CPU */
  135. static __init int thermal_throttle_init_device(void)
  136. {
  137. unsigned int cpu = 0;
  138. int err;
  139. if (!atomic_read(&therm_throt_en))
  140. return 0;
  141. register_hotcpu_notifier(&thermal_throttle_cpu_notifier);
  142. #ifdef CONFIG_HOTPLUG_CPU
  143. mutex_lock(&therm_cpu_lock);
  144. #endif
  145. /* connect live CPUs to sysfs */
  146. for_each_online_cpu(cpu) {
  147. err = thermal_throttle_add_dev(get_cpu_sysdev(cpu));
  148. WARN_ON(err);
  149. }
  150. #ifdef CONFIG_HOTPLUG_CPU
  151. mutex_unlock(&therm_cpu_lock);
  152. #endif
  153. return 0;
  154. }
  155. device_initcall(thermal_throttle_init_device);
  156. #endif /* CONFIG_SYSFS */