qos.c 12 KB

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