qos.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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->resume_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_RESUME_LATENCY:
  127. ret = pm_qos_update_target(&qos->resume_latency,
  128. &req->data.pnode, action, value);
  129. if (ret) {
  130. value = pm_qos_read_value(&qos->resume_latency);
  131. blocking_notifier_call_chain(&dev_pm_notifiers,
  132. (unsigned long)value,
  133. req);
  134. }
  135. break;
  136. case DEV_PM_QOS_LATENCY_TOLERANCE:
  137. ret = pm_qos_update_target(&qos->latency_tolerance,
  138. &req->data.pnode, action, value);
  139. if (ret) {
  140. value = pm_qos_read_value(&qos->latency_tolerance);
  141. req->dev->power.set_latency_tolerance(req->dev, value);
  142. }
  143. break;
  144. case DEV_PM_QOS_FLAGS:
  145. ret = pm_qos_update_flags(&qos->flags, &req->data.flr,
  146. action, value);
  147. break;
  148. default:
  149. ret = -EINVAL;
  150. }
  151. return ret;
  152. }
  153. /*
  154. * dev_pm_qos_constraints_allocate
  155. * @dev: device to allocate data for
  156. *
  157. * Called at the first call to add_request, for constraint data allocation
  158. * Must be called with the dev_pm_qos_mtx mutex held
  159. */
  160. static int dev_pm_qos_constraints_allocate(struct device *dev)
  161. {
  162. struct dev_pm_qos *qos;
  163. struct pm_qos_constraints *c;
  164. struct blocking_notifier_head *n;
  165. qos = kzalloc(sizeof(*qos), GFP_KERNEL);
  166. if (!qos)
  167. return -ENOMEM;
  168. n = kzalloc(sizeof(*n), GFP_KERNEL);
  169. if (!n) {
  170. kfree(qos);
  171. return -ENOMEM;
  172. }
  173. BLOCKING_INIT_NOTIFIER_HEAD(n);
  174. c = &qos->resume_latency;
  175. plist_head_init(&c->list);
  176. c->target_value = PM_QOS_RESUME_LATENCY_DEFAULT_VALUE;
  177. c->default_value = PM_QOS_RESUME_LATENCY_DEFAULT_VALUE;
  178. c->no_constraint_value = PM_QOS_RESUME_LATENCY_DEFAULT_VALUE;
  179. c->type = PM_QOS_MIN;
  180. c->notifiers = n;
  181. c = &qos->latency_tolerance;
  182. plist_head_init(&c->list);
  183. c->target_value = PM_QOS_LATENCY_TOLERANCE_DEFAULT_VALUE;
  184. c->default_value = PM_QOS_LATENCY_TOLERANCE_DEFAULT_VALUE;
  185. c->no_constraint_value = PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT;
  186. c->type = PM_QOS_MIN;
  187. INIT_LIST_HEAD(&qos->flags.list);
  188. spin_lock_irq(&dev->power.lock);
  189. dev->power.qos = qos;
  190. spin_unlock_irq(&dev->power.lock);
  191. return 0;
  192. }
  193. static void __dev_pm_qos_hide_latency_limit(struct device *dev);
  194. static void __dev_pm_qos_hide_flags(struct device *dev);
  195. /**
  196. * dev_pm_qos_constraints_destroy
  197. * @dev: target device
  198. *
  199. * Called from the device PM subsystem on device removal under device_pm_lock().
  200. */
  201. void dev_pm_qos_constraints_destroy(struct device *dev)
  202. {
  203. struct dev_pm_qos *qos;
  204. struct dev_pm_qos_request *req, *tmp;
  205. struct pm_qos_constraints *c;
  206. struct pm_qos_flags *f;
  207. mutex_lock(&dev_pm_qos_sysfs_mtx);
  208. /*
  209. * If the device's PM QoS resume latency limit or PM QoS flags have been
  210. * exposed to user space, they have to be hidden at this point.
  211. */
  212. pm_qos_sysfs_remove_resume_latency(dev);
  213. pm_qos_sysfs_remove_flags(dev);
  214. mutex_lock(&dev_pm_qos_mtx);
  215. __dev_pm_qos_hide_latency_limit(dev);
  216. __dev_pm_qos_hide_flags(dev);
  217. qos = dev->power.qos;
  218. if (!qos)
  219. goto out;
  220. /* Flush the constraints lists for the device. */
  221. c = &qos->resume_latency;
  222. plist_for_each_entry_safe(req, tmp, &c->list, data.pnode) {
  223. /*
  224. * Update constraints list and call the notification
  225. * callbacks if needed
  226. */
  227. apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
  228. memset(req, 0, sizeof(*req));
  229. }
  230. c = &qos->latency_tolerance;
  231. plist_for_each_entry_safe(req, tmp, &c->list, data.pnode) {
  232. apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
  233. memset(req, 0, sizeof(*req));
  234. }
  235. f = &qos->flags;
  236. list_for_each_entry_safe(req, tmp, &f->list, data.flr.node) {
  237. apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
  238. memset(req, 0, sizeof(*req));
  239. }
  240. spin_lock_irq(&dev->power.lock);
  241. dev->power.qos = ERR_PTR(-ENODEV);
  242. spin_unlock_irq(&dev->power.lock);
  243. kfree(c->notifiers);
  244. kfree(qos);
  245. out:
  246. mutex_unlock(&dev_pm_qos_mtx);
  247. mutex_unlock(&dev_pm_qos_sysfs_mtx);
  248. }
  249. static bool dev_pm_qos_invalid_request(struct device *dev,
  250. struct dev_pm_qos_request *req)
  251. {
  252. return !req || (req->type == DEV_PM_QOS_LATENCY_TOLERANCE
  253. && !dev->power.set_latency_tolerance);
  254. }
  255. static int __dev_pm_qos_add_request(struct device *dev,
  256. struct dev_pm_qos_request *req,
  257. enum dev_pm_qos_req_type type, s32 value)
  258. {
  259. int ret = 0;
  260. if (!dev || dev_pm_qos_invalid_request(dev, req))
  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. if (IS_ERR(dev->power.qos))
  266. ret = -ENODEV;
  267. else if (!dev->power.qos)
  268. ret = dev_pm_qos_constraints_allocate(dev);
  269. trace_dev_pm_qos_add_request(dev_name(dev), type, value);
  270. if (!ret) {
  271. req->dev = dev;
  272. req->type = type;
  273. ret = apply_constraint(req, PM_QOS_ADD_REQ, value);
  274. }
  275. return ret;
  276. }
  277. /**
  278. * dev_pm_qos_add_request - inserts new qos request into the list
  279. * @dev: target device for the constraint
  280. * @req: pointer to a preallocated handle
  281. * @type: type of the request
  282. * @value: defines the qos request
  283. *
  284. * This function inserts a new entry in the device constraints list of
  285. * requested qos performance characteristics. It recomputes the aggregate
  286. * QoS expectations of parameters and initializes the dev_pm_qos_request
  287. * handle. Caller needs to save this handle for later use in updates and
  288. * removal.
  289. *
  290. * Returns 1 if the aggregated constraint value has changed,
  291. * 0 if the aggregated constraint value has not changed,
  292. * -EINVAL in case of wrong parameters, -ENOMEM if there's not enough memory
  293. * to allocate for data structures, -ENODEV if the device has just been removed
  294. * from the system.
  295. *
  296. * Callers should ensure that the target device is not RPM_SUSPENDED before
  297. * using this function for requests of type DEV_PM_QOS_FLAGS.
  298. */
  299. int dev_pm_qos_add_request(struct device *dev, struct dev_pm_qos_request *req,
  300. enum dev_pm_qos_req_type type, s32 value)
  301. {
  302. int ret;
  303. mutex_lock(&dev_pm_qos_mtx);
  304. ret = __dev_pm_qos_add_request(dev, req, type, value);
  305. mutex_unlock(&dev_pm_qos_mtx);
  306. return ret;
  307. }
  308. EXPORT_SYMBOL_GPL(dev_pm_qos_add_request);
  309. /**
  310. * __dev_pm_qos_update_request - Modify an existing device PM QoS request.
  311. * @req : PM QoS request to modify.
  312. * @new_value: New value to request.
  313. */
  314. static int __dev_pm_qos_update_request(struct dev_pm_qos_request *req,
  315. s32 new_value)
  316. {
  317. s32 curr_value;
  318. int ret = 0;
  319. if (!req) /*guard against callers passing in null */
  320. return -EINVAL;
  321. if (WARN(!dev_pm_qos_request_active(req),
  322. "%s() called for unknown object\n", __func__))
  323. return -EINVAL;
  324. if (IS_ERR_OR_NULL(req->dev->power.qos))
  325. return -ENODEV;
  326. switch(req->type) {
  327. case DEV_PM_QOS_RESUME_LATENCY:
  328. case DEV_PM_QOS_LATENCY_TOLERANCE:
  329. curr_value = req->data.pnode.prio;
  330. break;
  331. case DEV_PM_QOS_FLAGS:
  332. curr_value = req->data.flr.flags;
  333. break;
  334. default:
  335. return -EINVAL;
  336. }
  337. trace_dev_pm_qos_update_request(dev_name(req->dev), req->type,
  338. new_value);
  339. if (curr_value != new_value)
  340. ret = apply_constraint(req, PM_QOS_UPDATE_REQ, new_value);
  341. return ret;
  342. }
  343. /**
  344. * dev_pm_qos_update_request - modifies an existing qos request
  345. * @req : handle to list element holding a dev_pm_qos request to use
  346. * @new_value: defines the qos request
  347. *
  348. * Updates an existing dev PM qos request along with updating the
  349. * target value.
  350. *
  351. * Attempts are made to make this code callable on hot code paths.
  352. *
  353. * Returns 1 if the aggregated constraint value has changed,
  354. * 0 if the aggregated constraint value has not changed,
  355. * -EINVAL in case of wrong parameters, -ENODEV if the device has been
  356. * removed from the system
  357. *
  358. * Callers should ensure that the target device is not RPM_SUSPENDED before
  359. * using this function for requests of type DEV_PM_QOS_FLAGS.
  360. */
  361. int dev_pm_qos_update_request(struct dev_pm_qos_request *req, s32 new_value)
  362. {
  363. int ret;
  364. mutex_lock(&dev_pm_qos_mtx);
  365. ret = __dev_pm_qos_update_request(req, new_value);
  366. mutex_unlock(&dev_pm_qos_mtx);
  367. return ret;
  368. }
  369. EXPORT_SYMBOL_GPL(dev_pm_qos_update_request);
  370. static int __dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
  371. {
  372. int ret;
  373. if (!req) /*guard against callers passing in null */
  374. return -EINVAL;
  375. if (WARN(!dev_pm_qos_request_active(req),
  376. "%s() called for unknown object\n", __func__))
  377. return -EINVAL;
  378. if (IS_ERR_OR_NULL(req->dev->power.qos))
  379. return -ENODEV;
  380. trace_dev_pm_qos_remove_request(dev_name(req->dev), req->type,
  381. PM_QOS_DEFAULT_VALUE);
  382. ret = apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
  383. memset(req, 0, sizeof(*req));
  384. return ret;
  385. }
  386. /**
  387. * dev_pm_qos_remove_request - modifies an existing qos request
  388. * @req: handle to request list element
  389. *
  390. * Will remove pm qos request from the list of constraints and
  391. * recompute the current target value. Call this on slow code paths.
  392. *
  393. * Returns 1 if the aggregated constraint value has changed,
  394. * 0 if the aggregated constraint value has not changed,
  395. * -EINVAL in case of wrong parameters, -ENODEV if the device has been
  396. * removed from the system
  397. *
  398. * Callers should ensure that the target device is not RPM_SUSPENDED before
  399. * using this function for requests of type DEV_PM_QOS_FLAGS.
  400. */
  401. int dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
  402. {
  403. int ret;
  404. mutex_lock(&dev_pm_qos_mtx);
  405. ret = __dev_pm_qos_remove_request(req);
  406. mutex_unlock(&dev_pm_qos_mtx);
  407. return ret;
  408. }
  409. EXPORT_SYMBOL_GPL(dev_pm_qos_remove_request);
  410. /**
  411. * dev_pm_qos_add_notifier - sets notification entry for changes to target value
  412. * of per-device PM QoS constraints
  413. *
  414. * @dev: target device for the constraint
  415. * @notifier: notifier block managed by caller.
  416. *
  417. * Will register the notifier into a notification chain that gets called
  418. * upon changes to the target value for the device.
  419. *
  420. * If the device's constraints object doesn't exist when this routine is called,
  421. * it will be created (or error code will be returned if that fails).
  422. */
  423. int dev_pm_qos_add_notifier(struct device *dev, struct notifier_block *notifier)
  424. {
  425. int ret = 0;
  426. mutex_lock(&dev_pm_qos_mtx);
  427. if (IS_ERR(dev->power.qos))
  428. ret = -ENODEV;
  429. else if (!dev->power.qos)
  430. ret = dev_pm_qos_constraints_allocate(dev);
  431. if (!ret)
  432. ret = blocking_notifier_chain_register(dev->power.qos->resume_latency.notifiers,
  433. notifier);
  434. mutex_unlock(&dev_pm_qos_mtx);
  435. return ret;
  436. }
  437. EXPORT_SYMBOL_GPL(dev_pm_qos_add_notifier);
  438. /**
  439. * dev_pm_qos_remove_notifier - deletes notification for changes to target value
  440. * of per-device PM QoS constraints
  441. *
  442. * @dev: target device for the constraint
  443. * @notifier: notifier block to be removed.
  444. *
  445. * Will remove the notifier from the notification chain that gets called
  446. * upon changes to the target value.
  447. */
  448. int dev_pm_qos_remove_notifier(struct device *dev,
  449. struct notifier_block *notifier)
  450. {
  451. int retval = 0;
  452. mutex_lock(&dev_pm_qos_mtx);
  453. /* Silently return if the constraints object is not present. */
  454. if (!IS_ERR_OR_NULL(dev->power.qos))
  455. retval = blocking_notifier_chain_unregister(dev->power.qos->resume_latency.notifiers,
  456. notifier);
  457. mutex_unlock(&dev_pm_qos_mtx);
  458. return retval;
  459. }
  460. EXPORT_SYMBOL_GPL(dev_pm_qos_remove_notifier);
  461. /**
  462. * dev_pm_qos_add_global_notifier - sets notification entry for changes to
  463. * target value of the PM QoS constraints for any device
  464. *
  465. * @notifier: notifier block managed by caller.
  466. *
  467. * Will register the notifier into a notification chain that gets called
  468. * upon changes to the target value for any device.
  469. */
  470. int dev_pm_qos_add_global_notifier(struct notifier_block *notifier)
  471. {
  472. return blocking_notifier_chain_register(&dev_pm_notifiers, notifier);
  473. }
  474. EXPORT_SYMBOL_GPL(dev_pm_qos_add_global_notifier);
  475. /**
  476. * dev_pm_qos_remove_global_notifier - deletes notification for changes to
  477. * target value of PM QoS constraints for any device
  478. *
  479. * @notifier: notifier block to be removed.
  480. *
  481. * Will remove the notifier from the notification chain that gets called
  482. * upon changes to the target value for any device.
  483. */
  484. int dev_pm_qos_remove_global_notifier(struct notifier_block *notifier)
  485. {
  486. return blocking_notifier_chain_unregister(&dev_pm_notifiers, notifier);
  487. }
  488. EXPORT_SYMBOL_GPL(dev_pm_qos_remove_global_notifier);
  489. /**
  490. * dev_pm_qos_add_ancestor_request - Add PM QoS request for device's ancestor.
  491. * @dev: Device whose ancestor to add the request for.
  492. * @req: Pointer to the preallocated handle.
  493. * @type: Type of the request.
  494. * @value: Constraint latency value.
  495. */
  496. int dev_pm_qos_add_ancestor_request(struct device *dev,
  497. struct dev_pm_qos_request *req,
  498. enum dev_pm_qos_req_type type, s32 value)
  499. {
  500. struct device *ancestor = dev->parent;
  501. int ret = -ENODEV;
  502. switch (type) {
  503. case DEV_PM_QOS_RESUME_LATENCY:
  504. while (ancestor && !ancestor->power.ignore_children)
  505. ancestor = ancestor->parent;
  506. break;
  507. case DEV_PM_QOS_LATENCY_TOLERANCE:
  508. while (ancestor && !ancestor->power.set_latency_tolerance)
  509. ancestor = ancestor->parent;
  510. break;
  511. default:
  512. ancestor = NULL;
  513. }
  514. if (ancestor)
  515. ret = dev_pm_qos_add_request(ancestor, req, type, value);
  516. if (ret < 0)
  517. req->dev = NULL;
  518. return ret;
  519. }
  520. EXPORT_SYMBOL_GPL(dev_pm_qos_add_ancestor_request);
  521. static void __dev_pm_qos_drop_user_request(struct device *dev,
  522. enum dev_pm_qos_req_type type)
  523. {
  524. struct dev_pm_qos_request *req = NULL;
  525. switch(type) {
  526. case DEV_PM_QOS_RESUME_LATENCY:
  527. req = dev->power.qos->resume_latency_req;
  528. dev->power.qos->resume_latency_req = NULL;
  529. break;
  530. case DEV_PM_QOS_LATENCY_TOLERANCE:
  531. req = dev->power.qos->latency_tolerance_req;
  532. dev->power.qos->latency_tolerance_req = NULL;
  533. break;
  534. case DEV_PM_QOS_FLAGS:
  535. req = dev->power.qos->flags_req;
  536. dev->power.qos->flags_req = NULL;
  537. break;
  538. }
  539. __dev_pm_qos_remove_request(req);
  540. kfree(req);
  541. }
  542. static void dev_pm_qos_drop_user_request(struct device *dev,
  543. enum dev_pm_qos_req_type type)
  544. {
  545. mutex_lock(&dev_pm_qos_mtx);
  546. __dev_pm_qos_drop_user_request(dev, type);
  547. mutex_unlock(&dev_pm_qos_mtx);
  548. }
  549. /**
  550. * dev_pm_qos_expose_latency_limit - Expose PM QoS latency limit to user space.
  551. * @dev: Device whose PM QoS latency limit is to be exposed to user space.
  552. * @value: Initial value of the latency limit.
  553. */
  554. int dev_pm_qos_expose_latency_limit(struct device *dev, s32 value)
  555. {
  556. struct dev_pm_qos_request *req;
  557. int ret;
  558. if (!device_is_registered(dev) || value < 0)
  559. return -EINVAL;
  560. req = kzalloc(sizeof(*req), GFP_KERNEL);
  561. if (!req)
  562. return -ENOMEM;
  563. ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_RESUME_LATENCY, value);
  564. if (ret < 0) {
  565. kfree(req);
  566. return ret;
  567. }
  568. mutex_lock(&dev_pm_qos_sysfs_mtx);
  569. mutex_lock(&dev_pm_qos_mtx);
  570. if (IS_ERR_OR_NULL(dev->power.qos))
  571. ret = -ENODEV;
  572. else if (dev->power.qos->resume_latency_req)
  573. ret = -EEXIST;
  574. if (ret < 0) {
  575. __dev_pm_qos_remove_request(req);
  576. kfree(req);
  577. mutex_unlock(&dev_pm_qos_mtx);
  578. goto out;
  579. }
  580. dev->power.qos->resume_latency_req = req;
  581. mutex_unlock(&dev_pm_qos_mtx);
  582. ret = pm_qos_sysfs_add_resume_latency(dev);
  583. if (ret)
  584. dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_RESUME_LATENCY);
  585. out:
  586. mutex_unlock(&dev_pm_qos_sysfs_mtx);
  587. return ret;
  588. }
  589. EXPORT_SYMBOL_GPL(dev_pm_qos_expose_latency_limit);
  590. static void __dev_pm_qos_hide_latency_limit(struct device *dev)
  591. {
  592. if (!IS_ERR_OR_NULL(dev->power.qos) && dev->power.qos->resume_latency_req)
  593. __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_RESUME_LATENCY);
  594. }
  595. /**
  596. * dev_pm_qos_hide_latency_limit - Hide PM QoS latency limit from user space.
  597. * @dev: Device whose PM QoS latency limit is to be hidden from user space.
  598. */
  599. void dev_pm_qos_hide_latency_limit(struct device *dev)
  600. {
  601. mutex_lock(&dev_pm_qos_sysfs_mtx);
  602. pm_qos_sysfs_remove_resume_latency(dev);
  603. mutex_lock(&dev_pm_qos_mtx);
  604. __dev_pm_qos_hide_latency_limit(dev);
  605. mutex_unlock(&dev_pm_qos_mtx);
  606. mutex_unlock(&dev_pm_qos_sysfs_mtx);
  607. }
  608. EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_limit);
  609. /**
  610. * dev_pm_qos_expose_flags - Expose PM QoS flags of a device to user space.
  611. * @dev: Device whose PM QoS flags are to be exposed to user space.
  612. * @val: Initial values of the flags.
  613. */
  614. int dev_pm_qos_expose_flags(struct device *dev, s32 val)
  615. {
  616. struct dev_pm_qos_request *req;
  617. int ret;
  618. if (!device_is_registered(dev))
  619. return -EINVAL;
  620. req = kzalloc(sizeof(*req), GFP_KERNEL);
  621. if (!req)
  622. return -ENOMEM;
  623. ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_FLAGS, val);
  624. if (ret < 0) {
  625. kfree(req);
  626. return ret;
  627. }
  628. pm_runtime_get_sync(dev);
  629. mutex_lock(&dev_pm_qos_sysfs_mtx);
  630. mutex_lock(&dev_pm_qos_mtx);
  631. if (IS_ERR_OR_NULL(dev->power.qos))
  632. ret = -ENODEV;
  633. else if (dev->power.qos->flags_req)
  634. ret = -EEXIST;
  635. if (ret < 0) {
  636. __dev_pm_qos_remove_request(req);
  637. kfree(req);
  638. mutex_unlock(&dev_pm_qos_mtx);
  639. goto out;
  640. }
  641. dev->power.qos->flags_req = req;
  642. mutex_unlock(&dev_pm_qos_mtx);
  643. ret = pm_qos_sysfs_add_flags(dev);
  644. if (ret)
  645. dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);
  646. out:
  647. mutex_unlock(&dev_pm_qos_sysfs_mtx);
  648. pm_runtime_put(dev);
  649. return ret;
  650. }
  651. EXPORT_SYMBOL_GPL(dev_pm_qos_expose_flags);
  652. static void __dev_pm_qos_hide_flags(struct device *dev)
  653. {
  654. if (!IS_ERR_OR_NULL(dev->power.qos) && dev->power.qos->flags_req)
  655. __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);
  656. }
  657. /**
  658. * dev_pm_qos_hide_flags - Hide PM QoS flags of a device from user space.
  659. * @dev: Device whose PM QoS flags are to be hidden from user space.
  660. */
  661. void dev_pm_qos_hide_flags(struct device *dev)
  662. {
  663. pm_runtime_get_sync(dev);
  664. mutex_lock(&dev_pm_qos_sysfs_mtx);
  665. pm_qos_sysfs_remove_flags(dev);
  666. mutex_lock(&dev_pm_qos_mtx);
  667. __dev_pm_qos_hide_flags(dev);
  668. mutex_unlock(&dev_pm_qos_mtx);
  669. mutex_unlock(&dev_pm_qos_sysfs_mtx);
  670. pm_runtime_put(dev);
  671. }
  672. EXPORT_SYMBOL_GPL(dev_pm_qos_hide_flags);
  673. /**
  674. * dev_pm_qos_update_flags - Update PM QoS flags request owned by user space.
  675. * @dev: Device to update the PM QoS flags request for.
  676. * @mask: Flags to set/clear.
  677. * @set: Whether to set or clear the flags (true means set).
  678. */
  679. int dev_pm_qos_update_flags(struct device *dev, s32 mask, bool set)
  680. {
  681. s32 value;
  682. int ret;
  683. pm_runtime_get_sync(dev);
  684. mutex_lock(&dev_pm_qos_mtx);
  685. if (IS_ERR_OR_NULL(dev->power.qos) || !dev->power.qos->flags_req) {
  686. ret = -EINVAL;
  687. goto out;
  688. }
  689. value = dev_pm_qos_requested_flags(dev);
  690. if (set)
  691. value |= mask;
  692. else
  693. value &= ~mask;
  694. ret = __dev_pm_qos_update_request(dev->power.qos->flags_req, value);
  695. out:
  696. mutex_unlock(&dev_pm_qos_mtx);
  697. pm_runtime_put(dev);
  698. return ret;
  699. }
  700. /**
  701. * dev_pm_qos_get_user_latency_tolerance - Get user space latency tolerance.
  702. * @dev: Device to obtain the user space latency tolerance for.
  703. */
  704. s32 dev_pm_qos_get_user_latency_tolerance(struct device *dev)
  705. {
  706. s32 ret;
  707. mutex_lock(&dev_pm_qos_mtx);
  708. ret = IS_ERR_OR_NULL(dev->power.qos)
  709. || !dev->power.qos->latency_tolerance_req ?
  710. PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT :
  711. dev->power.qos->latency_tolerance_req->data.pnode.prio;
  712. mutex_unlock(&dev_pm_qos_mtx);
  713. return ret;
  714. }
  715. /**
  716. * dev_pm_qos_update_user_latency_tolerance - Update user space latency tolerance.
  717. * @dev: Device to update the user space latency tolerance for.
  718. * @val: New user space latency tolerance for @dev (negative values disable).
  719. */
  720. int dev_pm_qos_update_user_latency_tolerance(struct device *dev, s32 val)
  721. {
  722. int ret;
  723. mutex_lock(&dev_pm_qos_mtx);
  724. if (IS_ERR_OR_NULL(dev->power.qos)
  725. || !dev->power.qos->latency_tolerance_req) {
  726. struct dev_pm_qos_request *req;
  727. if (val < 0) {
  728. ret = -EINVAL;
  729. goto out;
  730. }
  731. req = kzalloc(sizeof(*req), GFP_KERNEL);
  732. if (!req) {
  733. ret = -ENOMEM;
  734. goto out;
  735. }
  736. ret = __dev_pm_qos_add_request(dev, req, DEV_PM_QOS_LATENCY_TOLERANCE, val);
  737. if (ret < 0) {
  738. kfree(req);
  739. goto out;
  740. }
  741. dev->power.qos->latency_tolerance_req = req;
  742. } else {
  743. if (val < 0) {
  744. __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_LATENCY_TOLERANCE);
  745. ret = 0;
  746. } else {
  747. ret = __dev_pm_qos_update_request(dev->power.qos->latency_tolerance_req, val);
  748. }
  749. }
  750. out:
  751. mutex_unlock(&dev_pm_qos_mtx);
  752. return ret;
  753. }