qos.c 19 KB

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