qos.c 15 KB

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