blob: a07f27447cf9bc66ee5ff014cd5f726a4aae7101 [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
30static int
Ben Skeggs6f876982010-09-16 16:47:14 +100031nouveau_pm_clock_set(struct drm_device *dev, u8 id, u32 khz)
32{
33 struct drm_nouveau_private *dev_priv = dev->dev_private;
34 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
35 void *pre_state;
36
37 if (khz == 0)
38 return 0;
39
40 pre_state = pm->clock_pre(dev, id, khz);
41 if (IS_ERR(pre_state))
42 return PTR_ERR(pre_state);
43
44 if (pre_state)
45 pm->clock_set(dev, pre_state);
46 return 0;
47}
48
49static int
Ben Skeggs64f1c112010-09-17 13:35:25 +100050nouveau_pm_perflvl_set(struct drm_device *dev, struct nouveau_pm_level *perflvl)
51{
52 struct drm_nouveau_private *dev_priv = dev->dev_private;
53 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
54 int ret;
55
56 if (perflvl == pm->cur)
57 return 0;
58
59 if (pm->voltage.supported && pm->voltage_set && perflvl->voltage) {
60 ret = pm->voltage_set(dev, perflvl->voltage);
61 if (ret) {
62 NV_ERROR(dev, "voltage_set %d failed: %d\n",
63 perflvl->voltage, ret);
64 }
65 }
66
67 nouveau_pm_clock_set(dev, PLL_CORE, perflvl->core);
68 nouveau_pm_clock_set(dev, PLL_SHADER, perflvl->shader);
69 nouveau_pm_clock_set(dev, PLL_MEMORY, perflvl->memory);
70 nouveau_pm_clock_set(dev, PLL_UNK05, perflvl->unk05);
71
72 pm->cur = perflvl;
73 return 0;
74}
75
76static int
Ben Skeggs6f876982010-09-16 16:47:14 +100077nouveau_pm_profile_set(struct drm_device *dev, const char *profile)
78{
79 struct drm_nouveau_private *dev_priv = dev->dev_private;
80 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
81 struct nouveau_pm_level *perflvl = NULL;
Ben Skeggs6f876982010-09-16 16:47:14 +100082
83 /* safety precaution, for now */
84 if (nouveau_perflvl_wr != 7777)
85 return -EPERM;
86
87 if (!pm->clock_set)
88 return -EINVAL;
89
90 if (!strncmp(profile, "boot", 4))
91 perflvl = &pm->boot;
92 else {
93 int pl = simple_strtol(profile, NULL, 10);
94 int i;
95
96 for (i = 0; i < pm->nr_perflvl; i++) {
97 if (pm->perflvl[i].id == pl) {
98 perflvl = &pm->perflvl[i];
99 break;
100 }
101 }
102
103 if (!perflvl)
104 return -EINVAL;
105 }
106
Ben Skeggs6f876982010-09-16 16:47:14 +1000107 NV_INFO(dev, "setting performance level: %s\n", profile);
Ben Skeggs64f1c112010-09-17 13:35:25 +1000108 return nouveau_pm_perflvl_set(dev, perflvl);
Ben Skeggs6f876982010-09-16 16:47:14 +1000109}
110
111static int
Ben Skeggs330c5982010-09-16 15:39:49 +1000112nouveau_pm_perflvl_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
113{
114 struct drm_nouveau_private *dev_priv = dev->dev_private;
115 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
116 int ret;
117
118 if (!pm->clock_get)
119 return -EINVAL;
120
121 memset(perflvl, 0, sizeof(*perflvl));
122
123 ret = pm->clock_get(dev, PLL_CORE);
124 if (ret > 0)
125 perflvl->core = ret;
126
127 ret = pm->clock_get(dev, PLL_MEMORY);
128 if (ret > 0)
129 perflvl->memory = ret;
130
131 ret = pm->clock_get(dev, PLL_SHADER);
132 if (ret > 0)
133 perflvl->shader = ret;
134
135 ret = pm->clock_get(dev, PLL_UNK05);
136 if (ret > 0)
137 perflvl->unk05 = ret;
138
139 if (pm->voltage.supported && pm->voltage_get) {
140 ret = pm->voltage_get(dev);
141 if (ret > 0)
142 perflvl->voltage = ret;
143 }
144
145 return 0;
146}
147
148static void
149nouveau_pm_perflvl_info(struct nouveau_pm_level *perflvl, char *ptr, int len)
150{
Francisco Jerez0fbb1142010-09-20 16:18:28 +0200151 char c[16], s[16], v[16], f[16];
152
153 c[0] = '\0';
154 if (perflvl->core)
155 snprintf(c, sizeof(c), " core %dMHz", perflvl->core / 1000);
Ben Skeggs330c5982010-09-16 15:39:49 +1000156
157 s[0] = '\0';
158 if (perflvl->shader)
159 snprintf(s, sizeof(s), " shader %dMHz", perflvl->shader / 1000);
160
161 v[0] = '\0';
162 if (perflvl->voltage)
163 snprintf(v, sizeof(v), " voltage %dmV", perflvl->voltage * 10);
164
165 f[0] = '\0';
166 if (perflvl->fanspeed)
167 snprintf(f, sizeof(f), " fanspeed %d%%", perflvl->fanspeed);
168
Francisco Jerez0fbb1142010-09-20 16:18:28 +0200169 snprintf(ptr, len, "memory %dMHz%s%s%s%s\n", perflvl->memory / 1000,
170 c, s, v, f);
Ben Skeggs330c5982010-09-16 15:39:49 +1000171}
172
173static ssize_t
174nouveau_pm_get_perflvl_info(struct device *d,
175 struct device_attribute *a, char *buf)
176{
177 struct nouveau_pm_level *perflvl = (struct nouveau_pm_level *)a;
178 char *ptr = buf;
179 int len = PAGE_SIZE;
180
181 snprintf(ptr, len, "%d: ", perflvl->id);
182 ptr += strlen(buf);
183 len -= strlen(buf);
184
185 nouveau_pm_perflvl_info(perflvl, ptr, len);
186 return strlen(buf);
187}
188
189static ssize_t
190nouveau_pm_get_perflvl(struct device *d, struct device_attribute *a, char *buf)
191{
192 struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
193 struct drm_nouveau_private *dev_priv = dev->dev_private;
194 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
195 struct nouveau_pm_level cur;
196 int len = PAGE_SIZE, ret;
197 char *ptr = buf;
198
199 if (!pm->cur)
200 snprintf(ptr, len, "setting: boot\n");
201 else if (pm->cur == &pm->boot)
202 snprintf(ptr, len, "setting: boot\nc: ");
203 else
204 snprintf(ptr, len, "setting: static %d\nc: ", pm->cur->id);
205 ptr += strlen(buf);
206 len -= strlen(buf);
207
208 ret = nouveau_pm_perflvl_get(dev, &cur);
209 if (ret == 0)
210 nouveau_pm_perflvl_info(&cur, ptr, len);
211 return strlen(buf);
212}
213
214static ssize_t
215nouveau_pm_set_perflvl(struct device *d, struct device_attribute *a,
216 const char *buf, size_t count)
217{
Ben Skeggs6f876982010-09-16 16:47:14 +1000218 struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
219 int ret;
220
221 ret = nouveau_pm_profile_set(dev, buf);
222 if (ret)
223 return ret;
224 return strlen(buf);
Ben Skeggs330c5982010-09-16 15:39:49 +1000225}
226
227DEVICE_ATTR(performance_level, S_IRUGO | S_IWUSR,
228 nouveau_pm_get_perflvl, nouveau_pm_set_perflvl);
229
230int
231nouveau_pm_init(struct drm_device *dev)
232{
233 struct drm_nouveau_private *dev_priv = dev->dev_private;
234 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
235 struct device *d = &dev->pdev->dev;
236 char info[256];
237 int ret, i;
238
239 nouveau_volt_init(dev);
240 nouveau_perf_init(dev);
241
242 NV_INFO(dev, "%d available performance level(s)\n", pm->nr_perflvl);
243 for (i = 0; i < pm->nr_perflvl; i++) {
244 nouveau_pm_perflvl_info(&pm->perflvl[i], info, sizeof(info));
245 NV_INFO(dev, "%d: %s", pm->perflvl[i].id, info);
246 }
247
248 /* determine current ("boot") performance level */
249 ret = nouveau_pm_perflvl_get(dev, &pm->boot);
250 if (ret == 0) {
251 pm->cur = &pm->boot;
252
253 nouveau_pm_perflvl_info(&pm->boot, info, sizeof(info));
254 NV_INFO(dev, "c: %s", info);
255 }
256
Ben Skeggs6f876982010-09-16 16:47:14 +1000257 /* switch performance levels now if requested */
258 if (nouveau_perflvl != NULL) {
259 ret = nouveau_pm_profile_set(dev, nouveau_perflvl);
260 if (ret) {
261 NV_ERROR(dev, "error setting perflvl \"%s\": %d\n",
262 nouveau_perflvl, ret);
263 }
264 }
265
Ben Skeggs330c5982010-09-16 15:39:49 +1000266 /* initialise sysfs */
267 ret = device_create_file(d, &dev_attr_performance_level);
268 if (ret)
269 return ret;
270
271 for (i = 0; i < pm->nr_perflvl; i++) {
272 struct nouveau_pm_level *perflvl = &pm->perflvl[i];
273
274 perflvl->dev_attr.attr.name = perflvl->name;
275 perflvl->dev_attr.attr.mode = S_IRUGO;
276 perflvl->dev_attr.show = nouveau_pm_get_perflvl_info;
277 perflvl->dev_attr.store = NULL;
278 sysfs_attr_init(&perflvl->dev_attr.attr);
279
280 ret = device_create_file(d, &perflvl->dev_attr);
281 if (ret) {
282 NV_ERROR(dev, "failed pervlvl %d sysfs: %d\n",
283 perflvl->id, i);
284 perflvl->dev_attr.attr.name = NULL;
285 nouveau_pm_fini(dev);
286 return ret;
287 }
288 }
289
290 return 0;
291}
292
293void
294nouveau_pm_fini(struct drm_device *dev)
295{
296 struct drm_nouveau_private *dev_priv = dev->dev_private;
297 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
298 struct device *d = &dev->pdev->dev;
299 int i;
300
Ben Skeggs64f1c112010-09-17 13:35:25 +1000301 if (pm->cur != &pm->boot)
302 nouveau_pm_perflvl_set(dev, &pm->boot);
303
Ben Skeggs330c5982010-09-16 15:39:49 +1000304 device_remove_file(d, &dev_attr_performance_level);
305 for (i = 0; i < pm->nr_perflvl; i++) {
306 struct nouveau_pm_level *pl = &pm->perflvl[i];
307
308 if (!pl->dev_attr.attr.name)
309 break;
310
311 device_remove_file(d, &pl->dev_attr);
312 }
313
314 nouveau_perf_fini(dev);
315 nouveau_volt_fini(dev);
316}
317
Ben Skeggs64f1c112010-09-17 13:35:25 +1000318void
319nouveau_pm_resume(struct drm_device *dev)
320{
321 struct drm_nouveau_private *dev_priv = dev->dev_private;
322 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
323 struct nouveau_pm_level *perflvl;
324
325 if (pm->cur == &pm->boot)
326 return;
327
328 perflvl = pm->cur;
329 pm->cur = &pm->boot;
330 nouveau_pm_perflvl_set(dev, perflvl);
331}