qos.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. * . A global mutex protects the constraints users from the data being
  34. * allocated and free'd.
  35. */
  36. #include <linux/pm_qos.h>
  37. #include <linux/spinlock.h>
  38. #include <linux/slab.h>
  39. #include <linux/device.h>
  40. #include <linux/mutex.h>
  41. #include <linux/export.h>
  42. static DEFINE_MUTEX(dev_pm_qos_mtx);
  43. static BLOCKING_NOTIFIER_HEAD(dev_pm_notifiers);
  44. /**
  45. * dev_pm_qos_read_value - Get PM QoS constraint for a given device.
  46. * @dev: Device to get the PM QoS constraint value for.
  47. */
  48. s32 dev_pm_qos_read_value(struct device *dev)
  49. {
  50. struct pm_qos_constraints *c;
  51. unsigned long flags;
  52. s32 ret = 0;
  53. spin_lock_irqsave(&dev->power.lock, flags);
  54. c = dev->power.constraints;
  55. if (c)
  56. ret = pm_qos_read_value(c);
  57. spin_unlock_irqrestore(&dev->power.lock, flags);
  58. return ret;
  59. }
  60. /*
  61. * apply_constraint
  62. * @req: constraint request to apply
  63. * @action: action to perform add/update/remove, of type enum pm_qos_req_action
  64. * @value: defines the qos request
  65. *
  66. * Internal function to update the constraints list using the PM QoS core
  67. * code and if needed call the per-device and the global notification
  68. * callbacks
  69. */
  70. static int apply_constraint(struct dev_pm_qos_request *req,
  71. enum pm_qos_req_action action, int value)
  72. {
  73. int ret, curr_value;
  74. ret = pm_qos_update_target(req->dev->power.constraints,
  75. &req->node, action, value);
  76. if (ret) {
  77. /* Call the global callbacks if needed */
  78. curr_value = pm_qos_read_value(req->dev->power.constraints);
  79. blocking_notifier_call_chain(&dev_pm_notifiers,
  80. (unsigned long)curr_value,
  81. req);
  82. }
  83. return ret;
  84. }
  85. /*
  86. * dev_pm_qos_constraints_allocate
  87. * @dev: device to allocate data for
  88. *
  89. * Called at the first call to add_request, for constraint data allocation
  90. * Must be called with the dev_pm_qos_mtx mutex held
  91. */
  92. static int dev_pm_qos_constraints_allocate(struct device *dev)
  93. {
  94. struct pm_qos_constraints *c;
  95. struct blocking_notifier_head *n;
  96. c = kzalloc(sizeof(*c), GFP_KERNEL);
  97. if (!c)
  98. return -ENOMEM;
  99. n = kzalloc(sizeof(*n), GFP_KERNEL);
  100. if (!n) {
  101. kfree(c);
  102. return -ENOMEM;
  103. }
  104. BLOCKING_INIT_NOTIFIER_HEAD(n);
  105. plist_head_init(&c->list);
  106. c->target_value = PM_QOS_DEV_LAT_DEFAULT_VALUE;
  107. c->default_value = PM_QOS_DEV_LAT_DEFAULT_VALUE;
  108. c->type = PM_QOS_MIN;
  109. c->notifiers = n;
  110. spin_lock_irq(&dev->power.lock);
  111. dev->power.constraints = c;
  112. spin_unlock_irq(&dev->power.lock);
  113. return 0;
  114. }
  115. /**
  116. * dev_pm_qos_constraints_init - Initalize device's PM QoS constraints pointer.
  117. * @dev: target device
  118. *
  119. * Called from the device PM subsystem during device insertion under
  120. * device_pm_lock().
  121. */
  122. void dev_pm_qos_constraints_init(struct device *dev)
  123. {
  124. mutex_lock(&dev_pm_qos_mtx);
  125. dev->power.constraints = NULL;
  126. dev->power.power_state = PMSG_ON;
  127. mutex_unlock(&dev_pm_qos_mtx);
  128. }
  129. /**
  130. * dev_pm_qos_constraints_destroy
  131. * @dev: target device
  132. *
  133. * Called from the device PM subsystem on device removal under device_pm_lock().
  134. */
  135. void dev_pm_qos_constraints_destroy(struct device *dev)
  136. {
  137. struct dev_pm_qos_request *req, *tmp;
  138. struct pm_qos_constraints *c;
  139. mutex_lock(&dev_pm_qos_mtx);
  140. dev->power.power_state = PMSG_INVALID;
  141. c = dev->power.constraints;
  142. if (!c)
  143. goto out;
  144. /* Flush the constraints list for the device */
  145. plist_for_each_entry_safe(req, tmp, &c->list, node) {
  146. /*
  147. * Update constraints list and call the notification
  148. * callbacks if needed
  149. */
  150. apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
  151. memset(req, 0, sizeof(*req));
  152. }
  153. spin_lock_irq(&dev->power.lock);
  154. dev->power.constraints = NULL;
  155. spin_unlock_irq(&dev->power.lock);
  156. kfree(c->notifiers);
  157. kfree(c);
  158. out:
  159. mutex_unlock(&dev_pm_qos_mtx);
  160. }
  161. /**
  162. * dev_pm_qos_add_request - inserts new qos request into the list
  163. * @dev: target device for the constraint
  164. * @req: pointer to a preallocated handle
  165. * @value: defines the qos request
  166. *
  167. * This function inserts a new entry in the device constraints list of
  168. * requested qos performance characteristics. It recomputes the aggregate
  169. * QoS expectations of parameters and initializes the dev_pm_qos_request
  170. * handle. Caller needs to save this handle for later use in updates and
  171. * removal.
  172. *
  173. * Returns 1 if the aggregated constraint value has changed,
  174. * 0 if the aggregated constraint value has not changed,
  175. * -EINVAL in case of wrong parameters, -ENOMEM if there's not enough memory
  176. * to allocate for data structures, -ENODEV if the device has just been removed
  177. * from the system.
  178. */
  179. int dev_pm_qos_add_request(struct device *dev, struct dev_pm_qos_request *req,
  180. s32 value)
  181. {
  182. int ret = 0;
  183. if (!dev || !req) /*guard against callers passing in null */
  184. return -EINVAL;
  185. if (dev_pm_qos_request_active(req)) {
  186. WARN(1, KERN_ERR "dev_pm_qos_add_request() called for already "
  187. "added request\n");
  188. return -EINVAL;
  189. }
  190. req->dev = dev;
  191. mutex_lock(&dev_pm_qos_mtx);
  192. if (!dev->power.constraints) {
  193. if (dev->power.power_state.event == PM_EVENT_INVALID) {
  194. /* The device has been removed from the system. */
  195. req->dev = NULL;
  196. ret = -ENODEV;
  197. goto out;
  198. } else {
  199. /*
  200. * Allocate the constraints data on the first call to
  201. * add_request, i.e. only if the data is not already
  202. * allocated and if the device has not been removed.
  203. */
  204. ret = dev_pm_qos_constraints_allocate(dev);
  205. }
  206. }
  207. if (!ret)
  208. ret = apply_constraint(req, PM_QOS_ADD_REQ, value);
  209. out:
  210. mutex_unlock(&dev_pm_qos_mtx);
  211. return ret;
  212. }
  213. EXPORT_SYMBOL_GPL(dev_pm_qos_add_request);
  214. /**
  215. * dev_pm_qos_update_request - modifies an existing qos request
  216. * @req : handle to list element holding a dev_pm_qos request to use
  217. * @new_value: defines the qos request
  218. *
  219. * Updates an existing dev PM qos request along with updating the
  220. * target value.
  221. *
  222. * Attempts are made to make this code callable on hot code paths.
  223. *
  224. * Returns 1 if the aggregated constraint value has changed,
  225. * 0 if the aggregated constraint value has not changed,
  226. * -EINVAL in case of wrong parameters, -ENODEV if the device has been
  227. * removed from the system
  228. */
  229. int dev_pm_qos_update_request(struct dev_pm_qos_request *req,
  230. s32 new_value)
  231. {
  232. int ret = 0;
  233. if (!req) /*guard against callers passing in null */
  234. return -EINVAL;
  235. if (!dev_pm_qos_request_active(req)) {
  236. WARN(1, KERN_ERR "dev_pm_qos_update_request() called for "
  237. "unknown object\n");
  238. return -EINVAL;
  239. }
  240. mutex_lock(&dev_pm_qos_mtx);
  241. if (req->dev->power.constraints) {
  242. if (new_value != req->node.prio)
  243. ret = apply_constraint(req, PM_QOS_UPDATE_REQ,
  244. new_value);
  245. } else {
  246. /* Return if the device has been removed */
  247. ret = -ENODEV;
  248. }
  249. mutex_unlock(&dev_pm_qos_mtx);
  250. return ret;
  251. }
  252. EXPORT_SYMBOL_GPL(dev_pm_qos_update_request);
  253. /**
  254. * dev_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. Call this on slow code paths.
  259. *
  260. * Returns 1 if the aggregated constraint value has changed,
  261. * 0 if the aggregated constraint value has not changed,
  262. * -EINVAL in case of wrong parameters, -ENODEV if the device has been
  263. * removed from the system
  264. */
  265. int dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
  266. {
  267. int ret = 0;
  268. if (!req) /*guard against callers passing in null */
  269. return -EINVAL;
  270. if (!dev_pm_qos_request_active(req)) {
  271. WARN(1, KERN_ERR "dev_pm_qos_remove_request() called for "
  272. "unknown object\n");
  273. return -EINVAL;
  274. }
  275. mutex_lock(&dev_pm_qos_mtx);
  276. if (req->dev->power.constraints) {
  277. ret = apply_constraint(req, PM_QOS_REMOVE_REQ,
  278. PM_QOS_DEFAULT_VALUE);
  279. memset(req, 0, sizeof(*req));
  280. } else {
  281. /* Return if the device has been removed */
  282. ret = -ENODEV;
  283. }
  284. mutex_unlock(&dev_pm_qos_mtx);
  285. return ret;
  286. }
  287. EXPORT_SYMBOL_GPL(dev_pm_qos_remove_request);
  288. /**
  289. * dev_pm_qos_add_notifier - sets notification entry for changes to target value
  290. * of per-device PM QoS constraints
  291. *
  292. * @dev: target device for the constraint
  293. * @notifier: notifier block managed by caller.
  294. *
  295. * Will register the notifier into a notification chain that gets called
  296. * upon changes to the target value for the device.
  297. */
  298. int dev_pm_qos_add_notifier(struct device *dev, struct notifier_block *notifier)
  299. {
  300. int retval = 0;
  301. mutex_lock(&dev_pm_qos_mtx);
  302. /* Silently return if the constraints object is not present. */
  303. if (dev->power.constraints)
  304. retval = blocking_notifier_chain_register(
  305. dev->power.constraints->notifiers,
  306. notifier);
  307. mutex_unlock(&dev_pm_qos_mtx);
  308. return retval;
  309. }
  310. EXPORT_SYMBOL_GPL(dev_pm_qos_add_notifier);
  311. /**
  312. * dev_pm_qos_remove_notifier - deletes notification for changes to target value
  313. * of per-device PM QoS constraints
  314. *
  315. * @dev: target device for the constraint
  316. * @notifier: notifier block to be removed.
  317. *
  318. * Will remove the notifier from the notification chain that gets called
  319. * upon changes to the target value.
  320. */
  321. int dev_pm_qos_remove_notifier(struct device *dev,
  322. struct notifier_block *notifier)
  323. {
  324. int retval = 0;
  325. mutex_lock(&dev_pm_qos_mtx);
  326. /* Silently return if the constraints object is not present. */
  327. if (dev->power.constraints)
  328. retval = blocking_notifier_chain_unregister(
  329. dev->power.constraints->notifiers,
  330. notifier);
  331. mutex_unlock(&dev_pm_qos_mtx);
  332. return retval;
  333. }
  334. EXPORT_SYMBOL_GPL(dev_pm_qos_remove_notifier);
  335. /**
  336. * dev_pm_qos_add_global_notifier - sets notification entry for changes to
  337. * target value of the PM QoS constraints for any device
  338. *
  339. * @notifier: notifier block managed by caller.
  340. *
  341. * Will register the notifier into a notification chain that gets called
  342. * upon changes to the target value for any device.
  343. */
  344. int dev_pm_qos_add_global_notifier(struct notifier_block *notifier)
  345. {
  346. return blocking_notifier_chain_register(&dev_pm_notifiers, notifier);
  347. }
  348. EXPORT_SYMBOL_GPL(dev_pm_qos_add_global_notifier);
  349. /**
  350. * dev_pm_qos_remove_global_notifier - deletes notification for changes to
  351. * target value of PM QoS constraints for any device
  352. *
  353. * @notifier: notifier block to be removed.
  354. *
  355. * Will remove the notifier from the notification chain that gets called
  356. * upon changes to the target value for any device.
  357. */
  358. int dev_pm_qos_remove_global_notifier(struct notifier_block *notifier)
  359. {
  360. return blocking_notifier_chain_unregister(&dev_pm_notifiers, notifier);
  361. }
  362. EXPORT_SYMBOL_GPL(dev_pm_qos_remove_global_notifier);