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
*obj
)
29 struct nvkm_therm_priv
*therm
= container_of(obj
, typeof(*therm
), base
);
31 therm
->bios_sensor
.offset_constant
= 0;
33 therm
->bios_sensor
.thrs_fan_boost
.temp
= 90;
34 therm
->bios_sensor
.thrs_fan_boost
.hysteresis
= 3;
36 therm
->bios_sensor
.thrs_down_clock
.temp
= 95;
37 therm
->bios_sensor
.thrs_down_clock
.hysteresis
= 3;
39 therm
->bios_sensor
.thrs_critical
.temp
= 105;
40 therm
->bios_sensor
.thrs_critical
.hysteresis
= 5;
42 therm
->bios_sensor
.thrs_shutdown
.temp
= 135;
43 therm
->bios_sensor
.thrs_shutdown
.hysteresis
= 5; /*not that it matters */
48 nvkm_therm_temp_safety_checks(struct nvkm_therm
*obj
)
50 struct nvkm_therm_priv
*therm
= container_of(obj
, typeof(*therm
), base
);
51 struct nvbios_therm_sensor
*s
= &therm
->bios_sensor
;
53 /* enforce a minimum hysteresis on thresholds */
54 s
->thrs_fan_boost
.hysteresis
= max_t(u8
, s
->thrs_fan_boost
.hysteresis
, 2);
55 s
->thrs_down_clock
.hysteresis
= max_t(u8
, s
->thrs_down_clock
.hysteresis
, 2);
56 s
->thrs_critical
.hysteresis
= max_t(u8
, s
->thrs_critical
.hysteresis
, 2);
57 s
->thrs_shutdown
.hysteresis
= max_t(u8
, s
->thrs_shutdown
.hysteresis
, 2);
60 /* must be called with alarm_program_lock taken ! */
62 nvkm_therm_sensor_set_threshold_state(struct nvkm_therm
*obj
,
63 enum nvkm_therm_thrs thrs
,
64 enum nvkm_therm_thrs_state st
)
66 struct nvkm_therm_priv
*therm
= container_of(obj
, typeof(*therm
), base
);
67 therm
->sensor
.alarm_state
[thrs
] = st
;
70 /* must be called with alarm_program_lock taken ! */
71 enum nvkm_therm_thrs_state
72 nvkm_therm_sensor_get_threshold_state(struct nvkm_therm
*obj
,
73 enum nvkm_therm_thrs thrs
)
75 struct nvkm_therm_priv
*therm
= container_of(obj
, typeof(*therm
), base
);
76 return therm
->sensor
.alarm_state
[thrs
];
80 nv_poweroff_work(struct work_struct
*work
)
82 orderly_poweroff(true);
87 nvkm_therm_sensor_event(struct nvkm_therm
*obj
, enum nvkm_therm_thrs thrs
,
88 enum nvkm_therm_thrs_direction dir
)
90 struct nvkm_therm_priv
*therm
= container_of(obj
, typeof(*therm
), base
);
91 struct nvkm_subdev
*subdev
= &therm
->base
.subdev
;
93 const char *thresolds
[] = {
94 "fanboost", "downclock", "critical", "shutdown"
96 int temperature
= therm
->base
.temp_get(&therm
->base
);
98 if (thrs
< 0 || thrs
> 3)
101 if (dir
== NVKM_THERM_THRS_FALLING
)
103 "temperature (%i C) went below the '%s' threshold\n",
104 temperature
, thresolds
[thrs
]);
106 nvkm_info(subdev
, "temperature (%i C) hit the '%s' threshold\n",
107 temperature
, thresolds
[thrs
]);
109 active
= (dir
== NVKM_THERM_THRS_RISING
);
111 case NVKM_THERM_THRS_FANBOOST
:
113 nvkm_therm_fan_set(&therm
->base
, true, 100);
114 nvkm_therm_fan_mode(&therm
->base
, NVKM_THERM_CTRL_AUTO
);
117 case NVKM_THERM_THRS_DOWNCLOCK
:
118 if (therm
->emergency
.downclock
)
119 therm
->emergency
.downclock(&therm
->base
, active
);
121 case NVKM_THERM_THRS_CRITICAL
:
122 if (therm
->emergency
.pause
)
123 therm
->emergency
.pause(&therm
->base
, active
);
125 case NVKM_THERM_THRS_SHUTDOWN
:
127 struct work_struct
*work
;
129 work
= kmalloc(sizeof(*work
), GFP_ATOMIC
);
131 INIT_WORK(work
, nv_poweroff_work
);
136 case NVKM_THERM_THRS_NR
:
142 /* must be called with alarm_program_lock taken ! */
144 nvkm_therm_threshold_hyst_polling(struct nvkm_therm
*therm
,
145 const struct nvbios_therm_threshold
*thrs
,
146 enum nvkm_therm_thrs thrs_name
)
148 enum nvkm_therm_thrs_direction direction
;
149 enum nvkm_therm_thrs_state prev_state
, new_state
;
150 int temp
= therm
->temp_get(therm
);
152 prev_state
= nvkm_therm_sensor_get_threshold_state(therm
, thrs_name
);
154 if (temp
>= thrs
->temp
&& prev_state
== NVKM_THERM_THRS_LOWER
) {
155 direction
= NVKM_THERM_THRS_RISING
;
156 new_state
= NVKM_THERM_THRS_HIGHER
;
157 } else if (temp
<= thrs
->temp
- thrs
->hysteresis
&&
158 prev_state
== NVKM_THERM_THRS_HIGHER
) {
159 direction
= NVKM_THERM_THRS_FALLING
;
160 new_state
= NVKM_THERM_THRS_LOWER
;
162 return; /* nothing to do */
164 nvkm_therm_sensor_set_threshold_state(therm
, thrs_name
, new_state
);
165 nvkm_therm_sensor_event(therm
, thrs_name
, direction
);
169 alarm_timer_callback(struct nvkm_alarm
*alarm
)
171 struct nvkm_therm_priv
*therm
=
172 container_of(alarm
, struct nvkm_therm_priv
, sensor
.therm_poll_alarm
);
173 struct nvbios_therm_sensor
*sensor
= &therm
->bios_sensor
;
174 struct nvkm_timer
*tmr
= nvkm_timer(therm
);
177 spin_lock_irqsave(&therm
->sensor
.alarm_program_lock
, flags
);
179 nvkm_therm_threshold_hyst_polling(&therm
->base
, &sensor
->thrs_fan_boost
,
180 NVKM_THERM_THRS_FANBOOST
);
182 nvkm_therm_threshold_hyst_polling(&therm
->base
,
183 &sensor
->thrs_down_clock
,
184 NVKM_THERM_THRS_DOWNCLOCK
);
186 nvkm_therm_threshold_hyst_polling(&therm
->base
, &sensor
->thrs_critical
,
187 NVKM_THERM_THRS_CRITICAL
);
189 nvkm_therm_threshold_hyst_polling(&therm
->base
, &sensor
->thrs_shutdown
,
190 NVKM_THERM_THRS_SHUTDOWN
);
192 spin_unlock_irqrestore(&therm
->sensor
.alarm_program_lock
, flags
);
194 /* schedule the next poll in one second */
195 if (therm
->base
.temp_get(&therm
->base
) >= 0 && list_empty(&alarm
->head
))
196 tmr
->alarm(tmr
, 1000000000ULL, alarm
);
200 nvkm_therm_program_alarms_polling(struct nvkm_therm
*obj
)
202 struct nvkm_therm_priv
*therm
= container_of(obj
, typeof(*therm
), base
);
203 struct nvbios_therm_sensor
*sensor
= &therm
->bios_sensor
;
205 nvkm_debug(&therm
->base
.subdev
,
206 "programmed thresholds [ %d(%d), %d(%d), %d(%d), %d(%d) ]\n",
207 sensor
->thrs_fan_boost
.temp
,
208 sensor
->thrs_fan_boost
.hysteresis
,
209 sensor
->thrs_down_clock
.temp
,
210 sensor
->thrs_down_clock
.hysteresis
,
211 sensor
->thrs_critical
.temp
,
212 sensor
->thrs_critical
.hysteresis
,
213 sensor
->thrs_shutdown
.temp
,
214 sensor
->thrs_shutdown
.hysteresis
);
216 alarm_timer_callback(&therm
->sensor
.therm_poll_alarm
);
220 nvkm_therm_sensor_init(struct nvkm_therm
*obj
)
222 struct nvkm_therm_priv
*therm
= container_of(obj
, typeof(*therm
), base
);
223 therm
->sensor
.program_alarms(&therm
->base
);
228 nvkm_therm_sensor_fini(struct nvkm_therm
*obj
, bool suspend
)
230 struct nvkm_therm_priv
*therm
= container_of(obj
, typeof(*therm
), base
);
231 struct nvkm_timer
*tmr
= nvkm_timer(therm
);
234 tmr
->alarm_cancel(tmr
, &therm
->sensor
.therm_poll_alarm
);
239 nvkm_therm_sensor_preinit(struct nvkm_therm
*therm
)
241 const char *sensor_avail
= "yes";
243 if (therm
->temp_get(therm
) < 0)
246 nvkm_debug(&therm
->subdev
, "internal sensor: %s\n", sensor_avail
);
250 nvkm_therm_sensor_ctor(struct nvkm_therm
*obj
)
252 struct nvkm_therm_priv
*therm
= container_of(obj
, typeof(*therm
), base
);
253 struct nvkm_subdev
*subdev
= &therm
->base
.subdev
;
254 struct nvkm_bios
*bios
= subdev
->device
->bios
;
256 nvkm_alarm_init(&therm
->sensor
.therm_poll_alarm
, alarm_timer_callback
);
258 nvkm_therm_temp_set_defaults(&therm
->base
);
259 if (nvbios_therm_sensor_parse(bios
, NVBIOS_THERM_DOMAIN_CORE
,
260 &therm
->bios_sensor
))
261 nvkm_error(subdev
, "nvbios_therm_sensor_parse failed\n");
262 nvkm_therm_temp_safety_checks(&therm
->base
);