qos.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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 "power.h"
  43. static DEFINE_MUTEX(dev_pm_qos_mtx);
  44. static BLOCKING_NOTIFIER_HEAD(dev_pm_notifiers);
  45. /**
  46. * __dev_pm_qos_flags - Check PM QoS flags for a given device.
  47. * @dev: Device to check the PM QoS flags for.
  48. * @mask: Flags to check against.
  49. *
  50. * This routine must be called with dev->power.lock held.
  51. */
  52. enum pm_qos_flags_status __dev_pm_qos_flags(struct device *dev, s32 mask)
  53. {
  54. struct dev_pm_qos *qos = dev->power.qos;
  55. struct pm_qos_flags *pqf;
  56. s32 val;
  57. if (!qos)
  58. return PM_QOS_FLAGS_UNDEFINED;
  59. pqf = &qos->flags;
  60. if (list_empty(&pqf->list))
  61. return PM_QOS_FLAGS_UNDEFINED;
  62. val = pqf->effective_flags & mask;
  63. if (val)
  64. return (val == mask) ? PM_QOS_FLAGS_ALL : PM_QOS_FLAGS_SOME;
  65. return PM_QOS_FLAGS_NONE;
  66. }
  67. /**
  68. * dev_pm_qos_flags - Check PM QoS flags for a given device (locked).
  69. * @dev: Device to check the PM QoS flags for.
  70. * @mask: Flags to check against.
  71. */
  72. enum pm_qos_flags_status dev_pm_qos_flags(struct device *dev, s32 mask)
  73. {
  74. unsigned long irqflags;
  75. enum pm_qos_flags_status ret;
  76. spin_lock_irqsave(&dev->power.lock, irqflags);
  77. ret = __dev_pm_qos_flags(dev, mask);
  78. spin_unlock_irqrestore(&dev->power.lock, irqflags);
  79. return ret;
  80. }
  81. /**
  82. * __dev_pm_qos_read_value - Get PM QoS constraint for a given device.
  83. * @dev: Device to get the PM QoS constraint value for.
  84. *
  85. * This routine must be called with dev->power.lock held.
  86. */
  87. s32 __dev_pm_qos_read_value(struct device *dev)
  88. {
  89. return dev->power.qos ? pm_qos_read_value(&dev->power.qos->latency) : 0;
  90. }
  91. /**
  92. * dev_pm_qos_read_value - Get PM QoS constraint for a given device (locked).
  93. * @dev: Device to get the PM QoS constraint value for.
  94. */
  95. s32 dev_pm_qos_read_value(struct device *dev)
  96. {
  97. unsigned long flags;
  98. s32 ret;
  99. spin_lock_irqsave(&dev->power.lock, flags);
  100. ret = __dev_pm_qos_read_value(dev);
  101. spin_unlock_irqrestore(&dev->power.lock, flags);
  102. return ret;
  103. }
  104. /**
  105. * apply_constraint - Add/modify/remove device PM QoS request.
  106. * @req: Constraint request to apply
  107. * @action: Action to perform (add/update/remove).
  108. * @value: Value to assign to the QoS request.
  109. *
  110. * Internal function to update the constraints list using the PM QoS core
  111. * code and if needed call the per-device and the global notification
  112. * callbacks
  113. */
  114. static int apply_constraint(struct dev_pm_qos_request *req,
  115. enum pm_qos_req_action action, s32 value)
  116. {
  117. struct dev_pm_qos *qos = req->dev->power.qos;
  118. int ret;
  119. switch(req->type) {
  120. case DEV_PM_QOS_LATENCY:
  121. ret = pm_qos_update_target(&qos->latency, &req->data.pnode,
  122. action, value);
  123. if (ret) {
  124. value = pm_qos_read_value(&qos->latency);
  125. blocking_notifier_call_chain(&dev_pm_notifiers,
  126. (unsigned long)value,
  127. req);
  128. }
  129. break;
  130. case DEV_PM_QOS_FLAGS:
  131. ret = pm_qos_update_flags(&qos->flags, &req->data.flr,
  132. action, value);
  133. break;
  134. default:
  135. ret = -EINVAL;
  136. }
  137. return ret;
  138. }
  139. /*
  140. * dev_pm_qos_constraints_allocate
  141. * @dev: device to allocate data for
  142. *
  143. * Called at the first call to add_request, for constraint data allocation
  144. * Must be called with the dev_pm_qos_mtx mutex held
  145. */
  146. static int dev_pm_qos_constraints_allocate(struct device *dev)
  147. {
  148. struct dev_pm_qos *qos;
  149. struct pm_qos_constraints *c;
  150. struct blocking_notifier_head *n;
  151. qos = kzalloc(sizeof(*qos), GFP_KERNEL);
  152. if (!qos)
  153. return -ENOMEM;
  154. n = kzalloc(sizeof(*n), GFP_KERNEL);
  155. if (!n) {
  156. kfree(qos);
  157. return -ENOMEM;
  158. }
  159. BLOCKING_INIT_NOTIFIER_HEAD(n);
  160. c = &qos->latency;
  161. plist_head_init(&c->list);
  162. c->target_value = PM_QOS_DEV_LAT_DEFAULT_VALUE;
  163. c->default_value = PM_QOS_DEV_LAT_DEFAULT_VALUE;
  164. c->type = PM_QOS_MIN;
  165. c->notifiers = n;
  166. INIT_LIST_HEAD(&qos->flags.list);
  167. spin_lock_irq(&dev->power.lock);
  168. dev->power.qos = qos;
  169. spin_unlock_irq(&dev->power.lock);
  170. return 0;
  171. }
  172. /**
  173. * dev_pm_qos_constraints_init - Initalize device's PM QoS constraints pointer.
  174. * @dev: target device
  175. *
  176. * Called from the device PM subsystem during device insertion under
  177. * device_pm_lock().
  178. */
  179. void dev_pm_qos_constraints_init(struct device *dev)
  180. {
  181. mutex_lock(&dev_pm_qos_mtx);
  182. dev->power.qos = NULL;
  183. dev->power.power_state = PMSG_ON;
  184. mutex_unlock(&dev_pm_qos_mtx);
  185. }
  186. /**
  187. * dev_pm_qos_constraints_destroy
  188. * @dev: target device
  189. *
  190. * Called from the device PM subsystem on device removal under device_pm_lock().
  191. */
  192. void dev_pm_qos_constraints_destroy(struct device *dev)
  193. {
  194. struct dev_pm_qos *qos;
  195. struct dev_pm_qos_request *req, *tmp;
  196. struct pm_qos_constraints *c;
  197. /*
  198. * If the device's PM QoS resume latency limit has been exposed to user
  199. * space, it has to be hidden at this point.
  200. */
  201. dev_pm_qos_hide_latency_limit(dev);
  202. mutex_lock(&dev_pm_qos_mtx);
  203. dev->power.power_state = PMSG_INVALID;
  204. qos = dev->power.qos;
  205. if (!qos)
  206. goto out;
  207. c = &qos->latency;
  208. /* Flush the constraints list for the device */
  209. plist_for_each_entry_safe(req, tmp, &c->list, data.pnode) {
  210. /*
  211. * Update constraints list and call the notification
  212. * callbacks if needed
  213. */
  214. apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
  215. memset(req, 0, sizeof(*req));
  216. }
  217. spin_lock_irq(&dev->power.lock);
  218. dev->power.qos = NULL;
  219. spin_unlock_irq(&dev->power.lock);
  220. kfree(c->notifiers);
  221. kfree(c);
  222. out:
  223. mutex_unlock(&dev_pm_qos_mtx);
  224. }
  225. /**
  226. * dev_pm_qos_add_request - inserts new qos request into the list
  227. * @dev: target device for the constraint
  228. * @req: pointer to a preallocated handle
  229. * @type: type of the request
  230. * @value: defines the qos request
  231. *
  232. * This function inserts a new entry in the device constraints list of
  233. * requested qos performance characteristics. It recomputes the aggregate
  234. * QoS expectations of parameters and initializes the dev_pm_qos_request
  235. * handle. Caller needs to save this handle for later use in updates and
  236. * removal.
  237. *
  238. * Returns 1 if the aggregated constraint value has changed,
  239. * 0 if the aggregated constraint value has not changed,
  240. * -EINVAL in case of wrong parameters, -ENOMEM if there's not enough memory
  241. * to allocate for data structures, -ENODEV if the device has just been removed
  242. * from the system.
  243. */
  244. int dev_pm_qos_add_request(struct device *dev, struct dev_pm_qos_request *req,
  245. enum dev_pm_qos_req_type type, s32 value)
  246. {
  247. int ret = 0;
  248. if (!dev || !req) /*guard against callers passing in null */
  249. return -EINVAL;
  250. if (WARN(dev_pm_qos_request_active(req),
  251. "%s() called for already added request\n", __func__))
  252. return -EINVAL;
  253. req->dev = dev;
  254. mutex_lock(&dev_pm_qos_mtx);
  255. if (!dev->power.qos) {
  256. if (dev->power.power_state.event == PM_EVENT_INVALID) {
  257. /* The device has been removed from the system. */
  258. req->dev = NULL;
  259. ret = -ENODEV;
  260. goto out;
  261. } else {
  262. /*
  263. * Allocate the constraints data on the first call to
  264. * add_request, i.e. only if the data is not already
  265. * allocated and if the device has not been removed.
  266. */
  267. ret = dev_pm_qos_constraints_allocate(dev);
  268. }
  269. }
  270. if (!ret) {
  271. req->type = type;
  272. ret = apply_constraint(req, PM_QOS_ADD_REQ, value);
  273. }
  274. out:
  275. mutex_unlock(&dev_pm_qos_mtx);
  276. return ret;
  277. }
  278. EXPORT_SYMBOL_GPL(dev_pm_qos_add_request);
  279. /**
  280. * dev_pm_qos_update_request - modifies an existing qos request
  281. * @req : handle to list element holding a dev_pm_qos request to use
  282. * @new_value: defines the qos request
  283. *
  284. * Updates an existing dev PM qos request along with updating the
  285. * target value.
  286. *
  287. * Attempts are made to make this code callable on hot code paths.
  288. *
  289. * Returns 1 if the aggregated constraint value has changed,
  290. * 0 if the aggregated constraint value has not changed,
  291. * -EINVAL in case of wrong parameters, -ENODEV if the device has been
  292. * removed from the system
  293. */
  294. int dev_pm_qos_update_request(struct dev_pm_qos_request *req,
  295. s32 new_value)
  296. {
  297. s32 curr_value;
  298. int ret = 0;
  299. if (!req) /*guard against callers passing in null */
  300. return -EINVAL;
  301. if (WARN(!dev_pm_qos_request_active(req),
  302. "%s() called for unknown object\n", __func__))
  303. return -EINVAL;
  304. mutex_lock(&dev_pm_qos_mtx);
  305. if (!req->dev->power.qos) {
  306. ret = -ENODEV;
  307. goto out;
  308. }
  309. switch(req->type) {
  310. case DEV_PM_QOS_LATENCY:
  311. curr_value = req->data.pnode.prio;
  312. break;
  313. case DEV_PM_QOS_FLAGS:
  314. curr_value = req->data.flr.flags;
  315. break;
  316. default:
  317. ret = -EINVAL;
  318. goto out;
  319. }
  320. if (curr_value != new_value)
  321. ret = apply_constraint(req, PM_QOS_UPDATE_REQ, new_value);
  322. out:
  323. mutex_unlock(&dev_pm_qos_mtx);
  324. return ret;
  325. }
  326. EXPORT_SYMBOL_GPL(dev_pm_qos_update_request);
  327. /**
  328. * dev_pm_qos_remove_request - modifies an existing qos request
  329. * @req: handle to request list element
  330. *
  331. * Will remove pm qos request from the list of constraints and
  332. * recompute the current target value. Call this on slow code paths.
  333. *
  334. * Returns 1 if the aggregated constraint value has changed,
  335. * 0 if the aggregated constraint value has not changed,
  336. * -EINVAL in case of wrong parameters, -ENODEV if the device has been
  337. * removed from the system
  338. */
  339. int dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
  340. {
  341. int ret = 0;
  342. if (!req) /*guard against callers passing in null */
  343. return -EINVAL;
  344. if (WARN(!dev_pm_qos_request_active(req),
  345. "%s() called for unknown object\n", __func__))
  346. return -EINVAL;
  347. mutex_lock(&dev_pm_qos_mtx);
  348. if (req->dev->power.qos) {
  349. ret = apply_constraint(req, PM_QOS_REMOVE_REQ,
  350. PM_QOS_DEFAULT_VALUE);
  351. memset(req, 0, sizeof(*req));
  352. } else {
  353. /* Return if the device has been removed */
  354. ret = -ENODEV;
  355. }
  356. mutex_unlock(&dev_pm_qos_mtx);
  357. return ret;
  358. }
  359. EXPORT_SYMBOL_GPL(dev_pm_qos_remove_request);
  360. /**
  361. * dev_pm_qos_add_notifier - sets notification entry for changes to target value
  362. * of per-device PM QoS constraints
  363. *
  364. * @dev: target device for the constraint
  365. * @notifier: notifier block managed by caller.
  366. *
  367. * Will register the notifier into a notification chain that gets called
  368. * upon changes to the target value for the device.
  369. *
  370. * If the device's constraints object doesn't exist when this routine is called,
  371. * it will be created (or error code will be returned if that fails).
  372. */
  373. int dev_pm_qos_add_notifier(struct device *dev, struct notifier_block *notifier)
  374. {
  375. int ret = 0;
  376. mutex_lock(&dev_pm_qos_mtx);
  377. if (!dev->power.qos)
  378. ret = dev->power.power_state.event != PM_EVENT_INVALID ?
  379. dev_pm_qos_constraints_allocate(dev) : -ENODEV;
  380. if (!ret)
  381. ret = blocking_notifier_chain_register(
  382. dev->power.qos->latency.notifiers, notifier);
  383. mutex_unlock(&dev_pm_qos_mtx);
  384. return ret;
  385. }
  386. EXPORT_SYMBOL_GPL(dev_pm_qos_add_notifier);
  387. /**
  388. * dev_pm_qos_remove_notifier - deletes notification for changes to target value
  389. * of per-device PM QoS constraints
  390. *
  391. * @dev: target device for the constraint
  392. * @notifier: notifier block to be removed.
  393. *
  394. * Will remove the notifier from the notification chain that gets called
  395. * upon changes to the target value.
  396. */
  397. int dev_pm_qos_remove_notifier(struct device *dev,
  398. struct notifier_block *notifier)
  399. {
  400. int retval = 0;
  401. mutex_lock(&dev_pm_qos_mtx);
  402. /* Silently return if the constraints object is not present. */
  403. if (dev->power.qos)
  404. retval = blocking_notifier_chain_unregister(
  405. dev->power.qos->latency.notifiers,
  406. notifier);
  407. mutex_unlock(&dev_pm_qos_mtx);
  408. return retval;
  409. }
  410. EXPORT_SYMBOL_GPL(dev_pm_qos_remove_notifier);
  411. /**
  412. * dev_pm_qos_add_global_notifier - sets notification entry for changes to
  413. * target value of the PM QoS constraints for any device
  414. *
  415. * @notifier: notifier block managed by caller.
  416. *
  417. * Will register the notifier into a notification chain that gets called
  418. * upon changes to the target value for any device.
  419. */
  420. int dev_pm_qos_add_global_notifier(struct notifier_block *notifier)
  421. {
  422. return blocking_notifier_chain_register(&dev_pm_notifiers, notifier);
  423. }
  424. EXPORT_SYMBOL_GPL(dev_pm_qos_add_global_notifier);
  425. /**
  426. * dev_pm_qos_remove_global_notifier - deletes notification for changes to
  427. * target value of PM QoS constraints for any device
  428. *
  429. * @notifier: notifier block to be removed.
  430. *
  431. * Will remove the notifier from the notification chain that gets called
  432. * upon changes to the target value for any device.
  433. */
  434. int dev_pm_qos_remove_global_notifier(struct notifier_block *notifier)
  435. {
  436. return blocking_notifier_chain_unregister(&dev_pm_notifiers, notifier);
  437. }
  438. EXPORT_SYMBOL_GPL(dev_pm_qos_remove_global_notifier);
  439. /**
  440. * dev_pm_qos_add_ancestor_request - Add PM QoS request for device's ancestor.
  441. * @dev: Device whose ancestor to add the request for.
  442. * @req: Pointer to the preallocated handle.
  443. * @value: Constraint latency value.
  444. */
  445. int dev_pm_qos_add_ancestor_request(struct device *dev,
  446. struct dev_pm_qos_request *req, s32 value)
  447. {
  448. struct device *ancestor = dev->parent;
  449. int error = -ENODEV;
  450. while (ancestor && !ancestor->power.ignore_children)
  451. ancestor = ancestor->parent;
  452. if (ancestor)
  453. error = dev_pm_qos_add_request(ancestor, req,
  454. DEV_PM_QOS_LATENCY, value);
  455. if (error)
  456. req->dev = NULL;
  457. return error;
  458. }
  459. EXPORT_SYMBOL_GPL(dev_pm_qos_add_ancestor_request);
  460. #ifdef CONFIG_PM_RUNTIME
  461. static void __dev_pm_qos_drop_user_request(struct device *dev)
  462. {
  463. dev_pm_qos_remove_request(dev->power.pq_req);
  464. dev->power.pq_req = NULL;
  465. }
  466. /**
  467. * dev_pm_qos_expose_latency_limit - Expose PM QoS latency limit to user space.
  468. * @dev: Device whose PM QoS latency limit is to be exposed to user space.
  469. * @value: Initial value of the latency limit.
  470. */
  471. int dev_pm_qos_expose_latency_limit(struct device *dev, s32 value)
  472. {
  473. struct dev_pm_qos_request *req;
  474. int ret;
  475. if (!device_is_registered(dev) || value < 0)
  476. return -EINVAL;
  477. if (dev->power.pq_req)
  478. return -EEXIST;
  479. req = kzalloc(sizeof(*req), GFP_KERNEL);
  480. if (!req)
  481. return -ENOMEM;
  482. ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_LATENCY, value);
  483. if (ret < 0)
  484. return ret;
  485. dev->power.pq_req = req;
  486. ret = pm_qos_sysfs_add(dev);
  487. if (ret)
  488. __dev_pm_qos_drop_user_request(dev);
  489. return ret;
  490. }
  491. EXPORT_SYMBOL_GPL(dev_pm_qos_expose_latency_limit);
  492. /**
  493. * dev_pm_qos_hide_latency_limit - Hide PM QoS latency limit from user space.
  494. * @dev: Device whose PM QoS latency limit is to be hidden from user space.
  495. */
  496. void dev_pm_qos_hide_latency_limit(struct device *dev)
  497. {
  498. if (dev->power.pq_req) {
  499. pm_qos_sysfs_remove(dev);
  500. __dev_pm_qos_drop_user_request(dev);
  501. }
  502. }
  503. EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_limit);
  504. #endif /* CONFIG_PM_RUNTIME */