qos.c 23 KB

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