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 "priv.h" | |
26 | ||
27 | #include <core/object.h> | |
28 | #include <core/device.h> | |
29 | ||
30 | #include <subdev/bios.h> | |
31 | ||
32 | static void | |
33 | nouveau_therm_temp_set_defaults(struct nouveau_therm *therm) | |
34 | { | |
35 | struct nouveau_therm_priv *priv = (void *)therm; | |
36 | ||
37 | priv->bios_sensor.slope_mult = 1; | |
38 | priv->bios_sensor.slope_div = 1; | |
39 | priv->bios_sensor.offset_num = 0; | |
40 | priv->bios_sensor.offset_den = 1; | |
41 | priv->bios_sensor.offset_constant = 0; | |
42 | ||
43 | priv->bios_sensor.thrs_fan_boost.temp = 90; | |
44 | priv->bios_sensor.thrs_fan_boost.hysteresis = 3; | |
45 | ||
46 | priv->bios_sensor.thrs_down_clock.temp = 95; | |
47 | priv->bios_sensor.thrs_down_clock.hysteresis = 3; | |
48 | ||
49 | priv->bios_sensor.thrs_critical.temp = 105; | |
50 | priv->bios_sensor.thrs_critical.hysteresis = 5; | |
51 | ||
52 | priv->bios_sensor.thrs_shutdown.temp = 135; | |
53 | priv->bios_sensor.thrs_shutdown.hysteresis = 5; /*not that it matters */ | |
54 | } | |
55 | ||
56 | ||
57 | static void | |
58 | nouveau_therm_temp_safety_checks(struct nouveau_therm *therm) | |
59 | { | |
60 | struct nouveau_therm_priv *priv = (void *)therm; | |
61 | ||
62 | if (!priv->bios_sensor.slope_div) | |
63 | priv->bios_sensor.slope_div = 1; | |
64 | if (!priv->bios_sensor.offset_den) | |
65 | priv->bios_sensor.offset_den = 1; | |
66 | } | |
67 | ||
0083b91d MP |
68 | /* must be called with alarm_program_lock taken ! */ |
69 | void nouveau_therm_sensor_set_threshold_state(struct nouveau_therm *therm, | |
70 | enum nouveau_therm_thrs thrs, | |
71 | enum nouveau_therm_thrs_state st) | |
72 | { | |
73 | struct nouveau_therm_priv *priv = (void *)therm; | |
74 | priv->sensor.alarm_state[thrs] = st; | |
75 | } | |
76 | ||
77 | /* must be called with alarm_program_lock taken ! */ | |
78 | enum nouveau_therm_thrs_state | |
79 | nouveau_therm_sensor_get_threshold_state(struct nouveau_therm *therm, | |
80 | enum nouveau_therm_thrs thrs) | |
81 | { | |
82 | struct nouveau_therm_priv *priv = (void *)therm; | |
83 | return priv->sensor.alarm_state[thrs]; | |
84 | } | |
85 | ||
86 | void nouveau_therm_sensor_event(struct nouveau_therm *therm, | |
87 | enum nouveau_therm_thrs thrs, | |
88 | enum nouveau_therm_thrs_direction dir) | |
89 | { | |
90 | struct nouveau_therm_priv *priv = (void *)therm; | |
91 | bool active; | |
92 | const char *thresolds[] = { | |
93 | "fanboost", "downclock", "critical", "shutdown" | |
94 | }; | |
95 | uint8_t temperature = therm->temp_get(therm); | |
96 | ||
97 | if (thrs < 0 || thrs > 3) | |
98 | return; | |
99 | ||
100 | if (dir == NOUVEAU_THERM_THRS_FALLING) | |
101 | nv_info(therm, "temperature (%u C) went bellow the '%s' threshold\n", | |
102 | temperature, thresolds[thrs]); | |
103 | else | |
104 | nv_info(therm, "temperature (%u C) hit the '%s' threshold\n", | |
105 | temperature, thresolds[thrs]); | |
106 | ||
107 | active = (dir == NOUVEAU_THERM_THRS_RISING); | |
108 | switch (thrs) { | |
109 | case NOUVEAU_THERM_THRS_FANBOOST: | |
110 | nouveau_therm_fan_set(therm, true, 100); | |
1a22274b | 111 | nouveau_therm_mode(therm, NOUVEAU_THERM_CTRL_AUTO); |
0083b91d MP |
112 | break; |
113 | case NOUVEAU_THERM_THRS_DOWNCLOCK: | |
114 | if (priv->emergency.downclock) | |
115 | priv->emergency.downclock(therm, active); | |
116 | break; | |
117 | case NOUVEAU_THERM_THRS_CRITICAL: | |
118 | if (priv->emergency.pause) | |
119 | priv->emergency.pause(therm, active); | |
120 | break; | |
121 | case NOUVEAU_THERM_THRS_SHUTDOWN: | |
122 | orderly_poweroff(true); | |
123 | break; | |
124 | case NOUVEAU_THERM_THRS_NR: | |
125 | break; | |
126 | } | |
127 | ||
128 | } | |
129 | ||
130 | /* must be called with alarm_program_lock taken ! */ | |
131 | static void | |
132 | nouveau_therm_threshold_hyst_polling(struct nouveau_therm *therm, | |
133 | const struct nvbios_therm_threshold *thrs, | |
134 | enum nouveau_therm_thrs thrs_name) | |
135 | { | |
136 | enum nouveau_therm_thrs_direction direction; | |
137 | enum nouveau_therm_thrs_state prev_state, new_state; | |
138 | int temp = therm->temp_get(therm); | |
139 | ||
140 | prev_state = nouveau_therm_sensor_get_threshold_state(therm, thrs_name); | |
141 | ||
142 | if (temp >= thrs->temp && prev_state == NOUVEAU_THERM_THRS_LOWER) { | |
143 | direction = NOUVEAU_THERM_THRS_RISING; | |
144 | new_state = NOUVEAU_THERM_THRS_HIGHER; | |
145 | } else if (temp <= thrs->temp - thrs->hysteresis && | |
146 | prev_state == NOUVEAU_THERM_THRS_HIGHER) { | |
147 | direction = NOUVEAU_THERM_THRS_FALLING; | |
148 | new_state = NOUVEAU_THERM_THRS_LOWER; | |
149 | } else | |
150 | return; /* nothing to do */ | |
151 | ||
152 | nouveau_therm_sensor_set_threshold_state(therm, thrs_name, new_state); | |
153 | nouveau_therm_sensor_event(therm, thrs_name, direction); | |
154 | } | |
155 | ||
156 | static void | |
157 | alarm_timer_callback(struct nouveau_alarm *alarm) | |
158 | { | |
159 | struct nouveau_therm_priv *priv = | |
160 | container_of(alarm, struct nouveau_therm_priv, sensor.therm_poll_alarm); | |
161 | struct nvbios_therm_sensor *sensor = &priv->bios_sensor; | |
162 | struct nouveau_timer *ptimer = nouveau_timer(priv); | |
163 | struct nouveau_therm *therm = &priv->base; | |
164 | unsigned long flags; | |
165 | ||
166 | spin_lock_irqsave(&priv->sensor.alarm_program_lock, flags); | |
167 | ||
168 | nouveau_therm_threshold_hyst_polling(therm, &sensor->thrs_fan_boost, | |
169 | NOUVEAU_THERM_THRS_FANBOOST); | |
170 | ||
171 | nouveau_therm_threshold_hyst_polling(therm, &sensor->thrs_down_clock, | |
172 | NOUVEAU_THERM_THRS_DOWNCLOCK); | |
173 | ||
174 | nouveau_therm_threshold_hyst_polling(therm, &sensor->thrs_critical, | |
175 | NOUVEAU_THERM_THRS_CRITICAL); | |
176 | ||
177 | nouveau_therm_threshold_hyst_polling(therm, &sensor->thrs_shutdown, | |
178 | NOUVEAU_THERM_THRS_SHUTDOWN); | |
179 | ||
180 | /* schedule the next poll in one second */ | |
181 | if (list_empty(&alarm->head)) | |
182 | ptimer->alarm(ptimer, 1000 * 1000 * 1000, alarm); | |
183 | ||
184 | spin_unlock_irqrestore(&priv->sensor.alarm_program_lock, flags); | |
185 | } | |
186 | ||
187 | void | |
188 | nouveau_therm_program_alarms_polling(struct nouveau_therm *therm) | |
189 | { | |
190 | struct nouveau_therm_priv *priv = (void *)therm; | |
191 | struct nvbios_therm_sensor *sensor = &priv->bios_sensor; | |
192 | ||
193 | nv_info(therm, | |
194 | "programmed thresholds [ %d(%d), %d(%d), %d(%d), %d(%d) ]\n", | |
195 | sensor->thrs_fan_boost.temp, sensor->thrs_fan_boost.hysteresis, | |
196 | sensor->thrs_down_clock.temp, | |
197 | sensor->thrs_down_clock.hysteresis, | |
198 | sensor->thrs_critical.temp, sensor->thrs_critical.hysteresis, | |
199 | sensor->thrs_shutdown.temp, sensor->thrs_shutdown.hysteresis); | |
200 | ||
201 | alarm_timer_callback(&priv->sensor.therm_poll_alarm); | |
202 | } | |
203 | ||
aa1b9b48 MP |
204 | int |
205 | nouveau_therm_sensor_ctor(struct nouveau_therm *therm) | |
206 | { | |
207 | struct nouveau_therm_priv *priv = (void *)therm; | |
208 | struct nouveau_bios *bios = nouveau_bios(therm); | |
209 | ||
0083b91d MP |
210 | nouveau_alarm_init(&priv->sensor.therm_poll_alarm, alarm_timer_callback); |
211 | ||
aa1b9b48 MP |
212 | nouveau_therm_temp_set_defaults(therm); |
213 | if (nvbios_therm_sensor_parse(bios, NVBIOS_THERM_DOMAIN_CORE, | |
214 | &priv->bios_sensor)) | |
215 | nv_error(therm, "nvbios_therm_sensor_parse failed\n"); | |
216 | nouveau_therm_temp_safety_checks(therm); | |
217 | ||
218 | return 0; | |
219 | } |