qos.c 23 KB

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