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