blob: 788c4cfe0cdb3b0bd94d903ecb27a5608dee1cd9 [file] [log] [blame]
Mark Grossd82b3512008-02-04 22:30:08 -08001/*
2 * This module exposes the interface to kernel space for specifying
3 * QoS dependencies. It provides infrastructure for registration of:
4 *
Mark Grossed771342010-05-06 01:59:26 +02005 * Dependents on a QoS value : register requests
Mark Grossd82b3512008-02-04 22:30:08 -08006 * 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 *
Mark Grossed771342010-05-06 01:59:26 +020017 * There are lists of pm_qos_objects each one wrapping requests, notifiers
Mark Grossd82b3512008-02-04 22:30:08 -080018 *
Mark Grossed771342010-05-06 01:59:26 +020019 * User mode requests on a QOS parameter register themselves to the
Mark Grossd82b3512008-02-04 22:30:08 -080020 * 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
Mark Grossed771342010-05-06 01:59:26 +020023 * 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
Mark Grossd82b3512008-02-04 22:30:08 -080025 * pointer or exits the pm_qos_object will get an opportunity to clean up.
26 *
Richard Hughesbf1db692008-08-05 13:01:35 -070027 * Mark Gross <mgross@linux.intel.com>
Mark Grossd82b3512008-02-04 22:30:08 -080028 */
29
Mark Grossed771342010-05-06 01:59:26 +020030/*#define DEBUG*/
31
Jean Pihete8db0be2011-08-25 15:35:03 +020032#include <linux/pm_qos.h>
Mark Grossd82b3512008-02-04 22:30:08 -080033#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>
Rafael J. Wysocki0775a602011-05-27 00:05:23 +020043#include <linux/kernel.h>
Mark Grossd82b3512008-02-04 22:30:08 -080044
45#include <linux/uaccess.h>
46
47/*
Jean Pihetcc749982011-08-25 15:35:12 +020048 * locking rule: all changes to constraints or notifiers lists
Mark Grossd82b3512008-02-04 22:30:08 -080049 * or pm_qos_object list and pm_qos_objects need to happen with pm_qos_lock
50 * held, taken with _irqsave. One lock to rule them all
51 */
James Bottomley5f279842010-07-19 02:00:18 +020052enum pm_qos_type {
53 PM_QOS_MAX, /* return the largest value */
54 PM_QOS_MIN /* return the smallest value */
55};
Mark Grossd82b3512008-02-04 22:30:08 -080056
Tim Chen333c5ae2011-02-11 12:49:04 -080057/*
58 * Note: The lockless read path depends on the CPU accessing
59 * target_value atomically. Atomic access is only guaranteed on all CPU
60 * types linux supports for 32 bit quantites
61 */
Mark Grossd82b3512008-02-04 22:30:08 -080062struct pm_qos_object {
Jean Pihetcc749982011-08-25 15:35:12 +020063 struct plist_head constraints;
Mark Grossd82b3512008-02-04 22:30:08 -080064 struct blocking_notifier_head *notifiers;
65 struct miscdevice pm_qos_power_miscdev;
66 char *name;
Tim Chen333c5ae2011-02-11 12:49:04 -080067 s32 target_value; /* Do not change to 64 bit */
Mark Grossd82b3512008-02-04 22:30:08 -080068 s32 default_value;
James Bottomley5f279842010-07-19 02:00:18 +020069 enum pm_qos_type type;
Mark Grossd82b3512008-02-04 22:30:08 -080070};
71
James Bottomley5f279842010-07-19 02:00:18 +020072static DEFINE_SPINLOCK(pm_qos_lock);
73
Mark Grossd82b3512008-02-04 22:30:08 -080074static struct pm_qos_object null_pm_qos;
75static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier);
76static struct pm_qos_object cpu_dma_pm_qos = {
Jean Pihetcc749982011-08-25 15:35:12 +020077 .constraints = PLIST_HEAD_INIT(cpu_dma_pm_qos.constraints),
Mark Grossd82b3512008-02-04 22:30:08 -080078 .notifiers = &cpu_dma_lat_notifier,
79 .name = "cpu_dma_latency",
Tim Chen333c5ae2011-02-11 12:49:04 -080080 .target_value = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE,
81 .default_value = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE,
James Bottomley5f279842010-07-19 02:00:18 +020082 .type = PM_QOS_MIN,
Mark Grossd82b3512008-02-04 22:30:08 -080083};
84
85static BLOCKING_NOTIFIER_HEAD(network_lat_notifier);
86static struct pm_qos_object network_lat_pm_qos = {
Jean Pihetcc749982011-08-25 15:35:12 +020087 .constraints = PLIST_HEAD_INIT(network_lat_pm_qos.constraints),
Mark Grossd82b3512008-02-04 22:30:08 -080088 .notifiers = &network_lat_notifier,
89 .name = "network_latency",
Tim Chen333c5ae2011-02-11 12:49:04 -080090 .target_value = PM_QOS_NETWORK_LAT_DEFAULT_VALUE,
91 .default_value = PM_QOS_NETWORK_LAT_DEFAULT_VALUE,
James Bottomley5f279842010-07-19 02:00:18 +020092 .type = PM_QOS_MIN
Mark Grossd82b3512008-02-04 22:30:08 -080093};
94
95
96static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier);
97static struct pm_qos_object network_throughput_pm_qos = {
Jean Pihetcc749982011-08-25 15:35:12 +020098 .constraints = PLIST_HEAD_INIT(network_throughput_pm_qos.constraints),
Mark Grossd82b3512008-02-04 22:30:08 -080099 .notifiers = &network_throughput_notifier,
100 .name = "network_throughput",
Tim Chen333c5ae2011-02-11 12:49:04 -0800101 .target_value = PM_QOS_NETWORK_THROUGHPUT_DEFAULT_VALUE,
102 .default_value = PM_QOS_NETWORK_THROUGHPUT_DEFAULT_VALUE,
James Bottomley5f279842010-07-19 02:00:18 +0200103 .type = PM_QOS_MAX,
Mark Grossd82b3512008-02-04 22:30:08 -0800104};
105
106
107static struct pm_qos_object *pm_qos_array[] = {
108 &null_pm_qos,
109 &cpu_dma_pm_qos,
110 &network_lat_pm_qos,
111 &network_throughput_pm_qos
112};
113
Mark Grossd82b3512008-02-04 22:30:08 -0800114static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
115 size_t count, loff_t *f_pos);
Thomas Renningerf9b9e802011-02-28 22:06:34 +0100116static ssize_t pm_qos_power_read(struct file *filp, char __user *buf,
117 size_t count, loff_t *f_pos);
Mark Grossd82b3512008-02-04 22:30:08 -0800118static int pm_qos_power_open(struct inode *inode, struct file *filp);
119static int pm_qos_power_release(struct inode *inode, struct file *filp);
120
121static const struct file_operations pm_qos_power_fops = {
122 .write = pm_qos_power_write,
Thomas Renningerf9b9e802011-02-28 22:06:34 +0100123 .read = pm_qos_power_read,
Mark Grossd82b3512008-02-04 22:30:08 -0800124 .open = pm_qos_power_open,
125 .release = pm_qos_power_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200126 .llseek = noop_llseek,
Mark Grossd82b3512008-02-04 22:30:08 -0800127};
128
James Bottomley5f279842010-07-19 02:00:18 +0200129/* unlocked internal variant */
130static inline int pm_qos_get_value(struct pm_qos_object *o)
Mark Grossd82b3512008-02-04 22:30:08 -0800131{
Jean Pihetcc749982011-08-25 15:35:12 +0200132 if (plist_head_empty(&o->constraints))
James Bottomley5f279842010-07-19 02:00:18 +0200133 return o->default_value;
134
135 switch (o->type) {
136 case PM_QOS_MIN:
Jean Pihetcc749982011-08-25 15:35:12 +0200137 return plist_first(&o->constraints)->prio;
James Bottomley5f279842010-07-19 02:00:18 +0200138
139 case PM_QOS_MAX:
Jean Pihetcc749982011-08-25 15:35:12 +0200140 return plist_last(&o->constraints)->prio;
James Bottomley5f279842010-07-19 02:00:18 +0200141
142 default:
143 /* runtime check for not using enum */
144 BUG();
145 }
Mark Grossd82b3512008-02-04 22:30:08 -0800146}
147
Tim Chen333c5ae2011-02-11 12:49:04 -0800148static inline s32 pm_qos_read_value(struct pm_qos_object *o)
149{
150 return o->target_value;
151}
152
153static inline void pm_qos_set_value(struct pm_qos_object *o, s32 value)
154{
155 o->target_value = value;
156}
157
James Bottomley5f279842010-07-19 02:00:18 +0200158static void update_target(struct pm_qos_object *o, struct plist_node *node,
159 int del, int value)
Mark Grossd82b3512008-02-04 22:30:08 -0800160{
Mark Grossd82b3512008-02-04 22:30:08 -0800161 unsigned long flags;
James Bottomley5f279842010-07-19 02:00:18 +0200162 int prev_value, curr_value;
Mark Grossd82b3512008-02-04 22:30:08 -0800163
164 spin_lock_irqsave(&pm_qos_lock, flags);
James Bottomley5f279842010-07-19 02:00:18 +0200165 prev_value = pm_qos_get_value(o);
166 /* PM_QOS_DEFAULT_VALUE is a signal that the value is unchanged */
167 if (value != PM_QOS_DEFAULT_VALUE) {
168 /*
169 * to change the list, we atomically remove, reinit
170 * with new value and add, then see if the extremal
171 * changed
172 */
Jean Pihetcc749982011-08-25 15:35:12 +0200173 plist_del(node, &o->constraints);
James Bottomley5f279842010-07-19 02:00:18 +0200174 plist_node_init(node, value);
Jean Pihetcc749982011-08-25 15:35:12 +0200175 plist_add(node, &o->constraints);
James Bottomley5f279842010-07-19 02:00:18 +0200176 } else if (del) {
Jean Pihetcc749982011-08-25 15:35:12 +0200177 plist_del(node, &o->constraints);
James Bottomley5f279842010-07-19 02:00:18 +0200178 } else {
Jean Pihetcc749982011-08-25 15:35:12 +0200179 plist_add(node, &o->constraints);
Mark Grossd82b3512008-02-04 22:30:08 -0800180 }
James Bottomley5f279842010-07-19 02:00:18 +0200181 curr_value = pm_qos_get_value(o);
Tim Chen333c5ae2011-02-11 12:49:04 -0800182 pm_qos_set_value(o, curr_value);
Mark Grossd82b3512008-02-04 22:30:08 -0800183 spin_unlock_irqrestore(&pm_qos_lock, flags);
184
James Bottomley5f279842010-07-19 02:00:18 +0200185 if (prev_value != curr_value)
186 blocking_notifier_call_chain(o->notifiers,
187 (unsigned long)curr_value,
188 NULL);
Mark Grossd82b3512008-02-04 22:30:08 -0800189}
190
Mark Grossd82b3512008-02-04 22:30:08 -0800191/**
Mark Grossed771342010-05-06 01:59:26 +0200192 * pm_qos_request - returns current system wide qos expectation
Mark Grossd82b3512008-02-04 22:30:08 -0800193 * @pm_qos_class: identification of which qos value is requested
194 *
Tim Chen333c5ae2011-02-11 12:49:04 -0800195 * This function returns the current target value.
Mark Grossd82b3512008-02-04 22:30:08 -0800196 */
Mark Grossed771342010-05-06 01:59:26 +0200197int pm_qos_request(int pm_qos_class)
Mark Grossd82b3512008-02-04 22:30:08 -0800198{
Tim Chen333c5ae2011-02-11 12:49:04 -0800199 return pm_qos_read_value(pm_qos_array[pm_qos_class]);
Mark Grossd82b3512008-02-04 22:30:08 -0800200}
Mark Grossed771342010-05-06 01:59:26 +0200201EXPORT_SYMBOL_GPL(pm_qos_request);
Mark Grossd82b3512008-02-04 22:30:08 -0800202
Jean Pihetcc749982011-08-25 15:35:12 +0200203int pm_qos_request_active(struct pm_qos_request *req)
James Bottomley82f68252010-07-05 22:53:06 +0200204{
205 return req->pm_qos_class != 0;
206}
207EXPORT_SYMBOL_GPL(pm_qos_request_active);
208
Mark Grossd82b3512008-02-04 22:30:08 -0800209/**
Mark Grossed771342010-05-06 01:59:26 +0200210 * pm_qos_add_request - inserts new qos request into the list
Jean Pihetcc749982011-08-25 15:35:12 +0200211 * @req: pointer to a preallocated handle
Saravana Kannan25cc69e2010-08-26 20:18:43 +0200212 * @pm_qos_class: identifies which list of qos request to use
Mark Grossd82b3512008-02-04 22:30:08 -0800213 * @value: defines the qos request
214 *
215 * This function inserts a new entry in the pm_qos_class list of requested qos
Richard Hughesbf1db692008-08-05 13:01:35 -0700216 * performance characteristics. It recomputes the aggregate QoS expectations
Jean Pihetcc749982011-08-25 15:35:12 +0200217 * for the pm_qos_class of parameters and initializes the pm_qos_request
Saravana Kannan25cc69e2010-08-26 20:18:43 +0200218 * handle. Caller needs to save this handle for later use in updates and
219 * removal.
Mark Grossd82b3512008-02-04 22:30:08 -0800220 */
Saravana Kannan25cc69e2010-08-26 20:18:43 +0200221
Jean Pihetcc749982011-08-25 15:35:12 +0200222void pm_qos_add_request(struct pm_qos_request *req,
James Bottomley82f68252010-07-05 22:53:06 +0200223 int pm_qos_class, s32 value)
Mark Grossd82b3512008-02-04 22:30:08 -0800224{
James Bottomley82f68252010-07-05 22:53:06 +0200225 struct pm_qos_object *o = pm_qos_array[pm_qos_class];
226 int new_value;
Mark Grossd82b3512008-02-04 22:30:08 -0800227
Jean Pihetcc749982011-08-25 15:35:12 +0200228 if (pm_qos_request_active(req)) {
James Bottomley82f68252010-07-05 22:53:06 +0200229 WARN(1, KERN_ERR "pm_qos_add_request() called for already added request\n");
230 return;
Mark Grossd82b3512008-02-04 22:30:08 -0800231 }
James Bottomley82f68252010-07-05 22:53:06 +0200232 if (value == PM_QOS_DEFAULT_VALUE)
233 new_value = o->default_value;
234 else
235 new_value = value;
Jean Pihetcc749982011-08-25 15:35:12 +0200236 plist_node_init(&req->node, new_value);
237 req->pm_qos_class = pm_qos_class;
238 update_target(o, &req->node, 0, PM_QOS_DEFAULT_VALUE);
Mark Grossd82b3512008-02-04 22:30:08 -0800239}
Mark Grossed771342010-05-06 01:59:26 +0200240EXPORT_SYMBOL_GPL(pm_qos_add_request);
Mark Grossd82b3512008-02-04 22:30:08 -0800241
242/**
Mark Grossed771342010-05-06 01:59:26 +0200243 * pm_qos_update_request - modifies an existing qos request
Jean Pihetcc749982011-08-25 15:35:12 +0200244 * @req : handle to list element holding a pm_qos request to use
Mark Grossd82b3512008-02-04 22:30:08 -0800245 * @value: defines the qos request
246 *
Mark Grossed771342010-05-06 01:59:26 +0200247 * Updates an existing qos request for the pm_qos_class of parameters along
Mark Grossd82b3512008-02-04 22:30:08 -0800248 * with updating the target pm_qos_class value.
249 *
Mark Grossed771342010-05-06 01:59:26 +0200250 * Attempts are made to make this code callable on hot code paths.
Mark Grossd82b3512008-02-04 22:30:08 -0800251 */
Jean Pihetcc749982011-08-25 15:35:12 +0200252void pm_qos_update_request(struct pm_qos_request *req,
James Bottomley5f279842010-07-19 02:00:18 +0200253 s32 new_value)
Mark Grossd82b3512008-02-04 22:30:08 -0800254{
Mark Grossed771342010-05-06 01:59:26 +0200255 s32 temp;
James Bottomley5f279842010-07-19 02:00:18 +0200256 struct pm_qos_object *o;
Mark Grossd82b3512008-02-04 22:30:08 -0800257
Jean Pihetcc749982011-08-25 15:35:12 +0200258 if (!req) /*guard against callers passing in null */
James Bottomley5f279842010-07-19 02:00:18 +0200259 return;
Mark Grossed771342010-05-06 01:59:26 +0200260
Jean Pihetcc749982011-08-25 15:35:12 +0200261 if (!pm_qos_request_active(req)) {
James Bottomley82f68252010-07-05 22:53:06 +0200262 WARN(1, KERN_ERR "pm_qos_update_request() called for unknown object\n");
263 return;
264 }
265
Jean Pihetcc749982011-08-25 15:35:12 +0200266 o = pm_qos_array[req->pm_qos_class];
James Bottomley5f279842010-07-19 02:00:18 +0200267
268 if (new_value == PM_QOS_DEFAULT_VALUE)
269 temp = o->default_value;
270 else
271 temp = new_value;
272
Jean Pihetcc749982011-08-25 15:35:12 +0200273 if (temp != req->node.prio)
274 update_target(o, &req->node, 0, temp);
Mark Grossd82b3512008-02-04 22:30:08 -0800275}
Mark Grossed771342010-05-06 01:59:26 +0200276EXPORT_SYMBOL_GPL(pm_qos_update_request);
Mark Grossd82b3512008-02-04 22:30:08 -0800277
278/**
Mark Grossed771342010-05-06 01:59:26 +0200279 * pm_qos_remove_request - modifies an existing qos request
Jean Pihetcc749982011-08-25 15:35:12 +0200280 * @req: handle to request list element
Mark Grossd82b3512008-02-04 22:30:08 -0800281 *
Jean Pihetcc749982011-08-25 15:35:12 +0200282 * Will remove pm qos request from the list of constraints and
Mark Grossed771342010-05-06 01:59:26 +0200283 * recompute the current target value for the pm_qos_class. Call this
284 * on slow code paths.
Mark Grossd82b3512008-02-04 22:30:08 -0800285 */
Jean Pihetcc749982011-08-25 15:35:12 +0200286void pm_qos_remove_request(struct pm_qos_request *req)
Mark Grossd82b3512008-02-04 22:30:08 -0800287{
James Bottomley5f279842010-07-19 02:00:18 +0200288 struct pm_qos_object *o;
Mark Grossd82b3512008-02-04 22:30:08 -0800289
Jean Pihetcc749982011-08-25 15:35:12 +0200290 if (req == NULL)
Mark Grossed771342010-05-06 01:59:26 +0200291 return;
292 /* silent return to keep pcm code cleaner */
293
Jean Pihetcc749982011-08-25 15:35:12 +0200294 if (!pm_qos_request_active(req)) {
James Bottomley82f68252010-07-05 22:53:06 +0200295 WARN(1, KERN_ERR "pm_qos_remove_request() called for unknown object\n");
296 return;
297 }
298
Jean Pihetcc749982011-08-25 15:35:12 +0200299 o = pm_qos_array[req->pm_qos_class];
300 update_target(o, &req->node, 1, PM_QOS_DEFAULT_VALUE);
301 memset(req, 0, sizeof(*req));
Mark Grossd82b3512008-02-04 22:30:08 -0800302}
Mark Grossed771342010-05-06 01:59:26 +0200303EXPORT_SYMBOL_GPL(pm_qos_remove_request);
Mark Grossd82b3512008-02-04 22:30:08 -0800304
305/**
306 * pm_qos_add_notifier - sets notification entry for changes to target value
307 * @pm_qos_class: identifies which qos target changes should be notified.
308 * @notifier: notifier block managed by caller.
309 *
310 * will register the notifier into a notification chain that gets called
Richard Hughesbf1db692008-08-05 13:01:35 -0700311 * upon changes to the pm_qos_class target value.
Mark Grossd82b3512008-02-04 22:30:08 -0800312 */
Mark Grossed771342010-05-06 01:59:26 +0200313int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier)
Mark Grossd82b3512008-02-04 22:30:08 -0800314{
315 int retval;
316
317 retval = blocking_notifier_chain_register(
318 pm_qos_array[pm_qos_class]->notifiers, notifier);
319
320 return retval;
321}
322EXPORT_SYMBOL_GPL(pm_qos_add_notifier);
323
324/**
325 * pm_qos_remove_notifier - deletes notification entry from chain.
326 * @pm_qos_class: identifies which qos target changes are notified.
327 * @notifier: notifier block to be removed.
328 *
329 * will remove the notifier from the notification chain that gets called
Richard Hughesbf1db692008-08-05 13:01:35 -0700330 * upon changes to the pm_qos_class target value.
Mark Grossd82b3512008-02-04 22:30:08 -0800331 */
332int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier)
333{
334 int retval;
335
336 retval = blocking_notifier_chain_unregister(
337 pm_qos_array[pm_qos_class]->notifiers, notifier);
338
339 return retval;
340}
341EXPORT_SYMBOL_GPL(pm_qos_remove_notifier);
342
Jean Pihet4a31a332011-08-25 15:35:20 +0200343/* User space interface to PM QoS classes via misc devices */
344static int register_pm_qos_misc(struct pm_qos_object *qos)
345{
346 qos->pm_qos_power_miscdev.minor = MISC_DYNAMIC_MINOR;
347 qos->pm_qos_power_miscdev.name = qos->name;
348 qos->pm_qos_power_miscdev.fops = &pm_qos_power_fops;
349
350 return misc_register(&qos->pm_qos_power_miscdev);
351}
352
353static int find_pm_qos_object_by_minor(int minor)
354{
355 int pm_qos_class;
356
357 for (pm_qos_class = 0;
358 pm_qos_class < PM_QOS_NUM_CLASSES; pm_qos_class++) {
359 if (minor ==
360 pm_qos_array[pm_qos_class]->pm_qos_power_miscdev.minor)
361 return pm_qos_class;
362 }
363 return -1;
364}
365
Mark Grossd82b3512008-02-04 22:30:08 -0800366static int pm_qos_power_open(struct inode *inode, struct file *filp)
367{
Mark Grossd82b3512008-02-04 22:30:08 -0800368 long pm_qos_class;
369
370 pm_qos_class = find_pm_qos_object_by_minor(iminor(inode));
371 if (pm_qos_class >= 0) {
Jean Pihetcc749982011-08-25 15:35:12 +0200372 struct pm_qos_request *req = kzalloc(sizeof(*req), GFP_KERNEL);
James Bottomley82f68252010-07-05 22:53:06 +0200373 if (!req)
374 return -ENOMEM;
375
376 pm_qos_add_request(req, pm_qos_class, PM_QOS_DEFAULT_VALUE);
377 filp->private_data = req;
Mark Grossed771342010-05-06 01:59:26 +0200378
379 if (filp->private_data)
Mark Grossd82b3512008-02-04 22:30:08 -0800380 return 0;
381 }
Mark Grossd82b3512008-02-04 22:30:08 -0800382 return -EPERM;
383}
384
385static int pm_qos_power_release(struct inode *inode, struct file *filp)
386{
Jean Pihetcc749982011-08-25 15:35:12 +0200387 struct pm_qos_request *req;
Mark Grossd82b3512008-02-04 22:30:08 -0800388
James Bottomley82f68252010-07-05 22:53:06 +0200389 req = filp->private_data;
Mark Grossed771342010-05-06 01:59:26 +0200390 pm_qos_remove_request(req);
James Bottomley82f68252010-07-05 22:53:06 +0200391 kfree(req);
Mark Grossd82b3512008-02-04 22:30:08 -0800392
393 return 0;
394}
395
Mark Grossed771342010-05-06 01:59:26 +0200396
Thomas Renningerf9b9e802011-02-28 22:06:34 +0100397static ssize_t pm_qos_power_read(struct file *filp, char __user *buf,
398 size_t count, loff_t *f_pos)
399{
400 s32 value;
401 unsigned long flags;
402 struct pm_qos_object *o;
Jean Pihetcc749982011-08-25 15:35:12 +0200403 struct pm_qos_request *req = filp->private_data;
Thomas Renningerf9b9e802011-02-28 22:06:34 +0100404
Jean Pihetcc749982011-08-25 15:35:12 +0200405 if (!req)
Thomas Renningerf9b9e802011-02-28 22:06:34 +0100406 return -EINVAL;
Jean Pihetcc749982011-08-25 15:35:12 +0200407 if (!pm_qos_request_active(req))
Thomas Renningerf9b9e802011-02-28 22:06:34 +0100408 return -EINVAL;
409
Jean Pihetcc749982011-08-25 15:35:12 +0200410 o = pm_qos_array[req->pm_qos_class];
Thomas Renningerf9b9e802011-02-28 22:06:34 +0100411 spin_lock_irqsave(&pm_qos_lock, flags);
412 value = pm_qos_get_value(o);
413 spin_unlock_irqrestore(&pm_qos_lock, flags);
414
415 return simple_read_from_buffer(buf, count, f_pos, &value, sizeof(s32));
416}
417
Mark Grossd82b3512008-02-04 22:30:08 -0800418static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
419 size_t count, loff_t *f_pos)
420{
421 s32 value;
Jean Pihetcc749982011-08-25 15:35:12 +0200422 struct pm_qos_request *req;
Mark Grossd82b3512008-02-04 22:30:08 -0800423
Mark Grossed771342010-05-06 01:59:26 +0200424 if (count == sizeof(s32)) {
425 if (copy_from_user(&value, buf, sizeof(s32)))
426 return -EFAULT;
Rafael J. Wysocki0775a602011-05-27 00:05:23 +0200427 } else if (count <= 11) { /* ASCII perhaps? */
428 char ascii_value[11];
429 unsigned long int ulval;
430 int ret;
431
432 if (copy_from_user(ascii_value, buf, count))
Mark Grossed771342010-05-06 01:59:26 +0200433 return -EFAULT;
Rafael J. Wysocki0775a602011-05-27 00:05:23 +0200434
435 if (count > 10) {
436 if (ascii_value[10] == '\n')
437 ascii_value[10] = '\0';
438 else
439 return -EINVAL;
440 } else {
441 ascii_value[count] = '\0';
442 }
443 ret = strict_strtoul(ascii_value, 16, &ulval);
444 if (ret) {
445 pr_debug("%s, 0x%lx, 0x%x\n", ascii_value, ulval, ret);
mark gross0109c2c2010-09-09 23:20:09 +0200446 return -EINVAL;
Rafael J. Wysocki0775a602011-05-27 00:05:23 +0200447 }
448 value = (s32)lower_32_bits(ulval);
449 } else {
Mark Grossd82b3512008-02-04 22:30:08 -0800450 return -EINVAL;
Rafael J. Wysocki0775a602011-05-27 00:05:23 +0200451 }
Mark Grossd82b3512008-02-04 22:30:08 -0800452
Jean Pihetcc749982011-08-25 15:35:12 +0200453 req = filp->private_data;
454 pm_qos_update_request(req, value);
Mark Grossed771342010-05-06 01:59:26 +0200455
456 return count;
Mark Grossd82b3512008-02-04 22:30:08 -0800457}
458
459
460static int __init pm_qos_power_init(void)
461{
462 int ret = 0;
463
464 ret = register_pm_qos_misc(&cpu_dma_pm_qos);
465 if (ret < 0) {
466 printk(KERN_ERR "pm_qos_param: cpu_dma_latency setup failed\n");
467 return ret;
468 }
469 ret = register_pm_qos_misc(&network_lat_pm_qos);
470 if (ret < 0) {
471 printk(KERN_ERR "pm_qos_param: network_latency setup failed\n");
472 return ret;
473 }
474 ret = register_pm_qos_misc(&network_throughput_pm_qos);
475 if (ret < 0)
476 printk(KERN_ERR
477 "pm_qos_param: network_throughput setup failed\n");
478
479 return ret;
480}
481
482late_initcall(pm_qos_power_init);