blob: 85a56dea0ef76e5d5751444f8cd20432b75bba64 [file] [log] [blame]
Ben Skeggs330c5982010-09-16 15:39:49 +10001/*
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
Martin Peres34e9d852010-09-22 20:54:22 +020030#include <linux/hwmon.h>
31#include <linux/hwmon-sysfs.h>
32
Ben Skeggs330c5982010-09-16 15:39:49 +100033static int
Ben Skeggs6f876982010-09-16 16:47:14 +100034nouveau_pm_clock_set(struct drm_device *dev, u8 id, u32 khz)
35{
36 struct drm_nouveau_private *dev_priv = dev->dev_private;
37 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
38 void *pre_state;
39
40 if (khz == 0)
41 return 0;
42
43 pre_state = pm->clock_pre(dev, id, khz);
44 if (IS_ERR(pre_state))
45 return PTR_ERR(pre_state);
46
47 if (pre_state)
48 pm->clock_set(dev, pre_state);
49 return 0;
50}
51
52static int
Ben Skeggs64f1c112010-09-17 13:35:25 +100053nouveau_pm_perflvl_set(struct drm_device *dev, struct nouveau_pm_level *perflvl)
54{
55 struct drm_nouveau_private *dev_priv = dev->dev_private;
56 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
57 int ret;
58
59 if (perflvl == pm->cur)
60 return 0;
61
62 if (pm->voltage.supported && pm->voltage_set && perflvl->voltage) {
63 ret = pm->voltage_set(dev, perflvl->voltage);
64 if (ret) {
65 NV_ERROR(dev, "voltage_set %d failed: %d\n",
66 perflvl->voltage, ret);
67 }
68 }
69
70 nouveau_pm_clock_set(dev, PLL_CORE, perflvl->core);
71 nouveau_pm_clock_set(dev, PLL_SHADER, perflvl->shader);
72 nouveau_pm_clock_set(dev, PLL_MEMORY, perflvl->memory);
73 nouveau_pm_clock_set(dev, PLL_UNK05, perflvl->unk05);
74
75 pm->cur = perflvl;
76 return 0;
77}
78
79static int
Ben Skeggs6f876982010-09-16 16:47:14 +100080nouveau_pm_profile_set(struct drm_device *dev, const char *profile)
81{
82 struct drm_nouveau_private *dev_priv = dev->dev_private;
83 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
84 struct nouveau_pm_level *perflvl = NULL;
Ben Skeggs6f876982010-09-16 16:47:14 +100085
86 /* safety precaution, for now */
87 if (nouveau_perflvl_wr != 7777)
88 return -EPERM;
89
90 if (!pm->clock_set)
91 return -EINVAL;
92
93 if (!strncmp(profile, "boot", 4))
94 perflvl = &pm->boot;
95 else {
96 int pl = simple_strtol(profile, NULL, 10);
97 int i;
98
99 for (i = 0; i < pm->nr_perflvl; i++) {
100 if (pm->perflvl[i].id == pl) {
101 perflvl = &pm->perflvl[i];
102 break;
103 }
104 }
105
106 if (!perflvl)
107 return -EINVAL;
108 }
109
Ben Skeggs6f876982010-09-16 16:47:14 +1000110 NV_INFO(dev, "setting performance level: %s\n", profile);
Ben Skeggs64f1c112010-09-17 13:35:25 +1000111 return nouveau_pm_perflvl_set(dev, perflvl);
Ben Skeggs6f876982010-09-16 16:47:14 +1000112}
113
114static int
Ben Skeggs330c5982010-09-16 15:39:49 +1000115nouveau_pm_perflvl_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
116{
117 struct drm_nouveau_private *dev_priv = dev->dev_private;
118 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
119 int ret;
120
121 if (!pm->clock_get)
122 return -EINVAL;
123
124 memset(perflvl, 0, sizeof(*perflvl));
125
126 ret = pm->clock_get(dev, PLL_CORE);
127 if (ret > 0)
128 perflvl->core = ret;
129
130 ret = pm->clock_get(dev, PLL_MEMORY);
131 if (ret > 0)
132 perflvl->memory = ret;
133
134 ret = pm->clock_get(dev, PLL_SHADER);
135 if (ret > 0)
136 perflvl->shader = ret;
137
138 ret = pm->clock_get(dev, PLL_UNK05);
139 if (ret > 0)
140 perflvl->unk05 = ret;
141
142 if (pm->voltage.supported && pm->voltage_get) {
143 ret = pm->voltage_get(dev);
144 if (ret > 0)
145 perflvl->voltage = ret;
146 }
147
148 return 0;
149}
150
151static void
152nouveau_pm_perflvl_info(struct nouveau_pm_level *perflvl, char *ptr, int len)
153{
Francisco Jerez0fbb1142010-09-20 16:18:28 +0200154 char c[16], s[16], v[16], f[16];
155
156 c[0] = '\0';
157 if (perflvl->core)
158 snprintf(c, sizeof(c), " core %dMHz", perflvl->core / 1000);
Ben Skeggs330c5982010-09-16 15:39:49 +1000159
160 s[0] = '\0';
161 if (perflvl->shader)
162 snprintf(s, sizeof(s), " shader %dMHz", perflvl->shader / 1000);
163
164 v[0] = '\0';
165 if (perflvl->voltage)
166 snprintf(v, sizeof(v), " voltage %dmV", perflvl->voltage * 10);
167
168 f[0] = '\0';
169 if (perflvl->fanspeed)
170 snprintf(f, sizeof(f), " fanspeed %d%%", perflvl->fanspeed);
171
Francisco Jerez0fbb1142010-09-20 16:18:28 +0200172 snprintf(ptr, len, "memory %dMHz%s%s%s%s\n", perflvl->memory / 1000,
173 c, s, v, f);
Ben Skeggs330c5982010-09-16 15:39:49 +1000174}
175
176static ssize_t
177nouveau_pm_get_perflvl_info(struct device *d,
178 struct device_attribute *a, char *buf)
179{
180 struct nouveau_pm_level *perflvl = (struct nouveau_pm_level *)a;
181 char *ptr = buf;
182 int len = PAGE_SIZE;
183
184 snprintf(ptr, len, "%d: ", perflvl->id);
185 ptr += strlen(buf);
186 len -= strlen(buf);
187
188 nouveau_pm_perflvl_info(perflvl, ptr, len);
189 return strlen(buf);
190}
191
192static ssize_t
193nouveau_pm_get_perflvl(struct device *d, struct device_attribute *a, char *buf)
194{
Martin Peres34e9d852010-09-22 20:54:22 +0200195 struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
Ben Skeggs330c5982010-09-16 15:39:49 +1000196 struct drm_nouveau_private *dev_priv = dev->dev_private;
197 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
198 struct nouveau_pm_level cur;
199 int len = PAGE_SIZE, ret;
200 char *ptr = buf;
201
202 if (!pm->cur)
203 snprintf(ptr, len, "setting: boot\n");
204 else if (pm->cur == &pm->boot)
205 snprintf(ptr, len, "setting: boot\nc: ");
206 else
207 snprintf(ptr, len, "setting: static %d\nc: ", pm->cur->id);
208 ptr += strlen(buf);
209 len -= strlen(buf);
210
211 ret = nouveau_pm_perflvl_get(dev, &cur);
212 if (ret == 0)
213 nouveau_pm_perflvl_info(&cur, ptr, len);
214 return strlen(buf);
215}
216
217static ssize_t
218nouveau_pm_set_perflvl(struct device *d, struct device_attribute *a,
219 const char *buf, size_t count)
220{
Martin Peres34e9d852010-09-22 20:54:22 +0200221 struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
Ben Skeggs6f876982010-09-16 16:47:14 +1000222 int ret;
223
224 ret = nouveau_pm_profile_set(dev, buf);
225 if (ret)
226 return ret;
227 return strlen(buf);
Ben Skeggs330c5982010-09-16 15:39:49 +1000228}
229
230DEVICE_ATTR(performance_level, S_IRUGO | S_IWUSR,
231 nouveau_pm_get_perflvl, nouveau_pm_set_perflvl);
232
Martin Peres34e9d852010-09-22 20:54:22 +0200233static int
234nouveau_sysfs_init(struct drm_device *dev)
Ben Skeggs330c5982010-09-16 15:39:49 +1000235{
236 struct drm_nouveau_private *dev_priv = dev->dev_private;
237 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
238 struct device *d = &dev->pdev->dev;
Ben Skeggs330c5982010-09-16 15:39:49 +1000239 int ret, i;
240
Ben Skeggs330c5982010-09-16 15:39:49 +1000241 ret = device_create_file(d, &dev_attr_performance_level);
242 if (ret)
243 return ret;
244
245 for (i = 0; i < pm->nr_perflvl; i++) {
246 struct nouveau_pm_level *perflvl = &pm->perflvl[i];
247
248 perflvl->dev_attr.attr.name = perflvl->name;
249 perflvl->dev_attr.attr.mode = S_IRUGO;
250 perflvl->dev_attr.show = nouveau_pm_get_perflvl_info;
251 perflvl->dev_attr.store = NULL;
252 sysfs_attr_init(&perflvl->dev_attr.attr);
253
254 ret = device_create_file(d, &perflvl->dev_attr);
255 if (ret) {
256 NV_ERROR(dev, "failed pervlvl %d sysfs: %d\n",
257 perflvl->id, i);
258 perflvl->dev_attr.attr.name = NULL;
259 nouveau_pm_fini(dev);
260 return ret;
261 }
262 }
263
264 return 0;
265}
266
Martin Peres34e9d852010-09-22 20:54:22 +0200267static void
268nouveau_sysfs_fini(struct drm_device *dev)
Ben Skeggs330c5982010-09-16 15:39:49 +1000269{
270 struct drm_nouveau_private *dev_priv = dev->dev_private;
271 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
272 struct device *d = &dev->pdev->dev;
273 int i;
274
275 device_remove_file(d, &dev_attr_performance_level);
276 for (i = 0; i < pm->nr_perflvl; i++) {
277 struct nouveau_pm_level *pl = &pm->perflvl[i];
278
279 if (!pl->dev_attr.attr.name)
280 break;
281
282 device_remove_file(d, &pl->dev_attr);
283 }
Martin Peres34e9d852010-09-22 20:54:22 +0200284}
285
286
287
288static ssize_t
289nouveau_hwmon_show_temp(struct device *d, struct device_attribute *a, char *buf)
290{
291 struct drm_device *dev = dev_get_drvdata(d);
Francisco Jerez8155cac2010-09-23 20:58:38 +0200292 struct drm_nouveau_private *dev_priv = dev->dev_private;
293 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
Martin Peres34e9d852010-09-22 20:54:22 +0200294
Francisco Jerez8155cac2010-09-23 20:58:38 +0200295 return snprintf(buf, PAGE_SIZE, "%d\n", pm->temp_get(dev)*1000);
Martin Peres34e9d852010-09-22 20:54:22 +0200296}
297static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, nouveau_hwmon_show_temp,
298 NULL, 0);
299
300static ssize_t
301nouveau_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}
310static ssize_t
311nouveau_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_strtoul(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}
329static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, nouveau_hwmon_max_temp,
330 nouveau_hwmon_set_max_temp,
331 0);
332
333static ssize_t
334nouveau_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}
344static ssize_t
345nouveau_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_strtoul(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}
364static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
365 nouveau_hwmon_critical_temp,
366 nouveau_hwmon_set_critical_temp,
367 0);
368
369static 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}
375static SENSOR_DEVICE_ATTR(name, S_IRUGO, nouveau_hwmon_show_name, NULL, 0);
376
377static 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}
383static SENSOR_DEVICE_ATTR(update_rate, S_IRUGO,
384 nouveau_hwmon_show_update_rate,
385 NULL, 0);
386
387static 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
396static const struct attribute_group hwmon_attrgroup = {
397 .attrs = hwmon_attributes,
398};
399
400static int
401nouveau_hwmon_init(struct drm_device *dev)
402{
403 struct drm_nouveau_private *dev_priv = dev->dev_private;
Francisco Jerez8155cac2010-09-23 20:58:38 +0200404 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
Martin Peres34e9d852010-09-22 20:54:22 +0200405 struct device *hwmon_dev;
406 int ret;
407
Francisco Jerez8155cac2010-09-23 20:58:38 +0200408 if (!pm->temp_get)
409 return -ENODEV;
Martin Peres34e9d852010-09-22 20:54:22 +0200410
411 hwmon_dev = hwmon_device_register(&dev->pdev->dev);
412 if (IS_ERR(hwmon_dev)) {
413 ret = PTR_ERR(hwmon_dev);
414 NV_ERROR(dev,
415 "Unable to register hwmon device: %d\n", ret);
416 return ret;
417 }
418 dev_set_drvdata(hwmon_dev, dev);
419 ret = sysfs_create_group(&hwmon_dev->kobj,
420 &hwmon_attrgroup);
421 if (ret) {
422 NV_ERROR(dev,
423 "Unable to create hwmon sysfs file: %d\n", ret);
424 hwmon_device_unregister(hwmon_dev);
425 return ret;
426 }
427
Francisco Jerez8155cac2010-09-23 20:58:38 +0200428 pm->hwmon = hwmon_dev;
Martin Peres34e9d852010-09-22 20:54:22 +0200429
430 return 0;
431}
432
433static void
434nouveau_hwmon_fini(struct drm_device *dev)
435{
436 struct drm_nouveau_private *dev_priv = dev->dev_private;
Francisco Jerez8155cac2010-09-23 20:58:38 +0200437 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
Martin Peres34e9d852010-09-22 20:54:22 +0200438
Francisco Jerez8155cac2010-09-23 20:58:38 +0200439 if (pm->hwmon) {
440 sysfs_remove_group(&pm->hwmon->kobj, &hwmon_attrgroup);
441 hwmon_device_unregister(pm->hwmon);
Martin Peres34e9d852010-09-22 20:54:22 +0200442 }
443}
444
Martin Peres34e9d852010-09-22 20:54:22 +0200445int
446nouveau_pm_init(struct drm_device *dev)
447{
448 struct drm_nouveau_private *dev_priv = dev->dev_private;
449 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
450 char info[256];
451 int ret, i;
452
453 nouveau_volt_init(dev);
454 nouveau_perf_init(dev);
455 nouveau_temp_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);
Ben Skeggs330c5982010-09-16 15:39:49 +1000495
496 nouveau_perf_fini(dev);
497 nouveau_volt_fini(dev);
Martin Peres34e9d852010-09-22 20:54:22 +0200498 nouveau_temp_fini(dev);
499
500 nouveau_hwmon_fini(dev);
501 nouveau_sysfs_fini(dev);
Ben Skeggs330c5982010-09-16 15:39:49 +1000502}
503
Ben Skeggs64f1c112010-09-17 13:35:25 +1000504void
505nouveau_pm_resume(struct drm_device *dev)
506{
507 struct drm_nouveau_private *dev_priv = dev->dev_private;
508 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
509 struct nouveau_pm_level *perflvl;
510
511 if (pm->cur == &pm->boot)
512 return;
513
514 perflvl = pm->cur;
515 pm->cur = &pm->boot;
516 nouveau_pm_perflvl_set(dev, perflvl);
517}