1 /*
2  * Copyright 2010 Red Hat Inc.
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: Ben Skeggs
23  */
24 
25 #include "drmP.h"
26 
27 #include "nouveau_drv.h"
28 #include "nouveau_pm.h"
29 #include "nouveau_gpio.h"
30 
31 #ifdef CONFIG_ACPI
32 #include <linux/acpi.h>
33 #endif
34 #include <linux/power_supply.h>
35 #include <linux/hwmon.h>
36 #include <linux/hwmon-sysfs.h>
37 
38 static int
nouveau_pwmfan_get(struct drm_device * dev)39 nouveau_pwmfan_get(struct drm_device *dev)
40 {
41 	struct drm_nouveau_private *dev_priv = dev->dev_private;
42 	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
43 	struct gpio_func gpio;
44 	u32 divs, duty;
45 	int ret;
46 
47 	if (!pm->pwm_get)
48 		return -ENODEV;
49 
50 	ret = nouveau_gpio_find(dev, 0, DCB_GPIO_PWM_FAN, 0xff, &gpio);
51 	if (ret == 0) {
52 		ret = pm->pwm_get(dev, gpio.line, &divs, &duty);
53 		if (ret == 0 && divs) {
54 			divs = max(divs, duty);
55 			if (dev_priv->card_type <= NV_40 || (gpio.log[0] & 1))
56 				duty = divs - duty;
57 			return (duty * 100) / divs;
58 		}
59 
60 		return nouveau_gpio_func_get(dev, gpio.func) * 100;
61 	}
62 
63 	return -ENODEV;
64 }
65 
66 static int
nouveau_pwmfan_set(struct drm_device * dev,int percent)67 nouveau_pwmfan_set(struct drm_device *dev, int percent)
68 {
69 	struct drm_nouveau_private *dev_priv = dev->dev_private;
70 	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
71 	struct gpio_func gpio;
72 	u32 divs, duty;
73 	int ret;
74 
75 	if (!pm->pwm_set)
76 		return -ENODEV;
77 
78 	ret = nouveau_gpio_find(dev, 0, DCB_GPIO_PWM_FAN, 0xff, &gpio);
79 	if (ret == 0) {
80 		divs = pm->fan.pwm_divisor;
81 		if (pm->fan.pwm_freq) {
82 			/*XXX: PNVIO clock more than likely... */
83 			divs = 135000 / pm->fan.pwm_freq;
84 			if (dev_priv->chipset < 0xa3)
85 				divs /= 4;
86 		}
87 
88 		duty = ((divs * percent) + 99) / 100;
89 		if (dev_priv->card_type <= NV_40 || (gpio.log[0] & 1))
90 			duty = divs - duty;
91 
92 		ret = pm->pwm_set(dev, gpio.line, divs, duty);
93 		if (!ret)
94 			pm->fan.percent = percent;
95 		return ret;
96 	}
97 
98 	return -ENODEV;
99 }
100 
101 static int
nouveau_pm_perflvl_aux(struct drm_device * dev,struct nouveau_pm_level * perflvl,struct nouveau_pm_level * a,struct nouveau_pm_level * b)102 nouveau_pm_perflvl_aux(struct drm_device *dev, struct nouveau_pm_level *perflvl,
103 		       struct nouveau_pm_level *a, struct nouveau_pm_level *b)
104 {
105 	struct drm_nouveau_private *dev_priv = dev->dev_private;
106 	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
107 	int ret;
108 
109 	/*XXX: not on all boards, we should control based on temperature
110 	 *     on recent boards..  or maybe on some other factor we don't
111 	 *     know about?
112 	 */
113 	if (a->fanspeed && b->fanspeed && b->fanspeed > a->fanspeed) {
114 		ret = nouveau_pwmfan_set(dev, perflvl->fanspeed);
115 		if (ret && ret != -ENODEV) {
116 			NV_ERROR(dev, "fanspeed set failed: %d\n", ret);
117 			return ret;
118 		}
119 	}
120 
121 	if (pm->voltage.supported && pm->voltage_set) {
122 		if (perflvl->volt_min && b->volt_min > a->volt_min) {
123 			ret = pm->voltage_set(dev, perflvl->volt_min);
124 			if (ret) {
125 				NV_ERROR(dev, "voltage set failed: %d\n", ret);
126 				return ret;
127 			}
128 		}
129 	}
130 
131 	return 0;
132 }
133 
134 static int
nouveau_pm_perflvl_set(struct drm_device * dev,struct nouveau_pm_level * perflvl)135 nouveau_pm_perflvl_set(struct drm_device *dev, struct nouveau_pm_level *perflvl)
136 {
137 	struct drm_nouveau_private *dev_priv = dev->dev_private;
138 	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
139 	void *state;
140 	int ret;
141 
142 	if (perflvl == pm->cur)
143 		return 0;
144 
145 	ret = nouveau_pm_perflvl_aux(dev, perflvl, pm->cur, perflvl);
146 	if (ret)
147 		return ret;
148 
149 	state = pm->clocks_pre(dev, perflvl);
150 	if (IS_ERR(state)) {
151 		ret = PTR_ERR(state);
152 		goto error;
153 	}
154 	ret = pm->clocks_set(dev, state);
155 	if (ret)
156 		goto error;
157 
158 	ret = nouveau_pm_perflvl_aux(dev, perflvl, perflvl, pm->cur);
159 	if (ret)
160 		return ret;
161 
162 	pm->cur = perflvl;
163 	return 0;
164 
165 error:
166 	/* restore the fan speed and voltage before leaving */
167 	nouveau_pm_perflvl_aux(dev, perflvl, perflvl, pm->cur);
168 	return ret;
169 }
170 
171 void
nouveau_pm_trigger(struct drm_device * dev)172 nouveau_pm_trigger(struct drm_device *dev)
173 {
174 	struct drm_nouveau_private *dev_priv = dev->dev_private;
175 	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
176 	struct nouveau_pm_profile *profile = NULL;
177 	struct nouveau_pm_level *perflvl = NULL;
178 	int ret;
179 
180 	/* select power profile based on current power source */
181 	if (power_supply_is_system_supplied())
182 		profile = pm->profile_ac;
183 	else
184 		profile = pm->profile_dc;
185 
186 	if (profile != pm->profile) {
187 		pm->profile->func->fini(pm->profile);
188 		pm->profile = profile;
189 		pm->profile->func->init(pm->profile);
190 	}
191 
192 	/* select performance level based on profile */
193 	perflvl = profile->func->select(profile);
194 
195 	/* change perflvl, if necessary */
196 	if (perflvl != pm->cur) {
197 		struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
198 		u64 time0 = ptimer->read(dev);
199 
200 		NV_INFO(dev, "setting performance level: %d", perflvl->id);
201 		ret = nouveau_pm_perflvl_set(dev, perflvl);
202 		if (ret)
203 			NV_INFO(dev, "> reclocking failed: %d\n\n", ret);
204 
205 		NV_INFO(dev, "> reclocking took %lluns\n\n",
206 			     ptimer->read(dev) - time0);
207 	}
208 }
209 
210 static struct nouveau_pm_profile *
profile_find(struct drm_device * dev,const char * string)211 profile_find(struct drm_device *dev, const char *string)
212 {
213 	struct drm_nouveau_private *dev_priv = dev->dev_private;
214 	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
215 	struct nouveau_pm_profile *profile;
216 
217 	list_for_each_entry(profile, &pm->profiles, head) {
218 		if (!strncmp(profile->name, string, sizeof(profile->name)))
219 			return profile;
220 	}
221 
222 	return NULL;
223 }
224 
225 static int
nouveau_pm_profile_set(struct drm_device * dev,const char * profile)226 nouveau_pm_profile_set(struct drm_device *dev, const char *profile)
227 {
228 	struct drm_nouveau_private *dev_priv = dev->dev_private;
229 	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
230 	struct nouveau_pm_profile *ac = NULL, *dc = NULL;
231 	char string[16], *cur = string, *ptr;
232 
233 	/* safety precaution, for now */
234 	if (nouveau_perflvl_wr != 7777)
235 		return -EPERM;
236 
237 	strncpy(string, profile, sizeof(string));
238 	string[sizeof(string) - 1] = 0;
239 	if ((ptr = strchr(string, '\n')))
240 		*ptr = '\0';
241 
242 	ptr = strsep(&cur, ",");
243 	if (ptr)
244 		ac = profile_find(dev, ptr);
245 
246 	ptr = strsep(&cur, ",");
247 	if (ptr)
248 		dc = profile_find(dev, ptr);
249 	else
250 		dc = ac;
251 
252 	if (ac == NULL || dc == NULL)
253 		return -EINVAL;
254 
255 	pm->profile_ac = ac;
256 	pm->profile_dc = dc;
257 	nouveau_pm_trigger(dev);
258 	return 0;
259 }
260 
261 static void
nouveau_pm_static_dummy(struct nouveau_pm_profile * profile)262 nouveau_pm_static_dummy(struct nouveau_pm_profile *profile)
263 {
264 }
265 
266 static struct nouveau_pm_level *
nouveau_pm_static_select(struct nouveau_pm_profile * profile)267 nouveau_pm_static_select(struct nouveau_pm_profile *profile)
268 {
269 	return container_of(profile, struct nouveau_pm_level, profile);
270 }
271 
272 const struct nouveau_pm_profile_func nouveau_pm_static_profile_func = {
273 	.destroy = nouveau_pm_static_dummy,
274 	.init = nouveau_pm_static_dummy,
275 	.fini = nouveau_pm_static_dummy,
276 	.select = nouveau_pm_static_select,
277 };
278 
279 static int
nouveau_pm_perflvl_get(struct drm_device * dev,struct nouveau_pm_level * perflvl)280 nouveau_pm_perflvl_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
281 {
282 	struct drm_nouveau_private *dev_priv = dev->dev_private;
283 	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
284 	int ret;
285 
286 	memset(perflvl, 0, sizeof(*perflvl));
287 
288 	if (pm->clocks_get) {
289 		ret = pm->clocks_get(dev, perflvl);
290 		if (ret)
291 			return ret;
292 	}
293 
294 	if (pm->voltage.supported && pm->voltage_get) {
295 		ret = pm->voltage_get(dev);
296 		if (ret > 0) {
297 			perflvl->volt_min = ret;
298 			perflvl->volt_max = ret;
299 		}
300 	}
301 
302 	ret = nouveau_pwmfan_get(dev);
303 	if (ret > 0)
304 		perflvl->fanspeed = ret;
305 
306 	nouveau_mem_timing_read(dev, &perflvl->timing);
307 	return 0;
308 }
309 
310 static void
nouveau_pm_perflvl_info(struct nouveau_pm_level * perflvl,char * ptr,int len)311 nouveau_pm_perflvl_info(struct nouveau_pm_level *perflvl, char *ptr, int len)
312 {
313 	char c[16], s[16], v[32], f[16], m[16];
314 
315 	c[0] = '\0';
316 	if (perflvl->core)
317 		snprintf(c, sizeof(c), " core %dMHz", perflvl->core / 1000);
318 
319 	s[0] = '\0';
320 	if (perflvl->shader)
321 		snprintf(s, sizeof(s), " shader %dMHz", perflvl->shader / 1000);
322 
323 	m[0] = '\0';
324 	if (perflvl->memory)
325 		snprintf(m, sizeof(m), " memory %dMHz", perflvl->memory / 1000);
326 
327 	v[0] = '\0';
328 	if (perflvl->volt_min && perflvl->volt_min != perflvl->volt_max) {
329 		snprintf(v, sizeof(v), " voltage %dmV-%dmV",
330 			 perflvl->volt_min / 1000, perflvl->volt_max / 1000);
331 	} else
332 	if (perflvl->volt_min) {
333 		snprintf(v, sizeof(v), " voltage %dmV",
334 			 perflvl->volt_min / 1000);
335 	}
336 
337 	f[0] = '\0';
338 	if (perflvl->fanspeed)
339 		snprintf(f, sizeof(f), " fanspeed %d%%", perflvl->fanspeed);
340 
341 	snprintf(ptr, len, "%s%s%s%s%s\n", c, s, m, v, f);
342 }
343 
344 static ssize_t
nouveau_pm_get_perflvl_info(struct device * d,struct device_attribute * a,char * buf)345 nouveau_pm_get_perflvl_info(struct device *d,
346 			    struct device_attribute *a, char *buf)
347 {
348 	struct nouveau_pm_level *perflvl =
349 		container_of(a, struct nouveau_pm_level, dev_attr);
350 	char *ptr = buf;
351 	int len = PAGE_SIZE;
352 
353 	snprintf(ptr, len, "%d:", perflvl->id);
354 	ptr += strlen(buf);
355 	len -= strlen(buf);
356 
357 	nouveau_pm_perflvl_info(perflvl, ptr, len);
358 	return strlen(buf);
359 }
360 
361 static ssize_t
nouveau_pm_get_perflvl(struct device * d,struct device_attribute * a,char * buf)362 nouveau_pm_get_perflvl(struct device *d, struct device_attribute *a, char *buf)
363 {
364 	struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
365 	struct drm_nouveau_private *dev_priv = dev->dev_private;
366 	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
367 	struct nouveau_pm_level cur;
368 	int len = PAGE_SIZE, ret;
369 	char *ptr = buf;
370 
371 	snprintf(ptr, len, "profile: %s, %s\nc:",
372 		 pm->profile_ac->name, pm->profile_dc->name);
373 	ptr += strlen(buf);
374 	len -= strlen(buf);
375 
376 	ret = nouveau_pm_perflvl_get(dev, &cur);
377 	if (ret == 0)
378 		nouveau_pm_perflvl_info(&cur, ptr, len);
379 	return strlen(buf);
380 }
381 
382 static ssize_t
nouveau_pm_set_perflvl(struct device * d,struct device_attribute * a,const char * buf,size_t count)383 nouveau_pm_set_perflvl(struct device *d, struct device_attribute *a,
384 		       const char *buf, size_t count)
385 {
386 	struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
387 	int ret;
388 
389 	ret = nouveau_pm_profile_set(dev, buf);
390 	if (ret)
391 		return ret;
392 	return strlen(buf);
393 }
394 
395 static DEVICE_ATTR(performance_level, S_IRUGO | S_IWUSR,
396 		   nouveau_pm_get_perflvl, nouveau_pm_set_perflvl);
397 
398 static int
nouveau_sysfs_init(struct drm_device * dev)399 nouveau_sysfs_init(struct drm_device *dev)
400 {
401 	struct drm_nouveau_private *dev_priv = dev->dev_private;
402 	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
403 	struct device *d = &dev->pdev->dev;
404 	int ret, i;
405 
406 	ret = device_create_file(d, &dev_attr_performance_level);
407 	if (ret)
408 		return ret;
409 
410 	for (i = 0; i < pm->nr_perflvl; i++) {
411 		struct nouveau_pm_level *perflvl = &pm->perflvl[i];
412 
413 		perflvl->dev_attr.attr.name = perflvl->name;
414 		perflvl->dev_attr.attr.mode = S_IRUGO;
415 		perflvl->dev_attr.show = nouveau_pm_get_perflvl_info;
416 		perflvl->dev_attr.store = NULL;
417 		sysfs_attr_init(&perflvl->dev_attr.attr);
418 
419 		ret = device_create_file(d, &perflvl->dev_attr);
420 		if (ret) {
421 			NV_ERROR(dev, "failed pervlvl %d sysfs: %d\n",
422 				 perflvl->id, i);
423 			perflvl->dev_attr.attr.name = NULL;
424 			nouveau_pm_fini(dev);
425 			return ret;
426 		}
427 	}
428 
429 	return 0;
430 }
431 
432 static void
nouveau_sysfs_fini(struct drm_device * dev)433 nouveau_sysfs_fini(struct drm_device *dev)
434 {
435 	struct drm_nouveau_private *dev_priv = dev->dev_private;
436 	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
437 	struct device *d = &dev->pdev->dev;
438 	int i;
439 
440 	device_remove_file(d, &dev_attr_performance_level);
441 	for (i = 0; i < pm->nr_perflvl; i++) {
442 		struct nouveau_pm_level *pl = &pm->perflvl[i];
443 
444 		if (!pl->dev_attr.attr.name)
445 			break;
446 
447 		device_remove_file(d, &pl->dev_attr);
448 	}
449 }
450 
451 #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
452 static ssize_t
nouveau_hwmon_show_temp(struct device * d,struct device_attribute * a,char * buf)453 nouveau_hwmon_show_temp(struct device *d, struct device_attribute *a, char *buf)
454 {
455 	struct drm_device *dev = dev_get_drvdata(d);
456 	struct drm_nouveau_private *dev_priv = dev->dev_private;
457 	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
458 
459 	return snprintf(buf, PAGE_SIZE, "%d\n", pm->temp_get(dev)*1000);
460 }
461 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, nouveau_hwmon_show_temp,
462 						  NULL, 0);
463 
464 static ssize_t
nouveau_hwmon_max_temp(struct device * d,struct device_attribute * a,char * buf)465 nouveau_hwmon_max_temp(struct device *d, struct device_attribute *a, char *buf)
466 {
467 	struct drm_device *dev = dev_get_drvdata(d);
468 	struct drm_nouveau_private *dev_priv = dev->dev_private;
469 	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
470 	struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
471 
472 	return snprintf(buf, PAGE_SIZE, "%d\n", temp->down_clock*1000);
473 }
474 static ssize_t
nouveau_hwmon_set_max_temp(struct device * d,struct device_attribute * a,const char * buf,size_t count)475 nouveau_hwmon_set_max_temp(struct device *d, struct device_attribute *a,
476 						const char *buf, size_t count)
477 {
478 	struct drm_device *dev = dev_get_drvdata(d);
479 	struct drm_nouveau_private *dev_priv = dev->dev_private;
480 	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
481 	struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
482 	long value;
483 
484 	if (kstrtol(buf, 10, &value) == -EINVAL)
485 		return count;
486 
487 	temp->down_clock = value/1000;
488 
489 	nouveau_temp_safety_checks(dev);
490 
491 	return count;
492 }
493 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, nouveau_hwmon_max_temp,
494 						  nouveau_hwmon_set_max_temp,
495 						  0);
496 
497 static ssize_t
nouveau_hwmon_critical_temp(struct device * d,struct device_attribute * a,char * buf)498 nouveau_hwmon_critical_temp(struct device *d, struct device_attribute *a,
499 							char *buf)
500 {
501 	struct drm_device *dev = dev_get_drvdata(d);
502 	struct drm_nouveau_private *dev_priv = dev->dev_private;
503 	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
504 	struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
505 
506 	return snprintf(buf, PAGE_SIZE, "%d\n", temp->critical*1000);
507 }
508 static ssize_t
nouveau_hwmon_set_critical_temp(struct device * d,struct device_attribute * a,const char * buf,size_t count)509 nouveau_hwmon_set_critical_temp(struct device *d, struct device_attribute *a,
510 							    const char *buf,
511 								size_t count)
512 {
513 	struct drm_device *dev = dev_get_drvdata(d);
514 	struct drm_nouveau_private *dev_priv = dev->dev_private;
515 	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
516 	struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
517 	long value;
518 
519 	if (kstrtol(buf, 10, &value) == -EINVAL)
520 		return count;
521 
522 	temp->critical = value/1000;
523 
524 	nouveau_temp_safety_checks(dev);
525 
526 	return count;
527 }
528 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
529 						nouveau_hwmon_critical_temp,
530 						nouveau_hwmon_set_critical_temp,
531 						0);
532 
nouveau_hwmon_show_name(struct device * dev,struct device_attribute * attr,char * buf)533 static ssize_t nouveau_hwmon_show_name(struct device *dev,
534 				      struct device_attribute *attr,
535 				      char *buf)
536 {
537 	return sprintf(buf, "nouveau\n");
538 }
539 static SENSOR_DEVICE_ATTR(name, S_IRUGO, nouveau_hwmon_show_name, NULL, 0);
540 
nouveau_hwmon_show_update_rate(struct device * dev,struct device_attribute * attr,char * buf)541 static ssize_t nouveau_hwmon_show_update_rate(struct device *dev,
542 				      struct device_attribute *attr,
543 				      char *buf)
544 {
545 	return sprintf(buf, "1000\n");
546 }
547 static SENSOR_DEVICE_ATTR(update_rate, S_IRUGO,
548 						nouveau_hwmon_show_update_rate,
549 						NULL, 0);
550 
551 static ssize_t
nouveau_hwmon_show_fan0_input(struct device * d,struct device_attribute * attr,char * buf)552 nouveau_hwmon_show_fan0_input(struct device *d, struct device_attribute *attr,
553 			      char *buf)
554 {
555 	struct drm_device *dev = dev_get_drvdata(d);
556 	struct drm_nouveau_private *dev_priv = dev->dev_private;
557 	struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
558 	struct gpio_func gpio;
559 	u32 cycles, cur, prev;
560 	u64 start;
561 	int ret;
562 
563 	ret = nouveau_gpio_find(dev, 0, DCB_GPIO_FAN_SENSE, 0xff, &gpio);
564 	if (ret)
565 		return ret;
566 
567 	/* Monitor the GPIO input 0x3b for 250ms.
568 	 * When the fan spins, it changes the value of GPIO FAN_SENSE.
569 	 * We get 4 changes (0 -> 1 -> 0 -> 1 -> [...]) per complete rotation.
570 	 */
571 	start = ptimer->read(dev);
572 	prev = nouveau_gpio_sense(dev, 0, gpio.line);
573 	cycles = 0;
574 	do {
575 		cur = nouveau_gpio_sense(dev, 0, gpio.line);
576 		if (prev != cur) {
577 			cycles++;
578 			prev = cur;
579 		}
580 
581 		usleep_range(500, 1000); /* supports 0 < rpm < 7500 */
582 	} while (ptimer->read(dev) - start < 250000000);
583 
584 	/* interpolate to get rpm */
585 	return sprintf(buf, "%i\n", cycles / 4 * 4 * 60);
586 }
587 static SENSOR_DEVICE_ATTR(fan0_input, S_IRUGO, nouveau_hwmon_show_fan0_input,
588 			  NULL, 0);
589 
590 static ssize_t
nouveau_hwmon_get_pwm0(struct device * d,struct device_attribute * a,char * buf)591 nouveau_hwmon_get_pwm0(struct device *d, struct device_attribute *a, char *buf)
592 {
593 	struct drm_device *dev = dev_get_drvdata(d);
594 	int ret;
595 
596 	ret = nouveau_pwmfan_get(dev);
597 	if (ret < 0)
598 		return ret;
599 
600 	return sprintf(buf, "%i\n", ret);
601 }
602 
603 static ssize_t
nouveau_hwmon_set_pwm0(struct device * d,struct device_attribute * a,const char * buf,size_t count)604 nouveau_hwmon_set_pwm0(struct device *d, struct device_attribute *a,
605 		       const char *buf, size_t count)
606 {
607 	struct drm_device *dev = dev_get_drvdata(d);
608 	struct drm_nouveau_private *dev_priv = dev->dev_private;
609 	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
610 	int ret = -ENODEV;
611 	long value;
612 
613 	if (nouveau_perflvl_wr != 7777)
614 		return -EPERM;
615 
616 	if (kstrtol(buf, 10, &value) == -EINVAL)
617 		return -EINVAL;
618 
619 	if (value < pm->fan.min_duty)
620 		value = pm->fan.min_duty;
621 	if (value > pm->fan.max_duty)
622 		value = pm->fan.max_duty;
623 
624 	ret = nouveau_pwmfan_set(dev, value);
625 	if (ret)
626 		return ret;
627 
628 	return count;
629 }
630 
631 static SENSOR_DEVICE_ATTR(pwm0, S_IRUGO | S_IWUSR,
632 			  nouveau_hwmon_get_pwm0,
633 			  nouveau_hwmon_set_pwm0, 0);
634 
635 static ssize_t
nouveau_hwmon_get_pwm0_min(struct device * d,struct device_attribute * a,char * buf)636 nouveau_hwmon_get_pwm0_min(struct device *d,
637 			   struct device_attribute *a, char *buf)
638 {
639 	struct drm_device *dev = dev_get_drvdata(d);
640 	struct drm_nouveau_private *dev_priv = dev->dev_private;
641 	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
642 
643 	return sprintf(buf, "%i\n", pm->fan.min_duty);
644 }
645 
646 static ssize_t
nouveau_hwmon_set_pwm0_min(struct device * d,struct device_attribute * a,const char * buf,size_t count)647 nouveau_hwmon_set_pwm0_min(struct device *d, struct device_attribute *a,
648 			   const char *buf, size_t count)
649 {
650 	struct drm_device *dev = dev_get_drvdata(d);
651 	struct drm_nouveau_private *dev_priv = dev->dev_private;
652 	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
653 	long value;
654 
655 	if (kstrtol(buf, 10, &value) == -EINVAL)
656 		return -EINVAL;
657 
658 	if (value < 0)
659 		value = 0;
660 
661 	if (pm->fan.max_duty - value < 10)
662 		value = pm->fan.max_duty - 10;
663 
664 	if (value < 10)
665 		pm->fan.min_duty = 10;
666 	else
667 		pm->fan.min_duty = value;
668 
669 	return count;
670 }
671 
672 static SENSOR_DEVICE_ATTR(pwm0_min, S_IRUGO | S_IWUSR,
673 			  nouveau_hwmon_get_pwm0_min,
674 			  nouveau_hwmon_set_pwm0_min, 0);
675 
676 static ssize_t
nouveau_hwmon_get_pwm0_max(struct device * d,struct device_attribute * a,char * buf)677 nouveau_hwmon_get_pwm0_max(struct device *d,
678 			   struct device_attribute *a, char *buf)
679 {
680 	struct drm_device *dev = dev_get_drvdata(d);
681 	struct drm_nouveau_private *dev_priv = dev->dev_private;
682 	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
683 
684 	return sprintf(buf, "%i\n", pm->fan.max_duty);
685 }
686 
687 static ssize_t
nouveau_hwmon_set_pwm0_max(struct device * d,struct device_attribute * a,const char * buf,size_t count)688 nouveau_hwmon_set_pwm0_max(struct device *d, struct device_attribute *a,
689 			   const char *buf, size_t count)
690 {
691 	struct drm_device *dev = dev_get_drvdata(d);
692 	struct drm_nouveau_private *dev_priv = dev->dev_private;
693 	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
694 	long value;
695 
696 	if (kstrtol(buf, 10, &value) == -EINVAL)
697 		return -EINVAL;
698 
699 	if (value < 0)
700 		value = 0;
701 
702 	if (value - pm->fan.min_duty < 10)
703 		value = pm->fan.min_duty + 10;
704 
705 	if (value > 100)
706 		pm->fan.max_duty = 100;
707 	else
708 		pm->fan.max_duty = value;
709 
710 	return count;
711 }
712 
713 static SENSOR_DEVICE_ATTR(pwm0_max, S_IRUGO | S_IWUSR,
714 			  nouveau_hwmon_get_pwm0_max,
715 			  nouveau_hwmon_set_pwm0_max, 0);
716 
717 static struct attribute *hwmon_attributes[] = {
718 	&sensor_dev_attr_temp1_input.dev_attr.attr,
719 	&sensor_dev_attr_temp1_max.dev_attr.attr,
720 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
721 	&sensor_dev_attr_name.dev_attr.attr,
722 	&sensor_dev_attr_update_rate.dev_attr.attr,
723 	NULL
724 };
725 static struct attribute *hwmon_fan_rpm_attributes[] = {
726 	&sensor_dev_attr_fan0_input.dev_attr.attr,
727 	NULL
728 };
729 static struct attribute *hwmon_pwm_fan_attributes[] = {
730 	&sensor_dev_attr_pwm0.dev_attr.attr,
731 	&sensor_dev_attr_pwm0_min.dev_attr.attr,
732 	&sensor_dev_attr_pwm0_max.dev_attr.attr,
733 	NULL
734 };
735 
736 static const struct attribute_group hwmon_attrgroup = {
737 	.attrs = hwmon_attributes,
738 };
739 static const struct attribute_group hwmon_fan_rpm_attrgroup = {
740 	.attrs = hwmon_fan_rpm_attributes,
741 };
742 static const struct attribute_group hwmon_pwm_fan_attrgroup = {
743 	.attrs = hwmon_pwm_fan_attributes,
744 };
745 #endif
746 
747 static int
nouveau_hwmon_init(struct drm_device * dev)748 nouveau_hwmon_init(struct drm_device *dev)
749 {
750 	struct drm_nouveau_private *dev_priv = dev->dev_private;
751 	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
752 #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
753 	struct device *hwmon_dev;
754 	int ret = 0;
755 
756 	if (!pm->temp_get)
757 		return -ENODEV;
758 
759 	hwmon_dev = hwmon_device_register(&dev->pdev->dev);
760 	if (IS_ERR(hwmon_dev)) {
761 		ret = PTR_ERR(hwmon_dev);
762 		NV_ERROR(dev,
763 			"Unable to register hwmon device: %d\n", ret);
764 		return ret;
765 	}
766 	dev_set_drvdata(hwmon_dev, dev);
767 
768 	/* default sysfs entries */
769 	ret = sysfs_create_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
770 	if (ret) {
771 		if (ret)
772 			goto error;
773 	}
774 
775 	/* if the card has a pwm fan */
776 	/*XXX: incorrect, need better detection for this, some boards have
777 	 *     the gpio entries for pwm fan control even when there's no
778 	 *     actual fan connected to it... therm table? */
779 	if (nouveau_pwmfan_get(dev) >= 0) {
780 		ret = sysfs_create_group(&dev->pdev->dev.kobj,
781 					 &hwmon_pwm_fan_attrgroup);
782 		if (ret)
783 			goto error;
784 	}
785 
786 	/* if the card can read the fan rpm */
787 	if (nouveau_gpio_func_valid(dev, DCB_GPIO_FAN_SENSE)) {
788 		ret = sysfs_create_group(&dev->pdev->dev.kobj,
789 					 &hwmon_fan_rpm_attrgroup);
790 		if (ret)
791 			goto error;
792 	}
793 
794 	pm->hwmon = hwmon_dev;
795 
796 	return 0;
797 
798 error:
799 	NV_ERROR(dev, "Unable to create some hwmon sysfs files: %d\n", ret);
800 	hwmon_device_unregister(hwmon_dev);
801 	pm->hwmon = NULL;
802 	return ret;
803 #else
804 	pm->hwmon = NULL;
805 	return 0;
806 #endif
807 }
808 
809 static void
nouveau_hwmon_fini(struct drm_device * dev)810 nouveau_hwmon_fini(struct drm_device *dev)
811 {
812 #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
813 	struct drm_nouveau_private *dev_priv = dev->dev_private;
814 	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
815 
816 	if (pm->hwmon) {
817 		sysfs_remove_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
818 		sysfs_remove_group(&dev->pdev->dev.kobj,
819 				   &hwmon_pwm_fan_attrgroup);
820 		sysfs_remove_group(&dev->pdev->dev.kobj,
821 				   &hwmon_fan_rpm_attrgroup);
822 
823 		hwmon_device_unregister(pm->hwmon);
824 	}
825 #endif
826 }
827 
828 #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
829 static int
nouveau_pm_acpi_event(struct notifier_block * nb,unsigned long val,void * data)830 nouveau_pm_acpi_event(struct notifier_block *nb, unsigned long val, void *data)
831 {
832 	struct drm_nouveau_private *dev_priv =
833 		container_of(nb, struct drm_nouveau_private, engine.pm.acpi_nb);
834 	struct drm_device *dev = dev_priv->dev;
835 	struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
836 
837 	if (strcmp(entry->device_class, "ac_adapter") == 0) {
838 		bool ac = power_supply_is_system_supplied();
839 
840 		NV_DEBUG(dev, "power supply changed: %s\n", ac ? "AC" : "DC");
841 		nouveau_pm_trigger(dev);
842 	}
843 
844 	return NOTIFY_OK;
845 }
846 #endif
847 
848 int
nouveau_pm_init(struct drm_device * dev)849 nouveau_pm_init(struct drm_device *dev)
850 {
851 	struct drm_nouveau_private *dev_priv = dev->dev_private;
852 	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
853 	char info[256];
854 	int ret, i;
855 
856 	/* parse aux tables from vbios */
857 	nouveau_volt_init(dev);
858 	nouveau_temp_init(dev);
859 
860 	/* determine current ("boot") performance level */
861 	ret = nouveau_pm_perflvl_get(dev, &pm->boot);
862 	if (ret) {
863 		NV_ERROR(dev, "failed to determine boot perflvl\n");
864 		return ret;
865 	}
866 
867 	strncpy(pm->boot.name, "boot", 4);
868 	strncpy(pm->boot.profile.name, "boot", 4);
869 	pm->boot.profile.func = &nouveau_pm_static_profile_func;
870 
871 	INIT_LIST_HEAD(&pm->profiles);
872 	list_add(&pm->boot.profile.head, &pm->profiles);
873 
874 	pm->profile_ac = &pm->boot.profile;
875 	pm->profile_dc = &pm->boot.profile;
876 	pm->profile = &pm->boot.profile;
877 	pm->cur = &pm->boot;
878 
879 	/* add performance levels from vbios */
880 	nouveau_perf_init(dev);
881 
882 	/* display available performance levels */
883 	NV_INFO(dev, "%d available performance level(s)\n", pm->nr_perflvl);
884 	for (i = 0; i < pm->nr_perflvl; i++) {
885 		nouveau_pm_perflvl_info(&pm->perflvl[i], info, sizeof(info));
886 		NV_INFO(dev, "%d:%s", pm->perflvl[i].id, info);
887 	}
888 
889 	nouveau_pm_perflvl_info(&pm->boot, info, sizeof(info));
890 	NV_INFO(dev, "c:%s", info);
891 
892 	/* switch performance levels now if requested */
893 	if (nouveau_perflvl != NULL)
894 		nouveau_pm_profile_set(dev, nouveau_perflvl);
895 
896 	/* determine the current fan speed */
897 	pm->fan.percent = nouveau_pwmfan_get(dev);
898 
899 	nouveau_sysfs_init(dev);
900 	nouveau_hwmon_init(dev);
901 #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
902 	pm->acpi_nb.notifier_call = nouveau_pm_acpi_event;
903 	register_acpi_notifier(&pm->acpi_nb);
904 #endif
905 
906 	return 0;
907 }
908 
909 void
nouveau_pm_fini(struct drm_device * dev)910 nouveau_pm_fini(struct drm_device *dev)
911 {
912 	struct drm_nouveau_private *dev_priv = dev->dev_private;
913 	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
914 	struct nouveau_pm_profile *profile, *tmp;
915 
916 	list_for_each_entry_safe(profile, tmp, &pm->profiles, head) {
917 		list_del(&profile->head);
918 		profile->func->destroy(profile);
919 	}
920 
921 	if (pm->cur != &pm->boot)
922 		nouveau_pm_perflvl_set(dev, &pm->boot);
923 
924 	nouveau_temp_fini(dev);
925 	nouveau_perf_fini(dev);
926 	nouveau_volt_fini(dev);
927 
928 #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
929 	unregister_acpi_notifier(&pm->acpi_nb);
930 #endif
931 	nouveau_hwmon_fini(dev);
932 	nouveau_sysfs_fini(dev);
933 }
934 
935 void
nouveau_pm_resume(struct drm_device * dev)936 nouveau_pm_resume(struct drm_device *dev)
937 {
938 	struct drm_nouveau_private *dev_priv = dev->dev_private;
939 	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
940 	struct nouveau_pm_level *perflvl;
941 
942 	if (!pm->cur || pm->cur == &pm->boot)
943 		return;
944 
945 	perflvl = pm->cur;
946 	pm->cur = &pm->boot;
947 	nouveau_pm_perflvl_set(dev, perflvl);
948 	nouveau_pwmfan_set(dev, pm->fan.percent);
949 }
950