GNU Linux-libre 4.14.259-gnu1
[releases.git] / drivers / gpu / drm / nouveau / nvkm / subdev / therm / base.c
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
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
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
67 nvkm_therm_compute_linear_duty(struct nvkm_therm *therm, u8 linear_min_temp,
68                                u8 linear_max_temp)
69 {
70         u8  temp = therm->func->temp_get(therm);
71         u16 duty;
72
73         /* handle the non-linear part first */
74         if (temp < linear_min_temp)
75                 return therm->fan->bios.min_duty;
76         else if (temp > linear_max_temp)
77                 return therm->fan->bios.max_duty;
78
79         /* we are in the linear zone */
80         duty  = (temp - linear_min_temp);
81         duty *= (therm->fan->bios.max_duty - therm->fan->bios.min_duty);
82         duty /= (linear_max_temp - linear_min_temp);
83         duty += therm->fan->bios.min_duty;
84         return duty;
85 }
86
87 static int
88 nvkm_therm_update_linear(struct nvkm_therm *therm)
89 {
90         u8  min = therm->fan->bios.linear_min_temp;
91         u8  max = therm->fan->bios.linear_max_temp;
92         return nvkm_therm_compute_linear_duty(therm, min, max);
93 }
94
95 static int
96 nvkm_therm_update_linear_fallback(struct nvkm_therm *therm)
97 {
98         u8 max = therm->bios_sensor.thrs_fan_boost.temp;
99         return nvkm_therm_compute_linear_duty(therm, 30, max);
100 }
101
102 static void
103 nvkm_therm_update(struct nvkm_therm *therm, int mode)
104 {
105         struct nvkm_subdev *subdev = &therm->subdev;
106         struct nvkm_timer *tmr = subdev->device->timer;
107         unsigned long flags;
108         bool immd = true;
109         bool poll = true;
110         int duty = -1;
111
112         spin_lock_irqsave(&therm->lock, flags);
113         if (mode < 0)
114                 mode = therm->mode;
115         therm->mode = mode;
116
117         switch (mode) {
118         case NVKM_THERM_CTRL_MANUAL:
119                 nvkm_timer_alarm(tmr, 0, &therm->alarm);
120                 duty = nvkm_therm_fan_get(therm);
121                 if (duty < 0)
122                         duty = 100;
123                 poll = false;
124                 break;
125         case NVKM_THERM_CTRL_AUTO:
126                 switch(therm->fan->bios.fan_mode) {
127                 case NVBIOS_THERM_FAN_TRIP:
128                         duty = nvkm_therm_update_trip(therm);
129                         break;
130                 case NVBIOS_THERM_FAN_LINEAR:
131                         duty = nvkm_therm_update_linear(therm);
132                         break;
133                 case NVBIOS_THERM_FAN_OTHER:
134                         if (therm->cstate) {
135                                 duty = therm->cstate;
136                                 poll = false;
137                         } else {
138                                 duty = nvkm_therm_update_linear_fallback(therm);
139                         }
140                         break;
141                 }
142                 immd = false;
143                 break;
144         case NVKM_THERM_CTRL_NONE:
145         default:
146                 nvkm_timer_alarm(tmr, 0, &therm->alarm);
147                 poll = false;
148         }
149
150         if (poll)
151                 nvkm_timer_alarm(tmr, 1000000000ULL, &therm->alarm);
152         spin_unlock_irqrestore(&therm->lock, flags);
153
154         if (duty >= 0) {
155                 nvkm_debug(subdev, "FAN target request: %d%%\n", duty);
156                 nvkm_therm_fan_set(therm, immd, duty);
157         }
158 }
159
160 int
161 nvkm_therm_cstate(struct nvkm_therm *therm, int fan, int dir)
162 {
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);
167                 therm->cstate = fan;
168                 nvkm_therm_update(therm, -1);
169         }
170         return 0;
171 }
172
173 static void
174 nvkm_therm_alarm(struct nvkm_alarm *alarm)
175 {
176         struct nvkm_therm *therm =
177                container_of(alarm, struct nvkm_therm, alarm);
178         nvkm_therm_update(therm, -1);
179 }
180
181 int
182 nvkm_therm_fan_mode(struct nvkm_therm *therm, int mode)
183 {
184         struct nvkm_subdev *subdev = &therm->subdev;
185         struct nvkm_device *device = subdev->device;
186         static const char *name[] = {
187                 "disabled",
188                 "manual",
189                 "automatic"
190         };
191
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 &&
195              !device->pmu))
196                 return -EINVAL;
197
198         /* do not allow automatic fan management if the thermal sensor is
199          * not available */
200         if (mode == NVKM_THERM_CTRL_AUTO &&
201             therm->func->temp_get(therm) < 0)
202                 return -EINVAL;
203
204         if (therm->mode == mode)
205                 return 0;
206
207         nvkm_debug(subdev, "fan management: %s\n", name[mode]);
208         nvkm_therm_update(therm, mode);
209         return 0;
210 }
211
212 int
213 nvkm_therm_attr_get(struct nvkm_therm *therm, enum nvkm_therm_attr_type type)
214 {
215         switch (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:
221                 return therm->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;
238         }
239
240         return -EINVAL;
241 }
242
243 int
244 nvkm_therm_attr_set(struct nvkm_therm *therm,
245                     enum nvkm_therm_attr_type type, int value)
246 {
247         switch (type) {
248         case NVKM_THERM_ATTR_FAN_MIN_DUTY:
249                 if (value < 0)
250                         value = 0;
251                 if (value > therm->fan->bios.max_duty)
252                         value = therm->fan->bios.max_duty;
253                 therm->fan->bios.min_duty = value;
254                 return 0;
255         case NVKM_THERM_ATTR_FAN_MAX_DUTY:
256                 if (value < 0)
257                         value = 0;
258                 if (value < therm->fan->bios.min_duty)
259                         value = therm->fan->bios.min_duty;
260                 therm->fan->bios.max_duty = value;
261                 return 0;
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);
267                 return 0;
268         case NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST:
269                 therm->bios_sensor.thrs_fan_boost.hysteresis = value;
270                 therm->func->program_alarms(therm);
271                 return 0;
272         case NVKM_THERM_ATTR_THRS_DOWN_CLK:
273                 therm->bios_sensor.thrs_down_clock.temp = value;
274                 therm->func->program_alarms(therm);
275                 return 0;
276         case NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST:
277                 therm->bios_sensor.thrs_down_clock.hysteresis = value;
278                 therm->func->program_alarms(therm);
279                 return 0;
280         case NVKM_THERM_ATTR_THRS_CRITICAL:
281                 therm->bios_sensor.thrs_critical.temp = value;
282                 therm->func->program_alarms(therm);
283                 return 0;
284         case NVKM_THERM_ATTR_THRS_CRITICAL_HYST:
285                 therm->bios_sensor.thrs_critical.hysteresis = value;
286                 therm->func->program_alarms(therm);
287                 return 0;
288         case NVKM_THERM_ATTR_THRS_SHUTDOWN:
289                 therm->bios_sensor.thrs_shutdown.temp = value;
290                 therm->func->program_alarms(therm);
291                 return 0;
292         case NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST:
293                 therm->bios_sensor.thrs_shutdown.hysteresis = value;
294                 therm->func->program_alarms(therm);
295                 return 0;
296         }
297
298         return -EINVAL;
299 }
300
301 static void
302 nvkm_therm_intr(struct nvkm_subdev *subdev)
303 {
304         struct nvkm_therm *therm = nvkm_therm(subdev);
305         if (therm->func->intr)
306                 therm->func->intr(therm);
307 }
308
309 static int
310 nvkm_therm_fini(struct nvkm_subdev *subdev, bool suspend)
311 {
312         struct nvkm_therm *therm = nvkm_therm(subdev);
313
314         if (therm->func->fini)
315                 therm->func->fini(therm);
316
317         nvkm_therm_fan_fini(therm, suspend);
318         nvkm_therm_sensor_fini(therm, suspend);
319
320         if (suspend) {
321                 therm->suspend = therm->mode;
322                 therm->mode = NVKM_THERM_CTRL_NONE;
323         }
324
325         return 0;
326 }
327
328 static int
329 nvkm_therm_oneinit(struct nvkm_subdev *subdev)
330 {
331         struct nvkm_therm *therm = nvkm_therm(subdev);
332         nvkm_therm_sensor_ctor(therm);
333         nvkm_therm_ic_ctor(therm);
334         nvkm_therm_fan_ctor(therm);
335         nvkm_therm_fan_mode(therm, NVKM_THERM_CTRL_AUTO);
336         nvkm_therm_sensor_preinit(therm);
337         return 0;
338 }
339
340 static int
341 nvkm_therm_init(struct nvkm_subdev *subdev)
342 {
343         struct nvkm_therm *therm = nvkm_therm(subdev);
344
345         therm->func->init(therm);
346
347         if (therm->suspend >= 0) {
348                 /* restore the pwm value only when on manual or auto mode */
349                 if (therm->suspend > 0)
350                         nvkm_therm_fan_set(therm, true, therm->fan->percent);
351
352                 nvkm_therm_fan_mode(therm, therm->suspend);
353         }
354
355         nvkm_therm_sensor_init(therm);
356         nvkm_therm_fan_init(therm);
357         return 0;
358 }
359
360 static void *
361 nvkm_therm_dtor(struct nvkm_subdev *subdev)
362 {
363         struct nvkm_therm *therm = nvkm_therm(subdev);
364         kfree(therm->fan);
365         return therm;
366 }
367
368 static const struct nvkm_subdev_func
369 nvkm_therm = {
370         .dtor = nvkm_therm_dtor,
371         .oneinit = nvkm_therm_oneinit,
372         .init = nvkm_therm_init,
373         .fini = nvkm_therm_fini,
374         .intr = nvkm_therm_intr,
375 };
376
377 int
378 nvkm_therm_new_(const struct nvkm_therm_func *func, struct nvkm_device *device,
379                 int index, struct nvkm_therm **ptherm)
380 {
381         struct nvkm_therm *therm;
382
383         if (!(therm = *ptherm = kzalloc(sizeof(*therm), GFP_KERNEL)))
384                 return -ENOMEM;
385
386         nvkm_subdev_ctor(&nvkm_therm, device, index, &therm->subdev);
387         therm->func = func;
388
389         nvkm_alarm_init(&therm->alarm, nvkm_therm_alarm);
390         spin_lock_init(&therm->lock);
391         spin_lock_init(&therm->sensor.alarm_program_lock);
392
393         therm->fan_get = nvkm_therm_fan_user_get;
394         therm->fan_set = nvkm_therm_fan_user_set;
395         therm->attr_get = nvkm_therm_attr_get;
396         therm->attr_set = nvkm_therm_attr_set;
397         therm->mode = therm->suspend = -1; /* undefined */
398         return 0;
399 }