blob: 9f7b158f5825c7dfd97b5f112afef7a9858e7b23 [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 Skeggs5c6dc652010-09-27 09:47:56 +100034nouveau_pm_clock_set(struct drm_device *dev, struct nouveau_pm_level *perflvl,
35 u8 id, u32 khz)
Ben Skeggs6f876982010-09-16 16:47:14 +100036{
37 struct drm_nouveau_private *dev_priv = dev->dev_private;
38 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
39 void *pre_state;
40
41 if (khz == 0)
42 return 0;
43
Ben Skeggs5c6dc652010-09-27 09:47:56 +100044 pre_state = pm->clock_pre(dev, perflvl, id, khz);
Ben Skeggs6f876982010-09-16 16:47:14 +100045 if (IS_ERR(pre_state))
46 return PTR_ERR(pre_state);
47
48 if (pre_state)
49 pm->clock_set(dev, pre_state);
50 return 0;
51}
52
53static int
Ben Skeggs64f1c112010-09-17 13:35:25 +100054nouveau_pm_perflvl_set(struct drm_device *dev, struct nouveau_pm_level *perflvl)
55{
56 struct drm_nouveau_private *dev_priv = dev->dev_private;
57 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
58 int ret;
59
60 if (perflvl == pm->cur)
61 return 0;
62
63 if (pm->voltage.supported && pm->voltage_set && perflvl->voltage) {
64 ret = pm->voltage_set(dev, perflvl->voltage);
65 if (ret) {
66 NV_ERROR(dev, "voltage_set %d failed: %d\n",
67 perflvl->voltage, ret);
68 }
69 }
70
Ben Skeggs5c6dc652010-09-27 09:47:56 +100071 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);
Ben Skeggs64f1c112010-09-17 13:35:25 +100075
76 pm->cur = perflvl;
77 return 0;
78}
79
80static int
Ben Skeggs6f876982010-09-16 16:47:14 +100081nouveau_pm_profile_set(struct drm_device *dev, const char *profile)
82{
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;
Ben Skeggs6f876982010-09-16 16:47:14 +100086
87 /* safety precaution, for now */
88 if (nouveau_perflvl_wr != 7777)
89 return -EPERM;
90
91 if (!pm->clock_set)
92 return -EINVAL;
93
94 if (!strncmp(profile, "boot", 4))
95 perflvl = &pm->boot;
96 else {
97 int pl = simple_strtol(profile, NULL, 10);
98 int i;
99
100 for (i = 0; i < pm->nr_perflvl; i++) {
101 if (pm->perflvl[i].id == pl) {
102 perflvl = &pm->perflvl[i];
103 break;
104 }
105 }
106
107 if (!perflvl)
108 return -EINVAL;
109 }
110
Ben Skeggs6f876982010-09-16 16:47:14 +1000111 NV_INFO(dev, "setting performance level: %s\n", profile);
Ben Skeggs64f1c112010-09-17 13:35:25 +1000112 return nouveau_pm_perflvl_set(dev, perflvl);
Ben Skeggs6f876982010-09-16 16:47:14 +1000113}
114
115static int
Ben Skeggs330c5982010-09-16 15:39:49 +1000116nouveau_pm_perflvl_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
117{
118 struct drm_nouveau_private *dev_priv = dev->dev_private;
119 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
120 int ret;
121
122 if (!pm->clock_get)
123 return -EINVAL;
124
125 memset(perflvl, 0, sizeof(*perflvl));
126
127 ret = pm->clock_get(dev, PLL_CORE);
128 if (ret > 0)
129 perflvl->core = ret;
130
131 ret = pm->clock_get(dev, PLL_MEMORY);
132 if (ret > 0)
133 perflvl->memory = ret;
134
135 ret = pm->clock_get(dev, PLL_SHADER);
136 if (ret > 0)
137 perflvl->shader = ret;
138
139 ret = pm->clock_get(dev, PLL_UNK05);
140 if (ret > 0)
141 perflvl->unk05 = ret;
142
143 if (pm->voltage.supported && pm->voltage_get) {
144 ret = pm->voltage_get(dev);
145 if (ret > 0)
146 perflvl->voltage = ret;
147 }
148
149 return 0;
150}
151
152static void
153nouveau_pm_perflvl_info(struct nouveau_pm_level *perflvl, char *ptr, int len)
154{
Francisco Jerez0fbb1142010-09-20 16:18:28 +0200155 char c[16], s[16], v[16], f[16];
156
157 c[0] = '\0';
158 if (perflvl->core)
159 snprintf(c, sizeof(c), " core %dMHz", perflvl->core / 1000);
Ben Skeggs330c5982010-09-16 15:39:49 +1000160
161 s[0] = '\0';
162 if (perflvl->shader)
163 snprintf(s, sizeof(s), " shader %dMHz", perflvl->shader / 1000);
164
165 v[0] = '\0';
166 if (perflvl->voltage)
167 snprintf(v, sizeof(v), " voltage %dmV", perflvl->voltage * 10);
168
169 f[0] = '\0';
170 if (perflvl->fanspeed)
171 snprintf(f, sizeof(f), " fanspeed %d%%", perflvl->fanspeed);
172
Francisco Jerez0fbb1142010-09-20 16:18:28 +0200173 snprintf(ptr, len, "memory %dMHz%s%s%s%s\n", perflvl->memory / 1000,
174 c, s, v, f);
Ben Skeggs330c5982010-09-16 15:39:49 +1000175}
176
177static ssize_t
178nouveau_pm_get_perflvl_info(struct device *d,
179 struct device_attribute *a, char *buf)
180{
181 struct nouveau_pm_level *perflvl = (struct nouveau_pm_level *)a;
182 char *ptr = buf;
183 int len = PAGE_SIZE;
184
185 snprintf(ptr, len, "%d: ", perflvl->id);
186 ptr += strlen(buf);
187 len -= strlen(buf);
188
189 nouveau_pm_perflvl_info(perflvl, ptr, len);
190 return strlen(buf);
191}
192
193static ssize_t
194nouveau_pm_get_perflvl(struct device *d, struct device_attribute *a, char *buf)
195{
Martin Peres34e9d852010-09-22 20:54:22 +0200196 struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
Ben Skeggs330c5982010-09-16 15:39:49 +1000197 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;
201 char *ptr = buf;
202
203 if (!pm->cur)
204 snprintf(ptr, len, "setting: boot\n");
205 else if (pm->cur == &pm->boot)
206 snprintf(ptr, len, "setting: boot\nc: ");
207 else
208 snprintf(ptr, len, "setting: static %d\nc: ", pm->cur->id);
209 ptr += strlen(buf);
210 len -= strlen(buf);
211
212 ret = nouveau_pm_perflvl_get(dev, &cur);
213 if (ret == 0)
214 nouveau_pm_perflvl_info(&cur, ptr, len);
215 return strlen(buf);
216}
217
218static ssize_t
219nouveau_pm_set_perflvl(struct device *d, struct device_attribute *a,
220 const char *buf, size_t count)
221{
Martin Peres34e9d852010-09-22 20:54:22 +0200222 struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
Ben Skeggs6f876982010-09-16 16:47:14 +1000223 int ret;
224
225 ret = nouveau_pm_profile_set(dev, buf);
226 if (ret)
227 return ret;
228 return strlen(buf);
Ben Skeggs330c5982010-09-16 15:39:49 +1000229}
230
Francisco Jerez5c4abd02010-09-23 20:36:42 +0200231static DEVICE_ATTR(performance_level, S_IRUGO | S_IWUSR,
232 nouveau_pm_get_perflvl, nouveau_pm_set_perflvl);
Ben Skeggs330c5982010-09-16 15:39:49 +1000233
Martin Peres34e9d852010-09-22 20:54:22 +0200234static int
235nouveau_sysfs_init(struct drm_device *dev)
Ben Skeggs330c5982010-09-16 15:39:49 +1000236{
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;
Ben Skeggs330c5982010-09-16 15:39:49 +1000240 int ret, i;
241
Ben Skeggs330c5982010-09-16 15:39:49 +1000242 ret = device_create_file(d, &dev_attr_performance_level);
243 if (ret)
244 return ret;
245
246 for (i = 0; i < pm->nr_perflvl; i++) {
247 struct nouveau_pm_level *perflvl = &pm->perflvl[i];
248
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);
254
255 ret = device_create_file(d, &perflvl->dev_attr);
256 if (ret) {
257 NV_ERROR(dev, "failed pervlvl %d sysfs: %d\n",
258 perflvl->id, i);
259 perflvl->dev_attr.attr.name = NULL;
260 nouveau_pm_fini(dev);
261 return ret;
262 }
263 }
264
265 return 0;
266}
267
Martin Peres34e9d852010-09-22 20:54:22 +0200268static void
269nouveau_sysfs_fini(struct drm_device *dev)
Ben Skeggs330c5982010-09-16 15:39:49 +1000270{
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;
274 int i;
275
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];
279
280 if (!pl->dev_attr.attr.name)
281 break;
282
283 device_remove_file(d, &pl->dev_attr);
284 }
Martin Peres34e9d852010-09-22 20:54:22 +0200285}
286
Martin Peresb54262f2010-10-26 12:48:28 +0200287#ifdef CONFIG_HWMON
Martin Peres34e9d852010-09-22 20:54:22 +0200288static 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
Francisco Jerez5c4abd02010-09-23 20:36:42 +0200320 if (strict_strtol(buf, 10, &value) == -EINVAL)
Martin Peres34e9d852010-09-22 20:54:22 +0200321 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
Francisco Jerez5c4abd02010-09-23 20:36:42 +0200355 if (strict_strtol(buf, 10, &value) == -EINVAL)
Martin Peres34e9d852010-09-22 20:54:22 +0200356 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};
Martin Peresb54262f2010-10-26 12:48:28 +0200399#endif
Martin Peres34e9d852010-09-22 20:54:22 +0200400
401static int
402nouveau_hwmon_init(struct drm_device *dev)
403{
Martin Peresb54262f2010-10-26 12:48:28 +0200404#ifdef CONFIG_HWMON
Martin Peres34e9d852010-09-22 20:54:22 +0200405 struct drm_nouveau_private *dev_priv = dev->dev_private;
Francisco Jerez8155cac2010-09-23 20:58:38 +0200406 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
Martin Peres34e9d852010-09-22 20:54:22 +0200407 struct device *hwmon_dev;
408 int ret;
409
Francisco Jerez8155cac2010-09-23 20:58:38 +0200410 if (!pm->temp_get)
411 return -ENODEV;
Martin Peres34e9d852010-09-22 20:54:22 +0200412
413 hwmon_dev = hwmon_device_register(&dev->pdev->dev);
414 if (IS_ERR(hwmon_dev)) {
415 ret = PTR_ERR(hwmon_dev);
416 NV_ERROR(dev,
417 "Unable to register hwmon device: %d\n", ret);
418 return ret;
419 }
420 dev_set_drvdata(hwmon_dev, dev);
421 ret = sysfs_create_group(&hwmon_dev->kobj,
422 &hwmon_attrgroup);
423 if (ret) {
424 NV_ERROR(dev,
425 "Unable to create hwmon sysfs file: %d\n", ret);
426 hwmon_device_unregister(hwmon_dev);
427 return ret;
428 }
429
Francisco Jerez8155cac2010-09-23 20:58:38 +0200430 pm->hwmon = hwmon_dev;
Martin Peresb54262f2010-10-26 12:48:28 +0200431#endif
Martin Peres34e9d852010-09-22 20:54:22 +0200432 return 0;
433}
434
435static void
436nouveau_hwmon_fini(struct drm_device *dev)
437{
Martin Peresb54262f2010-10-26 12:48:28 +0200438#ifdef CONFIG_HWMON
Martin Peres34e9d852010-09-22 20:54:22 +0200439 struct drm_nouveau_private *dev_priv = dev->dev_private;
Francisco Jerez8155cac2010-09-23 20:58:38 +0200440 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
Martin Peres34e9d852010-09-22 20:54:22 +0200441
Francisco Jerez8155cac2010-09-23 20:58:38 +0200442 if (pm->hwmon) {
443 sysfs_remove_group(&pm->hwmon->kobj, &hwmon_attrgroup);
444 hwmon_device_unregister(pm->hwmon);
Martin Peres34e9d852010-09-22 20:54:22 +0200445 }
Martin Peresb54262f2010-10-26 12:48:28 +0200446#endif
Martin Peres34e9d852010-09-22 20:54:22 +0200447}
448
Martin Peres34e9d852010-09-22 20:54:22 +0200449int
450nouveau_pm_init(struct drm_device *dev)
451{
452 struct drm_nouveau_private *dev_priv = dev->dev_private;
453 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
454 char info[256];
455 int ret, i;
456
457 nouveau_volt_init(dev);
458 nouveau_perf_init(dev);
459 nouveau_temp_init(dev);
Roy Spliet7760fcb2010-09-17 23:17:24 +0200460 nouveau_mem_timing_init(dev);
Martin Peres34e9d852010-09-22 20:54:22 +0200461
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);
466 }
467
468 /* determine current ("boot") performance level */
469 ret = nouveau_pm_perflvl_get(dev, &pm->boot);
470 if (ret == 0) {
471 pm->cur = &pm->boot;
472
473 nouveau_pm_perflvl_info(&pm->boot, info, sizeof(info));
474 NV_INFO(dev, "c: %s", info);
475 }
476
477 /* switch performance levels now if requested */
478 if (nouveau_perflvl != NULL) {
479 ret = nouveau_pm_profile_set(dev, nouveau_perflvl);
480 if (ret) {
481 NV_ERROR(dev, "error setting perflvl \"%s\": %d\n",
482 nouveau_perflvl, ret);
483 }
484 }
485
486 nouveau_sysfs_init(dev);
487 nouveau_hwmon_init(dev);
488
489 return 0;
490}
491
492void
493nouveau_pm_fini(struct drm_device *dev)
494{
495 struct drm_nouveau_private *dev_priv = dev->dev_private;
496 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
497
498 if (pm->cur != &pm->boot)
499 nouveau_pm_perflvl_set(dev, &pm->boot);
Ben Skeggs330c5982010-09-16 15:39:49 +1000500
Roy Spliet7760fcb2010-09-17 23:17:24 +0200501 nouveau_mem_timing_fini(dev);
502 nouveau_temp_fini(dev);
Ben Skeggs330c5982010-09-16 15:39:49 +1000503 nouveau_perf_fini(dev);
504 nouveau_volt_fini(dev);
Martin Peres34e9d852010-09-22 20:54:22 +0200505
506 nouveau_hwmon_fini(dev);
507 nouveau_sysfs_fini(dev);
Ben Skeggs330c5982010-09-16 15:39:49 +1000508}
509
Ben Skeggs64f1c112010-09-17 13:35:25 +1000510void
511nouveau_pm_resume(struct drm_device *dev)
512{
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;
516
517 if (pm->cur == &pm->boot)
518 return;
519
520 perflvl = pm->cur;
521 pm->cur = &pm->boot;
522 nouveau_pm_perflvl_set(dev, perflvl);
523}