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