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 #include <core/option.h>
27 #include <subdev/pmu.h>
28 
29 int
30 nvkm_therm_temp_get(struct nvkm_therm *therm)
31 {
32 	if (therm->func->temp_get)
33 		return therm->func->temp_get(therm);
34 	return -ENODEV;
35 }
36 
37 static int
38 nvkm_therm_update_trip(struct nvkm_therm *therm)
39 {
40 	struct nvbios_therm_trip_point *trip = therm->fan->bios.trip,
41 				       *cur_trip = NULL,
42 				       *last_trip = therm->last_trip;
43 	u8  temp = therm->func->temp_get(therm);
44 	u16 duty, i;
45 
46 	/* look for the trip point corresponding to the current temperature */
47 	cur_trip = NULL;
48 	for (i = 0; i < therm->fan->bios.nr_fan_trip; i++) {
49 		if (temp >= trip[i].temp)
50 			cur_trip = &trip[i];
51 	}
52 
53 	/* account for the hysteresis cycle */
54 	if (last_trip && temp <= (last_trip->temp) &&
55 	    temp > (last_trip->temp - last_trip->hysteresis))
56 		cur_trip = last_trip;
57 
58 	if (cur_trip) {
59 		duty = cur_trip->fan_duty;
60 		therm->last_trip = cur_trip;
61 	} else {
62 		duty = 0;
63 		therm->last_trip = NULL;
64 	}
65 
66 	return duty;
67 }
68 
69 static int
70 nvkm_therm_compute_linear_duty(struct nvkm_therm *therm, u8 linear_min_temp,
71                                u8 linear_max_temp)
72 {
73 	u8  temp = therm->func->temp_get(therm);
74 	u16 duty;
75 
76 	/* handle the non-linear part first */
77 	if (temp < linear_min_temp)
78 		return therm->fan->bios.min_duty;
79 	else if (temp > linear_max_temp)
80 		return therm->fan->bios.max_duty;
81 
82 	/* we are in the linear zone */
83 	duty  = (temp - linear_min_temp);
84 	duty *= (therm->fan->bios.max_duty - therm->fan->bios.min_duty);
85 	duty /= (linear_max_temp - linear_min_temp);
86 	duty += therm->fan->bios.min_duty;
87 	return duty;
88 }
89 
90 static int
91 nvkm_therm_update_linear(struct nvkm_therm *therm)
92 {
93 	u8  min = therm->fan->bios.linear_min_temp;
94 	u8  max = therm->fan->bios.linear_max_temp;
95 	return nvkm_therm_compute_linear_duty(therm, min, max);
96 }
97 
98 static int
99 nvkm_therm_update_linear_fallback(struct nvkm_therm *therm)
100 {
101 	u8 max = therm->bios_sensor.thrs_fan_boost.temp;
102 	return nvkm_therm_compute_linear_duty(therm, 30, max);
103 }
104 
105 static void
106 nvkm_therm_update(struct nvkm_therm *therm, int mode)
107 {
108 	struct nvkm_subdev *subdev = &therm->subdev;
109 	struct nvkm_timer *tmr = subdev->device->timer;
110 	unsigned long flags;
111 	bool immd = true;
112 	bool poll = true;
113 	int duty = -1;
114 
115 	spin_lock_irqsave(&therm->lock, flags);
116 	if (mode < 0)
117 		mode = therm->mode;
118 	therm->mode = mode;
119 
120 	switch (mode) {
121 	case NVKM_THERM_CTRL_MANUAL:
122 		nvkm_timer_alarm(tmr, 0, &therm->alarm);
123 		duty = nvkm_therm_fan_get(therm);
124 		if (duty < 0)
125 			duty = 100;
126 		poll = false;
127 		break;
128 	case NVKM_THERM_CTRL_AUTO:
129 		switch(therm->fan->bios.fan_mode) {
130 		case NVBIOS_THERM_FAN_TRIP:
131 			duty = nvkm_therm_update_trip(therm);
132 			break;
133 		case NVBIOS_THERM_FAN_LINEAR:
134 			duty = nvkm_therm_update_linear(therm);
135 			break;
136 		case NVBIOS_THERM_FAN_OTHER:
137 			if (therm->cstate) {
138 				duty = therm->cstate;
139 				poll = false;
140 			} else {
141 				duty = nvkm_therm_update_linear_fallback(therm);
142 			}
143 			break;
144 		}
145 		immd = false;
146 		break;
147 	case NVKM_THERM_CTRL_NONE:
148 	default:
149 		nvkm_timer_alarm(tmr, 0, &therm->alarm);
150 		poll = false;
151 	}
152 
153 	if (poll)
154 		nvkm_timer_alarm(tmr, 1000000000ULL, &therm->alarm);
155 	spin_unlock_irqrestore(&therm->lock, flags);
156 
157 	if (duty >= 0) {
158 		nvkm_debug(subdev, "FAN target request: %d%%\n", duty);
159 		nvkm_therm_fan_set(therm, immd, duty);
160 	}
161 }
162 
163 int
164 nvkm_therm_cstate(struct nvkm_therm *therm, int fan, int dir)
165 {
166 	struct nvkm_subdev *subdev = &therm->subdev;
167 	if (!dir || (dir < 0 && fan < therm->cstate) ||
168 		    (dir > 0 && fan > therm->cstate)) {
169 		nvkm_debug(subdev, "default fan speed -> %d%%\n", fan);
170 		therm->cstate = fan;
171 		nvkm_therm_update(therm, -1);
172 	}
173 	return 0;
174 }
175 
176 static void
177 nvkm_therm_alarm(struct nvkm_alarm *alarm)
178 {
179 	struct nvkm_therm *therm =
180 	       container_of(alarm, struct nvkm_therm, alarm);
181 	nvkm_therm_update(therm, -1);
182 }
183 
184 int
185 nvkm_therm_fan_mode(struct nvkm_therm *therm, int mode)
186 {
187 	struct nvkm_subdev *subdev = &therm->subdev;
188 	struct nvkm_device *device = subdev->device;
189 	static const char *name[] = {
190 		"disabled",
191 		"manual",
192 		"automatic"
193 	};
194 
195 	/* The default PPWR ucode on fermi interferes with fan management */
196 	if ((mode >= ARRAY_SIZE(name)) ||
197 	    (mode != NVKM_THERM_CTRL_NONE && nvkm_pmu_fan_controlled(device)))
198 		return -EINVAL;
199 
200 	/* do not allow automatic fan management if the thermal sensor is
201 	 * not available */
202 	if (mode == NVKM_THERM_CTRL_AUTO &&
203 	    therm->func->temp_get(therm) < 0)
204 		return -EINVAL;
205 
206 	if (therm->mode == mode)
207 		return 0;
208 
209 	nvkm_debug(subdev, "fan management: %s\n", name[mode]);
210 	nvkm_therm_update(therm, mode);
211 	return 0;
212 }
213 
214 int
215 nvkm_therm_attr_get(struct nvkm_therm *therm, enum nvkm_therm_attr_type type)
216 {
217 	switch (type) {
218 	case NVKM_THERM_ATTR_FAN_MIN_DUTY:
219 		return therm->fan->bios.min_duty;
220 	case NVKM_THERM_ATTR_FAN_MAX_DUTY:
221 		return therm->fan->bios.max_duty;
222 	case NVKM_THERM_ATTR_FAN_MODE:
223 		return therm->mode;
224 	case NVKM_THERM_ATTR_THRS_FAN_BOOST:
225 		return therm->bios_sensor.thrs_fan_boost.temp;
226 	case NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST:
227 		return therm->bios_sensor.thrs_fan_boost.hysteresis;
228 	case NVKM_THERM_ATTR_THRS_DOWN_CLK:
229 		return therm->bios_sensor.thrs_down_clock.temp;
230 	case NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST:
231 		return therm->bios_sensor.thrs_down_clock.hysteresis;
232 	case NVKM_THERM_ATTR_THRS_CRITICAL:
233 		return therm->bios_sensor.thrs_critical.temp;
234 	case NVKM_THERM_ATTR_THRS_CRITICAL_HYST:
235 		return therm->bios_sensor.thrs_critical.hysteresis;
236 	case NVKM_THERM_ATTR_THRS_SHUTDOWN:
237 		return therm->bios_sensor.thrs_shutdown.temp;
238 	case NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST:
239 		return therm->bios_sensor.thrs_shutdown.hysteresis;
240 	}
241 
242 	return -EINVAL;
243 }
244 
245 int
246 nvkm_therm_attr_set(struct nvkm_therm *therm,
247 		    enum nvkm_therm_attr_type type, int value)
248 {
249 	switch (type) {
250 	case NVKM_THERM_ATTR_FAN_MIN_DUTY:
251 		if (value < 0)
252 			value = 0;
253 		if (value > therm->fan->bios.max_duty)
254 			value = therm->fan->bios.max_duty;
255 		therm->fan->bios.min_duty = value;
256 		return 0;
257 	case NVKM_THERM_ATTR_FAN_MAX_DUTY:
258 		if (value < 0)
259 			value = 0;
260 		if (value < therm->fan->bios.min_duty)
261 			value = therm->fan->bios.min_duty;
262 		therm->fan->bios.max_duty = value;
263 		return 0;
264 	case NVKM_THERM_ATTR_FAN_MODE:
265 		return nvkm_therm_fan_mode(therm, value);
266 	case NVKM_THERM_ATTR_THRS_FAN_BOOST:
267 		therm->bios_sensor.thrs_fan_boost.temp = value;
268 		therm->func->program_alarms(therm);
269 		return 0;
270 	case NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST:
271 		therm->bios_sensor.thrs_fan_boost.hysteresis = value;
272 		therm->func->program_alarms(therm);
273 		return 0;
274 	case NVKM_THERM_ATTR_THRS_DOWN_CLK:
275 		therm->bios_sensor.thrs_down_clock.temp = value;
276 		therm->func->program_alarms(therm);
277 		return 0;
278 	case NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST:
279 		therm->bios_sensor.thrs_down_clock.hysteresis = value;
280 		therm->func->program_alarms(therm);
281 		return 0;
282 	case NVKM_THERM_ATTR_THRS_CRITICAL:
283 		therm->bios_sensor.thrs_critical.temp = value;
284 		therm->func->program_alarms(therm);
285 		return 0;
286 	case NVKM_THERM_ATTR_THRS_CRITICAL_HYST:
287 		therm->bios_sensor.thrs_critical.hysteresis = value;
288 		therm->func->program_alarms(therm);
289 		return 0;
290 	case NVKM_THERM_ATTR_THRS_SHUTDOWN:
291 		therm->bios_sensor.thrs_shutdown.temp = value;
292 		therm->func->program_alarms(therm);
293 		return 0;
294 	case NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST:
295 		therm->bios_sensor.thrs_shutdown.hysteresis = value;
296 		therm->func->program_alarms(therm);
297 		return 0;
298 	}
299 
300 	return -EINVAL;
301 }
302 
303 void
304 nvkm_therm_clkgate_enable(struct nvkm_therm *therm)
305 {
306 	if (!therm || !therm->func->clkgate_enable || !therm->clkgating_enabled)
307 		return;
308 
309 	nvkm_debug(&therm->subdev,
310 		   "Enabling clockgating\n");
311 	therm->func->clkgate_enable(therm);
312 }
313 
314 void
315 nvkm_therm_clkgate_fini(struct nvkm_therm *therm, bool suspend)
316 {
317 	if (!therm || !therm->func->clkgate_fini || !therm->clkgating_enabled)
318 		return;
319 
320 	nvkm_debug(&therm->subdev,
321 		   "Preparing clockgating for %s\n",
322 		   suspend ? "suspend" : "fini");
323 	therm->func->clkgate_fini(therm, suspend);
324 }
325 
326 static void
327 nvkm_therm_clkgate_oneinit(struct nvkm_therm *therm)
328 {
329 	if (!therm->func->clkgate_enable || !therm->clkgating_enabled)
330 		return;
331 
332 	nvkm_info(&therm->subdev, "Clockgating enabled\n");
333 }
334 
335 static void
336 nvkm_therm_intr(struct nvkm_subdev *subdev)
337 {
338 	struct nvkm_therm *therm = nvkm_therm(subdev);
339 	if (therm->func->intr)
340 		therm->func->intr(therm);
341 }
342 
343 static int
344 nvkm_therm_fini(struct nvkm_subdev *subdev, bool suspend)
345 {
346 	struct nvkm_therm *therm = nvkm_therm(subdev);
347 
348 	if (therm->func->fini)
349 		therm->func->fini(therm);
350 
351 	nvkm_therm_fan_fini(therm, suspend);
352 	nvkm_therm_sensor_fini(therm, suspend);
353 
354 	if (suspend) {
355 		therm->suspend = therm->mode;
356 		therm->mode = NVKM_THERM_CTRL_NONE;
357 	}
358 
359 	return 0;
360 }
361 
362 static int
363 nvkm_therm_oneinit(struct nvkm_subdev *subdev)
364 {
365 	struct nvkm_therm *therm = nvkm_therm(subdev);
366 	nvkm_therm_sensor_ctor(therm);
367 	nvkm_therm_ic_ctor(therm);
368 	nvkm_therm_fan_ctor(therm);
369 	nvkm_therm_fan_mode(therm, NVKM_THERM_CTRL_AUTO);
370 	nvkm_therm_sensor_preinit(therm);
371 	nvkm_therm_clkgate_oneinit(therm);
372 	return 0;
373 }
374 
375 static int
376 nvkm_therm_init(struct nvkm_subdev *subdev)
377 {
378 	struct nvkm_therm *therm = nvkm_therm(subdev);
379 
380 	if (therm->func->init)
381 		therm->func->init(therm);
382 
383 	if (therm->suspend >= 0) {
384 		/* restore the pwm value only when on manual or auto mode */
385 		if (therm->suspend > 0)
386 			nvkm_therm_fan_set(therm, true, therm->fan->percent);
387 
388 		nvkm_therm_fan_mode(therm, therm->suspend);
389 	}
390 
391 	nvkm_therm_sensor_init(therm);
392 	nvkm_therm_fan_init(therm);
393 	return 0;
394 }
395 
396 void
397 nvkm_therm_clkgate_init(struct nvkm_therm *therm,
398 			const struct nvkm_therm_clkgate_pack *p)
399 {
400 	if (!therm || !therm->func->clkgate_init || !therm->clkgating_enabled)
401 		return;
402 
403 	therm->func->clkgate_init(therm, p);
404 }
405 
406 static void *
407 nvkm_therm_dtor(struct nvkm_subdev *subdev)
408 {
409 	struct nvkm_therm *therm = nvkm_therm(subdev);
410 	kfree(therm->fan);
411 	return therm;
412 }
413 
414 static const struct nvkm_subdev_func
415 nvkm_therm = {
416 	.dtor = nvkm_therm_dtor,
417 	.oneinit = nvkm_therm_oneinit,
418 	.init = nvkm_therm_init,
419 	.fini = nvkm_therm_fini,
420 	.intr = nvkm_therm_intr,
421 };
422 
423 void
424 nvkm_therm_ctor(struct nvkm_therm *therm, struct nvkm_device *device, enum nvkm_subdev_type type,
425 		int inst, const struct nvkm_therm_func *func)
426 {
427 	nvkm_subdev_ctor(&nvkm_therm, device, type, inst, &therm->subdev);
428 	therm->func = func;
429 
430 	nvkm_alarm_init(&therm->alarm, nvkm_therm_alarm);
431 	spin_lock_init(&therm->lock);
432 	spin_lock_init(&therm->sensor.alarm_program_lock);
433 
434 	therm->fan_get = nvkm_therm_fan_user_get;
435 	therm->fan_set = nvkm_therm_fan_user_set;
436 	therm->attr_get = nvkm_therm_attr_get;
437 	therm->attr_set = nvkm_therm_attr_set;
438 	therm->mode = therm->suspend = -1; /* undefined */
439 
440 	therm->clkgating_enabled = nvkm_boolopt(device->cfgopt,
441 						"NvPmEnableGating", false);
442 }
443 
444 int
445 nvkm_therm_new_(const struct nvkm_therm_func *func, struct nvkm_device *device,
446 		enum nvkm_subdev_type type, int inst, struct nvkm_therm **ptherm)
447 {
448 	struct nvkm_therm *therm;
449 
450 	if (!(therm = *ptherm = kzalloc(sizeof(*therm), GFP_KERNEL)))
451 		return -ENOMEM;
452 
453 	nvkm_therm_ctor(therm, device, type, inst, func);
454 	return 0;
455 }
456