qos.c 13 KB

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