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