1 /*
2 * Copyright 2012 The Nouveau community
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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.
21 *
22 * Authors: Martin Peres
23 */
24 #include "priv.h"
25
26 int
nvkm_therm_temp_get(struct nvkm_therm * therm)27 nvkm_therm_temp_get(struct nvkm_therm *therm)
28 {
29 if (therm->func->temp_get)
30 return therm->func->temp_get(therm);
31 return -ENODEV;
32 }
33
34 static int
nvkm_therm_update_trip(struct nvkm_therm * therm)35 nvkm_therm_update_trip(struct nvkm_therm *therm)
36 {
37 struct nvbios_therm_trip_point *trip = therm->fan->bios.trip,
38 *cur_trip = NULL,
39 *last_trip = therm->last_trip;
40 u8 temp = therm->func->temp_get(therm);
41 u16 duty, i;
42
43 /* look for the trip point corresponding to the current temperature */
44 cur_trip = NULL;
45 for (i = 0; i < therm->fan->bios.nr_fan_trip; i++) {
46 if (temp >= trip[i].temp)
47 cur_trip = &trip[i];
48 }
49
50 /* account for the hysteresis cycle */
51 if (last_trip && temp <= (last_trip->temp) &&
52 temp > (last_trip->temp - last_trip->hysteresis))
53 cur_trip = last_trip;
54
55 if (cur_trip) {
56 duty = cur_trip->fan_duty;
57 therm->last_trip = cur_trip;
58 } else {
59 duty = 0;
60 therm->last_trip = NULL;
61 }
62
63 return duty;
64 }
65
66 static int
nvkm_therm_update_linear(struct nvkm_therm * therm)67 nvkm_therm_update_linear(struct nvkm_therm *therm)
68 {
69 u8 linear_min_temp = therm->fan->bios.linear_min_temp;
70 u8 linear_max_temp = therm->fan->bios.linear_max_temp;
71 u8 temp = therm->func->temp_get(therm);
72 u16 duty;
73
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;
79
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;
85 return duty;
86 }
87
88 static void
nvkm_therm_update(struct nvkm_therm * therm,int mode)89 nvkm_therm_update(struct nvkm_therm *therm, int mode)
90 {
91 struct nvkm_subdev *subdev = &therm->subdev;
92 struct nvkm_timer *tmr = subdev->device->timer;
93 unsigned long flags;
94 bool immd = true;
95 bool poll = true;
96 int duty = -1;
97
98 spin_lock_irqsave(&therm->lock, flags);
99 if (mode < 0)
100 mode = therm->mode;
101 therm->mode = mode;
102
103 switch (mode) {
104 case NVKM_THERM_CTRL_MANUAL:
105 nvkm_timer_alarm_cancel(tmr, &therm->alarm);
106 duty = nvkm_therm_fan_get(therm);
107 if (duty < 0)
108 duty = 100;
109 poll = false;
110 break;
111 case NVKM_THERM_CTRL_AUTO:
112 switch(therm->fan->bios.fan_mode) {
113 case NVBIOS_THERM_FAN_TRIP:
114 duty = nvkm_therm_update_trip(therm);
115 break;
116 case NVBIOS_THERM_FAN_LINEAR:
117 duty = nvkm_therm_update_linear(therm);
118 break;
119 case NVBIOS_THERM_FAN_OTHER:
120 if (therm->cstate)
121 duty = therm->cstate;
122 poll = false;
123 break;
124 }
125 immd = false;
126 break;
127 case NVKM_THERM_CTRL_NONE:
128 default:
129 nvkm_timer_alarm_cancel(tmr, &therm->alarm);
130 poll = false;
131 }
132
133 if (poll)
134 nvkm_timer_alarm(tmr, 1000000000ULL, &therm->alarm);
135 spin_unlock_irqrestore(&therm->lock, flags);
136
137 if (duty >= 0) {
138 nvkm_debug(subdev, "FAN target request: %d%%\n", duty);
139 nvkm_therm_fan_set(therm, immd, duty);
140 }
141 }
142
143 int
nvkm_therm_cstate(struct nvkm_therm * therm,int fan,int dir)144 nvkm_therm_cstate(struct nvkm_therm *therm, int fan, int dir)
145 {
146 struct nvkm_subdev *subdev = &therm->subdev;
147 if (!dir || (dir < 0 && fan < therm->cstate) ||
148 (dir > 0 && fan > therm->cstate)) {
149 nvkm_debug(subdev, "default fan speed -> %d%%\n", fan);
150 therm->cstate = fan;
151 nvkm_therm_update(therm, -1);
152 }
153 return 0;
154 }
155
156 static void
nvkm_therm_alarm(struct nvkm_alarm * alarm)157 nvkm_therm_alarm(struct nvkm_alarm *alarm)
158 {
159 struct nvkm_therm *therm =
160 container_of(alarm, struct nvkm_therm, alarm);
161 nvkm_therm_update(therm, -1);
162 }
163
164 int
nvkm_therm_fan_mode(struct nvkm_therm * therm,int mode)165 nvkm_therm_fan_mode(struct nvkm_therm *therm, int mode)
166 {
167 struct nvkm_subdev *subdev = &therm->subdev;
168 struct nvkm_device *device = subdev->device;
169 static const char *name[] = {
170 "disabled",
171 "manual",
172 "automatic"
173 };
174
175 /* The default PPWR ucode on fermi interferes with fan management */
176 if ((mode >= ARRAY_SIZE(name)) ||
177 (mode != NVKM_THERM_CTRL_NONE && device->card_type >= NV_C0 &&
178 !device->pmu))
179 return -EINVAL;
180
181 /* do not allow automatic fan management if the thermal sensor is
182 * not available */
183 if (mode == NVKM_THERM_CTRL_AUTO &&
184 therm->func->temp_get(therm) < 0)
185 return -EINVAL;
186
187 if (therm->mode == mode)
188 return 0;
189
190 nvkm_debug(subdev, "fan management: %s\n", name[mode]);
191 nvkm_therm_update(therm, mode);
192 return 0;
193 }
194
195 int
nvkm_therm_attr_get(struct nvkm_therm * therm,enum nvkm_therm_attr_type type)196 nvkm_therm_attr_get(struct nvkm_therm *therm, enum nvkm_therm_attr_type type)
197 {
198 switch (type) {
199 case NVKM_THERM_ATTR_FAN_MIN_DUTY:
200 return therm->fan->bios.min_duty;
201 case NVKM_THERM_ATTR_FAN_MAX_DUTY:
202 return therm->fan->bios.max_duty;
203 case NVKM_THERM_ATTR_FAN_MODE:
204 return therm->mode;
205 case NVKM_THERM_ATTR_THRS_FAN_BOOST:
206 return therm->bios_sensor.thrs_fan_boost.temp;
207 case NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST:
208 return therm->bios_sensor.thrs_fan_boost.hysteresis;
209 case NVKM_THERM_ATTR_THRS_DOWN_CLK:
210 return therm->bios_sensor.thrs_down_clock.temp;
211 case NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST:
212 return therm->bios_sensor.thrs_down_clock.hysteresis;
213 case NVKM_THERM_ATTR_THRS_CRITICAL:
214 return therm->bios_sensor.thrs_critical.temp;
215 case NVKM_THERM_ATTR_THRS_CRITICAL_HYST:
216 return therm->bios_sensor.thrs_critical.hysteresis;
217 case NVKM_THERM_ATTR_THRS_SHUTDOWN:
218 return therm->bios_sensor.thrs_shutdown.temp;
219 case NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST:
220 return therm->bios_sensor.thrs_shutdown.hysteresis;
221 }
222
223 return -EINVAL;
224 }
225
226 int
nvkm_therm_attr_set(struct nvkm_therm * therm,enum nvkm_therm_attr_type type,int value)227 nvkm_therm_attr_set(struct nvkm_therm *therm,
228 enum nvkm_therm_attr_type type, int value)
229 {
230 switch (type) {
231 case NVKM_THERM_ATTR_FAN_MIN_DUTY:
232 if (value < 0)
233 value = 0;
234 if (value > therm->fan->bios.max_duty)
235 value = therm->fan->bios.max_duty;
236 therm->fan->bios.min_duty = value;
237 return 0;
238 case NVKM_THERM_ATTR_FAN_MAX_DUTY:
239 if (value < 0)
240 value = 0;
241 if (value < therm->fan->bios.min_duty)
242 value = therm->fan->bios.min_duty;
243 therm->fan->bios.max_duty = value;
244 return 0;
245 case NVKM_THERM_ATTR_FAN_MODE:
246 return nvkm_therm_fan_mode(therm, value);
247 case NVKM_THERM_ATTR_THRS_FAN_BOOST:
248 therm->bios_sensor.thrs_fan_boost.temp = value;
249 therm->func->program_alarms(therm);
250 return 0;
251 case NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST:
252 therm->bios_sensor.thrs_fan_boost.hysteresis = value;
253 therm->func->program_alarms(therm);
254 return 0;
255 case NVKM_THERM_ATTR_THRS_DOWN_CLK:
256 therm->bios_sensor.thrs_down_clock.temp = value;
257 therm->func->program_alarms(therm);
258 return 0;
259 case NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST:
260 therm->bios_sensor.thrs_down_clock.hysteresis = value;
261 therm->func->program_alarms(therm);
262 return 0;
263 case NVKM_THERM_ATTR_THRS_CRITICAL:
264 therm->bios_sensor.thrs_critical.temp = value;
265 therm->func->program_alarms(therm);
266 return 0;
267 case NVKM_THERM_ATTR_THRS_CRITICAL_HYST:
268 therm->bios_sensor.thrs_critical.hysteresis = value;
269 therm->func->program_alarms(therm);
270 return 0;
271 case NVKM_THERM_ATTR_THRS_SHUTDOWN:
272 therm->bios_sensor.thrs_shutdown.temp = value;
273 therm->func->program_alarms(therm);
274 return 0;
275 case NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST:
276 therm->bios_sensor.thrs_shutdown.hysteresis = value;
277 therm->func->program_alarms(therm);
278 return 0;
279 }
280
281 return -EINVAL;
282 }
283
284 static void
nvkm_therm_intr(struct nvkm_subdev * subdev)285 nvkm_therm_intr(struct nvkm_subdev *subdev)
286 {
287 struct nvkm_therm *therm = nvkm_therm(subdev);
288 if (therm->func->intr)
289 therm->func->intr(therm);
290 }
291
292 static int
nvkm_therm_fini(struct nvkm_subdev * subdev,bool suspend)293 nvkm_therm_fini(struct nvkm_subdev *subdev, bool suspend)
294 {
295 struct nvkm_therm *therm = nvkm_therm(subdev);
296
297 if (therm->func->fini)
298 therm->func->fini(therm);
299
300 nvkm_therm_fan_fini(therm, suspend);
301 nvkm_therm_sensor_fini(therm, suspend);
302
303 if (suspend) {
304 therm->suspend = therm->mode;
305 therm->mode = NVKM_THERM_CTRL_NONE;
306 }
307
308 return 0;
309 }
310
311 static int
nvkm_therm_oneinit(struct nvkm_subdev * subdev)312 nvkm_therm_oneinit(struct nvkm_subdev *subdev)
313 {
314 struct nvkm_therm *therm = nvkm_therm(subdev);
315 nvkm_therm_sensor_ctor(therm);
316 nvkm_therm_ic_ctor(therm);
317 nvkm_therm_fan_ctor(therm);
318 nvkm_therm_fan_mode(therm, NVKM_THERM_CTRL_AUTO);
319 nvkm_therm_sensor_preinit(therm);
320 return 0;
321 }
322
323 static int
nvkm_therm_init(struct nvkm_subdev * subdev)324 nvkm_therm_init(struct nvkm_subdev *subdev)
325 {
326 struct nvkm_therm *therm = nvkm_therm(subdev);
327
328 therm->func->init(therm);
329
330 if (therm->suspend >= 0) {
331 /* restore the pwm value only when on manual or auto mode */
332 if (therm->suspend > 0)
333 nvkm_therm_fan_set(therm, true, therm->fan->percent);
334
335 nvkm_therm_fan_mode(therm, therm->suspend);
336 }
337
338 nvkm_therm_sensor_init(therm);
339 nvkm_therm_fan_init(therm);
340 return 0;
341 }
342
343 static void *
nvkm_therm_dtor(struct nvkm_subdev * subdev)344 nvkm_therm_dtor(struct nvkm_subdev *subdev)
345 {
346 struct nvkm_therm *therm = nvkm_therm(subdev);
347 kfree(therm->fan);
348 return therm;
349 }
350
351 static const struct nvkm_subdev_func
352 nvkm_therm = {
353 .dtor = nvkm_therm_dtor,
354 .oneinit = nvkm_therm_oneinit,
355 .init = nvkm_therm_init,
356 .fini = nvkm_therm_fini,
357 .intr = nvkm_therm_intr,
358 };
359
360 int
nvkm_therm_new_(const struct nvkm_therm_func * func,struct nvkm_device * device,int index,struct nvkm_therm ** ptherm)361 nvkm_therm_new_(const struct nvkm_therm_func *func, struct nvkm_device *device,
362 int index, struct nvkm_therm **ptherm)
363 {
364 struct nvkm_therm *therm;
365
366 if (!(therm = *ptherm = kzalloc(sizeof(*therm), GFP_KERNEL)))
367 return -ENOMEM;
368
369 nvkm_subdev_ctor(&nvkm_therm, device, index, 0, &therm->subdev);
370 therm->func = func;
371
372 nvkm_alarm_init(&therm->alarm, nvkm_therm_alarm);
373 spin_lock_init(&therm->lock);
374 spin_lock_init(&therm->sensor.alarm_program_lock);
375
376 therm->fan_get = nvkm_therm_fan_user_get;
377 therm->fan_set = nvkm_therm_fan_user_set;
378 therm->attr_get = nvkm_therm_attr_get;
379 therm->attr_set = nvkm_therm_attr_set;
380 therm->mode = therm->suspend = -1; /* undefined */
381 return 0;
382 }
383