qos.c 19 KB

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