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);
288 nouveau_hwmon_show_temp(struct device *d, struct device_attribute *a, char *buf)
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;
294 return snprintf(buf, PAGE_SIZE, "%d\n", pm->temp_get(dev)*1000);
296 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, nouveau_hwmon_show_temp,
300 nouveau_hwmon_max_temp(struct device *d, struct device_attribute *a, char *buf)
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;
307 return snprintf(buf, PAGE_SIZE, "%d\n", temp->down_clock*1000);
310 nouveau_hwmon_set_max_temp(struct device *d, struct device_attribute *a,
311 const char *buf, size_t count)
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;
319 if (strict_strtol(buf, 10, &value) == -EINVAL)
322 temp->down_clock = value/1000;
324 nouveau_temp_safety_checks(dev);
328 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, nouveau_hwmon_max_temp,
329 nouveau_hwmon_set_max_temp,
333 nouveau_hwmon_critical_temp(struct device *d, struct device_attribute *a,
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;
341 return snprintf(buf, PAGE_SIZE, "%d\n", temp->critical*1000);
344 nouveau_hwmon_set_critical_temp(struct device *d, struct device_attribute *a,
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;
354 if (strict_strtol(buf, 10, &value) == -EINVAL)
357 temp->critical = value/1000;
359 nouveau_temp_safety_checks(dev);
363 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
364 nouveau_hwmon_critical_temp,
365 nouveau_hwmon_set_critical_temp,
368 static ssize_t nouveau_hwmon_show_name(struct device *dev,
369 struct device_attribute *attr,
372 return sprintf(buf, "nouveau\n");
374 static SENSOR_DEVICE_ATTR(name, S_IRUGO, nouveau_hwmon_show_name, NULL, 0);
376 static ssize_t nouveau_hwmon_show_update_rate(struct device *dev,
377 struct device_attribute *attr,
380 return sprintf(buf, "1000\n");
382 static SENSOR_DEVICE_ATTR(update_rate, S_IRUGO,
383 nouveau_hwmon_show_update_rate,
386 static 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,
395 static const struct attribute_group hwmon_attrgroup = {
396 .attrs = hwmon_attributes,
400 nouveau_hwmon_init(struct drm_device *dev)
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;
410 hwmon_dev = hwmon_device_register(&dev->pdev->dev);
411 if (IS_ERR(hwmon_dev)) {
412 ret = PTR_ERR(hwmon_dev);
414 "Unable to register hwmon device: %d\n", ret);
417 dev_set_drvdata(hwmon_dev, dev);
418 ret = sysfs_create_group(&hwmon_dev->kobj,
422 "Unable to create hwmon sysfs file: %d\n", ret);
423 hwmon_device_unregister(hwmon_dev);
427 pm->hwmon = hwmon_dev;
433 nouveau_hwmon_fini(struct drm_device *dev)
435 struct drm_nouveau_private *dev_priv = dev->dev_private;
436 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
439 sysfs_remove_group(&pm->hwmon->kobj, &hwmon_attrgroup);
440 hwmon_device_unregister(pm->hwmon);
445 nouveau_pm_init(struct drm_device *dev)
447 struct drm_nouveau_private *dev_priv = dev->dev_private;
448 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
452 nouveau_volt_init(dev);
453 nouveau_perf_init(dev);
454 nouveau_temp_init(dev);
455 nouveau_mem_timing_init(dev);
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);
463 /* determine current ("boot") performance level */
464 ret = nouveau_pm_perflvl_get(dev, &pm->boot);
468 nouveau_pm_perflvl_info(&pm->boot, info, sizeof(info));
469 NV_INFO(dev, "c: %s", info);
472 /* switch performance levels now if requested */
473 if (nouveau_perflvl != NULL) {
474 ret = nouveau_pm_profile_set(dev, nouveau_perflvl);
476 NV_ERROR(dev, "error setting perflvl \"%s\": %d\n",
477 nouveau_perflvl, ret);
481 nouveau_sysfs_init(dev);
482 nouveau_hwmon_init(dev);
488 nouveau_pm_fini(struct drm_device *dev)
490 struct drm_nouveau_private *dev_priv = dev->dev_private;
491 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
493 if (pm->cur != &pm->boot)
494 nouveau_pm_perflvl_set(dev, &pm->boot);
496 nouveau_mem_timing_fini(dev);
497 nouveau_temp_fini(dev);
498 nouveau_perf_fini(dev);
499 nouveau_volt_fini(dev);
501 nouveau_hwmon_fini(dev);
502 nouveau_sysfs_fini(dev);
506 nouveau_pm_resume(struct drm_device *dev)
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;
512 if (pm->cur == &pm->boot)
517 nouveau_pm_perflvl_set(dev, perflvl);