PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / gpu / drm / nouveau / nouveau_sysfs.c
blob89201a17ce752357444ec9f5ad0a4a42a84a79f8
1 /*
2 * Copyright 2013 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
22 * Authors: Ben Skeggs <bskeggs@redhat.com>
25 #include "nouveau_sysfs.h"
27 #include <core/object.h>
28 #include <core/class.h>
30 static inline struct drm_device *
31 drm_device(struct device *d)
33 return pci_get_drvdata(to_pci_dev(d));
36 #define snappendf(p,r,f,a...) do { \
37 snprintf(p, r, f, ##a); \
38 r -= strlen(p); \
39 p += strlen(p); \
40 } while(0)
42 static ssize_t
43 nouveau_sysfs_pstate_get(struct device *d, struct device_attribute *a, char *b)
45 struct nouveau_sysfs *sysfs = nouveau_sysfs(drm_device(d));
46 struct nv_control_pstate_info info;
47 size_t cnt = PAGE_SIZE;
48 char *buf = b;
49 int ret, i;
51 ret = nv_exec(sysfs->ctrl, NV_CONTROL_PSTATE_INFO, &info, sizeof(info));
52 if (ret)
53 return ret;
55 for (i = 0; i < info.count + 1; i++) {
56 const s32 state = i < info.count ? i :
57 NV_CONTROL_PSTATE_ATTR_STATE_CURRENT;
58 struct nv_control_pstate_attr attr = {
59 .state = state,
60 .index = 0,
63 ret = nv_exec(sysfs->ctrl, NV_CONTROL_PSTATE_ATTR,
64 &attr, sizeof(attr));
65 if (ret)
66 return ret;
68 if (i < info.count)
69 snappendf(buf, cnt, "%02x:", attr.state);
70 else
71 snappendf(buf, cnt, "--:");
73 attr.index = 0;
74 do {
75 attr.state = state;
76 ret = nv_exec(sysfs->ctrl, NV_CONTROL_PSTATE_ATTR,
77 &attr, sizeof(attr));
78 if (ret)
79 return ret;
81 snappendf(buf, cnt, " %s %d", attr.name, attr.min);
82 if (attr.min != attr.max)
83 snappendf(buf, cnt, "-%d", attr.max);
84 snappendf(buf, cnt, " %s", attr.unit);
85 } while (attr.index);
87 if ((state >= 0 && info.pstate == state) ||
88 (state < 0 && info.ustate < 0))
89 snappendf(buf, cnt, " *");
90 snappendf(buf, cnt, "\n");
93 return strlen(b);
96 static ssize_t
97 nouveau_sysfs_pstate_set(struct device *d, struct device_attribute *a,
98 const char *buf, size_t count)
100 struct nouveau_sysfs *sysfs = nouveau_sysfs(drm_device(d));
101 struct nv_control_pstate_user args;
102 long value, ret;
103 char *tmp;
105 if ((tmp = strchr(buf, '\n')))
106 *tmp = '\0';
108 if (!strcasecmp(buf, "none"))
109 args.state = NV_CONTROL_PSTATE_USER_STATE_UNKNOWN;
110 else
111 if (!strcasecmp(buf, "auto"))
112 args.state = NV_CONTROL_PSTATE_USER_STATE_PERFMON;
113 else {
114 ret = kstrtol(buf, 16, &value);
115 if (ret)
116 return ret;
117 args.state = value;
120 ret = nv_exec(sysfs->ctrl, NV_CONTROL_PSTATE_USER, &args, sizeof(args));
121 if (ret < 0)
122 return ret;
124 return count;
127 static DEVICE_ATTR(pstate, S_IRUGO | S_IWUSR,
128 nouveau_sysfs_pstate_get, nouveau_sysfs_pstate_set);
130 void
131 nouveau_sysfs_fini(struct drm_device *dev)
133 struct nouveau_sysfs *sysfs = nouveau_sysfs(dev);
134 struct nouveau_drm *drm = nouveau_drm(dev);
136 if (sysfs->ctrl) {
137 device_remove_file(&dev->pdev->dev, &dev_attr_pstate);
138 nouveau_object_del(nv_object(drm), NVDRM_DEVICE, NVDRM_CONTROL);
141 drm->sysfs = NULL;
142 kfree(sysfs);
146 nouveau_sysfs_init(struct drm_device *dev)
148 struct nouveau_drm *drm = nouveau_drm(dev);
149 struct nouveau_sysfs *sysfs;
150 int ret;
152 sysfs = drm->sysfs = kzalloc(sizeof(*sysfs), GFP_KERNEL);
153 if (!sysfs)
154 return -ENOMEM;
156 ret = nouveau_object_new(nv_object(drm), NVDRM_DEVICE, NVDRM_CONTROL,
157 NV_CONTROL_CLASS, NULL, 0, &sysfs->ctrl);
158 if (ret == 0)
159 device_create_file(&dev->pdev->dev, &dev_attr_pstate);
161 return 0;