blob: 5dac3546c1b89679842300fd1b2aff9f5c5e3da6 [file] [log] [blame]
Ben Skeggs26fdd782013-10-15 11:52:56 +10001/*
2 * Copyright 2013 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 <bskeggs@redhat.com>
23 */
24
Ben Skeggsfdb751e2014-08-10 04:10:23 +100025#include <nvif/os.h>
26#include <nvif/class.h>
Ben Skeggsa532da92014-08-10 04:10:25 +100027#include <nvif/ioctl.h>
Ben Skeggs26fdd782013-10-15 11:52:56 +100028
Ben Skeggsfdb751e2014-08-10 04:10:23 +100029#include "nouveau_sysfs.h"
Ben Skeggs26fdd782013-10-15 11:52:56 +100030
Ben Skeggs0d48b582014-08-10 04:10:30 +100031MODULE_PARM_DESC(pstate, "enable sysfs pstate file, which will be moved in the future");
Pierre Moreau703fa262014-08-18 22:43:24 +020032int nouveau_pstate;
Ben Skeggs0d48b582014-08-10 04:10:30 +100033module_param_named(pstate, nouveau_pstate, int, 0400);
34
Ben Skeggs26fdd782013-10-15 11:52:56 +100035static inline struct drm_device *
36drm_device(struct device *d)
37{
Alexandre Courbot420b9462014-02-17 15:17:26 +090038 return dev_get_drvdata(d);
Ben Skeggs26fdd782013-10-15 11:52:56 +100039}
40
41#define snappendf(p,r,f,a...) do { \
42 snprintf(p, r, f, ##a); \
43 r -= strlen(p); \
44 p += strlen(p); \
45} while(0)
46
47static ssize_t
48nouveau_sysfs_pstate_get(struct device *d, struct device_attribute *a, char *b)
49{
50 struct nouveau_sysfs *sysfs = nouveau_sysfs(drm_device(d));
Ben Skeggsa532da92014-08-10 04:10:25 +100051 struct nvif_control_pstate_info_v0 info = {};
Ben Skeggs26fdd782013-10-15 11:52:56 +100052 size_t cnt = PAGE_SIZE;
53 char *buf = b;
54 int ret, i;
55
Ben Skeggsa532da92014-08-10 04:10:25 +100056 ret = nvif_mthd(&sysfs->ctrl, NVIF_CONTROL_PSTATE_INFO,
Ben Skeggs0ad72862014-08-10 04:10:22 +100057 &info, sizeof(info));
Ben Skeggs26fdd782013-10-15 11:52:56 +100058 if (ret)
59 return ret;
60
61 for (i = 0; i < info.count + 1; i++) {
62 const s32 state = i < info.count ? i :
Ben Skeggsa532da92014-08-10 04:10:25 +100063 NVIF_CONTROL_PSTATE_ATTR_V0_STATE_CURRENT;
64 struct nvif_control_pstate_attr_v0 attr = {
Ben Skeggs26fdd782013-10-15 11:52:56 +100065 .state = state,
66 .index = 0,
67 };
68
Ben Skeggsa532da92014-08-10 04:10:25 +100069 ret = nvif_mthd(&sysfs->ctrl, NVIF_CONTROL_PSTATE_ATTR,
Ben Skeggs0ad72862014-08-10 04:10:22 +100070 &attr, sizeof(attr));
Ben Skeggs26fdd782013-10-15 11:52:56 +100071 if (ret)
72 return ret;
73
74 if (i < info.count)
75 snappendf(buf, cnt, "%02x:", attr.state);
76 else
Ben Skeggs7238eca2014-06-13 14:17:09 +100077 snappendf(buf, cnt, "%s:", info.pwrsrc == 0 ? "DC" :
78 info.pwrsrc == 1 ? "AC" :
79 "--");
Ben Skeggs26fdd782013-10-15 11:52:56 +100080
81 attr.index = 0;
82 do {
83 attr.state = state;
Ben Skeggsa532da92014-08-10 04:10:25 +100084 ret = nvif_mthd(&sysfs->ctrl,
85 NVIF_CONTROL_PSTATE_ATTR,
Ben Skeggs0ad72862014-08-10 04:10:22 +100086 &attr, sizeof(attr));
Ben Skeggs26fdd782013-10-15 11:52:56 +100087 if (ret)
88 return ret;
89
90 snappendf(buf, cnt, " %s %d", attr.name, attr.min);
91 if (attr.min != attr.max)
92 snappendf(buf, cnt, "-%d", attr.max);
93 snappendf(buf, cnt, " %s", attr.unit);
94 } while (attr.index);
95
Ben Skeggs7238eca2014-06-13 14:17:09 +100096 if (state >= 0) {
97 if (info.ustate_ac == state)
98 snappendf(buf, cnt, " AC");
99 if (info.ustate_dc == state)
100 snappendf(buf, cnt, " DC");
101 if (info.pstate == state)
102 snappendf(buf, cnt, " *");
103 } else {
104 if (info.ustate_ac < -1)
105 snappendf(buf, cnt, " AC");
106 if (info.ustate_dc < -1)
107 snappendf(buf, cnt, " DC");
108 }
109
Ben Skeggs26fdd782013-10-15 11:52:56 +1000110 snappendf(buf, cnt, "\n");
111 }
112
113 return strlen(b);
114}
115
116static ssize_t
117nouveau_sysfs_pstate_set(struct device *d, struct device_attribute *a,
118 const char *buf, size_t count)
119{
120 struct nouveau_sysfs *sysfs = nouveau_sysfs(drm_device(d));
Ben Skeggsa532da92014-08-10 04:10:25 +1000121 struct nvif_control_pstate_user_v0 args = { .pwrsrc = -EINVAL };
Ben Skeggs26fdd782013-10-15 11:52:56 +1000122 long value, ret;
123 char *tmp;
124
125 if ((tmp = strchr(buf, '\n')))
126 *tmp = '\0';
127
Ben Skeggs7238eca2014-06-13 14:17:09 +1000128 if (!strncasecmp(buf, "dc:", 3)) {
129 args.pwrsrc = 0;
130 buf += 3;
131 } else
132 if (!strncasecmp(buf, "ac:", 3)) {
133 args.pwrsrc = 1;
134 buf += 3;
135 }
136
Ben Skeggs26fdd782013-10-15 11:52:56 +1000137 if (!strcasecmp(buf, "none"))
Ben Skeggsa532da92014-08-10 04:10:25 +1000138 args.ustate = NVIF_CONTROL_PSTATE_USER_V0_STATE_UNKNOWN;
Ben Skeggs26fdd782013-10-15 11:52:56 +1000139 else
140 if (!strcasecmp(buf, "auto"))
Ben Skeggsa532da92014-08-10 04:10:25 +1000141 args.ustate = NVIF_CONTROL_PSTATE_USER_V0_STATE_PERFMON;
Ben Skeggs26fdd782013-10-15 11:52:56 +1000142 else {
143 ret = kstrtol(buf, 16, &value);
144 if (ret)
145 return ret;
Ben Skeggs7238eca2014-06-13 14:17:09 +1000146 args.ustate = value;
Ben Skeggs26fdd782013-10-15 11:52:56 +1000147 }
148
Ben Skeggsa532da92014-08-10 04:10:25 +1000149 ret = nvif_mthd(&sysfs->ctrl, NVIF_CONTROL_PSTATE_USER,
Ben Skeggs0ad72862014-08-10 04:10:22 +1000150 &args, sizeof(args));
Ben Skeggs26fdd782013-10-15 11:52:56 +1000151 if (ret < 0)
152 return ret;
153
154 return count;
155}
156
157static DEVICE_ATTR(pstate, S_IRUGO | S_IWUSR,
158 nouveau_sysfs_pstate_get, nouveau_sysfs_pstate_set);
159
160void
161nouveau_sysfs_fini(struct drm_device *dev)
162{
163 struct nouveau_sysfs *sysfs = nouveau_sysfs(dev);
164 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000165 struct nvif_device *device = &drm->device;
Ben Skeggs26fdd782013-10-15 11:52:56 +1000166
Ben Skeggs0d48b582014-08-10 04:10:30 +1000167 if (sysfs && sysfs->ctrl.priv) {
Ben Skeggs26c9e8e2015-08-20 14:54:23 +1000168 device_remove_file(nvxx_device(device)->dev, &dev_attr_pstate);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000169 nvif_object_fini(&sysfs->ctrl);
Ben Skeggs26fdd782013-10-15 11:52:56 +1000170 }
171
172 drm->sysfs = NULL;
173 kfree(sysfs);
174}
175
176int
177nouveau_sysfs_init(struct drm_device *dev)
178{
179 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000180 struct nvif_device *device = &drm->device;
Ben Skeggs26fdd782013-10-15 11:52:56 +1000181 struct nouveau_sysfs *sysfs;
182 int ret;
183
Ben Skeggs0d48b582014-08-10 04:10:30 +1000184 if (!nouveau_pstate)
185 return 0;
186
Ben Skeggs26fdd782013-10-15 11:52:56 +1000187 sysfs = drm->sysfs = kzalloc(sizeof(*sysfs), GFP_KERNEL);
188 if (!sysfs)
189 return -ENOMEM;
190
Ben Skeggsfcf3f912015-09-04 14:40:32 +1000191 ret = nvif_object_init(&device->object, 0, NVIF_IOCTL_NEW_V0_CONTROL,
192 NULL, 0, &sysfs->ctrl);
Ben Skeggs26fdd782013-10-15 11:52:56 +1000193 if (ret == 0)
Ben Skeggs26c9e8e2015-08-20 14:54:23 +1000194 device_create_file(nvxx_device(device)->dev, &dev_attr_pstate);
Ben Skeggs26fdd782013-10-15 11:52:56 +1000195
196 return 0;
197}