qos.c 20 KB

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