2 * Copyright 2012 The Nouveau community
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
22 * Authors: Martin Peres
27 nvkm_therm_temp_set_defaults(struct nvkm_therm
*therm
)
29 therm
->bios_sensor
.offset_constant
= 0;
31 therm
->bios_sensor
.thrs_fan_boost
.temp
= 90;
32 therm
->bios_sensor
.thrs_fan_boost
.hysteresis
= 3;
34 therm
->bios_sensor
.thrs_down_clock
.temp
= 95;
35 therm
->bios_sensor
.thrs_down_clock
.hysteresis
= 3;
37 therm
->bios_sensor
.thrs_critical
.temp
= 105;
38 therm
->bios_sensor
.thrs_critical
.hysteresis
= 5;
40 therm
->bios_sensor
.thrs_shutdown
.temp
= 135;
41 therm
->bios_sensor
.thrs_shutdown
.hysteresis
= 5; /*not that it matters */
45 nvkm_therm_temp_safety_checks(struct nvkm_therm
*therm
)
47 struct nvbios_therm_sensor
*s
= &therm
->bios_sensor
;
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);
56 /* must be called with alarm_program_lock taken ! */
58 nvkm_therm_sensor_set_threshold_state(struct nvkm_therm
*therm
,
59 enum nvkm_therm_thrs thrs
,
60 enum nvkm_therm_thrs_state st
)
62 therm
->sensor
.alarm_state
[thrs
] = st
;
65 /* must be called with alarm_program_lock taken ! */
66 enum nvkm_therm_thrs_state
67 nvkm_therm_sensor_get_threshold_state(struct nvkm_therm
*therm
,
68 enum nvkm_therm_thrs thrs
)
70 return therm
->sensor
.alarm_state
[thrs
];
74 nv_poweroff_work(struct work_struct
*work
)
76 orderly_poweroff(true);
81 nvkm_therm_sensor_event(struct nvkm_therm
*therm
, enum nvkm_therm_thrs thrs
,
82 enum nvkm_therm_thrs_direction dir
)
84 struct nvkm_subdev
*subdev
= &therm
->subdev
;
86 static const char * const thresholds
[] = {
87 "fanboost", "downclock", "critical", "shutdown"
89 int temperature
= therm
->func
->temp_get(therm
);
91 if (thrs
< 0 || thrs
> 3)
94 if (dir
== NVKM_THERM_THRS_FALLING
)
96 "temperature (%i C) went below the '%s' threshold\n",
97 temperature
, thresholds
[thrs
]);
99 nvkm_info(subdev
, "temperature (%i C) hit the '%s' threshold\n",
100 temperature
, thresholds
[thrs
]);
102 active
= (dir
== NVKM_THERM_THRS_RISING
);
104 case NVKM_THERM_THRS_FANBOOST
:
106 nvkm_therm_fan_set(therm
, true, 100);
107 nvkm_therm_fan_mode(therm
, NVKM_THERM_CTRL_AUTO
);
110 case NVKM_THERM_THRS_DOWNCLOCK
:
111 if (therm
->emergency
.downclock
)
112 therm
->emergency
.downclock(therm
, active
);
114 case NVKM_THERM_THRS_CRITICAL
:
115 if (therm
->emergency
.pause
)
116 therm
->emergency
.pause(therm
, active
);
118 case NVKM_THERM_THRS_SHUTDOWN
:
120 struct work_struct
*work
;
122 work
= kmalloc(sizeof(*work
), GFP_ATOMIC
);
124 INIT_WORK(work
, nv_poweroff_work
);
129 case NVKM_THERM_THRS_NR
:
135 /* must be called with alarm_program_lock taken ! */
137 nvkm_therm_threshold_hyst_polling(struct nvkm_therm
*therm
,
138 const struct nvbios_therm_threshold
*thrs
,
139 enum nvkm_therm_thrs thrs_name
)
141 enum nvkm_therm_thrs_direction direction
;
142 enum nvkm_therm_thrs_state prev_state
, new_state
;
143 int temp
= therm
->func
->temp_get(therm
);
145 prev_state
= nvkm_therm_sensor_get_threshold_state(therm
, thrs_name
);
147 if (temp
>= thrs
->temp
&& prev_state
== NVKM_THERM_THRS_LOWER
) {
148 direction
= NVKM_THERM_THRS_RISING
;
149 new_state
= NVKM_THERM_THRS_HIGHER
;
150 } else if (temp
<= thrs
->temp
- thrs
->hysteresis
&&
151 prev_state
== NVKM_THERM_THRS_HIGHER
) {
152 direction
= NVKM_THERM_THRS_FALLING
;
153 new_state
= NVKM_THERM_THRS_LOWER
;
155 return; /* nothing to do */
157 nvkm_therm_sensor_set_threshold_state(therm
, thrs_name
, new_state
);
158 nvkm_therm_sensor_event(therm
, thrs_name
, direction
);
162 alarm_timer_callback(struct nvkm_alarm
*alarm
)
164 struct nvkm_therm
*therm
=
165 container_of(alarm
, struct nvkm_therm
, sensor
.therm_poll_alarm
);
166 struct nvbios_therm_sensor
*sensor
= &therm
->bios_sensor
;
167 struct nvkm_timer
*tmr
= therm
->subdev
.device
->timer
;
170 spin_lock_irqsave(&therm
->sensor
.alarm_program_lock
, flags
);
172 nvkm_therm_threshold_hyst_polling(therm
, &sensor
->thrs_fan_boost
,
173 NVKM_THERM_THRS_FANBOOST
);
175 nvkm_therm_threshold_hyst_polling(therm
,
176 &sensor
->thrs_down_clock
,
177 NVKM_THERM_THRS_DOWNCLOCK
);
179 nvkm_therm_threshold_hyst_polling(therm
, &sensor
->thrs_critical
,
180 NVKM_THERM_THRS_CRITICAL
);
182 nvkm_therm_threshold_hyst_polling(therm
, &sensor
->thrs_shutdown
,
183 NVKM_THERM_THRS_SHUTDOWN
);
185 spin_unlock_irqrestore(&therm
->sensor
.alarm_program_lock
, flags
);
187 /* schedule the next poll in one second */
188 if (therm
->func
->temp_get(therm
) >= 0)
189 nvkm_timer_alarm(tmr
, 1000000000ULL, alarm
);
193 nvkm_therm_program_alarms_polling(struct nvkm_therm
*therm
)
195 struct nvbios_therm_sensor
*sensor
= &therm
->bios_sensor
;
197 nvkm_debug(&therm
->subdev
,
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
);
208 alarm_timer_callback(&therm
->sensor
.therm_poll_alarm
);
212 nvkm_therm_sensor_init(struct nvkm_therm
*therm
)
214 therm
->func
->program_alarms(therm
);
219 nvkm_therm_sensor_fini(struct nvkm_therm
*therm
, bool suspend
)
221 struct nvkm_timer
*tmr
= therm
->subdev
.device
->timer
;
223 nvkm_timer_alarm(tmr
, 0, &therm
->sensor
.therm_poll_alarm
);
228 nvkm_therm_sensor_preinit(struct nvkm_therm
*therm
)
230 const char *sensor_avail
= "yes";
232 if (therm
->func
->temp_get(therm
) < 0)
235 nvkm_debug(&therm
->subdev
, "internal sensor: %s\n", sensor_avail
);
239 nvkm_therm_sensor_ctor(struct nvkm_therm
*therm
)
241 struct nvkm_subdev
*subdev
= &therm
->subdev
;
242 struct nvkm_bios
*bios
= subdev
->device
->bios
;
244 nvkm_alarm_init(&therm
->sensor
.therm_poll_alarm
, alarm_timer_callback
);
246 nvkm_therm_temp_set_defaults(therm
);
247 if (nvbios_therm_sensor_parse(bios
, NVBIOS_THERM_DOMAIN_CORE
,
248 &therm
->bios_sensor
))
249 nvkm_error(subdev
, "nvbios_therm_sensor_parse failed\n");
250 nvkm_therm_temp_safety_checks(therm
);