blob: 0dca191ee173469e91acf5ef30ddf77fa18ed6e2 [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
Ben Skeggs60326492010-10-12 12:31:32 +100025#ifdef CONFIG_ACPI
26#include <linux/acpi.h>
27#endif
28#include <linux/power_supply.h>
Martin Peres34e9d852010-09-22 20:54:22 +020029#include <linux/hwmon.h>
30#include <linux/hwmon-sysfs.h>
31
Ben Skeggs77145f12012-07-31 16:16:21 +100032#include "drmP.h"
33
34#include "nouveau_drm.h"
35#include "nouveau_pm.h"
36
Ben Skeggs77145f12012-07-31 16:16:21 +100037#include <subdev/gpio.h>
38#include <subdev/timer.h>
Martin Peresaa1b9b42012-09-02 02:55:58 +020039#include <subdev/therm.h>
Ben Skeggs77145f12012-07-31 16:16:21 +100040
41MODULE_PARM_DESC(perflvl, "Performance level (default: boot)");
42static char *nouveau_perflvl;
43module_param_named(perflvl, nouveau_perflvl, charp, 0400);
44
45MODULE_PARM_DESC(perflvl_wr, "Allow perflvl changes (warning: dangerous!)");
46static int nouveau_perflvl_wr;
47module_param_named(perflvl_wr, nouveau_perflvl_wr, int, 0400);
48
Ben Skeggs330c5982010-09-16 15:39:49 +100049static int
Ben Skeggs0b627a02011-10-27 12:02:12 +100050nouveau_pm_perflvl_aux(struct drm_device *dev, struct nouveau_pm_level *perflvl,
51 struct nouveau_pm_level *a, struct nouveau_pm_level *b)
52{
Ben Skeggs77145f12012-07-31 16:16:21 +100053 struct nouveau_drm *drm = nouveau_drm(dev);
54 struct nouveau_pm *pm = nouveau_pm(dev);
Martin Peresaa1b9b42012-09-02 02:55:58 +020055 struct nouveau_therm *therm = nouveau_therm(drm);
Ben Skeggs0b627a02011-10-27 12:02:12 +100056 int ret;
57
58 /*XXX: not on all boards, we should control based on temperature
59 * on recent boards.. or maybe on some other factor we don't
60 * know about?
61 */
Martin Peresaa1b9b42012-09-02 02:55:58 +020062 if (therm && therm->fan_set &&
63 a->fanspeed && b->fanspeed && b->fanspeed > a->fanspeed) {
64 ret = therm->fan_set(therm, perflvl->fanspeed);
Ben Skeggs0b627a02011-10-27 12:02:12 +100065 if (ret && ret != -ENODEV) {
Ben Skeggs77145f12012-07-31 16:16:21 +100066 NV_ERROR(drm, "fanspeed set failed: %d\n", ret);
Ben Skeggs0b627a02011-10-27 12:02:12 +100067 return ret;
68 }
69 }
70
71 if (pm->voltage.supported && pm->voltage_set) {
Ben Skeggsd2edab42011-11-10 13:20:14 +100072 if (perflvl->volt_min && b->volt_min > a->volt_min) {
Ben Skeggs0b627a02011-10-27 12:02:12 +100073 ret = pm->voltage_set(dev, perflvl->volt_min);
74 if (ret) {
Ben Skeggs77145f12012-07-31 16:16:21 +100075 NV_ERROR(drm, "voltage set failed: %d\n", ret);
Ben Skeggs0b627a02011-10-27 12:02:12 +100076 return ret;
77 }
78 }
79 }
80
81 return 0;
82}
83
84static int
Ben Skeggs64f1c112010-09-17 13:35:25 +100085nouveau_pm_perflvl_set(struct drm_device *dev, struct nouveau_pm_level *perflvl)
86{
Ben Skeggs77145f12012-07-31 16:16:21 +100087 struct nouveau_pm *pm = nouveau_pm(dev);
Ben Skeggsff2b6c62011-10-27 10:28:17 +100088 void *state;
Ben Skeggs64f1c112010-09-17 13:35:25 +100089 int ret;
90
91 if (perflvl == pm->cur)
92 return 0;
93
Ben Skeggs0b627a02011-10-27 12:02:12 +100094 ret = nouveau_pm_perflvl_aux(dev, perflvl, pm->cur, perflvl);
95 if (ret)
96 return ret;
Ben Skeggs64f1c112010-09-17 13:35:25 +100097
Ben Skeggsff2b6c62011-10-27 10:28:17 +100098 state = pm->clocks_pre(dev, perflvl);
Martin Peresb0103742011-11-03 00:03:06 +010099 if (IS_ERR(state)) {
100 ret = PTR_ERR(state);
101 goto error;
102 }
103 ret = pm->clocks_set(dev, state);
104 if (ret)
105 goto error;
Ben Skeggs64f1c112010-09-17 13:35:25 +1000106
Ben Skeggs0b627a02011-10-27 12:02:12 +1000107 ret = nouveau_pm_perflvl_aux(dev, perflvl, perflvl, pm->cur);
108 if (ret)
109 return ret;
110
Ben Skeggs64f1c112010-09-17 13:35:25 +1000111 pm->cur = perflvl;
112 return 0;
Martin Peresb0103742011-11-03 00:03:06 +0100113
114error:
115 /* restore the fan speed and voltage before leaving */
116 nouveau_pm_perflvl_aux(dev, perflvl, perflvl, pm->cur);
117 return ret;
Ben Skeggs64f1c112010-09-17 13:35:25 +1000118}
119
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000120void
121nouveau_pm_trigger(struct drm_device *dev)
122{
Ben Skeggs77145f12012-07-31 16:16:21 +1000123 struct nouveau_drm *drm = nouveau_drm(dev);
124 struct nouveau_timer *ptimer = nouveau_timer(drm->device);
125 struct nouveau_pm *pm = nouveau_pm(dev);
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000126 struct nouveau_pm_profile *profile = NULL;
127 struct nouveau_pm_level *perflvl = NULL;
128 int ret;
129
130 /* select power profile based on current power source */
131 if (power_supply_is_system_supplied())
132 profile = pm->profile_ac;
133 else
134 profile = pm->profile_dc;
135
Ben Skeggs25c53c12012-01-24 18:03:25 +1000136 if (profile != pm->profile) {
137 pm->profile->func->fini(pm->profile);
138 pm->profile = profile;
139 pm->profile->func->init(pm->profile);
140 }
141
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000142 /* select performance level based on profile */
143 perflvl = profile->func->select(profile);
144
145 /* change perflvl, if necessary */
146 if (perflvl != pm->cur) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000147 u64 time0 = ptimer->read(ptimer);
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000148
Ben Skeggs77145f12012-07-31 16:16:21 +1000149 NV_INFO(drm, "setting performance level: %d", perflvl->id);
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000150 ret = nouveau_pm_perflvl_set(dev, perflvl);
151 if (ret)
Ben Skeggs77145f12012-07-31 16:16:21 +1000152 NV_INFO(drm, "> reclocking failed: %d\n\n", ret);
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000153
Ben Skeggs77145f12012-07-31 16:16:21 +1000154 NV_INFO(drm, "> reclocking took %lluns\n\n",
155 ptimer->read(ptimer) - time0);
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000156 }
157}
158
159static struct nouveau_pm_profile *
160profile_find(struct drm_device *dev, const char *string)
161{
Ben Skeggs77145f12012-07-31 16:16:21 +1000162 struct nouveau_pm *pm = nouveau_pm(dev);
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000163 struct nouveau_pm_profile *profile;
164
165 list_for_each_entry(profile, &pm->profiles, head) {
166 if (!strncmp(profile->name, string, sizeof(profile->name)))
167 return profile;
168 }
169
170 return NULL;
171}
172
Ben Skeggs64f1c112010-09-17 13:35:25 +1000173static int
Ben Skeggs6f876982010-09-16 16:47:14 +1000174nouveau_pm_profile_set(struct drm_device *dev, const char *profile)
175{
Ben Skeggs77145f12012-07-31 16:16:21 +1000176 struct nouveau_pm *pm = nouveau_pm(dev);
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000177 struct nouveau_pm_profile *ac = NULL, *dc = NULL;
178 char string[16], *cur = string, *ptr;
Ben Skeggs6f876982010-09-16 16:47:14 +1000179
180 /* safety precaution, for now */
181 if (nouveau_perflvl_wr != 7777)
182 return -EPERM;
183
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000184 strncpy(string, profile, sizeof(string));
Jim Meyering5799d9e2012-04-17 21:27:54 +0200185 string[sizeof(string) - 1] = 0;
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000186 if ((ptr = strchr(string, '\n')))
187 *ptr = '\0';
Ben Skeggs6f876982010-09-16 16:47:14 +1000188
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000189 ptr = strsep(&cur, ",");
190 if (ptr)
191 ac = profile_find(dev, ptr);
Ben Skeggs6f876982010-09-16 16:47:14 +1000192
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000193 ptr = strsep(&cur, ",");
194 if (ptr)
195 dc = profile_find(dev, ptr);
196 else
197 dc = ac;
Ben Skeggs6f876982010-09-16 16:47:14 +1000198
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000199 if (ac == NULL || dc == NULL)
200 return -EINVAL;
Martin Peresb0103742011-11-03 00:03:06 +0100201
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000202 pm->profile_ac = ac;
203 pm->profile_dc = dc;
204 nouveau_pm_trigger(dev);
205 return 0;
Ben Skeggs6f876982010-09-16 16:47:14 +1000206}
207
Ben Skeggs25c53c12012-01-24 18:03:25 +1000208static void
209nouveau_pm_static_dummy(struct nouveau_pm_profile *profile)
210{
211}
212
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000213static struct nouveau_pm_level *
214nouveau_pm_static_select(struct nouveau_pm_profile *profile)
215{
216 return container_of(profile, struct nouveau_pm_level, profile);
217}
218
219const struct nouveau_pm_profile_func nouveau_pm_static_profile_func = {
Ben Skeggs25c53c12012-01-24 18:03:25 +1000220 .destroy = nouveau_pm_static_dummy,
221 .init = nouveau_pm_static_dummy,
222 .fini = nouveau_pm_static_dummy,
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000223 .select = nouveau_pm_static_select,
224};
225
Ben Skeggs6f876982010-09-16 16:47:14 +1000226static int
Ben Skeggs330c5982010-09-16 15:39:49 +1000227nouveau_pm_perflvl_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
228{
Martin Peresaa1b9b42012-09-02 02:55:58 +0200229 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +1000230 struct nouveau_pm *pm = nouveau_pm(dev);
Martin Peresaa1b9b42012-09-02 02:55:58 +0200231 struct nouveau_therm *therm = nouveau_therm(drm->device);
Ben Skeggs330c5982010-09-16 15:39:49 +1000232 int ret;
233
Ben Skeggs330c5982010-09-16 15:39:49 +1000234 memset(perflvl, 0, sizeof(*perflvl));
235
Ben Skeggsc11dd0d2012-03-07 14:18:49 +1000236 if (pm->clocks_get) {
237 ret = pm->clocks_get(dev, perflvl);
238 if (ret)
239 return ret;
240 }
Ben Skeggs330c5982010-09-16 15:39:49 +1000241
242 if (pm->voltage.supported && pm->voltage_get) {
243 ret = pm->voltage_get(dev);
Ben Skeggs3b5565d2011-06-09 16:57:07 +1000244 if (ret > 0) {
245 perflvl->volt_min = ret;
246 perflvl->volt_max = ret;
247 }
Ben Skeggs330c5982010-09-16 15:39:49 +1000248 }
249
Martin Peresaa1b9b42012-09-02 02:55:58 +0200250 if (therm && therm->fan_get) {
251 ret = therm->fan_get(therm);
252 if (ret >= 0)
253 perflvl->fanspeed = ret;
254 }
Ben Skeggs771e1032011-07-28 11:01:21 +1000255
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000256 nouveau_mem_timing_read(dev, &perflvl->timing);
Ben Skeggs330c5982010-09-16 15:39:49 +1000257 return 0;
258}
259
260static void
261nouveau_pm_perflvl_info(struct nouveau_pm_level *perflvl, char *ptr, int len)
262{
Ben Skeggs085028c2012-01-18 09:02:28 +1000263 char c[16], s[16], v[32], f[16], m[16];
Francisco Jerez0fbb1142010-09-20 16:18:28 +0200264
265 c[0] = '\0';
266 if (perflvl->core)
267 snprintf(c, sizeof(c), " core %dMHz", perflvl->core / 1000);
Ben Skeggs330c5982010-09-16 15:39:49 +1000268
269 s[0] = '\0';
270 if (perflvl->shader)
271 snprintf(s, sizeof(s), " shader %dMHz", perflvl->shader / 1000);
272
Ben Skeggs93dccbe2011-06-09 17:27:47 +1000273 m[0] = '\0';
274 if (perflvl->memory)
275 snprintf(m, sizeof(m), " memory %dMHz", perflvl->memory / 1000);
276
Ben Skeggs330c5982010-09-16 15:39:49 +1000277 v[0] = '\0';
Ben Skeggs3b5565d2011-06-09 16:57:07 +1000278 if (perflvl->volt_min && perflvl->volt_min != perflvl->volt_max) {
279 snprintf(v, sizeof(v), " voltage %dmV-%dmV",
280 perflvl->volt_min / 1000, perflvl->volt_max / 1000);
281 } else
282 if (perflvl->volt_min) {
283 snprintf(v, sizeof(v), " voltage %dmV",
284 perflvl->volt_min / 1000);
285 }
Ben Skeggs330c5982010-09-16 15:39:49 +1000286
287 f[0] = '\0';
288 if (perflvl->fanspeed)
289 snprintf(f, sizeof(f), " fanspeed %d%%", perflvl->fanspeed);
290
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000291 snprintf(ptr, len, "%s%s%s%s%s\n", c, s, m, v, f);
Ben Skeggs330c5982010-09-16 15:39:49 +1000292}
293
294static ssize_t
295nouveau_pm_get_perflvl_info(struct device *d,
296 struct device_attribute *a, char *buf)
297{
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000298 struct nouveau_pm_level *perflvl =
299 container_of(a, struct nouveau_pm_level, dev_attr);
Ben Skeggs330c5982010-09-16 15:39:49 +1000300 char *ptr = buf;
301 int len = PAGE_SIZE;
302
Ben Skeggs93dccbe2011-06-09 17:27:47 +1000303 snprintf(ptr, len, "%d:", perflvl->id);
Ben Skeggs330c5982010-09-16 15:39:49 +1000304 ptr += strlen(buf);
305 len -= strlen(buf);
306
307 nouveau_pm_perflvl_info(perflvl, ptr, len);
308 return strlen(buf);
309}
310
311static ssize_t
312nouveau_pm_get_perflvl(struct device *d, struct device_attribute *a, char *buf)
313{
Martin Peres34e9d852010-09-22 20:54:22 +0200314 struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
Ben Skeggs77145f12012-07-31 16:16:21 +1000315 struct nouveau_pm *pm = nouveau_pm(dev);
Ben Skeggs330c5982010-09-16 15:39:49 +1000316 struct nouveau_pm_level cur;
317 int len = PAGE_SIZE, ret;
318 char *ptr = buf;
319
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000320 snprintf(ptr, len, "profile: %s, %s\nc:",
321 pm->profile_ac->name, pm->profile_dc->name);
Ben Skeggs330c5982010-09-16 15:39:49 +1000322 ptr += strlen(buf);
323 len -= strlen(buf);
324
325 ret = nouveau_pm_perflvl_get(dev, &cur);
326 if (ret == 0)
327 nouveau_pm_perflvl_info(&cur, ptr, len);
328 return strlen(buf);
329}
330
331static ssize_t
332nouveau_pm_set_perflvl(struct device *d, struct device_attribute *a,
333 const char *buf, size_t count)
334{
Martin Peres34e9d852010-09-22 20:54:22 +0200335 struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
Ben Skeggs6f876982010-09-16 16:47:14 +1000336 int ret;
337
338 ret = nouveau_pm_profile_set(dev, buf);
339 if (ret)
340 return ret;
341 return strlen(buf);
Ben Skeggs330c5982010-09-16 15:39:49 +1000342}
343
Francisco Jerez5c4abd02010-09-23 20:36:42 +0200344static DEVICE_ATTR(performance_level, S_IRUGO | S_IWUSR,
345 nouveau_pm_get_perflvl, nouveau_pm_set_perflvl);
Ben Skeggs330c5982010-09-16 15:39:49 +1000346
Martin Peres34e9d852010-09-22 20:54:22 +0200347static int
348nouveau_sysfs_init(struct drm_device *dev)
Ben Skeggs330c5982010-09-16 15:39:49 +1000349{
Ben Skeggs77145f12012-07-31 16:16:21 +1000350 struct nouveau_drm *drm = nouveau_drm(dev);
351 struct nouveau_pm *pm = nouveau_pm(dev);
Ben Skeggs330c5982010-09-16 15:39:49 +1000352 struct device *d = &dev->pdev->dev;
Ben Skeggs330c5982010-09-16 15:39:49 +1000353 int ret, i;
354
Ben Skeggs330c5982010-09-16 15:39:49 +1000355 ret = device_create_file(d, &dev_attr_performance_level);
356 if (ret)
357 return ret;
358
359 for (i = 0; i < pm->nr_perflvl; i++) {
360 struct nouveau_pm_level *perflvl = &pm->perflvl[i];
361
362 perflvl->dev_attr.attr.name = perflvl->name;
363 perflvl->dev_attr.attr.mode = S_IRUGO;
364 perflvl->dev_attr.show = nouveau_pm_get_perflvl_info;
365 perflvl->dev_attr.store = NULL;
366 sysfs_attr_init(&perflvl->dev_attr.attr);
367
368 ret = device_create_file(d, &perflvl->dev_attr);
369 if (ret) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000370 NV_ERROR(drm, "failed pervlvl %d sysfs: %d\n",
Ben Skeggs330c5982010-09-16 15:39:49 +1000371 perflvl->id, i);
372 perflvl->dev_attr.attr.name = NULL;
373 nouveau_pm_fini(dev);
374 return ret;
375 }
376 }
377
378 return 0;
379}
380
Martin Peres34e9d852010-09-22 20:54:22 +0200381static void
382nouveau_sysfs_fini(struct drm_device *dev)
Ben Skeggs330c5982010-09-16 15:39:49 +1000383{
Ben Skeggs77145f12012-07-31 16:16:21 +1000384 struct nouveau_pm *pm = nouveau_pm(dev);
Ben Skeggs330c5982010-09-16 15:39:49 +1000385 struct device *d = &dev->pdev->dev;
386 int i;
387
388 device_remove_file(d, &dev_attr_performance_level);
389 for (i = 0; i < pm->nr_perflvl; i++) {
390 struct nouveau_pm_level *pl = &pm->perflvl[i];
391
392 if (!pl->dev_attr.attr.name)
393 break;
394
395 device_remove_file(d, &pl->dev_attr);
396 }
Martin Peres34e9d852010-09-22 20:54:22 +0200397}
398
Ken Milmore658e86e2011-07-03 19:54:28 +0100399#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
Martin Peres34e9d852010-09-22 20:54:22 +0200400static ssize_t
401nouveau_hwmon_show_temp(struct device *d, struct device_attribute *a, char *buf)
402{
403 struct drm_device *dev = dev_get_drvdata(d);
Martin Peresaa1b9b42012-09-02 02:55:58 +0200404 struct nouveau_drm *drm = nouveau_drm(dev);
405 struct nouveau_therm *therm = nouveau_therm(drm->device);
Martin Peres34e9d852010-09-22 20:54:22 +0200406
Martin Peresaa1b9b42012-09-02 02:55:58 +0200407 return snprintf(buf, PAGE_SIZE, "%d\n", therm->temp_get(therm) * 1000);
Martin Peres34e9d852010-09-22 20:54:22 +0200408}
409static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, nouveau_hwmon_show_temp,
410 NULL, 0);
411
412static ssize_t
413nouveau_hwmon_max_temp(struct device *d, struct device_attribute *a, char *buf)
414{
415 struct drm_device *dev = dev_get_drvdata(d);
Martin Peresaa1b9b42012-09-02 02:55:58 +0200416 struct nouveau_drm *drm = nouveau_drm(dev);
417 struct nouveau_therm *therm = nouveau_therm(drm->device);
Martin Peres34e9d852010-09-22 20:54:22 +0200418
Martin Peresaa1b9b42012-09-02 02:55:58 +0200419 return snprintf(buf, PAGE_SIZE, "%d\n",
420 therm->attr_get(therm, NOUVEAU_THERM_ATTR_THRS_DOWN_CLK) * 1000);
Martin Peres34e9d852010-09-22 20:54:22 +0200421}
422static ssize_t
423nouveau_hwmon_set_max_temp(struct device *d, struct device_attribute *a,
424 const char *buf, size_t count)
425{
426 struct drm_device *dev = dev_get_drvdata(d);
Martin Peresaa1b9b42012-09-02 02:55:58 +0200427 struct nouveau_drm *drm = nouveau_drm(dev);
428 struct nouveau_therm *therm = nouveau_therm(drm->device);
Martin Peres34e9d852010-09-22 20:54:22 +0200429 long value;
430
Martin Peresddb20052011-12-17 12:24:59 +0100431 if (kstrtol(buf, 10, &value) == -EINVAL)
Martin Peres34e9d852010-09-22 20:54:22 +0200432 return count;
433
Martin Peresaa1b9b42012-09-02 02:55:58 +0200434 therm->attr_set(therm, NOUVEAU_THERM_ATTR_THRS_DOWN_CLK, value / 1000);
Martin Peres34e9d852010-09-22 20:54:22 +0200435
436 return count;
437}
438static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, nouveau_hwmon_max_temp,
439 nouveau_hwmon_set_max_temp,
440 0);
441
442static ssize_t
443nouveau_hwmon_critical_temp(struct device *d, struct device_attribute *a,
444 char *buf)
445{
446 struct drm_device *dev = dev_get_drvdata(d);
Martin Peresaa1b9b42012-09-02 02:55:58 +0200447 struct nouveau_drm *drm = nouveau_drm(dev);
448 struct nouveau_therm *therm = nouveau_therm(drm->device);
Martin Peres34e9d852010-09-22 20:54:22 +0200449
Martin Peresaa1b9b42012-09-02 02:55:58 +0200450 return snprintf(buf, PAGE_SIZE, "%d\n",
451 therm->attr_get(therm, NOUVEAU_THERM_ATTR_THRS_CRITICAL) * 1000);
Martin Peres34e9d852010-09-22 20:54:22 +0200452}
453static ssize_t
454nouveau_hwmon_set_critical_temp(struct device *d, struct device_attribute *a,
455 const char *buf,
456 size_t count)
457{
458 struct drm_device *dev = dev_get_drvdata(d);
Martin Peresaa1b9b42012-09-02 02:55:58 +0200459 struct nouveau_drm *drm = nouveau_drm(dev);
460 struct nouveau_therm *therm = nouveau_therm(drm->device);
Martin Peres34e9d852010-09-22 20:54:22 +0200461 long value;
462
Martin Peresddb20052011-12-17 12:24:59 +0100463 if (kstrtol(buf, 10, &value) == -EINVAL)
Martin Peres34e9d852010-09-22 20:54:22 +0200464 return count;
465
Martin Peresaa1b9b42012-09-02 02:55:58 +0200466 therm->attr_set(therm, NOUVEAU_THERM_ATTR_THRS_CRITICAL, value / 1000);
Martin Peres34e9d852010-09-22 20:54:22 +0200467
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
Martin Peres11b7d8952011-08-15 11:10:30 +1000493static 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);
Ben Skeggs77145f12012-07-31 16:16:21 +1000498 struct nouveau_drm *drm = nouveau_drm(dev);
Martin Peresaa1b9b42012-09-02 02:55:58 +0200499 struct nouveau_therm *therm = nouveau_therm(drm->device);
Martin Peres11b7d8952011-08-15 11:10:30 +1000500
Martin Peresaa1b9b42012-09-02 02:55:58 +0200501 return snprintf(buf, PAGE_SIZE, "%d\n", therm->fan_sense(therm));
Martin Peres11b7d8952011-08-15 11:10:30 +1000502}
503static SENSOR_DEVICE_ATTR(fan0_input, S_IRUGO, nouveau_hwmon_show_fan0_input,
504 NULL, 0);
505
506static ssize_t
Martin Peresc9cbf132012-09-04 13:39:40 +0200507nouveau_hwmon_get_pwm1(struct device *d, struct device_attribute *a, char *buf)
Martin Peres11b7d8952011-08-15 11:10:30 +1000508{
509 struct drm_device *dev = dev_get_drvdata(d);
Martin Peresaa1b9b42012-09-02 02:55:58 +0200510 struct nouveau_drm *drm = nouveau_drm(dev);
511 struct nouveau_therm *therm = nouveau_therm(drm->device);
Ben Skeggsa1750942011-09-17 01:42:12 +1000512 int ret;
Martin Peres11b7d8952011-08-15 11:10:30 +1000513
Martin Peresaa1b9b42012-09-02 02:55:58 +0200514 ret = therm->fan_get(therm);
Martin Peres11b7d8952011-08-15 11:10:30 +1000515 if (ret < 0)
516 return ret;
517
518 return sprintf(buf, "%i\n", ret);
519}
520
521static ssize_t
Martin Peresc9cbf132012-09-04 13:39:40 +0200522nouveau_hwmon_set_pwm1(struct device *d, struct device_attribute *a,
Martin Peres11b7d8952011-08-15 11:10:30 +1000523 const char *buf, size_t count)
524{
525 struct drm_device *dev = dev_get_drvdata(d);
Martin Peresaa1b9b42012-09-02 02:55:58 +0200526 struct nouveau_drm *drm = nouveau_drm(dev);
527 struct nouveau_therm *therm = nouveau_therm(drm->device);
Martin Peres11b7d8952011-08-15 11:10:30 +1000528 int ret = -ENODEV;
529 long value;
530
531 if (nouveau_perflvl_wr != 7777)
532 return -EPERM;
533
Martin Peresddb20052011-12-17 12:24:59 +0100534 if (kstrtol(buf, 10, &value) == -EINVAL)
Martin Peres11b7d8952011-08-15 11:10:30 +1000535 return -EINVAL;
536
Martin Peresaa1b9b42012-09-02 02:55:58 +0200537 ret = therm->fan_set(therm, value);
Martin Peres11b7d8952011-08-15 11:10:30 +1000538 if (ret)
539 return ret;
540
541 return count;
542}
543
Martin Peresc9cbf132012-09-04 13:39:40 +0200544static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR,
545 nouveau_hwmon_get_pwm1,
546 nouveau_hwmon_set_pwm1, 0);
Martin Peres11b7d8952011-08-15 11:10:30 +1000547
548static ssize_t
Martin Peresc9cbf132012-09-04 13:39:40 +0200549nouveau_hwmon_get_pwm1_min(struct device *d,
Martin Peres11b7d8952011-08-15 11:10:30 +1000550 struct device_attribute *a, char *buf)
551{
552 struct drm_device *dev = dev_get_drvdata(d);
Martin Peresaa1b9b42012-09-02 02:55:58 +0200553 struct nouveau_drm *drm = nouveau_drm(dev);
554 struct nouveau_therm *therm = nouveau_therm(drm->device);
555 int ret;
Martin Peres11b7d8952011-08-15 11:10:30 +1000556
Martin Peresaa1b9b42012-09-02 02:55:58 +0200557 ret = therm->attr_get(therm, NOUVEAU_THERM_ATTR_FAN_MIN_DUTY);
558 if (ret < 0)
559 return ret;
560
561 return sprintf(buf, "%i\n", ret);
Martin Peres11b7d8952011-08-15 11:10:30 +1000562}
563
564static ssize_t
Martin Peresc9cbf132012-09-04 13:39:40 +0200565nouveau_hwmon_set_pwm1_min(struct device *d, struct device_attribute *a,
Martin Peres11b7d8952011-08-15 11:10:30 +1000566 const char *buf, size_t count)
567{
568 struct drm_device *dev = dev_get_drvdata(d);
Martin Peresaa1b9b42012-09-02 02:55:58 +0200569 struct nouveau_drm *drm = nouveau_drm(dev);
570 struct nouveau_therm *therm = nouveau_therm(drm->device);
Martin Peres11b7d8952011-08-15 11:10:30 +1000571 long value;
Martin Peresaa1b9b42012-09-02 02:55:58 +0200572 int ret;
Martin Peres11b7d8952011-08-15 11:10:30 +1000573
Martin Peresddb20052011-12-17 12:24:59 +0100574 if (kstrtol(buf, 10, &value) == -EINVAL)
Martin Peres11b7d8952011-08-15 11:10:30 +1000575 return -EINVAL;
576
Martin Peresaa1b9b42012-09-02 02:55:58 +0200577 ret = therm->attr_set(therm, NOUVEAU_THERM_ATTR_FAN_MIN_DUTY, value);
578 if (ret < 0)
579 return ret;
Martin Peres11b7d8952011-08-15 11:10:30 +1000580
581 return count;
582}
583
Martin Peresc9cbf132012-09-04 13:39:40 +0200584static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO | S_IWUSR,
585 nouveau_hwmon_get_pwm1_min,
586 nouveau_hwmon_set_pwm1_min, 0);
Martin Peres11b7d8952011-08-15 11:10:30 +1000587
588static ssize_t
Martin Peresc9cbf132012-09-04 13:39:40 +0200589nouveau_hwmon_get_pwm1_max(struct device *d,
Martin Peres11b7d8952011-08-15 11:10:30 +1000590 struct device_attribute *a, char *buf)
591{
592 struct drm_device *dev = dev_get_drvdata(d);
Martin Peresaa1b9b42012-09-02 02:55:58 +0200593 struct nouveau_drm *drm = nouveau_drm(dev);
594 struct nouveau_therm *therm = nouveau_therm(drm->device);
595 int ret;
Martin Peres11b7d8952011-08-15 11:10:30 +1000596
Martin Peresaa1b9b42012-09-02 02:55:58 +0200597 ret = therm->attr_get(therm, NOUVEAU_THERM_ATTR_FAN_MAX_DUTY);
598 if (ret < 0)
599 return ret;
600
601 return sprintf(buf, "%i\n", ret);
Martin Peres11b7d8952011-08-15 11:10:30 +1000602}
603
604static ssize_t
Martin Peresc9cbf132012-09-04 13:39:40 +0200605nouveau_hwmon_set_pwm1_max(struct device *d, struct device_attribute *a,
Martin Peres11b7d8952011-08-15 11:10:30 +1000606 const char *buf, size_t count)
607{
608 struct drm_device *dev = dev_get_drvdata(d);
Martin Peresaa1b9b42012-09-02 02:55:58 +0200609 struct nouveau_drm *drm = nouveau_drm(dev);
610 struct nouveau_therm *therm = nouveau_therm(drm->device);
Martin Peres11b7d8952011-08-15 11:10:30 +1000611 long value;
Martin Peresaa1b9b42012-09-02 02:55:58 +0200612 int ret;
Martin Peres11b7d8952011-08-15 11:10:30 +1000613
Martin Peresddb20052011-12-17 12:24:59 +0100614 if (kstrtol(buf, 10, &value) == -EINVAL)
Martin Peres11b7d8952011-08-15 11:10:30 +1000615 return -EINVAL;
616
Martin Peresaa1b9b42012-09-02 02:55:58 +0200617 ret = therm->attr_set(therm, NOUVEAU_THERM_ATTR_FAN_MAX_DUTY, value);
618 if (ret < 0)
619 return ret;
Martin Peres11b7d8952011-08-15 11:10:30 +1000620
621 return count;
622}
623
Martin Peresc9cbf132012-09-04 13:39:40 +0200624static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO | S_IWUSR,
625 nouveau_hwmon_get_pwm1_max,
626 nouveau_hwmon_set_pwm1_max, 0);
Martin Peres11b7d8952011-08-15 11:10:30 +1000627
Martin Peres34e9d852010-09-22 20:54:22 +0200628static struct attribute *hwmon_attributes[] = {
629 &sensor_dev_attr_temp1_input.dev_attr.attr,
630 &sensor_dev_attr_temp1_max.dev_attr.attr,
631 &sensor_dev_attr_temp1_crit.dev_attr.attr,
632 &sensor_dev_attr_name.dev_attr.attr,
633 &sensor_dev_attr_update_rate.dev_attr.attr,
634 NULL
635};
Martin Peres11b7d8952011-08-15 11:10:30 +1000636static struct attribute *hwmon_fan_rpm_attributes[] = {
637 &sensor_dev_attr_fan0_input.dev_attr.attr,
638 NULL
639};
640static struct attribute *hwmon_pwm_fan_attributes[] = {
Martin Peresc9cbf132012-09-04 13:39:40 +0200641 &sensor_dev_attr_pwm1.dev_attr.attr,
642 &sensor_dev_attr_pwm1_min.dev_attr.attr,
643 &sensor_dev_attr_pwm1_max.dev_attr.attr,
Martin Peres11b7d8952011-08-15 11:10:30 +1000644 NULL
645};
Martin Peres34e9d852010-09-22 20:54:22 +0200646
647static const struct attribute_group hwmon_attrgroup = {
648 .attrs = hwmon_attributes,
649};
Martin Peres11b7d8952011-08-15 11:10:30 +1000650static const struct attribute_group hwmon_fan_rpm_attrgroup = {
651 .attrs = hwmon_fan_rpm_attributes,
652};
653static const struct attribute_group hwmon_pwm_fan_attrgroup = {
654 .attrs = hwmon_pwm_fan_attributes,
655};
Martin Peresb54262f2010-10-26 12:48:28 +0200656#endif
Martin Peres34e9d852010-09-22 20:54:22 +0200657
658static int
659nouveau_hwmon_init(struct drm_device *dev)
660{
Ben Skeggs77145f12012-07-31 16:16:21 +1000661 struct nouveau_pm *pm = nouveau_pm(dev);
662 struct nouveau_drm *drm = nouveau_drm(dev);
Martin Peresaa1b9b42012-09-02 02:55:58 +0200663 struct nouveau_therm *therm = nouveau_therm(drm->device);
Ben Skeggs77145f12012-07-31 16:16:21 +1000664
Dave Airlie095f9792012-01-10 10:13:16 +0000665#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
Martin Peres34e9d852010-09-22 20:54:22 +0200666 struct device *hwmon_dev;
Martin Peres11b7d8952011-08-15 11:10:30 +1000667 int ret = 0;
Martin Peres34e9d852010-09-22 20:54:22 +0200668
Martin Peresaa1b9b42012-09-02 02:55:58 +0200669 if (!therm || !therm->temp_get || !therm->attr_get ||
670 !therm->attr_set || therm->temp_get(therm) < 0)
Francisco Jerez8155cac2010-09-23 20:58:38 +0200671 return -ENODEV;
Martin Peres34e9d852010-09-22 20:54:22 +0200672
673 hwmon_dev = hwmon_device_register(&dev->pdev->dev);
674 if (IS_ERR(hwmon_dev)) {
675 ret = PTR_ERR(hwmon_dev);
Ben Skeggs77145f12012-07-31 16:16:21 +1000676 NV_ERROR(drm, "Unable to register hwmon device: %d\n", ret);
Martin Peres34e9d852010-09-22 20:54:22 +0200677 return ret;
678 }
679 dev_set_drvdata(hwmon_dev, dev);
Martin Peres11b7d8952011-08-15 11:10:30 +1000680
681 /* default sysfs entries */
Lucas Stach07cfe0e2011-01-06 20:29:53 +0100682 ret = sysfs_create_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
Martin Peres34e9d852010-09-22 20:54:22 +0200683 if (ret) {
Martin Peres11b7d8952011-08-15 11:10:30 +1000684 if (ret)
685 goto error;
686 }
687
688 /* if the card has a pwm fan */
689 /*XXX: incorrect, need better detection for this, some boards have
690 * the gpio entries for pwm fan control even when there's no
691 * actual fan connected to it... therm table? */
Martin Peresaa1b9b42012-09-02 02:55:58 +0200692 if (therm->fan_get && therm->fan_get(therm) >= 0) {
Martin Peres11b7d8952011-08-15 11:10:30 +1000693 ret = sysfs_create_group(&dev->pdev->dev.kobj,
694 &hwmon_pwm_fan_attrgroup);
695 if (ret)
696 goto error;
697 }
698
699 /* if the card can read the fan rpm */
Martin Peresaa1b9b42012-09-02 02:55:58 +0200700 if (therm->fan_sense(therm) >= 0) {
Martin Peres11b7d8952011-08-15 11:10:30 +1000701 ret = sysfs_create_group(&dev->pdev->dev.kobj,
702 &hwmon_fan_rpm_attrgroup);
703 if (ret)
704 goto error;
Martin Peres34e9d852010-09-22 20:54:22 +0200705 }
706
Francisco Jerez8155cac2010-09-23 20:58:38 +0200707 pm->hwmon = hwmon_dev;
Martin Peres11b7d8952011-08-15 11:10:30 +1000708
Martin Peres34e9d852010-09-22 20:54:22 +0200709 return 0;
Martin Peres11b7d8952011-08-15 11:10:30 +1000710
711error:
Ben Skeggs77145f12012-07-31 16:16:21 +1000712 NV_ERROR(drm, "Unable to create some hwmon sysfs files: %d\n", ret);
Martin Peres11b7d8952011-08-15 11:10:30 +1000713 hwmon_device_unregister(hwmon_dev);
714 pm->hwmon = NULL;
715 return ret;
716#else
717 pm->hwmon = NULL;
718 return 0;
719#endif
Martin Peres34e9d852010-09-22 20:54:22 +0200720}
721
722static void
723nouveau_hwmon_fini(struct drm_device *dev)
724{
Ken Milmore658e86e2011-07-03 19:54:28 +0100725#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
Ben Skeggs77145f12012-07-31 16:16:21 +1000726 struct nouveau_pm *pm = nouveau_pm(dev);
Martin Peres34e9d852010-09-22 20:54:22 +0200727
Francisco Jerez8155cac2010-09-23 20:58:38 +0200728 if (pm->hwmon) {
Lucas Stach8c06a3e2011-01-30 10:54:11 +0100729 sysfs_remove_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
Martin Peresddb20052011-12-17 12:24:59 +0100730 sysfs_remove_group(&dev->pdev->dev.kobj,
731 &hwmon_pwm_fan_attrgroup);
732 sysfs_remove_group(&dev->pdev->dev.kobj,
733 &hwmon_fan_rpm_attrgroup);
Martin Peres11b7d8952011-08-15 11:10:30 +1000734
Francisco Jerez8155cac2010-09-23 20:58:38 +0200735 hwmon_device_unregister(pm->hwmon);
Martin Peres34e9d852010-09-22 20:54:22 +0200736 }
Martin Peresb54262f2010-10-26 12:48:28 +0200737#endif
Martin Peres34e9d852010-09-22 20:54:22 +0200738}
739
Martin Peres1f962792011-04-12 00:55:44 +0200740#if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
Ben Skeggs60326492010-10-12 12:31:32 +1000741static int
742nouveau_pm_acpi_event(struct notifier_block *nb, unsigned long val, void *data)
743{
Ben Skeggs77145f12012-07-31 16:16:21 +1000744 struct nouveau_pm *pm = container_of(nb, struct nouveau_pm, acpi_nb);
745 struct nouveau_drm *drm = nouveau_drm(pm->dev);
Ben Skeggs60326492010-10-12 12:31:32 +1000746 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
747
748 if (strcmp(entry->device_class, "ac_adapter") == 0) {
749 bool ac = power_supply_is_system_supplied();
750
Ben Skeggs77145f12012-07-31 16:16:21 +1000751 NV_DEBUG(drm, "power supply changed: %s\n", ac ? "AC" : "DC");
752 nouveau_pm_trigger(pm->dev);
Ben Skeggs60326492010-10-12 12:31:32 +1000753 }
754
755 return NOTIFY_OK;
756}
757#endif
758
Martin Peres34e9d852010-09-22 20:54:22 +0200759int
760nouveau_pm_init(struct drm_device *dev)
761{
Ben Skeggs77145f12012-07-31 16:16:21 +1000762 struct nouveau_device *device = nouveau_dev(dev);
763 struct nouveau_drm *drm = nouveau_drm(dev);
764 struct nouveau_pm *pm;
Martin Peres34e9d852010-09-22 20:54:22 +0200765 char info[256];
766 int ret, i;
767
Ben Skeggs77145f12012-07-31 16:16:21 +1000768 pm = drm->pm = kzalloc(sizeof(*pm), GFP_KERNEL);
769 if (!pm)
770 return -ENOMEM;
771
772 pm->dev = dev;
773
774 if (device->card_type < NV_40) {
775 pm->clocks_get = nv04_pm_clocks_get;
776 pm->clocks_pre = nv04_pm_clocks_pre;
777 pm->clocks_set = nv04_pm_clocks_set;
778 if (nouveau_gpio(drm->device)) {
779 pm->voltage_get = nouveau_voltage_gpio_get;
780 pm->voltage_set = nouveau_voltage_gpio_set;
781 }
782 } else
783 if (device->card_type < NV_50) {
784 pm->clocks_get = nv40_pm_clocks_get;
785 pm->clocks_pre = nv40_pm_clocks_pre;
786 pm->clocks_set = nv40_pm_clocks_set;
787 pm->voltage_get = nouveau_voltage_gpio_get;
788 pm->voltage_set = nouveau_voltage_gpio_set;
Ben Skeggs77145f12012-07-31 16:16:21 +1000789 } else
790 if (device->card_type < NV_C0) {
791 if (device->chipset < 0xa3 ||
792 device->chipset == 0xaa ||
793 device->chipset == 0xac) {
794 pm->clocks_get = nv50_pm_clocks_get;
795 pm->clocks_pre = nv50_pm_clocks_pre;
796 pm->clocks_set = nv50_pm_clocks_set;
797 } else {
798 pm->clocks_get = nva3_pm_clocks_get;
799 pm->clocks_pre = nva3_pm_clocks_pre;
800 pm->clocks_set = nva3_pm_clocks_set;
801 }
802 pm->voltage_get = nouveau_voltage_gpio_get;
803 pm->voltage_set = nouveau_voltage_gpio_set;
Ben Skeggs77145f12012-07-31 16:16:21 +1000804 } else
805 if (device->card_type < NV_E0) {
806 pm->clocks_get = nvc0_pm_clocks_get;
807 pm->clocks_pre = nvc0_pm_clocks_pre;
808 pm->clocks_set = nvc0_pm_clocks_set;
809 pm->voltage_get = nouveau_voltage_gpio_get;
810 pm->voltage_set = nouveau_voltage_gpio_set;
Ben Skeggs77145f12012-07-31 16:16:21 +1000811 }
812
813
Ben Skeggs68a64ca2012-01-23 13:47:02 +1000814 /* parse aux tables from vbios */
Martin Peres34e9d852010-09-22 20:54:22 +0200815 nouveau_volt_init(dev);
Martin Peres34e9d852010-09-22 20:54:22 +0200816
Dmitry Eremin-Solenikovd89c8ce2012-09-25 10:31:40 +0400817 INIT_LIST_HEAD(&pm->profiles);
818
Ben Skeggs68a64ca2012-01-23 13:47:02 +1000819 /* determine current ("boot") performance level */
820 ret = nouveau_pm_perflvl_get(dev, &pm->boot);
821 if (ret) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000822 NV_ERROR(drm, "failed to determine boot perflvl\n");
Ben Skeggs68a64ca2012-01-23 13:47:02 +1000823 return ret;
824 }
825
826 strncpy(pm->boot.name, "boot", 4);
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000827 strncpy(pm->boot.profile.name, "boot", 4);
828 pm->boot.profile.func = &nouveau_pm_static_profile_func;
829
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000830 list_add(&pm->boot.profile.head, &pm->profiles);
831
832 pm->profile_ac = &pm->boot.profile;
833 pm->profile_dc = &pm->boot.profile;
Ben Skeggs25c53c12012-01-24 18:03:25 +1000834 pm->profile = &pm->boot.profile;
Ben Skeggs68a64ca2012-01-23 13:47:02 +1000835 pm->cur = &pm->boot;
836
837 /* add performance levels from vbios */
838 nouveau_perf_init(dev);
839
840 /* display available performance levels */
Ben Skeggs77145f12012-07-31 16:16:21 +1000841 NV_INFO(drm, "%d available performance level(s)\n", pm->nr_perflvl);
Martin Peres34e9d852010-09-22 20:54:22 +0200842 for (i = 0; i < pm->nr_perflvl; i++) {
843 nouveau_pm_perflvl_info(&pm->perflvl[i], info, sizeof(info));
Ben Skeggs77145f12012-07-31 16:16:21 +1000844 NV_INFO(drm, "%d:%s", pm->perflvl[i].id, info);
Martin Peres34e9d852010-09-22 20:54:22 +0200845 }
846
Ben Skeggs68a64ca2012-01-23 13:47:02 +1000847 nouveau_pm_perflvl_info(&pm->boot, info, sizeof(info));
Ben Skeggs77145f12012-07-31 16:16:21 +1000848 NV_INFO(drm, "c:%s", info);
Martin Peres34e9d852010-09-22 20:54:22 +0200849
850 /* switch performance levels now if requested */
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000851 if (nouveau_perflvl != NULL)
852 nouveau_pm_profile_set(dev, nouveau_perflvl);
Martin Peres34e9d852010-09-22 20:54:22 +0200853
854 nouveau_sysfs_init(dev);
855 nouveau_hwmon_init(dev);
Martin Peres1f962792011-04-12 00:55:44 +0200856#if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
Ben Skeggs60326492010-10-12 12:31:32 +1000857 pm->acpi_nb.notifier_call = nouveau_pm_acpi_event;
858 register_acpi_notifier(&pm->acpi_nb);
859#endif
Martin Peres34e9d852010-09-22 20:54:22 +0200860
861 return 0;
862}
863
864void
865nouveau_pm_fini(struct drm_device *dev)
866{
Ben Skeggs77145f12012-07-31 16:16:21 +1000867 struct nouveau_pm *pm = nouveau_pm(dev);
Ben Skeggs25c53c12012-01-24 18:03:25 +1000868 struct nouveau_pm_profile *profile, *tmp;
869
870 list_for_each_entry_safe(profile, tmp, &pm->profiles, head) {
871 list_del(&profile->head);
872 profile->func->destroy(profile);
873 }
Martin Peres34e9d852010-09-22 20:54:22 +0200874
875 if (pm->cur != &pm->boot)
876 nouveau_pm_perflvl_set(dev, &pm->boot);
Ben Skeggs330c5982010-09-16 15:39:49 +1000877
878 nouveau_perf_fini(dev);
879 nouveau_volt_fini(dev);
Martin Peres34e9d852010-09-22 20:54:22 +0200880
Martin Peres1f962792011-04-12 00:55:44 +0200881#if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
Ben Skeggs60326492010-10-12 12:31:32 +1000882 unregister_acpi_notifier(&pm->acpi_nb);
883#endif
Martin Peres34e9d852010-09-22 20:54:22 +0200884 nouveau_hwmon_fini(dev);
885 nouveau_sysfs_fini(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +1000886
887 nouveau_drm(dev)->pm = NULL;
888 kfree(pm);
Ben Skeggs330c5982010-09-16 15:39:49 +1000889}
890
Ben Skeggs64f1c112010-09-17 13:35:25 +1000891void
892nouveau_pm_resume(struct drm_device *dev)
893{
Ben Skeggs77145f12012-07-31 16:16:21 +1000894 struct nouveau_pm *pm = nouveau_pm(dev);
Ben Skeggs64f1c112010-09-17 13:35:25 +1000895 struct nouveau_pm_level *perflvl;
896
Ben Skeggs317495b2011-02-17 11:11:28 +1000897 if (!pm->cur || pm->cur == &pm->boot)
Ben Skeggs64f1c112010-09-17 13:35:25 +1000898 return;
899
900 perflvl = pm->cur;
901 pm->cur = &pm->boot;
902 nouveau_pm_perflvl_set(dev, perflvl);
903}