]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/nouveau/nouveau_pm.c
drm/nouveau/pm: calculate memory timings at perflvl creation time
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / nouveau / nouveau_pm.c
CommitLineData
330c5988
BS
1/*
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"
a0b25635 29#include "nouveau_gpio.h"
330c5988 30
6032649d
BS
31#ifdef CONFIG_ACPI
32#include <linux/acpi.h>
33#endif
34#include <linux/power_supply.h>
34e9d85a
MP
35#include <linux/hwmon.h>
36#include <linux/hwmon-sysfs.h>
37
a175094c
BS
38static int
39nouveau_pwmfan_get(struct drm_device *dev)
40{
41 struct drm_nouveau_private *dev_priv = dev->dev_private;
a175094c 42 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
a0b25635 43 struct gpio_func gpio;
a175094c
BS
44 u32 divs, duty;
45 int ret;
46
1e054157 47 if (!pm->pwm_get)
a175094c 48 return -ENODEV;
a175094c 49
a0b25635
BS
50 ret = nouveau_gpio_find(dev, 0, DCB_GPIO_PWM_FAN, 0xff, &gpio);
51 if (ret == 0) {
52 ret = pm->pwm_get(dev, gpio.line, &divs, &duty);
3d8a408c 53 if (ret == 0 && divs) {
a175094c 54 divs = max(divs, duty);
a0b25635 55 if (dev_priv->card_type <= NV_40 || (gpio.log[0] & 1))
a175094c
BS
56 duty = divs - duty;
57 return (duty * 100) / divs;
58 }
59
a0b25635 60 return nouveau_gpio_func_get(dev, gpio.func) * 100;
a175094c
BS
61 }
62
63 return -ENODEV;
64}
65
66static int
67nouveau_pwmfan_set(struct drm_device *dev, int percent)
68{
69 struct drm_nouveau_private *dev_priv = dev->dev_private;
70 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
a0b25635 71 struct gpio_func gpio;
a175094c 72 u32 divs, duty;
a0b25635 73 int ret;
a175094c 74
1e054157 75 if (!pm->pwm_set)
a175094c 76 return -ENODEV;
a175094c 77
a0b25635
BS
78 ret = nouveau_gpio_find(dev, 0, DCB_GPIO_PWM_FAN, 0xff, &gpio);
79 if (ret == 0) {
b1aa5531 80 divs = pm->fan.pwm_divisor;
a175094c
BS
81 if (pm->fan.pwm_freq) {
82 /*XXX: PNVIO clock more than likely... */
83 divs = 135000 / pm->fan.pwm_freq;
84 if (dev_priv->chipset < 0xa3)
85 divs /= 4;
86 }
87
88 duty = ((divs * percent) + 99) / 100;
a0b25635 89 if (dev_priv->card_type <= NV_40 || (gpio.log[0] & 1))
a175094c
BS
90 duty = divs - duty;
91
bc6389e4
MP
92 ret = pm->pwm_set(dev, gpio.line, divs, duty);
93 if (!ret)
94 pm->fan.percent = percent;
95 return ret;
a175094c
BS
96 }
97
98 return -ENODEV;
99}
100
64f1c11a 101static int
0b627a0b
BS
102nouveau_pm_perflvl_aux(struct drm_device *dev, struct nouveau_pm_level *perflvl,
103 struct nouveau_pm_level *a, struct nouveau_pm_level *b)
64f1c11a
BS
104{
105 struct drm_nouveau_private *dev_priv = dev->dev_private;
106 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
107 int ret;
108
11b7d895
MP
109 /*XXX: not on all boards, we should control based on temperature
110 * on recent boards.. or maybe on some other factor we don't
111 * know about?
112 */
0b627a0b 113 if (a->fanspeed && b->fanspeed && b->fanspeed > a->fanspeed) {
a175094c 114 ret = nouveau_pwmfan_set(dev, perflvl->fanspeed);
0b627a0b
BS
115 if (ret && ret != -ENODEV) {
116 NV_ERROR(dev, "fanspeed set failed: %d\n", ret);
117 return ret;
118 }
771e1035
BS
119 }
120
0b627a0b 121 if (pm->voltage.supported && pm->voltage_set) {
d2edab4a 122 if (perflvl->volt_min && b->volt_min > a->volt_min) {
0b627a0b
BS
123 ret = pm->voltage_set(dev, perflvl->volt_min);
124 if (ret) {
125 NV_ERROR(dev, "voltage set failed: %d\n", ret);
126 return ret;
127 }
64f1c11a
BS
128 }
129 }
130
0b627a0b
BS
131 return 0;
132}
133
134static int
135nouveau_pm_perflvl_set(struct drm_device *dev, struct nouveau_pm_level *perflvl)
136{
137 struct drm_nouveau_private *dev_priv = dev->dev_private;
138 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
139 void *state;
140 int ret;
141
142 if (perflvl == pm->cur)
143 return 0;
144
145 ret = nouveau_pm_perflvl_aux(dev, perflvl, pm->cur, perflvl);
146 if (ret)
147 return ret;
148
ff2b6c6e 149 state = pm->clocks_pre(dev, perflvl);
b0103747
MP
150 if (IS_ERR(state)) {
151 ret = PTR_ERR(state);
152 goto error;
153 }
154 ret = pm->clocks_set(dev, state);
155 if (ret)
156 goto error;
64f1c11a 157
0b627a0b
BS
158 ret = nouveau_pm_perflvl_aux(dev, perflvl, perflvl, pm->cur);
159 if (ret)
160 return ret;
161
64f1c11a
BS
162 pm->cur = perflvl;
163 return 0;
b0103747
MP
164
165error:
166 /* restore the fan speed and voltage before leaving */
167 nouveau_pm_perflvl_aux(dev, perflvl, perflvl, pm->cur);
168 return ret;
64f1c11a
BS
169}
170
6f876986
BS
171static int
172nouveau_pm_profile_set(struct drm_device *dev, const char *profile)
173{
174 struct drm_nouveau_private *dev_priv = dev->dev_private;
175 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
176 struct nouveau_pm_level *perflvl = NULL;
b0103747
MP
177 u64 start_time;
178 int ret = 0;
ddb20055 179 long pl;
6f876986
BS
180
181 /* safety precaution, for now */
182 if (nouveau_perflvl_wr != 7777)
183 return -EPERM;
184
6f876986
BS
185 if (!strncmp(profile, "boot", 4))
186 perflvl = &pm->boot;
187 else {
6f876986 188 int i;
ddb20055
MP
189 if (kstrtol(profile, 10, &pl) == -EINVAL)
190 return -EINVAL;
6f876986
BS
191
192 for (i = 0; i < pm->nr_perflvl; i++) {
193 if (pm->perflvl[i].id == pl) {
194 perflvl = &pm->perflvl[i];
195 break;
196 }
197 }
198
199 if (!perflvl)
200 return -EINVAL;
201 }
202
b0103747
MP
203 NV_INFO(dev, "setting performance level: %s", profile);
204 start_time = nv04_timer_read(dev);
205 ret = nouveau_pm_perflvl_set(dev, perflvl);
206 if (!ret) {
207 NV_INFO(dev, "> reclocking took %lluns\n\n",
208 (nv04_timer_read(dev) - start_time));
209 } else {
210 NV_INFO(dev, "> reclocking failed\n\n");
211 }
212
213 return ret;
6f876986
BS
214}
215
330c5988
BS
216static int
217nouveau_pm_perflvl_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
218{
219 struct drm_nouveau_private *dev_priv = dev->dev_private;
220 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
221 int ret;
222
330c5988
BS
223 memset(perflvl, 0, sizeof(*perflvl));
224
ff2b6c6e
BS
225 ret = pm->clocks_get(dev, perflvl);
226 if (ret)
227 return ret;
330c5988
BS
228
229 if (pm->voltage.supported && pm->voltage_get) {
230 ret = pm->voltage_get(dev);
3b5565dd
BS
231 if (ret > 0) {
232 perflvl->volt_min = ret;
233 perflvl->volt_max = ret;
234 }
330c5988
BS
235 }
236
a175094c
BS
237 ret = nouveau_pwmfan_get(dev);
238 if (ret > 0)
239 perflvl->fanspeed = ret;
771e1035 240
fd99fd61 241 nouveau_mem_timing_read(dev, &perflvl->timing);
330c5988
BS
242 return 0;
243}
244
245static void
246nouveau_pm_perflvl_info(struct nouveau_pm_level *perflvl, char *ptr, int len)
247{
93dccbed 248 char c[16], s[16], v[32], f[16], t[16], m[16];
0fbb114a
FJ
249
250 c[0] = '\0';
251 if (perflvl->core)
252 snprintf(c, sizeof(c), " core %dMHz", perflvl->core / 1000);
330c5988
BS
253
254 s[0] = '\0';
255 if (perflvl->shader)
256 snprintf(s, sizeof(s), " shader %dMHz", perflvl->shader / 1000);
257
93dccbed
BS
258 m[0] = '\0';
259 if (perflvl->memory)
260 snprintf(m, sizeof(m), " memory %dMHz", perflvl->memory / 1000);
261
330c5988 262 v[0] = '\0';
3b5565dd
BS
263 if (perflvl->volt_min && perflvl->volt_min != perflvl->volt_max) {
264 snprintf(v, sizeof(v), " voltage %dmV-%dmV",
265 perflvl->volt_min / 1000, perflvl->volt_max / 1000);
266 } else
267 if (perflvl->volt_min) {
268 snprintf(v, sizeof(v), " voltage %dmV",
269 perflvl->volt_min / 1000);
270 }
330c5988
BS
271
272 f[0] = '\0';
273 if (perflvl->fanspeed)
274 snprintf(f, sizeof(f), " fanspeed %d%%", perflvl->fanspeed);
275
e614b2e7
MP
276 t[0] = '\0';
277 if (perflvl->timing)
278 snprintf(t, sizeof(t), " timing %d", perflvl->timing->id);
279
93dccbed 280 snprintf(ptr, len, "%s%s%s%s%s%s\n", c, s, m, t, v, f);
330c5988
BS
281}
282
283static ssize_t
284nouveau_pm_get_perflvl_info(struct device *d,
285 struct device_attribute *a, char *buf)
286{
287 struct nouveau_pm_level *perflvl = (struct nouveau_pm_level *)a;
288 char *ptr = buf;
289 int len = PAGE_SIZE;
290
93dccbed 291 snprintf(ptr, len, "%d:", perflvl->id);
330c5988
BS
292 ptr += strlen(buf);
293 len -= strlen(buf);
294
295 nouveau_pm_perflvl_info(perflvl, ptr, len);
296 return strlen(buf);
297}
298
299static ssize_t
300nouveau_pm_get_perflvl(struct device *d, struct device_attribute *a, char *buf)
301{
34e9d85a 302 struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
330c5988
BS
303 struct drm_nouveau_private *dev_priv = dev->dev_private;
304 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
305 struct nouveau_pm_level cur;
306 int len = PAGE_SIZE, ret;
307 char *ptr = buf;
308
309 if (!pm->cur)
310 snprintf(ptr, len, "setting: boot\n");
311 else if (pm->cur == &pm->boot)
93dccbed 312 snprintf(ptr, len, "setting: boot\nc:");
330c5988 313 else
93dccbed 314 snprintf(ptr, len, "setting: static %d\nc:", pm->cur->id);
330c5988
BS
315 ptr += strlen(buf);
316 len -= strlen(buf);
317
318 ret = nouveau_pm_perflvl_get(dev, &cur);
319 if (ret == 0)
320 nouveau_pm_perflvl_info(&cur, ptr, len);
321 return strlen(buf);
322}
323
324static ssize_t
325nouveau_pm_set_perflvl(struct device *d, struct device_attribute *a,
326 const char *buf, size_t count)
327{
34e9d85a 328 struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
6f876986
BS
329 int ret;
330
331 ret = nouveau_pm_profile_set(dev, buf);
332 if (ret)
333 return ret;
334 return strlen(buf);
330c5988
BS
335}
336
5c4abd09
FJ
337static DEVICE_ATTR(performance_level, S_IRUGO | S_IWUSR,
338 nouveau_pm_get_perflvl, nouveau_pm_set_perflvl);
330c5988 339
34e9d85a
MP
340static int
341nouveau_sysfs_init(struct drm_device *dev)
330c5988
BS
342{
343 struct drm_nouveau_private *dev_priv = dev->dev_private;
344 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
345 struct device *d = &dev->pdev->dev;
330c5988
BS
346 int ret, i;
347
330c5988
BS
348 ret = device_create_file(d, &dev_attr_performance_level);
349 if (ret)
350 return ret;
351
352 for (i = 0; i < pm->nr_perflvl; i++) {
353 struct nouveau_pm_level *perflvl = &pm->perflvl[i];
354
355 perflvl->dev_attr.attr.name = perflvl->name;
356 perflvl->dev_attr.attr.mode = S_IRUGO;
357 perflvl->dev_attr.show = nouveau_pm_get_perflvl_info;
358 perflvl->dev_attr.store = NULL;
359 sysfs_attr_init(&perflvl->dev_attr.attr);
360
361 ret = device_create_file(d, &perflvl->dev_attr);
362 if (ret) {
363 NV_ERROR(dev, "failed pervlvl %d sysfs: %d\n",
364 perflvl->id, i);
365 perflvl->dev_attr.attr.name = NULL;
366 nouveau_pm_fini(dev);
367 return ret;
368 }
369 }
370
371 return 0;
372}
373
34e9d85a
MP
374static void
375nouveau_sysfs_fini(struct drm_device *dev)
330c5988
BS
376{
377 struct drm_nouveau_private *dev_priv = dev->dev_private;
378 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
379 struct device *d = &dev->pdev->dev;
380 int i;
381
382 device_remove_file(d, &dev_attr_performance_level);
383 for (i = 0; i < pm->nr_perflvl; i++) {
384 struct nouveau_pm_level *pl = &pm->perflvl[i];
385
386 if (!pl->dev_attr.attr.name)
387 break;
388
389 device_remove_file(d, &pl->dev_attr);
390 }
34e9d85a
MP
391}
392
658e86ee 393#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
34e9d85a
MP
394static ssize_t
395nouveau_hwmon_show_temp(struct device *d, struct device_attribute *a, char *buf)
396{
397 struct drm_device *dev = dev_get_drvdata(d);
8155cac4
FJ
398 struct drm_nouveau_private *dev_priv = dev->dev_private;
399 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
34e9d85a 400
8155cac4 401 return snprintf(buf, PAGE_SIZE, "%d\n", pm->temp_get(dev)*1000);
34e9d85a
MP
402}
403static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, nouveau_hwmon_show_temp,
404 NULL, 0);
405
406static ssize_t
407nouveau_hwmon_max_temp(struct device *d, struct device_attribute *a, char *buf)
408{
409 struct drm_device *dev = dev_get_drvdata(d);
410 struct drm_nouveau_private *dev_priv = dev->dev_private;
411 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
412 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
413
414 return snprintf(buf, PAGE_SIZE, "%d\n", temp->down_clock*1000);
415}
416static ssize_t
417nouveau_hwmon_set_max_temp(struct device *d, struct device_attribute *a,
418 const char *buf, size_t count)
419{
420 struct drm_device *dev = dev_get_drvdata(d);
421 struct drm_nouveau_private *dev_priv = dev->dev_private;
422 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
423 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
424 long value;
425
ddb20055 426 if (kstrtol(buf, 10, &value) == -EINVAL)
34e9d85a
MP
427 return count;
428
429 temp->down_clock = value/1000;
430
431 nouveau_temp_safety_checks(dev);
432
433 return count;
434}
435static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, nouveau_hwmon_max_temp,
436 nouveau_hwmon_set_max_temp,
437 0);
438
439static ssize_t
440nouveau_hwmon_critical_temp(struct device *d, struct device_attribute *a,
441 char *buf)
442{
443 struct drm_device *dev = dev_get_drvdata(d);
444 struct drm_nouveau_private *dev_priv = dev->dev_private;
445 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
446 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
447
448 return snprintf(buf, PAGE_SIZE, "%d\n", temp->critical*1000);
449}
450static ssize_t
451nouveau_hwmon_set_critical_temp(struct device *d, struct device_attribute *a,
452 const char *buf,
453 size_t count)
454{
455 struct drm_device *dev = dev_get_drvdata(d);
456 struct drm_nouveau_private *dev_priv = dev->dev_private;
457 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
458 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
459 long value;
460
ddb20055 461 if (kstrtol(buf, 10, &value) == -EINVAL)
34e9d85a
MP
462 return count;
463
464 temp->critical = value/1000;
465
466 nouveau_temp_safety_checks(dev);
467
468 return count;
469}
470static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
471 nouveau_hwmon_critical_temp,
472 nouveau_hwmon_set_critical_temp,
473 0);
474
475static ssize_t nouveau_hwmon_show_name(struct device *dev,
476 struct device_attribute *attr,
477 char *buf)
478{
479 return sprintf(buf, "nouveau\n");
480}
481static SENSOR_DEVICE_ATTR(name, S_IRUGO, nouveau_hwmon_show_name, NULL, 0);
482
483static ssize_t nouveau_hwmon_show_update_rate(struct device *dev,
484 struct device_attribute *attr,
485 char *buf)
486{
487 return sprintf(buf, "1000\n");
488}
489static SENSOR_DEVICE_ATTR(update_rate, S_IRUGO,
490 nouveau_hwmon_show_update_rate,
491 NULL, 0);
492
11b7d895
MP
493static ssize_t
494nouveau_hwmon_show_fan0_input(struct device *d, struct device_attribute *attr,
495 char *buf)
496{
497 struct drm_device *dev = dev_get_drvdata(d);
498 struct drm_nouveau_private *dev_priv = dev->dev_private;
499 struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
a0b25635 500 struct gpio_func gpio;
11b7d895
MP
501 u32 cycles, cur, prev;
502 u64 start;
a0b25635 503 int ret;
11b7d895 504
a0b25635
BS
505 ret = nouveau_gpio_find(dev, 0, DCB_GPIO_FAN_SENSE, 0xff, &gpio);
506 if (ret)
507 return ret;
11b7d895
MP
508
509 /* Monitor the GPIO input 0x3b for 250ms.
510 * When the fan spins, it changes the value of GPIO FAN_SENSE.
511 * We get 4 changes (0 -> 1 -> 0 -> 1 -> [...]) per complete rotation.
512 */
513 start = ptimer->read(dev);
a0b25635 514 prev = nouveau_gpio_sense(dev, 0, gpio.line);
11b7d895
MP
515 cycles = 0;
516 do {
a0b25635 517 cur = nouveau_gpio_sense(dev, 0, gpio.line);
11b7d895
MP
518 if (prev != cur) {
519 cycles++;
520 prev = cur;
521 }
522
523 usleep_range(500, 1000); /* supports 0 < rpm < 7500 */
524 } while (ptimer->read(dev) - start < 250000000);
525
526 /* interpolate to get rpm */
527 return sprintf(buf, "%i\n", cycles / 4 * 4 * 60);
528}
529static SENSOR_DEVICE_ATTR(fan0_input, S_IRUGO, nouveau_hwmon_show_fan0_input,
530 NULL, 0);
531
532static ssize_t
533nouveau_hwmon_get_pwm0(struct device *d, struct device_attribute *a, char *buf)
534{
535 struct drm_device *dev = dev_get_drvdata(d);
a175094c 536 int ret;
11b7d895 537
a175094c 538 ret = nouveau_pwmfan_get(dev);
11b7d895
MP
539 if (ret < 0)
540 return ret;
541
542 return sprintf(buf, "%i\n", ret);
543}
544
545static ssize_t
546nouveau_hwmon_set_pwm0(struct device *d, struct device_attribute *a,
547 const char *buf, size_t count)
548{
549 struct drm_device *dev = dev_get_drvdata(d);
550 struct drm_nouveau_private *dev_priv = dev->dev_private;
551 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
552 int ret = -ENODEV;
553 long value;
554
555 if (nouveau_perflvl_wr != 7777)
556 return -EPERM;
557
ddb20055 558 if (kstrtol(buf, 10, &value) == -EINVAL)
11b7d895
MP
559 return -EINVAL;
560
561 if (value < pm->fan.min_duty)
562 value = pm->fan.min_duty;
563 if (value > pm->fan.max_duty)
564 value = pm->fan.max_duty;
565
a175094c 566 ret = nouveau_pwmfan_set(dev, value);
11b7d895
MP
567 if (ret)
568 return ret;
569
570 return count;
571}
572
573static SENSOR_DEVICE_ATTR(pwm0, S_IRUGO | S_IWUSR,
574 nouveau_hwmon_get_pwm0,
575 nouveau_hwmon_set_pwm0, 0);
576
577static ssize_t
578nouveau_hwmon_get_pwm0_min(struct device *d,
579 struct device_attribute *a, char *buf)
580{
581 struct drm_device *dev = dev_get_drvdata(d);
582 struct drm_nouveau_private *dev_priv = dev->dev_private;
583 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
584
585 return sprintf(buf, "%i\n", pm->fan.min_duty);
586}
587
588static ssize_t
589nouveau_hwmon_set_pwm0_min(struct device *d, struct device_attribute *a,
590 const char *buf, size_t count)
591{
592 struct drm_device *dev = dev_get_drvdata(d);
593 struct drm_nouveau_private *dev_priv = dev->dev_private;
594 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
595 long value;
596
ddb20055 597 if (kstrtol(buf, 10, &value) == -EINVAL)
11b7d895
MP
598 return -EINVAL;
599
600 if (value < 0)
601 value = 0;
602
603 if (pm->fan.max_duty - value < 10)
604 value = pm->fan.max_duty - 10;
605
606 if (value < 10)
607 pm->fan.min_duty = 10;
608 else
609 pm->fan.min_duty = value;
610
611 return count;
612}
613
614static SENSOR_DEVICE_ATTR(pwm0_min, S_IRUGO | S_IWUSR,
615 nouveau_hwmon_get_pwm0_min,
616 nouveau_hwmon_set_pwm0_min, 0);
617
618static ssize_t
619nouveau_hwmon_get_pwm0_max(struct device *d,
620 struct device_attribute *a, char *buf)
621{
622 struct drm_device *dev = dev_get_drvdata(d);
623 struct drm_nouveau_private *dev_priv = dev->dev_private;
624 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
625
626 return sprintf(buf, "%i\n", pm->fan.max_duty);
627}
628
629static ssize_t
630nouveau_hwmon_set_pwm0_max(struct device *d, struct device_attribute *a,
631 const char *buf, size_t count)
632{
633 struct drm_device *dev = dev_get_drvdata(d);
634 struct drm_nouveau_private *dev_priv = dev->dev_private;
635 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
636 long value;
637
ddb20055 638 if (kstrtol(buf, 10, &value) == -EINVAL)
11b7d895
MP
639 return -EINVAL;
640
641 if (value < 0)
642 value = 0;
643
644 if (value - pm->fan.min_duty < 10)
645 value = pm->fan.min_duty + 10;
646
647 if (value > 100)
648 pm->fan.max_duty = 100;
649 else
650 pm->fan.max_duty = value;
651
652 return count;
653}
654
655static SENSOR_DEVICE_ATTR(pwm0_max, S_IRUGO | S_IWUSR,
656 nouveau_hwmon_get_pwm0_max,
657 nouveau_hwmon_set_pwm0_max, 0);
658
34e9d85a
MP
659static struct attribute *hwmon_attributes[] = {
660 &sensor_dev_attr_temp1_input.dev_attr.attr,
661 &sensor_dev_attr_temp1_max.dev_attr.attr,
662 &sensor_dev_attr_temp1_crit.dev_attr.attr,
663 &sensor_dev_attr_name.dev_attr.attr,
664 &sensor_dev_attr_update_rate.dev_attr.attr,
665 NULL
666};
11b7d895
MP
667static struct attribute *hwmon_fan_rpm_attributes[] = {
668 &sensor_dev_attr_fan0_input.dev_attr.attr,
669 NULL
670};
671static struct attribute *hwmon_pwm_fan_attributes[] = {
672 &sensor_dev_attr_pwm0.dev_attr.attr,
673 &sensor_dev_attr_pwm0_min.dev_attr.attr,
674 &sensor_dev_attr_pwm0_max.dev_attr.attr,
675 NULL
676};
34e9d85a
MP
677
678static const struct attribute_group hwmon_attrgroup = {
679 .attrs = hwmon_attributes,
680};
11b7d895
MP
681static const struct attribute_group hwmon_fan_rpm_attrgroup = {
682 .attrs = hwmon_fan_rpm_attributes,
683};
684static const struct attribute_group hwmon_pwm_fan_attrgroup = {
685 .attrs = hwmon_pwm_fan_attributes,
686};
b54262f3 687#endif
34e9d85a
MP
688
689static int
690nouveau_hwmon_init(struct drm_device *dev)
691{
692 struct drm_nouveau_private *dev_priv = dev->dev_private;
8155cac4 693 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
095f979a 694#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
34e9d85a 695 struct device *hwmon_dev;
11b7d895 696 int ret = 0;
34e9d85a 697
8155cac4
FJ
698 if (!pm->temp_get)
699 return -ENODEV;
34e9d85a
MP
700
701 hwmon_dev = hwmon_device_register(&dev->pdev->dev);
702 if (IS_ERR(hwmon_dev)) {
703 ret = PTR_ERR(hwmon_dev);
704 NV_ERROR(dev,
705 "Unable to register hwmon device: %d\n", ret);
706 return ret;
707 }
708 dev_set_drvdata(hwmon_dev, dev);
11b7d895
MP
709
710 /* default sysfs entries */
07cfe0e7 711 ret = sysfs_create_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
34e9d85a 712 if (ret) {
11b7d895
MP
713 if (ret)
714 goto error;
715 }
716
717 /* if the card has a pwm fan */
718 /*XXX: incorrect, need better detection for this, some boards have
719 * the gpio entries for pwm fan control even when there's no
720 * actual fan connected to it... therm table? */
a175094c 721 if (nouveau_pwmfan_get(dev) >= 0) {
11b7d895
MP
722 ret = sysfs_create_group(&dev->pdev->dev.kobj,
723 &hwmon_pwm_fan_attrgroup);
724 if (ret)
725 goto error;
726 }
727
728 /* if the card can read the fan rpm */
a0b25635 729 if (nouveau_gpio_func_valid(dev, DCB_GPIO_FAN_SENSE)) {
11b7d895
MP
730 ret = sysfs_create_group(&dev->pdev->dev.kobj,
731 &hwmon_fan_rpm_attrgroup);
732 if (ret)
733 goto error;
34e9d85a
MP
734 }
735
8155cac4 736 pm->hwmon = hwmon_dev;
11b7d895 737
34e9d85a 738 return 0;
11b7d895
MP
739
740error:
741 NV_ERROR(dev, "Unable to create some hwmon sysfs files: %d\n", ret);
742 hwmon_device_unregister(hwmon_dev);
743 pm->hwmon = NULL;
744 return ret;
745#else
746 pm->hwmon = NULL;
747 return 0;
748#endif
34e9d85a
MP
749}
750
751static void
752nouveau_hwmon_fini(struct drm_device *dev)
753{
658e86ee 754#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
34e9d85a 755 struct drm_nouveau_private *dev_priv = dev->dev_private;
8155cac4 756 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
34e9d85a 757
8155cac4 758 if (pm->hwmon) {
8c06a3e0 759 sysfs_remove_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
ddb20055
MP
760 sysfs_remove_group(&dev->pdev->dev.kobj,
761 &hwmon_pwm_fan_attrgroup);
762 sysfs_remove_group(&dev->pdev->dev.kobj,
763 &hwmon_fan_rpm_attrgroup);
11b7d895 764
8155cac4 765 hwmon_device_unregister(pm->hwmon);
34e9d85a 766 }
b54262f3 767#endif
34e9d85a
MP
768}
769
1f962797 770#if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
6032649d
BS
771static int
772nouveau_pm_acpi_event(struct notifier_block *nb, unsigned long val, void *data)
773{
774 struct drm_nouveau_private *dev_priv =
775 container_of(nb, struct drm_nouveau_private, engine.pm.acpi_nb);
776 struct drm_device *dev = dev_priv->dev;
777 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
778
779 if (strcmp(entry->device_class, "ac_adapter") == 0) {
780 bool ac = power_supply_is_system_supplied();
781
782 NV_DEBUG(dev, "power supply changed: %s\n", ac ? "AC" : "DC");
783 }
784
785 return NOTIFY_OK;
786}
787#endif
788
34e9d85a
MP
789int
790nouveau_pm_init(struct drm_device *dev)
791{
792 struct drm_nouveau_private *dev_priv = dev->dev_private;
793 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
794 char info[256];
795 int ret, i;
796
68a64cad 797 /* parse aux tables from vbios */
34e9d85a 798 nouveau_volt_init(dev);
34e9d85a
MP
799 nouveau_temp_init(dev);
800
68a64cad
BS
801 /* determine current ("boot") performance level */
802 ret = nouveau_pm_perflvl_get(dev, &pm->boot);
803 if (ret) {
804 NV_ERROR(dev, "failed to determine boot perflvl\n");
805 return ret;
806 }
807
808 strncpy(pm->boot.name, "boot", 4);
68a64cad
BS
809 pm->cur = &pm->boot;
810
811 /* add performance levels from vbios */
812 nouveau_perf_init(dev);
813
814 /* display available performance levels */
34e9d85a
MP
815 NV_INFO(dev, "%d available performance level(s)\n", pm->nr_perflvl);
816 for (i = 0; i < pm->nr_perflvl; i++) {
817 nouveau_pm_perflvl_info(&pm->perflvl[i], info, sizeof(info));
93dccbed 818 NV_INFO(dev, "%d:%s", pm->perflvl[i].id, info);
34e9d85a
MP
819 }
820
68a64cad
BS
821 nouveau_pm_perflvl_info(&pm->boot, info, sizeof(info));
822 NV_INFO(dev, "c:%s", info);
34e9d85a
MP
823
824 /* switch performance levels now if requested */
825 if (nouveau_perflvl != NULL) {
826 ret = nouveau_pm_profile_set(dev, nouveau_perflvl);
827 if (ret) {
828 NV_ERROR(dev, "error setting perflvl \"%s\": %d\n",
829 nouveau_perflvl, ret);
830 }
831 }
832
bc6389e4
MP
833 /* determine the current fan speed */
834 pm->fan.percent = nouveau_pwmfan_get(dev);
835
34e9d85a
MP
836 nouveau_sysfs_init(dev);
837 nouveau_hwmon_init(dev);
1f962797 838#if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
6032649d
BS
839 pm->acpi_nb.notifier_call = nouveau_pm_acpi_event;
840 register_acpi_notifier(&pm->acpi_nb);
841#endif
34e9d85a
MP
842
843 return 0;
844}
845
846void
847nouveau_pm_fini(struct drm_device *dev)
848{
849 struct drm_nouveau_private *dev_priv = dev->dev_private;
850 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
851
852 if (pm->cur != &pm->boot)
853 nouveau_pm_perflvl_set(dev, &pm->boot);
330c5988 854
7760fcb0 855 nouveau_temp_fini(dev);
330c5988
BS
856 nouveau_perf_fini(dev);
857 nouveau_volt_fini(dev);
34e9d85a 858
1f962797 859#if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
6032649d
BS
860 unregister_acpi_notifier(&pm->acpi_nb);
861#endif
34e9d85a
MP
862 nouveau_hwmon_fini(dev);
863 nouveau_sysfs_fini(dev);
330c5988
BS
864}
865
64f1c11a
BS
866void
867nouveau_pm_resume(struct drm_device *dev)
868{
869 struct drm_nouveau_private *dev_priv = dev->dev_private;
870 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
871 struct nouveau_pm_level *perflvl;
872
317495b2 873 if (!pm->cur || pm->cur == &pm->boot)
64f1c11a
BS
874 return;
875
876 perflvl = pm->cur;
877 pm->cur = &pm->boot;
878 nouveau_pm_perflvl_set(dev, perflvl);
bc6389e4 879 nouveau_pwmfan_set(dev, pm->fan.percent);
64f1c11a 880}