qos.c 25 KB

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