drm/nv50/pm: add support for pwm fan control
[firefly-linux-kernel-4.4.55.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
30 #ifdef CONFIG_ACPI
31 #include <linux/acpi.h>
32 #endif
33 #include <linux/power_supply.h>
34 #include <linux/hwmon.h>
35 #include <linux/hwmon-sysfs.h>
36
37 static int
38 nouveau_pm_clock_set(struct drm_device *dev, struct nouveau_pm_level *perflvl,
39                      u8 id, u32 khz)
40 {
41         struct drm_nouveau_private *dev_priv = dev->dev_private;
42         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
43         void *pre_state;
44
45         if (khz == 0)
46                 return 0;
47
48         pre_state = pm->clock_pre(dev, perflvl, id, khz);
49         if (IS_ERR(pre_state))
50                 return PTR_ERR(pre_state);
51
52         if (pre_state)
53                 pm->clock_set(dev, pre_state);
54         return 0;
55 }
56
57 static int
58 nouveau_pm_perflvl_set(struct drm_device *dev, struct nouveau_pm_level *perflvl)
59 {
60         struct drm_nouveau_private *dev_priv = dev->dev_private;
61         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
62         int ret;
63
64         if (perflvl == pm->cur)
65                 return 0;
66
67         if (pm->fanspeed_set && perflvl->fanspeed) {
68                 ret = pm->fanspeed_set(dev, perflvl->fanspeed);
69                 if (ret)
70                         NV_ERROR(dev, "set fanspeed failed: %d\n", ret);
71         }
72
73         if (pm->voltage.supported && pm->voltage_set && perflvl->volt_min) {
74                 ret = pm->voltage_set(dev, perflvl->volt_min);
75                 if (ret) {
76                         NV_ERROR(dev, "voltage_set %d failed: %d\n",
77                                  perflvl->volt_min, ret);
78                 }
79         }
80
81         if (pm->clocks_pre) {
82                 void *state = pm->clocks_pre(dev, perflvl);
83                 if (IS_ERR(state))
84                         return PTR_ERR(state);
85                 pm->clocks_set(dev, state);
86         } else
87         if (pm->clock_set) {
88                 nouveau_pm_clock_set(dev, perflvl, PLL_CORE, perflvl->core);
89                 nouveau_pm_clock_set(dev, perflvl, PLL_SHADER, perflvl->shader);
90                 nouveau_pm_clock_set(dev, perflvl, PLL_MEMORY, perflvl->memory);
91                 nouveau_pm_clock_set(dev, perflvl, PLL_UNK05, perflvl->unk05);
92         }
93
94         pm->cur = perflvl;
95         return 0;
96 }
97
98 static int
99 nouveau_pm_profile_set(struct drm_device *dev, const char *profile)
100 {
101         struct drm_nouveau_private *dev_priv = dev->dev_private;
102         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
103         struct nouveau_pm_level *perflvl = NULL;
104
105         /* safety precaution, for now */
106         if (nouveau_perflvl_wr != 7777)
107                 return -EPERM;
108
109         if (!strncmp(profile, "boot", 4))
110                 perflvl = &pm->boot;
111         else {
112                 int pl = simple_strtol(profile, NULL, 10);
113                 int i;
114
115                 for (i = 0; i < pm->nr_perflvl; i++) {
116                         if (pm->perflvl[i].id == pl) {
117                                 perflvl = &pm->perflvl[i];
118                                 break;
119                         }
120                 }
121
122                 if (!perflvl)
123                         return -EINVAL;
124         }
125
126         NV_INFO(dev, "setting performance level: %s\n", profile);
127         return nouveau_pm_perflvl_set(dev, perflvl);
128 }
129
130 static int
131 nouveau_pm_perflvl_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
132 {
133         struct drm_nouveau_private *dev_priv = dev->dev_private;
134         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
135         int ret;
136
137         memset(perflvl, 0, sizeof(*perflvl));
138
139         if (pm->clocks_get) {
140                 ret = pm->clocks_get(dev, perflvl);
141                 if (ret)
142                         return ret;
143         } else
144         if (pm->clock_get) {
145                 ret = pm->clock_get(dev, PLL_CORE);
146                 if (ret > 0)
147                         perflvl->core = ret;
148
149                 ret = pm->clock_get(dev, PLL_MEMORY);
150                 if (ret > 0)
151                         perflvl->memory = ret;
152
153                 ret = pm->clock_get(dev, PLL_SHADER);
154                 if (ret > 0)
155                         perflvl->shader = ret;
156
157                 ret = pm->clock_get(dev, PLL_UNK05);
158                 if (ret > 0)
159                         perflvl->unk05 = ret;
160         }
161
162         if (pm->voltage.supported && pm->voltage_get) {
163                 ret = pm->voltage_get(dev);
164                 if (ret > 0) {
165                         perflvl->volt_min = ret;
166                         perflvl->volt_max = ret;
167                 }
168         }
169
170         if (pm->fanspeed_get) {
171                 ret = pm->fanspeed_get(dev);
172                 if (ret > 0)
173                         perflvl->fanspeed = ret;
174         }
175
176         return 0;
177 }
178
179 static void
180 nouveau_pm_perflvl_info(struct nouveau_pm_level *perflvl, char *ptr, int len)
181 {
182         char c[16], s[16], v[32], f[16], t[16], m[16];
183
184         c[0] = '\0';
185         if (perflvl->core)
186                 snprintf(c, sizeof(c), " core %dMHz", perflvl->core / 1000);
187
188         s[0] = '\0';
189         if (perflvl->shader)
190                 snprintf(s, sizeof(s), " shader %dMHz", perflvl->shader / 1000);
191
192         m[0] = '\0';
193         if (perflvl->memory)
194                 snprintf(m, sizeof(m), " memory %dMHz", perflvl->memory / 1000);
195
196         v[0] = '\0';
197         if (perflvl->volt_min && perflvl->volt_min != perflvl->volt_max) {
198                 snprintf(v, sizeof(v), " voltage %dmV-%dmV",
199                          perflvl->volt_min / 1000, perflvl->volt_max / 1000);
200         } else
201         if (perflvl->volt_min) {
202                 snprintf(v, sizeof(v), " voltage %dmV",
203                          perflvl->volt_min / 1000);
204         }
205
206         f[0] = '\0';
207         if (perflvl->fanspeed)
208                 snprintf(f, sizeof(f), " fanspeed %d%%", perflvl->fanspeed);
209
210         t[0] = '\0';
211         if (perflvl->timing)
212                 snprintf(t, sizeof(t), " timing %d", perflvl->timing->id);
213
214         snprintf(ptr, len, "%s%s%s%s%s%s\n", c, s, m, t, v, f);
215 }
216
217 static ssize_t
218 nouveau_pm_get_perflvl_info(struct device *d,
219                             struct device_attribute *a, char *buf)
220 {
221         struct nouveau_pm_level *perflvl = (struct nouveau_pm_level *)a;
222         char *ptr = buf;
223         int len = PAGE_SIZE;
224
225         snprintf(ptr, len, "%d:", perflvl->id);
226         ptr += strlen(buf);
227         len -= strlen(buf);
228
229         nouveau_pm_perflvl_info(perflvl, ptr, len);
230         return strlen(buf);
231 }
232
233 static ssize_t
234 nouveau_pm_get_perflvl(struct device *d, struct device_attribute *a, char *buf)
235 {
236         struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
237         struct drm_nouveau_private *dev_priv = dev->dev_private;
238         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
239         struct nouveau_pm_level cur;
240         int len = PAGE_SIZE, ret;
241         char *ptr = buf;
242
243         if (!pm->cur)
244                 snprintf(ptr, len, "setting: boot\n");
245         else if (pm->cur == &pm->boot)
246                 snprintf(ptr, len, "setting: boot\nc:");
247         else
248                 snprintf(ptr, len, "setting: static %d\nc:", pm->cur->id);
249         ptr += strlen(buf);
250         len -= strlen(buf);
251
252         ret = nouveau_pm_perflvl_get(dev, &cur);
253         if (ret == 0)
254                 nouveau_pm_perflvl_info(&cur, ptr, len);
255         return strlen(buf);
256 }
257
258 static ssize_t
259 nouveau_pm_set_perflvl(struct device *d, struct device_attribute *a,
260                        const char *buf, size_t count)
261 {
262         struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
263         int ret;
264
265         ret = nouveau_pm_profile_set(dev, buf);
266         if (ret)
267                 return ret;
268         return strlen(buf);
269 }
270
271 static DEVICE_ATTR(performance_level, S_IRUGO | S_IWUSR,
272                    nouveau_pm_get_perflvl, nouveau_pm_set_perflvl);
273
274 static int
275 nouveau_sysfs_init(struct drm_device *dev)
276 {
277         struct drm_nouveau_private *dev_priv = dev->dev_private;
278         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
279         struct device *d = &dev->pdev->dev;
280         int ret, i;
281
282         ret = device_create_file(d, &dev_attr_performance_level);
283         if (ret)
284                 return ret;
285
286         for (i = 0; i < pm->nr_perflvl; i++) {
287                 struct nouveau_pm_level *perflvl = &pm->perflvl[i];
288
289                 perflvl->dev_attr.attr.name = perflvl->name;
290                 perflvl->dev_attr.attr.mode = S_IRUGO;
291                 perflvl->dev_attr.show = nouveau_pm_get_perflvl_info;
292                 perflvl->dev_attr.store = NULL;
293                 sysfs_attr_init(&perflvl->dev_attr.attr);
294
295                 ret = device_create_file(d, &perflvl->dev_attr);
296                 if (ret) {
297                         NV_ERROR(dev, "failed pervlvl %d sysfs: %d\n",
298                                  perflvl->id, i);
299                         perflvl->dev_attr.attr.name = NULL;
300                         nouveau_pm_fini(dev);
301                         return ret;
302                 }
303         }
304
305         return 0;
306 }
307
308 static void
309 nouveau_sysfs_fini(struct drm_device *dev)
310 {
311         struct drm_nouveau_private *dev_priv = dev->dev_private;
312         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
313         struct device *d = &dev->pdev->dev;
314         int i;
315
316         device_remove_file(d, &dev_attr_performance_level);
317         for (i = 0; i < pm->nr_perflvl; i++) {
318                 struct nouveau_pm_level *pl = &pm->perflvl[i];
319
320                 if (!pl->dev_attr.attr.name)
321                         break;
322
323                 device_remove_file(d, &pl->dev_attr);
324         }
325 }
326
327 #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
328 static ssize_t
329 nouveau_hwmon_show_temp(struct device *d, struct device_attribute *a, char *buf)
330 {
331         struct drm_device *dev = dev_get_drvdata(d);
332         struct drm_nouveau_private *dev_priv = dev->dev_private;
333         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
334
335         return snprintf(buf, PAGE_SIZE, "%d\n", pm->temp_get(dev)*1000);
336 }
337 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, nouveau_hwmon_show_temp,
338                                                   NULL, 0);
339
340 static ssize_t
341 nouveau_hwmon_max_temp(struct device *d, struct device_attribute *a, char *buf)
342 {
343         struct drm_device *dev = dev_get_drvdata(d);
344         struct drm_nouveau_private *dev_priv = dev->dev_private;
345         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
346         struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
347
348         return snprintf(buf, PAGE_SIZE, "%d\n", temp->down_clock*1000);
349 }
350 static ssize_t
351 nouveau_hwmon_set_max_temp(struct device *d, struct device_attribute *a,
352                                                 const char *buf, size_t count)
353 {
354         struct drm_device *dev = dev_get_drvdata(d);
355         struct drm_nouveau_private *dev_priv = dev->dev_private;
356         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
357         struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
358         long value;
359
360         if (strict_strtol(buf, 10, &value) == -EINVAL)
361                 return count;
362
363         temp->down_clock = value/1000;
364
365         nouveau_temp_safety_checks(dev);
366
367         return count;
368 }
369 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, nouveau_hwmon_max_temp,
370                                                   nouveau_hwmon_set_max_temp,
371                                                   0);
372
373 static ssize_t
374 nouveau_hwmon_critical_temp(struct device *d, struct device_attribute *a,
375                                                         char *buf)
376 {
377         struct drm_device *dev = dev_get_drvdata(d);
378         struct drm_nouveau_private *dev_priv = dev->dev_private;
379         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
380         struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
381
382         return snprintf(buf, PAGE_SIZE, "%d\n", temp->critical*1000);
383 }
384 static ssize_t
385 nouveau_hwmon_set_critical_temp(struct device *d, struct device_attribute *a,
386                                                             const char *buf,
387                                                                 size_t count)
388 {
389         struct drm_device *dev = dev_get_drvdata(d);
390         struct drm_nouveau_private *dev_priv = dev->dev_private;
391         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
392         struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
393         long value;
394
395         if (strict_strtol(buf, 10, &value) == -EINVAL)
396                 return count;
397
398         temp->critical = value/1000;
399
400         nouveau_temp_safety_checks(dev);
401
402         return count;
403 }
404 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
405                                                 nouveau_hwmon_critical_temp,
406                                                 nouveau_hwmon_set_critical_temp,
407                                                 0);
408
409 static ssize_t nouveau_hwmon_show_name(struct device *dev,
410                                       struct device_attribute *attr,
411                                       char *buf)
412 {
413         return sprintf(buf, "nouveau\n");
414 }
415 static SENSOR_DEVICE_ATTR(name, S_IRUGO, nouveau_hwmon_show_name, NULL, 0);
416
417 static ssize_t nouveau_hwmon_show_update_rate(struct device *dev,
418                                       struct device_attribute *attr,
419                                       char *buf)
420 {
421         return sprintf(buf, "1000\n");
422 }
423 static SENSOR_DEVICE_ATTR(update_rate, S_IRUGO,
424                                                 nouveau_hwmon_show_update_rate,
425                                                 NULL, 0);
426
427 static struct attribute *hwmon_attributes[] = {
428         &sensor_dev_attr_temp1_input.dev_attr.attr,
429         &sensor_dev_attr_temp1_max.dev_attr.attr,
430         &sensor_dev_attr_temp1_crit.dev_attr.attr,
431         &sensor_dev_attr_name.dev_attr.attr,
432         &sensor_dev_attr_update_rate.dev_attr.attr,
433         NULL
434 };
435
436 static const struct attribute_group hwmon_attrgroup = {
437         .attrs = hwmon_attributes,
438 };
439 #endif
440
441 static int
442 nouveau_hwmon_init(struct drm_device *dev)
443 {
444 #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
445         struct drm_nouveau_private *dev_priv = dev->dev_private;
446         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
447         struct device *hwmon_dev;
448         int ret;
449
450         if (!pm->temp_get)
451                 return -ENODEV;
452
453         hwmon_dev = hwmon_device_register(&dev->pdev->dev);
454         if (IS_ERR(hwmon_dev)) {
455                 ret = PTR_ERR(hwmon_dev);
456                 NV_ERROR(dev,
457                         "Unable to register hwmon device: %d\n", ret);
458                 return ret;
459         }
460         dev_set_drvdata(hwmon_dev, dev);
461         ret = sysfs_create_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
462         if (ret) {
463                 NV_ERROR(dev,
464                         "Unable to create hwmon sysfs file: %d\n", ret);
465                 hwmon_device_unregister(hwmon_dev);
466                 return ret;
467         }
468
469         pm->hwmon = hwmon_dev;
470 #endif
471         return 0;
472 }
473
474 static void
475 nouveau_hwmon_fini(struct drm_device *dev)
476 {
477 #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
478         struct drm_nouveau_private *dev_priv = dev->dev_private;
479         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
480
481         if (pm->hwmon) {
482                 sysfs_remove_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
483                 hwmon_device_unregister(pm->hwmon);
484         }
485 #endif
486 }
487
488 #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
489 static int
490 nouveau_pm_acpi_event(struct notifier_block *nb, unsigned long val, void *data)
491 {
492         struct drm_nouveau_private *dev_priv =
493                 container_of(nb, struct drm_nouveau_private, engine.pm.acpi_nb);
494         struct drm_device *dev = dev_priv->dev;
495         struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
496
497         if (strcmp(entry->device_class, "ac_adapter") == 0) {
498                 bool ac = power_supply_is_system_supplied();
499
500                 NV_DEBUG(dev, "power supply changed: %s\n", ac ? "AC" : "DC");
501         }
502
503         return NOTIFY_OK;
504 }
505 #endif
506
507 int
508 nouveau_pm_init(struct drm_device *dev)
509 {
510         struct drm_nouveau_private *dev_priv = dev->dev_private;
511         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
512         char info[256];
513         int ret, i;
514
515         nouveau_mem_timing_init(dev);
516         nouveau_volt_init(dev);
517         nouveau_perf_init(dev);
518         nouveau_temp_init(dev);
519
520         NV_INFO(dev, "%d available performance level(s)\n", pm->nr_perflvl);
521         for (i = 0; i < pm->nr_perflvl; i++) {
522                 nouveau_pm_perflvl_info(&pm->perflvl[i], info, sizeof(info));
523                 NV_INFO(dev, "%d:%s", pm->perflvl[i].id, info);
524         }
525
526         /* determine current ("boot") performance level */
527         ret = nouveau_pm_perflvl_get(dev, &pm->boot);
528         if (ret == 0) {
529                 strncpy(pm->boot.name, "boot", 4);
530                 pm->cur = &pm->boot;
531
532                 nouveau_pm_perflvl_info(&pm->boot, info, sizeof(info));
533                 NV_INFO(dev, "c:%s", info);
534         }
535
536         /* switch performance levels now if requested */
537         if (nouveau_perflvl != NULL) {
538                 ret = nouveau_pm_profile_set(dev, nouveau_perflvl);
539                 if (ret) {
540                         NV_ERROR(dev, "error setting perflvl \"%s\": %d\n",
541                                  nouveau_perflvl, ret);
542                 }
543         }
544
545         nouveau_sysfs_init(dev);
546         nouveau_hwmon_init(dev);
547 #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
548         pm->acpi_nb.notifier_call = nouveau_pm_acpi_event;
549         register_acpi_notifier(&pm->acpi_nb);
550 #endif
551
552         return 0;
553 }
554
555 void
556 nouveau_pm_fini(struct drm_device *dev)
557 {
558         struct drm_nouveau_private *dev_priv = dev->dev_private;
559         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
560
561         if (pm->cur != &pm->boot)
562                 nouveau_pm_perflvl_set(dev, &pm->boot);
563
564         nouveau_temp_fini(dev);
565         nouveau_perf_fini(dev);
566         nouveau_volt_fini(dev);
567         nouveau_mem_timing_fini(dev);
568
569 #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
570         unregister_acpi_notifier(&pm->acpi_nb);
571 #endif
572         nouveau_hwmon_fini(dev);
573         nouveau_sysfs_fini(dev);
574 }
575
576 void
577 nouveau_pm_resume(struct drm_device *dev)
578 {
579         struct drm_nouveau_private *dev_priv = dev->dev_private;
580         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
581         struct nouveau_pm_level *perflvl;
582
583         if (!pm->cur || pm->cur == &pm->boot)
584                 return;
585
586         perflvl = pm->cur;
587         pm->cur = &pm->boot;
588         nouveau_pm_perflvl_set(dev, perflvl);
589 }