Commit | Line | Data |
---|---|---|
4f86d3a8 LB |
1 | /* |
2 | * sysfs.c - sysfs support | |
3 | * | |
4 | * (C) 2006-2007 Shaohua Li <shaohua.li@intel.com> | |
5 | * | |
6 | * This code is licenced under the GPL. | |
7 | */ | |
8 | ||
9 | #include <linux/kernel.h> | |
10 | #include <linux/cpuidle.h> | |
11 | #include <linux/sysfs.h> | |
5a0e3ad6 | 12 | #include <linux/slab.h> |
4f86d3a8 LB |
13 | #include <linux/cpu.h> |
14 | ||
15 | #include "cpuidle.h" | |
16 | ||
17 | static unsigned int sysfs_switch; | |
18 | static int __init cpuidle_sysfs_setup(char *unused) | |
19 | { | |
20 | sysfs_switch = 1; | |
21 | return 1; | |
22 | } | |
23 | __setup("cpuidle_sysfs_switch", cpuidle_sysfs_setup); | |
24 | ||
66198f36 | 25 | static ssize_t show_available_governors(struct sysdev_class *class, |
c9be0a36 | 26 | struct sysdev_class_attribute *attr, |
66198f36 | 27 | char *buf) |
4f86d3a8 LB |
28 | { |
29 | ssize_t i = 0; | |
30 | struct cpuidle_governor *tmp; | |
31 | ||
32 | mutex_lock(&cpuidle_lock); | |
33 | list_for_each_entry(tmp, &cpuidle_governors, governor_list) { | |
34 | if (i >= (ssize_t) ((PAGE_SIZE/sizeof(char)) - CPUIDLE_NAME_LEN - 2)) | |
35 | goto out; | |
36 | i += scnprintf(&buf[i], CPUIDLE_NAME_LEN, "%s ", tmp->name); | |
37 | } | |
38 | ||
39 | out: | |
40 | i+= sprintf(&buf[i], "\n"); | |
41 | mutex_unlock(&cpuidle_lock); | |
42 | return i; | |
43 | } | |
44 | ||
66198f36 | 45 | static ssize_t show_current_driver(struct sysdev_class *class, |
c9be0a36 | 46 | struct sysdev_class_attribute *attr, |
66198f36 | 47 | char *buf) |
4f86d3a8 LB |
48 | { |
49 | ssize_t ret; | |
50 | ||
51 | spin_lock(&cpuidle_driver_lock); | |
52 | if (cpuidle_curr_driver) | |
53 | ret = sprintf(buf, "%s\n", cpuidle_curr_driver->name); | |
54 | else | |
55 | ret = sprintf(buf, "none\n"); | |
56 | spin_unlock(&cpuidle_driver_lock); | |
57 | ||
58 | return ret; | |
59 | } | |
60 | ||
66198f36 | 61 | static ssize_t show_current_governor(struct sysdev_class *class, |
c9be0a36 | 62 | struct sysdev_class_attribute *attr, |
66198f36 | 63 | char *buf) |
4f86d3a8 LB |
64 | { |
65 | ssize_t ret; | |
66 | ||
67 | mutex_lock(&cpuidle_lock); | |
68 | if (cpuidle_curr_governor) | |
69 | ret = sprintf(buf, "%s\n", cpuidle_curr_governor->name); | |
70 | else | |
71 | ret = sprintf(buf, "none\n"); | |
72 | mutex_unlock(&cpuidle_lock); | |
73 | ||
74 | return ret; | |
75 | } | |
76 | ||
66198f36 | 77 | static ssize_t store_current_governor(struct sysdev_class *class, |
c9be0a36 | 78 | struct sysdev_class_attribute *attr, |
66198f36 | 79 | const char *buf, size_t count) |
4f86d3a8 LB |
80 | { |
81 | char gov_name[CPUIDLE_NAME_LEN]; | |
82 | int ret = -EINVAL; | |
83 | size_t len = count; | |
84 | struct cpuidle_governor *gov; | |
85 | ||
86 | if (!len || len >= sizeof(gov_name)) | |
87 | return -EINVAL; | |
88 | ||
89 | memcpy(gov_name, buf, len); | |
90 | gov_name[len] = '\0'; | |
91 | if (gov_name[len - 1] == '\n') | |
92 | gov_name[--len] = '\0'; | |
93 | ||
94 | mutex_lock(&cpuidle_lock); | |
95 | ||
96 | list_for_each_entry(gov, &cpuidle_governors, governor_list) { | |
97 | if (strlen(gov->name) == len && !strcmp(gov->name, gov_name)) { | |
98 | ret = cpuidle_switch_governor(gov); | |
99 | break; | |
100 | } | |
101 | } | |
102 | ||
103 | mutex_unlock(&cpuidle_lock); | |
104 | ||
105 | if (ret) | |
106 | return ret; | |
107 | else | |
108 | return count; | |
109 | } | |
110 | ||
66198f36 RV |
111 | static SYSDEV_CLASS_ATTR(current_driver, 0444, show_current_driver, NULL); |
112 | static SYSDEV_CLASS_ATTR(current_governor_ro, 0444, show_current_governor, | |
113 | NULL); | |
4f86d3a8 LB |
114 | |
115 | static struct attribute *cpuclass_default_attrs[] = { | |
116 | &attr_current_driver.attr, | |
117 | &attr_current_governor_ro.attr, | |
118 | NULL | |
119 | }; | |
120 | ||
66198f36 RV |
121 | static SYSDEV_CLASS_ATTR(available_governors, 0444, show_available_governors, |
122 | NULL); | |
123 | static SYSDEV_CLASS_ATTR(current_governor, 0644, show_current_governor, | |
124 | store_current_governor); | |
4f86d3a8 LB |
125 | |
126 | static struct attribute *cpuclass_switch_attrs[] = { | |
127 | &attr_available_governors.attr, | |
128 | &attr_current_driver.attr, | |
129 | &attr_current_governor.attr, | |
130 | NULL | |
131 | }; | |
132 | ||
133 | static struct attribute_group cpuclass_attr_group = { | |
134 | .attrs = cpuclass_default_attrs, | |
135 | .name = "cpuidle", | |
136 | }; | |
137 | ||
138 | /** | |
139 | * cpuidle_add_class_sysfs - add CPU global sysfs attributes | |
140 | */ | |
141 | int cpuidle_add_class_sysfs(struct sysdev_class *cls) | |
142 | { | |
143 | if (sysfs_switch) | |
144 | cpuclass_attr_group.attrs = cpuclass_switch_attrs; | |
145 | ||
146 | return sysfs_create_group(&cls->kset.kobj, &cpuclass_attr_group); | |
147 | } | |
148 | ||
149 | /** | |
150 | * cpuidle_remove_class_sysfs - remove CPU global sysfs attributes | |
151 | */ | |
152 | void cpuidle_remove_class_sysfs(struct sysdev_class *cls) | |
153 | { | |
154 | sysfs_remove_group(&cls->kset.kobj, &cpuclass_attr_group); | |
155 | } | |
156 | ||
157 | struct cpuidle_attr { | |
158 | struct attribute attr; | |
159 | ssize_t (*show)(struct cpuidle_device *, char *); | |
160 | ssize_t (*store)(struct cpuidle_device *, const char *, size_t count); | |
161 | }; | |
162 | ||
163 | #define define_one_ro(_name, show) \ | |
164 | static struct cpuidle_attr attr_##_name = __ATTR(_name, 0444, show, NULL) | |
165 | #define define_one_rw(_name, show, store) \ | |
166 | static struct cpuidle_attr attr_##_name = __ATTR(_name, 0644, show, store) | |
167 | ||
168 | #define kobj_to_cpuidledev(k) container_of(k, struct cpuidle_device, kobj) | |
169 | #define attr_to_cpuidleattr(a) container_of(a, struct cpuidle_attr, attr) | |
170 | static ssize_t cpuidle_show(struct kobject * kobj, struct attribute * attr ,char * buf) | |
171 | { | |
172 | int ret = -EIO; | |
173 | struct cpuidle_device *dev = kobj_to_cpuidledev(kobj); | |
174 | struct cpuidle_attr * cattr = attr_to_cpuidleattr(attr); | |
175 | ||
176 | if (cattr->show) { | |
177 | mutex_lock(&cpuidle_lock); | |
178 | ret = cattr->show(dev, buf); | |
179 | mutex_unlock(&cpuidle_lock); | |
180 | } | |
181 | return ret; | |
182 | } | |
183 | ||
184 | static ssize_t cpuidle_store(struct kobject * kobj, struct attribute * attr, | |
185 | const char * buf, size_t count) | |
186 | { | |
187 | int ret = -EIO; | |
188 | struct cpuidle_device *dev = kobj_to_cpuidledev(kobj); | |
189 | struct cpuidle_attr * cattr = attr_to_cpuidleattr(attr); | |
190 | ||
191 | if (cattr->store) { | |
192 | mutex_lock(&cpuidle_lock); | |
193 | ret = cattr->store(dev, buf, count); | |
194 | mutex_unlock(&cpuidle_lock); | |
195 | } | |
196 | return ret; | |
197 | } | |
198 | ||
52cf25d0 | 199 | static const struct sysfs_ops cpuidle_sysfs_ops = { |
4f86d3a8 LB |
200 | .show = cpuidle_show, |
201 | .store = cpuidle_store, | |
202 | }; | |
203 | ||
204 | static void cpuidle_sysfs_release(struct kobject *kobj) | |
205 | { | |
206 | struct cpuidle_device *dev = kobj_to_cpuidledev(kobj); | |
207 | ||
208 | complete(&dev->kobj_unregister); | |
209 | } | |
210 | ||
211 | static struct kobj_type ktype_cpuidle = { | |
212 | .sysfs_ops = &cpuidle_sysfs_ops, | |
213 | .release = cpuidle_sysfs_release, | |
214 | }; | |
215 | ||
216 | struct cpuidle_state_attr { | |
217 | struct attribute attr; | |
218 | ssize_t (*show)(struct cpuidle_state *, char *); | |
219 | ssize_t (*store)(struct cpuidle_state *, const char *, size_t); | |
220 | }; | |
221 | ||
222 | #define define_one_state_ro(_name, show) \ | |
223 | static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0444, show, NULL) | |
224 | ||
225 | #define define_show_state_function(_name) \ | |
226 | static ssize_t show_state_##_name(struct cpuidle_state *state, char *buf) \ | |
227 | { \ | |
228 | return sprintf(buf, "%u\n", state->_name);\ | |
229 | } | |
230 | ||
8b78cf60 YY |
231 | #define define_show_state_ull_function(_name) \ |
232 | static ssize_t show_state_##_name(struct cpuidle_state *state, char *buf) \ | |
233 | { \ | |
234 | return sprintf(buf, "%llu\n", state->_name);\ | |
235 | } | |
236 | ||
4fcb2fcd VP |
237 | #define define_show_state_str_function(_name) \ |
238 | static ssize_t show_state_##_name(struct cpuidle_state *state, char *buf) \ | |
239 | { \ | |
240 | if (state->_name[0] == '\0')\ | |
241 | return sprintf(buf, "<null>\n");\ | |
242 | return sprintf(buf, "%s\n", state->_name);\ | |
4f86d3a8 LB |
243 | } |
244 | ||
245 | define_show_state_function(exit_latency) | |
246 | define_show_state_function(power_usage) | |
8b78cf60 YY |
247 | define_show_state_ull_function(usage) |
248 | define_show_state_ull_function(time) | |
4fcb2fcd VP |
249 | define_show_state_str_function(name) |
250 | define_show_state_str_function(desc) | |
251 | ||
4f86d3a8 | 252 | define_one_state_ro(name, show_state_name); |
4fcb2fcd | 253 | define_one_state_ro(desc, show_state_desc); |
4f86d3a8 LB |
254 | define_one_state_ro(latency, show_state_exit_latency); |
255 | define_one_state_ro(power, show_state_power_usage); | |
256 | define_one_state_ro(usage, show_state_usage); | |
257 | define_one_state_ro(time, show_state_time); | |
258 | ||
259 | static struct attribute *cpuidle_state_default_attrs[] = { | |
260 | &attr_name.attr, | |
4fcb2fcd | 261 | &attr_desc.attr, |
4f86d3a8 LB |
262 | &attr_latency.attr, |
263 | &attr_power.attr, | |
264 | &attr_usage.attr, | |
265 | &attr_time.attr, | |
266 | NULL | |
267 | }; | |
268 | ||
269 | #define kobj_to_state_obj(k) container_of(k, struct cpuidle_state_kobj, kobj) | |
270 | #define kobj_to_state(k) (kobj_to_state_obj(k)->state) | |
271 | #define attr_to_stateattr(a) container_of(a, struct cpuidle_state_attr, attr) | |
272 | static ssize_t cpuidle_state_show(struct kobject * kobj, | |
273 | struct attribute * attr ,char * buf) | |
274 | { | |
275 | int ret = -EIO; | |
276 | struct cpuidle_state *state = kobj_to_state(kobj); | |
277 | struct cpuidle_state_attr * cattr = attr_to_stateattr(attr); | |
278 | ||
279 | if (cattr->show) | |
280 | ret = cattr->show(state, buf); | |
281 | ||
282 | return ret; | |
283 | } | |
284 | ||
52cf25d0 | 285 | static const struct sysfs_ops cpuidle_state_sysfs_ops = { |
4f86d3a8 LB |
286 | .show = cpuidle_state_show, |
287 | }; | |
288 | ||
289 | static void cpuidle_state_sysfs_release(struct kobject *kobj) | |
290 | { | |
291 | struct cpuidle_state_kobj *state_obj = kobj_to_state_obj(kobj); | |
292 | ||
293 | complete(&state_obj->kobj_unregister); | |
294 | } | |
295 | ||
296 | static struct kobj_type ktype_state_cpuidle = { | |
297 | .sysfs_ops = &cpuidle_state_sysfs_ops, | |
298 | .default_attrs = cpuidle_state_default_attrs, | |
299 | .release = cpuidle_state_sysfs_release, | |
300 | }; | |
301 | ||
302 | static void inline cpuidle_free_state_kobj(struct cpuidle_device *device, int i) | |
303 | { | |
c10997f6 | 304 | kobject_put(&device->kobjs[i]->kobj); |
4f86d3a8 LB |
305 | wait_for_completion(&device->kobjs[i]->kobj_unregister); |
306 | kfree(device->kobjs[i]); | |
307 | device->kobjs[i] = NULL; | |
308 | } | |
309 | ||
310 | /** | |
311 | * cpuidle_add_driver_sysfs - adds driver-specific sysfs attributes | |
312 | * @device: the target device | |
313 | */ | |
314 | int cpuidle_add_state_sysfs(struct cpuidle_device *device) | |
315 | { | |
316 | int i, ret = -ENOMEM; | |
317 | struct cpuidle_state_kobj *kobj; | |
318 | ||
319 | /* state statistics */ | |
320 | for (i = 0; i < device->state_count; i++) { | |
321 | kobj = kzalloc(sizeof(struct cpuidle_state_kobj), GFP_KERNEL); | |
322 | if (!kobj) | |
323 | goto error_state; | |
324 | kobj->state = &device->states[i]; | |
325 | init_completion(&kobj->kobj_unregister); | |
326 | ||
94f57f33 GKH |
327 | ret = kobject_init_and_add(&kobj->kobj, &ktype_state_cpuidle, &device->kobj, |
328 | "state%d", i); | |
4f86d3a8 LB |
329 | if (ret) { |
330 | kfree(kobj); | |
331 | goto error_state; | |
332 | } | |
94f57f33 | 333 | kobject_uevent(&kobj->kobj, KOBJ_ADD); |
4f86d3a8 LB |
334 | device->kobjs[i] = kobj; |
335 | } | |
336 | ||
337 | return 0; | |
338 | ||
339 | error_state: | |
340 | for (i = i - 1; i >= 0; i--) | |
341 | cpuidle_free_state_kobj(device, i); | |
342 | return ret; | |
343 | } | |
344 | ||
345 | /** | |
346 | * cpuidle_remove_driver_sysfs - removes driver-specific sysfs attributes | |
347 | * @device: the target device | |
348 | */ | |
349 | void cpuidle_remove_state_sysfs(struct cpuidle_device *device) | |
350 | { | |
351 | int i; | |
352 | ||
353 | for (i = 0; i < device->state_count; i++) | |
354 | cpuidle_free_state_kobj(device, i); | |
355 | } | |
356 | ||
357 | /** | |
358 | * cpuidle_add_sysfs - creates a sysfs instance for the target device | |
359 | * @sysdev: the target device | |
360 | */ | |
361 | int cpuidle_add_sysfs(struct sys_device *sysdev) | |
362 | { | |
363 | int cpu = sysdev->id; | |
364 | struct cpuidle_device *dev; | |
94f57f33 | 365 | int error; |
4f86d3a8 LB |
366 | |
367 | dev = per_cpu(cpuidle_devices, cpu); | |
94f57f33 GKH |
368 | error = kobject_init_and_add(&dev->kobj, &ktype_cpuidle, &sysdev->kobj, |
369 | "cpuidle"); | |
370 | if (!error) | |
371 | kobject_uevent(&dev->kobj, KOBJ_ADD); | |
372 | return error; | |
4f86d3a8 LB |
373 | } |
374 | ||
375 | /** | |
376 | * cpuidle_remove_sysfs - deletes a sysfs instance on the target device | |
377 | * @sysdev: the target device | |
378 | */ | |
379 | void cpuidle_remove_sysfs(struct sys_device *sysdev) | |
380 | { | |
381 | int cpu = sysdev->id; | |
382 | struct cpuidle_device *dev; | |
383 | ||
384 | dev = per_cpu(cpuidle_devices, cpu); | |
c10997f6 | 385 | kobject_put(&dev->kobj); |
4f86d3a8 | 386 | } |