]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/gpu/drm/nouveau/nouveau_pm.c
percpu, x86: don't use PMD_SIZE as embedded atom_size on 32bit
[mv-sheeva.git] / drivers / gpu / drm / nouveau / nouveau_pm.c
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
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
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
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
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
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 *
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
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         if ((ptr = strchr(string, '\n')))
239                 *ptr = '\0';
240
241         ptr = strsep(&cur, ",");
242         if (ptr)
243                 ac = profile_find(dev, ptr);
244
245         ptr = strsep(&cur, ",");
246         if (ptr)
247                 dc = profile_find(dev, ptr);
248         else
249                 dc = ac;
250
251         if (ac == NULL || dc == NULL)
252                 return -EINVAL;
253
254         pm->profile_ac = ac;
255         pm->profile_dc = dc;
256         nouveau_pm_trigger(dev);
257         return 0;
258 }
259
260 static void
261 nouveau_pm_static_dummy(struct nouveau_pm_profile *profile)
262 {
263 }
264
265 static struct nouveau_pm_level *
266 nouveau_pm_static_select(struct nouveau_pm_profile *profile)
267 {
268         return container_of(profile, struct nouveau_pm_level, profile);
269 }
270
271 const struct nouveau_pm_profile_func nouveau_pm_static_profile_func = {
272         .destroy = nouveau_pm_static_dummy,
273         .init = nouveau_pm_static_dummy,
274         .fini = nouveau_pm_static_dummy,
275         .select = nouveau_pm_static_select,
276 };
277
278 static int
279 nouveau_pm_perflvl_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
280 {
281         struct drm_nouveau_private *dev_priv = dev->dev_private;
282         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
283         int ret;
284
285         memset(perflvl, 0, sizeof(*perflvl));
286
287         if (pm->clocks_get) {
288                 ret = pm->clocks_get(dev, perflvl);
289                 if (ret)
290                         return ret;
291         }
292
293         if (pm->voltage.supported && pm->voltage_get) {
294                 ret = pm->voltage_get(dev);
295                 if (ret > 0) {
296                         perflvl->volt_min = ret;
297                         perflvl->volt_max = ret;
298                 }
299         }
300
301         ret = nouveau_pwmfan_get(dev);
302         if (ret > 0)
303                 perflvl->fanspeed = ret;
304
305         nouveau_mem_timing_read(dev, &perflvl->timing);
306         return 0;
307 }
308
309 static void
310 nouveau_pm_perflvl_info(struct nouveau_pm_level *perflvl, char *ptr, int len)
311 {
312         char c[16], s[16], v[32], f[16], m[16];
313
314         c[0] = '\0';
315         if (perflvl->core)
316                 snprintf(c, sizeof(c), " core %dMHz", perflvl->core / 1000);
317
318         s[0] = '\0';
319         if (perflvl->shader)
320                 snprintf(s, sizeof(s), " shader %dMHz", perflvl->shader / 1000);
321
322         m[0] = '\0';
323         if (perflvl->memory)
324                 snprintf(m, sizeof(m), " memory %dMHz", perflvl->memory / 1000);
325
326         v[0] = '\0';
327         if (perflvl->volt_min && perflvl->volt_min != perflvl->volt_max) {
328                 snprintf(v, sizeof(v), " voltage %dmV-%dmV",
329                          perflvl->volt_min / 1000, perflvl->volt_max / 1000);
330         } else
331         if (perflvl->volt_min) {
332                 snprintf(v, sizeof(v), " voltage %dmV",
333                          perflvl->volt_min / 1000);
334         }
335
336         f[0] = '\0';
337         if (perflvl->fanspeed)
338                 snprintf(f, sizeof(f), " fanspeed %d%%", perflvl->fanspeed);
339
340         snprintf(ptr, len, "%s%s%s%s%s\n", c, s, m, v, f);
341 }
342
343 static ssize_t
344 nouveau_pm_get_perflvl_info(struct device *d,
345                             struct device_attribute *a, char *buf)
346 {
347         struct nouveau_pm_level *perflvl =
348                 container_of(a, struct nouveau_pm_level, dev_attr);
349         char *ptr = buf;
350         int len = PAGE_SIZE;
351
352         snprintf(ptr, len, "%d:", perflvl->id);
353         ptr += strlen(buf);
354         len -= strlen(buf);
355
356         nouveau_pm_perflvl_info(perflvl, ptr, len);
357         return strlen(buf);
358 }
359
360 static ssize_t
361 nouveau_pm_get_perflvl(struct device *d, struct device_attribute *a, char *buf)
362 {
363         struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
364         struct drm_nouveau_private *dev_priv = dev->dev_private;
365         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
366         struct nouveau_pm_level cur;
367         int len = PAGE_SIZE, ret;
368         char *ptr = buf;
369
370         snprintf(ptr, len, "profile: %s, %s\nc:",
371                  pm->profile_ac->name, pm->profile_dc->name);
372         ptr += strlen(buf);
373         len -= strlen(buf);
374
375         ret = nouveau_pm_perflvl_get(dev, &cur);
376         if (ret == 0)
377                 nouveau_pm_perflvl_info(&cur, ptr, len);
378         return strlen(buf);
379 }
380
381 static ssize_t
382 nouveau_pm_set_perflvl(struct device *d, struct device_attribute *a,
383                        const char *buf, size_t count)
384 {
385         struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
386         int ret;
387
388         ret = nouveau_pm_profile_set(dev, buf);
389         if (ret)
390                 return ret;
391         return strlen(buf);
392 }
393
394 static DEVICE_ATTR(performance_level, S_IRUGO | S_IWUSR,
395                    nouveau_pm_get_perflvl, nouveau_pm_set_perflvl);
396
397 static int
398 nouveau_sysfs_init(struct drm_device *dev)
399 {
400         struct drm_nouveau_private *dev_priv = dev->dev_private;
401         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
402         struct device *d = &dev->pdev->dev;
403         int ret, i;
404
405         ret = device_create_file(d, &dev_attr_performance_level);
406         if (ret)
407                 return ret;
408
409         for (i = 0; i < pm->nr_perflvl; i++) {
410                 struct nouveau_pm_level *perflvl = &pm->perflvl[i];
411
412                 perflvl->dev_attr.attr.name = perflvl->name;
413                 perflvl->dev_attr.attr.mode = S_IRUGO;
414                 perflvl->dev_attr.show = nouveau_pm_get_perflvl_info;
415                 perflvl->dev_attr.store = NULL;
416                 sysfs_attr_init(&perflvl->dev_attr.attr);
417
418                 ret = device_create_file(d, &perflvl->dev_attr);
419                 if (ret) {
420                         NV_ERROR(dev, "failed pervlvl %d sysfs: %d\n",
421                                  perflvl->id, i);
422                         perflvl->dev_attr.attr.name = NULL;
423                         nouveau_pm_fini(dev);
424                         return ret;
425                 }
426         }
427
428         return 0;
429 }
430
431 static void
432 nouveau_sysfs_fini(struct drm_device *dev)
433 {
434         struct drm_nouveau_private *dev_priv = dev->dev_private;
435         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
436         struct device *d = &dev->pdev->dev;
437         int i;
438
439         device_remove_file(d, &dev_attr_performance_level);
440         for (i = 0; i < pm->nr_perflvl; i++) {
441                 struct nouveau_pm_level *pl = &pm->perflvl[i];
442
443                 if (!pl->dev_attr.attr.name)
444                         break;
445
446                 device_remove_file(d, &pl->dev_attr);
447         }
448 }
449
450 #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
451 static ssize_t
452 nouveau_hwmon_show_temp(struct device *d, struct device_attribute *a, char *buf)
453 {
454         struct drm_device *dev = dev_get_drvdata(d);
455         struct drm_nouveau_private *dev_priv = dev->dev_private;
456         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
457
458         return snprintf(buf, PAGE_SIZE, "%d\n", pm->temp_get(dev)*1000);
459 }
460 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, nouveau_hwmon_show_temp,
461                                                   NULL, 0);
462
463 static ssize_t
464 nouveau_hwmon_max_temp(struct device *d, struct device_attribute *a, char *buf)
465 {
466         struct drm_device *dev = dev_get_drvdata(d);
467         struct drm_nouveau_private *dev_priv = dev->dev_private;
468         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
469         struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
470
471         return snprintf(buf, PAGE_SIZE, "%d\n", temp->down_clock*1000);
472 }
473 static ssize_t
474 nouveau_hwmon_set_max_temp(struct device *d, struct device_attribute *a,
475                                                 const char *buf, size_t count)
476 {
477         struct drm_device *dev = dev_get_drvdata(d);
478         struct drm_nouveau_private *dev_priv = dev->dev_private;
479         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
480         struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
481         long value;
482
483         if (kstrtol(buf, 10, &value) == -EINVAL)
484                 return count;
485
486         temp->down_clock = value/1000;
487
488         nouveau_temp_safety_checks(dev);
489
490         return count;
491 }
492 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, nouveau_hwmon_max_temp,
493                                                   nouveau_hwmon_set_max_temp,
494                                                   0);
495
496 static ssize_t
497 nouveau_hwmon_critical_temp(struct device *d, struct device_attribute *a,
498                                                         char *buf)
499 {
500         struct drm_device *dev = dev_get_drvdata(d);
501         struct drm_nouveau_private *dev_priv = dev->dev_private;
502         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
503         struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
504
505         return snprintf(buf, PAGE_SIZE, "%d\n", temp->critical*1000);
506 }
507 static ssize_t
508 nouveau_hwmon_set_critical_temp(struct device *d, struct device_attribute *a,
509                                                             const char *buf,
510                                                                 size_t count)
511 {
512         struct drm_device *dev = dev_get_drvdata(d);
513         struct drm_nouveau_private *dev_priv = dev->dev_private;
514         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
515         struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
516         long value;
517
518         if (kstrtol(buf, 10, &value) == -EINVAL)
519                 return count;
520
521         temp->critical = value/1000;
522
523         nouveau_temp_safety_checks(dev);
524
525         return count;
526 }
527 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
528                                                 nouveau_hwmon_critical_temp,
529                                                 nouveau_hwmon_set_critical_temp,
530                                                 0);
531
532 static ssize_t nouveau_hwmon_show_name(struct device *dev,
533                                       struct device_attribute *attr,
534                                       char *buf)
535 {
536         return sprintf(buf, "nouveau\n");
537 }
538 static SENSOR_DEVICE_ATTR(name, S_IRUGO, nouveau_hwmon_show_name, NULL, 0);
539
540 static ssize_t nouveau_hwmon_show_update_rate(struct device *dev,
541                                       struct device_attribute *attr,
542                                       char *buf)
543 {
544         return sprintf(buf, "1000\n");
545 }
546 static SENSOR_DEVICE_ATTR(update_rate, S_IRUGO,
547                                                 nouveau_hwmon_show_update_rate,
548                                                 NULL, 0);
549
550 static ssize_t
551 nouveau_hwmon_show_fan0_input(struct device *d, struct device_attribute *attr,
552                               char *buf)
553 {
554         struct drm_device *dev = dev_get_drvdata(d);
555         struct drm_nouveau_private *dev_priv = dev->dev_private;
556         struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
557         struct gpio_func gpio;
558         u32 cycles, cur, prev;
559         u64 start;
560         int ret;
561
562         ret = nouveau_gpio_find(dev, 0, DCB_GPIO_FAN_SENSE, 0xff, &gpio);
563         if (ret)
564                 return ret;
565
566         /* Monitor the GPIO input 0x3b for 250ms.
567          * When the fan spins, it changes the value of GPIO FAN_SENSE.
568          * We get 4 changes (0 -> 1 -> 0 -> 1 -> [...]) per complete rotation.
569          */
570         start = ptimer->read(dev);
571         prev = nouveau_gpio_sense(dev, 0, gpio.line);
572         cycles = 0;
573         do {
574                 cur = nouveau_gpio_sense(dev, 0, gpio.line);
575                 if (prev != cur) {
576                         cycles++;
577                         prev = cur;
578                 }
579
580                 usleep_range(500, 1000); /* supports 0 < rpm < 7500 */
581         } while (ptimer->read(dev) - start < 250000000);
582
583         /* interpolate to get rpm */
584         return sprintf(buf, "%i\n", cycles / 4 * 4 * 60);
585 }
586 static SENSOR_DEVICE_ATTR(fan0_input, S_IRUGO, nouveau_hwmon_show_fan0_input,
587                           NULL, 0);
588
589 static ssize_t
590 nouveau_hwmon_get_pwm0(struct device *d, struct device_attribute *a, char *buf)
591 {
592         struct drm_device *dev = dev_get_drvdata(d);
593         int ret;
594
595         ret = nouveau_pwmfan_get(dev);
596         if (ret < 0)
597                 return ret;
598
599         return sprintf(buf, "%i\n", ret);
600 }
601
602 static ssize_t
603 nouveau_hwmon_set_pwm0(struct device *d, struct device_attribute *a,
604                        const char *buf, size_t count)
605 {
606         struct drm_device *dev = dev_get_drvdata(d);
607         struct drm_nouveau_private *dev_priv = dev->dev_private;
608         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
609         int ret = -ENODEV;
610         long value;
611
612         if (nouveau_perflvl_wr != 7777)
613                 return -EPERM;
614
615         if (kstrtol(buf, 10, &value) == -EINVAL)
616                 return -EINVAL;
617
618         if (value < pm->fan.min_duty)
619                 value = pm->fan.min_duty;
620         if (value > pm->fan.max_duty)
621                 value = pm->fan.max_duty;
622
623         ret = nouveau_pwmfan_set(dev, value);
624         if (ret)
625                 return ret;
626
627         return count;
628 }
629
630 static SENSOR_DEVICE_ATTR(pwm0, S_IRUGO | S_IWUSR,
631                           nouveau_hwmon_get_pwm0,
632                           nouveau_hwmon_set_pwm0, 0);
633
634 static ssize_t
635 nouveau_hwmon_get_pwm0_min(struct device *d,
636                            struct device_attribute *a, char *buf)
637 {
638         struct drm_device *dev = dev_get_drvdata(d);
639         struct drm_nouveau_private *dev_priv = dev->dev_private;
640         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
641
642         return sprintf(buf, "%i\n", pm->fan.min_duty);
643 }
644
645 static ssize_t
646 nouveau_hwmon_set_pwm0_min(struct device *d, struct device_attribute *a,
647                            const char *buf, size_t count)
648 {
649         struct drm_device *dev = dev_get_drvdata(d);
650         struct drm_nouveau_private *dev_priv = dev->dev_private;
651         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
652         long value;
653
654         if (kstrtol(buf, 10, &value) == -EINVAL)
655                 return -EINVAL;
656
657         if (value < 0)
658                 value = 0;
659
660         if (pm->fan.max_duty - value < 10)
661                 value = pm->fan.max_duty - 10;
662
663         if (value < 10)
664                 pm->fan.min_duty = 10;
665         else
666                 pm->fan.min_duty = value;
667
668         return count;
669 }
670
671 static SENSOR_DEVICE_ATTR(pwm0_min, S_IRUGO | S_IWUSR,
672                           nouveau_hwmon_get_pwm0_min,
673                           nouveau_hwmon_set_pwm0_min, 0);
674
675 static ssize_t
676 nouveau_hwmon_get_pwm0_max(struct device *d,
677                            struct device_attribute *a, char *buf)
678 {
679         struct drm_device *dev = dev_get_drvdata(d);
680         struct drm_nouveau_private *dev_priv = dev->dev_private;
681         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
682
683         return sprintf(buf, "%i\n", pm->fan.max_duty);
684 }
685
686 static ssize_t
687 nouveau_hwmon_set_pwm0_max(struct device *d, struct device_attribute *a,
688                            const char *buf, size_t count)
689 {
690         struct drm_device *dev = dev_get_drvdata(d);
691         struct drm_nouveau_private *dev_priv = dev->dev_private;
692         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
693         long value;
694
695         if (kstrtol(buf, 10, &value) == -EINVAL)
696                 return -EINVAL;
697
698         if (value < 0)
699                 value = 0;
700
701         if (value - pm->fan.min_duty < 10)
702                 value = pm->fan.min_duty + 10;
703
704         if (value > 100)
705                 pm->fan.max_duty = 100;
706         else
707                 pm->fan.max_duty = value;
708
709         return count;
710 }
711
712 static SENSOR_DEVICE_ATTR(pwm0_max, S_IRUGO | S_IWUSR,
713                           nouveau_hwmon_get_pwm0_max,
714                           nouveau_hwmon_set_pwm0_max, 0);
715
716 static struct attribute *hwmon_attributes[] = {
717         &sensor_dev_attr_temp1_input.dev_attr.attr,
718         &sensor_dev_attr_temp1_max.dev_attr.attr,
719         &sensor_dev_attr_temp1_crit.dev_attr.attr,
720         &sensor_dev_attr_name.dev_attr.attr,
721         &sensor_dev_attr_update_rate.dev_attr.attr,
722         NULL
723 };
724 static struct attribute *hwmon_fan_rpm_attributes[] = {
725         &sensor_dev_attr_fan0_input.dev_attr.attr,
726         NULL
727 };
728 static struct attribute *hwmon_pwm_fan_attributes[] = {
729         &sensor_dev_attr_pwm0.dev_attr.attr,
730         &sensor_dev_attr_pwm0_min.dev_attr.attr,
731         &sensor_dev_attr_pwm0_max.dev_attr.attr,
732         NULL
733 };
734
735 static const struct attribute_group hwmon_attrgroup = {
736         .attrs = hwmon_attributes,
737 };
738 static const struct attribute_group hwmon_fan_rpm_attrgroup = {
739         .attrs = hwmon_fan_rpm_attributes,
740 };
741 static const struct attribute_group hwmon_pwm_fan_attrgroup = {
742         .attrs = hwmon_pwm_fan_attributes,
743 };
744 #endif
745
746 static int
747 nouveau_hwmon_init(struct drm_device *dev)
748 {
749         struct drm_nouveau_private *dev_priv = dev->dev_private;
750         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
751 #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
752         struct device *hwmon_dev;
753         int ret = 0;
754
755         if (!pm->temp_get)
756                 return -ENODEV;
757
758         hwmon_dev = hwmon_device_register(&dev->pdev->dev);
759         if (IS_ERR(hwmon_dev)) {
760                 ret = PTR_ERR(hwmon_dev);
761                 NV_ERROR(dev,
762                         "Unable to register hwmon device: %d\n", ret);
763                 return ret;
764         }
765         dev_set_drvdata(hwmon_dev, dev);
766
767         /* default sysfs entries */
768         ret = sysfs_create_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
769         if (ret) {
770                 if (ret)
771                         goto error;
772         }
773
774         /* if the card has a pwm fan */
775         /*XXX: incorrect, need better detection for this, some boards have
776          *     the gpio entries for pwm fan control even when there's no
777          *     actual fan connected to it... therm table? */
778         if (nouveau_pwmfan_get(dev) >= 0) {
779                 ret = sysfs_create_group(&dev->pdev->dev.kobj,
780                                          &hwmon_pwm_fan_attrgroup);
781                 if (ret)
782                         goto error;
783         }
784
785         /* if the card can read the fan rpm */
786         if (nouveau_gpio_func_valid(dev, DCB_GPIO_FAN_SENSE)) {
787                 ret = sysfs_create_group(&dev->pdev->dev.kobj,
788                                          &hwmon_fan_rpm_attrgroup);
789                 if (ret)
790                         goto error;
791         }
792
793         pm->hwmon = hwmon_dev;
794
795         return 0;
796
797 error:
798         NV_ERROR(dev, "Unable to create some hwmon sysfs files: %d\n", ret);
799         hwmon_device_unregister(hwmon_dev);
800         pm->hwmon = NULL;
801         return ret;
802 #else
803         pm->hwmon = NULL;
804         return 0;
805 #endif
806 }
807
808 static void
809 nouveau_hwmon_fini(struct drm_device *dev)
810 {
811 #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
812         struct drm_nouveau_private *dev_priv = dev->dev_private;
813         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
814
815         if (pm->hwmon) {
816                 sysfs_remove_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
817                 sysfs_remove_group(&dev->pdev->dev.kobj,
818                                    &hwmon_pwm_fan_attrgroup);
819                 sysfs_remove_group(&dev->pdev->dev.kobj,
820                                    &hwmon_fan_rpm_attrgroup);
821
822                 hwmon_device_unregister(pm->hwmon);
823         }
824 #endif
825 }
826
827 #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
828 static int
829 nouveau_pm_acpi_event(struct notifier_block *nb, unsigned long val, void *data)
830 {
831         struct drm_nouveau_private *dev_priv =
832                 container_of(nb, struct drm_nouveau_private, engine.pm.acpi_nb);
833         struct drm_device *dev = dev_priv->dev;
834         struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
835
836         if (strcmp(entry->device_class, "ac_adapter") == 0) {
837                 bool ac = power_supply_is_system_supplied();
838
839                 NV_DEBUG(dev, "power supply changed: %s\n", ac ? "AC" : "DC");
840                 nouveau_pm_trigger(dev);
841         }
842
843         return NOTIFY_OK;
844 }
845 #endif
846
847 int
848 nouveau_pm_init(struct drm_device *dev)
849 {
850         struct drm_nouveau_private *dev_priv = dev->dev_private;
851         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
852         char info[256];
853         int ret, i;
854
855         /* parse aux tables from vbios */
856         nouveau_volt_init(dev);
857         nouveau_temp_init(dev);
858
859         /* determine current ("boot") performance level */
860         ret = nouveau_pm_perflvl_get(dev, &pm->boot);
861         if (ret) {
862                 NV_ERROR(dev, "failed to determine boot perflvl\n");
863                 return ret;
864         }
865
866         strncpy(pm->boot.name, "boot", 4);
867         strncpy(pm->boot.profile.name, "boot", 4);
868         pm->boot.profile.func = &nouveau_pm_static_profile_func;
869
870         INIT_LIST_HEAD(&pm->profiles);
871         list_add(&pm->boot.profile.head, &pm->profiles);
872
873         pm->profile_ac = &pm->boot.profile;
874         pm->profile_dc = &pm->boot.profile;
875         pm->profile = &pm->boot.profile;
876         pm->cur = &pm->boot;
877
878         /* add performance levels from vbios */
879         nouveau_perf_init(dev);
880
881         /* display available performance levels */
882         NV_INFO(dev, "%d available performance level(s)\n", pm->nr_perflvl);
883         for (i = 0; i < pm->nr_perflvl; i++) {
884                 nouveau_pm_perflvl_info(&pm->perflvl[i], info, sizeof(info));
885                 NV_INFO(dev, "%d:%s", pm->perflvl[i].id, info);
886         }
887
888         nouveau_pm_perflvl_info(&pm->boot, info, sizeof(info));
889         NV_INFO(dev, "c:%s", info);
890
891         /* switch performance levels now if requested */
892         if (nouveau_perflvl != NULL)
893                 nouveau_pm_profile_set(dev, nouveau_perflvl);
894
895         /* determine the current fan speed */
896         pm->fan.percent = nouveau_pwmfan_get(dev);
897
898         nouveau_sysfs_init(dev);
899         nouveau_hwmon_init(dev);
900 #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
901         pm->acpi_nb.notifier_call = nouveau_pm_acpi_event;
902         register_acpi_notifier(&pm->acpi_nb);
903 #endif
904
905         return 0;
906 }
907
908 void
909 nouveau_pm_fini(struct drm_device *dev)
910 {
911         struct drm_nouveau_private *dev_priv = dev->dev_private;
912         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
913         struct nouveau_pm_profile *profile, *tmp;
914
915         list_for_each_entry_safe(profile, tmp, &pm->profiles, head) {
916                 list_del(&profile->head);
917                 profile->func->destroy(profile);
918         }
919
920         if (pm->cur != &pm->boot)
921                 nouveau_pm_perflvl_set(dev, &pm->boot);
922
923         nouveau_temp_fini(dev);
924         nouveau_perf_fini(dev);
925         nouveau_volt_fini(dev);
926
927 #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
928         unregister_acpi_notifier(&pm->acpi_nb);
929 #endif
930         nouveau_hwmon_fini(dev);
931         nouveau_sysfs_fini(dev);
932 }
933
934 void
935 nouveau_pm_resume(struct drm_device *dev)
936 {
937         struct drm_nouveau_private *dev_priv = dev->dev_private;
938         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
939         struct nouveau_pm_level *perflvl;
940
941         if (!pm->cur || pm->cur == &pm->boot)
942                 return;
943
944         perflvl = pm->cur;
945         pm->cur = &pm->boot;
946         nouveau_pm_perflvl_set(dev, perflvl);
947         nouveau_pwmfan_set(dev, pm->fan.percent);
948 }