drm/nouveau: allow static performance level setting
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / nouveau / nouveau_pm.c
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
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
49 static int
50 nouveau_pm_profile_set(struct drm_device *dev, const char *profile)
51 {
52         struct drm_nouveau_private *dev_priv = dev->dev_private;
53         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
54         struct nouveau_pm_level *perflvl = NULL;
55         int ret;
56
57         /* safety precaution, for now */
58         if (nouveau_perflvl_wr != 7777)
59                 return -EPERM;
60
61         if (!pm->clock_set)
62                 return -EINVAL;
63
64         if (!strncmp(profile, "boot", 4))
65                 perflvl = &pm->boot;
66         else {
67                 int pl = simple_strtol(profile, NULL, 10);
68                 int i;
69
70                 for (i = 0; i < pm->nr_perflvl; i++) {
71                         if (pm->perflvl[i].id == pl) {
72                                 perflvl = &pm->perflvl[i];
73                                 break;
74                         }
75                 }
76
77                 if (!perflvl)
78                         return -EINVAL;
79         }
80
81         if (perflvl == pm->cur)
82                 return 0;
83
84         NV_INFO(dev, "setting performance level: %s\n", profile);
85         if (pm->voltage.supported && pm->voltage_set && perflvl->voltage) {
86                 ret = pm->voltage_set(dev, perflvl->voltage);
87                 if (ret) {
88                         NV_ERROR(dev, "voltage_set %d failed: %d\n",
89                                  perflvl->voltage, ret);
90                 }
91         }
92
93         nouveau_pm_clock_set(dev, PLL_CORE, perflvl->core);
94         nouveau_pm_clock_set(dev, PLL_SHADER, perflvl->shader);
95         nouveau_pm_clock_set(dev, PLL_MEMORY, perflvl->memory);
96         nouveau_pm_clock_set(dev, PLL_UNK05, perflvl->unk05);
97
98         pm->cur = perflvl;
99         return 0;
100 }
101
102 static int
103 nouveau_pm_perflvl_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
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
109         if (!pm->clock_get)
110                 return -EINVAL;
111
112         memset(perflvl, 0, sizeof(*perflvl));
113
114         ret = pm->clock_get(dev, PLL_CORE);
115         if (ret > 0)
116                 perflvl->core = ret;
117
118         ret = pm->clock_get(dev, PLL_MEMORY);
119         if (ret > 0)
120                 perflvl->memory = ret;
121
122         ret = pm->clock_get(dev, PLL_SHADER);
123         if (ret > 0)
124                 perflvl->shader = ret;
125
126         ret = pm->clock_get(dev, PLL_UNK05);
127         if (ret > 0)
128                 perflvl->unk05 = ret;
129
130         if (pm->voltage.supported && pm->voltage_get) {
131                 ret = pm->voltage_get(dev);
132                 if (ret > 0)
133                         perflvl->voltage = ret;
134         }
135
136         return 0;
137 }
138
139 static void
140 nouveau_pm_perflvl_info(struct nouveau_pm_level *perflvl, char *ptr, int len)
141 {
142         char s[16], v[16], f[16];
143
144         s[0] = '\0';
145         if (perflvl->shader)
146                 snprintf(s, sizeof(s), " shader %dMHz", perflvl->shader / 1000);
147
148         v[0] = '\0';
149         if (perflvl->voltage)
150                 snprintf(v, sizeof(v), " voltage %dmV", perflvl->voltage * 10);
151
152         f[0] = '\0';
153         if (perflvl->fanspeed)
154                 snprintf(f, sizeof(f), " fanspeed %d%%", perflvl->fanspeed);
155
156         snprintf(ptr, len, "core %dMHz memory %dMHz%s%s%s\n",
157                  perflvl->core / 1000, perflvl->memory / 1000, s, v, f);
158 }
159
160 static ssize_t
161 nouveau_pm_get_perflvl_info(struct device *d,
162                             struct device_attribute *a, char *buf)
163 {
164         struct nouveau_pm_level *perflvl = (struct nouveau_pm_level *)a;
165         char *ptr = buf;
166         int len = PAGE_SIZE;
167
168         snprintf(ptr, len, "%d: ", perflvl->id);
169         ptr += strlen(buf);
170         len -= strlen(buf);
171
172         nouveau_pm_perflvl_info(perflvl, ptr, len);
173         return strlen(buf);
174 }
175
176 static ssize_t
177 nouveau_pm_get_perflvl(struct device *d, struct device_attribute *a, char *buf)
178 {
179         struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
180         struct drm_nouveau_private *dev_priv = dev->dev_private;
181         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
182         struct nouveau_pm_level cur;
183         int len = PAGE_SIZE, ret;
184         char *ptr = buf;
185
186         if (!pm->cur)
187                 snprintf(ptr, len, "setting: boot\n");
188         else if (pm->cur == &pm->boot)
189                 snprintf(ptr, len, "setting: boot\nc: ");
190         else
191                 snprintf(ptr, len, "setting: static %d\nc: ", pm->cur->id);
192         ptr += strlen(buf);
193         len -= strlen(buf);
194
195         ret = nouveau_pm_perflvl_get(dev, &cur);
196         if (ret == 0)
197                 nouveau_pm_perflvl_info(&cur, ptr, len);
198         return strlen(buf);
199 }
200
201 static ssize_t
202 nouveau_pm_set_perflvl(struct device *d, struct device_attribute *a,
203                        const char *buf, size_t count)
204 {
205         struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
206         int ret;
207
208         ret = nouveau_pm_profile_set(dev, buf);
209         if (ret)
210                 return ret;
211         return strlen(buf);
212 }
213
214 DEVICE_ATTR(performance_level, S_IRUGO | S_IWUSR,
215             nouveau_pm_get_perflvl, nouveau_pm_set_perflvl);
216
217 int
218 nouveau_pm_init(struct drm_device *dev)
219 {
220         struct drm_nouveau_private *dev_priv = dev->dev_private;
221         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
222         struct device *d = &dev->pdev->dev;
223         char info[256];
224         int ret, i;
225
226         nouveau_volt_init(dev);
227         nouveau_perf_init(dev);
228
229         NV_INFO(dev, "%d available performance level(s)\n", pm->nr_perflvl);
230         for (i = 0; i < pm->nr_perflvl; i++) {
231                 nouveau_pm_perflvl_info(&pm->perflvl[i], info, sizeof(info));
232                 NV_INFO(dev, "%d: %s", pm->perflvl[i].id, info);
233         }
234
235         /* determine current ("boot") performance level */
236         ret = nouveau_pm_perflvl_get(dev, &pm->boot);
237         if (ret == 0) {
238                 pm->cur = &pm->boot;
239
240                 nouveau_pm_perflvl_info(&pm->boot, info, sizeof(info));
241                 NV_INFO(dev, "c: %s", info);
242         }
243
244         /* switch performance levels now if requested */
245         if (nouveau_perflvl != NULL) {
246                 ret = nouveau_pm_profile_set(dev, nouveau_perflvl);
247                 if (ret) {
248                         NV_ERROR(dev, "error setting perflvl \"%s\": %d\n",
249                                  nouveau_perflvl, ret);
250                 }
251         }
252
253         /* initialise sysfs */
254         ret = device_create_file(d, &dev_attr_performance_level);
255         if (ret)
256                 return ret;
257
258         for (i = 0; i < pm->nr_perflvl; i++) {
259                 struct nouveau_pm_level *perflvl = &pm->perflvl[i];
260
261                 perflvl->dev_attr.attr.name = perflvl->name;
262                 perflvl->dev_attr.attr.mode = S_IRUGO;
263                 perflvl->dev_attr.show = nouveau_pm_get_perflvl_info;
264                 perflvl->dev_attr.store = NULL;
265                 sysfs_attr_init(&perflvl->dev_attr.attr);
266
267                 ret = device_create_file(d, &perflvl->dev_attr);
268                 if (ret) {
269                         NV_ERROR(dev, "failed pervlvl %d sysfs: %d\n",
270                                  perflvl->id, i);
271                         perflvl->dev_attr.attr.name = NULL;
272                         nouveau_pm_fini(dev);
273                         return ret;
274                 }
275         }
276
277         return 0;
278 }
279
280 void
281 nouveau_pm_fini(struct drm_device *dev)
282 {
283         struct drm_nouveau_private *dev_priv = dev->dev_private;
284         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
285         struct device *d = &dev->pdev->dev;
286         int i;
287
288         device_remove_file(d, &dev_attr_performance_level);
289         for (i = 0; i < pm->nr_perflvl; i++) {
290                 struct nouveau_pm_level *pl = &pm->perflvl[i];
291
292                 if (!pl->dev_attr.attr.name)
293                         break;
294
295                 device_remove_file(d, &pl->dev_attr);
296         }
297
298         nouveau_perf_fini(dev);
299         nouveau_volt_fini(dev);
300 }
301