xref: /openbmc/linux/drivers/gpu/drm/nouveau/nvkm/subdev/therm/temp.c (revision cf40a76e7d5874bb25f4404eecc58a2e033af885)
1c39f472eSBen Skeggs /*
2c39f472eSBen Skeggs  * Copyright 2012 The Nouveau community
3c39f472eSBen Skeggs  *
4c39f472eSBen Skeggs  * Permission is hereby granted, free of charge, to any person obtaining a
5c39f472eSBen Skeggs  * copy of this software and associated documentation files (the "Software"),
6c39f472eSBen Skeggs  * to deal in the Software without restriction, including without limitation
7c39f472eSBen Skeggs  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8c39f472eSBen Skeggs  * and/or sell copies of the Software, and to permit persons to whom the
9c39f472eSBen Skeggs  * Software is furnished to do so, subject to the following conditions:
10c39f472eSBen Skeggs  *
11c39f472eSBen Skeggs  * The above copyright notice and this permission notice shall be included in
12c39f472eSBen Skeggs  * all copies or substantial portions of the Software.
13c39f472eSBen Skeggs  *
14c39f472eSBen Skeggs  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15c39f472eSBen Skeggs  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16c39f472eSBen Skeggs  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17c39f472eSBen Skeggs  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18c39f472eSBen Skeggs  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19c39f472eSBen Skeggs  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20c39f472eSBen Skeggs  * OTHER DEALINGS IN THE SOFTWARE.
21c39f472eSBen Skeggs  *
22c39f472eSBen Skeggs  * Authors: Martin Peres
23c39f472eSBen Skeggs  */
24c39f472eSBen Skeggs #include "priv.h"
25c39f472eSBen Skeggs 
26c39f472eSBen Skeggs static void
nvkm_therm_temp_set_defaults(struct nvkm_therm * therm)2757113c01SBen Skeggs nvkm_therm_temp_set_defaults(struct nvkm_therm *therm)
28c39f472eSBen Skeggs {
29da06b46bSBen Skeggs 	therm->bios_sensor.offset_constant = 0;
30c39f472eSBen Skeggs 
31da06b46bSBen Skeggs 	therm->bios_sensor.thrs_fan_boost.temp = 90;
32da06b46bSBen Skeggs 	therm->bios_sensor.thrs_fan_boost.hysteresis = 3;
33c39f472eSBen Skeggs 
34da06b46bSBen Skeggs 	therm->bios_sensor.thrs_down_clock.temp = 95;
35da06b46bSBen Skeggs 	therm->bios_sensor.thrs_down_clock.hysteresis = 3;
36c39f472eSBen Skeggs 
37da06b46bSBen Skeggs 	therm->bios_sensor.thrs_critical.temp = 105;
38da06b46bSBen Skeggs 	therm->bios_sensor.thrs_critical.hysteresis = 5;
39c39f472eSBen Skeggs 
40da06b46bSBen Skeggs 	therm->bios_sensor.thrs_shutdown.temp = 135;
41da06b46bSBen Skeggs 	therm->bios_sensor.thrs_shutdown.hysteresis = 5; /*not that it matters */
42c39f472eSBen Skeggs }
43c39f472eSBen Skeggs 
44c39f472eSBen Skeggs static void
nvkm_therm_temp_safety_checks(struct nvkm_therm * therm)4557113c01SBen Skeggs nvkm_therm_temp_safety_checks(struct nvkm_therm *therm)
46c39f472eSBen Skeggs {
47da06b46bSBen Skeggs 	struct nvbios_therm_sensor *s = &therm->bios_sensor;
48c39f472eSBen Skeggs 
49c39f472eSBen Skeggs 	/* enforce a minimum hysteresis on thresholds */
50c39f472eSBen Skeggs 	s->thrs_fan_boost.hysteresis = max_t(u8, s->thrs_fan_boost.hysteresis, 2);
51c39f472eSBen Skeggs 	s->thrs_down_clock.hysteresis = max_t(u8, s->thrs_down_clock.hysteresis, 2);
52c39f472eSBen Skeggs 	s->thrs_critical.hysteresis = max_t(u8, s->thrs_critical.hysteresis, 2);
53c39f472eSBen Skeggs 	s->thrs_shutdown.hysteresis = max_t(u8, s->thrs_shutdown.hysteresis, 2);
54c39f472eSBen Skeggs }
55c39f472eSBen Skeggs 
56c39f472eSBen Skeggs /* must be called with alarm_program_lock taken ! */
57e1404611SBen Skeggs void
nvkm_therm_sensor_set_threshold_state(struct nvkm_therm * therm,enum nvkm_therm_thrs thrs,enum nvkm_therm_thrs_state st)5857113c01SBen Skeggs nvkm_therm_sensor_set_threshold_state(struct nvkm_therm *therm,
59e1404611SBen Skeggs 				      enum nvkm_therm_thrs thrs,
60e1404611SBen Skeggs 				      enum nvkm_therm_thrs_state st)
61c39f472eSBen Skeggs {
62da06b46bSBen Skeggs 	therm->sensor.alarm_state[thrs] = st;
63c39f472eSBen Skeggs }
64c39f472eSBen Skeggs 
65c39f472eSBen Skeggs /* must be called with alarm_program_lock taken ! */
66e1404611SBen Skeggs enum nvkm_therm_thrs_state
nvkm_therm_sensor_get_threshold_state(struct nvkm_therm * therm,enum nvkm_therm_thrs thrs)6757113c01SBen Skeggs nvkm_therm_sensor_get_threshold_state(struct nvkm_therm *therm,
68e1404611SBen Skeggs 				      enum nvkm_therm_thrs thrs)
69c39f472eSBen Skeggs {
70da06b46bSBen Skeggs 	return therm->sensor.alarm_state[thrs];
71c39f472eSBen Skeggs }
72c39f472eSBen Skeggs 
73c39f472eSBen Skeggs static void
nv_poweroff_work(struct work_struct * work)74c39f472eSBen Skeggs nv_poweroff_work(struct work_struct *work)
75c39f472eSBen Skeggs {
76c39f472eSBen Skeggs 	orderly_poweroff(true);
77c39f472eSBen Skeggs 	kfree(work);
78c39f472eSBen Skeggs }
79c39f472eSBen Skeggs 
80e1404611SBen Skeggs void
nvkm_therm_sensor_event(struct nvkm_therm * therm,enum nvkm_therm_thrs thrs,enum nvkm_therm_thrs_direction dir)8157113c01SBen Skeggs nvkm_therm_sensor_event(struct nvkm_therm *therm, enum nvkm_therm_thrs thrs,
82e1404611SBen Skeggs 			enum nvkm_therm_thrs_direction dir)
83c39f472eSBen Skeggs {
8457113c01SBen Skeggs 	struct nvkm_subdev *subdev = &therm->subdev;
85c39f472eSBen Skeggs 	bool active;
86*4a84a6fdSColin Ian King 	static const char * const thresholds[] = {
87c39f472eSBen Skeggs 		"fanboost", "downclock", "critical", "shutdown"
88c39f472eSBen Skeggs 	};
8957113c01SBen Skeggs 	int temperature = therm->func->temp_get(therm);
90c39f472eSBen Skeggs 
91c39f472eSBen Skeggs 	if (thrs < 0 || thrs > 3)
92c39f472eSBen Skeggs 		return;
93c39f472eSBen Skeggs 
94e1404611SBen Skeggs 	if (dir == NVKM_THERM_THRS_FALLING)
95b3c418bbSBen Skeggs 		nvkm_info(subdev,
96b3c418bbSBen Skeggs 			  "temperature (%i C) went below the '%s' threshold\n",
97*4a84a6fdSColin Ian King 			  temperature, thresholds[thrs]);
98c39f472eSBen Skeggs 	else
99b3c418bbSBen Skeggs 		nvkm_info(subdev, "temperature (%i C) hit the '%s' threshold\n",
100*4a84a6fdSColin Ian King 			  temperature, thresholds[thrs]);
101c39f472eSBen Skeggs 
102e1404611SBen Skeggs 	active = (dir == NVKM_THERM_THRS_RISING);
103c39f472eSBen Skeggs 	switch (thrs) {
104e1404611SBen Skeggs 	case NVKM_THERM_THRS_FANBOOST:
105c39f472eSBen Skeggs 		if (active) {
10657113c01SBen Skeggs 			nvkm_therm_fan_set(therm, true, 100);
10757113c01SBen Skeggs 			nvkm_therm_fan_mode(therm, NVKM_THERM_CTRL_AUTO);
108c39f472eSBen Skeggs 		}
109c39f472eSBen Skeggs 		break;
110e1404611SBen Skeggs 	case NVKM_THERM_THRS_DOWNCLOCK:
111da06b46bSBen Skeggs 		if (therm->emergency.downclock)
11257113c01SBen Skeggs 			therm->emergency.downclock(therm, active);
113c39f472eSBen Skeggs 		break;
114e1404611SBen Skeggs 	case NVKM_THERM_THRS_CRITICAL:
115da06b46bSBen Skeggs 		if (therm->emergency.pause)
11657113c01SBen Skeggs 			therm->emergency.pause(therm, active);
117c39f472eSBen Skeggs 		break;
118e1404611SBen Skeggs 	case NVKM_THERM_THRS_SHUTDOWN:
119c39f472eSBen Skeggs 		if (active) {
120c39f472eSBen Skeggs 			struct work_struct *work;
121c39f472eSBen Skeggs 
122c39f472eSBen Skeggs 			work = kmalloc(sizeof(*work), GFP_ATOMIC);
123c39f472eSBen Skeggs 			if (work) {
124c39f472eSBen Skeggs 				INIT_WORK(work, nv_poweroff_work);
125c39f472eSBen Skeggs 				schedule_work(work);
126c39f472eSBen Skeggs 			}
127c39f472eSBen Skeggs 		}
128c39f472eSBen Skeggs 		break;
129e1404611SBen Skeggs 	case NVKM_THERM_THRS_NR:
130c39f472eSBen Skeggs 		break;
131c39f472eSBen Skeggs 	}
132c39f472eSBen Skeggs 
133c39f472eSBen Skeggs }
134c39f472eSBen Skeggs 
135c39f472eSBen Skeggs /* must be called with alarm_program_lock taken ! */
136c39f472eSBen Skeggs static void
nvkm_therm_threshold_hyst_polling(struct nvkm_therm * therm,const struct nvbios_therm_threshold * thrs,enum nvkm_therm_thrs thrs_name)137e1404611SBen Skeggs nvkm_therm_threshold_hyst_polling(struct nvkm_therm *therm,
138c39f472eSBen Skeggs 				  const struct nvbios_therm_threshold *thrs,
139e1404611SBen Skeggs 				  enum nvkm_therm_thrs thrs_name)
140c39f472eSBen Skeggs {
141e1404611SBen Skeggs 	enum nvkm_therm_thrs_direction direction;
142e1404611SBen Skeggs 	enum nvkm_therm_thrs_state prev_state, new_state;
14357113c01SBen Skeggs 	int temp = therm->func->temp_get(therm);
144c39f472eSBen Skeggs 
145e1404611SBen Skeggs 	prev_state = nvkm_therm_sensor_get_threshold_state(therm, thrs_name);
146c39f472eSBen Skeggs 
147e1404611SBen Skeggs 	if (temp >= thrs->temp && prev_state == NVKM_THERM_THRS_LOWER) {
148e1404611SBen Skeggs 		direction = NVKM_THERM_THRS_RISING;
149e1404611SBen Skeggs 		new_state = NVKM_THERM_THRS_HIGHER;
150c39f472eSBen Skeggs 	} else if (temp <= thrs->temp - thrs->hysteresis &&
151e1404611SBen Skeggs 			prev_state == NVKM_THERM_THRS_HIGHER) {
152e1404611SBen Skeggs 		direction = NVKM_THERM_THRS_FALLING;
153e1404611SBen Skeggs 		new_state = NVKM_THERM_THRS_LOWER;
154c39f472eSBen Skeggs 	} else
155c39f472eSBen Skeggs 		return; /* nothing to do */
156c39f472eSBen Skeggs 
157e1404611SBen Skeggs 	nvkm_therm_sensor_set_threshold_state(therm, thrs_name, new_state);
158e1404611SBen Skeggs 	nvkm_therm_sensor_event(therm, thrs_name, direction);
159c39f472eSBen Skeggs }
160c39f472eSBen Skeggs 
161c39f472eSBen Skeggs static void
alarm_timer_callback(struct nvkm_alarm * alarm)162e1404611SBen Skeggs alarm_timer_callback(struct nvkm_alarm *alarm)
163c39f472eSBen Skeggs {
16457113c01SBen Skeggs 	struct nvkm_therm *therm =
16557113c01SBen Skeggs 		container_of(alarm, struct nvkm_therm, sensor.therm_poll_alarm);
166da06b46bSBen Skeggs 	struct nvbios_therm_sensor *sensor = &therm->bios_sensor;
16757113c01SBen Skeggs 	struct nvkm_timer *tmr = therm->subdev.device->timer;
168c39f472eSBen Skeggs 	unsigned long flags;
169c39f472eSBen Skeggs 
170da06b46bSBen Skeggs 	spin_lock_irqsave(&therm->sensor.alarm_program_lock, flags);
171c39f472eSBen Skeggs 
17257113c01SBen Skeggs 	nvkm_therm_threshold_hyst_polling(therm, &sensor->thrs_fan_boost,
173e1404611SBen Skeggs 					  NVKM_THERM_THRS_FANBOOST);
174c39f472eSBen Skeggs 
17557113c01SBen Skeggs 	nvkm_therm_threshold_hyst_polling(therm,
176da06b46bSBen Skeggs 					  &sensor->thrs_down_clock,
177e1404611SBen Skeggs 					  NVKM_THERM_THRS_DOWNCLOCK);
178c39f472eSBen Skeggs 
17957113c01SBen Skeggs 	nvkm_therm_threshold_hyst_polling(therm, &sensor->thrs_critical,
180e1404611SBen Skeggs 					  NVKM_THERM_THRS_CRITICAL);
181c39f472eSBen Skeggs 
18257113c01SBen Skeggs 	nvkm_therm_threshold_hyst_polling(therm, &sensor->thrs_shutdown,
183e1404611SBen Skeggs 					  NVKM_THERM_THRS_SHUTDOWN);
184c39f472eSBen Skeggs 
185da06b46bSBen Skeggs 	spin_unlock_irqrestore(&therm->sensor.alarm_program_lock, flags);
186c39f472eSBen Skeggs 
187c39f472eSBen Skeggs 	/* schedule the next poll in one second */
188e4311ee5SBen Skeggs 	if (therm->func->temp_get(therm) >= 0)
18931649ecfSBen Skeggs 		nvkm_timer_alarm(tmr, 1000000000ULL, alarm);
190c39f472eSBen Skeggs }
191c39f472eSBen Skeggs 
192c39f472eSBen Skeggs void
nvkm_therm_program_alarms_polling(struct nvkm_therm * therm)19357113c01SBen Skeggs nvkm_therm_program_alarms_polling(struct nvkm_therm *therm)
194c39f472eSBen Skeggs {
195da06b46bSBen Skeggs 	struct nvbios_therm_sensor *sensor = &therm->bios_sensor;
196c39f472eSBen Skeggs 
19757113c01SBen Skeggs 	nvkm_debug(&therm->subdev,
198c39f472eSBen Skeggs 		   "programmed thresholds [ %d(%d), %d(%d), %d(%d), %d(%d) ]\n",
199b3c418bbSBen Skeggs 		   sensor->thrs_fan_boost.temp,
200b3c418bbSBen Skeggs 		   sensor->thrs_fan_boost.hysteresis,
201c39f472eSBen Skeggs 		   sensor->thrs_down_clock.temp,
202c39f472eSBen Skeggs 		   sensor->thrs_down_clock.hysteresis,
203b3c418bbSBen Skeggs 		   sensor->thrs_critical.temp,
204b3c418bbSBen Skeggs 		   sensor->thrs_critical.hysteresis,
205b3c418bbSBen Skeggs 		   sensor->thrs_shutdown.temp,
206b3c418bbSBen Skeggs 		   sensor->thrs_shutdown.hysteresis);
207c39f472eSBen Skeggs 
208da06b46bSBen Skeggs 	alarm_timer_callback(&therm->sensor.therm_poll_alarm);
209c39f472eSBen Skeggs }
210c39f472eSBen Skeggs 
211c39f472eSBen Skeggs int
nvkm_therm_sensor_init(struct nvkm_therm * therm)21257113c01SBen Skeggs nvkm_therm_sensor_init(struct nvkm_therm *therm)
213c39f472eSBen Skeggs {
21457113c01SBen Skeggs 	therm->func->program_alarms(therm);
215c39f472eSBen Skeggs 	return 0;
216c39f472eSBen Skeggs }
217c39f472eSBen Skeggs 
218c39f472eSBen Skeggs int
nvkm_therm_sensor_fini(struct nvkm_therm * therm,bool suspend)21957113c01SBen Skeggs nvkm_therm_sensor_fini(struct nvkm_therm *therm, bool suspend)
220c39f472eSBen Skeggs {
22157113c01SBen Skeggs 	struct nvkm_timer *tmr = therm->subdev.device->timer;
222c39f472eSBen Skeggs 	if (suspend)
2237eaf1198SBen Skeggs 		nvkm_timer_alarm(tmr, 0, &therm->sensor.therm_poll_alarm);
224c39f472eSBen Skeggs 	return 0;
225c39f472eSBen Skeggs }
226c39f472eSBen Skeggs 
227c39f472eSBen Skeggs void
nvkm_therm_sensor_preinit(struct nvkm_therm * therm)228e1404611SBen Skeggs nvkm_therm_sensor_preinit(struct nvkm_therm *therm)
229c39f472eSBen Skeggs {
230c39f472eSBen Skeggs 	const char *sensor_avail = "yes";
231c39f472eSBen Skeggs 
23257113c01SBen Skeggs 	if (therm->func->temp_get(therm) < 0)
233c39f472eSBen Skeggs 		sensor_avail = "no";
234c39f472eSBen Skeggs 
235b3c418bbSBen Skeggs 	nvkm_debug(&therm->subdev, "internal sensor: %s\n", sensor_avail);
236c39f472eSBen Skeggs }
237c39f472eSBen Skeggs 
238c39f472eSBen Skeggs int
nvkm_therm_sensor_ctor(struct nvkm_therm * therm)23957113c01SBen Skeggs nvkm_therm_sensor_ctor(struct nvkm_therm *therm)
240c39f472eSBen Skeggs {
24157113c01SBen Skeggs 	struct nvkm_subdev *subdev = &therm->subdev;
24246484438SBen Skeggs 	struct nvkm_bios *bios = subdev->device->bios;
243c39f472eSBen Skeggs 
244da06b46bSBen Skeggs 	nvkm_alarm_init(&therm->sensor.therm_poll_alarm, alarm_timer_callback);
245c39f472eSBen Skeggs 
24657113c01SBen Skeggs 	nvkm_therm_temp_set_defaults(therm);
247c39f472eSBen Skeggs 	if (nvbios_therm_sensor_parse(bios, NVBIOS_THERM_DOMAIN_CORE,
248da06b46bSBen Skeggs 				      &therm->bios_sensor))
249b3c418bbSBen Skeggs 		nvkm_error(subdev, "nvbios_therm_sensor_parse failed\n");
25057113c01SBen Skeggs 	nvkm_therm_temp_safety_checks(therm);
251c39f472eSBen Skeggs 
252c39f472eSBen Skeggs 	return 0;
253c39f472eSBen Skeggs }
254