aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpu/drm/nouveau/nouveau_pm.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/gpu/drm/nouveau/nouveau_pm.c')
-rw-r--r--drivers/gpu/drm/nouveau/nouveau_pm.c518
1 files changed, 518 insertions, 0 deletions
diff --git a/drivers/gpu/drm/nouveau/nouveau_pm.c b/drivers/gpu/drm/nouveau/nouveau_pm.c
new file mode 100644
index 000000000000..1c99c55d6d46
--- /dev/null
+++ b/drivers/gpu/drm/nouveau/nouveau_pm.c
@@ -0,0 +1,518 @@
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
33static int
34nouveau_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
53static int
54nouveau_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
80static int
81nouveau_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
115static int
116nouveau_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
152static void
153nouveau_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
177static ssize_t
178nouveau_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
193static ssize_t
194nouveau_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
218static ssize_t
219nouveau_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
231static DEVICE_ATTR(performance_level, S_IRUGO | S_IWUSR,
232 nouveau_pm_get_perflvl, nouveau_pm_set_perflvl);
233
234static int
235nouveau_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
268static void
269nouveau_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
287static ssize_t
288nouveau_hwmon_show_temp(struct device *d, struct device_attribute *a, char *buf)
289{
290 struct drm_device *dev = dev_get_drvdata(d);
291 struct drm_nouveau_private *dev_priv = dev->dev_private;
292 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
293
294 return snprintf(buf, PAGE_SIZE, "%d\n", pm->temp_get(dev)*1000);
295}
296static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, nouveau_hwmon_show_temp,
297 NULL, 0);
298
299static ssize_t
300nouveau_hwmon_max_temp(struct device *d, struct device_attribute *a, char *buf)
301{
302 struct drm_device *dev = dev_get_drvdata(d);
303 struct drm_nouveau_private *dev_priv = dev->dev_private;
304 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
305 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
306
307 return snprintf(buf, PAGE_SIZE, "%d\n", temp->down_clock*1000);
308}
309static ssize_t
310nouveau_hwmon_set_max_temp(struct device *d, struct device_attribute *a,
311 const char *buf, size_t count)
312{
313 struct drm_device *dev = dev_get_drvdata(d);
314 struct drm_nouveau_private *dev_priv = dev->dev_private;
315 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
316 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
317 long value;
318
319 if (strict_strtol(buf, 10, &value) == -EINVAL)
320 return count;
321
322 temp->down_clock = value/1000;
323
324 nouveau_temp_safety_checks(dev);
325
326 return count;
327}
328static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, nouveau_hwmon_max_temp,
329 nouveau_hwmon_set_max_temp,
330 0);
331
332static ssize_t
333nouveau_hwmon_critical_temp(struct device *d, struct device_attribute *a,
334 char *buf)
335{
336 struct drm_device *dev = dev_get_drvdata(d);
337 struct drm_nouveau_private *dev_priv = dev->dev_private;
338 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
339 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
340
341 return snprintf(buf, PAGE_SIZE, "%d\n", temp->critical*1000);
342}
343static ssize_t
344nouveau_hwmon_set_critical_temp(struct device *d, struct device_attribute *a,
345 const char *buf,
346 size_t count)
347{
348 struct drm_device *dev = dev_get_drvdata(d);
349 struct drm_nouveau_private *dev_priv = dev->dev_private;
350 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
351 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
352 long value;
353
354 if (strict_strtol(buf, 10, &value) == -EINVAL)
355 return count;
356
357 temp->critical = value/1000;
358
359 nouveau_temp_safety_checks(dev);
360
361 return count;
362}
363static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
364 nouveau_hwmon_critical_temp,
365 nouveau_hwmon_set_critical_temp,
366 0);
367
368static ssize_t nouveau_hwmon_show_name(struct device *dev,
369 struct device_attribute *attr,
370 char *buf)
371{
372 return sprintf(buf, "nouveau\n");
373}
374static SENSOR_DEVICE_ATTR(name, S_IRUGO, nouveau_hwmon_show_name, NULL, 0);
375
376static ssize_t nouveau_hwmon_show_update_rate(struct device *dev,
377 struct device_attribute *attr,
378 char *buf)
379{
380 return sprintf(buf, "1000\n");
381}
382static SENSOR_DEVICE_ATTR(update_rate, S_IRUGO,
383 nouveau_hwmon_show_update_rate,
384 NULL, 0);
385
386static struct attribute *hwmon_attributes[] = {
387 &sensor_dev_attr_temp1_input.dev_attr.attr,
388 &sensor_dev_attr_temp1_max.dev_attr.attr,
389 &sensor_dev_attr_temp1_crit.dev_attr.attr,
390 &sensor_dev_attr_name.dev_attr.attr,
391 &sensor_dev_attr_update_rate.dev_attr.attr,
392 NULL
393};
394
395static const struct attribute_group hwmon_attrgroup = {
396 .attrs = hwmon_attributes,
397};
398
399static int
400nouveau_hwmon_init(struct drm_device *dev)
401{
402 struct drm_nouveau_private *dev_priv = dev->dev_private;
403 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
404 struct device *hwmon_dev;
405 int ret;
406
407 if (!pm->temp_get)
408 return -ENODEV;
409
410 hwmon_dev = hwmon_device_register(&dev->pdev->dev);
411 if (IS_ERR(hwmon_dev)) {
412 ret = PTR_ERR(hwmon_dev);
413 NV_ERROR(dev,
414 "Unable to register hwmon device: %d\n", ret);
415 return ret;
416 }
417 dev_set_drvdata(hwmon_dev, dev);
418 ret = sysfs_create_group(&hwmon_dev->kobj,
419 &hwmon_attrgroup);
420 if (ret) {
421 NV_ERROR(dev,
422 "Unable to create hwmon sysfs file: %d\n", ret);
423 hwmon_device_unregister(hwmon_dev);
424 return ret;
425 }
426
427 pm->hwmon = hwmon_dev;
428
429 return 0;
430}
431
432static void
433nouveau_hwmon_fini(struct drm_device *dev)
434{
435 struct drm_nouveau_private *dev_priv = dev->dev_private;
436 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
437
438 if (pm->hwmon) {
439 sysfs_remove_group(&pm->hwmon->kobj, &hwmon_attrgroup);
440 hwmon_device_unregister(pm->hwmon);
441 }
442}
443
444int
445nouveau_pm_init(struct drm_device *dev)
446{
447 struct drm_nouveau_private *dev_priv = dev->dev_private;
448 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
449 char info[256];
450 int ret, i;
451
452 nouveau_volt_init(dev);
453 nouveau_perf_init(dev);
454 nouveau_temp_init(dev);
455 nouveau_mem_timing_init(dev);
456
457 NV_INFO(dev, "%d available performance level(s)\n", pm->nr_perflvl);
458 for (i = 0; i < pm->nr_perflvl; i++) {
459 nouveau_pm_perflvl_info(&pm->perflvl[i], info, sizeof(info));
460 NV_INFO(dev, "%d: %s", pm->perflvl[i].id, info);
461 }
462
463 /* determine current ("boot") performance level */
464 ret = nouveau_pm_perflvl_get(dev, &pm->boot);
465 if (ret == 0) {
466 pm->cur = &pm->boot;
467
468 nouveau_pm_perflvl_info(&pm->boot, info, sizeof(info));
469 NV_INFO(dev, "c: %s", info);
470 }
471
472 /* switch performance levels now if requested */
473 if (nouveau_perflvl != NULL) {
474 ret = nouveau_pm_profile_set(dev, nouveau_perflvl);
475 if (ret) {
476 NV_ERROR(dev, "error setting perflvl \"%s\": %d\n",
477 nouveau_perflvl, ret);
478 }
479 }
480
481 nouveau_sysfs_init(dev);
482 nouveau_hwmon_init(dev);
483
484 return 0;
485}
486
487void
488nouveau_pm_fini(struct drm_device *dev)
489{
490 struct drm_nouveau_private *dev_priv = dev->dev_private;
491 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
492
493 if (pm->cur != &pm->boot)
494 nouveau_pm_perflvl_set(dev, &pm->boot);
495
496 nouveau_mem_timing_fini(dev);
497 nouveau_temp_fini(dev);
498 nouveau_perf_fini(dev);
499 nouveau_volt_fini(dev);
500
501 nouveau_hwmon_fini(dev);
502 nouveau_sysfs_fini(dev);
503}
504
505void
506nouveau_pm_resume(struct drm_device *dev)
507{
508 struct drm_nouveau_private *dev_priv = dev->dev_private;
509 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
510 struct nouveau_pm_level *perflvl;
511
512 if (pm->cur == &pm->boot)
513 return;
514
515 perflvl = pm->cur;
516 pm->cur = &pm->boot;
517 nouveau_pm_perflvl_set(dev, perflvl);
518}