pm_qos_params.c 12 KB

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