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