]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/gpu/drm/nouveau/core/subdev/therm/base.c
drm/nv40/therm: reserve negative temperatures for errors
[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;
95 int duty;
96
97 spin_lock_irqsave(&priv->lock, flags);
98 if (mode < 0)
99 mode = priv->mode;
100 priv->mode = mode;
101
102 switch (mode) {
1a22274b
BS
103 case NOUVEAU_THERM_CTRL_MANUAL:
104 duty = nouveau_therm_fan_get(therm);
105 if (duty < 0)
106 duty = 100;
694472f4 107 break;
1a22274b 108 case NOUVEAU_THERM_CTRL_AUTO:
694472f4
MP
109 if (priv->fan->bios.nr_fan_trip)
110 duty = nouveau_therm_update_trip(therm);
111 else
112 duty = nouveau_therm_update_linear(therm);
113 break;
1a22274b 114 case NOUVEAU_THERM_CTRL_NONE:
694472f4
MP
115 default:
116 goto done;
117 }
118
1a22274b
BS
119 nv_debug(therm, "FAN target request: %d%%\n", duty);
120 nouveau_therm_fan_set(therm, (mode != NOUVEAU_THERM_CTRL_AUTO), duty);
694472f4
MP
121
122done:
1a22274b 123 if (list_empty(&priv->alarm.head) && (mode == NOUVEAU_THERM_CTRL_AUTO))
694472f4
MP
124 ptimer->alarm(ptimer, 1000000000ULL, &priv->alarm);
125 spin_unlock_irqrestore(&priv->lock, flags);
126}
127
128static void
129nouveau_therm_alarm(struct nouveau_alarm *alarm)
130{
131 struct nouveau_therm_priv *priv =
132 container_of(alarm, struct nouveau_therm_priv, alarm);
133 nouveau_therm_update(&priv->base, -1);
134}
135
0083b91d 136int
c4ce9246 137nouveau_therm_fan_mode(struct nouveau_therm *therm, int mode)
694472f4
MP
138{
139 struct nouveau_therm_priv *priv = (void *)therm;
1a22274b
BS
140 struct nouveau_device *device = nv_device(therm);
141 static const char *name[] = {
142 "disabled",
143 "manual",
144 "automatic"
145 };
694472f4
MP
146
147 /* The default PDAEMON ucode interferes with fan management */
1a22274b
BS
148 if ((mode >= ARRAY_SIZE(name)) ||
149 (mode != NOUVEAU_THERM_CTRL_NONE && device->card_type >= NV_C0))
694472f4
MP
150 return -EINVAL;
151
1a22274b
BS
152 if (priv->mode == mode)
153 return 0;
694472f4 154
c4ce9246 155 nv_info(therm, "fan management: %s\n", name[mode]);
694472f4
MP
156 nouveau_therm_update(therm, mode);
157 return 0;
158}
159
aa1b9b48
MP
160int
161nouveau_therm_attr_get(struct nouveau_therm *therm,
162 enum nouveau_therm_attr_type type)
163{
164 struct nouveau_therm_priv *priv = (void *)therm;
165
166 switch (type) {
167 case NOUVEAU_THERM_ATTR_FAN_MIN_DUTY:
9c3bd3a5 168 return priv->fan->bios.min_duty;
aa1b9b48 169 case NOUVEAU_THERM_ATTR_FAN_MAX_DUTY:
9c3bd3a5 170 return priv->fan->bios.max_duty;
2f951a5d 171 case NOUVEAU_THERM_ATTR_FAN_MODE:
694472f4 172 return priv->mode;
aa1b9b48
MP
173 case NOUVEAU_THERM_ATTR_THRS_FAN_BOOST:
174 return priv->bios_sensor.thrs_fan_boost.temp;
175 case NOUVEAU_THERM_ATTR_THRS_FAN_BOOST_HYST:
176 return priv->bios_sensor.thrs_fan_boost.hysteresis;
177 case NOUVEAU_THERM_ATTR_THRS_DOWN_CLK:
178 return priv->bios_sensor.thrs_down_clock.temp;
179 case NOUVEAU_THERM_ATTR_THRS_DOWN_CLK_HYST:
180 return priv->bios_sensor.thrs_down_clock.hysteresis;
181 case NOUVEAU_THERM_ATTR_THRS_CRITICAL:
182 return priv->bios_sensor.thrs_critical.temp;
183 case NOUVEAU_THERM_ATTR_THRS_CRITICAL_HYST:
184 return priv->bios_sensor.thrs_critical.hysteresis;
185 case NOUVEAU_THERM_ATTR_THRS_SHUTDOWN:
186 return priv->bios_sensor.thrs_shutdown.temp;
187 case NOUVEAU_THERM_ATTR_THRS_SHUTDOWN_HYST:
188 return priv->bios_sensor.thrs_shutdown.hysteresis;
189 }
190
191 return -EINVAL;
192}
193
194int
195nouveau_therm_attr_set(struct nouveau_therm *therm,
196 enum nouveau_therm_attr_type type, int value)
197{
198 struct nouveau_therm_priv *priv = (void *)therm;
199
200 switch (type) {
201 case NOUVEAU_THERM_ATTR_FAN_MIN_DUTY:
202 if (value < 0)
203 value = 0;
9c3bd3a5
BS
204 if (value > priv->fan->bios.max_duty)
205 value = priv->fan->bios.max_duty;
206 priv->fan->bios.min_duty = value;
aa1b9b48
MP
207 return 0;
208 case NOUVEAU_THERM_ATTR_FAN_MAX_DUTY:
209 if (value < 0)
210 value = 0;
9c3bd3a5
BS
211 if (value < priv->fan->bios.min_duty)
212 value = priv->fan->bios.min_duty;
213 priv->fan->bios.max_duty = value;
aa1b9b48 214 return 0;
2f951a5d 215 case NOUVEAU_THERM_ATTR_FAN_MODE:
c4ce9246 216 return nouveau_therm_fan_mode(therm, value);
aa1b9b48
MP
217 case NOUVEAU_THERM_ATTR_THRS_FAN_BOOST:
218 priv->bios_sensor.thrs_fan_boost.temp = value;
0083b91d 219 priv->sensor.program_alarms(therm);
aa1b9b48
MP
220 return 0;
221 case NOUVEAU_THERM_ATTR_THRS_FAN_BOOST_HYST:
222 priv->bios_sensor.thrs_fan_boost.hysteresis = value;
0083b91d 223 priv->sensor.program_alarms(therm);
aa1b9b48
MP
224 return 0;
225 case NOUVEAU_THERM_ATTR_THRS_DOWN_CLK:
226 priv->bios_sensor.thrs_down_clock.temp = value;
0083b91d 227 priv->sensor.program_alarms(therm);
aa1b9b48
MP
228 return 0;
229 case NOUVEAU_THERM_ATTR_THRS_DOWN_CLK_HYST:
230 priv->bios_sensor.thrs_down_clock.hysteresis = value;
0083b91d 231 priv->sensor.program_alarms(therm);
aa1b9b48
MP
232 return 0;
233 case NOUVEAU_THERM_ATTR_THRS_CRITICAL:
234 priv->bios_sensor.thrs_critical.temp = value;
0083b91d 235 priv->sensor.program_alarms(therm);
aa1b9b48
MP
236 return 0;
237 case NOUVEAU_THERM_ATTR_THRS_CRITICAL_HYST:
238 priv->bios_sensor.thrs_critical.hysteresis = value;
0083b91d 239 priv->sensor.program_alarms(therm);
aa1b9b48
MP
240 return 0;
241 case NOUVEAU_THERM_ATTR_THRS_SHUTDOWN:
242 priv->bios_sensor.thrs_shutdown.temp = value;
0083b91d 243 priv->sensor.program_alarms(therm);
aa1b9b48
MP
244 return 0;
245 case NOUVEAU_THERM_ATTR_THRS_SHUTDOWN_HYST:
246 priv->bios_sensor.thrs_shutdown.hysteresis = value;
0083b91d 247 priv->sensor.program_alarms(therm);
aa1b9b48
MP
248 return 0;
249 }
250
251 return -EINVAL;
252}
253
254int
5f066c32 255_nouveau_therm_init(struct nouveau_object *object)
aa1b9b48
MP
256{
257 struct nouveau_therm *therm = (void *)object;
258 struct nouveau_therm_priv *priv = (void *)therm;
259 int ret;
260
261 ret = nouveau_subdev_init(&therm->base);
262 if (ret)
263 return ret;
264
1a22274b 265 if (priv->suspend >= 0)
c4ce9246 266 nouveau_therm_fan_mode(therm, priv->mode);
0083b91d 267 priv->sensor.program_alarms(therm);
aa1b9b48
MP
268 return 0;
269}
270
271int
5f066c32 272_nouveau_therm_fini(struct nouveau_object *object, bool suspend)
aa1b9b48
MP
273{
274 struct nouveau_therm *therm = (void *)object;
275 struct nouveau_therm_priv *priv = (void *)therm;
276
1a22274b
BS
277 if (suspend) {
278 priv->suspend = priv->mode;
279 priv->mode = NOUVEAU_THERM_CTRL_NONE;
280 }
aa1b9b48
MP
281
282 return nouveau_subdev_fini(&therm->base, suspend);
283}
5f066c32
BS
284
285int
286nouveau_therm_create_(struct nouveau_object *parent,
287 struct nouveau_object *engine,
288 struct nouveau_oclass *oclass,
289 int length, void **pobject)
290{
291 struct nouveau_therm_priv *priv;
292 int ret;
293
294 ret = nouveau_subdev_create_(parent, engine, oclass, 0, "PTHERM",
295 "therm", length, pobject);
296 priv = *pobject;
297 if (ret)
298 return ret;
299
694472f4
MP
300 nouveau_alarm_init(&priv->alarm, nouveau_therm_alarm);
301 spin_lock_init(&priv->lock);
3969f05b 302 spin_lock_init(&priv->sensor.alarm_program_lock);
694472f4 303
5f066c32
BS
304 priv->base.fan_get = nouveau_therm_fan_user_get;
305 priv->base.fan_set = nouveau_therm_fan_user_set;
306 priv->base.fan_sense = nouveau_therm_fan_sense;
307 priv->base.attr_get = nouveau_therm_attr_get;
308 priv->base.attr_set = nouveau_therm_attr_set;
1a22274b 309 priv->mode = priv->suspend = -1; /* undefined */
5f066c32
BS
310 return 0;
311}
9c3bd3a5
BS
312
313int
314nouveau_therm_preinit(struct nouveau_therm *therm)
315{
9c3bd3a5 316 nouveau_therm_sensor_ctor(therm);
13506e2a 317 nouveau_therm_ic_ctor(therm);
9c3bd3a5 318 nouveau_therm_fan_ctor(therm);
1a22274b 319
c4ce9246 320 nouveau_therm_fan_mode(therm, NOUVEAU_THERM_CTRL_NONE);
9c3bd3a5
BS
321 return 0;
322}
323
324void
325_nouveau_therm_dtor(struct nouveau_object *object)
326{
327 struct nouveau_therm_priv *priv = (void *)object;
328 kfree(priv->fan);
329 nouveau_subdev_destroy(&priv->base.base);
330}