qos.c 26 KB

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