qos.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * Devices PM QoS constraints management
  3. *
  4. * Copyright (C) 2011 Texas Instruments, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. *
  11. * This module exposes the interface to kernel space for specifying
  12. * per-device PM QoS dependencies. It provides infrastructure for registration
  13. * of:
  14. *
  15. * Dependents on a QoS value : register requests
  16. * Watchers of QoS value : get notified when target QoS value changes
  17. *
  18. * This QoS design is best effort based. Dependents register their QoS needs.
  19. * Watchers register to keep track of the current QoS needs of the system.
  20. * Watchers can register different types of notification callbacks:
  21. * . a per-device notification callback using the dev_pm_qos_*_notifier API.
  22. * The notification chain data is stored in the per-device constraint
  23. * data struct.
  24. * . a system-wide notification callback using the dev_pm_qos_*_global_notifier
  25. * API. The notification chain data is stored in a static variable.
  26. *
  27. * Note about the per-device constraint data struct allocation:
  28. * . The per-device constraints data struct ptr is tored into the device
  29. * dev_pm_info.
  30. * . To minimize the data usage by the per-device constraints, the data struct
  31. * is only allocated at the first call to dev_pm_qos_add_request.
  32. * . The data is later free'd when the device is removed from the system.
  33. * . The constraints_state variable from dev_pm_info tracks the data struct
  34. * allocation state:
  35. * DEV_PM_QOS_NO_DEVICE: No device present or device removed, no data
  36. * allocated,
  37. * DEV_PM_QOS_DEVICE_PRESENT: Device present, data not allocated and will be
  38. * allocated at the first call to dev_pm_qos_add_request,
  39. * DEV_PM_QOS_ALLOCATED: Device present, data allocated. The per-device
  40. * PM QoS constraints framework is operational and constraints can be
  41. * added, updated or removed using the dev_pm_qos_* API.
  42. * . A global mutex protects the constraints users from the data being
  43. * allocated and free'd.
  44. */
  45. #include <linux/pm_qos.h>
  46. #include <linux/spinlock.h>
  47. #include <linux/slab.h>
  48. #include <linux/device.h>
  49. #include <linux/mutex.h>
  50. static DEFINE_MUTEX(dev_pm_qos_mtx);
  51. static BLOCKING_NOTIFIER_HEAD(dev_pm_notifiers);
  52. /*
  53. * apply_constraint
  54. * @req: constraint request to apply
  55. * @action: action to perform add/update/remove, of type enum pm_qos_req_action
  56. * @value: defines the qos request
  57. *
  58. * Internal function to update the constraints list using the PM QoS core
  59. * code and if needed call the per-device and the global notification
  60. * callbacks
  61. */
  62. static int apply_constraint(struct dev_pm_qos_request *req,
  63. enum pm_qos_req_action action, int value)
  64. {
  65. int ret, curr_value;
  66. ret = pm_qos_update_target(req->dev->power.constraints,
  67. &req->node, action, value);
  68. if (ret) {
  69. /* Call the global callbacks if needed */
  70. curr_value = pm_qos_read_value(req->dev->power.constraints);
  71. blocking_notifier_call_chain(&dev_pm_notifiers,
  72. (unsigned long)curr_value,
  73. req);
  74. }
  75. return ret;
  76. }
  77. /*
  78. * dev_pm_qos_constraints_allocate
  79. * @dev: device to allocate data for
  80. *
  81. * Called at the first call to add_request, for constraint data allocation
  82. * Must be called with the dev_pm_qos_mtx mutex held
  83. */
  84. static int dev_pm_qos_constraints_allocate(struct device *dev)
  85. {
  86. struct pm_qos_constraints *c;
  87. struct blocking_notifier_head *n;
  88. c = kzalloc(sizeof(*c), GFP_KERNEL);
  89. if (!c)
  90. return -ENOMEM;
  91. n = kzalloc(sizeof(*n), GFP_KERNEL);
  92. if (!n) {
  93. kfree(c);
  94. return -ENOMEM;
  95. }
  96. BLOCKING_INIT_NOTIFIER_HEAD(n);
  97. dev->power.constraints = c;
  98. plist_head_init(&dev->power.constraints->list);
  99. dev->power.constraints->target_value = PM_QOS_DEV_LAT_DEFAULT_VALUE;
  100. dev->power.constraints->default_value = PM_QOS_DEV_LAT_DEFAULT_VALUE;
  101. dev->power.constraints->type = PM_QOS_MIN;
  102. dev->power.constraints->notifiers = n;
  103. dev->power.constraints_state = DEV_PM_QOS_ALLOCATED;
  104. return 0;
  105. }
  106. /**
  107. * dev_pm_qos_constraints_init
  108. * @dev: target device
  109. *
  110. * Called from the device PM subsystem at device insertion
  111. */
  112. void dev_pm_qos_constraints_init(struct device *dev)
  113. {
  114. mutex_lock(&dev_pm_qos_mtx);
  115. dev->power.constraints_state = DEV_PM_QOS_DEVICE_PRESENT;
  116. mutex_unlock(&dev_pm_qos_mtx);
  117. }
  118. /**
  119. * dev_pm_qos_constraints_destroy
  120. * @dev: target device
  121. *
  122. * Called from the device PM subsystem at device removal
  123. */
  124. void dev_pm_qos_constraints_destroy(struct device *dev)
  125. {
  126. struct dev_pm_qos_request *req, *tmp;
  127. mutex_lock(&dev_pm_qos_mtx);
  128. if (dev->power.constraints_state == DEV_PM_QOS_ALLOCATED) {
  129. /* Flush the constraints list for the device */
  130. plist_for_each_entry_safe(req, tmp,
  131. &dev->power.constraints->list,
  132. node) {
  133. /*
  134. * Update constraints list and call the notification
  135. * callbacks if needed
  136. */
  137. apply_constraint(req, PM_QOS_REMOVE_REQ,
  138. PM_QOS_DEFAULT_VALUE);
  139. memset(req, 0, sizeof(*req));
  140. }
  141. kfree(dev->power.constraints->notifiers);
  142. kfree(dev->power.constraints);
  143. dev->power.constraints = NULL;
  144. }
  145. dev->power.constraints_state = DEV_PM_QOS_NO_DEVICE;
  146. mutex_unlock(&dev_pm_qos_mtx);
  147. }
  148. /**
  149. * dev_pm_qos_add_request - inserts new qos request into the list
  150. * @dev: target device for the constraint
  151. * @req: pointer to a preallocated handle
  152. * @value: defines the qos request
  153. *
  154. * This function inserts a new entry in the device constraints list of
  155. * requested qos performance characteristics. It recomputes the aggregate
  156. * QoS expectations of parameters and initializes the dev_pm_qos_request
  157. * handle. Caller needs to save this handle for later use in updates and
  158. * removal.
  159. *
  160. * Returns 1 if the aggregated constraint value has changed,
  161. * 0 if the aggregated constraint value has not changed,
  162. * -EINVAL in case of wrong parameters, -ENODEV if the device has been
  163. * removed from the system
  164. */
  165. int dev_pm_qos_add_request(struct device *dev, struct dev_pm_qos_request *req,
  166. s32 value)
  167. {
  168. int ret = 0;
  169. if (!dev || !req) /*guard against callers passing in null */
  170. return -EINVAL;
  171. if (dev_pm_qos_request_active(req)) {
  172. WARN(1, KERN_ERR "dev_pm_qos_add_request() called for already "
  173. "added request\n");
  174. return -EINVAL;
  175. }
  176. mutex_lock(&dev_pm_qos_mtx);
  177. req->dev = dev;
  178. /* Return if the device has been removed */
  179. if (req->dev->power.constraints_state == DEV_PM_QOS_NO_DEVICE) {
  180. ret = -ENODEV;
  181. goto out;
  182. }
  183. /*
  184. * Allocate the constraints data on the first call to add_request,
  185. * i.e. only if the data is not already allocated and if the device has
  186. * not been removed
  187. */
  188. if (dev->power.constraints_state == DEV_PM_QOS_DEVICE_PRESENT)
  189. ret = dev_pm_qos_constraints_allocate(dev);
  190. if (!ret)
  191. ret = apply_constraint(req, PM_QOS_ADD_REQ, value);
  192. out:
  193. mutex_unlock(&dev_pm_qos_mtx);
  194. return ret;
  195. }
  196. EXPORT_SYMBOL_GPL(dev_pm_qos_add_request);
  197. /**
  198. * dev_pm_qos_update_request - modifies an existing qos request
  199. * @req : handle to list element holding a dev_pm_qos request to use
  200. * @new_value: defines the qos request
  201. *
  202. * Updates an existing dev PM qos request along with updating the
  203. * target value.
  204. *
  205. * Attempts are made to make this code callable on hot code paths.
  206. *
  207. * Returns 1 if the aggregated constraint value has changed,
  208. * 0 if the aggregated constraint value has not changed,
  209. * -EINVAL in case of wrong parameters, -ENODEV if the device has been
  210. * removed from the system
  211. */
  212. int dev_pm_qos_update_request(struct dev_pm_qos_request *req,
  213. s32 new_value)
  214. {
  215. int ret = 0;
  216. if (!req) /*guard against callers passing in null */
  217. return -EINVAL;
  218. if (!dev_pm_qos_request_active(req)) {
  219. WARN(1, KERN_ERR "dev_pm_qos_update_request() called for "
  220. "unknown object\n");
  221. return -EINVAL;
  222. }
  223. mutex_lock(&dev_pm_qos_mtx);
  224. if (req->dev->power.constraints_state == DEV_PM_QOS_ALLOCATED) {
  225. if (new_value != req->node.prio)
  226. ret = apply_constraint(req, PM_QOS_UPDATE_REQ,
  227. new_value);
  228. } else {
  229. /* Return if the device has been removed */
  230. ret = -ENODEV;
  231. }
  232. mutex_unlock(&dev_pm_qos_mtx);
  233. return ret;
  234. }
  235. EXPORT_SYMBOL_GPL(dev_pm_qos_update_request);
  236. /**
  237. * dev_pm_qos_remove_request - modifies an existing qos request
  238. * @req: handle to request list element
  239. *
  240. * Will remove pm qos request from the list of constraints and
  241. * recompute the current target value. Call this on slow code paths.
  242. *
  243. * Returns 1 if the aggregated constraint value has changed,
  244. * 0 if the aggregated constraint value has not changed,
  245. * -EINVAL in case of wrong parameters, -ENODEV if the device has been
  246. * removed from the system
  247. */
  248. int dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
  249. {
  250. int ret = 0;
  251. if (!req) /*guard against callers passing in null */
  252. return -EINVAL;
  253. if (!dev_pm_qos_request_active(req)) {
  254. WARN(1, KERN_ERR "dev_pm_qos_remove_request() called for "
  255. "unknown object\n");
  256. return -EINVAL;
  257. }
  258. mutex_lock(&dev_pm_qos_mtx);
  259. if (req->dev->power.constraints_state == DEV_PM_QOS_ALLOCATED) {
  260. ret = apply_constraint(req, PM_QOS_REMOVE_REQ,
  261. PM_QOS_DEFAULT_VALUE);
  262. memset(req, 0, sizeof(*req));
  263. } else {
  264. /* Return if the device has been removed */
  265. ret = -ENODEV;
  266. }
  267. mutex_unlock(&dev_pm_qos_mtx);
  268. return ret;
  269. }
  270. EXPORT_SYMBOL_GPL(dev_pm_qos_remove_request);
  271. /**
  272. * dev_pm_qos_add_notifier - sets notification entry for changes to target value
  273. * of per-device PM QoS constraints
  274. *
  275. * @dev: target device for the constraint
  276. * @notifier: notifier block managed by caller.
  277. *
  278. * Will register the notifier into a notification chain that gets called
  279. * upon changes to the target value for the device.
  280. */
  281. int dev_pm_qos_add_notifier(struct device *dev, struct notifier_block *notifier)
  282. {
  283. int retval = 0;
  284. mutex_lock(&dev_pm_qos_mtx);
  285. /* Silently return if the device has been removed */
  286. if (dev->power.constraints_state != DEV_PM_QOS_ALLOCATED)
  287. goto out;
  288. retval = blocking_notifier_chain_register(
  289. dev->power.constraints->notifiers,
  290. notifier);
  291. out:
  292. mutex_unlock(&dev_pm_qos_mtx);
  293. return retval;
  294. }
  295. EXPORT_SYMBOL_GPL(dev_pm_qos_add_notifier);
  296. /**
  297. * dev_pm_qos_remove_notifier - deletes notification for changes to target value
  298. * of per-device PM QoS constraints
  299. *
  300. * @dev: target device for the constraint
  301. * @notifier: notifier block to be removed.
  302. *
  303. * Will remove the notifier from the notification chain that gets called
  304. * upon changes to the target value.
  305. */
  306. int dev_pm_qos_remove_notifier(struct device *dev,
  307. struct notifier_block *notifier)
  308. {
  309. int retval = 0;
  310. mutex_lock(&dev_pm_qos_mtx);
  311. /* Silently return if the device has been removed */
  312. if (dev->power.constraints_state != DEV_PM_QOS_ALLOCATED)
  313. goto out;
  314. retval = blocking_notifier_chain_unregister(
  315. dev->power.constraints->notifiers,
  316. notifier);
  317. out:
  318. mutex_unlock(&dev_pm_qos_mtx);
  319. return retval;
  320. }
  321. EXPORT_SYMBOL_GPL(dev_pm_qos_remove_notifier);
  322. /**
  323. * dev_pm_qos_add_global_notifier - sets notification entry for changes to
  324. * target value of the PM QoS constraints for any device
  325. *
  326. * @notifier: notifier block managed by caller.
  327. *
  328. * Will register the notifier into a notification chain that gets called
  329. * upon changes to the target value for any device.
  330. */
  331. int dev_pm_qos_add_global_notifier(struct notifier_block *notifier)
  332. {
  333. return blocking_notifier_chain_register(&dev_pm_notifiers, notifier);
  334. }
  335. EXPORT_SYMBOL_GPL(dev_pm_qos_add_global_notifier);
  336. /**
  337. * dev_pm_qos_remove_global_notifier - deletes notification for changes to
  338. * target value of PM QoS constraints for any device
  339. *
  340. * @notifier: notifier block to be removed.
  341. *
  342. * Will remove the notifier from the notification chain that gets called
  343. * upon changes to the target value for any device.
  344. */
  345. int dev_pm_qos_remove_global_notifier(struct notifier_block *notifier)
  346. {
  347. return blocking_notifier_chain_unregister(&dev_pm_notifiers, notifier);
  348. }
  349. EXPORT_SYMBOL_GPL(dev_pm_qos_remove_global_notifier);