pm_qos_params.c 13 KB

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