qos.c 14 KB

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