2 * Copyright 2010 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
27 #include "nouveau_drv.h"
28 #include "nouveau_pm.h"
30 #include <linux/hwmon.h>
31 #include <linux/hwmon-sysfs.h>
34 nouveau_pm_clock_set(struct drm_device *dev, struct nouveau_pm_level *perflvl,
37 struct drm_nouveau_private *dev_priv = dev->dev_private;
38 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
44 pre_state = pm->clock_pre(dev, perflvl, id, khz);
45 if (IS_ERR(pre_state))
46 return PTR_ERR(pre_state);
49 pm->clock_set(dev, pre_state);
54 nouveau_pm_perflvl_set(struct drm_device *dev, struct nouveau_pm_level *perflvl)
56 struct drm_nouveau_private *dev_priv = dev->dev_private;
57 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
60 if (perflvl == pm->cur)
63 if (pm->voltage.supported && pm->voltage_set && perflvl->voltage) {
64 ret = pm->voltage_set(dev, perflvl->voltage);
66 NV_ERROR(dev, "voltage_set %d failed: %d\n",
67 perflvl->voltage, ret);
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);
81 nouveau_pm_profile_set(struct drm_device *dev, const char *profile)
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;
87 /* safety precaution, for now */
88 if (nouveau_perflvl_wr != 7777)
94 if (!strncmp(profile, "boot", 4))
97 int pl = simple_strtol(profile, NULL, 10);
100 for (i = 0; i < pm->nr_perflvl; i++) {
101 if (pm->perflvl[i].id == pl) {
102 perflvl = &pm->perflvl[i];
111 NV_INFO(dev, "setting performance level: %s\n", profile);
112 return nouveau_pm_perflvl_set(dev, perflvl);
116 nouveau_pm_perflvl_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
118 struct drm_nouveau_private *dev_priv = dev->dev_private;
119 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
125 memset(perflvl, 0, sizeof(*perflvl));
127 ret = pm->clock_get(dev, PLL_CORE);
131 ret = pm->clock_get(dev, PLL_MEMORY);
133 perflvl->memory = ret;
135 ret = pm->clock_get(dev, PLL_SHADER);
137 perflvl->shader = ret;
139 ret = pm->clock_get(dev, PLL_UNK05);
141 perflvl->unk05 = ret;
143 if (pm->voltage.supported && pm->voltage_get) {
144 ret = pm->voltage_get(dev);
146 perflvl->voltage = ret;
153 nouveau_pm_perflvl_info(struct nouveau_pm_level *perflvl, char *ptr, int len)
155 char c[16], s[16], v[16], f[16];
159 snprintf(c, sizeof(c), " core %dMHz", perflvl->core / 1000);
163 snprintf(s, sizeof(s), " shader %dMHz", perflvl->shader / 1000);
166 if (perflvl->voltage)
167 snprintf(v, sizeof(v), " voltage %dmV", perflvl->voltage * 10);
170 if (perflvl->fanspeed)
171 snprintf(f, sizeof(f), " fanspeed %d%%", perflvl->fanspeed);
173 snprintf(ptr, len, "memory %dMHz%s%s%s%s\n", perflvl->memory / 1000,
178 nouveau_pm_get_perflvl_info(struct device *d,
179 struct device_attribute *a, char *buf)
181 struct nouveau_pm_level *perflvl = (struct nouveau_pm_level *)a;
185 snprintf(ptr, len, "%d: ", perflvl->id);
189 nouveau_pm_perflvl_info(perflvl, ptr, len);
194 nouveau_pm_get_perflvl(struct device *d, struct device_attribute *a, char *buf)
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;
204 snprintf(ptr, len, "setting: boot\n");
205 else if (pm->cur == &pm->boot)
206 snprintf(ptr, len, "setting: boot\nc: ");
208 snprintf(ptr, len, "setting: static %d\nc: ", pm->cur->id);
212 ret = nouveau_pm_perflvl_get(dev, &cur);
214 nouveau_pm_perflvl_info(&cur, ptr, len);
219 nouveau_pm_set_perflvl(struct device *d, struct device_attribute *a,
220 const char *buf, size_t count)
222 struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
225 ret = nouveau_pm_profile_set(dev, buf);
231 static DEVICE_ATTR(performance_level, S_IRUGO | S_IWUSR,
232 nouveau_pm_get_perflvl, nouveau_pm_set_perflvl);
235 nouveau_sysfs_init(struct drm_device *dev)
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;
242 ret = device_create_file(d, &dev_attr_performance_level);
246 for (i = 0; i < pm->nr_perflvl; i++) {
247 struct nouveau_pm_level *perflvl = &pm->perflvl[i];
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);
255 ret = device_create_file(d, &perflvl->dev_attr);
257 NV_ERROR(dev, "failed pervlvl %d sysfs: %d\n",
259 perflvl->dev_attr.attr.name = NULL;
260 nouveau_pm_fini(dev);
269 nouveau_sysfs_fini(struct drm_device *dev)
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;
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];
280 if (!pl->dev_attr.attr.name)
283 device_remove_file(d, &pl->dev_attr);
289 nouveau_hwmon_show_temp(struct device *d, struct device_attribute *a, char *buf)
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;
295 return snprintf(buf, PAGE_SIZE, "%d\n", pm->temp_get(dev)*1000);
297 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, nouveau_hwmon_show_temp,
301 nouveau_hwmon_max_temp(struct device *d, struct device_attribute *a, char *buf)
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;
308 return snprintf(buf, PAGE_SIZE, "%d\n", temp->down_clock*1000);
311 nouveau_hwmon_set_max_temp(struct device *d, struct device_attribute *a,
312 const char *buf, size_t count)
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;
320 if (strict_strtol(buf, 10, &value) == -EINVAL)
323 temp->down_clock = value/1000;
325 nouveau_temp_safety_checks(dev);
329 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, nouveau_hwmon_max_temp,
330 nouveau_hwmon_set_max_temp,
334 nouveau_hwmon_critical_temp(struct device *d, struct device_attribute *a,
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;
342 return snprintf(buf, PAGE_SIZE, "%d\n", temp->critical*1000);
345 nouveau_hwmon_set_critical_temp(struct device *d, struct device_attribute *a,
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;
355 if (strict_strtol(buf, 10, &value) == -EINVAL)
358 temp->critical = value/1000;
360 nouveau_temp_safety_checks(dev);
364 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
365 nouveau_hwmon_critical_temp,
366 nouveau_hwmon_set_critical_temp,
369 static ssize_t nouveau_hwmon_show_name(struct device *dev,
370 struct device_attribute *attr,
373 return sprintf(buf, "nouveau\n");
375 static SENSOR_DEVICE_ATTR(name, S_IRUGO, nouveau_hwmon_show_name, NULL, 0);
377 static ssize_t nouveau_hwmon_show_update_rate(struct device *dev,
378 struct device_attribute *attr,
381 return sprintf(buf, "1000\n");
383 static SENSOR_DEVICE_ATTR(update_rate, S_IRUGO,
384 nouveau_hwmon_show_update_rate,
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,
396 static const struct attribute_group hwmon_attrgroup = {
397 .attrs = hwmon_attributes,
402 nouveau_hwmon_init(struct drm_device *dev)
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;
413 hwmon_dev = hwmon_device_register(&dev->pdev->dev);
414 if (IS_ERR(hwmon_dev)) {
415 ret = PTR_ERR(hwmon_dev);
417 "Unable to register hwmon device: %d\n", ret);
420 dev_set_drvdata(hwmon_dev, dev);
421 ret = sysfs_create_group(&hwmon_dev->kobj,
425 "Unable to create hwmon sysfs file: %d\n", ret);
426 hwmon_device_unregister(hwmon_dev);
430 pm->hwmon = hwmon_dev;
436 nouveau_hwmon_fini(struct drm_device *dev)
439 struct drm_nouveau_private *dev_priv = dev->dev_private;
440 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
443 sysfs_remove_group(&pm->hwmon->kobj, &hwmon_attrgroup);
444 hwmon_device_unregister(pm->hwmon);
450 nouveau_pm_init(struct drm_device *dev)
452 struct drm_nouveau_private *dev_priv = dev->dev_private;
453 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
457 nouveau_volt_init(dev);
458 nouveau_perf_init(dev);
459 nouveau_temp_init(dev);
460 nouveau_mem_timing_init(dev);
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);
468 /* determine current ("boot") performance level */
469 ret = nouveau_pm_perflvl_get(dev, &pm->boot);
473 nouveau_pm_perflvl_info(&pm->boot, info, sizeof(info));
474 NV_INFO(dev, "c: %s", info);
477 /* switch performance levels now if requested */
478 if (nouveau_perflvl != NULL) {
479 ret = nouveau_pm_profile_set(dev, nouveau_perflvl);
481 NV_ERROR(dev, "error setting perflvl \"%s\": %d\n",
482 nouveau_perflvl, ret);
486 nouveau_sysfs_init(dev);
487 nouveau_hwmon_init(dev);
493 nouveau_pm_fini(struct drm_device *dev)
495 struct drm_nouveau_private *dev_priv = dev->dev_private;
496 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
498 if (pm->cur != &pm->boot)
499 nouveau_pm_perflvl_set(dev, &pm->boot);
501 nouveau_mem_timing_fini(dev);
502 nouveau_temp_fini(dev);
503 nouveau_perf_fini(dev);
504 nouveau_volt_fini(dev);
506 nouveau_hwmon_fini(dev);
507 nouveau_sysfs_fini(dev);
511 nouveau_pm_resume(struct drm_device *dev)
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;
517 if (pm->cur == &pm->boot)
522 nouveau_pm_perflvl_set(dev, perflvl);