qos.c 14 KB

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