qos.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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. struct pm_qos_flags *f;
  199. /*
  200. * If the device's PM QoS resume latency limit or PM QoS flags have been
  201. * exposed to user space, they have to be hidden at this point.
  202. */
  203. dev_pm_qos_hide_latency_limit(dev);
  204. dev_pm_qos_hide_flags(dev);
  205. mutex_lock(&dev_pm_qos_mtx);
  206. dev->power.power_state = PMSG_INVALID;
  207. qos = dev->power.qos;
  208. if (!qos)
  209. goto out;
  210. /* Flush the constraints lists for the device. */
  211. c = &qos->latency;
  212. plist_for_each_entry_safe(req, tmp, &c->list, data.pnode) {
  213. /*
  214. * Update constraints list and call the notification
  215. * callbacks if needed
  216. */
  217. apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
  218. memset(req, 0, sizeof(*req));
  219. }
  220. f = &qos->flags;
  221. list_for_each_entry_safe(req, tmp, &f->list, data.flr.node) {
  222. apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
  223. memset(req, 0, sizeof(*req));
  224. }
  225. spin_lock_irq(&dev->power.lock);
  226. dev->power.qos = NULL;
  227. spin_unlock_irq(&dev->power.lock);
  228. kfree(c->notifiers);
  229. kfree(qos);
  230. out:
  231. mutex_unlock(&dev_pm_qos_mtx);
  232. }
  233. /**
  234. * dev_pm_qos_add_request - inserts new qos request into the list
  235. * @dev: target device for the constraint
  236. * @req: pointer to a preallocated handle
  237. * @type: type of the request
  238. * @value: defines the qos request
  239. *
  240. * This function inserts a new entry in the device constraints list of
  241. * requested qos performance characteristics. It recomputes the aggregate
  242. * QoS expectations of parameters and initializes the dev_pm_qos_request
  243. * handle. Caller needs to save this handle for later use in updates and
  244. * removal.
  245. *
  246. * Returns 1 if the aggregated constraint value has changed,
  247. * 0 if the aggregated constraint value has not changed,
  248. * -EINVAL in case of wrong parameters, -ENOMEM if there's not enough memory
  249. * to allocate for data structures, -ENODEV if the device has just been removed
  250. * from the system.
  251. *
  252. * Callers should ensure that the target device is not RPM_SUSPENDED before
  253. * using this function for requests of type DEV_PM_QOS_FLAGS.
  254. */
  255. int dev_pm_qos_add_request(struct device *dev, struct dev_pm_qos_request *req,
  256. enum dev_pm_qos_req_type type, s32 value)
  257. {
  258. int ret = 0;
  259. if (!dev || !req) /*guard against callers passing in null */
  260. return -EINVAL;
  261. if (WARN(dev_pm_qos_request_active(req),
  262. "%s() called for already added request\n", __func__))
  263. return -EINVAL;
  264. req->dev = dev;
  265. mutex_lock(&dev_pm_qos_mtx);
  266. if (!dev->power.qos) {
  267. if (dev->power.power_state.event == PM_EVENT_INVALID) {
  268. /* The device has been removed from the system. */
  269. req->dev = NULL;
  270. ret = -ENODEV;
  271. goto out;
  272. } else {
  273. /*
  274. * Allocate the constraints data on the first call to
  275. * add_request, i.e. only if the data is not already
  276. * allocated and if the device has not been removed.
  277. */
  278. ret = dev_pm_qos_constraints_allocate(dev);
  279. }
  280. }
  281. if (!ret) {
  282. req->type = type;
  283. ret = apply_constraint(req, PM_QOS_ADD_REQ, value);
  284. }
  285. out:
  286. mutex_unlock(&dev_pm_qos_mtx);
  287. return ret;
  288. }
  289. EXPORT_SYMBOL_GPL(dev_pm_qos_add_request);
  290. /**
  291. * __dev_pm_qos_update_request - Modify an existing device PM QoS request.
  292. * @req : PM QoS request to modify.
  293. * @new_value: New value to request.
  294. */
  295. static int __dev_pm_qos_update_request(struct dev_pm_qos_request *req,
  296. s32 new_value)
  297. {
  298. s32 curr_value;
  299. int ret = 0;
  300. if (!req->dev->power.qos)
  301. return -ENODEV;
  302. switch(req->type) {
  303. case DEV_PM_QOS_LATENCY:
  304. curr_value = req->data.pnode.prio;
  305. break;
  306. case DEV_PM_QOS_FLAGS:
  307. curr_value = req->data.flr.flags;
  308. break;
  309. default:
  310. return -EINVAL;
  311. }
  312. if (curr_value != new_value)
  313. ret = apply_constraint(req, PM_QOS_UPDATE_REQ, new_value);
  314. return ret;
  315. }
  316. /**
  317. * dev_pm_qos_update_request - modifies an existing qos request
  318. * @req : handle to list element holding a dev_pm_qos request to use
  319. * @new_value: defines the qos request
  320. *
  321. * Updates an existing dev PM qos request along with updating the
  322. * target value.
  323. *
  324. * Attempts are made to make this code callable on hot code paths.
  325. *
  326. * Returns 1 if the aggregated constraint value has changed,
  327. * 0 if the aggregated constraint value has not changed,
  328. * -EINVAL in case of wrong parameters, -ENODEV if the device has been
  329. * removed from the system
  330. *
  331. * Callers should ensure that the target device is not RPM_SUSPENDED before
  332. * using this function for requests of type DEV_PM_QOS_FLAGS.
  333. */
  334. int dev_pm_qos_update_request(struct dev_pm_qos_request *req, s32 new_value)
  335. {
  336. int ret;
  337. if (!req) /*guard against callers passing in null */
  338. return -EINVAL;
  339. if (WARN(!dev_pm_qos_request_active(req),
  340. "%s() called for unknown object\n", __func__))
  341. return -EINVAL;
  342. mutex_lock(&dev_pm_qos_mtx);
  343. ret = __dev_pm_qos_update_request(req, new_value);
  344. mutex_unlock(&dev_pm_qos_mtx);
  345. return ret;
  346. }
  347. EXPORT_SYMBOL_GPL(dev_pm_qos_update_request);
  348. /**
  349. * dev_pm_qos_remove_request - modifies an existing qos request
  350. * @req: handle to request list element
  351. *
  352. * Will remove pm qos request from the list of constraints and
  353. * recompute the current target value. Call this on slow code paths.
  354. *
  355. * Returns 1 if the aggregated constraint value has changed,
  356. * 0 if the aggregated constraint value has not changed,
  357. * -EINVAL in case of wrong parameters, -ENODEV if the device has been
  358. * removed from the system
  359. *
  360. * Callers should ensure that the target device is not RPM_SUSPENDED before
  361. * using this function for requests of type DEV_PM_QOS_FLAGS.
  362. */
  363. int dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
  364. {
  365. int ret = 0;
  366. if (!req) /*guard against callers passing in null */
  367. return -EINVAL;
  368. if (WARN(!dev_pm_qos_request_active(req),
  369. "%s() called for unknown object\n", __func__))
  370. return -EINVAL;
  371. mutex_lock(&dev_pm_qos_mtx);
  372. if (req->dev->power.qos) {
  373. ret = apply_constraint(req, PM_QOS_REMOVE_REQ,
  374. PM_QOS_DEFAULT_VALUE);
  375. memset(req, 0, sizeof(*req));
  376. } else {
  377. /* Return if the device has been removed */
  378. ret = -ENODEV;
  379. }
  380. mutex_unlock(&dev_pm_qos_mtx);
  381. return ret;
  382. }
  383. EXPORT_SYMBOL_GPL(dev_pm_qos_remove_request);
  384. /**
  385. * dev_pm_qos_add_notifier - sets notification entry for changes to target value
  386. * of per-device PM QoS constraints
  387. *
  388. * @dev: target device for the constraint
  389. * @notifier: notifier block managed by caller.
  390. *
  391. * Will register the notifier into a notification chain that gets called
  392. * upon changes to the target value for the device.
  393. *
  394. * If the device's constraints object doesn't exist when this routine is called,
  395. * it will be created (or error code will be returned if that fails).
  396. */
  397. int dev_pm_qos_add_notifier(struct device *dev, struct notifier_block *notifier)
  398. {
  399. int ret = 0;
  400. mutex_lock(&dev_pm_qos_mtx);
  401. if (!dev->power.qos)
  402. ret = dev->power.power_state.event != PM_EVENT_INVALID ?
  403. dev_pm_qos_constraints_allocate(dev) : -ENODEV;
  404. if (!ret)
  405. ret = blocking_notifier_chain_register(
  406. dev->power.qos->latency.notifiers, notifier);
  407. mutex_unlock(&dev_pm_qos_mtx);
  408. return ret;
  409. }
  410. EXPORT_SYMBOL_GPL(dev_pm_qos_add_notifier);
  411. /**
  412. * dev_pm_qos_remove_notifier - deletes notification for changes to target value
  413. * of per-device PM QoS constraints
  414. *
  415. * @dev: target device for the constraint
  416. * @notifier: notifier block to be removed.
  417. *
  418. * Will remove the notifier from the notification chain that gets called
  419. * upon changes to the target value.
  420. */
  421. int dev_pm_qos_remove_notifier(struct device *dev,
  422. struct notifier_block *notifier)
  423. {
  424. int retval = 0;
  425. mutex_lock(&dev_pm_qos_mtx);
  426. /* Silently return if the constraints object is not present. */
  427. if (dev->power.qos)
  428. retval = blocking_notifier_chain_unregister(
  429. dev->power.qos->latency.notifiers,
  430. notifier);
  431. mutex_unlock(&dev_pm_qos_mtx);
  432. return retval;
  433. }
  434. EXPORT_SYMBOL_GPL(dev_pm_qos_remove_notifier);
  435. /**
  436. * dev_pm_qos_add_global_notifier - sets notification entry for changes to
  437. * target value of the PM QoS constraints for any device
  438. *
  439. * @notifier: notifier block managed by caller.
  440. *
  441. * Will register the notifier into a notification chain that gets called
  442. * upon changes to the target value for any device.
  443. */
  444. int dev_pm_qos_add_global_notifier(struct notifier_block *notifier)
  445. {
  446. return blocking_notifier_chain_register(&dev_pm_notifiers, notifier);
  447. }
  448. EXPORT_SYMBOL_GPL(dev_pm_qos_add_global_notifier);
  449. /**
  450. * dev_pm_qos_remove_global_notifier - deletes notification for changes to
  451. * target value of PM QoS constraints for any device
  452. *
  453. * @notifier: notifier block to be removed.
  454. *
  455. * Will remove the notifier from the notification chain that gets called
  456. * upon changes to the target value for any device.
  457. */
  458. int dev_pm_qos_remove_global_notifier(struct notifier_block *notifier)
  459. {
  460. return blocking_notifier_chain_unregister(&dev_pm_notifiers, notifier);
  461. }
  462. EXPORT_SYMBOL_GPL(dev_pm_qos_remove_global_notifier);
  463. /**
  464. * dev_pm_qos_add_ancestor_request - Add PM QoS request for device's ancestor.
  465. * @dev: Device whose ancestor to add the request for.
  466. * @req: Pointer to the preallocated handle.
  467. * @value: Constraint latency value.
  468. */
  469. int dev_pm_qos_add_ancestor_request(struct device *dev,
  470. struct dev_pm_qos_request *req, s32 value)
  471. {
  472. struct device *ancestor = dev->parent;
  473. int ret = -ENODEV;
  474. while (ancestor && !ancestor->power.ignore_children)
  475. ancestor = ancestor->parent;
  476. if (ancestor)
  477. ret = dev_pm_qos_add_request(ancestor, req,
  478. DEV_PM_QOS_LATENCY, value);
  479. if (ret < 0)
  480. req->dev = NULL;
  481. return ret;
  482. }
  483. EXPORT_SYMBOL_GPL(dev_pm_qos_add_ancestor_request);
  484. #ifdef CONFIG_PM_RUNTIME
  485. static void __dev_pm_qos_drop_user_request(struct device *dev,
  486. enum dev_pm_qos_req_type type)
  487. {
  488. switch(type) {
  489. case DEV_PM_QOS_LATENCY:
  490. dev_pm_qos_remove_request(dev->power.qos->latency_req);
  491. dev->power.qos->latency_req = NULL;
  492. break;
  493. case DEV_PM_QOS_FLAGS:
  494. dev_pm_qos_remove_request(dev->power.qos->flags_req);
  495. dev->power.qos->flags_req = NULL;
  496. break;
  497. }
  498. }
  499. /**
  500. * dev_pm_qos_expose_latency_limit - Expose PM QoS latency limit to user space.
  501. * @dev: Device whose PM QoS latency limit is to be exposed to user space.
  502. * @value: Initial value of the latency limit.
  503. */
  504. int dev_pm_qos_expose_latency_limit(struct device *dev, s32 value)
  505. {
  506. struct dev_pm_qos_request *req;
  507. int ret;
  508. if (!device_is_registered(dev) || value < 0)
  509. return -EINVAL;
  510. if (dev->power.qos && dev->power.qos->latency_req)
  511. return -EEXIST;
  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. return ret;
  518. dev->power.qos->latency_req = req;
  519. ret = pm_qos_sysfs_add_latency(dev);
  520. if (ret)
  521. __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_LATENCY);
  522. return ret;
  523. }
  524. EXPORT_SYMBOL_GPL(dev_pm_qos_expose_latency_limit);
  525. /**
  526. * dev_pm_qos_hide_latency_limit - Hide PM QoS latency limit from user space.
  527. * @dev: Device whose PM QoS latency limit is to be hidden from user space.
  528. */
  529. void dev_pm_qos_hide_latency_limit(struct device *dev)
  530. {
  531. if (dev->power.qos && dev->power.qos->latency_req) {
  532. pm_qos_sysfs_remove_latency(dev);
  533. __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_LATENCY);
  534. }
  535. }
  536. EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_limit);
  537. /**
  538. * dev_pm_qos_expose_flags - Expose PM QoS flags of a device to user space.
  539. * @dev: Device whose PM QoS flags are to be exposed to user space.
  540. * @val: Initial values of the flags.
  541. */
  542. int dev_pm_qos_expose_flags(struct device *dev, s32 val)
  543. {
  544. struct dev_pm_qos_request *req;
  545. int ret;
  546. if (!device_is_registered(dev))
  547. return -EINVAL;
  548. if (dev->power.qos && dev->power.qos->flags_req)
  549. return -EEXIST;
  550. req = kzalloc(sizeof(*req), GFP_KERNEL);
  551. if (!req)
  552. return -ENOMEM;
  553. pm_runtime_get_sync(dev);
  554. ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_FLAGS, val);
  555. if (ret < 0)
  556. goto fail;
  557. dev->power.qos->flags_req = req;
  558. ret = pm_qos_sysfs_add_flags(dev);
  559. if (ret)
  560. __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);
  561. fail:
  562. pm_runtime_put(dev);
  563. return ret;
  564. }
  565. EXPORT_SYMBOL_GPL(dev_pm_qos_expose_flags);
  566. /**
  567. * dev_pm_qos_hide_flags - Hide PM QoS flags of a device from user space.
  568. * @dev: Device whose PM QoS flags are to be hidden from user space.
  569. */
  570. void dev_pm_qos_hide_flags(struct device *dev)
  571. {
  572. if (dev->power.qos && dev->power.qos->flags_req) {
  573. pm_qos_sysfs_remove_flags(dev);
  574. pm_runtime_get_sync(dev);
  575. __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);
  576. pm_runtime_put(dev);
  577. }
  578. }
  579. EXPORT_SYMBOL_GPL(dev_pm_qos_hide_flags);
  580. /**
  581. * dev_pm_qos_update_flags - Update PM QoS flags request owned by user space.
  582. * @dev: Device to update the PM QoS flags request for.
  583. * @mask: Flags to set/clear.
  584. * @set: Whether to set or clear the flags (true means set).
  585. */
  586. int dev_pm_qos_update_flags(struct device *dev, s32 mask, bool set)
  587. {
  588. s32 value;
  589. int ret;
  590. if (!dev->power.qos || !dev->power.qos->flags_req)
  591. return -EINVAL;
  592. pm_runtime_get_sync(dev);
  593. mutex_lock(&dev_pm_qos_mtx);
  594. value = dev_pm_qos_requested_flags(dev);
  595. if (set)
  596. value |= mask;
  597. else
  598. value &= ~mask;
  599. ret = __dev_pm_qos_update_request(dev->power.qos->flags_req, value);
  600. mutex_unlock(&dev_pm_qos_mtx);
  601. pm_runtime_put(dev);
  602. return ret;
  603. }
  604. #endif /* CONFIG_PM_RUNTIME */