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 #include <core/object.h>
28 #include <core/device.h>
30 #include <subdev/bios.h>
33 nouveau_therm_temp_set_defaults(struct nouveau_therm
*therm
)
35 struct nouveau_therm_priv
*priv
= (void *)therm
;
37 priv
->bios_sensor
.slope_mult
= 1;
38 priv
->bios_sensor
.slope_div
= 1;
39 priv
->bios_sensor
.offset_num
= 0;
40 priv
->bios_sensor
.offset_den
= 1;
41 priv
->bios_sensor
.offset_constant
= 0;
43 priv
->bios_sensor
.thrs_fan_boost
.temp
= 90;
44 priv
->bios_sensor
.thrs_fan_boost
.hysteresis
= 3;
46 priv
->bios_sensor
.thrs_down_clock
.temp
= 95;
47 priv
->bios_sensor
.thrs_down_clock
.hysteresis
= 3;
49 priv
->bios_sensor
.thrs_critical
.temp
= 105;
50 priv
->bios_sensor
.thrs_critical
.hysteresis
= 5;
52 priv
->bios_sensor
.thrs_shutdown
.temp
= 135;
53 priv
->bios_sensor
.thrs_shutdown
.hysteresis
= 5; /*not that it matters */
58 nouveau_therm_temp_safety_checks(struct nouveau_therm
*therm
)
60 struct nouveau_therm_priv
*priv
= (void *)therm
;
61 struct nvbios_therm_sensor
*s
= &priv
->bios_sensor
;
63 if (!priv
->bios_sensor
.slope_div
)
64 priv
->bios_sensor
.slope_div
= 1;
65 if (!priv
->bios_sensor
.offset_den
)
66 priv
->bios_sensor
.offset_den
= 1;
68 /* enforce a minimum hysteresis on thresholds */
69 s
->thrs_fan_boost
.hysteresis
= max_t(u8
, s
->thrs_fan_boost
.hysteresis
, 2);
70 s
->thrs_down_clock
.hysteresis
= max_t(u8
, s
->thrs_down_clock
.hysteresis
, 2);
71 s
->thrs_critical
.hysteresis
= max_t(u8
, s
->thrs_critical
.hysteresis
, 2);
72 s
->thrs_shutdown
.hysteresis
= max_t(u8
, s
->thrs_shutdown
.hysteresis
, 2);
75 /* must be called with alarm_program_lock taken ! */
76 void nouveau_therm_sensor_set_threshold_state(struct nouveau_therm
*therm
,
77 enum nouveau_therm_thrs thrs
,
78 enum nouveau_therm_thrs_state st
)
80 struct nouveau_therm_priv
*priv
= (void *)therm
;
81 priv
->sensor
.alarm_state
[thrs
] = st
;
84 /* must be called with alarm_program_lock taken ! */
85 enum nouveau_therm_thrs_state
86 nouveau_therm_sensor_get_threshold_state(struct nouveau_therm
*therm
,
87 enum nouveau_therm_thrs thrs
)
89 struct nouveau_therm_priv
*priv
= (void *)therm
;
90 return priv
->sensor
.alarm_state
[thrs
];
94 nv_poweroff_work(struct work_struct
*work
)
96 orderly_poweroff(true);
100 void nouveau_therm_sensor_event(struct nouveau_therm
*therm
,
101 enum nouveau_therm_thrs thrs
,
102 enum nouveau_therm_thrs_direction dir
)
104 struct nouveau_therm_priv
*priv
= (void *)therm
;
106 const char *thresolds
[] = {
107 "fanboost", "downclock", "critical", "shutdown"
109 uint8_t temperature
= therm
->temp_get(therm
);
111 if (thrs
< 0 || thrs
> 3)
114 if (dir
== NOUVEAU_THERM_THRS_FALLING
)
115 nv_info(therm
, "temperature (%u C) went below the '%s' threshold\n",
116 temperature
, thresolds
[thrs
]);
118 nv_info(therm
, "temperature (%u C) hit the '%s' threshold\n",
119 temperature
, thresolds
[thrs
]);
121 active
= (dir
== NOUVEAU_THERM_THRS_RISING
);
123 case NOUVEAU_THERM_THRS_FANBOOST
:
125 nouveau_therm_fan_set(therm
, true, 100);
126 nouveau_therm_mode(therm
, NOUVEAU_THERM_CTRL_AUTO
);
129 case NOUVEAU_THERM_THRS_DOWNCLOCK
:
130 if (priv
->emergency
.downclock
)
131 priv
->emergency
.downclock(therm
, active
);
133 case NOUVEAU_THERM_THRS_CRITICAL
:
134 if (priv
->emergency
.pause
)
135 priv
->emergency
.pause(therm
, active
);
137 case NOUVEAU_THERM_THRS_SHUTDOWN
:
139 struct work_struct
*work
;
141 work
= kmalloc(sizeof(*work
), GFP_ATOMIC
);
143 INIT_WORK(work
, nv_poweroff_work
);
148 case NOUVEAU_THERM_THRS_NR
:
154 /* must be called with alarm_program_lock taken ! */
156 nouveau_therm_threshold_hyst_polling(struct nouveau_therm
*therm
,
157 const struct nvbios_therm_threshold
*thrs
,
158 enum nouveau_therm_thrs thrs_name
)
160 enum nouveau_therm_thrs_direction direction
;
161 enum nouveau_therm_thrs_state prev_state
, new_state
;
162 int temp
= therm
->temp_get(therm
);
164 prev_state
= nouveau_therm_sensor_get_threshold_state(therm
, thrs_name
);
166 if (temp
>= thrs
->temp
&& prev_state
== NOUVEAU_THERM_THRS_LOWER
) {
167 direction
= NOUVEAU_THERM_THRS_RISING
;
168 new_state
= NOUVEAU_THERM_THRS_HIGHER
;
169 } else if (temp
<= thrs
->temp
- thrs
->hysteresis
&&
170 prev_state
== NOUVEAU_THERM_THRS_HIGHER
) {
171 direction
= NOUVEAU_THERM_THRS_FALLING
;
172 new_state
= NOUVEAU_THERM_THRS_LOWER
;
174 return; /* nothing to do */
176 nouveau_therm_sensor_set_threshold_state(therm
, thrs_name
, new_state
);
177 nouveau_therm_sensor_event(therm
, thrs_name
, direction
);
181 alarm_timer_callback(struct nouveau_alarm
*alarm
)
183 struct nouveau_therm_priv
*priv
=
184 container_of(alarm
, struct nouveau_therm_priv
, sensor
.therm_poll_alarm
);
185 struct nvbios_therm_sensor
*sensor
= &priv
->bios_sensor
;
186 struct nouveau_timer
*ptimer
= nouveau_timer(priv
);
187 struct nouveau_therm
*therm
= &priv
->base
;
190 spin_lock_irqsave(&priv
->sensor
.alarm_program_lock
, flags
);
192 nouveau_therm_threshold_hyst_polling(therm
, &sensor
->thrs_fan_boost
,
193 NOUVEAU_THERM_THRS_FANBOOST
);
195 nouveau_therm_threshold_hyst_polling(therm
, &sensor
->thrs_down_clock
,
196 NOUVEAU_THERM_THRS_DOWNCLOCK
);
198 nouveau_therm_threshold_hyst_polling(therm
, &sensor
->thrs_critical
,
199 NOUVEAU_THERM_THRS_CRITICAL
);
201 nouveau_therm_threshold_hyst_polling(therm
, &sensor
->thrs_shutdown
,
202 NOUVEAU_THERM_THRS_SHUTDOWN
);
204 /* schedule the next poll in one second */
205 if (list_empty(&alarm
->head
))
206 ptimer
->alarm(ptimer
, 1000 * 1000 * 1000, alarm
);
208 spin_unlock_irqrestore(&priv
->sensor
.alarm_program_lock
, flags
);
212 nouveau_therm_program_alarms_polling(struct nouveau_therm
*therm
)
214 struct nouveau_therm_priv
*priv
= (void *)therm
;
215 struct nvbios_therm_sensor
*sensor
= &priv
->bios_sensor
;
218 "programmed thresholds [ %d(%d), %d(%d), %d(%d), %d(%d) ]\n",
219 sensor
->thrs_fan_boost
.temp
, sensor
->thrs_fan_boost
.hysteresis
,
220 sensor
->thrs_down_clock
.temp
,
221 sensor
->thrs_down_clock
.hysteresis
,
222 sensor
->thrs_critical
.temp
, sensor
->thrs_critical
.hysteresis
,
223 sensor
->thrs_shutdown
.temp
, sensor
->thrs_shutdown
.hysteresis
);
225 alarm_timer_callback(&priv
->sensor
.therm_poll_alarm
);
229 nouveau_therm_sensor_ctor(struct nouveau_therm
*therm
)
231 struct nouveau_therm_priv
*priv
= (void *)therm
;
232 struct nouveau_bios
*bios
= nouveau_bios(therm
);
234 nouveau_alarm_init(&priv
->sensor
.therm_poll_alarm
, alarm_timer_callback
);
236 nouveau_therm_temp_set_defaults(therm
);
237 if (nvbios_therm_sensor_parse(bios
, NVBIOS_THERM_DOMAIN_CORE
,
239 nv_error(therm
, "nvbios_therm_sensor_parse failed\n");
240 nouveau_therm_temp_safety_checks(therm
);