1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include "priv.h"
25
26#include <core/option.h>
27#include <subdev/pmu.h>
28
29int
30nvkm_therm_temp_get(struct nvkm_therm *therm)
31{
32 if (therm->func->temp_get)
33 return therm->func->temp_get(therm);
34 return -ENODEV;
35}
36
37static int
38nvkm_therm_update_trip(struct nvkm_therm *therm)
39{
40 struct nvbios_therm_trip_point *trip = therm->fan->bios.trip,
41 *cur_trip = NULL,
42 *last_trip = therm->last_trip;
43 u8 temp = therm->func->temp_get(therm);
44 u16 duty, i;
45
46
47 cur_trip = NULL;
48 for (i = 0; i < therm->fan->bios.nr_fan_trip; i++) {
49 if (temp >= trip[i].temp)
50 cur_trip = &trip[i];
51 }
52
53
54 if (last_trip && temp <= (last_trip->temp) &&
55 temp > (last_trip->temp - last_trip->hysteresis))
56 cur_trip = last_trip;
57
58 if (cur_trip) {
59 duty = cur_trip->fan_duty;
60 therm->last_trip = cur_trip;
61 } else {
62 duty = 0;
63 therm->last_trip = NULL;
64 }
65
66 return duty;
67}
68
69static int
70nvkm_therm_compute_linear_duty(struct nvkm_therm *therm, u8 linear_min_temp,
71 u8 linear_max_temp)
72{
73 u8 temp = therm->func->temp_get(therm);
74 u16 duty;
75
76
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;
81
82
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;
87 return duty;
88}
89
90static int
91nvkm_therm_update_linear(struct nvkm_therm *therm)
92{
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);
96}
97
98static int
99nvkm_therm_update_linear_fallback(struct nvkm_therm *therm)
100{
101 u8 max = therm->bios_sensor.thrs_fan_boost.temp;
102 return nvkm_therm_compute_linear_duty(therm, 30, max);
103}
104
105static void
106nvkm_therm_update(struct nvkm_therm *therm, int mode)
107{
108 struct nvkm_subdev *subdev = &therm->subdev;
109 struct nvkm_timer *tmr = subdev->device->timer;
110 unsigned long flags;
111 bool immd = true;
112 bool poll = true;
113 int duty = -1;
114
115 spin_lock_irqsave(&therm->lock, flags);
116 if (mode < 0)
117 mode = therm->mode;
118 therm->mode = mode;
119
120 switch (mode) {
121 case NVKM_THERM_CTRL_MANUAL:
122 nvkm_timer_alarm(tmr, 0, &therm->alarm);
123 duty = nvkm_therm_fan_get(therm);
124 if (duty < 0)
125 duty = 100;
126 poll = false;
127 break;
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);
132 break;
133 case NVBIOS_THERM_FAN_LINEAR:
134 duty = nvkm_therm_update_linear(therm);
135 break;
136 case NVBIOS_THERM_FAN_OTHER:
137 if (therm->cstate) {
138 duty = therm->cstate;
139 poll = false;
140 } else {
141 duty = nvkm_therm_update_linear_fallback(therm);
142 }
143 break;
144 }
145 immd = false;
146 break;
147 case NVKM_THERM_CTRL_NONE:
148 default:
149 nvkm_timer_alarm(tmr, 0, &therm->alarm);
150 poll = false;
151 }
152
153 if (poll)
154 nvkm_timer_alarm(tmr, 1000000000ULL, &therm->alarm);
155 spin_unlock_irqrestore(&therm->lock, flags);
156
157 if (duty >= 0) {
158 nvkm_debug(subdev, "FAN target request: %d%%\n", duty);
159 nvkm_therm_fan_set(therm, immd, duty);
160 }
161}
162
163int
164nvkm_therm_cstate(struct nvkm_therm *therm, int fan, int dir)
165{
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);
170 therm->cstate = fan;
171 nvkm_therm_update(therm, -1);
172 }
173 return 0;
174}
175
176static void
177nvkm_therm_alarm(struct nvkm_alarm *alarm)
178{
179 struct nvkm_therm *therm =
180 container_of(alarm, struct nvkm_therm, alarm);
181 nvkm_therm_update(therm, -1);
182}
183
184int
185nvkm_therm_fan_mode(struct nvkm_therm *therm, int mode)
186{
187 struct nvkm_subdev *subdev = &therm->subdev;
188 struct nvkm_device *device = subdev->device;
189 static const char *name[] = {
190 "disabled",
191 "manual",
192 "automatic"
193 };
194
195
196 if ((mode >= ARRAY_SIZE(name)) ||
197 (mode != NVKM_THERM_CTRL_NONE && nvkm_pmu_fan_controlled(device)))
198 return -EINVAL;
199
200
201
202 if (mode == NVKM_THERM_CTRL_AUTO &&
203 therm->func->temp_get(therm) < 0)
204 return -EINVAL;
205
206 if (therm->mode == mode)
207 return 0;
208
209 nvkm_debug(subdev, "fan management: %s\n", name[mode]);
210 nvkm_therm_update(therm, mode);
211 return 0;
212}
213
214int
215nvkm_therm_attr_get(struct nvkm_therm *therm, enum nvkm_therm_attr_type type)
216{
217 switch (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:
223 return therm->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;
240 }
241
242 return -EINVAL;
243}
244
245int
246nvkm_therm_attr_set(struct nvkm_therm *therm,
247 enum nvkm_therm_attr_type type, int value)
248{
249 switch (type) {
250 case NVKM_THERM_ATTR_FAN_MIN_DUTY:
251 if (value < 0)
252 value = 0;
253 if (value > therm->fan->bios.max_duty)
254 value = therm->fan->bios.max_duty;
255 therm->fan->bios.min_duty = value;
256 return 0;
257 case NVKM_THERM_ATTR_FAN_MAX_DUTY:
258 if (value < 0)
259 value = 0;
260 if (value < therm->fan->bios.min_duty)
261 value = therm->fan->bios.min_duty;
262 therm->fan->bios.max_duty = value;
263 return 0;
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);
269 return 0;
270 case NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST:
271 therm->bios_sensor.thrs_fan_boost.hysteresis = value;
272 therm->func->program_alarms(therm);
273 return 0;
274 case NVKM_THERM_ATTR_THRS_DOWN_CLK:
275 therm->bios_sensor.thrs_down_clock.temp = value;
276 therm->func->program_alarms(therm);
277 return 0;
278 case NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST:
279 therm->bios_sensor.thrs_down_clock.hysteresis = value;
280 therm->func->program_alarms(therm);
281 return 0;
282 case NVKM_THERM_ATTR_THRS_CRITICAL:
283 therm->bios_sensor.thrs_critical.temp = value;
284 therm->func->program_alarms(therm);
285 return 0;
286 case NVKM_THERM_ATTR_THRS_CRITICAL_HYST:
287 therm->bios_sensor.thrs_critical.hysteresis = value;
288 therm->func->program_alarms(therm);
289 return 0;
290 case NVKM_THERM_ATTR_THRS_SHUTDOWN:
291 therm->bios_sensor.thrs_shutdown.temp = value;
292 therm->func->program_alarms(therm);
293 return 0;
294 case NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST:
295 therm->bios_sensor.thrs_shutdown.hysteresis = value;
296 therm->func->program_alarms(therm);
297 return 0;
298 }
299
300 return -EINVAL;
301}
302
303void
304nvkm_therm_clkgate_enable(struct nvkm_therm *therm)
305{
306 if (!therm || !therm->func->clkgate_enable || !therm->clkgating_enabled)
307 return;
308
309 nvkm_debug(&therm->subdev,
310 "Enabling clockgating\n");
311 therm->func->clkgate_enable(therm);
312}
313
314void
315nvkm_therm_clkgate_fini(struct nvkm_therm *therm, bool suspend)
316{
317 if (!therm || !therm->func->clkgate_fini || !therm->clkgating_enabled)
318 return;
319
320 nvkm_debug(&therm->subdev,
321 "Preparing clockgating for %s\n",
322 suspend ? "suspend" : "fini");
323 therm->func->clkgate_fini(therm, suspend);
324}
325
326static void
327nvkm_therm_clkgate_oneinit(struct nvkm_therm *therm)
328{
329 if (!therm->func->clkgate_enable || !therm->clkgating_enabled)
330 return;
331
332 nvkm_info(&therm->subdev, "Clockgating enabled\n");
333}
334
335static void
336nvkm_therm_intr(struct nvkm_subdev *subdev)
337{
338 struct nvkm_therm *therm = nvkm_therm(subdev);
339 if (therm->func->intr)
340 therm->func->intr(therm);
341}
342
343static int
344nvkm_therm_fini(struct nvkm_subdev *subdev, bool suspend)
345{
346 struct nvkm_therm *therm = nvkm_therm(subdev);
347
348 if (therm->func->fini)
349 therm->func->fini(therm);
350
351 nvkm_therm_fan_fini(therm, suspend);
352 nvkm_therm_sensor_fini(therm, suspend);
353
354 if (suspend) {
355 therm->suspend = therm->mode;
356 therm->mode = NVKM_THERM_CTRL_NONE;
357 }
358
359 return 0;
360}
361
362static int
363nvkm_therm_oneinit(struct nvkm_subdev *subdev)
364{
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);
372 return 0;
373}
374
375static int
376nvkm_therm_init(struct nvkm_subdev *subdev)
377{
378 struct nvkm_therm *therm = nvkm_therm(subdev);
379
380 if (therm->func->init)
381 therm->func->init(therm);
382
383 if (therm->suspend >= 0) {
384
385 if (therm->suspend > 0)
386 nvkm_therm_fan_set(therm, true, therm->fan->percent);
387
388 nvkm_therm_fan_mode(therm, therm->suspend);
389 }
390
391 nvkm_therm_sensor_init(therm);
392 nvkm_therm_fan_init(therm);
393 return 0;
394}
395
396void
397nvkm_therm_clkgate_init(struct nvkm_therm *therm,
398 const struct nvkm_therm_clkgate_pack *p)
399{
400 if (!therm || !therm->func->clkgate_init || !therm->clkgating_enabled)
401 return;
402
403 therm->func->clkgate_init(therm, p);
404}
405
406static void *
407nvkm_therm_dtor(struct nvkm_subdev *subdev)
408{
409 struct nvkm_therm *therm = nvkm_therm(subdev);
410 kfree(therm->fan);
411 return therm;
412}
413
414static const struct nvkm_subdev_func
415nvkm_therm = {
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,
421};
422
423void
424nvkm_therm_ctor(struct nvkm_therm *therm, struct nvkm_device *device, enum nvkm_subdev_type type,
425 int inst, const struct nvkm_therm_func *func)
426{
427 nvkm_subdev_ctor(&nvkm_therm, device, type, inst, &therm->subdev);
428 therm->func = func;
429
430 nvkm_alarm_init(&therm->alarm, nvkm_therm_alarm);
431 spin_lock_init(&therm->lock);
432 spin_lock_init(&therm->sensor.alarm_program_lock);
433
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;
439
440 therm->clkgating_enabled = nvkm_boolopt(device->cfgopt,
441 "NvPmEnableGating", false);
442}
443
444int
445nvkm_therm_new_(const struct nvkm_therm_func *func, struct nvkm_device *device,
446 enum nvkm_subdev_type type, int inst, struct nvkm_therm **ptherm)
447{
448 struct nvkm_therm *therm;
449
450 if (!(therm = *ptherm = kzalloc(sizeof(*therm), GFP_KERNEL)))
451 return -ENOMEM;
452
453 nvkm_therm_ctor(therm, device, type, inst, func);
454 return 0;
455}
456