blob: b1d3f4b26ebd61291bbf6fab7b04c16b701a3765 [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
Francisco Jerez5c4abd02010-09-23 20:36:42 +0200230static DEVICE_ATTR(performance_level, S_IRUGO | S_IWUSR,
231 nouveau_pm_get_perflvl, nouveau_pm_set_perflvl);
Ben Skeggs330c5982010-09-16 15:39:49 +1000232
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
Martin Peres34e9d852010-09-22 20:54:22 +0200286static ssize_t
287nouveau_hwmon_show_temp(struct device *d, struct device_attribute *a, char *buf)
288{
289 struct drm_device *dev = dev_get_drvdata(d);
Francisco Jerez8155cac2010-09-23 20:58:38 +0200290 struct drm_nouveau_private *dev_priv = dev->dev_private;
291 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
Martin Peres34e9d852010-09-22 20:54:22 +0200292
Francisco Jerez8155cac2010-09-23 20:58:38 +0200293 return snprintf(buf, PAGE_SIZE, "%d\n", pm->temp_get(dev)*1000);
Martin Peres34e9d852010-09-22 20:54:22 +0200294}
295static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, nouveau_hwmon_show_temp,
296 NULL, 0);
297
298static ssize_t
299nouveau_hwmon_max_temp(struct device *d, struct device_attribute *a, char *buf)
300{
301 struct drm_device *dev = dev_get_drvdata(d);
302 struct drm_nouveau_private *dev_priv = dev->dev_private;
303 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
304 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
305
306 return snprintf(buf, PAGE_SIZE, "%d\n", temp->down_clock*1000);
307}
308static ssize_t
309nouveau_hwmon_set_max_temp(struct device *d, struct device_attribute *a,
310 const char *buf, size_t count)
311{
312 struct drm_device *dev = dev_get_drvdata(d);
313 struct drm_nouveau_private *dev_priv = dev->dev_private;
314 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
315 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
316 long value;
317
Francisco Jerez5c4abd02010-09-23 20:36:42 +0200318 if (strict_strtol(buf, 10, &value) == -EINVAL)
Martin Peres34e9d852010-09-22 20:54:22 +0200319 return count;
320
321 temp->down_clock = value/1000;
322
323 nouveau_temp_safety_checks(dev);
324
325 return count;
326}
327static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, nouveau_hwmon_max_temp,
328 nouveau_hwmon_set_max_temp,
329 0);
330
331static ssize_t
332nouveau_hwmon_critical_temp(struct device *d, struct device_attribute *a,
333 char *buf)
334{
335 struct drm_device *dev = dev_get_drvdata(d);
336 struct drm_nouveau_private *dev_priv = dev->dev_private;
337 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
338 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
339
340 return snprintf(buf, PAGE_SIZE, "%d\n", temp->critical*1000);
341}
342static ssize_t
343nouveau_hwmon_set_critical_temp(struct device *d, struct device_attribute *a,
344 const char *buf,
345 size_t count)
346{
347 struct drm_device *dev = dev_get_drvdata(d);
348 struct drm_nouveau_private *dev_priv = dev->dev_private;
349 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
350 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
351 long value;
352
Francisco Jerez5c4abd02010-09-23 20:36:42 +0200353 if (strict_strtol(buf, 10, &value) == -EINVAL)
Martin Peres34e9d852010-09-22 20:54:22 +0200354 return count;
355
356 temp->critical = value/1000;
357
358 nouveau_temp_safety_checks(dev);
359
360 return count;
361}
362static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
363 nouveau_hwmon_critical_temp,
364 nouveau_hwmon_set_critical_temp,
365 0);
366
367static ssize_t nouveau_hwmon_show_name(struct device *dev,
368 struct device_attribute *attr,
369 char *buf)
370{
371 return sprintf(buf, "nouveau\n");
372}
373static SENSOR_DEVICE_ATTR(name, S_IRUGO, nouveau_hwmon_show_name, NULL, 0);
374
375static ssize_t nouveau_hwmon_show_update_rate(struct device *dev,
376 struct device_attribute *attr,
377 char *buf)
378{
379 return sprintf(buf, "1000\n");
380}
381static SENSOR_DEVICE_ATTR(update_rate, S_IRUGO,
382 nouveau_hwmon_show_update_rate,
383 NULL, 0);
384
385static struct attribute *hwmon_attributes[] = {
386 &sensor_dev_attr_temp1_input.dev_attr.attr,
387 &sensor_dev_attr_temp1_max.dev_attr.attr,
388 &sensor_dev_attr_temp1_crit.dev_attr.attr,
389 &sensor_dev_attr_name.dev_attr.attr,
390 &sensor_dev_attr_update_rate.dev_attr.attr,
391 NULL
392};
393
394static const struct attribute_group hwmon_attrgroup = {
395 .attrs = hwmon_attributes,
396};
397
398static int
399nouveau_hwmon_init(struct drm_device *dev)
400{
401 struct drm_nouveau_private *dev_priv = dev->dev_private;
Francisco Jerez8155cac2010-09-23 20:58:38 +0200402 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
Martin Peres34e9d852010-09-22 20:54:22 +0200403 struct device *hwmon_dev;
404 int ret;
405
Francisco Jerez8155cac2010-09-23 20:58:38 +0200406 if (!pm->temp_get)
407 return -ENODEV;
Martin Peres34e9d852010-09-22 20:54:22 +0200408
409 hwmon_dev = hwmon_device_register(&dev->pdev->dev);
410 if (IS_ERR(hwmon_dev)) {
411 ret = PTR_ERR(hwmon_dev);
412 NV_ERROR(dev,
413 "Unable to register hwmon device: %d\n", ret);
414 return ret;
415 }
416 dev_set_drvdata(hwmon_dev, dev);
417 ret = sysfs_create_group(&hwmon_dev->kobj,
418 &hwmon_attrgroup);
419 if (ret) {
420 NV_ERROR(dev,
421 "Unable to create hwmon sysfs file: %d\n", ret);
422 hwmon_device_unregister(hwmon_dev);
423 return ret;
424 }
425
Francisco Jerez8155cac2010-09-23 20:58:38 +0200426 pm->hwmon = hwmon_dev;
Martin Peres34e9d852010-09-22 20:54:22 +0200427
428 return 0;
429}
430
431static void
432nouveau_hwmon_fini(struct drm_device *dev)
433{
434 struct drm_nouveau_private *dev_priv = dev->dev_private;
Francisco Jerez8155cac2010-09-23 20:58:38 +0200435 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
Martin Peres34e9d852010-09-22 20:54:22 +0200436
Francisco Jerez8155cac2010-09-23 20:58:38 +0200437 if (pm->hwmon) {
438 sysfs_remove_group(&pm->hwmon->kobj, &hwmon_attrgroup);
439 hwmon_device_unregister(pm->hwmon);
Martin Peres34e9d852010-09-22 20:54:22 +0200440 }
441}
442
Martin Peres34e9d852010-09-22 20:54:22 +0200443int
444nouveau_pm_init(struct drm_device *dev)
445{
446 struct drm_nouveau_private *dev_priv = dev->dev_private;
447 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
448 char info[256];
449 int ret, i;
450
451 nouveau_volt_init(dev);
452 nouveau_perf_init(dev);
453 nouveau_temp_init(dev);
454
455 NV_INFO(dev, "%d available performance level(s)\n", pm->nr_perflvl);
456 for (i = 0; i < pm->nr_perflvl; i++) {
457 nouveau_pm_perflvl_info(&pm->perflvl[i], info, sizeof(info));
458 NV_INFO(dev, "%d: %s", pm->perflvl[i].id, info);
459 }
460
461 /* determine current ("boot") performance level */
462 ret = nouveau_pm_perflvl_get(dev, &pm->boot);
463 if (ret == 0) {
464 pm->cur = &pm->boot;
465
466 nouveau_pm_perflvl_info(&pm->boot, info, sizeof(info));
467 NV_INFO(dev, "c: %s", info);
468 }
469
470 /* switch performance levels now if requested */
471 if (nouveau_perflvl != NULL) {
472 ret = nouveau_pm_profile_set(dev, nouveau_perflvl);
473 if (ret) {
474 NV_ERROR(dev, "error setting perflvl \"%s\": %d\n",
475 nouveau_perflvl, ret);
476 }
477 }
478
479 nouveau_sysfs_init(dev);
480 nouveau_hwmon_init(dev);
481
482 return 0;
483}
484
485void
486nouveau_pm_fini(struct drm_device *dev)
487{
488 struct drm_nouveau_private *dev_priv = dev->dev_private;
489 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
490
491 if (pm->cur != &pm->boot)
492 nouveau_pm_perflvl_set(dev, &pm->boot);
Ben Skeggs330c5982010-09-16 15:39:49 +1000493
494 nouveau_perf_fini(dev);
495 nouveau_volt_fini(dev);
Martin Peres34e9d852010-09-22 20:54:22 +0200496 nouveau_temp_fini(dev);
497
498 nouveau_hwmon_fini(dev);
499 nouveau_sysfs_fini(dev);
Ben Skeggs330c5982010-09-16 15:39:49 +1000500}
501
Ben Skeggs64f1c112010-09-17 13:35:25 +1000502void
503nouveau_pm_resume(struct drm_device *dev)
504{
505 struct drm_nouveau_private *dev_priv = dev->dev_private;
506 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
507 struct nouveau_pm_level *perflvl;
508
509 if (pm->cur == &pm->boot)
510 return;
511
512 perflvl = pm->cur;
513 pm->cur = &pm->boot;
514 nouveau_pm_perflvl_set(dev, perflvl);
515}