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