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
26 #include <core/option.h>
27 #include <subdev/pmu.h>
30 nvkm_therm_temp_get(struct nvkm_therm *therm)
32 if (therm->func->temp_get)
33 return therm->func->temp_get(therm);
38 nvkm_therm_update_trip(struct nvkm_therm *therm)
40 struct nvbios_therm_trip_point *trip = therm->fan->bios.trip,
42 *last_trip = therm->last_trip;
43 u8 temp = therm->func->temp_get(therm);
46 /* look for the trip point corresponding to the current temperature */
48 for (i = 0; i < therm->fan->bios.nr_fan_trip; i++) {
49 if (temp >= trip[i].temp)
53 /* account for the hysteresis cycle */
54 if (last_trip && temp <= (last_trip->temp) &&
55 temp > (last_trip->temp - last_trip->hysteresis))
59 duty = cur_trip->fan_duty;
60 therm->last_trip = cur_trip;
63 therm->last_trip = NULL;
70 nvkm_therm_compute_linear_duty(struct nvkm_therm *therm, u8 linear_min_temp,
73 u8 temp = therm->func->temp_get(therm);
76 /* handle the non-linear part first */
77 if (temp < linear_min_temp)
78 return therm->fan->bios.min_duty;
79 else if (temp > linear_max_temp)
80 return therm->fan->bios.max_duty;
82 /* we are in the linear zone */
83 duty = (temp - linear_min_temp);
84 duty *= (therm->fan->bios.max_duty - therm->fan->bios.min_duty);
85 duty /= (linear_max_temp - linear_min_temp);
86 duty += therm->fan->bios.min_duty;
91 nvkm_therm_update_linear(struct nvkm_therm *therm)
93 u8 min = therm->fan->bios.linear_min_temp;
94 u8 max = therm->fan->bios.linear_max_temp;
95 return nvkm_therm_compute_linear_duty(therm, min, max);
99 nvkm_therm_update_linear_fallback(struct nvkm_therm *therm)
101 u8 max = therm->bios_sensor.thrs_fan_boost.temp;
102 return nvkm_therm_compute_linear_duty(therm, 30, max);
106 nvkm_therm_update(struct nvkm_therm *therm, int mode)
108 struct nvkm_subdev *subdev = &therm->subdev;
109 struct nvkm_timer *tmr = subdev->device->timer;
115 spin_lock_irqsave(&therm->lock, flags);
121 case NVKM_THERM_CTRL_MANUAL:
122 nvkm_timer_alarm(tmr, 0, &therm->alarm);
123 duty = nvkm_therm_fan_get(therm);
128 case NVKM_THERM_CTRL_AUTO:
129 switch(therm->fan->bios.fan_mode) {
130 case NVBIOS_THERM_FAN_TRIP:
131 duty = nvkm_therm_update_trip(therm);
133 case NVBIOS_THERM_FAN_LINEAR:
134 duty = nvkm_therm_update_linear(therm);
136 case NVBIOS_THERM_FAN_OTHER:
138 duty = therm->cstate;
141 duty = nvkm_therm_update_linear_fallback(therm);
147 case NVKM_THERM_CTRL_NONE:
149 nvkm_timer_alarm(tmr, 0, &therm->alarm);
154 nvkm_timer_alarm(tmr, 1000000000ULL, &therm->alarm);
155 spin_unlock_irqrestore(&therm->lock, flags);
158 nvkm_debug(subdev, "FAN target request: %d%%\n", duty);
159 nvkm_therm_fan_set(therm, immd, duty);
164 nvkm_therm_cstate(struct nvkm_therm *therm, int fan, int dir)
166 struct nvkm_subdev *subdev = &therm->subdev;
167 if (!dir || (dir < 0 && fan < therm->cstate) ||
168 (dir > 0 && fan > therm->cstate)) {
169 nvkm_debug(subdev, "default fan speed -> %d%%\n", fan);
171 nvkm_therm_update(therm, -1);
177 nvkm_therm_alarm(struct nvkm_alarm *alarm)
179 struct nvkm_therm *therm =
180 container_of(alarm, struct nvkm_therm, alarm);
181 nvkm_therm_update(therm, -1);
185 nvkm_therm_fan_mode(struct nvkm_therm *therm, int mode)
187 struct nvkm_subdev *subdev = &therm->subdev;
188 struct nvkm_device *device = subdev->device;
189 static const char *name[] = {
195 /* The default PPWR ucode on fermi interferes with fan management */
196 if ((mode >= ARRAY_SIZE(name)) ||
197 (mode != NVKM_THERM_CTRL_NONE && nvkm_pmu_fan_controlled(device)))
200 /* do not allow automatic fan management if the thermal sensor is
202 if (mode == NVKM_THERM_CTRL_AUTO &&
203 therm->func->temp_get(therm) < 0)
206 if (therm->mode == mode)
209 nvkm_debug(subdev, "fan management: %s\n", name[mode]);
210 nvkm_therm_update(therm, mode);
215 nvkm_therm_attr_get(struct nvkm_therm *therm, enum nvkm_therm_attr_type type)
218 case NVKM_THERM_ATTR_FAN_MIN_DUTY:
219 return therm->fan->bios.min_duty;
220 case NVKM_THERM_ATTR_FAN_MAX_DUTY:
221 return therm->fan->bios.max_duty;
222 case NVKM_THERM_ATTR_FAN_MODE:
224 case NVKM_THERM_ATTR_THRS_FAN_BOOST:
225 return therm->bios_sensor.thrs_fan_boost.temp;
226 case NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST:
227 return therm->bios_sensor.thrs_fan_boost.hysteresis;
228 case NVKM_THERM_ATTR_THRS_DOWN_CLK:
229 return therm->bios_sensor.thrs_down_clock.temp;
230 case NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST:
231 return therm->bios_sensor.thrs_down_clock.hysteresis;
232 case NVKM_THERM_ATTR_THRS_CRITICAL:
233 return therm->bios_sensor.thrs_critical.temp;
234 case NVKM_THERM_ATTR_THRS_CRITICAL_HYST:
235 return therm->bios_sensor.thrs_critical.hysteresis;
236 case NVKM_THERM_ATTR_THRS_SHUTDOWN:
237 return therm->bios_sensor.thrs_shutdown.temp;
238 case NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST:
239 return therm->bios_sensor.thrs_shutdown.hysteresis;
246 nvkm_therm_attr_set(struct nvkm_therm *therm,
247 enum nvkm_therm_attr_type type, int value)
250 case NVKM_THERM_ATTR_FAN_MIN_DUTY:
253 if (value > therm->fan->bios.max_duty)
254 value = therm->fan->bios.max_duty;
255 therm->fan->bios.min_duty = value;
257 case NVKM_THERM_ATTR_FAN_MAX_DUTY:
260 if (value < therm->fan->bios.min_duty)
261 value = therm->fan->bios.min_duty;
262 therm->fan->bios.max_duty = value;
264 case NVKM_THERM_ATTR_FAN_MODE:
265 return nvkm_therm_fan_mode(therm, value);
266 case NVKM_THERM_ATTR_THRS_FAN_BOOST:
267 therm->bios_sensor.thrs_fan_boost.temp = value;
268 therm->func->program_alarms(therm);
270 case NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST:
271 therm->bios_sensor.thrs_fan_boost.hysteresis = value;
272 therm->func->program_alarms(therm);
274 case NVKM_THERM_ATTR_THRS_DOWN_CLK:
275 therm->bios_sensor.thrs_down_clock.temp = value;
276 therm->func->program_alarms(therm);
278 case NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST:
279 therm->bios_sensor.thrs_down_clock.hysteresis = value;
280 therm->func->program_alarms(therm);
282 case NVKM_THERM_ATTR_THRS_CRITICAL:
283 therm->bios_sensor.thrs_critical.temp = value;
284 therm->func->program_alarms(therm);
286 case NVKM_THERM_ATTR_THRS_CRITICAL_HYST:
287 therm->bios_sensor.thrs_critical.hysteresis = value;
288 therm->func->program_alarms(therm);
290 case NVKM_THERM_ATTR_THRS_SHUTDOWN:
291 therm->bios_sensor.thrs_shutdown.temp = value;
292 therm->func->program_alarms(therm);
294 case NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST:
295 therm->bios_sensor.thrs_shutdown.hysteresis = value;
296 therm->func->program_alarms(therm);
304 nvkm_therm_clkgate_enable(struct nvkm_therm *therm)
306 if (!therm || !therm->func->clkgate_enable || !therm->clkgating_enabled)
309 nvkm_debug(&therm->subdev,
310 "Enabling clockgating\n");
311 therm->func->clkgate_enable(therm);
315 nvkm_therm_clkgate_fini(struct nvkm_therm *therm, bool suspend)
317 if (!therm || !therm->func->clkgate_fini || !therm->clkgating_enabled)
320 nvkm_debug(&therm->subdev,
321 "Preparing clockgating for %s\n",
322 suspend ? "suspend" : "fini");
323 therm->func->clkgate_fini(therm, suspend);
327 nvkm_therm_clkgate_oneinit(struct nvkm_therm *therm)
329 if (!therm->func->clkgate_enable || !therm->clkgating_enabled)
332 nvkm_info(&therm->subdev, "Clockgating enabled\n");
336 nvkm_therm_intr(struct nvkm_subdev *subdev)
338 struct nvkm_therm *therm = nvkm_therm(subdev);
339 if (therm->func->intr)
340 therm->func->intr(therm);
344 nvkm_therm_fini(struct nvkm_subdev *subdev, bool suspend)
346 struct nvkm_therm *therm = nvkm_therm(subdev);
348 if (therm->func->fini)
349 therm->func->fini(therm);
351 nvkm_therm_fan_fini(therm, suspend);
352 nvkm_therm_sensor_fini(therm, suspend);
355 therm->suspend = therm->mode;
356 therm->mode = NVKM_THERM_CTRL_NONE;
363 nvkm_therm_oneinit(struct nvkm_subdev *subdev)
365 struct nvkm_therm *therm = nvkm_therm(subdev);
366 nvkm_therm_sensor_ctor(therm);
367 nvkm_therm_ic_ctor(therm);
368 nvkm_therm_fan_ctor(therm);
369 nvkm_therm_fan_mode(therm, NVKM_THERM_CTRL_AUTO);
370 nvkm_therm_sensor_preinit(therm);
371 nvkm_therm_clkgate_oneinit(therm);
376 nvkm_therm_init(struct nvkm_subdev *subdev)
378 struct nvkm_therm *therm = nvkm_therm(subdev);
380 if (therm->func->init)
381 therm->func->init(therm);
383 if (therm->suspend >= 0) {
384 /* restore the pwm value only when on manual or auto mode */
385 if (therm->suspend > 0)
386 nvkm_therm_fan_set(therm, true, therm->fan->percent);
388 nvkm_therm_fan_mode(therm, therm->suspend);
391 nvkm_therm_sensor_init(therm);
392 nvkm_therm_fan_init(therm);
397 nvkm_therm_clkgate_init(struct nvkm_therm *therm,
398 const struct nvkm_therm_clkgate_pack *p)
400 if (!therm || !therm->func->clkgate_init || !therm->clkgating_enabled)
403 therm->func->clkgate_init(therm, p);
407 nvkm_therm_dtor(struct nvkm_subdev *subdev)
409 struct nvkm_therm *therm = nvkm_therm(subdev);
414 static const struct nvkm_subdev_func
416 .dtor = nvkm_therm_dtor,
417 .oneinit = nvkm_therm_oneinit,
418 .init = nvkm_therm_init,
419 .fini = nvkm_therm_fini,
420 .intr = nvkm_therm_intr,
424 nvkm_therm_ctor(struct nvkm_therm *therm, struct nvkm_device *device,
425 int index, const struct nvkm_therm_func *func)
427 nvkm_subdev_ctor(&nvkm_therm, device, index, &therm->subdev);
430 nvkm_alarm_init(&therm->alarm, nvkm_therm_alarm);
431 spin_lock_init(&therm->lock);
432 spin_lock_init(&therm->sensor.alarm_program_lock);
434 therm->fan_get = nvkm_therm_fan_user_get;
435 therm->fan_set = nvkm_therm_fan_user_set;
436 therm->attr_get = nvkm_therm_attr_get;
437 therm->attr_set = nvkm_therm_attr_set;
438 therm->mode = therm->suspend = -1; /* undefined */
440 therm->clkgating_enabled = nvkm_boolopt(device->cfgopt,
441 "NvPmEnableGating", false);
445 nvkm_therm_new_(const struct nvkm_therm_func *func, struct nvkm_device *device,
446 int index, struct nvkm_therm **ptherm)
448 struct nvkm_therm *therm;
450 if (!(therm = *ptherm = kzalloc(sizeof(*therm), GFP_KERNEL)))
453 nvkm_therm_ctor(therm, device, index, func);