pm_qos_params.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. /*#define DEBUG*/
  30. #include <linux/pm_qos_params.h>
  31. #include <linux/sched.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/slab.h>
  34. #include <linux/time.h>
  35. #include <linux/fs.h>
  36. #include <linux/device.h>
  37. #include <linux/miscdevice.h>
  38. #include <linux/string.h>
  39. #include <linux/platform_device.h>
  40. #include <linux/init.h>
  41. #include <linux/uaccess.h>
  42. /*
  43. * locking rule: all changes to requests or notifiers lists
  44. * or pm_qos_object list and pm_qos_objects need to happen with pm_qos_lock
  45. * held, taken with _irqsave. One lock to rule them all
  46. */
  47. enum pm_qos_type {
  48. PM_QOS_MAX, /* return the largest value */
  49. PM_QOS_MIN /* return the smallest value */
  50. };
  51. struct pm_qos_object {
  52. struct plist_head requests;
  53. struct blocking_notifier_head *notifiers;
  54. struct miscdevice pm_qos_power_miscdev;
  55. char *name;
  56. s32 default_value;
  57. enum pm_qos_type type;
  58. };
  59. static DEFINE_SPINLOCK(pm_qos_lock);
  60. static struct pm_qos_object null_pm_qos;
  61. static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier);
  62. static struct pm_qos_object cpu_dma_pm_qos = {
  63. .requests = PLIST_HEAD_INIT(cpu_dma_pm_qos.requests, pm_qos_lock),
  64. .notifiers = &cpu_dma_lat_notifier,
  65. .name = "cpu_dma_latency",
  66. .default_value = 2000 * USEC_PER_SEC,
  67. .type = PM_QOS_MIN,
  68. };
  69. static BLOCKING_NOTIFIER_HEAD(network_lat_notifier);
  70. static struct pm_qos_object network_lat_pm_qos = {
  71. .requests = PLIST_HEAD_INIT(network_lat_pm_qos.requests, pm_qos_lock),
  72. .notifiers = &network_lat_notifier,
  73. .name = "network_latency",
  74. .default_value = 2000 * USEC_PER_SEC,
  75. .type = PM_QOS_MIN
  76. };
  77. static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier);
  78. static struct pm_qos_object network_throughput_pm_qos = {
  79. .requests = PLIST_HEAD_INIT(network_throughput_pm_qos.requests, pm_qos_lock),
  80. .notifiers = &network_throughput_notifier,
  81. .name = "network_throughput",
  82. .default_value = 0,
  83. .type = PM_QOS_MAX,
  84. };
  85. static struct pm_qos_object *pm_qos_array[] = {
  86. &null_pm_qos,
  87. &cpu_dma_pm_qos,
  88. &network_lat_pm_qos,
  89. &network_throughput_pm_qos
  90. };
  91. static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
  92. size_t count, loff_t *f_pos);
  93. static ssize_t pm_qos_power_read(struct file *filp, char __user *buf,
  94. size_t count, loff_t *f_pos);
  95. static int pm_qos_power_open(struct inode *inode, struct file *filp);
  96. static int pm_qos_power_release(struct inode *inode, struct file *filp);
  97. static const struct file_operations pm_qos_power_fops = {
  98. .write = pm_qos_power_write,
  99. .read = pm_qos_power_read,
  100. .open = pm_qos_power_open,
  101. .release = pm_qos_power_release,
  102. .llseek = noop_llseek,
  103. };
  104. /* unlocked internal variant */
  105. static inline int pm_qos_get_value(struct pm_qos_object *o)
  106. {
  107. if (plist_head_empty(&o->requests))
  108. return o->default_value;
  109. switch (o->type) {
  110. case PM_QOS_MIN:
  111. return plist_first(&o->requests)->prio;
  112. case PM_QOS_MAX:
  113. return plist_last(&o->requests)->prio;
  114. default:
  115. /* runtime check for not using enum */
  116. BUG();
  117. }
  118. }
  119. static void update_target(struct pm_qos_object *o, struct plist_node *node,
  120. int del, int value)
  121. {
  122. unsigned long flags;
  123. int prev_value, curr_value;
  124. spin_lock_irqsave(&pm_qos_lock, flags);
  125. prev_value = pm_qos_get_value(o);
  126. /* PM_QOS_DEFAULT_VALUE is a signal that the value is unchanged */
  127. if (value != PM_QOS_DEFAULT_VALUE) {
  128. /*
  129. * to change the list, we atomically remove, reinit
  130. * with new value and add, then see if the extremal
  131. * changed
  132. */
  133. plist_del(node, &o->requests);
  134. plist_node_init(node, value);
  135. plist_add(node, &o->requests);
  136. } else if (del) {
  137. plist_del(node, &o->requests);
  138. } else {
  139. plist_add(node, &o->requests);
  140. }
  141. curr_value = pm_qos_get_value(o);
  142. spin_unlock_irqrestore(&pm_qos_lock, flags);
  143. if (prev_value != curr_value)
  144. blocking_notifier_call_chain(o->notifiers,
  145. (unsigned long)curr_value,
  146. NULL);
  147. }
  148. static int register_pm_qos_misc(struct pm_qos_object *qos)
  149. {
  150. qos->pm_qos_power_miscdev.minor = MISC_DYNAMIC_MINOR;
  151. qos->pm_qos_power_miscdev.name = qos->name;
  152. qos->pm_qos_power_miscdev.fops = &pm_qos_power_fops;
  153. return misc_register(&qos->pm_qos_power_miscdev);
  154. }
  155. static int find_pm_qos_object_by_minor(int minor)
  156. {
  157. int pm_qos_class;
  158. for (pm_qos_class = 0;
  159. pm_qos_class < PM_QOS_NUM_CLASSES; pm_qos_class++) {
  160. if (minor ==
  161. pm_qos_array[pm_qos_class]->pm_qos_power_miscdev.minor)
  162. return pm_qos_class;
  163. }
  164. return -1;
  165. }
  166. /**
  167. * pm_qos_request - returns current system wide qos expectation
  168. * @pm_qos_class: identification of which qos value is requested
  169. *
  170. * This function returns the current target value in an atomic manner.
  171. */
  172. int pm_qos_request(int pm_qos_class)
  173. {
  174. unsigned long flags;
  175. int value;
  176. spin_lock_irqsave(&pm_qos_lock, flags);
  177. value = pm_qos_get_value(pm_qos_array[pm_qos_class]);
  178. spin_unlock_irqrestore(&pm_qos_lock, flags);
  179. return value;
  180. }
  181. EXPORT_SYMBOL_GPL(pm_qos_request);
  182. int pm_qos_request_active(struct pm_qos_request_list *req)
  183. {
  184. return req->pm_qos_class != 0;
  185. }
  186. EXPORT_SYMBOL_GPL(pm_qos_request_active);
  187. /**
  188. * pm_qos_add_request - inserts new qos request into the list
  189. * @dep: pointer to a preallocated handle
  190. * @pm_qos_class: identifies which list of qos request to use
  191. * @value: defines the qos request
  192. *
  193. * This function inserts a new entry in the pm_qos_class list of requested qos
  194. * performance characteristics. It recomputes the aggregate QoS expectations
  195. * for the pm_qos_class of parameters and initializes the pm_qos_request_list
  196. * handle. Caller needs to save this handle for later use in updates and
  197. * removal.
  198. */
  199. void pm_qos_add_request(struct pm_qos_request_list *dep,
  200. int pm_qos_class, s32 value)
  201. {
  202. struct pm_qos_object *o = pm_qos_array[pm_qos_class];
  203. int new_value;
  204. if (pm_qos_request_active(dep)) {
  205. WARN(1, KERN_ERR "pm_qos_add_request() called for already added request\n");
  206. return;
  207. }
  208. if (value == PM_QOS_DEFAULT_VALUE)
  209. new_value = o->default_value;
  210. else
  211. new_value = value;
  212. plist_node_init(&dep->list, new_value);
  213. dep->pm_qos_class = pm_qos_class;
  214. update_target(o, &dep->list, 0, PM_QOS_DEFAULT_VALUE);
  215. }
  216. EXPORT_SYMBOL_GPL(pm_qos_add_request);
  217. /**
  218. * pm_qos_update_request - modifies an existing qos request
  219. * @pm_qos_req : handle to list element holding a pm_qos request to use
  220. * @value: defines the qos request
  221. *
  222. * Updates an existing qos request for the pm_qos_class of parameters along
  223. * with updating the target pm_qos_class value.
  224. *
  225. * Attempts are made to make this code callable on hot code paths.
  226. */
  227. void pm_qos_update_request(struct pm_qos_request_list *pm_qos_req,
  228. s32 new_value)
  229. {
  230. s32 temp;
  231. struct pm_qos_object *o;
  232. if (!pm_qos_req) /*guard against callers passing in null */
  233. return;
  234. if (!pm_qos_request_active(pm_qos_req)) {
  235. WARN(1, KERN_ERR "pm_qos_update_request() called for unknown object\n");
  236. return;
  237. }
  238. o = pm_qos_array[pm_qos_req->pm_qos_class];
  239. if (new_value == PM_QOS_DEFAULT_VALUE)
  240. temp = o->default_value;
  241. else
  242. temp = new_value;
  243. if (temp != pm_qos_req->list.prio)
  244. update_target(o, &pm_qos_req->list, 0, temp);
  245. }
  246. EXPORT_SYMBOL_GPL(pm_qos_update_request);
  247. /**
  248. * pm_qos_remove_request - modifies an existing qos request
  249. * @pm_qos_req: handle to request list element
  250. *
  251. * Will remove pm qos request from the list of requests and
  252. * recompute the current target value for the pm_qos_class. Call this
  253. * on slow code paths.
  254. */
  255. void pm_qos_remove_request(struct pm_qos_request_list *pm_qos_req)
  256. {
  257. struct pm_qos_object *o;
  258. if (pm_qos_req == NULL)
  259. return;
  260. /* silent return to keep pcm code cleaner */
  261. if (!pm_qos_request_active(pm_qos_req)) {
  262. WARN(1, KERN_ERR "pm_qos_remove_request() called for unknown object\n");
  263. return;
  264. }
  265. o = pm_qos_array[pm_qos_req->pm_qos_class];
  266. update_target(o, &pm_qos_req->list, 1, PM_QOS_DEFAULT_VALUE);
  267. memset(pm_qos_req, 0, sizeof(*pm_qos_req));
  268. }
  269. EXPORT_SYMBOL_GPL(pm_qos_remove_request);
  270. /**
  271. * pm_qos_add_notifier - sets notification entry for changes to target value
  272. * @pm_qos_class: identifies which qos target changes should be notified.
  273. * @notifier: notifier block managed by caller.
  274. *
  275. * will register the notifier into a notification chain that gets called
  276. * upon changes to the pm_qos_class target value.
  277. */
  278. int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier)
  279. {
  280. int retval;
  281. retval = blocking_notifier_chain_register(
  282. pm_qos_array[pm_qos_class]->notifiers, notifier);
  283. return retval;
  284. }
  285. EXPORT_SYMBOL_GPL(pm_qos_add_notifier);
  286. /**
  287. * pm_qos_remove_notifier - deletes notification entry from chain.
  288. * @pm_qos_class: identifies which qos target changes are notified.
  289. * @notifier: notifier block to be removed.
  290. *
  291. * will remove the notifier from the notification chain that gets called
  292. * upon changes to the pm_qos_class target value.
  293. */
  294. int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier)
  295. {
  296. int retval;
  297. retval = blocking_notifier_chain_unregister(
  298. pm_qos_array[pm_qos_class]->notifiers, notifier);
  299. return retval;
  300. }
  301. EXPORT_SYMBOL_GPL(pm_qos_remove_notifier);
  302. static int pm_qos_power_open(struct inode *inode, struct file *filp)
  303. {
  304. long pm_qos_class;
  305. pm_qos_class = find_pm_qos_object_by_minor(iminor(inode));
  306. if (pm_qos_class >= 0) {
  307. struct pm_qos_request_list *req = kzalloc(sizeof(*req), GFP_KERNEL);
  308. if (!req)
  309. return -ENOMEM;
  310. pm_qos_add_request(req, pm_qos_class, PM_QOS_DEFAULT_VALUE);
  311. filp->private_data = req;
  312. if (filp->private_data)
  313. return 0;
  314. }
  315. return -EPERM;
  316. }
  317. static int pm_qos_power_release(struct inode *inode, struct file *filp)
  318. {
  319. struct pm_qos_request_list *req;
  320. req = filp->private_data;
  321. pm_qos_remove_request(req);
  322. kfree(req);
  323. return 0;
  324. }
  325. static ssize_t pm_qos_power_read(struct file *filp, char __user *buf,
  326. size_t count, loff_t *f_pos)
  327. {
  328. s32 value;
  329. unsigned long flags;
  330. struct pm_qos_object *o;
  331. struct pm_qos_request_list *pm_qos_req = filp->private_data;;
  332. if (!pm_qos_req)
  333. return -EINVAL;
  334. if (!pm_qos_request_active(pm_qos_req))
  335. return -EINVAL;
  336. o = pm_qos_array[pm_qos_req->pm_qos_class];
  337. spin_lock_irqsave(&pm_qos_lock, flags);
  338. value = pm_qos_get_value(o);
  339. spin_unlock_irqrestore(&pm_qos_lock, flags);
  340. return simple_read_from_buffer(buf, count, f_pos, &value, sizeof(s32));
  341. }
  342. static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
  343. size_t count, loff_t *f_pos)
  344. {
  345. s32 value;
  346. int x;
  347. char ascii_value[11];
  348. struct pm_qos_request_list *pm_qos_req;
  349. if (count == sizeof(s32)) {
  350. if (copy_from_user(&value, buf, sizeof(s32)))
  351. return -EFAULT;
  352. } else if (count == 11) { /* len('0x12345678/0') */
  353. if (copy_from_user(ascii_value, buf, 11))
  354. return -EFAULT;
  355. if (strlen(ascii_value) != 10)
  356. return -EINVAL;
  357. x = sscanf(ascii_value, "%x", &value);
  358. if (x != 1)
  359. return -EINVAL;
  360. pr_debug("%s, %d, 0x%x\n", ascii_value, x, value);
  361. } else
  362. return -EINVAL;
  363. pm_qos_req = filp->private_data;
  364. pm_qos_update_request(pm_qos_req, value);
  365. return count;
  366. }
  367. static int __init pm_qos_power_init(void)
  368. {
  369. int ret = 0;
  370. ret = register_pm_qos_misc(&cpu_dma_pm_qos);
  371. if (ret < 0) {
  372. printk(KERN_ERR "pm_qos_param: cpu_dma_latency setup failed\n");
  373. return ret;
  374. }
  375. ret = register_pm_qos_misc(&network_lat_pm_qos);
  376. if (ret < 0) {
  377. printk(KERN_ERR "pm_qos_param: network_latency setup failed\n");
  378. return ret;
  379. }
  380. ret = register_pm_qos_misc(&network_throughput_pm_qos);
  381. if (ret < 0)
  382. printk(KERN_ERR
  383. "pm_qos_param: network_throughput setup failed\n");
  384. return ret;
  385. }
  386. late_initcall(pm_qos_power_init);