]>
Commit | Line | Data |
---|---|---|
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" | |
29 | ||
6f876986 BS |
30 | static int |
31 | nouveau_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 | ||
64f1c11a BS |
49 | static int |
50 | nouveau_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 | ||
6f876986 BS |
76 | static int |
77 | nouveau_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; | |
6f876986 BS |
82 | |
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 | ||
6f876986 | 107 | NV_INFO(dev, "setting performance level: %s\n", profile); |
64f1c11a | 108 | return nouveau_pm_perflvl_set(dev, perflvl); |
6f876986 BS |
109 | } |
110 | ||
330c5988 BS |
111 | static int |
112 | nouveau_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 | ||
148 | static void | |
149 | nouveau_pm_perflvl_info(struct nouveau_pm_level *perflvl, char *ptr, int len) | |
150 | { | |
151 | char s[16], v[16], f[16]; | |
152 | ||
153 | s[0] = '\0'; | |
154 | if (perflvl->shader) | |
155 | snprintf(s, sizeof(s), " shader %dMHz", perflvl->shader / 1000); | |
156 | ||
157 | v[0] = '\0'; | |
158 | if (perflvl->voltage) | |
159 | snprintf(v, sizeof(v), " voltage %dmV", perflvl->voltage * 10); | |
160 | ||
161 | f[0] = '\0'; | |
162 | if (perflvl->fanspeed) | |
163 | snprintf(f, sizeof(f), " fanspeed %d%%", perflvl->fanspeed); | |
164 | ||
165 | snprintf(ptr, len, "core %dMHz memory %dMHz%s%s%s\n", | |
166 | perflvl->core / 1000, perflvl->memory / 1000, s, v, f); | |
167 | } | |
168 | ||
169 | static ssize_t | |
170 | nouveau_pm_get_perflvl_info(struct device *d, | |
171 | struct device_attribute *a, char *buf) | |
172 | { | |
173 | struct nouveau_pm_level *perflvl = (struct nouveau_pm_level *)a; | |
174 | char *ptr = buf; | |
175 | int len = PAGE_SIZE; | |
176 | ||
177 | snprintf(ptr, len, "%d: ", perflvl->id); | |
178 | ptr += strlen(buf); | |
179 | len -= strlen(buf); | |
180 | ||
181 | nouveau_pm_perflvl_info(perflvl, ptr, len); | |
182 | return strlen(buf); | |
183 | } | |
184 | ||
185 | static ssize_t | |
186 | nouveau_pm_get_perflvl(struct device *d, struct device_attribute *a, char *buf) | |
187 | { | |
188 | struct drm_device *dev = pci_get_drvdata(to_pci_dev(d)); | |
189 | struct drm_nouveau_private *dev_priv = dev->dev_private; | |
190 | struct nouveau_pm_engine *pm = &dev_priv->engine.pm; | |
191 | struct nouveau_pm_level cur; | |
192 | int len = PAGE_SIZE, ret; | |
193 | char *ptr = buf; | |
194 | ||
195 | if (!pm->cur) | |
196 | snprintf(ptr, len, "setting: boot\n"); | |
197 | else if (pm->cur == &pm->boot) | |
198 | snprintf(ptr, len, "setting: boot\nc: "); | |
199 | else | |
200 | snprintf(ptr, len, "setting: static %d\nc: ", pm->cur->id); | |
201 | ptr += strlen(buf); | |
202 | len -= strlen(buf); | |
203 | ||
204 | ret = nouveau_pm_perflvl_get(dev, &cur); | |
205 | if (ret == 0) | |
206 | nouveau_pm_perflvl_info(&cur, ptr, len); | |
207 | return strlen(buf); | |
208 | } | |
209 | ||
210 | static ssize_t | |
211 | nouveau_pm_set_perflvl(struct device *d, struct device_attribute *a, | |
212 | const char *buf, size_t count) | |
213 | { | |
6f876986 BS |
214 | struct drm_device *dev = pci_get_drvdata(to_pci_dev(d)); |
215 | int ret; | |
216 | ||
217 | ret = nouveau_pm_profile_set(dev, buf); | |
218 | if (ret) | |
219 | return ret; | |
220 | return strlen(buf); | |
330c5988 BS |
221 | } |
222 | ||
223 | DEVICE_ATTR(performance_level, S_IRUGO | S_IWUSR, | |
224 | nouveau_pm_get_perflvl, nouveau_pm_set_perflvl); | |
225 | ||
226 | int | |
227 | nouveau_pm_init(struct drm_device *dev) | |
228 | { | |
229 | struct drm_nouveau_private *dev_priv = dev->dev_private; | |
230 | struct nouveau_pm_engine *pm = &dev_priv->engine.pm; | |
231 | struct device *d = &dev->pdev->dev; | |
232 | char info[256]; | |
233 | int ret, i; | |
234 | ||
235 | nouveau_volt_init(dev); | |
236 | nouveau_perf_init(dev); | |
237 | ||
238 | NV_INFO(dev, "%d available performance level(s)\n", pm->nr_perflvl); | |
239 | for (i = 0; i < pm->nr_perflvl; i++) { | |
240 | nouveau_pm_perflvl_info(&pm->perflvl[i], info, sizeof(info)); | |
241 | NV_INFO(dev, "%d: %s", pm->perflvl[i].id, info); | |
242 | } | |
243 | ||
244 | /* determine current ("boot") performance level */ | |
245 | ret = nouveau_pm_perflvl_get(dev, &pm->boot); | |
246 | if (ret == 0) { | |
247 | pm->cur = &pm->boot; | |
248 | ||
249 | nouveau_pm_perflvl_info(&pm->boot, info, sizeof(info)); | |
250 | NV_INFO(dev, "c: %s", info); | |
251 | } | |
252 | ||
6f876986 BS |
253 | /* switch performance levels now if requested */ |
254 | if (nouveau_perflvl != NULL) { | |
255 | ret = nouveau_pm_profile_set(dev, nouveau_perflvl); | |
256 | if (ret) { | |
257 | NV_ERROR(dev, "error setting perflvl \"%s\": %d\n", | |
258 | nouveau_perflvl, ret); | |
259 | } | |
260 | } | |
261 | ||
330c5988 BS |
262 | /* initialise sysfs */ |
263 | ret = device_create_file(d, &dev_attr_performance_level); | |
264 | if (ret) | |
265 | return ret; | |
266 | ||
267 | for (i = 0; i < pm->nr_perflvl; i++) { | |
268 | struct nouveau_pm_level *perflvl = &pm->perflvl[i]; | |
269 | ||
270 | perflvl->dev_attr.attr.name = perflvl->name; | |
271 | perflvl->dev_attr.attr.mode = S_IRUGO; | |
272 | perflvl->dev_attr.show = nouveau_pm_get_perflvl_info; | |
273 | perflvl->dev_attr.store = NULL; | |
274 | sysfs_attr_init(&perflvl->dev_attr.attr); | |
275 | ||
276 | ret = device_create_file(d, &perflvl->dev_attr); | |
277 | if (ret) { | |
278 | NV_ERROR(dev, "failed pervlvl %d sysfs: %d\n", | |
279 | perflvl->id, i); | |
280 | perflvl->dev_attr.attr.name = NULL; | |
281 | nouveau_pm_fini(dev); | |
282 | return ret; | |
283 | } | |
284 | } | |
285 | ||
286 | return 0; | |
287 | } | |
288 | ||
289 | void | |
290 | nouveau_pm_fini(struct drm_device *dev) | |
291 | { | |
292 | struct drm_nouveau_private *dev_priv = dev->dev_private; | |
293 | struct nouveau_pm_engine *pm = &dev_priv->engine.pm; | |
294 | struct device *d = &dev->pdev->dev; | |
295 | int i; | |
296 | ||
64f1c11a BS |
297 | if (pm->cur != &pm->boot) |
298 | nouveau_pm_perflvl_set(dev, &pm->boot); | |
299 | ||
330c5988 BS |
300 | device_remove_file(d, &dev_attr_performance_level); |
301 | for (i = 0; i < pm->nr_perflvl; i++) { | |
302 | struct nouveau_pm_level *pl = &pm->perflvl[i]; | |
303 | ||
304 | if (!pl->dev_attr.attr.name) | |
305 | break; | |
306 | ||
307 | device_remove_file(d, &pl->dev_attr); | |
308 | } | |
309 | ||
310 | nouveau_perf_fini(dev); | |
311 | nouveau_volt_fini(dev); | |
312 | } | |
313 | ||
64f1c11a BS |
314 | void |
315 | nouveau_pm_resume(struct drm_device *dev) | |
316 | { | |
317 | struct drm_nouveau_private *dev_priv = dev->dev_private; | |
318 | struct nouveau_pm_engine *pm = &dev_priv->engine.pm; | |
319 | struct nouveau_pm_level *perflvl; | |
320 | ||
321 | if (pm->cur == &pm->boot) | |
322 | return; | |
323 | ||
324 | perflvl = pm->cur; | |
325 | pm->cur = &pm->boot; | |
326 | nouveau_pm_perflvl_set(dev, perflvl); | |
327 | } |