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"
31 #include <linux/acpi.h>
33 #include <linux/power_supply.h>
34 #include <linux/hwmon.h>
35 #include <linux/hwmon-sysfs.h>
38 nouveau_pm_clock_set(struct drm_device *dev, struct nouveau_pm_level *perflvl,
41 struct drm_nouveau_private *dev_priv = dev->dev_private;
42 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
48 pre_state = pm->clock_pre(dev, perflvl, id, khz);
49 if (IS_ERR(pre_state))
50 return PTR_ERR(pre_state);
53 pm->clock_set(dev, pre_state);
58 nouveau_pm_perflvl_set(struct drm_device *dev, struct nouveau_pm_level *perflvl)
60 struct drm_nouveau_private *dev_priv = dev->dev_private;
61 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
64 if (perflvl == pm->cur)
67 if (pm->voltage.supported && pm->voltage_set && perflvl->voltage) {
68 ret = pm->voltage_set(dev, perflvl->voltage);
70 NV_ERROR(dev, "voltage_set %d failed: %d\n",
71 perflvl->voltage, ret);
75 nouveau_pm_clock_set(dev, perflvl, PLL_CORE, perflvl->core);
76 nouveau_pm_clock_set(dev, perflvl, PLL_SHADER, perflvl->shader);
77 nouveau_pm_clock_set(dev, perflvl, PLL_MEMORY, perflvl->memory);
78 nouveau_pm_clock_set(dev, perflvl, PLL_UNK05, perflvl->unk05);
85 nouveau_pm_profile_set(struct drm_device *dev, const char *profile)
87 struct drm_nouveau_private *dev_priv = dev->dev_private;
88 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
89 struct nouveau_pm_level *perflvl = NULL;
91 /* safety precaution, for now */
92 if (nouveau_perflvl_wr != 7777)
98 if (!strncmp(profile, "boot", 4))
101 int pl = simple_strtol(profile, NULL, 10);
104 for (i = 0; i < pm->nr_perflvl; i++) {
105 if (pm->perflvl[i].id == pl) {
106 perflvl = &pm->perflvl[i];
115 NV_INFO(dev, "setting performance level: %s\n", profile);
116 return nouveau_pm_perflvl_set(dev, perflvl);
120 nouveau_pm_perflvl_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
122 struct drm_nouveau_private *dev_priv = dev->dev_private;
123 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
129 memset(perflvl, 0, sizeof(*perflvl));
131 ret = pm->clock_get(dev, PLL_CORE);
135 ret = pm->clock_get(dev, PLL_MEMORY);
137 perflvl->memory = ret;
139 ret = pm->clock_get(dev, PLL_SHADER);
141 perflvl->shader = ret;
143 ret = pm->clock_get(dev, PLL_UNK05);
145 perflvl->unk05 = ret;
147 if (pm->voltage.supported && pm->voltage_get) {
148 ret = pm->voltage_get(dev);
150 perflvl->voltage = ret;
157 nouveau_pm_perflvl_info(struct nouveau_pm_level *perflvl, char *ptr, int len)
159 char c[16], s[16], v[16], f[16], t[16];
163 snprintf(c, sizeof(c), " core %dMHz", perflvl->core / 1000);
167 snprintf(s, sizeof(s), " shader %dMHz", perflvl->shader / 1000);
170 if (perflvl->voltage)
171 snprintf(v, sizeof(v), " voltage %dmV", perflvl->voltage * 10);
174 if (perflvl->fanspeed)
175 snprintf(f, sizeof(f), " fanspeed %d%%", perflvl->fanspeed);
179 snprintf(t, sizeof(t), " timing %d", perflvl->timing->id);
181 snprintf(ptr, len, "memory %dMHz%s%s%s%s%s\n", perflvl->memory / 1000,
186 nouveau_pm_get_perflvl_info(struct device *d,
187 struct device_attribute *a, char *buf)
189 struct nouveau_pm_level *perflvl = (struct nouveau_pm_level *)a;
193 snprintf(ptr, len, "%d: ", perflvl->id);
197 nouveau_pm_perflvl_info(perflvl, ptr, len);
202 nouveau_pm_get_perflvl(struct device *d, struct device_attribute *a, char *buf)
204 struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
205 struct drm_nouveau_private *dev_priv = dev->dev_private;
206 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
207 struct nouveau_pm_level cur;
208 int len = PAGE_SIZE, ret;
212 snprintf(ptr, len, "setting: boot\n");
213 else if (pm->cur == &pm->boot)
214 snprintf(ptr, len, "setting: boot\nc: ");
216 snprintf(ptr, len, "setting: static %d\nc: ", pm->cur->id);
220 ret = nouveau_pm_perflvl_get(dev, &cur);
222 nouveau_pm_perflvl_info(&cur, ptr, len);
227 nouveau_pm_set_perflvl(struct device *d, struct device_attribute *a,
228 const char *buf, size_t count)
230 struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
233 ret = nouveau_pm_profile_set(dev, buf);
239 static DEVICE_ATTR(performance_level, S_IRUGO | S_IWUSR,
240 nouveau_pm_get_perflvl, nouveau_pm_set_perflvl);
243 nouveau_sysfs_init(struct drm_device *dev)
245 struct drm_nouveau_private *dev_priv = dev->dev_private;
246 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
247 struct device *d = &dev->pdev->dev;
250 ret = device_create_file(d, &dev_attr_performance_level);
254 for (i = 0; i < pm->nr_perflvl; i++) {
255 struct nouveau_pm_level *perflvl = &pm->perflvl[i];
257 perflvl->dev_attr.attr.name = perflvl->name;
258 perflvl->dev_attr.attr.mode = S_IRUGO;
259 perflvl->dev_attr.show = nouveau_pm_get_perflvl_info;
260 perflvl->dev_attr.store = NULL;
261 sysfs_attr_init(&perflvl->dev_attr.attr);
263 ret = device_create_file(d, &perflvl->dev_attr);
265 NV_ERROR(dev, "failed pervlvl %d sysfs: %d\n",
267 perflvl->dev_attr.attr.name = NULL;
268 nouveau_pm_fini(dev);
277 nouveau_sysfs_fini(struct drm_device *dev)
279 struct drm_nouveau_private *dev_priv = dev->dev_private;
280 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
281 struct device *d = &dev->pdev->dev;
284 device_remove_file(d, &dev_attr_performance_level);
285 for (i = 0; i < pm->nr_perflvl; i++) {
286 struct nouveau_pm_level *pl = &pm->perflvl[i];
288 if (!pl->dev_attr.attr.name)
291 device_remove_file(d, &pl->dev_attr);
297 nouveau_hwmon_show_temp(struct device *d, struct device_attribute *a, char *buf)
299 struct drm_device *dev = dev_get_drvdata(d);
300 struct drm_nouveau_private *dev_priv = dev->dev_private;
301 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
303 return snprintf(buf, PAGE_SIZE, "%d\n", pm->temp_get(dev)*1000);
305 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, nouveau_hwmon_show_temp,
309 nouveau_hwmon_max_temp(struct device *d, struct device_attribute *a, char *buf)
311 struct drm_device *dev = dev_get_drvdata(d);
312 struct drm_nouveau_private *dev_priv = dev->dev_private;
313 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
314 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
316 return snprintf(buf, PAGE_SIZE, "%d\n", temp->down_clock*1000);
319 nouveau_hwmon_set_max_temp(struct device *d, struct device_attribute *a,
320 const char *buf, size_t count)
322 struct drm_device *dev = dev_get_drvdata(d);
323 struct drm_nouveau_private *dev_priv = dev->dev_private;
324 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
325 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
328 if (strict_strtol(buf, 10, &value) == -EINVAL)
331 temp->down_clock = value/1000;
333 nouveau_temp_safety_checks(dev);
337 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, nouveau_hwmon_max_temp,
338 nouveau_hwmon_set_max_temp,
342 nouveau_hwmon_critical_temp(struct device *d, struct device_attribute *a,
345 struct drm_device *dev = dev_get_drvdata(d);
346 struct drm_nouveau_private *dev_priv = dev->dev_private;
347 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
348 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
350 return snprintf(buf, PAGE_SIZE, "%d\n", temp->critical*1000);
353 nouveau_hwmon_set_critical_temp(struct device *d, struct device_attribute *a,
357 struct drm_device *dev = dev_get_drvdata(d);
358 struct drm_nouveau_private *dev_priv = dev->dev_private;
359 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
360 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
363 if (strict_strtol(buf, 10, &value) == -EINVAL)
366 temp->critical = value/1000;
368 nouveau_temp_safety_checks(dev);
372 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
373 nouveau_hwmon_critical_temp,
374 nouveau_hwmon_set_critical_temp,
377 static ssize_t nouveau_hwmon_show_name(struct device *dev,
378 struct device_attribute *attr,
381 return sprintf(buf, "nouveau\n");
383 static SENSOR_DEVICE_ATTR(name, S_IRUGO, nouveau_hwmon_show_name, NULL, 0);
385 static ssize_t nouveau_hwmon_show_update_rate(struct device *dev,
386 struct device_attribute *attr,
389 return sprintf(buf, "1000\n");
391 static SENSOR_DEVICE_ATTR(update_rate, S_IRUGO,
392 nouveau_hwmon_show_update_rate,
395 static struct attribute *hwmon_attributes[] = {
396 &sensor_dev_attr_temp1_input.dev_attr.attr,
397 &sensor_dev_attr_temp1_max.dev_attr.attr,
398 &sensor_dev_attr_temp1_crit.dev_attr.attr,
399 &sensor_dev_attr_name.dev_attr.attr,
400 &sensor_dev_attr_update_rate.dev_attr.attr,
404 static const struct attribute_group hwmon_attrgroup = {
405 .attrs = hwmon_attributes,
410 nouveau_hwmon_init(struct drm_device *dev)
413 struct drm_nouveau_private *dev_priv = dev->dev_private;
414 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
415 struct device *hwmon_dev;
421 hwmon_dev = hwmon_device_register(&dev->pdev->dev);
422 if (IS_ERR(hwmon_dev)) {
423 ret = PTR_ERR(hwmon_dev);
425 "Unable to register hwmon device: %d\n", ret);
428 dev_set_drvdata(hwmon_dev, dev);
429 ret = sysfs_create_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
432 "Unable to create hwmon sysfs file: %d\n", ret);
433 hwmon_device_unregister(hwmon_dev);
437 pm->hwmon = hwmon_dev;
443 nouveau_hwmon_fini(struct drm_device *dev)
446 struct drm_nouveau_private *dev_priv = dev->dev_private;
447 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
450 sysfs_remove_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
451 hwmon_device_unregister(pm->hwmon);
456 #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
458 nouveau_pm_acpi_event(struct notifier_block *nb, unsigned long val, void *data)
460 struct drm_nouveau_private *dev_priv =
461 container_of(nb, struct drm_nouveau_private, engine.pm.acpi_nb);
462 struct drm_device *dev = dev_priv->dev;
463 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
465 if (strcmp(entry->device_class, "ac_adapter") == 0) {
466 bool ac = power_supply_is_system_supplied();
468 NV_DEBUG(dev, "power supply changed: %s\n", ac ? "AC" : "DC");
476 nouveau_pm_init(struct drm_device *dev)
478 struct drm_nouveau_private *dev_priv = dev->dev_private;
479 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
483 nouveau_mem_timing_init(dev);
484 nouveau_volt_init(dev);
485 nouveau_perf_init(dev);
486 nouveau_temp_init(dev);
488 NV_INFO(dev, "%d available performance level(s)\n", pm->nr_perflvl);
489 for (i = 0; i < pm->nr_perflvl; i++) {
490 nouveau_pm_perflvl_info(&pm->perflvl[i], info, sizeof(info));
491 NV_INFO(dev, "%d: %s", pm->perflvl[i].id, info);
494 /* determine current ("boot") performance level */
495 ret = nouveau_pm_perflvl_get(dev, &pm->boot);
497 strncpy(pm->boot.name, "boot", 4);
500 nouveau_pm_perflvl_info(&pm->boot, info, sizeof(info));
501 NV_INFO(dev, "c: %s", info);
504 /* switch performance levels now if requested */
505 if (nouveau_perflvl != NULL) {
506 ret = nouveau_pm_profile_set(dev, nouveau_perflvl);
508 NV_ERROR(dev, "error setting perflvl \"%s\": %d\n",
509 nouveau_perflvl, ret);
513 nouveau_sysfs_init(dev);
514 nouveau_hwmon_init(dev);
515 #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
516 pm->acpi_nb.notifier_call = nouveau_pm_acpi_event;
517 register_acpi_notifier(&pm->acpi_nb);
524 nouveau_pm_fini(struct drm_device *dev)
526 struct drm_nouveau_private *dev_priv = dev->dev_private;
527 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
529 if (pm->cur != &pm->boot)
530 nouveau_pm_perflvl_set(dev, &pm->boot);
532 nouveau_temp_fini(dev);
533 nouveau_perf_fini(dev);
534 nouveau_volt_fini(dev);
535 nouveau_mem_timing_fini(dev);
537 #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
538 unregister_acpi_notifier(&pm->acpi_nb);
540 nouveau_hwmon_fini(dev);
541 nouveau_sysfs_fini(dev);
545 nouveau_pm_resume(struct drm_device *dev)
547 struct drm_nouveau_private *dev_priv = dev->dev_private;
548 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
549 struct nouveau_pm_level *perflvl;
551 if (!pm->cur || pm->cur == &pm->boot)
556 nouveau_pm_perflvl_set(dev, perflvl);