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 int
27 nvkm_therm_update_trip(struct nvkm_therm *obj)
28 {
29 	struct nvkm_therm_priv *therm = container_of(obj, typeof(*therm), base);
30 	struct nvbios_therm_trip_point *trip = therm->fan->bios.trip,
31 				       *cur_trip = NULL,
32 				       *last_trip = therm->last_trip;
33 	u8  temp = therm->base.temp_get(&therm->base);
34 	u16 duty, i;
35 
36 	/* look for the trip point corresponding to the current temperature */
37 	cur_trip = NULL;
38 	for (i = 0; i < therm->fan->bios.nr_fan_trip; i++) {
39 		if (temp >= trip[i].temp)
40 			cur_trip = &trip[i];
41 	}
42 
43 	/* account for the hysteresis cycle */
44 	if (last_trip && temp <= (last_trip->temp) &&
45 	    temp > (last_trip->temp - last_trip->hysteresis))
46 		cur_trip = last_trip;
47 
48 	if (cur_trip) {
49 		duty = cur_trip->fan_duty;
50 		therm->last_trip = cur_trip;
51 	} else {
52 		duty = 0;
53 		therm->last_trip = NULL;
54 	}
55 
56 	return duty;
57 }
58 
59 static int
60 nvkm_therm_update_linear(struct nvkm_therm *obj)
61 {
62 	struct nvkm_therm_priv *therm = container_of(obj, typeof(*therm), base);
63 	u8  linear_min_temp = therm->fan->bios.linear_min_temp;
64 	u8  linear_max_temp = therm->fan->bios.linear_max_temp;
65 	u8  temp = therm->base.temp_get(&therm->base);
66 	u16 duty;
67 
68 	/* handle the non-linear part first */
69 	if (temp < linear_min_temp)
70 		return therm->fan->bios.min_duty;
71 	else if (temp > linear_max_temp)
72 		return therm->fan->bios.max_duty;
73 
74 	/* we are in the linear zone */
75 	duty  = (temp - linear_min_temp);
76 	duty *= (therm->fan->bios.max_duty - therm->fan->bios.min_duty);
77 	duty /= (linear_max_temp - linear_min_temp);
78 	duty += therm->fan->bios.min_duty;
79 	return duty;
80 }
81 
82 static void
83 nvkm_therm_update(struct nvkm_therm *obj, int mode)
84 {
85 	struct nvkm_therm_priv *therm = container_of(obj, typeof(*therm), base);
86 	struct nvkm_timer *tmr = nvkm_timer(therm);
87 	unsigned long flags;
88 	bool immd = true;
89 	bool poll = true;
90 	int duty = -1;
91 
92 	spin_lock_irqsave(&therm->lock, flags);
93 	if (mode < 0)
94 		mode = therm->mode;
95 	therm->mode = mode;
96 
97 	switch (mode) {
98 	case NVKM_THERM_CTRL_MANUAL:
99 		tmr->alarm_cancel(tmr, &therm->alarm);
100 		duty = nvkm_therm_fan_get(&therm->base);
101 		if (duty < 0)
102 			duty = 100;
103 		poll = false;
104 		break;
105 	case NVKM_THERM_CTRL_AUTO:
106 		switch(therm->fan->bios.fan_mode) {
107 		case NVBIOS_THERM_FAN_TRIP:
108 			duty = nvkm_therm_update_trip(&therm->base);
109 			break;
110 		case NVBIOS_THERM_FAN_LINEAR:
111 			duty = nvkm_therm_update_linear(&therm->base);
112 			break;
113 		case NVBIOS_THERM_FAN_OTHER:
114 			if (therm->cstate)
115 				duty = therm->cstate;
116 			poll = false;
117 			break;
118 		}
119 		immd = false;
120 		break;
121 	case NVKM_THERM_CTRL_NONE:
122 	default:
123 		tmr->alarm_cancel(tmr, &therm->alarm);
124 		poll = false;
125 	}
126 
127 	if (list_empty(&therm->alarm.head) && poll)
128 		tmr->alarm(tmr, 1000000000ULL, &therm->alarm);
129 	spin_unlock_irqrestore(&therm->lock, flags);
130 
131 	if (duty >= 0) {
132 		nv_debug(therm, "FAN target request: %d%%\n", duty);
133 		nvkm_therm_fan_set(&therm->base, immd, duty);
134 	}
135 }
136 
137 int
138 nvkm_therm_cstate(struct nvkm_therm *obj, int fan, int dir)
139 {
140 	struct nvkm_therm_priv *therm = container_of(obj, typeof(*therm), base);
141 	if (!dir || (dir < 0 && fan < therm->cstate) ||
142 		    (dir > 0 && fan > therm->cstate)) {
143 		nv_debug(therm, "default fan speed -> %d%%\n", fan);
144 		therm->cstate = fan;
145 		nvkm_therm_update(&therm->base, -1);
146 	}
147 	return 0;
148 }
149 
150 static void
151 nvkm_therm_alarm(struct nvkm_alarm *alarm)
152 {
153 	struct nvkm_therm_priv *therm =
154 	       container_of(alarm, struct nvkm_therm_priv, alarm);
155 	nvkm_therm_update(&therm->base, -1);
156 }
157 
158 int
159 nvkm_therm_fan_mode(struct nvkm_therm *obj, int mode)
160 {
161 	struct nvkm_therm_priv *therm = container_of(obj, typeof(*therm), base);
162 	struct nvkm_device *device = nv_device(therm);
163 	static const char *name[] = {
164 		"disabled",
165 		"manual",
166 		"automatic"
167 	};
168 
169 	/* The default PPWR ucode on fermi interferes with fan management */
170 	if ((mode >= ARRAY_SIZE(name)) ||
171 	    (mode != NVKM_THERM_CTRL_NONE && device->card_type >= NV_C0 &&
172 	     !nvkm_subdev(device, NVDEV_SUBDEV_PMU)))
173 		return -EINVAL;
174 
175 	/* do not allow automatic fan management if the thermal sensor is
176 	 * not available */
177 	if (mode == NVKM_THERM_CTRL_AUTO &&
178 	    therm->base.temp_get(&therm->base) < 0)
179 		return -EINVAL;
180 
181 	if (therm->mode == mode)
182 		return 0;
183 
184 	nv_info(therm, "fan management: %s\n", name[mode]);
185 	nvkm_therm_update(&therm->base, mode);
186 	return 0;
187 }
188 
189 int
190 nvkm_therm_attr_get(struct nvkm_therm *obj, enum nvkm_therm_attr_type type)
191 {
192 	struct nvkm_therm_priv *therm = container_of(obj, typeof(*therm), base);
193 
194 	switch (type) {
195 	case NVKM_THERM_ATTR_FAN_MIN_DUTY:
196 		return therm->fan->bios.min_duty;
197 	case NVKM_THERM_ATTR_FAN_MAX_DUTY:
198 		return therm->fan->bios.max_duty;
199 	case NVKM_THERM_ATTR_FAN_MODE:
200 		return therm->mode;
201 	case NVKM_THERM_ATTR_THRS_FAN_BOOST:
202 		return therm->bios_sensor.thrs_fan_boost.temp;
203 	case NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST:
204 		return therm->bios_sensor.thrs_fan_boost.hysteresis;
205 	case NVKM_THERM_ATTR_THRS_DOWN_CLK:
206 		return therm->bios_sensor.thrs_down_clock.temp;
207 	case NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST:
208 		return therm->bios_sensor.thrs_down_clock.hysteresis;
209 	case NVKM_THERM_ATTR_THRS_CRITICAL:
210 		return therm->bios_sensor.thrs_critical.temp;
211 	case NVKM_THERM_ATTR_THRS_CRITICAL_HYST:
212 		return therm->bios_sensor.thrs_critical.hysteresis;
213 	case NVKM_THERM_ATTR_THRS_SHUTDOWN:
214 		return therm->bios_sensor.thrs_shutdown.temp;
215 	case NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST:
216 		return therm->bios_sensor.thrs_shutdown.hysteresis;
217 	}
218 
219 	return -EINVAL;
220 }
221 
222 int
223 nvkm_therm_attr_set(struct nvkm_therm *obj,
224 		    enum nvkm_therm_attr_type type, int value)
225 {
226 	struct nvkm_therm_priv *therm = container_of(obj, typeof(*therm), base);
227 
228 	switch (type) {
229 	case NVKM_THERM_ATTR_FAN_MIN_DUTY:
230 		if (value < 0)
231 			value = 0;
232 		if (value > therm->fan->bios.max_duty)
233 			value = therm->fan->bios.max_duty;
234 		therm->fan->bios.min_duty = value;
235 		return 0;
236 	case NVKM_THERM_ATTR_FAN_MAX_DUTY:
237 		if (value < 0)
238 			value = 0;
239 		if (value < therm->fan->bios.min_duty)
240 			value = therm->fan->bios.min_duty;
241 		therm->fan->bios.max_duty = value;
242 		return 0;
243 	case NVKM_THERM_ATTR_FAN_MODE:
244 		return nvkm_therm_fan_mode(&therm->base, value);
245 	case NVKM_THERM_ATTR_THRS_FAN_BOOST:
246 		therm->bios_sensor.thrs_fan_boost.temp = value;
247 		therm->sensor.program_alarms(&therm->base);
248 		return 0;
249 	case NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST:
250 		therm->bios_sensor.thrs_fan_boost.hysteresis = value;
251 		therm->sensor.program_alarms(&therm->base);
252 		return 0;
253 	case NVKM_THERM_ATTR_THRS_DOWN_CLK:
254 		therm->bios_sensor.thrs_down_clock.temp = value;
255 		therm->sensor.program_alarms(&therm->base);
256 		return 0;
257 	case NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST:
258 		therm->bios_sensor.thrs_down_clock.hysteresis = value;
259 		therm->sensor.program_alarms(&therm->base);
260 		return 0;
261 	case NVKM_THERM_ATTR_THRS_CRITICAL:
262 		therm->bios_sensor.thrs_critical.temp = value;
263 		therm->sensor.program_alarms(&therm->base);
264 		return 0;
265 	case NVKM_THERM_ATTR_THRS_CRITICAL_HYST:
266 		therm->bios_sensor.thrs_critical.hysteresis = value;
267 		therm->sensor.program_alarms(&therm->base);
268 		return 0;
269 	case NVKM_THERM_ATTR_THRS_SHUTDOWN:
270 		therm->bios_sensor.thrs_shutdown.temp = value;
271 		therm->sensor.program_alarms(&therm->base);
272 		return 0;
273 	case NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST:
274 		therm->bios_sensor.thrs_shutdown.hysteresis = value;
275 		therm->sensor.program_alarms(&therm->base);
276 		return 0;
277 	}
278 
279 	return -EINVAL;
280 }
281 
282 int
283 _nvkm_therm_init(struct nvkm_object *object)
284 {
285 	struct nvkm_therm_priv *therm = (void *)object;
286 	int ret;
287 
288 	ret = nvkm_subdev_init(&therm->base.subdev);
289 	if (ret)
290 		return ret;
291 
292 	if (therm->suspend >= 0) {
293 		/* restore the pwm value only when on manual or auto mode */
294 		if (therm->suspend > 0)
295 			nvkm_therm_fan_set(&therm->base, true, therm->fan->percent);
296 
297 		nvkm_therm_fan_mode(&therm->base, therm->suspend);
298 	}
299 	nvkm_therm_sensor_init(&therm->base);
300 	nvkm_therm_fan_init(&therm->base);
301 	return 0;
302 }
303 
304 int
305 _nvkm_therm_fini(struct nvkm_object *object, bool suspend)
306 {
307 	struct nvkm_therm_priv *therm = (void *)object;
308 
309 	nvkm_therm_fan_fini(&therm->base, suspend);
310 	nvkm_therm_sensor_fini(&therm->base, suspend);
311 	if (suspend) {
312 		therm->suspend = therm->mode;
313 		therm->mode = NVKM_THERM_CTRL_NONE;
314 	}
315 
316 	return nvkm_subdev_fini(&therm->base.subdev, suspend);
317 }
318 
319 int
320 nvkm_therm_create_(struct nvkm_object *parent, struct nvkm_object *engine,
321 		   struct nvkm_oclass *oclass, int length, void **pobject)
322 {
323 	struct nvkm_therm_priv *therm;
324 	int ret;
325 
326 	ret = nvkm_subdev_create_(parent, engine, oclass, 0, "PTHERM",
327 				  "therm", length, pobject);
328 	therm = *pobject;
329 	if (ret)
330 		return ret;
331 
332 	nvkm_alarm_init(&therm->alarm, nvkm_therm_alarm);
333 	spin_lock_init(&therm->lock);
334 	spin_lock_init(&therm->sensor.alarm_program_lock);
335 
336 	therm->base.fan_get = nvkm_therm_fan_user_get;
337 	therm->base.fan_set = nvkm_therm_fan_user_set;
338 	therm->base.fan_sense = nvkm_therm_fan_sense;
339 	therm->base.attr_get = nvkm_therm_attr_get;
340 	therm->base.attr_set = nvkm_therm_attr_set;
341 	therm->mode = therm->suspend = -1; /* undefined */
342 	return 0;
343 }
344 
345 int
346 nvkm_therm_preinit(struct nvkm_therm *therm)
347 {
348 	nvkm_therm_sensor_ctor(therm);
349 	nvkm_therm_ic_ctor(therm);
350 	nvkm_therm_fan_ctor(therm);
351 
352 	nvkm_therm_fan_mode(therm, NVKM_THERM_CTRL_AUTO);
353 	nvkm_therm_sensor_preinit(therm);
354 	return 0;
355 }
356 
357 void
358 _nvkm_therm_dtor(struct nvkm_object *object)
359 {
360 	struct nvkm_therm_priv *therm = (void *)object;
361 	kfree(therm->fan);
362 	nvkm_subdev_destroy(&therm->base.subdev);
363 }
364