qos.c 26 KB

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