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