]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/gpu/drm/nouveau/core/subdev/therm/base.c
Merge tag 'pm+acpi-3.15-rc1-3' of git://git.kernel.org/pub/scm/linux/kernel/git/rafae...
[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;
682b1fc7 95 bool immd = true;
36faa2fc 96 bool poll = true;
682b1fc7 97 int duty = -1;
694472f4
MP
98
99 spin_lock_irqsave(&priv->lock, flags);
100 if (mode < 0)
101 mode = priv->mode;
102 priv->mode = mode;
103
104 switch (mode) {
1a22274b 105 case NOUVEAU_THERM_CTRL_MANUAL:
c4a62a76 106 ptimer->alarm_cancel(ptimer, &priv->alarm);
1a22274b
BS
107 duty = nouveau_therm_fan_get(therm);
108 if (duty < 0)
109 duty = 100;
36faa2fc 110 poll = false;
694472f4 111 break;
1a22274b 112 case NOUVEAU_THERM_CTRL_AUTO:
0e994d64
MP
113 switch(priv->fan->bios.fan_mode) {
114 case NVBIOS_THERM_FAN_TRIP:
694472f4 115 duty = nouveau_therm_update_trip(therm);
0e994d64
MP
116 break;
117 case NVBIOS_THERM_FAN_LINEAR:
694472f4 118 duty = nouveau_therm_update_linear(therm);
0e994d64
MP
119 break;
120 case NVBIOS_THERM_FAN_OTHER:
09dacc7b
BS
121 if (priv->cstate)
122 duty = priv->cstate;
36faa2fc 123 poll = false;
0e994d64 124 break;
36faa2fc 125 }
682b1fc7 126 immd = false;
694472f4 127 break;
1a22274b 128 case NOUVEAU_THERM_CTRL_NONE:
694472f4 129 default:
c4a62a76 130 ptimer->alarm_cancel(ptimer, &priv->alarm);
36faa2fc 131 poll = false;
694472f4
MP
132 }
133
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);
682b1fc7
BS
137
138 if (duty >= 0) {
139 nv_debug(therm, "FAN target request: %d%%\n", duty);
140 nouveau_therm_fan_set(therm, immd, duty);
141 }
694472f4
MP
142}
143
6387e2cb
BS
144int
145nouveau_therm_cstate(struct nouveau_therm *ptherm, int fan, int dir)
146{
147 struct nouveau_therm_priv *priv = (void *)ptherm;
148 if (!dir || (dir < 0 && fan < priv->cstate) ||
149 (dir > 0 && fan > priv->cstate)) {
150 nv_debug(ptherm, "default fan speed -> %d%%\n", fan);
151 priv->cstate = fan;
152 nouveau_therm_update(ptherm, -1);
153 }
154 return 0;
155}
156
694472f4
MP
157static void
158nouveau_therm_alarm(struct nouveau_alarm *alarm)
159{
160 struct nouveau_therm_priv *priv =
161 container_of(alarm, struct nouveau_therm_priv, alarm);
162 nouveau_therm_update(&priv->base, -1);
163}
164
0083b91d 165int
c4ce9246 166nouveau_therm_fan_mode(struct nouveau_therm *therm, int mode)
694472f4
MP
167{
168 struct nouveau_therm_priv *priv = (void *)therm;
1a22274b
BS
169 struct nouveau_device *device = nv_device(therm);
170 static const char *name[] = {
171 "disabled",
172 "manual",
173 "automatic"
174 };
694472f4 175
09b8d73b 176 /* The default PPWR ucode on fermi interferes with fan management */
1a22274b 177 if ((mode >= ARRAY_SIZE(name)) ||
09b8d73b
BS
178 (mode != NOUVEAU_THERM_CTRL_NONE && device->card_type >= NV_C0 &&
179 !nouveau_subdev(device, NVDEV_SUBDEV_PWR)))
694472f4
MP
180 return -EINVAL;
181
98ee7c7c
MP
182 /* do not allow automatic fan management if the thermal sensor is
183 * not available */
dcd9262b 184 if (mode == NOUVEAU_THERM_CTRL_AUTO && therm->temp_get(therm) < 0)
98ee7c7c
MP
185 return -EINVAL;
186
1a22274b
BS
187 if (priv->mode == mode)
188 return 0;
694472f4 189
c4ce9246 190 nv_info(therm, "fan management: %s\n", name[mode]);
694472f4
MP
191 nouveau_therm_update(therm, mode);
192 return 0;
193}
194
aa1b9b48
MP
195int
196nouveau_therm_attr_get(struct nouveau_therm *therm,
197 enum nouveau_therm_attr_type type)
198{
199 struct nouveau_therm_priv *priv = (void *)therm;
200
201 switch (type) {
202 case NOUVEAU_THERM_ATTR_FAN_MIN_DUTY:
9c3bd3a5 203 return priv->fan->bios.min_duty;
aa1b9b48 204 case NOUVEAU_THERM_ATTR_FAN_MAX_DUTY:
9c3bd3a5 205 return priv->fan->bios.max_duty;
2f951a5d 206 case NOUVEAU_THERM_ATTR_FAN_MODE:
694472f4 207 return priv->mode;
aa1b9b48
MP
208 case NOUVEAU_THERM_ATTR_THRS_FAN_BOOST:
209 return priv->bios_sensor.thrs_fan_boost.temp;
210 case NOUVEAU_THERM_ATTR_THRS_FAN_BOOST_HYST:
211 return priv->bios_sensor.thrs_fan_boost.hysteresis;
212 case NOUVEAU_THERM_ATTR_THRS_DOWN_CLK:
213 return priv->bios_sensor.thrs_down_clock.temp;
214 case NOUVEAU_THERM_ATTR_THRS_DOWN_CLK_HYST:
215 return priv->bios_sensor.thrs_down_clock.hysteresis;
216 case NOUVEAU_THERM_ATTR_THRS_CRITICAL:
217 return priv->bios_sensor.thrs_critical.temp;
218 case NOUVEAU_THERM_ATTR_THRS_CRITICAL_HYST:
219 return priv->bios_sensor.thrs_critical.hysteresis;
220 case NOUVEAU_THERM_ATTR_THRS_SHUTDOWN:
221 return priv->bios_sensor.thrs_shutdown.temp;
222 case NOUVEAU_THERM_ATTR_THRS_SHUTDOWN_HYST:
223 return priv->bios_sensor.thrs_shutdown.hysteresis;
224 }
225
226 return -EINVAL;
227}
228
229int
230nouveau_therm_attr_set(struct nouveau_therm *therm,
231 enum nouveau_therm_attr_type type, int value)
232{
233 struct nouveau_therm_priv *priv = (void *)therm;
234
235 switch (type) {
236 case NOUVEAU_THERM_ATTR_FAN_MIN_DUTY:
237 if (value < 0)
238 value = 0;
9c3bd3a5
BS
239 if (value > priv->fan->bios.max_duty)
240 value = priv->fan->bios.max_duty;
241 priv->fan->bios.min_duty = value;
aa1b9b48
MP
242 return 0;
243 case NOUVEAU_THERM_ATTR_FAN_MAX_DUTY:
244 if (value < 0)
245 value = 0;
9c3bd3a5
BS
246 if (value < priv->fan->bios.min_duty)
247 value = priv->fan->bios.min_duty;
248 priv->fan->bios.max_duty = value;
aa1b9b48 249 return 0;
2f951a5d 250 case NOUVEAU_THERM_ATTR_FAN_MODE:
c4ce9246 251 return nouveau_therm_fan_mode(therm, value);
aa1b9b48
MP
252 case NOUVEAU_THERM_ATTR_THRS_FAN_BOOST:
253 priv->bios_sensor.thrs_fan_boost.temp = value;
0083b91d 254 priv->sensor.program_alarms(therm);
aa1b9b48
MP
255 return 0;
256 case NOUVEAU_THERM_ATTR_THRS_FAN_BOOST_HYST:
257 priv->bios_sensor.thrs_fan_boost.hysteresis = value;
0083b91d 258 priv->sensor.program_alarms(therm);
aa1b9b48
MP
259 return 0;
260 case NOUVEAU_THERM_ATTR_THRS_DOWN_CLK:
261 priv->bios_sensor.thrs_down_clock.temp = value;
0083b91d 262 priv->sensor.program_alarms(therm);
aa1b9b48
MP
263 return 0;
264 case NOUVEAU_THERM_ATTR_THRS_DOWN_CLK_HYST:
265 priv->bios_sensor.thrs_down_clock.hysteresis = value;
0083b91d 266 priv->sensor.program_alarms(therm);
aa1b9b48
MP
267 return 0;
268 case NOUVEAU_THERM_ATTR_THRS_CRITICAL:
269 priv->bios_sensor.thrs_critical.temp = value;
0083b91d 270 priv->sensor.program_alarms(therm);
aa1b9b48
MP
271 return 0;
272 case NOUVEAU_THERM_ATTR_THRS_CRITICAL_HYST:
273 priv->bios_sensor.thrs_critical.hysteresis = value;
0083b91d 274 priv->sensor.program_alarms(therm);
aa1b9b48
MP
275 return 0;
276 case NOUVEAU_THERM_ATTR_THRS_SHUTDOWN:
277 priv->bios_sensor.thrs_shutdown.temp = value;
0083b91d 278 priv->sensor.program_alarms(therm);
aa1b9b48
MP
279 return 0;
280 case NOUVEAU_THERM_ATTR_THRS_SHUTDOWN_HYST:
281 priv->bios_sensor.thrs_shutdown.hysteresis = value;
0083b91d 282 priv->sensor.program_alarms(therm);
aa1b9b48
MP
283 return 0;
284 }
285
286 return -EINVAL;
287}
288
289int
5f066c32 290_nouveau_therm_init(struct nouveau_object *object)
aa1b9b48
MP
291{
292 struct nouveau_therm *therm = (void *)object;
293 struct nouveau_therm_priv *priv = (void *)therm;
294 int ret;
295
296 ret = nouveau_subdev_init(&therm->base);
297 if (ret)
298 return ret;
299
4cc00ad1
MP
300 if (priv->suspend >= 0) {
301 /* restore the pwm value only when on manual or auto mode */
302 if (priv->suspend > 0)
303 nouveau_therm_fan_set(therm, true, priv->fan->percent);
304
ffb8ea8a 305 nouveau_therm_fan_mode(therm, priv->suspend);
4cc00ad1 306 }
c4a62a76
MP
307 nouveau_therm_sensor_init(therm);
308 nouveau_therm_fan_init(therm);
aa1b9b48
MP
309 return 0;
310}
311
312int
5f066c32 313_nouveau_therm_fini(struct nouveau_object *object, bool suspend)
aa1b9b48
MP
314{
315 struct nouveau_therm *therm = (void *)object;
316 struct nouveau_therm_priv *priv = (void *)therm;
317
c4a62a76
MP
318 nouveau_therm_fan_fini(therm, suspend);
319 nouveau_therm_sensor_fini(therm, suspend);
1a22274b
BS
320 if (suspend) {
321 priv->suspend = priv->mode;
322 priv->mode = NOUVEAU_THERM_CTRL_NONE;
323 }
aa1b9b48
MP
324
325 return nouveau_subdev_fini(&therm->base, suspend);
326}
5f066c32
BS
327
328int
329nouveau_therm_create_(struct nouveau_object *parent,
330 struct nouveau_object *engine,
331 struct nouveau_oclass *oclass,
332 int length, void **pobject)
333{
334 struct nouveau_therm_priv *priv;
335 int ret;
336
337 ret = nouveau_subdev_create_(parent, engine, oclass, 0, "PTHERM",
338 "therm", length, pobject);
339 priv = *pobject;
340 if (ret)
341 return ret;
342
694472f4
MP
343 nouveau_alarm_init(&priv->alarm, nouveau_therm_alarm);
344 spin_lock_init(&priv->lock);
3969f05b 345 spin_lock_init(&priv->sensor.alarm_program_lock);
694472f4 346
5f066c32
BS
347 priv->base.fan_get = nouveau_therm_fan_user_get;
348 priv->base.fan_set = nouveau_therm_fan_user_set;
349 priv->base.fan_sense = nouveau_therm_fan_sense;
350 priv->base.attr_get = nouveau_therm_attr_get;
351 priv->base.attr_set = nouveau_therm_attr_set;
1a22274b 352 priv->mode = priv->suspend = -1; /* undefined */
5f066c32
BS
353 return 0;
354}
9c3bd3a5
BS
355
356int
357nouveau_therm_preinit(struct nouveau_therm *therm)
358{
9c3bd3a5 359 nouveau_therm_sensor_ctor(therm);
13506e2a 360 nouveau_therm_ic_ctor(therm);
9c3bd3a5 361 nouveau_therm_fan_ctor(therm);
1a22274b 362
208cf0b7 363 nouveau_therm_fan_mode(therm, NOUVEAU_THERM_CTRL_AUTO);
0b3ee377 364 nouveau_therm_sensor_preinit(therm);
9c3bd3a5
BS
365 return 0;
366}
367
368void
369_nouveau_therm_dtor(struct nouveau_object *object)
370{
371 struct nouveau_therm_priv *priv = (void *)object;
372 kfree(priv->fan);
373 nouveau_subdev_destroy(&priv->base.base);
374}