Merge branch 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs
[linux-2.6-block.git] / drivers / gpu / drm / nouveau / nvkm / subdev / therm / temp.c
CommitLineData
aa1b9b48
MP
1/*
2 * Copyright 2012 The Nouveau community
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Authors: Martin Peres
23 */
aa1b9b48
MP
24#include "priv.h"
25
aa1b9b48 26static void
57113c01 27nvkm_therm_temp_set_defaults(struct nvkm_therm *therm)
aa1b9b48 28{
da06b46b 29 therm->bios_sensor.offset_constant = 0;
aa1b9b48 30
da06b46b
BS
31 therm->bios_sensor.thrs_fan_boost.temp = 90;
32 therm->bios_sensor.thrs_fan_boost.hysteresis = 3;
aa1b9b48 33
da06b46b
BS
34 therm->bios_sensor.thrs_down_clock.temp = 95;
35 therm->bios_sensor.thrs_down_clock.hysteresis = 3;
aa1b9b48 36
da06b46b
BS
37 therm->bios_sensor.thrs_critical.temp = 105;
38 therm->bios_sensor.thrs_critical.hysteresis = 5;
aa1b9b48 39
da06b46b
BS
40 therm->bios_sensor.thrs_shutdown.temp = 135;
41 therm->bios_sensor.thrs_shutdown.hysteresis = 5; /*not that it matters */
aa1b9b48
MP
42}
43
aa1b9b48 44static void
57113c01 45nvkm_therm_temp_safety_checks(struct nvkm_therm *therm)
aa1b9b48 46{
da06b46b 47 struct nvbios_therm_sensor *s = &therm->bios_sensor;
aa1b9b48 48
3600ad5e
MP
49 /* enforce a minimum hysteresis on thresholds */
50 s->thrs_fan_boost.hysteresis = max_t(u8, s->thrs_fan_boost.hysteresis, 2);
51 s->thrs_down_clock.hysteresis = max_t(u8, s->thrs_down_clock.hysteresis, 2);
52 s->thrs_critical.hysteresis = max_t(u8, s->thrs_critical.hysteresis, 2);
53 s->thrs_shutdown.hysteresis = max_t(u8, s->thrs_shutdown.hysteresis, 2);
aa1b9b48
MP
54}
55
0083b91d 56/* must be called with alarm_program_lock taken ! */
e1404611 57void
57113c01 58nvkm_therm_sensor_set_threshold_state(struct nvkm_therm *therm,
e1404611
BS
59 enum nvkm_therm_thrs thrs,
60 enum nvkm_therm_thrs_state st)
0083b91d 61{
da06b46b 62 therm->sensor.alarm_state[thrs] = st;
0083b91d
MP
63}
64
65/* must be called with alarm_program_lock taken ! */
e1404611 66enum nvkm_therm_thrs_state
57113c01 67nvkm_therm_sensor_get_threshold_state(struct nvkm_therm *therm,
e1404611 68 enum nvkm_therm_thrs thrs)
0083b91d 69{
da06b46b 70 return therm->sensor.alarm_state[thrs];
0083b91d
MP
71}
72
07df3043
MS
73static void
74nv_poweroff_work(struct work_struct *work)
75{
76 orderly_poweroff(true);
77 kfree(work);
78}
79
e1404611 80void
57113c01 81nvkm_therm_sensor_event(struct nvkm_therm *therm, enum nvkm_therm_thrs thrs,
e1404611 82 enum nvkm_therm_thrs_direction dir)
0083b91d 83{
57113c01 84 struct nvkm_subdev *subdev = &therm->subdev;
0083b91d
MP
85 bool active;
86 const char *thresolds[] = {
87 "fanboost", "downclock", "critical", "shutdown"
88 };
57113c01 89 int temperature = therm->func->temp_get(therm);
0083b91d
MP
90
91 if (thrs < 0 || thrs > 3)
92 return;
93
e1404611 94 if (dir == NVKM_THERM_THRS_FALLING)
b3c418bb
BS
95 nvkm_info(subdev,
96 "temperature (%i C) went below the '%s' threshold\n",
97 temperature, thresolds[thrs]);
0083b91d 98 else
b3c418bb
BS
99 nvkm_info(subdev, "temperature (%i C) hit the '%s' threshold\n",
100 temperature, thresolds[thrs]);
0083b91d 101
e1404611 102 active = (dir == NVKM_THERM_THRS_RISING);
0083b91d 103 switch (thrs) {
e1404611 104 case NVKM_THERM_THRS_FANBOOST:
134fc032 105 if (active) {
57113c01
BS
106 nvkm_therm_fan_set(therm, true, 100);
107 nvkm_therm_fan_mode(therm, NVKM_THERM_CTRL_AUTO);
134fc032 108 }
0083b91d 109 break;
e1404611 110 case NVKM_THERM_THRS_DOWNCLOCK:
da06b46b 111 if (therm->emergency.downclock)
57113c01 112 therm->emergency.downclock(therm, active);
0083b91d 113 break;
e1404611 114 case NVKM_THERM_THRS_CRITICAL:
da06b46b 115 if (therm->emergency.pause)
57113c01 116 therm->emergency.pause(therm, active);
0083b91d 117 break;
e1404611 118 case NVKM_THERM_THRS_SHUTDOWN:
07df3043
MS
119 if (active) {
120 struct work_struct *work;
121
122 work = kmalloc(sizeof(*work), GFP_ATOMIC);
123 if (work) {
124 INIT_WORK(work, nv_poweroff_work);
125 schedule_work(work);
126 }
127 }
0083b91d 128 break;
e1404611 129 case NVKM_THERM_THRS_NR:
0083b91d
MP
130 break;
131 }
132
133}
134
135/* must be called with alarm_program_lock taken ! */
136static void
e1404611
BS
137nvkm_therm_threshold_hyst_polling(struct nvkm_therm *therm,
138 const struct nvbios_therm_threshold *thrs,
139 enum nvkm_therm_thrs thrs_name)
0083b91d 140{
e1404611
BS
141 enum nvkm_therm_thrs_direction direction;
142 enum nvkm_therm_thrs_state prev_state, new_state;
57113c01 143 int temp = therm->func->temp_get(therm);
0083b91d 144
e1404611 145 prev_state = nvkm_therm_sensor_get_threshold_state(therm, thrs_name);
0083b91d 146
e1404611
BS
147 if (temp >= thrs->temp && prev_state == NVKM_THERM_THRS_LOWER) {
148 direction = NVKM_THERM_THRS_RISING;
149 new_state = NVKM_THERM_THRS_HIGHER;
0083b91d 150 } else if (temp <= thrs->temp - thrs->hysteresis &&
e1404611
BS
151 prev_state == NVKM_THERM_THRS_HIGHER) {
152 direction = NVKM_THERM_THRS_FALLING;
153 new_state = NVKM_THERM_THRS_LOWER;
0083b91d
MP
154 } else
155 return; /* nothing to do */
156
e1404611
BS
157 nvkm_therm_sensor_set_threshold_state(therm, thrs_name, new_state);
158 nvkm_therm_sensor_event(therm, thrs_name, direction);
0083b91d
MP
159}
160
161static void
e1404611 162alarm_timer_callback(struct nvkm_alarm *alarm)
0083b91d 163{
57113c01
BS
164 struct nvkm_therm *therm =
165 container_of(alarm, struct nvkm_therm, sensor.therm_poll_alarm);
da06b46b 166 struct nvbios_therm_sensor *sensor = &therm->bios_sensor;
57113c01 167 struct nvkm_timer *tmr = therm->subdev.device->timer;
0083b91d
MP
168 unsigned long flags;
169
da06b46b 170 spin_lock_irqsave(&therm->sensor.alarm_program_lock, flags);
0083b91d 171
57113c01 172 nvkm_therm_threshold_hyst_polling(therm, &sensor->thrs_fan_boost,
e1404611 173 NVKM_THERM_THRS_FANBOOST);
0083b91d 174
57113c01 175 nvkm_therm_threshold_hyst_polling(therm,
da06b46b 176 &sensor->thrs_down_clock,
e1404611 177 NVKM_THERM_THRS_DOWNCLOCK);
0083b91d 178
57113c01 179 nvkm_therm_threshold_hyst_polling(therm, &sensor->thrs_critical,
e1404611 180 NVKM_THERM_THRS_CRITICAL);
0083b91d 181
57113c01 182 nvkm_therm_threshold_hyst_polling(therm, &sensor->thrs_shutdown,
e1404611 183 NVKM_THERM_THRS_SHUTDOWN);
0083b91d 184
da06b46b 185 spin_unlock_irqrestore(&therm->sensor.alarm_program_lock, flags);
bb78e7a1 186
0083b91d 187 /* schedule the next poll in one second */
57113c01 188 if (therm->func->temp_get(therm) >= 0 && list_empty(&alarm->head))
31649ecf 189 nvkm_timer_alarm(tmr, 1000000000ULL, alarm);
0083b91d
MP
190}
191
192void
57113c01 193nvkm_therm_program_alarms_polling(struct nvkm_therm *therm)
0083b91d 194{
da06b46b 195 struct nvbios_therm_sensor *sensor = &therm->bios_sensor;
0083b91d 196
57113c01 197 nvkm_debug(&therm->subdev,
b3c418bb
BS
198 "programmed thresholds [ %d(%d), %d(%d), %d(%d), %d(%d) ]\n",
199 sensor->thrs_fan_boost.temp,
200 sensor->thrs_fan_boost.hysteresis,
201 sensor->thrs_down_clock.temp,
202 sensor->thrs_down_clock.hysteresis,
203 sensor->thrs_critical.temp,
204 sensor->thrs_critical.hysteresis,
205 sensor->thrs_shutdown.temp,
206 sensor->thrs_shutdown.hysteresis);
0083b91d 207
da06b46b 208 alarm_timer_callback(&therm->sensor.therm_poll_alarm);
0083b91d
MP
209}
210
c4a62a76 211int
57113c01 212nvkm_therm_sensor_init(struct nvkm_therm *therm)
c4a62a76 213{
57113c01 214 therm->func->program_alarms(therm);
c4a62a76
MP
215 return 0;
216}
217
218int
57113c01 219nvkm_therm_sensor_fini(struct nvkm_therm *therm, bool suspend)
c4a62a76 220{
57113c01 221 struct nvkm_timer *tmr = therm->subdev.device->timer;
c4a62a76 222 if (suspend)
31649ecf 223 nvkm_timer_alarm_cancel(tmr, &therm->sensor.therm_poll_alarm);
c4a62a76
MP
224 return 0;
225}
226
0b3ee377 227void
e1404611 228nvkm_therm_sensor_preinit(struct nvkm_therm *therm)
0b3ee377
MP
229{
230 const char *sensor_avail = "yes";
231
57113c01 232 if (therm->func->temp_get(therm) < 0)
0b3ee377
MP
233 sensor_avail = "no";
234
b3c418bb 235 nvkm_debug(&therm->subdev, "internal sensor: %s\n", sensor_avail);
0b3ee377
MP
236}
237
aa1b9b48 238int
57113c01 239nvkm_therm_sensor_ctor(struct nvkm_therm *therm)
aa1b9b48 240{
57113c01 241 struct nvkm_subdev *subdev = &therm->subdev;
46484438 242 struct nvkm_bios *bios = subdev->device->bios;
aa1b9b48 243
da06b46b 244 nvkm_alarm_init(&therm->sensor.therm_poll_alarm, alarm_timer_callback);
0083b91d 245
57113c01 246 nvkm_therm_temp_set_defaults(therm);
aa1b9b48 247 if (nvbios_therm_sensor_parse(bios, NVBIOS_THERM_DOMAIN_CORE,
da06b46b 248 &therm->bios_sensor))
b3c418bb 249 nvkm_error(subdev, "nvbios_therm_sensor_parse failed\n");
57113c01 250 nvkm_therm_temp_safety_checks(therm);
aa1b9b48
MP
251
252 return 0;
253}