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
24 #include <nvkm/core/option.h>
28 nvkm_therm_temp_get(struct nvkm_therm *therm)
30 if (therm->func->temp_get)
31 return therm->func->temp_get(therm);
36 nvkm_therm_update_trip(struct nvkm_therm *therm)
38 struct nvbios_therm_trip_point *trip = therm->fan->bios.trip,
40 *last_trip = therm->last_trip;
41 u8 temp = therm->func->temp_get(therm);
44 /* look for the trip point corresponding to the current temperature */
46 for (i = 0; i < therm->fan->bios.nr_fan_trip; i++) {
47 if (temp >= trip[i].temp)
51 /* account for the hysteresis cycle */
52 if (last_trip && temp <= (last_trip->temp) &&
53 temp > (last_trip->temp - last_trip->hysteresis))
57 duty = cur_trip->fan_duty;
58 therm->last_trip = cur_trip;
61 therm->last_trip = NULL;
68 nvkm_therm_compute_linear_duty(struct nvkm_therm *therm, u8 linear_min_temp,
71 u8 temp = therm->func->temp_get(therm);
74 /* handle the non-linear part first */
75 if (temp < linear_min_temp)
76 return therm->fan->bios.min_duty;
77 else if (temp > linear_max_temp)
78 return therm->fan->bios.max_duty;
80 /* we are in the linear zone */
81 duty = (temp - linear_min_temp);
82 duty *= (therm->fan->bios.max_duty - therm->fan->bios.min_duty);
83 duty /= (linear_max_temp - linear_min_temp);
84 duty += therm->fan->bios.min_duty;
89 nvkm_therm_update_linear(struct nvkm_therm *therm)
91 u8 min = therm->fan->bios.linear_min_temp;
92 u8 max = therm->fan->bios.linear_max_temp;
93 return nvkm_therm_compute_linear_duty(therm, min, max);
97 nvkm_therm_update_linear_fallback(struct nvkm_therm *therm)
99 u8 max = therm->bios_sensor.thrs_fan_boost.temp;
100 return nvkm_therm_compute_linear_duty(therm, 30, max);
104 nvkm_therm_update(struct nvkm_therm *therm, int mode)
106 struct nvkm_subdev *subdev = &therm->subdev;
107 struct nvkm_timer *tmr = subdev->device->timer;
113 spin_lock_irqsave(&therm->lock, flags);
119 case NVKM_THERM_CTRL_MANUAL:
120 nvkm_timer_alarm(tmr, 0, &therm->alarm);
121 duty = nvkm_therm_fan_get(therm);
126 case NVKM_THERM_CTRL_AUTO:
127 switch(therm->fan->bios.fan_mode) {
128 case NVBIOS_THERM_FAN_TRIP:
129 duty = nvkm_therm_update_trip(therm);
131 case NVBIOS_THERM_FAN_LINEAR:
132 duty = nvkm_therm_update_linear(therm);
134 case NVBIOS_THERM_FAN_OTHER:
136 duty = therm->cstate;
138 duty = nvkm_therm_update_linear_fallback(therm);
144 case NVKM_THERM_CTRL_NONE:
146 nvkm_timer_alarm(tmr, 0, &therm->alarm);
151 nvkm_timer_alarm(tmr, 1000000000ULL, &therm->alarm);
152 spin_unlock_irqrestore(&therm->lock, flags);
155 nvkm_debug(subdev, "FAN target request: %d%%\n", duty);
156 nvkm_therm_fan_set(therm, immd, duty);
161 nvkm_therm_cstate(struct nvkm_therm *therm, int fan, int dir)
163 struct nvkm_subdev *subdev = &therm->subdev;
164 if (!dir || (dir < 0 && fan < therm->cstate) ||
165 (dir > 0 && fan > therm->cstate)) {
166 nvkm_debug(subdev, "default fan speed -> %d%%\n", fan);
168 nvkm_therm_update(therm, -1);
174 nvkm_therm_alarm(struct nvkm_alarm *alarm)
176 struct nvkm_therm *therm =
177 container_of(alarm, struct nvkm_therm, alarm);
178 nvkm_therm_update(therm, -1);
182 nvkm_therm_fan_mode(struct nvkm_therm *therm, int mode)
184 struct nvkm_subdev *subdev = &therm->subdev;
185 struct nvkm_device *device = subdev->device;
186 static const char *name[] = {
192 /* The default PPWR ucode on fermi interferes with fan management */
193 if ((mode >= ARRAY_SIZE(name)) ||
194 (mode != NVKM_THERM_CTRL_NONE && device->card_type >= NV_C0 &&
198 /* do not allow automatic fan management if the thermal sensor is
200 if (mode == NVKM_THERM_CTRL_AUTO &&
201 therm->func->temp_get(therm) < 0)
204 if (therm->mode == mode)
207 nvkm_debug(subdev, "fan management: %s\n", name[mode]);
208 nvkm_therm_update(therm, mode);
213 nvkm_therm_attr_get(struct nvkm_therm *therm, enum nvkm_therm_attr_type type)
216 case NVKM_THERM_ATTR_FAN_MIN_DUTY:
217 return therm->fan->bios.min_duty;
218 case NVKM_THERM_ATTR_FAN_MAX_DUTY:
219 return therm->fan->bios.max_duty;
220 case NVKM_THERM_ATTR_FAN_MODE:
222 case NVKM_THERM_ATTR_THRS_FAN_BOOST:
223 return therm->bios_sensor.thrs_fan_boost.temp;
224 case NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST:
225 return therm->bios_sensor.thrs_fan_boost.hysteresis;
226 case NVKM_THERM_ATTR_THRS_DOWN_CLK:
227 return therm->bios_sensor.thrs_down_clock.temp;
228 case NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST:
229 return therm->bios_sensor.thrs_down_clock.hysteresis;
230 case NVKM_THERM_ATTR_THRS_CRITICAL:
231 return therm->bios_sensor.thrs_critical.temp;
232 case NVKM_THERM_ATTR_THRS_CRITICAL_HYST:
233 return therm->bios_sensor.thrs_critical.hysteresis;
234 case NVKM_THERM_ATTR_THRS_SHUTDOWN:
235 return therm->bios_sensor.thrs_shutdown.temp;
236 case NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST:
237 return therm->bios_sensor.thrs_shutdown.hysteresis;
244 nvkm_therm_attr_set(struct nvkm_therm *therm,
245 enum nvkm_therm_attr_type type, int value)
248 case NVKM_THERM_ATTR_FAN_MIN_DUTY:
251 if (value > therm->fan->bios.max_duty)
252 value = therm->fan->bios.max_duty;
253 therm->fan->bios.min_duty = value;
255 case NVKM_THERM_ATTR_FAN_MAX_DUTY:
258 if (value < therm->fan->bios.min_duty)
259 value = therm->fan->bios.min_duty;
260 therm->fan->bios.max_duty = value;
262 case NVKM_THERM_ATTR_FAN_MODE:
263 return nvkm_therm_fan_mode(therm, value);
264 case NVKM_THERM_ATTR_THRS_FAN_BOOST:
265 therm->bios_sensor.thrs_fan_boost.temp = value;
266 therm->func->program_alarms(therm);
268 case NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST:
269 therm->bios_sensor.thrs_fan_boost.hysteresis = value;
270 therm->func->program_alarms(therm);
272 case NVKM_THERM_ATTR_THRS_DOWN_CLK:
273 therm->bios_sensor.thrs_down_clock.temp = value;
274 therm->func->program_alarms(therm);
276 case NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST:
277 therm->bios_sensor.thrs_down_clock.hysteresis = value;
278 therm->func->program_alarms(therm);
280 case NVKM_THERM_ATTR_THRS_CRITICAL:
281 therm->bios_sensor.thrs_critical.temp = value;
282 therm->func->program_alarms(therm);
284 case NVKM_THERM_ATTR_THRS_CRITICAL_HYST:
285 therm->bios_sensor.thrs_critical.hysteresis = value;
286 therm->func->program_alarms(therm);
288 case NVKM_THERM_ATTR_THRS_SHUTDOWN:
289 therm->bios_sensor.thrs_shutdown.temp = value;
290 therm->func->program_alarms(therm);
292 case NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST:
293 therm->bios_sensor.thrs_shutdown.hysteresis = value;
294 therm->func->program_alarms(therm);
302 nvkm_therm_clkgate_enable(struct nvkm_therm *therm)
304 if (!therm || !therm->func->clkgate_enable || !therm->clkgating_enabled)
307 nvkm_debug(&therm->subdev,
308 "Enabling clockgating\n");
309 therm->func->clkgate_enable(therm);
313 nvkm_therm_clkgate_fini(struct nvkm_therm *therm, bool suspend)
315 if (!therm || !therm->func->clkgate_fini || !therm->clkgating_enabled)
318 nvkm_debug(&therm->subdev,
319 "Preparing clockgating for %s\n",
320 suspend ? "suspend" : "fini");
321 therm->func->clkgate_fini(therm, suspend);
325 nvkm_therm_clkgate_oneinit(struct nvkm_therm *therm)
327 if (!therm->func->clkgate_enable || !therm->clkgating_enabled)
330 nvkm_info(&therm->subdev, "Clockgating enabled\n");
334 nvkm_therm_intr(struct nvkm_subdev *subdev)
336 struct nvkm_therm *therm = nvkm_therm(subdev);
337 if (therm->func->intr)
338 therm->func->intr(therm);
342 nvkm_therm_fini(struct nvkm_subdev *subdev, bool suspend)
344 struct nvkm_therm *therm = nvkm_therm(subdev);
346 if (therm->func->fini)
347 therm->func->fini(therm);
349 nvkm_therm_fan_fini(therm, suspend);
350 nvkm_therm_sensor_fini(therm, suspend);
353 therm->suspend = therm->mode;
354 therm->mode = NVKM_THERM_CTRL_NONE;
361 nvkm_therm_oneinit(struct nvkm_subdev *subdev)
363 struct nvkm_therm *therm = nvkm_therm(subdev);
364 nvkm_therm_sensor_ctor(therm);
365 nvkm_therm_ic_ctor(therm);
366 nvkm_therm_fan_ctor(therm);
367 nvkm_therm_fan_mode(therm, NVKM_THERM_CTRL_AUTO);
368 nvkm_therm_sensor_preinit(therm);
369 nvkm_therm_clkgate_oneinit(therm);
374 nvkm_therm_init(struct nvkm_subdev *subdev)
376 struct nvkm_therm *therm = nvkm_therm(subdev);
378 if (therm->func->init)
379 therm->func->init(therm);
381 if (therm->suspend >= 0) {
382 /* restore the pwm value only when on manual or auto mode */
383 if (therm->suspend > 0)
384 nvkm_therm_fan_set(therm, true, therm->fan->percent);
386 nvkm_therm_fan_mode(therm, therm->suspend);
389 nvkm_therm_sensor_init(therm);
390 nvkm_therm_fan_init(therm);
395 nvkm_therm_clkgate_init(struct nvkm_therm *therm,
396 const struct nvkm_therm_clkgate_pack *p)
398 if (!therm || !therm->func->clkgate_init || !therm->clkgating_enabled)
401 therm->func->clkgate_init(therm, p);
405 nvkm_therm_dtor(struct nvkm_subdev *subdev)
407 struct nvkm_therm *therm = nvkm_therm(subdev);
412 static const struct nvkm_subdev_func
414 .dtor = nvkm_therm_dtor,
415 .oneinit = nvkm_therm_oneinit,
416 .init = nvkm_therm_init,
417 .fini = nvkm_therm_fini,
418 .intr = nvkm_therm_intr,
422 nvkm_therm_ctor(struct nvkm_therm *therm, struct nvkm_device *device,
423 int index, const struct nvkm_therm_func *func)
425 nvkm_subdev_ctor(&nvkm_therm, device, index, &therm->subdev);
428 nvkm_alarm_init(&therm->alarm, nvkm_therm_alarm);
429 spin_lock_init(&therm->lock);
430 spin_lock_init(&therm->sensor.alarm_program_lock);
432 therm->fan_get = nvkm_therm_fan_user_get;
433 therm->fan_set = nvkm_therm_fan_user_set;
434 therm->attr_get = nvkm_therm_attr_get;
435 therm->attr_set = nvkm_therm_attr_set;
436 therm->mode = therm->suspend = -1; /* undefined */
438 therm->clkgating_enabled = nvkm_boolopt(device->cfgopt,
439 "NvPmEnableGating", false);
443 nvkm_therm_new_(const struct nvkm_therm_func *func, struct nvkm_device *device,
444 int index, struct nvkm_therm **ptherm)
446 struct nvkm_therm *therm;
448 if (!(therm = *ptherm = kzalloc(sizeof(*therm), GFP_KERNEL)))
451 nvkm_therm_ctor(therm, device, index, func);