1/* $NetBSD: nouveau_subdev_therm_temp.c,v 1.2 2016/02/05 23:49:26 riastradh Exp $ */
2
3/*
4 * Copyright 2012 The Nouveau community
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Martin Peres
25 */
26
27#include <sys/cdefs.h>
28__KERNEL_RCSID(0, "$NetBSD: nouveau_subdev_therm_temp.c,v 1.2 2016/02/05 23:49:26 riastradh Exp $");
29
30#include "priv.h"
31
32#include <core/object.h>
33#include <core/device.h>
34
35#include <subdev/bios.h>
36
37static void
38nouveau_therm_temp_set_defaults(struct nouveau_therm *therm)
39{
40 struct nouveau_therm_priv *priv = (void *)therm;
41
42 priv->bios_sensor.offset_constant = 0;
43
44 priv->bios_sensor.thrs_fan_boost.temp = 90;
45 priv->bios_sensor.thrs_fan_boost.hysteresis = 3;
46
47 priv->bios_sensor.thrs_down_clock.temp = 95;
48 priv->bios_sensor.thrs_down_clock.hysteresis = 3;
49
50 priv->bios_sensor.thrs_critical.temp = 105;
51 priv->bios_sensor.thrs_critical.hysteresis = 5;
52
53 priv->bios_sensor.thrs_shutdown.temp = 135;
54 priv->bios_sensor.thrs_shutdown.hysteresis = 5; /*not that it matters */
55}
56
57
58static void
59nouveau_therm_temp_safety_checks(struct nouveau_therm *therm)
60{
61 struct nouveau_therm_priv *priv = (void *)therm;
62 struct nvbios_therm_sensor *s = &priv->bios_sensor;
63
64 /* enforce a minimum hysteresis on thresholds */
65 s->thrs_fan_boost.hysteresis = max_t(u8, s->thrs_fan_boost.hysteresis, 2);
66 s->thrs_down_clock.hysteresis = max_t(u8, s->thrs_down_clock.hysteresis, 2);
67 s->thrs_critical.hysteresis = max_t(u8, s->thrs_critical.hysteresis, 2);
68 s->thrs_shutdown.hysteresis = max_t(u8, s->thrs_shutdown.hysteresis, 2);
69}
70
71/* must be called with alarm_program_lock taken ! */
72void nouveau_therm_sensor_set_threshold_state(struct nouveau_therm *therm,
73 enum nouveau_therm_thrs thrs,
74 enum nouveau_therm_thrs_state st)
75{
76 struct nouveau_therm_priv *priv = (void *)therm;
77 priv->sensor.alarm_state[thrs] = st;
78}
79
80/* must be called with alarm_program_lock taken ! */
81enum nouveau_therm_thrs_state
82nouveau_therm_sensor_get_threshold_state(struct nouveau_therm *therm,
83 enum nouveau_therm_thrs thrs)
84{
85 struct nouveau_therm_priv *priv = (void *)therm;
86 return priv->sensor.alarm_state[thrs];
87}
88
89static void
90nv_poweroff_work(struct work_struct *work)
91{
92 orderly_poweroff(true);
93 kfree(work);
94}
95
96void nouveau_therm_sensor_event(struct nouveau_therm *therm,
97 enum nouveau_therm_thrs thrs,
98 enum nouveau_therm_thrs_direction dir)
99{
100 struct nouveau_therm_priv *priv = (void *)therm;
101 bool active;
102 const char *thresolds[] = {
103 "fanboost", "downclock", "critical", "shutdown"
104 };
105 int temperature = therm->temp_get(therm);
106
107 if ((unsigned)thrs >= __arraycount(thresolds))
108 return;
109
110 if (dir == NOUVEAU_THERM_THRS_FALLING)
111 nv_info(therm, "temperature (%i C) went below the '%s' threshold\n",
112 temperature, thresolds[thrs]);
113 else
114 nv_info(therm, "temperature (%i C) hit the '%s' threshold\n",
115 temperature, thresolds[thrs]);
116
117 active = (dir == NOUVEAU_THERM_THRS_RISING);
118 switch (thrs) {
119 case NOUVEAU_THERM_THRS_FANBOOST:
120 if (active) {
121 nouveau_therm_fan_set(therm, true, 100);
122 nouveau_therm_fan_mode(therm, NOUVEAU_THERM_CTRL_AUTO);
123 }
124 break;
125 case NOUVEAU_THERM_THRS_DOWNCLOCK:
126 if (priv->emergency.downclock)
127 priv->emergency.downclock(therm, active);
128 break;
129 case NOUVEAU_THERM_THRS_CRITICAL:
130 if (priv->emergency.pause)
131 priv->emergency.pause(therm, active);
132 break;
133 case NOUVEAU_THERM_THRS_SHUTDOWN:
134 if (active) {
135 struct work_struct *work;
136
137 work = kmalloc(sizeof(*work), GFP_ATOMIC);
138 if (work) {
139 INIT_WORK(work, nv_poweroff_work);
140 schedule_work(work);
141 }
142 }
143 break;
144 case NOUVEAU_THERM_THRS_NR:
145 break;
146 }
147
148}
149
150/* must be called with alarm_program_lock taken ! */
151static void
152nouveau_therm_threshold_hyst_polling(struct nouveau_therm *therm,
153 const struct nvbios_therm_threshold *thrs,
154 enum nouveau_therm_thrs thrs_name)
155{
156 enum nouveau_therm_thrs_direction direction;
157 enum nouveau_therm_thrs_state prev_state, new_state;
158 int temp = therm->temp_get(therm);
159
160 prev_state = nouveau_therm_sensor_get_threshold_state(therm, thrs_name);
161
162 if (temp >= thrs->temp && prev_state == NOUVEAU_THERM_THRS_LOWER) {
163 direction = NOUVEAU_THERM_THRS_RISING;
164 new_state = NOUVEAU_THERM_THRS_HIGHER;
165 } else if (temp <= thrs->temp - thrs->hysteresis &&
166 prev_state == NOUVEAU_THERM_THRS_HIGHER) {
167 direction = NOUVEAU_THERM_THRS_FALLING;
168 new_state = NOUVEAU_THERM_THRS_LOWER;
169 } else
170 return; /* nothing to do */
171
172 nouveau_therm_sensor_set_threshold_state(therm, thrs_name, new_state);
173 nouveau_therm_sensor_event(therm, thrs_name, direction);
174}
175
176static void
177alarm_timer_callback(struct nouveau_alarm *alarm)
178{
179 struct nouveau_therm_priv *priv =
180 container_of(alarm, struct nouveau_therm_priv, sensor.therm_poll_alarm);
181 struct nvbios_therm_sensor *sensor = &priv->bios_sensor;
182 struct nouveau_timer *ptimer = nouveau_timer(priv);
183 struct nouveau_therm *therm = &priv->base;
184 unsigned long flags;
185
186 spin_lock_irqsave(&priv->sensor.alarm_program_lock, flags);
187
188 nouveau_therm_threshold_hyst_polling(therm, &sensor->thrs_fan_boost,
189 NOUVEAU_THERM_THRS_FANBOOST);
190
191 nouveau_therm_threshold_hyst_polling(therm, &sensor->thrs_down_clock,
192 NOUVEAU_THERM_THRS_DOWNCLOCK);
193
194 nouveau_therm_threshold_hyst_polling(therm, &sensor->thrs_critical,
195 NOUVEAU_THERM_THRS_CRITICAL);
196
197 nouveau_therm_threshold_hyst_polling(therm, &sensor->thrs_shutdown,
198 NOUVEAU_THERM_THRS_SHUTDOWN);
199
200 /* schedule the next poll in one second */
201 if (therm->temp_get(therm) >= 0 && list_empty(&alarm->head))
202 ptimer->alarm(ptimer, 1000 * 1000 * 1000, alarm);
203
204 spin_unlock_irqrestore(&priv->sensor.alarm_program_lock, flags);
205}
206
207void
208nouveau_therm_program_alarms_polling(struct nouveau_therm *therm)
209{
210 struct nouveau_therm_priv *priv = (void *)therm;
211 struct nvbios_therm_sensor *sensor = &priv->bios_sensor;
212
213 nv_debug(therm,
214 "programmed thresholds [ %d(%d), %d(%d), %d(%d), %d(%d) ]\n",
215 sensor->thrs_fan_boost.temp, sensor->thrs_fan_boost.hysteresis,
216 sensor->thrs_down_clock.temp,
217 sensor->thrs_down_clock.hysteresis,
218 sensor->thrs_critical.temp, sensor->thrs_critical.hysteresis,
219 sensor->thrs_shutdown.temp, sensor->thrs_shutdown.hysteresis);
220
221 alarm_timer_callback(&priv->sensor.therm_poll_alarm);
222}
223
224int
225nouveau_therm_sensor_init(struct nouveau_therm *therm)
226{
227 struct nouveau_therm_priv *priv = (void *)therm;
228 priv->sensor.program_alarms(therm);
229 return 0;
230}
231
232int
233nouveau_therm_sensor_fini(struct nouveau_therm *therm, bool suspend)
234{
235 struct nouveau_therm_priv *priv = (void *)therm;
236 struct nouveau_timer *ptimer = nouveau_timer(therm);
237
238 if (suspend)
239 ptimer->alarm_cancel(ptimer, &priv->sensor.therm_poll_alarm);
240 return 0;
241}
242
243void
244nouveau_therm_sensor_preinit(struct nouveau_therm *therm)
245{
246 const char *sensor_avail = "yes";
247
248 if (therm->temp_get(therm) < 0)
249 sensor_avail = "no";
250
251 nv_info(therm, "internal sensor: %s\n", sensor_avail);
252}
253
254int
255nouveau_therm_sensor_ctor(struct nouveau_therm *therm)
256{
257 struct nouveau_therm_priv *priv = (void *)therm;
258 struct nouveau_bios *bios = nouveau_bios(therm);
259
260 nouveau_alarm_init(&priv->sensor.therm_poll_alarm, alarm_timer_callback);
261
262 nouveau_therm_temp_set_defaults(therm);
263 if (nvbios_therm_sensor_parse(bios, NVBIOS_THERM_DOMAIN_CORE,
264 &priv->bios_sensor))
265 nv_error(therm, "nvbios_therm_sensor_parse failed\n");
266 nouveau_therm_temp_safety_checks(therm);
267
268 return 0;
269}
270