Merge branch 'drm-armada-devel' of git://ftp.arm.linux.org.uk/~rmk/linux-arm into...
[deliverable/linux.git] / drivers / gpu / drm / nouveau / nvkm / subdev / therm / temp.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 */
aa1b9b48
MP
24#include "priv.h"
25
aa1b9b48 26static void
e1404611 27nvkm_therm_temp_set_defaults(struct nvkm_therm *therm)
aa1b9b48 28{
e1404611 29 struct nvkm_therm_priv *priv = (void *)therm;
aa1b9b48 30
aa1b9b48
MP
31 priv->bios_sensor.offset_constant = 0;
32
33 priv->bios_sensor.thrs_fan_boost.temp = 90;
34 priv->bios_sensor.thrs_fan_boost.hysteresis = 3;
35
36 priv->bios_sensor.thrs_down_clock.temp = 95;
37 priv->bios_sensor.thrs_down_clock.hysteresis = 3;
38
39 priv->bios_sensor.thrs_critical.temp = 105;
40 priv->bios_sensor.thrs_critical.hysteresis = 5;
41
42 priv->bios_sensor.thrs_shutdown.temp = 135;
43 priv->bios_sensor.thrs_shutdown.hysteresis = 5; /*not that it matters */
44}
45
46
47static void
e1404611 48nvkm_therm_temp_safety_checks(struct nvkm_therm *therm)
aa1b9b48 49{
e1404611 50 struct nvkm_therm_priv *priv = (void *)therm;
3600ad5e 51 struct nvbios_therm_sensor *s = &priv->bios_sensor;
aa1b9b48 52
3600ad5e
MP
53 /* enforce a minimum hysteresis on thresholds */
54 s->thrs_fan_boost.hysteresis = max_t(u8, s->thrs_fan_boost.hysteresis, 2);
55 s->thrs_down_clock.hysteresis = max_t(u8, s->thrs_down_clock.hysteresis, 2);
56 s->thrs_critical.hysteresis = max_t(u8, s->thrs_critical.hysteresis, 2);
57 s->thrs_shutdown.hysteresis = max_t(u8, s->thrs_shutdown.hysteresis, 2);
aa1b9b48
MP
58}
59
0083b91d 60/* must be called with alarm_program_lock taken ! */
e1404611
BS
61void
62nvkm_therm_sensor_set_threshold_state(struct nvkm_therm *therm,
63 enum nvkm_therm_thrs thrs,
64 enum nvkm_therm_thrs_state st)
0083b91d 65{
e1404611 66 struct nvkm_therm_priv *priv = (void *)therm;
0083b91d
MP
67 priv->sensor.alarm_state[thrs] = st;
68}
69
70/* must be called with alarm_program_lock taken ! */
e1404611
BS
71enum nvkm_therm_thrs_state
72nvkm_therm_sensor_get_threshold_state(struct nvkm_therm *therm,
73 enum nvkm_therm_thrs thrs)
0083b91d 74{
e1404611 75 struct nvkm_therm_priv *priv = (void *)therm;
0083b91d
MP
76 return priv->sensor.alarm_state[thrs];
77}
78
07df3043
MS
79static void
80nv_poweroff_work(struct work_struct *work)
81{
82 orderly_poweroff(true);
83 kfree(work);
84}
85
e1404611
BS
86void
87nvkm_therm_sensor_event(struct nvkm_therm *therm, enum nvkm_therm_thrs thrs,
88 enum nvkm_therm_thrs_direction dir)
0083b91d 89{
e1404611 90 struct nvkm_therm_priv *priv = (void *)therm;
0083b91d
MP
91 bool active;
92 const char *thresolds[] = {
93 "fanboost", "downclock", "critical", "shutdown"
94 };
7591782b 95 int temperature = therm->temp_get(therm);
0083b91d
MP
96
97 if (thrs < 0 || thrs > 3)
98 return;
99
e1404611 100 if (dir == NVKM_THERM_THRS_FALLING)
7591782b 101 nv_info(therm, "temperature (%i C) went below the '%s' threshold\n",
0083b91d
MP
102 temperature, thresolds[thrs]);
103 else
7591782b 104 nv_info(therm, "temperature (%i C) hit the '%s' threshold\n",
0083b91d
MP
105 temperature, thresolds[thrs]);
106
e1404611 107 active = (dir == NVKM_THERM_THRS_RISING);
0083b91d 108 switch (thrs) {
e1404611 109 case NVKM_THERM_THRS_FANBOOST:
134fc032 110 if (active) {
e1404611
BS
111 nvkm_therm_fan_set(therm, true, 100);
112 nvkm_therm_fan_mode(therm, NVKM_THERM_CTRL_AUTO);
134fc032 113 }
0083b91d 114 break;
e1404611 115 case NVKM_THERM_THRS_DOWNCLOCK:
0083b91d
MP
116 if (priv->emergency.downclock)
117 priv->emergency.downclock(therm, active);
118 break;
e1404611 119 case NVKM_THERM_THRS_CRITICAL:
0083b91d
MP
120 if (priv->emergency.pause)
121 priv->emergency.pause(therm, active);
122 break;
e1404611 123 case NVKM_THERM_THRS_SHUTDOWN:
07df3043
MS
124 if (active) {
125 struct work_struct *work;
126
127 work = kmalloc(sizeof(*work), GFP_ATOMIC);
128 if (work) {
129 INIT_WORK(work, nv_poweroff_work);
130 schedule_work(work);
131 }
132 }
0083b91d 133 break;
e1404611 134 case NVKM_THERM_THRS_NR:
0083b91d
MP
135 break;
136 }
137
138}
139
140/* must be called with alarm_program_lock taken ! */
141static void
e1404611
BS
142nvkm_therm_threshold_hyst_polling(struct nvkm_therm *therm,
143 const struct nvbios_therm_threshold *thrs,
144 enum nvkm_therm_thrs thrs_name)
0083b91d 145{
e1404611
BS
146 enum nvkm_therm_thrs_direction direction;
147 enum nvkm_therm_thrs_state prev_state, new_state;
0083b91d
MP
148 int temp = therm->temp_get(therm);
149
e1404611 150 prev_state = nvkm_therm_sensor_get_threshold_state(therm, thrs_name);
0083b91d 151
e1404611
BS
152 if (temp >= thrs->temp && prev_state == NVKM_THERM_THRS_LOWER) {
153 direction = NVKM_THERM_THRS_RISING;
154 new_state = NVKM_THERM_THRS_HIGHER;
0083b91d 155 } else if (temp <= thrs->temp - thrs->hysteresis &&
e1404611
BS
156 prev_state == NVKM_THERM_THRS_HIGHER) {
157 direction = NVKM_THERM_THRS_FALLING;
158 new_state = NVKM_THERM_THRS_LOWER;
0083b91d
MP
159 } else
160 return; /* nothing to do */
161
e1404611
BS
162 nvkm_therm_sensor_set_threshold_state(therm, thrs_name, new_state);
163 nvkm_therm_sensor_event(therm, thrs_name, direction);
0083b91d
MP
164}
165
166static void
e1404611 167alarm_timer_callback(struct nvkm_alarm *alarm)
0083b91d 168{
e1404611
BS
169 struct nvkm_therm_priv *priv =
170 container_of(alarm, struct nvkm_therm_priv, sensor.therm_poll_alarm);
0083b91d 171 struct nvbios_therm_sensor *sensor = &priv->bios_sensor;
e1404611
BS
172 struct nvkm_timer *ptimer = nvkm_timer(priv);
173 struct nvkm_therm *therm = &priv->base;
0083b91d
MP
174 unsigned long flags;
175
176 spin_lock_irqsave(&priv->sensor.alarm_program_lock, flags);
177
e1404611
BS
178 nvkm_therm_threshold_hyst_polling(therm, &sensor->thrs_fan_boost,
179 NVKM_THERM_THRS_FANBOOST);
0083b91d 180
e1404611
BS
181 nvkm_therm_threshold_hyst_polling(therm, &sensor->thrs_down_clock,
182 NVKM_THERM_THRS_DOWNCLOCK);
0083b91d 183
e1404611
BS
184 nvkm_therm_threshold_hyst_polling(therm, &sensor->thrs_critical,
185 NVKM_THERM_THRS_CRITICAL);
0083b91d 186
e1404611
BS
187 nvkm_therm_threshold_hyst_polling(therm, &sensor->thrs_shutdown,
188 NVKM_THERM_THRS_SHUTDOWN);
0083b91d 189
bb78e7a1
MP
190 spin_unlock_irqrestore(&priv->sensor.alarm_program_lock, flags);
191
0083b91d 192 /* schedule the next poll in one second */
bf55eb84 193 if (therm->temp_get(therm) >= 0 && list_empty(&alarm->head))
bb78e7a1 194 ptimer->alarm(ptimer, 1000000000ULL, alarm);
0083b91d
MP
195}
196
197void
e1404611 198nvkm_therm_program_alarms_polling(struct nvkm_therm *therm)
0083b91d 199{
e1404611 200 struct nvkm_therm_priv *priv = (void *)therm;
0083b91d
MP
201 struct nvbios_therm_sensor *sensor = &priv->bios_sensor;
202
bdd4e843
BS
203 nv_debug(therm,
204 "programmed thresholds [ %d(%d), %d(%d), %d(%d), %d(%d) ]\n",
205 sensor->thrs_fan_boost.temp, sensor->thrs_fan_boost.hysteresis,
206 sensor->thrs_down_clock.temp,
207 sensor->thrs_down_clock.hysteresis,
208 sensor->thrs_critical.temp, sensor->thrs_critical.hysteresis,
209 sensor->thrs_shutdown.temp, sensor->thrs_shutdown.hysteresis);
0083b91d
MP
210
211 alarm_timer_callback(&priv->sensor.therm_poll_alarm);
212}
213
c4a62a76 214int
e1404611 215nvkm_therm_sensor_init(struct nvkm_therm *therm)
c4a62a76 216{
e1404611 217 struct nvkm_therm_priv *priv = (void *)therm;
c4a62a76
MP
218 priv->sensor.program_alarms(therm);
219 return 0;
220}
221
222int
e1404611 223nvkm_therm_sensor_fini(struct nvkm_therm *therm, bool suspend)
c4a62a76 224{
e1404611
BS
225 struct nvkm_therm_priv *priv = (void *)therm;
226 struct nvkm_timer *ptimer = nvkm_timer(therm);
c4a62a76
MP
227
228 if (suspend)
229 ptimer->alarm_cancel(ptimer, &priv->sensor.therm_poll_alarm);
230 return 0;
231}
232
0b3ee377 233void
e1404611 234nvkm_therm_sensor_preinit(struct nvkm_therm *therm)
0b3ee377
MP
235{
236 const char *sensor_avail = "yes";
237
238 if (therm->temp_get(therm) < 0)
239 sensor_avail = "no";
240
241 nv_info(therm, "internal sensor: %s\n", sensor_avail);
242}
243
aa1b9b48 244int
e1404611 245nvkm_therm_sensor_ctor(struct nvkm_therm *therm)
aa1b9b48 246{
e1404611
BS
247 struct nvkm_therm_priv *priv = (void *)therm;
248 struct nvkm_bios *bios = nvkm_bios(therm);
aa1b9b48 249
e1404611 250 nvkm_alarm_init(&priv->sensor.therm_poll_alarm, alarm_timer_callback);
0083b91d 251
e1404611 252 nvkm_therm_temp_set_defaults(therm);
aa1b9b48
MP
253 if (nvbios_therm_sensor_parse(bios, NVBIOS_THERM_DOMAIN_CORE,
254 &priv->bios_sensor))
255 nv_error(therm, "nvbios_therm_sensor_parse failed\n");
e1404611 256 nvkm_therm_temp_safety_checks(therm);
aa1b9b48
MP
257
258 return 0;
259}
This page took 0.186582 seconds and 5 git commands to generate.