1 /* $NetBSD: nouveau_nvkm_subdev_therm_temp.c,v 1.5 2021/12/19 10:51:59 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 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_therm_temp.c,v 1.5 2021/12/19 10:51:59 riastradh Exp $");
28
29 #include "priv.h"
30
31 static void
nvkm_therm_temp_set_defaults(struct nvkm_therm * therm)32 nvkm_therm_temp_set_defaults(struct nvkm_therm *therm)
33 {
34 therm->bios_sensor.offset_constant = 0;
35
36 therm->bios_sensor.thrs_fan_boost.temp = 90;
37 therm->bios_sensor.thrs_fan_boost.hysteresis = 3;
38
39 therm->bios_sensor.thrs_down_clock.temp = 95;
40 therm->bios_sensor.thrs_down_clock.hysteresis = 3;
41
42 therm->bios_sensor.thrs_critical.temp = 105;
43 therm->bios_sensor.thrs_critical.hysteresis = 5;
44
45 therm->bios_sensor.thrs_shutdown.temp = 135;
46 therm->bios_sensor.thrs_shutdown.hysteresis = 5; /*not that it matters */
47 }
48
49 static void
nvkm_therm_temp_safety_checks(struct nvkm_therm * therm)50 nvkm_therm_temp_safety_checks(struct nvkm_therm *therm)
51 {
52 struct nvbios_therm_sensor *s = &therm->bios_sensor;
53
54 /* enforce a minimum hysteresis on thresholds */
55 s->thrs_fan_boost.hysteresis = max_t(u8, s->thrs_fan_boost.hysteresis, 2);
56 s->thrs_down_clock.hysteresis = max_t(u8, s->thrs_down_clock.hysteresis, 2);
57 s->thrs_critical.hysteresis = max_t(u8, s->thrs_critical.hysteresis, 2);
58 s->thrs_shutdown.hysteresis = max_t(u8, s->thrs_shutdown.hysteresis, 2);
59 }
60
61 /* must be called with alarm_program_lock taken ! */
62 void
nvkm_therm_sensor_set_threshold_state(struct nvkm_therm * therm,enum nvkm_therm_thrs thrs,enum nvkm_therm_thrs_state st)63 nvkm_therm_sensor_set_threshold_state(struct nvkm_therm *therm,
64 enum nvkm_therm_thrs thrs,
65 enum nvkm_therm_thrs_state st)
66 {
67 therm->sensor.alarm_state[thrs] = st;
68 }
69
70 /* must be called with alarm_program_lock taken ! */
71 enum nvkm_therm_thrs_state
nvkm_therm_sensor_get_threshold_state(struct nvkm_therm * therm,enum nvkm_therm_thrs thrs)72 nvkm_therm_sensor_get_threshold_state(struct nvkm_therm *therm,
73 enum nvkm_therm_thrs thrs)
74 {
75 return therm->sensor.alarm_state[thrs];
76 }
77
78 static void
nv_poweroff_work(struct work_struct * work)79 nv_poweroff_work(struct work_struct *work)
80 {
81 orderly_poweroff(true);
82 kfree(work);
83 }
84
85 void
nvkm_therm_sensor_event(struct nvkm_therm * therm,enum nvkm_therm_thrs thrs,enum nvkm_therm_thrs_direction dir)86 nvkm_therm_sensor_event(struct nvkm_therm *therm, enum nvkm_therm_thrs thrs,
87 enum nvkm_therm_thrs_direction dir)
88 {
89 struct nvkm_subdev *subdev = &therm->subdev;
90 bool active;
91 static const char * const thresholds[] = {
92 "fanboost", "downclock", "critical", "shutdown"
93 };
94 int temperature = therm->func->temp_get(therm);
95
96 if ((unsigned)thrs >= __arraycount(thresholds))
97 return;
98
99 if (dir == NVKM_THERM_THRS_FALLING)
100 nvkm_info(subdev,
101 "temperature (%i C) went below the '%s' threshold\n",
102 temperature, thresholds[thrs]);
103 else
104 nvkm_info(subdev, "temperature (%i C) hit the '%s' threshold\n",
105 temperature, thresholds[thrs]);
106
107 active = (dir == NVKM_THERM_THRS_RISING);
108 switch (thrs) {
109 case NVKM_THERM_THRS_FANBOOST:
110 if (active) {
111 nvkm_therm_fan_set(therm, true, 100);
112 nvkm_therm_fan_mode(therm, NVKM_THERM_CTRL_AUTO);
113 }
114 break;
115 case NVKM_THERM_THRS_DOWNCLOCK:
116 if (therm->emergency.downclock)
117 therm->emergency.downclock(therm, active);
118 break;
119 case NVKM_THERM_THRS_CRITICAL:
120 if (therm->emergency.pause)
121 therm->emergency.pause(therm, active);
122 break;
123 case NVKM_THERM_THRS_SHUTDOWN:
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 }
133 break;
134 case NVKM_THERM_THRS_NR:
135 break;
136 }
137
138 }
139
140 /* must be called with alarm_program_lock taken ! */
141 static void
nvkm_therm_threshold_hyst_polling(struct nvkm_therm * therm,const struct nvbios_therm_threshold * thrs,enum nvkm_therm_thrs thrs_name)142 nvkm_therm_threshold_hyst_polling(struct nvkm_therm *therm,
143 const struct nvbios_therm_threshold *thrs,
144 enum nvkm_therm_thrs thrs_name)
145 {
146 enum nvkm_therm_thrs_direction direction;
147 enum nvkm_therm_thrs_state prev_state, new_state;
148 int temp = therm->func->temp_get(therm);
149
150 prev_state = nvkm_therm_sensor_get_threshold_state(therm, thrs_name);
151
152 if (temp >= thrs->temp && prev_state == NVKM_THERM_THRS_LOWER) {
153 direction = NVKM_THERM_THRS_RISING;
154 new_state = NVKM_THERM_THRS_HIGHER;
155 } else if (temp <= thrs->temp - thrs->hysteresis &&
156 prev_state == NVKM_THERM_THRS_HIGHER) {
157 direction = NVKM_THERM_THRS_FALLING;
158 new_state = NVKM_THERM_THRS_LOWER;
159 } else
160 return; /* nothing to do */
161
162 nvkm_therm_sensor_set_threshold_state(therm, thrs_name, new_state);
163 nvkm_therm_sensor_event(therm, thrs_name, direction);
164 }
165
166 static void
alarm_timer_callback(struct nvkm_alarm * alarm)167 alarm_timer_callback(struct nvkm_alarm *alarm)
168 {
169 struct nvkm_therm *therm =
170 container_of(alarm, struct nvkm_therm, sensor.therm_poll_alarm);
171 struct nvbios_therm_sensor *sensor = &therm->bios_sensor;
172 struct nvkm_timer *tmr = therm->subdev.device->timer;
173 unsigned long flags;
174
175 spin_lock_irqsave(&therm->sensor.alarm_program_lock, flags);
176
177 nvkm_therm_threshold_hyst_polling(therm, &sensor->thrs_fan_boost,
178 NVKM_THERM_THRS_FANBOOST);
179
180 nvkm_therm_threshold_hyst_polling(therm,
181 &sensor->thrs_down_clock,
182 NVKM_THERM_THRS_DOWNCLOCK);
183
184 nvkm_therm_threshold_hyst_polling(therm, &sensor->thrs_critical,
185 NVKM_THERM_THRS_CRITICAL);
186
187 nvkm_therm_threshold_hyst_polling(therm, &sensor->thrs_shutdown,
188 NVKM_THERM_THRS_SHUTDOWN);
189
190 spin_unlock_irqrestore(&therm->sensor.alarm_program_lock, flags);
191
192 /* schedule the next poll in one second */
193 if (therm->func->temp_get(therm) >= 0)
194 nvkm_timer_alarm(tmr, 1000000000ULL, alarm);
195 }
196
197 void
nvkm_therm_program_alarms_polling(struct nvkm_therm * therm)198 nvkm_therm_program_alarms_polling(struct nvkm_therm *therm)
199 {
200 struct nvbios_therm_sensor *sensor = &therm->bios_sensor;
201
202 nvkm_debug(&therm->subdev,
203 "programmed thresholds [ %d(%d), %d(%d), %d(%d), %d(%d) ]\n",
204 sensor->thrs_fan_boost.temp,
205 sensor->thrs_fan_boost.hysteresis,
206 sensor->thrs_down_clock.temp,
207 sensor->thrs_down_clock.hysteresis,
208 sensor->thrs_critical.temp,
209 sensor->thrs_critical.hysteresis,
210 sensor->thrs_shutdown.temp,
211 sensor->thrs_shutdown.hysteresis);
212
213 alarm_timer_callback(&therm->sensor.therm_poll_alarm);
214 }
215
216 int
nvkm_therm_sensor_init(struct nvkm_therm * therm)217 nvkm_therm_sensor_init(struct nvkm_therm *therm)
218 {
219 therm->func->program_alarms(therm);
220 return 0;
221 }
222
223 int
nvkm_therm_sensor_fini(struct nvkm_therm * therm,bool suspend)224 nvkm_therm_sensor_fini(struct nvkm_therm *therm, bool suspend)
225 {
226 struct nvkm_timer *tmr = therm->subdev.device->timer;
227 if (suspend)
228 nvkm_timer_alarm(tmr, 0, &therm->sensor.therm_poll_alarm);
229 return 0;
230 }
231
232 void
nvkm_therm_sensor_preinit(struct nvkm_therm * therm)233 nvkm_therm_sensor_preinit(struct nvkm_therm *therm)
234 {
235 const char *sensor_avail = "yes";
236
237 if (therm->func->temp_get(therm) < 0)
238 sensor_avail = "no";
239
240 nvkm_debug(&therm->subdev, "internal sensor: %s\n", sensor_avail);
241 }
242
243 int
nvkm_therm_sensor_ctor(struct nvkm_therm * therm)244 nvkm_therm_sensor_ctor(struct nvkm_therm *therm)
245 {
246 struct nvkm_subdev *subdev = &therm->subdev;
247 struct nvkm_bios *bios = subdev->device->bios;
248
249 nvkm_alarm_init(&therm->sensor.therm_poll_alarm, alarm_timer_callback);
250
251 nvkm_therm_temp_set_defaults(therm);
252 if (nvbios_therm_sensor_parse(bios, NVBIOS_THERM_DOMAIN_CORE,
253 &therm->bios_sensor))
254 nvkm_error(subdev, "nvbios_therm_sensor_parse failed\n");
255 nvkm_therm_temp_safety_checks(therm);
256
257 return 0;
258 }
259