xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/nouveau/nvkm/subdev/therm/nouveau_nvkm_subdev_therm_fan.c (revision 798b8d11ecd8257a8e35c3396210f98abf3d9ade)
1 /*	$NetBSD: nouveau_nvkm_subdev_therm_fan.c,v 1.4 2021/12/19 11:34:46 riastradh Exp $	*/
2 
3 /*
4  * Copyright 2012 Red Hat Inc.
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: Ben Skeggs
25  * 	    Martin Peres
26  */
27 #include <sys/cdefs.h>
28 __KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_therm_fan.c,v 1.4 2021/12/19 11:34:46 riastradh Exp $");
29 
30 #include "priv.h"
31 
32 #include <subdev/bios/fan.h>
33 #include <subdev/gpio.h>
34 #include <subdev/timer.h>
35 
36 static int
nvkm_fan_update(struct nvkm_fan * fan,bool immediate,int target)37 nvkm_fan_update(struct nvkm_fan *fan, bool immediate, int target)
38 {
39 	struct nvkm_therm *therm = fan->parent;
40 	struct nvkm_subdev *subdev = &therm->subdev;
41 	struct nvkm_timer *tmr = subdev->device->timer;
42 	unsigned long flags;
43 	int ret = 0;
44 	int duty;
45 
46 	/* update target fan speed, restricting to allowed range */
47 	spin_lock_irqsave(&fan->lock, flags);
48 	if (target < 0)
49 		target = fan->percent;
50 	target = max_t(u8, target, fan->bios.min_duty);
51 	target = min_t(u8, target, fan->bios.max_duty);
52 	if (fan->percent != target) {
53 #if 0 /* XXXMRG one log per second is a little excessive */
54 		nvkm_debug(subdev, "FAN target: %d\n", target);
55 #endif
56 		fan->percent = target;
57 	}
58 
59 	/* check that we're not already at the target duty cycle */
60 	duty = fan->get(therm);
61 	if (duty == target) {
62 		spin_unlock_irqrestore(&fan->lock, flags);
63 		return 0;
64 	}
65 
66 	/* smooth out the fanspeed increase/decrease */
67 	if (!immediate && duty >= 0) {
68 		/* the constant "3" is a rough approximation taken from
69 		 * nvidia's behaviour.
70 		 * it is meant to bump the fan speed more incrementally
71 		 */
72 		if (duty < target)
73 			duty = min(duty + 3, target);
74 		else if (duty > target)
75 			duty = max(duty - 3, target);
76 	} else {
77 		duty = target;
78 	}
79 
80 #if 0 /* XXXMRG one log per second is a little excessive */
81 	nvkm_debug(subdev, "FAN update: %d\n", duty);
82 #endif
83 	ret = fan->set(therm, duty);
84 	if (ret) {
85 		spin_unlock_irqrestore(&fan->lock, flags);
86 		return ret;
87 	}
88 
89 	/* fan speed updated, drop the fan lock before grabbing the
90 	 * alarm-scheduling lock and risking a deadlock
91 	 */
92 	spin_unlock_irqrestore(&fan->lock, flags);
93 
94 	/* schedule next fan update, if not at target speed already */
95 	if (target != duty) {
96 		u16 bump_period = fan->bios.bump_period;
97 		u16 slow_down_period = fan->bios.slow_down_period;
98 		u64 delay;
99 
100 		if (duty > target)
101 			delay = slow_down_period;
102 		else if (duty == target)
103 			delay = min(bump_period, slow_down_period) ;
104 		else
105 			delay = bump_period;
106 
107 		nvkm_timer_alarm(tmr, delay * 1000 * 1000, &fan->alarm);
108 	}
109 
110 	return ret;
111 }
112 
113 static void
nvkm_fan_alarm(struct nvkm_alarm * alarm)114 nvkm_fan_alarm(struct nvkm_alarm *alarm)
115 {
116 	struct nvkm_fan *fan = container_of(alarm, struct nvkm_fan, alarm);
117 	nvkm_fan_update(fan, false, -1);
118 }
119 
120 int
nvkm_therm_fan_get(struct nvkm_therm * therm)121 nvkm_therm_fan_get(struct nvkm_therm *therm)
122 {
123 	return therm->fan->get(therm);
124 }
125 
126 int
nvkm_therm_fan_set(struct nvkm_therm * therm,bool immediate,int percent)127 nvkm_therm_fan_set(struct nvkm_therm *therm, bool immediate, int percent)
128 {
129 	return nvkm_fan_update(therm->fan, immediate, percent);
130 }
131 
132 int
nvkm_therm_fan_sense(struct nvkm_therm * therm)133 nvkm_therm_fan_sense(struct nvkm_therm *therm)
134 {
135 	struct nvkm_device *device = therm->subdev.device;
136 	struct nvkm_timer *tmr = device->timer;
137 	struct nvkm_gpio *gpio = device->gpio;
138 	u32 cycles, cur, prev;
139 	u64 start, end, tach;
140 
141 	if (therm->func->fan_sense)
142 		return therm->func->fan_sense(therm);
143 
144 	if (therm->fan->tach.func == DCB_GPIO_UNUSED)
145 		return -ENODEV;
146 
147 	/* Time a complete rotation and extrapolate to RPM:
148 	 * When the fan spins, it changes the value of GPIO FAN_SENSE.
149 	 * We get 4 changes (0 -> 1 -> 0 -> 1) per complete rotation.
150 	 */
151 	start = nvkm_timer_read(tmr);
152 	prev = nvkm_gpio_get(gpio, 0, therm->fan->tach.func,
153 				      therm->fan->tach.line);
154 	cycles = 0;
155 	do {
156 		usleep_range(500, 1000); /* supports 0 < rpm < 7500 */
157 
158 		cur = nvkm_gpio_get(gpio, 0, therm->fan->tach.func,
159 					     therm->fan->tach.line);
160 		if (prev != cur) {
161 			if (!start)
162 				start = nvkm_timer_read(tmr);
163 			cycles++;
164 			prev = cur;
165 		}
166 	} while (cycles < 5 && nvkm_timer_read(tmr) - start < 250000000);
167 	end = nvkm_timer_read(tmr);
168 
169 	if (cycles == 5) {
170 		tach = (u64)60000000000ULL;
171 		do_div(tach, (end - start));
172 		return tach;
173 	} else
174 		return 0;
175 }
176 
177 int
nvkm_therm_fan_user_get(struct nvkm_therm * therm)178 nvkm_therm_fan_user_get(struct nvkm_therm *therm)
179 {
180 	return nvkm_therm_fan_get(therm);
181 }
182 
183 int
nvkm_therm_fan_user_set(struct nvkm_therm * therm,int percent)184 nvkm_therm_fan_user_set(struct nvkm_therm *therm, int percent)
185 {
186 	if (therm->mode != NVKM_THERM_CTRL_MANUAL)
187 		return -EINVAL;
188 
189 	return nvkm_therm_fan_set(therm, true, percent);
190 }
191 
192 static void
nvkm_therm_fan_set_defaults(struct nvkm_therm * therm)193 nvkm_therm_fan_set_defaults(struct nvkm_therm *therm)
194 {
195 	therm->fan->bios.pwm_freq = 0;
196 	therm->fan->bios.min_duty = 0;
197 	therm->fan->bios.max_duty = 100;
198 	therm->fan->bios.bump_period = 500;
199 	therm->fan->bios.slow_down_period = 2000;
200 	therm->fan->bios.linear_min_temp = 40;
201 	therm->fan->bios.linear_max_temp = 85;
202 }
203 
204 static void
nvkm_therm_fan_safety_checks(struct nvkm_therm * therm)205 nvkm_therm_fan_safety_checks(struct nvkm_therm *therm)
206 {
207 	if (therm->fan->bios.min_duty > 100)
208 		therm->fan->bios.min_duty = 100;
209 	if (therm->fan->bios.max_duty > 100)
210 		therm->fan->bios.max_duty = 100;
211 
212 	if (therm->fan->bios.min_duty > therm->fan->bios.max_duty)
213 		therm->fan->bios.min_duty = therm->fan->bios.max_duty;
214 }
215 
216 int
nvkm_therm_fan_init(struct nvkm_therm * therm)217 nvkm_therm_fan_init(struct nvkm_therm *therm)
218 {
219 	return 0;
220 }
221 
222 int
nvkm_therm_fan_fini(struct nvkm_therm * therm,bool suspend)223 nvkm_therm_fan_fini(struct nvkm_therm *therm, bool suspend)
224 {
225 	struct nvkm_timer *tmr = therm->subdev.device->timer;
226 	if (suspend)
227 		nvkm_timer_alarm(tmr, 0, &therm->fan->alarm);
228 	return 0;
229 }
230 
231 void
nvkm_therm_fan_dtor(struct nvkm_therm * therm)232 nvkm_therm_fan_dtor(struct nvkm_therm *therm)
233 {
234 	if (therm->fan->dtor)
235 		therm->fan->dtor(therm->fan);
236 	spin_lock_destroy(&therm->fan->lock);
237 }
238 
239 int
nvkm_therm_fan_ctor(struct nvkm_therm * therm)240 nvkm_therm_fan_ctor(struct nvkm_therm *therm)
241 {
242 	struct nvkm_subdev *subdev = &therm->subdev;
243 	struct nvkm_device *device = subdev->device;
244 	struct nvkm_gpio *gpio = device->gpio;
245 	struct nvkm_bios *bios = device->bios;
246 	struct dcb_gpio_func func;
247 	int ret;
248 
249 	/* attempt to locate a drivable fan, and determine control method */
250 	ret = nvkm_gpio_find(gpio, 0, DCB_GPIO_FAN, 0xff, &func);
251 	if (ret == 0) {
252 		/* FIXME: is this really the place to perform such checks ? */
253 		if (func.line != 16 && func.log[0] & DCB_GPIO_LOG_DIR_IN) {
254 			nvkm_debug(subdev, "GPIO_FAN is in input mode\n");
255 			ret = -EINVAL;
256 		} else {
257 			ret = nvkm_fanpwm_create(therm, &func);
258 			if (ret != 0)
259 				ret = nvkm_fantog_create(therm, &func);
260 		}
261 	}
262 
263 	/* no controllable fan found, create a dummy fan module */
264 	if (ret != 0) {
265 		ret = nvkm_fannil_create(therm);
266 		if (ret)
267 			return ret;
268 	}
269 
270 	nvkm_debug(subdev, "FAN control: %s\n", therm->fan->type);
271 
272 	/* read the current speed, it is useful when resuming */
273 	therm->fan->percent = nvkm_therm_fan_get(therm);
274 
275 	/* attempt to detect a tachometer connection */
276 	ret = nvkm_gpio_find(gpio, 0, DCB_GPIO_FAN_SENSE, 0xff,
277 			     &therm->fan->tach);
278 	if (ret)
279 		therm->fan->tach.func = DCB_GPIO_UNUSED;
280 
281 	/* initialise fan bump/slow update handling */
282 	therm->fan->parent = therm;
283 	nvkm_alarm_init(&therm->fan->alarm, nvkm_fan_alarm);
284 	spin_lock_init(&therm->fan->lock);
285 
286 	/* other random init... */
287 	nvkm_therm_fan_set_defaults(therm);
288 	nvbios_perf_fan_parse(bios, &therm->fan->perf);
289 	if (!nvbios_fan_parse(bios, &therm->fan->bios)) {
290 		nvkm_debug(subdev, "parsing the fan table failed\n");
291 		if (nvbios_therm_fan_parse(bios, &therm->fan->bios))
292 			nvkm_error(subdev, "parsing both fan tables failed\n");
293 	}
294 	nvkm_therm_fan_safety_checks(therm);
295 	return 0;
296 }
297