qos.c 19 KB

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