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