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