x86 idle APM: deprecate CONFIG_APM_CPU_IDLE
[deliverable/linux.git] / kernel / pm_qos_params.c
1 /*
2 * This module exposes the interface to kernel space for specifying
3 * QoS dependencies. It provides infrastructure for registration of:
4 *
5 * Dependents on a QoS value : register requests
6 * Watchers of QoS value : get notified when target QoS value changes
7 *
8 * This QoS design is best effort based. Dependents register their QoS needs.
9 * Watchers register to keep track of the current QoS needs of the system.
10 *
11 * There are 3 basic classes of QoS parameter: latency, timeout, throughput
12 * each have defined units:
13 * latency: usec
14 * timeout: usec <-- currently not used.
15 * throughput: kbs (kilo byte / sec)
16 *
17 * There are lists of pm_qos_objects each one wrapping requests, notifiers
18 *
19 * User mode requests on a QOS parameter register themselves to the
20 * subsystem by opening the device node /dev/... and writing there request to
21 * the node. As long as the process holds a file handle open to the node the
22 * client continues to be accounted for. Upon file release the usermode
23 * request is removed and a new qos target is computed. This way when the
24 * request that the application has is cleaned up when closes the file
25 * pointer or exits the pm_qos_object will get an opportunity to clean up.
26 *
27 * Mark Gross <mgross@linux.intel.com>
28 */
29
30 /*#define DEBUG*/
31
32 #include <linux/pm_qos_params.h>
33 #include <linux/sched.h>
34 #include <linux/spinlock.h>
35 #include <linux/slab.h>
36 #include <linux/time.h>
37 #include <linux/fs.h>
38 #include <linux/device.h>
39 #include <linux/miscdevice.h>
40 #include <linux/string.h>
41 #include <linux/platform_device.h>
42 #include <linux/init.h>
43
44 #include <linux/uaccess.h>
45
46 /*
47 * locking rule: all changes to requests or notifiers lists
48 * or pm_qos_object list and pm_qos_objects need to happen with pm_qos_lock
49 * held, taken with _irqsave. One lock to rule them all
50 */
51 enum pm_qos_type {
52 PM_QOS_MAX, /* return the largest value */
53 PM_QOS_MIN /* return the smallest value */
54 };
55
56 /*
57 * Note: The lockless read path depends on the CPU accessing
58 * target_value atomically. Atomic access is only guaranteed on all CPU
59 * types linux supports for 32 bit quantites
60 */
61 struct pm_qos_object {
62 struct plist_head requests;
63 struct blocking_notifier_head *notifiers;
64 struct miscdevice pm_qos_power_miscdev;
65 char *name;
66 s32 target_value; /* Do not change to 64 bit */
67 s32 default_value;
68 enum pm_qos_type type;
69 };
70
71 static DEFINE_SPINLOCK(pm_qos_lock);
72
73 static struct pm_qos_object null_pm_qos;
74 static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier);
75 static struct pm_qos_object cpu_dma_pm_qos = {
76 .requests = PLIST_HEAD_INIT(cpu_dma_pm_qos.requests, pm_qos_lock),
77 .notifiers = &cpu_dma_lat_notifier,
78 .name = "cpu_dma_latency",
79 .target_value = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE,
80 .default_value = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE,
81 .type = PM_QOS_MIN,
82 };
83
84 static BLOCKING_NOTIFIER_HEAD(network_lat_notifier);
85 static struct pm_qos_object network_lat_pm_qos = {
86 .requests = PLIST_HEAD_INIT(network_lat_pm_qos.requests, pm_qos_lock),
87 .notifiers = &network_lat_notifier,
88 .name = "network_latency",
89 .target_value = PM_QOS_NETWORK_LAT_DEFAULT_VALUE,
90 .default_value = PM_QOS_NETWORK_LAT_DEFAULT_VALUE,
91 .type = PM_QOS_MIN
92 };
93
94
95 static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier);
96 static struct pm_qos_object network_throughput_pm_qos = {
97 .requests = PLIST_HEAD_INIT(network_throughput_pm_qos.requests, pm_qos_lock),
98 .notifiers = &network_throughput_notifier,
99 .name = "network_throughput",
100 .target_value = PM_QOS_NETWORK_THROUGHPUT_DEFAULT_VALUE,
101 .default_value = PM_QOS_NETWORK_THROUGHPUT_DEFAULT_VALUE,
102 .type = PM_QOS_MAX,
103 };
104
105
106 static struct pm_qos_object *pm_qos_array[] = {
107 &null_pm_qos,
108 &cpu_dma_pm_qos,
109 &network_lat_pm_qos,
110 &network_throughput_pm_qos
111 };
112
113 static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
114 size_t count, loff_t *f_pos);
115 static int pm_qos_power_open(struct inode *inode, struct file *filp);
116 static int pm_qos_power_release(struct inode *inode, struct file *filp);
117
118 static const struct file_operations pm_qos_power_fops = {
119 .write = pm_qos_power_write,
120 .open = pm_qos_power_open,
121 .release = pm_qos_power_release,
122 .llseek = noop_llseek,
123 };
124
125 /* unlocked internal variant */
126 static inline int pm_qos_get_value(struct pm_qos_object *o)
127 {
128 if (plist_head_empty(&o->requests))
129 return o->default_value;
130
131 switch (o->type) {
132 case PM_QOS_MIN:
133 return plist_first(&o->requests)->prio;
134
135 case PM_QOS_MAX:
136 return plist_last(&o->requests)->prio;
137
138 default:
139 /* runtime check for not using enum */
140 BUG();
141 }
142 }
143
144 static inline s32 pm_qos_read_value(struct pm_qos_object *o)
145 {
146 return o->target_value;
147 }
148
149 static inline void pm_qos_set_value(struct pm_qos_object *o, s32 value)
150 {
151 o->target_value = value;
152 }
153
154 static void update_target(struct pm_qos_object *o, struct plist_node *node,
155 int del, int value)
156 {
157 unsigned long flags;
158 int prev_value, curr_value;
159
160 spin_lock_irqsave(&pm_qos_lock, flags);
161 prev_value = pm_qos_get_value(o);
162 /* PM_QOS_DEFAULT_VALUE is a signal that the value is unchanged */
163 if (value != PM_QOS_DEFAULT_VALUE) {
164 /*
165 * to change the list, we atomically remove, reinit
166 * with new value and add, then see if the extremal
167 * changed
168 */
169 plist_del(node, &o->requests);
170 plist_node_init(node, value);
171 plist_add(node, &o->requests);
172 } else if (del) {
173 plist_del(node, &o->requests);
174 } else {
175 plist_add(node, &o->requests);
176 }
177 curr_value = pm_qos_get_value(o);
178 pm_qos_set_value(o, curr_value);
179 spin_unlock_irqrestore(&pm_qos_lock, flags);
180
181 if (prev_value != curr_value)
182 blocking_notifier_call_chain(o->notifiers,
183 (unsigned long)curr_value,
184 NULL);
185 }
186
187 static int register_pm_qos_misc(struct pm_qos_object *qos)
188 {
189 qos->pm_qos_power_miscdev.minor = MISC_DYNAMIC_MINOR;
190 qos->pm_qos_power_miscdev.name = qos->name;
191 qos->pm_qos_power_miscdev.fops = &pm_qos_power_fops;
192
193 return misc_register(&qos->pm_qos_power_miscdev);
194 }
195
196 static int find_pm_qos_object_by_minor(int minor)
197 {
198 int pm_qos_class;
199
200 for (pm_qos_class = 0;
201 pm_qos_class < PM_QOS_NUM_CLASSES; pm_qos_class++) {
202 if (minor ==
203 pm_qos_array[pm_qos_class]->pm_qos_power_miscdev.minor)
204 return pm_qos_class;
205 }
206 return -1;
207 }
208
209 /**
210 * pm_qos_request - returns current system wide qos expectation
211 * @pm_qos_class: identification of which qos value is requested
212 *
213 * This function returns the current target value.
214 */
215 int pm_qos_request(int pm_qos_class)
216 {
217 return pm_qos_read_value(pm_qos_array[pm_qos_class]);
218 }
219 EXPORT_SYMBOL_GPL(pm_qos_request);
220
221 int pm_qos_request_active(struct pm_qos_request_list *req)
222 {
223 return req->pm_qos_class != 0;
224 }
225 EXPORT_SYMBOL_GPL(pm_qos_request_active);
226
227 /**
228 * pm_qos_add_request - inserts new qos request into the list
229 * @dep: pointer to a preallocated handle
230 * @pm_qos_class: identifies which list of qos request to use
231 * @value: defines the qos request
232 *
233 * This function inserts a new entry in the pm_qos_class list of requested qos
234 * performance characteristics. It recomputes the aggregate QoS expectations
235 * for the pm_qos_class of parameters and initializes the pm_qos_request_list
236 * handle. Caller needs to save this handle for later use in updates and
237 * removal.
238 */
239
240 void pm_qos_add_request(struct pm_qos_request_list *dep,
241 int pm_qos_class, s32 value)
242 {
243 struct pm_qos_object *o = pm_qos_array[pm_qos_class];
244 int new_value;
245
246 if (pm_qos_request_active(dep)) {
247 WARN(1, KERN_ERR "pm_qos_add_request() called for already added request\n");
248 return;
249 }
250 if (value == PM_QOS_DEFAULT_VALUE)
251 new_value = o->default_value;
252 else
253 new_value = value;
254 plist_node_init(&dep->list, new_value);
255 dep->pm_qos_class = pm_qos_class;
256 update_target(o, &dep->list, 0, PM_QOS_DEFAULT_VALUE);
257 }
258 EXPORT_SYMBOL_GPL(pm_qos_add_request);
259
260 /**
261 * pm_qos_update_request - modifies an existing qos request
262 * @pm_qos_req : handle to list element holding a pm_qos request to use
263 * @value: defines the qos request
264 *
265 * Updates an existing qos request for the pm_qos_class of parameters along
266 * with updating the target pm_qos_class value.
267 *
268 * Attempts are made to make this code callable on hot code paths.
269 */
270 void pm_qos_update_request(struct pm_qos_request_list *pm_qos_req,
271 s32 new_value)
272 {
273 s32 temp;
274 struct pm_qos_object *o;
275
276 if (!pm_qos_req) /*guard against callers passing in null */
277 return;
278
279 if (!pm_qos_request_active(pm_qos_req)) {
280 WARN(1, KERN_ERR "pm_qos_update_request() called for unknown object\n");
281 return;
282 }
283
284 o = pm_qos_array[pm_qos_req->pm_qos_class];
285
286 if (new_value == PM_QOS_DEFAULT_VALUE)
287 temp = o->default_value;
288 else
289 temp = new_value;
290
291 if (temp != pm_qos_req->list.prio)
292 update_target(o, &pm_qos_req->list, 0, temp);
293 }
294 EXPORT_SYMBOL_GPL(pm_qos_update_request);
295
296 /**
297 * pm_qos_remove_request - modifies an existing qos request
298 * @pm_qos_req: handle to request list element
299 *
300 * Will remove pm qos request from the list of requests and
301 * recompute the current target value for the pm_qos_class. Call this
302 * on slow code paths.
303 */
304 void pm_qos_remove_request(struct pm_qos_request_list *pm_qos_req)
305 {
306 struct pm_qos_object *o;
307
308 if (pm_qos_req == NULL)
309 return;
310 /* silent return to keep pcm code cleaner */
311
312 if (!pm_qos_request_active(pm_qos_req)) {
313 WARN(1, KERN_ERR "pm_qos_remove_request() called for unknown object\n");
314 return;
315 }
316
317 o = pm_qos_array[pm_qos_req->pm_qos_class];
318 update_target(o, &pm_qos_req->list, 1, PM_QOS_DEFAULT_VALUE);
319 memset(pm_qos_req, 0, sizeof(*pm_qos_req));
320 }
321 EXPORT_SYMBOL_GPL(pm_qos_remove_request);
322
323 /**
324 * pm_qos_add_notifier - sets notification entry for changes to target value
325 * @pm_qos_class: identifies which qos target changes should be notified.
326 * @notifier: notifier block managed by caller.
327 *
328 * will register the notifier into a notification chain that gets called
329 * upon changes to the pm_qos_class target value.
330 */
331 int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier)
332 {
333 int retval;
334
335 retval = blocking_notifier_chain_register(
336 pm_qos_array[pm_qos_class]->notifiers, notifier);
337
338 return retval;
339 }
340 EXPORT_SYMBOL_GPL(pm_qos_add_notifier);
341
342 /**
343 * pm_qos_remove_notifier - deletes notification entry from chain.
344 * @pm_qos_class: identifies which qos target changes are notified.
345 * @notifier: notifier block to be removed.
346 *
347 * will remove the notifier from the notification chain that gets called
348 * upon changes to the pm_qos_class target value.
349 */
350 int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier)
351 {
352 int retval;
353
354 retval = blocking_notifier_chain_unregister(
355 pm_qos_array[pm_qos_class]->notifiers, notifier);
356
357 return retval;
358 }
359 EXPORT_SYMBOL_GPL(pm_qos_remove_notifier);
360
361 static int pm_qos_power_open(struct inode *inode, struct file *filp)
362 {
363 long pm_qos_class;
364
365 pm_qos_class = find_pm_qos_object_by_minor(iminor(inode));
366 if (pm_qos_class >= 0) {
367 struct pm_qos_request_list *req = kzalloc(sizeof(*req), GFP_KERNEL);
368 if (!req)
369 return -ENOMEM;
370
371 pm_qos_add_request(req, pm_qos_class, PM_QOS_DEFAULT_VALUE);
372 filp->private_data = req;
373
374 if (filp->private_data)
375 return 0;
376 }
377 return -EPERM;
378 }
379
380 static int pm_qos_power_release(struct inode *inode, struct file *filp)
381 {
382 struct pm_qos_request_list *req;
383
384 req = filp->private_data;
385 pm_qos_remove_request(req);
386 kfree(req);
387
388 return 0;
389 }
390
391
392 static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
393 size_t count, loff_t *f_pos)
394 {
395 s32 value;
396 int x;
397 char ascii_value[11];
398 struct pm_qos_request_list *pm_qos_req;
399
400 if (count == sizeof(s32)) {
401 if (copy_from_user(&value, buf, sizeof(s32)))
402 return -EFAULT;
403 } else if (count == 11) { /* len('0x12345678/0') */
404 if (copy_from_user(ascii_value, buf, 11))
405 return -EFAULT;
406 if (strlen(ascii_value) != 10)
407 return -EINVAL;
408 x = sscanf(ascii_value, "%x", &value);
409 if (x != 1)
410 return -EINVAL;
411 pr_debug("%s, %d, 0x%x\n", ascii_value, x, value);
412 } else
413 return -EINVAL;
414
415 pm_qos_req = filp->private_data;
416 pm_qos_update_request(pm_qos_req, value);
417
418 return count;
419 }
420
421
422 static int __init pm_qos_power_init(void)
423 {
424 int ret = 0;
425
426 ret = register_pm_qos_misc(&cpu_dma_pm_qos);
427 if (ret < 0) {
428 printk(KERN_ERR "pm_qos_param: cpu_dma_latency setup failed\n");
429 return ret;
430 }
431 ret = register_pm_qos_misc(&network_lat_pm_qos);
432 if (ret < 0) {
433 printk(KERN_ERR "pm_qos_param: network_latency setup failed\n");
434 return ret;
435 }
436 ret = register_pm_qos_misc(&network_throughput_pm_qos);
437 if (ret < 0)
438 printk(KERN_ERR
439 "pm_qos_param: network_throughput setup failed\n");
440
441 return ret;
442 }
443
444 late_initcall(pm_qos_power_init);
This page took 0.057959 seconds and 5 git commands to generate.