qos.c 24 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 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. #ifdef CONFIG_PM_RUNTIME
  522. static void __dev_pm_qos_drop_user_request(struct device *dev,
  523. enum dev_pm_qos_req_type type)
  524. {
  525. struct dev_pm_qos_request *req = NULL;
  526. switch(type) {
  527. case DEV_PM_QOS_RESUME_LATENCY:
  528. req = dev->power.qos->resume_latency_req;
  529. dev->power.qos->resume_latency_req = NULL;
  530. break;
  531. case DEV_PM_QOS_LATENCY_TOLERANCE:
  532. req = dev->power.qos->latency_tolerance_req;
  533. dev->power.qos->latency_tolerance_req = NULL;
  534. break;
  535. case DEV_PM_QOS_FLAGS:
  536. req = dev->power.qos->flags_req;
  537. dev->power.qos->flags_req = NULL;
  538. break;
  539. }
  540. __dev_pm_qos_remove_request(req);
  541. kfree(req);
  542. }
  543. static void dev_pm_qos_drop_user_request(struct device *dev,
  544. enum dev_pm_qos_req_type type)
  545. {
  546. mutex_lock(&dev_pm_qos_mtx);
  547. __dev_pm_qos_drop_user_request(dev, type);
  548. mutex_unlock(&dev_pm_qos_mtx);
  549. }
  550. /**
  551. * dev_pm_qos_expose_latency_limit - Expose PM QoS latency limit to user space.
  552. * @dev: Device whose PM QoS latency limit is to be exposed to user space.
  553. * @value: Initial value of the latency limit.
  554. */
  555. int dev_pm_qos_expose_latency_limit(struct device *dev, s32 value)
  556. {
  557. struct dev_pm_qos_request *req;
  558. int ret;
  559. if (!device_is_registered(dev) || value < 0)
  560. return -EINVAL;
  561. req = kzalloc(sizeof(*req), GFP_KERNEL);
  562. if (!req)
  563. return -ENOMEM;
  564. ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_RESUME_LATENCY, value);
  565. if (ret < 0) {
  566. kfree(req);
  567. return ret;
  568. }
  569. mutex_lock(&dev_pm_qos_sysfs_mtx);
  570. mutex_lock(&dev_pm_qos_mtx);
  571. if (IS_ERR_OR_NULL(dev->power.qos))
  572. ret = -ENODEV;
  573. else if (dev->power.qos->resume_latency_req)
  574. ret = -EEXIST;
  575. if (ret < 0) {
  576. __dev_pm_qos_remove_request(req);
  577. kfree(req);
  578. mutex_unlock(&dev_pm_qos_mtx);
  579. goto out;
  580. }
  581. dev->power.qos->resume_latency_req = req;
  582. mutex_unlock(&dev_pm_qos_mtx);
  583. ret = pm_qos_sysfs_add_resume_latency(dev);
  584. if (ret)
  585. dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_RESUME_LATENCY);
  586. out:
  587. mutex_unlock(&dev_pm_qos_sysfs_mtx);
  588. return ret;
  589. }
  590. EXPORT_SYMBOL_GPL(dev_pm_qos_expose_latency_limit);
  591. static void __dev_pm_qos_hide_latency_limit(struct device *dev)
  592. {
  593. if (!IS_ERR_OR_NULL(dev->power.qos) && dev->power.qos->resume_latency_req)
  594. __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_RESUME_LATENCY);
  595. }
  596. /**
  597. * dev_pm_qos_hide_latency_limit - Hide PM QoS latency limit from user space.
  598. * @dev: Device whose PM QoS latency limit is to be hidden from user space.
  599. */
  600. void dev_pm_qos_hide_latency_limit(struct device *dev)
  601. {
  602. mutex_lock(&dev_pm_qos_sysfs_mtx);
  603. pm_qos_sysfs_remove_resume_latency(dev);
  604. mutex_lock(&dev_pm_qos_mtx);
  605. __dev_pm_qos_hide_latency_limit(dev);
  606. mutex_unlock(&dev_pm_qos_mtx);
  607. mutex_unlock(&dev_pm_qos_sysfs_mtx);
  608. }
  609. EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_limit);
  610. /**
  611. * dev_pm_qos_expose_flags - Expose PM QoS flags of a device to user space.
  612. * @dev: Device whose PM QoS flags are to be exposed to user space.
  613. * @val: Initial values of the flags.
  614. */
  615. int dev_pm_qos_expose_flags(struct device *dev, s32 val)
  616. {
  617. struct dev_pm_qos_request *req;
  618. int ret;
  619. if (!device_is_registered(dev))
  620. return -EINVAL;
  621. req = kzalloc(sizeof(*req), GFP_KERNEL);
  622. if (!req)
  623. return -ENOMEM;
  624. ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_FLAGS, val);
  625. if (ret < 0) {
  626. kfree(req);
  627. return ret;
  628. }
  629. pm_runtime_get_sync(dev);
  630. mutex_lock(&dev_pm_qos_sysfs_mtx);
  631. mutex_lock(&dev_pm_qos_mtx);
  632. if (IS_ERR_OR_NULL(dev->power.qos))
  633. ret = -ENODEV;
  634. else if (dev->power.qos->flags_req)
  635. ret = -EEXIST;
  636. if (ret < 0) {
  637. __dev_pm_qos_remove_request(req);
  638. kfree(req);
  639. mutex_unlock(&dev_pm_qos_mtx);
  640. goto out;
  641. }
  642. dev->power.qos->flags_req = req;
  643. mutex_unlock(&dev_pm_qos_mtx);
  644. ret = pm_qos_sysfs_add_flags(dev);
  645. if (ret)
  646. dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);
  647. out:
  648. mutex_unlock(&dev_pm_qos_sysfs_mtx);
  649. pm_runtime_put(dev);
  650. return ret;
  651. }
  652. EXPORT_SYMBOL_GPL(dev_pm_qos_expose_flags);
  653. static void __dev_pm_qos_hide_flags(struct device *dev)
  654. {
  655. if (!IS_ERR_OR_NULL(dev->power.qos) && dev->power.qos->flags_req)
  656. __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);
  657. }
  658. /**
  659. * dev_pm_qos_hide_flags - Hide PM QoS flags of a device from user space.
  660. * @dev: Device whose PM QoS flags are to be hidden from user space.
  661. */
  662. void dev_pm_qos_hide_flags(struct device *dev)
  663. {
  664. pm_runtime_get_sync(dev);
  665. mutex_lock(&dev_pm_qos_sysfs_mtx);
  666. pm_qos_sysfs_remove_flags(dev);
  667. mutex_lock(&dev_pm_qos_mtx);
  668. __dev_pm_qos_hide_flags(dev);
  669. mutex_unlock(&dev_pm_qos_mtx);
  670. mutex_unlock(&dev_pm_qos_sysfs_mtx);
  671. pm_runtime_put(dev);
  672. }
  673. EXPORT_SYMBOL_GPL(dev_pm_qos_hide_flags);
  674. /**
  675. * dev_pm_qos_update_flags - Update PM QoS flags request owned by user space.
  676. * @dev: Device to update the PM QoS flags request for.
  677. * @mask: Flags to set/clear.
  678. * @set: Whether to set or clear the flags (true means set).
  679. */
  680. int dev_pm_qos_update_flags(struct device *dev, s32 mask, bool set)
  681. {
  682. s32 value;
  683. int ret;
  684. pm_runtime_get_sync(dev);
  685. mutex_lock(&dev_pm_qos_mtx);
  686. if (IS_ERR_OR_NULL(dev->power.qos) || !dev->power.qos->flags_req) {
  687. ret = -EINVAL;
  688. goto out;
  689. }
  690. value = dev_pm_qos_requested_flags(dev);
  691. if (set)
  692. value |= mask;
  693. else
  694. value &= ~mask;
  695. ret = __dev_pm_qos_update_request(dev->power.qos->flags_req, value);
  696. out:
  697. mutex_unlock(&dev_pm_qos_mtx);
  698. pm_runtime_put(dev);
  699. return ret;
  700. }
  701. /**
  702. * dev_pm_qos_get_user_latency_tolerance - Get user space latency tolerance.
  703. * @dev: Device to obtain the user space latency tolerance for.
  704. */
  705. s32 dev_pm_qos_get_user_latency_tolerance(struct device *dev)
  706. {
  707. s32 ret;
  708. mutex_lock(&dev_pm_qos_mtx);
  709. ret = IS_ERR_OR_NULL(dev->power.qos)
  710. || !dev->power.qos->latency_tolerance_req ?
  711. PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT :
  712. dev->power.qos->latency_tolerance_req->data.pnode.prio;
  713. mutex_unlock(&dev_pm_qos_mtx);
  714. return ret;
  715. }
  716. /**
  717. * dev_pm_qos_update_user_latency_tolerance - Update user space latency tolerance.
  718. * @dev: Device to update the user space latency tolerance for.
  719. * @val: New user space latency tolerance for @dev (negative values disable).
  720. */
  721. int dev_pm_qos_update_user_latency_tolerance(struct device *dev, s32 val)
  722. {
  723. int ret;
  724. mutex_lock(&dev_pm_qos_mtx);
  725. if (IS_ERR_OR_NULL(dev->power.qos)
  726. || !dev->power.qos->latency_tolerance_req) {
  727. struct dev_pm_qos_request *req;
  728. if (val < 0) {
  729. ret = -EINVAL;
  730. goto out;
  731. }
  732. req = kzalloc(sizeof(*req), GFP_KERNEL);
  733. if (!req) {
  734. ret = -ENOMEM;
  735. goto out;
  736. }
  737. ret = __dev_pm_qos_add_request(dev, req, DEV_PM_QOS_LATENCY_TOLERANCE, val);
  738. if (ret < 0) {
  739. kfree(req);
  740. goto out;
  741. }
  742. dev->power.qos->latency_tolerance_req = req;
  743. } else {
  744. if (val < 0) {
  745. __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_LATENCY_TOLERANCE);
  746. ret = 0;
  747. } else {
  748. ret = __dev_pm_qos_update_request(dev->power.qos->latency_tolerance_req, val);
  749. }
  750. }
  751. out:
  752. mutex_unlock(&dev_pm_qos_mtx);
  753. return ret;
  754. }
  755. #else /* !CONFIG_PM_RUNTIME */
  756. static void __dev_pm_qos_hide_latency_limit(struct device *dev) {}
  757. static void __dev_pm_qos_hide_flags(struct device *dev) {}
  758. #endif /* CONFIG_PM_RUNTIME */