qos.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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 <linux/pm_runtime.h>
  43. #include <linux/err.h>
  44. #include "power.h"
  45. static DEFINE_MUTEX(dev_pm_qos_mtx);
  46. static DEFINE_MUTEX(dev_pm_qos_sysfs_mtx);
  47. static BLOCKING_NOTIFIER_HEAD(dev_pm_notifiers);
  48. /**
  49. * __dev_pm_qos_flags - Check PM QoS flags for a given device.
  50. * @dev: Device to check the PM QoS flags for.
  51. * @mask: Flags to check against.
  52. *
  53. * This routine must be called with dev->power.lock held.
  54. */
  55. enum pm_qos_flags_status __dev_pm_qos_flags(struct device *dev, s32 mask)
  56. {
  57. struct dev_pm_qos *qos = dev->power.qos;
  58. struct pm_qos_flags *pqf;
  59. s32 val;
  60. if (IS_ERR_OR_NULL(qos))
  61. return PM_QOS_FLAGS_UNDEFINED;
  62. pqf = &qos->flags;
  63. if (list_empty(&pqf->list))
  64. return PM_QOS_FLAGS_UNDEFINED;
  65. val = pqf->effective_flags & mask;
  66. if (val)
  67. return (val == mask) ? PM_QOS_FLAGS_ALL : PM_QOS_FLAGS_SOME;
  68. return PM_QOS_FLAGS_NONE;
  69. }
  70. /**
  71. * dev_pm_qos_flags - Check PM QoS flags for a given device (locked).
  72. * @dev: Device to check the PM QoS flags for.
  73. * @mask: Flags to check against.
  74. */
  75. enum pm_qos_flags_status dev_pm_qos_flags(struct device *dev, s32 mask)
  76. {
  77. unsigned long irqflags;
  78. enum pm_qos_flags_status ret;
  79. spin_lock_irqsave(&dev->power.lock, irqflags);
  80. ret = __dev_pm_qos_flags(dev, mask);
  81. spin_unlock_irqrestore(&dev->power.lock, irqflags);
  82. return ret;
  83. }
  84. EXPORT_SYMBOL_GPL(dev_pm_qos_flags);
  85. /**
  86. * __dev_pm_qos_read_value - Get PM QoS constraint for a given device.
  87. * @dev: Device to get the PM QoS constraint value for.
  88. *
  89. * This routine must be called with dev->power.lock held.
  90. */
  91. s32 __dev_pm_qos_read_value(struct device *dev)
  92. {
  93. return IS_ERR_OR_NULL(dev->power.qos) ?
  94. 0 : pm_qos_read_value(&dev->power.qos->latency);
  95. }
  96. /**
  97. * dev_pm_qos_read_value - Get PM QoS constraint for a given device (locked).
  98. * @dev: Device to get the PM QoS constraint value for.
  99. */
  100. s32 dev_pm_qos_read_value(struct device *dev)
  101. {
  102. unsigned long flags;
  103. s32 ret;
  104. spin_lock_irqsave(&dev->power.lock, flags);
  105. ret = __dev_pm_qos_read_value(dev);
  106. spin_unlock_irqrestore(&dev->power.lock, flags);
  107. return ret;
  108. }
  109. /**
  110. * apply_constraint - Add/modify/remove device PM QoS request.
  111. * @req: Constraint request to apply
  112. * @action: Action to perform (add/update/remove).
  113. * @value: Value to assign to the QoS request.
  114. *
  115. * Internal function to update the constraints list using the PM QoS core
  116. * code and if needed call the per-device and the global notification
  117. * callbacks
  118. */
  119. static int apply_constraint(struct dev_pm_qos_request *req,
  120. enum pm_qos_req_action action, s32 value)
  121. {
  122. struct dev_pm_qos *qos = req->dev->power.qos;
  123. int ret;
  124. switch(req->type) {
  125. case DEV_PM_QOS_LATENCY:
  126. ret = pm_qos_update_target(&qos->latency, &req->data.pnode,
  127. action, value);
  128. if (ret) {
  129. value = pm_qos_read_value(&qos->latency);
  130. blocking_notifier_call_chain(&dev_pm_notifiers,
  131. (unsigned long)value,
  132. req);
  133. }
  134. break;
  135. case DEV_PM_QOS_FLAGS:
  136. ret = pm_qos_update_flags(&qos->flags, &req->data.flr,
  137. action, value);
  138. break;
  139. default:
  140. ret = -EINVAL;
  141. }
  142. return ret;
  143. }
  144. /*
  145. * dev_pm_qos_constraints_allocate
  146. * @dev: device to allocate data for
  147. *
  148. * Called at the first call to add_request, for constraint data allocation
  149. * Must be called with the dev_pm_qos_mtx mutex held
  150. */
  151. static int dev_pm_qos_constraints_allocate(struct device *dev)
  152. {
  153. struct dev_pm_qos *qos;
  154. struct pm_qos_constraints *c;
  155. struct blocking_notifier_head *n;
  156. qos = kzalloc(sizeof(*qos), GFP_KERNEL);
  157. if (!qos)
  158. return -ENOMEM;
  159. n = kzalloc(sizeof(*n), GFP_KERNEL);
  160. if (!n) {
  161. kfree(qos);
  162. return -ENOMEM;
  163. }
  164. BLOCKING_INIT_NOTIFIER_HEAD(n);
  165. c = &qos->latency;
  166. plist_head_init(&c->list);
  167. c->target_value = PM_QOS_DEV_LAT_DEFAULT_VALUE;
  168. c->default_value = PM_QOS_DEV_LAT_DEFAULT_VALUE;
  169. c->type = PM_QOS_MIN;
  170. c->notifiers = n;
  171. INIT_LIST_HEAD(&qos->flags.list);
  172. spin_lock_irq(&dev->power.lock);
  173. dev->power.qos = qos;
  174. spin_unlock_irq(&dev->power.lock);
  175. return 0;
  176. }
  177. static void __dev_pm_qos_hide_latency_limit(struct device *dev);
  178. static void __dev_pm_qos_hide_flags(struct device *dev);
  179. /**
  180. * dev_pm_qos_constraints_destroy
  181. * @dev: target device
  182. *
  183. * Called from the device PM subsystem on device removal under device_pm_lock().
  184. */
  185. void dev_pm_qos_constraints_destroy(struct device *dev)
  186. {
  187. struct dev_pm_qos *qos;
  188. struct dev_pm_qos_request *req, *tmp;
  189. struct pm_qos_constraints *c;
  190. struct pm_qos_flags *f;
  191. mutex_lock(&dev_pm_qos_sysfs_mtx);
  192. /*
  193. * If the device's PM QoS resume latency limit or PM QoS flags have been
  194. * exposed to user space, they have to be hidden at this point.
  195. */
  196. pm_qos_sysfs_remove_latency(dev);
  197. pm_qos_sysfs_remove_flags(dev);
  198. mutex_lock(&dev_pm_qos_mtx);
  199. __dev_pm_qos_hide_latency_limit(dev);
  200. __dev_pm_qos_hide_flags(dev);
  201. qos = dev->power.qos;
  202. if (!qos)
  203. goto out;
  204. /* Flush the constraints lists for the device. */
  205. c = &qos->latency;
  206. plist_for_each_entry_safe(req, tmp, &c->list, data.pnode) {
  207. /*
  208. * Update constraints list and call the notification
  209. * callbacks if needed
  210. */
  211. apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
  212. memset(req, 0, sizeof(*req));
  213. }
  214. f = &qos->flags;
  215. list_for_each_entry_safe(req, tmp, &f->list, data.flr.node) {
  216. apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
  217. memset(req, 0, sizeof(*req));
  218. }
  219. spin_lock_irq(&dev->power.lock);
  220. dev->power.qos = ERR_PTR(-ENODEV);
  221. spin_unlock_irq(&dev->power.lock);
  222. kfree(c->notifiers);
  223. kfree(qos);
  224. out:
  225. mutex_unlock(&dev_pm_qos_mtx);
  226. mutex_unlock(&dev_pm_qos_sysfs_mtx);
  227. }
  228. /**
  229. * dev_pm_qos_add_request - inserts new qos request into the list
  230. * @dev: target device for the constraint
  231. * @req: pointer to a preallocated handle
  232. * @type: type of the request
  233. * @value: defines the qos request
  234. *
  235. * This function inserts a new entry in the device constraints list of
  236. * requested qos performance characteristics. It recomputes the aggregate
  237. * QoS expectations of parameters and initializes the dev_pm_qos_request
  238. * handle. Caller needs to save this handle for later use in updates and
  239. * removal.
  240. *
  241. * Returns 1 if the aggregated constraint value has changed,
  242. * 0 if the aggregated constraint value has not changed,
  243. * -EINVAL in case of wrong parameters, -ENOMEM if there's not enough memory
  244. * to allocate for data structures, -ENODEV if the device has just been removed
  245. * from the system.
  246. *
  247. * Callers should ensure that the target device is not RPM_SUSPENDED before
  248. * using this function for requests of type DEV_PM_QOS_FLAGS.
  249. */
  250. int dev_pm_qos_add_request(struct device *dev, struct dev_pm_qos_request *req,
  251. enum dev_pm_qos_req_type type, s32 value)
  252. {
  253. int ret = 0;
  254. if (!dev || !req) /*guard against callers passing in null */
  255. return -EINVAL;
  256. if (WARN(dev_pm_qos_request_active(req),
  257. "%s() called for already added request\n", __func__))
  258. return -EINVAL;
  259. mutex_lock(&dev_pm_qos_mtx);
  260. if (IS_ERR(dev->power.qos))
  261. ret = -ENODEV;
  262. else if (!dev->power.qos)
  263. ret = dev_pm_qos_constraints_allocate(dev);
  264. if (!ret) {
  265. req->dev = dev;
  266. req->type = type;
  267. ret = apply_constraint(req, PM_QOS_ADD_REQ, value);
  268. }
  269. mutex_unlock(&dev_pm_qos_mtx);
  270. return ret;
  271. }
  272. EXPORT_SYMBOL_GPL(dev_pm_qos_add_request);
  273. /**
  274. * __dev_pm_qos_update_request - Modify an existing device PM QoS request.
  275. * @req : PM QoS request to modify.
  276. * @new_value: New value to request.
  277. */
  278. static int __dev_pm_qos_update_request(struct dev_pm_qos_request *req,
  279. s32 new_value)
  280. {
  281. s32 curr_value;
  282. int ret = 0;
  283. if (!req) /*guard against callers passing in null */
  284. return -EINVAL;
  285. if (WARN(!dev_pm_qos_request_active(req),
  286. "%s() called for unknown object\n", __func__))
  287. return -EINVAL;
  288. if (IS_ERR_OR_NULL(req->dev->power.qos))
  289. return -ENODEV;
  290. switch(req->type) {
  291. case DEV_PM_QOS_LATENCY:
  292. curr_value = req->data.pnode.prio;
  293. break;
  294. case DEV_PM_QOS_FLAGS:
  295. curr_value = req->data.flr.flags;
  296. break;
  297. default:
  298. return -EINVAL;
  299. }
  300. if (curr_value != new_value)
  301. ret = apply_constraint(req, PM_QOS_UPDATE_REQ, new_value);
  302. return ret;
  303. }
  304. /**
  305. * dev_pm_qos_update_request - modifies an existing qos request
  306. * @req : handle to list element holding a dev_pm_qos request to use
  307. * @new_value: defines the qos request
  308. *
  309. * Updates an existing dev PM qos request along with updating the
  310. * target value.
  311. *
  312. * Attempts are made to make this code callable on hot code paths.
  313. *
  314. * Returns 1 if the aggregated constraint value has changed,
  315. * 0 if the aggregated constraint value has not changed,
  316. * -EINVAL in case of wrong parameters, -ENODEV if the device has been
  317. * removed from the system
  318. *
  319. * Callers should ensure that the target device is not RPM_SUSPENDED before
  320. * using this function for requests of type DEV_PM_QOS_FLAGS.
  321. */
  322. int dev_pm_qos_update_request(struct dev_pm_qos_request *req, s32 new_value)
  323. {
  324. int ret;
  325. mutex_lock(&dev_pm_qos_mtx);
  326. ret = __dev_pm_qos_update_request(req, new_value);
  327. mutex_unlock(&dev_pm_qos_mtx);
  328. return ret;
  329. }
  330. EXPORT_SYMBOL_GPL(dev_pm_qos_update_request);
  331. static int __dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
  332. {
  333. int ret;
  334. if (!req) /*guard against callers passing in null */
  335. return -EINVAL;
  336. if (WARN(!dev_pm_qos_request_active(req),
  337. "%s() called for unknown object\n", __func__))
  338. return -EINVAL;
  339. if (IS_ERR_OR_NULL(req->dev->power.qos))
  340. return -ENODEV;
  341. ret = apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
  342. memset(req, 0, sizeof(*req));
  343. return ret;
  344. }
  345. /**
  346. * dev_pm_qos_remove_request - modifies an existing qos request
  347. * @req: handle to request list element
  348. *
  349. * Will remove pm qos request from the list of constraints and
  350. * recompute the current target value. Call this on slow code paths.
  351. *
  352. * Returns 1 if the aggregated constraint value has changed,
  353. * 0 if the aggregated constraint value has not changed,
  354. * -EINVAL in case of wrong parameters, -ENODEV if the device has been
  355. * removed from the system
  356. *
  357. * Callers should ensure that the target device is not RPM_SUSPENDED before
  358. * using this function for requests of type DEV_PM_QOS_FLAGS.
  359. */
  360. int dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
  361. {
  362. int ret;
  363. mutex_lock(&dev_pm_qos_mtx);
  364. ret = __dev_pm_qos_remove_request(req);
  365. mutex_unlock(&dev_pm_qos_mtx);
  366. return ret;
  367. }
  368. EXPORT_SYMBOL_GPL(dev_pm_qos_remove_request);
  369. /**
  370. * dev_pm_qos_add_notifier - sets notification entry for changes to target value
  371. * of per-device PM QoS constraints
  372. *
  373. * @dev: target device for the constraint
  374. * @notifier: notifier block managed by caller.
  375. *
  376. * Will register the notifier into a notification chain that gets called
  377. * upon changes to the target value for the device.
  378. *
  379. * If the device's constraints object doesn't exist when this routine is called,
  380. * it will be created (or error code will be returned if that fails).
  381. */
  382. int dev_pm_qos_add_notifier(struct device *dev, struct notifier_block *notifier)
  383. {
  384. int ret = 0;
  385. mutex_lock(&dev_pm_qos_mtx);
  386. if (IS_ERR(dev->power.qos))
  387. ret = -ENODEV;
  388. else if (!dev->power.qos)
  389. ret = dev_pm_qos_constraints_allocate(dev);
  390. if (!ret)
  391. ret = blocking_notifier_chain_register(
  392. dev->power.qos->latency.notifiers, notifier);
  393. mutex_unlock(&dev_pm_qos_mtx);
  394. return ret;
  395. }
  396. EXPORT_SYMBOL_GPL(dev_pm_qos_add_notifier);
  397. /**
  398. * dev_pm_qos_remove_notifier - deletes notification for changes to target value
  399. * of per-device PM QoS constraints
  400. *
  401. * @dev: target device for the constraint
  402. * @notifier: notifier block to be removed.
  403. *
  404. * Will remove the notifier from the notification chain that gets called
  405. * upon changes to the target value.
  406. */
  407. int dev_pm_qos_remove_notifier(struct device *dev,
  408. struct notifier_block *notifier)
  409. {
  410. int retval = 0;
  411. mutex_lock(&dev_pm_qos_mtx);
  412. /* Silently return if the constraints object is not present. */
  413. if (!IS_ERR_OR_NULL(dev->power.qos))
  414. retval = blocking_notifier_chain_unregister(
  415. dev->power.qos->latency.notifiers,
  416. notifier);
  417. mutex_unlock(&dev_pm_qos_mtx);
  418. return retval;
  419. }
  420. EXPORT_SYMBOL_GPL(dev_pm_qos_remove_notifier);
  421. /**
  422. * dev_pm_qos_add_global_notifier - sets notification entry for changes to
  423. * target value of the PM QoS constraints for any device
  424. *
  425. * @notifier: notifier block managed by caller.
  426. *
  427. * Will register the notifier into a notification chain that gets called
  428. * upon changes to the target value for any device.
  429. */
  430. int dev_pm_qos_add_global_notifier(struct notifier_block *notifier)
  431. {
  432. return blocking_notifier_chain_register(&dev_pm_notifiers, notifier);
  433. }
  434. EXPORT_SYMBOL_GPL(dev_pm_qos_add_global_notifier);
  435. /**
  436. * dev_pm_qos_remove_global_notifier - deletes notification for changes to
  437. * target value of PM QoS constraints for any device
  438. *
  439. * @notifier: notifier block to be removed.
  440. *
  441. * Will remove the notifier from the notification chain that gets called
  442. * upon changes to the target value for any device.
  443. */
  444. int dev_pm_qos_remove_global_notifier(struct notifier_block *notifier)
  445. {
  446. return blocking_notifier_chain_unregister(&dev_pm_notifiers, notifier);
  447. }
  448. EXPORT_SYMBOL_GPL(dev_pm_qos_remove_global_notifier);
  449. /**
  450. * dev_pm_qos_add_ancestor_request - Add PM QoS request for device's ancestor.
  451. * @dev: Device whose ancestor to add the request for.
  452. * @req: Pointer to the preallocated handle.
  453. * @value: Constraint latency value.
  454. */
  455. int dev_pm_qos_add_ancestor_request(struct device *dev,
  456. struct dev_pm_qos_request *req, s32 value)
  457. {
  458. struct device *ancestor = dev->parent;
  459. int ret = -ENODEV;
  460. while (ancestor && !ancestor->power.ignore_children)
  461. ancestor = ancestor->parent;
  462. if (ancestor)
  463. ret = dev_pm_qos_add_request(ancestor, req,
  464. DEV_PM_QOS_LATENCY, value);
  465. if (ret < 0)
  466. req->dev = NULL;
  467. return ret;
  468. }
  469. EXPORT_SYMBOL_GPL(dev_pm_qos_add_ancestor_request);
  470. #ifdef CONFIG_PM_RUNTIME
  471. static void __dev_pm_qos_drop_user_request(struct device *dev,
  472. enum dev_pm_qos_req_type type)
  473. {
  474. struct dev_pm_qos_request *req = NULL;
  475. switch(type) {
  476. case DEV_PM_QOS_LATENCY:
  477. req = dev->power.qos->latency_req;
  478. dev->power.qos->latency_req = NULL;
  479. break;
  480. case DEV_PM_QOS_FLAGS:
  481. req = dev->power.qos->flags_req;
  482. dev->power.qos->flags_req = NULL;
  483. break;
  484. }
  485. __dev_pm_qos_remove_request(req);
  486. kfree(req);
  487. }
  488. static void dev_pm_qos_drop_user_request(struct device *dev,
  489. enum dev_pm_qos_req_type type)
  490. {
  491. mutex_lock(&dev_pm_qos_mtx);
  492. __dev_pm_qos_drop_user_request(dev, type);
  493. mutex_unlock(&dev_pm_qos_mtx);
  494. }
  495. /**
  496. * dev_pm_qos_expose_latency_limit - Expose PM QoS latency limit to user space.
  497. * @dev: Device whose PM QoS latency limit is to be exposed to user space.
  498. * @value: Initial value of the latency limit.
  499. */
  500. int dev_pm_qos_expose_latency_limit(struct device *dev, s32 value)
  501. {
  502. struct dev_pm_qos_request *req;
  503. int ret;
  504. if (!device_is_registered(dev) || value < 0)
  505. return -EINVAL;
  506. req = kzalloc(sizeof(*req), GFP_KERNEL);
  507. if (!req)
  508. return -ENOMEM;
  509. ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_LATENCY, value);
  510. if (ret < 0) {
  511. kfree(req);
  512. return ret;
  513. }
  514. mutex_lock(&dev_pm_qos_sysfs_mtx);
  515. mutex_lock(&dev_pm_qos_mtx);
  516. if (IS_ERR_OR_NULL(dev->power.qos))
  517. ret = -ENODEV;
  518. else if (dev->power.qos->latency_req)
  519. ret = -EEXIST;
  520. if (ret < 0) {
  521. __dev_pm_qos_remove_request(req);
  522. kfree(req);
  523. mutex_unlock(&dev_pm_qos_mtx);
  524. goto out;
  525. }
  526. dev->power.qos->latency_req = req;
  527. mutex_unlock(&dev_pm_qos_mtx);
  528. ret = pm_qos_sysfs_add_latency(dev);
  529. if (ret)
  530. dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_LATENCY);
  531. out:
  532. mutex_unlock(&dev_pm_qos_sysfs_mtx);
  533. return ret;
  534. }
  535. EXPORT_SYMBOL_GPL(dev_pm_qos_expose_latency_limit);
  536. static void __dev_pm_qos_hide_latency_limit(struct device *dev)
  537. {
  538. if (!IS_ERR_OR_NULL(dev->power.qos) && dev->power.qos->latency_req)
  539. __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_LATENCY);
  540. }
  541. /**
  542. * dev_pm_qos_hide_latency_limit - Hide PM QoS latency limit from user space.
  543. * @dev: Device whose PM QoS latency limit is to be hidden from user space.
  544. */
  545. void dev_pm_qos_hide_latency_limit(struct device *dev)
  546. {
  547. mutex_lock(&dev_pm_qos_sysfs_mtx);
  548. pm_qos_sysfs_remove_latency(dev);
  549. mutex_lock(&dev_pm_qos_mtx);
  550. __dev_pm_qos_hide_latency_limit(dev);
  551. mutex_unlock(&dev_pm_qos_mtx);
  552. mutex_unlock(&dev_pm_qos_sysfs_mtx);
  553. }
  554. EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_limit);
  555. /**
  556. * dev_pm_qos_expose_flags - Expose PM QoS flags of a device to user space.
  557. * @dev: Device whose PM QoS flags are to be exposed to user space.
  558. * @val: Initial values of the flags.
  559. */
  560. int dev_pm_qos_expose_flags(struct device *dev, s32 val)
  561. {
  562. struct dev_pm_qos_request *req;
  563. int ret;
  564. if (!device_is_registered(dev))
  565. return -EINVAL;
  566. req = kzalloc(sizeof(*req), GFP_KERNEL);
  567. if (!req)
  568. return -ENOMEM;
  569. ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_FLAGS, val);
  570. if (ret < 0) {
  571. kfree(req);
  572. return ret;
  573. }
  574. pm_runtime_get_sync(dev);
  575. mutex_lock(&dev_pm_qos_sysfs_mtx);
  576. mutex_lock(&dev_pm_qos_mtx);
  577. if (IS_ERR_OR_NULL(dev->power.qos))
  578. ret = -ENODEV;
  579. else if (dev->power.qos->flags_req)
  580. ret = -EEXIST;
  581. if (ret < 0) {
  582. __dev_pm_qos_remove_request(req);
  583. kfree(req);
  584. mutex_unlock(&dev_pm_qos_mtx);
  585. goto out;
  586. }
  587. dev->power.qos->flags_req = req;
  588. mutex_unlock(&dev_pm_qos_mtx);
  589. ret = pm_qos_sysfs_add_flags(dev);
  590. if (ret)
  591. dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);
  592. out:
  593. mutex_unlock(&dev_pm_qos_sysfs_mtx);
  594. pm_runtime_put(dev);
  595. return ret;
  596. }
  597. EXPORT_SYMBOL_GPL(dev_pm_qos_expose_flags);
  598. static void __dev_pm_qos_hide_flags(struct device *dev)
  599. {
  600. if (!IS_ERR_OR_NULL(dev->power.qos) && dev->power.qos->flags_req)
  601. __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);
  602. }
  603. /**
  604. * dev_pm_qos_hide_flags - Hide PM QoS flags of a device from user space.
  605. * @dev: Device whose PM QoS flags are to be hidden from user space.
  606. */
  607. void dev_pm_qos_hide_flags(struct device *dev)
  608. {
  609. pm_runtime_get_sync(dev);
  610. mutex_lock(&dev_pm_qos_sysfs_mtx);
  611. pm_qos_sysfs_remove_flags(dev);
  612. mutex_lock(&dev_pm_qos_mtx);
  613. __dev_pm_qos_hide_flags(dev);
  614. mutex_unlock(&dev_pm_qos_mtx);
  615. mutex_unlock(&dev_pm_qos_sysfs_mtx);
  616. pm_runtime_put(dev);
  617. }
  618. EXPORT_SYMBOL_GPL(dev_pm_qos_hide_flags);
  619. /**
  620. * dev_pm_qos_update_flags - Update PM QoS flags request owned by user space.
  621. * @dev: Device to update the PM QoS flags request for.
  622. * @mask: Flags to set/clear.
  623. * @set: Whether to set or clear the flags (true means set).
  624. */
  625. int dev_pm_qos_update_flags(struct device *dev, s32 mask, bool set)
  626. {
  627. s32 value;
  628. int ret;
  629. pm_runtime_get_sync(dev);
  630. mutex_lock(&dev_pm_qos_mtx);
  631. if (IS_ERR_OR_NULL(dev->power.qos) || !dev->power.qos->flags_req) {
  632. ret = -EINVAL;
  633. goto out;
  634. }
  635. value = dev_pm_qos_requested_flags(dev);
  636. if (set)
  637. value |= mask;
  638. else
  639. value &= ~mask;
  640. ret = __dev_pm_qos_update_request(dev->power.qos->flags_req, value);
  641. out:
  642. mutex_unlock(&dev_pm_qos_mtx);
  643. pm_runtime_put(dev);
  644. return ret;
  645. }
  646. #else /* !CONFIG_PM_RUNTIME */
  647. static void __dev_pm_qos_hide_latency_limit(struct device *dev) {}
  648. static void __dev_pm_qos_hide_flags(struct device *dev) {}
  649. #endif /* CONFIG_PM_RUNTIME */