]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/gpu/drm/nouveau/core/subdev/therm/base.c
drm/nouveau/therm: no toggle fan control either if we can't guarantee no pwm connected
[mirror_ubuntu-hirsute-kernel.git] / drivers / gpu / drm / nouveau / core / subdev / therm / base.c
CommitLineData
aa1b9b48
MP
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
25#include <core/object.h>
26#include <core/device.h>
27
28#include <subdev/bios.h>
29
30#include "priv.h"
31
694472f4
MP
32static int
33nouveau_therm_update_trip(struct nouveau_therm *therm)
34{
35 struct nouveau_therm_priv *priv = (void *)therm;
36 struct nouveau_therm_trip_point *trip = priv->fan->bios.trip,
37 *cur_trip = NULL,
38 *last_trip = priv->last_trip;
39 u8 temp = therm->temp_get(therm);
40 u16 duty, i;
41
42 /* look for the trip point corresponding to the current temperature */
43 cur_trip = NULL;
44 for (i = 0; i < priv->fan->bios.nr_fan_trip; i++) {
45 if (temp >= trip[i].temp)
46 cur_trip = &trip[i];
47 }
48
49 /* account for the hysteresis cycle */
50 if (last_trip && temp <= (last_trip->temp) &&
51 temp > (last_trip->temp - last_trip->hysteresis))
52 cur_trip = last_trip;
53
54 if (cur_trip) {
55 duty = cur_trip->fan_duty;
56 priv->last_trip = cur_trip;
57 } else {
58 duty = 0;
59 priv->last_trip = NULL;
60 }
61
62 return duty;
63}
64
65static int
66nouveau_therm_update_linear(struct nouveau_therm *therm)
67{
68 struct nouveau_therm_priv *priv = (void *)therm;
69 u8 linear_min_temp = priv->fan->bios.linear_min_temp;
70 u8 linear_max_temp = priv->fan->bios.linear_max_temp;
71 u8 temp = therm->temp_get(therm);
72 u16 duty;
73
a624bafb
MP
74 /* handle the non-linear part first */
75 if (temp < linear_min_temp)
76 return priv->fan->bios.min_duty;
77 else if (temp > linear_max_temp)
78 return priv->fan->bios.max_duty;
79
80 /* we are in the linear zone */
694472f4
MP
81 duty = (temp - linear_min_temp);
82 duty *= (priv->fan->bios.max_duty - priv->fan->bios.min_duty);
83 duty /= (linear_max_temp - linear_min_temp);
84 duty += priv->fan->bios.min_duty;
85
86 return duty;
87}
88
89static void
90nouveau_therm_update(struct nouveau_therm *therm, int mode)
91{
92 struct nouveau_timer *ptimer = nouveau_timer(therm);
93 struct nouveau_therm_priv *priv = (void *)therm;
94 unsigned long flags;
36faa2fc 95 bool poll = true;
694472f4
MP
96 int duty;
97
98 spin_lock_irqsave(&priv->lock, flags);
99 if (mode < 0)
100 mode = priv->mode;
101 priv->mode = mode;
102
103 switch (mode) {
1a22274b 104 case NOUVEAU_THERM_CTRL_MANUAL:
c4a62a76 105 ptimer->alarm_cancel(ptimer, &priv->alarm);
1a22274b
BS
106 duty = nouveau_therm_fan_get(therm);
107 if (duty < 0)
108 duty = 100;
36faa2fc 109 poll = false;
694472f4 110 break;
1a22274b 111 case NOUVEAU_THERM_CTRL_AUTO:
36faa2fc 112 if (priv->fan->bios.nr_fan_trip) {
694472f4 113 duty = nouveau_therm_update_trip(therm);
36faa2fc
BS
114 } else
115 if (priv->fan->bios.linear_min_temp ||
116 priv->fan->bios.linear_max_temp) {
694472f4 117 duty = nouveau_therm_update_linear(therm);
36faa2fc
BS
118 } else {
119 duty = priv->cstate;
120 poll = false;
121 }
694472f4 122 break;
1a22274b 123 case NOUVEAU_THERM_CTRL_NONE:
694472f4 124 default:
c4a62a76 125 ptimer->alarm_cancel(ptimer, &priv->alarm);
36faa2fc 126 poll = false;
694472f4
MP
127 goto done;
128 }
129
1a22274b
BS
130 nv_debug(therm, "FAN target request: %d%%\n", duty);
131 nouveau_therm_fan_set(therm, (mode != NOUVEAU_THERM_CTRL_AUTO), duty);
694472f4
MP
132
133done:
36faa2fc 134 if (list_empty(&priv->alarm.head) && poll)
694472f4
MP
135 ptimer->alarm(ptimer, 1000000000ULL, &priv->alarm);
136 spin_unlock_irqrestore(&priv->lock, flags);
137}
138
6387e2cb
BS
139int
140nouveau_therm_cstate(struct nouveau_therm *ptherm, int fan, int dir)
141{
142 struct nouveau_therm_priv *priv = (void *)ptherm;
143 if (!dir || (dir < 0 && fan < priv->cstate) ||
144 (dir > 0 && fan > priv->cstate)) {
145 nv_debug(ptherm, "default fan speed -> %d%%\n", fan);
146 priv->cstate = fan;
147 nouveau_therm_update(ptherm, -1);
148 }
149 return 0;
150}
151
694472f4
MP
152static void
153nouveau_therm_alarm(struct nouveau_alarm *alarm)
154{
155 struct nouveau_therm_priv *priv =
156 container_of(alarm, struct nouveau_therm_priv, alarm);
157 nouveau_therm_update(&priv->base, -1);
158}
159
0083b91d 160int
c4ce9246 161nouveau_therm_fan_mode(struct nouveau_therm *therm, int mode)
694472f4
MP
162{
163 struct nouveau_therm_priv *priv = (void *)therm;
1a22274b
BS
164 struct nouveau_device *device = nv_device(therm);
165 static const char *name[] = {
166 "disabled",
167 "manual",
168 "automatic"
169 };
694472f4 170
09b8d73b 171 /* The default PPWR ucode on fermi interferes with fan management */
1a22274b 172 if ((mode >= ARRAY_SIZE(name)) ||
09b8d73b
BS
173 (mode != NOUVEAU_THERM_CTRL_NONE && device->card_type >= NV_C0 &&
174 !nouveau_subdev(device, NVDEV_SUBDEV_PWR)))
694472f4
MP
175 return -EINVAL;
176
98ee7c7c
MP
177 /* do not allow automatic fan management if the thermal sensor is
178 * not available */
09b8d73b 179 if (priv->mode == NOUVEAU_THERM_CTRL_AUTO && therm->temp_get(therm) < 0)
98ee7c7c
MP
180 return -EINVAL;
181
1a22274b
BS
182 if (priv->mode == mode)
183 return 0;
694472f4 184
c4ce9246 185 nv_info(therm, "fan management: %s\n", name[mode]);
694472f4
MP
186 nouveau_therm_update(therm, mode);
187 return 0;
188}
189
aa1b9b48
MP
190int
191nouveau_therm_attr_get(struct nouveau_therm *therm,
192 enum nouveau_therm_attr_type type)
193{
194 struct nouveau_therm_priv *priv = (void *)therm;
195
196 switch (type) {
197 case NOUVEAU_THERM_ATTR_FAN_MIN_DUTY:
9c3bd3a5 198 return priv->fan->bios.min_duty;
aa1b9b48 199 case NOUVEAU_THERM_ATTR_FAN_MAX_DUTY:
9c3bd3a5 200 return priv->fan->bios.max_duty;
2f951a5d 201 case NOUVEAU_THERM_ATTR_FAN_MODE:
694472f4 202 return priv->mode;
aa1b9b48
MP
203 case NOUVEAU_THERM_ATTR_THRS_FAN_BOOST:
204 return priv->bios_sensor.thrs_fan_boost.temp;
205 case NOUVEAU_THERM_ATTR_THRS_FAN_BOOST_HYST:
206 return priv->bios_sensor.thrs_fan_boost.hysteresis;
207 case NOUVEAU_THERM_ATTR_THRS_DOWN_CLK:
208 return priv->bios_sensor.thrs_down_clock.temp;
209 case NOUVEAU_THERM_ATTR_THRS_DOWN_CLK_HYST:
210 return priv->bios_sensor.thrs_down_clock.hysteresis;
211 case NOUVEAU_THERM_ATTR_THRS_CRITICAL:
212 return priv->bios_sensor.thrs_critical.temp;
213 case NOUVEAU_THERM_ATTR_THRS_CRITICAL_HYST:
214 return priv->bios_sensor.thrs_critical.hysteresis;
215 case NOUVEAU_THERM_ATTR_THRS_SHUTDOWN:
216 return priv->bios_sensor.thrs_shutdown.temp;
217 case NOUVEAU_THERM_ATTR_THRS_SHUTDOWN_HYST:
218 return priv->bios_sensor.thrs_shutdown.hysteresis;
219 }
220
221 return -EINVAL;
222}
223
224int
225nouveau_therm_attr_set(struct nouveau_therm *therm,
226 enum nouveau_therm_attr_type type, int value)
227{
228 struct nouveau_therm_priv *priv = (void *)therm;
229
230 switch (type) {
231 case NOUVEAU_THERM_ATTR_FAN_MIN_DUTY:
232 if (value < 0)
233 value = 0;
9c3bd3a5
BS
234 if (value > priv->fan->bios.max_duty)
235 value = priv->fan->bios.max_duty;
236 priv->fan->bios.min_duty = value;
aa1b9b48
MP
237 return 0;
238 case NOUVEAU_THERM_ATTR_FAN_MAX_DUTY:
239 if (value < 0)
240 value = 0;
9c3bd3a5
BS
241 if (value < priv->fan->bios.min_duty)
242 value = priv->fan->bios.min_duty;
243 priv->fan->bios.max_duty = value;
aa1b9b48 244 return 0;
2f951a5d 245 case NOUVEAU_THERM_ATTR_FAN_MODE:
c4ce9246 246 return nouveau_therm_fan_mode(therm, value);
aa1b9b48
MP
247 case NOUVEAU_THERM_ATTR_THRS_FAN_BOOST:
248 priv->bios_sensor.thrs_fan_boost.temp = value;
0083b91d 249 priv->sensor.program_alarms(therm);
aa1b9b48
MP
250 return 0;
251 case NOUVEAU_THERM_ATTR_THRS_FAN_BOOST_HYST:
252 priv->bios_sensor.thrs_fan_boost.hysteresis = value;
0083b91d 253 priv->sensor.program_alarms(therm);
aa1b9b48
MP
254 return 0;
255 case NOUVEAU_THERM_ATTR_THRS_DOWN_CLK:
256 priv->bios_sensor.thrs_down_clock.temp = value;
0083b91d 257 priv->sensor.program_alarms(therm);
aa1b9b48
MP
258 return 0;
259 case NOUVEAU_THERM_ATTR_THRS_DOWN_CLK_HYST:
260 priv->bios_sensor.thrs_down_clock.hysteresis = value;
0083b91d 261 priv->sensor.program_alarms(therm);
aa1b9b48
MP
262 return 0;
263 case NOUVEAU_THERM_ATTR_THRS_CRITICAL:
264 priv->bios_sensor.thrs_critical.temp = value;
0083b91d 265 priv->sensor.program_alarms(therm);
aa1b9b48
MP
266 return 0;
267 case NOUVEAU_THERM_ATTR_THRS_CRITICAL_HYST:
268 priv->bios_sensor.thrs_critical.hysteresis = value;
0083b91d 269 priv->sensor.program_alarms(therm);
aa1b9b48
MP
270 return 0;
271 case NOUVEAU_THERM_ATTR_THRS_SHUTDOWN:
272 priv->bios_sensor.thrs_shutdown.temp = value;
0083b91d 273 priv->sensor.program_alarms(therm);
aa1b9b48
MP
274 return 0;
275 case NOUVEAU_THERM_ATTR_THRS_SHUTDOWN_HYST:
276 priv->bios_sensor.thrs_shutdown.hysteresis = value;
0083b91d 277 priv->sensor.program_alarms(therm);
aa1b9b48
MP
278 return 0;
279 }
280
281 return -EINVAL;
282}
283
284int
5f066c32 285_nouveau_therm_init(struct nouveau_object *object)
aa1b9b48
MP
286{
287 struct nouveau_therm *therm = (void *)object;
288 struct nouveau_therm_priv *priv = (void *)therm;
289 int ret;
290
291 ret = nouveau_subdev_init(&therm->base);
292 if (ret)
293 return ret;
294
4cc00ad1
MP
295 if (priv->suspend >= 0) {
296 /* restore the pwm value only when on manual or auto mode */
297 if (priv->suspend > 0)
298 nouveau_therm_fan_set(therm, true, priv->fan->percent);
299
ffb8ea8a 300 nouveau_therm_fan_mode(therm, priv->suspend);
4cc00ad1 301 }
c4a62a76
MP
302 nouveau_therm_sensor_init(therm);
303 nouveau_therm_fan_init(therm);
aa1b9b48
MP
304 return 0;
305}
306
307int
5f066c32 308_nouveau_therm_fini(struct nouveau_object *object, bool suspend)
aa1b9b48
MP
309{
310 struct nouveau_therm *therm = (void *)object;
311 struct nouveau_therm_priv *priv = (void *)therm;
312
c4a62a76
MP
313 nouveau_therm_fan_fini(therm, suspend);
314 nouveau_therm_sensor_fini(therm, suspend);
1a22274b
BS
315 if (suspend) {
316 priv->suspend = priv->mode;
317 priv->mode = NOUVEAU_THERM_CTRL_NONE;
318 }
aa1b9b48
MP
319
320 return nouveau_subdev_fini(&therm->base, suspend);
321}
5f066c32
BS
322
323int
324nouveau_therm_create_(struct nouveau_object *parent,
325 struct nouveau_object *engine,
326 struct nouveau_oclass *oclass,
327 int length, void **pobject)
328{
329 struct nouveau_therm_priv *priv;
330 int ret;
331
332 ret = nouveau_subdev_create_(parent, engine, oclass, 0, "PTHERM",
333 "therm", length, pobject);
334 priv = *pobject;
335 if (ret)
336 return ret;
337
694472f4
MP
338 nouveau_alarm_init(&priv->alarm, nouveau_therm_alarm);
339 spin_lock_init(&priv->lock);
3969f05b 340 spin_lock_init(&priv->sensor.alarm_program_lock);
694472f4 341
5f066c32
BS
342 priv->base.fan_get = nouveau_therm_fan_user_get;
343 priv->base.fan_set = nouveau_therm_fan_user_set;
344 priv->base.fan_sense = nouveau_therm_fan_sense;
345 priv->base.attr_get = nouveau_therm_attr_get;
346 priv->base.attr_set = nouveau_therm_attr_set;
1a22274b 347 priv->mode = priv->suspend = -1; /* undefined */
5f066c32
BS
348 return 0;
349}
9c3bd3a5
BS
350
351int
352nouveau_therm_preinit(struct nouveau_therm *therm)
353{
9c3bd3a5 354 nouveau_therm_sensor_ctor(therm);
13506e2a 355 nouveau_therm_ic_ctor(therm);
9c3bd3a5 356 nouveau_therm_fan_ctor(therm);
1a22274b 357
c4ce9246 358 nouveau_therm_fan_mode(therm, NOUVEAU_THERM_CTRL_NONE);
0b3ee377 359 nouveau_therm_sensor_preinit(therm);
9c3bd3a5
BS
360 return 0;
361}
362
363void
364_nouveau_therm_dtor(struct nouveau_object *object)
365{
366 struct nouveau_therm_priv *priv = (void *)object;
367 kfree(priv->fan);
368 nouveau_subdev_destroy(&priv->base.base);
369}