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