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