latency.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * latency.c: Explicit system-wide latency-expectation infrastructure
  3. *
  4. * The purpose of this infrastructure is to allow device drivers to set
  5. * latency constraint they have and to collect and summarize these
  6. * expectations globally. The cummulated result can then be used by
  7. * power management and similar users to make decisions that have
  8. * tradoffs with a latency component.
  9. *
  10. * An example user of this are the x86 C-states; each higher C state saves
  11. * more power, but has a higher exit latency. For the idle loop power
  12. * code to make a good decision which C-state to use, information about
  13. * acceptable latencies is required.
  14. *
  15. * An example announcer of latency is an audio driver that knowns it
  16. * will get an interrupt when the hardware has 200 usec of samples
  17. * left in the DMA buffer; in that case the driver can set a latency
  18. * constraint of, say, 150 usec.
  19. *
  20. * Multiple drivers can each announce their maximum accepted latency,
  21. * to keep these appart, a string based identifier is used.
  22. *
  23. *
  24. * (C) Copyright 2006 Intel Corporation
  25. * Author: Arjan van de Ven <arjan@linux.intel.com>
  26. *
  27. * This program is free software; you can redistribute it and/or
  28. * modify it under the terms of the GNU General Public License
  29. * as published by the Free Software Foundation; version 2
  30. * of the License.
  31. */
  32. #include <linux/latency.h>
  33. #include <linux/list.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/slab.h>
  36. #include <linux/module.h>
  37. #include <linux/notifier.h>
  38. #include <asm/atomic.h>
  39. struct latency_info {
  40. struct list_head list;
  41. int usecs;
  42. char *identifier;
  43. };
  44. /*
  45. * locking rule: all modifications to current_max_latency and
  46. * latency_list need to be done while holding the latency_lock.
  47. * latency_lock needs to be taken _irqsave.
  48. */
  49. static atomic_t current_max_latency;
  50. static DEFINE_SPINLOCK(latency_lock);
  51. static LIST_HEAD(latency_list);
  52. static BLOCKING_NOTIFIER_HEAD(latency_notifier);
  53. /*
  54. * This function returns the maximum latency allowed, which
  55. * happens to be the minimum of all maximum latencies on the
  56. * list.
  57. */
  58. static int __find_max_latency(void)
  59. {
  60. int min = INFINITE_LATENCY;
  61. struct latency_info *info;
  62. list_for_each_entry(info, &latency_list, list) {
  63. if (info->usecs < min)
  64. min = info->usecs;
  65. }
  66. return min;
  67. }
  68. /**
  69. * set_acceptable_latency - sets the maximum latency acceptable
  70. * @identifier: string that identifies this driver
  71. * @usecs: maximum acceptable latency for this driver
  72. *
  73. * This function informs the kernel that this device(driver)
  74. * can accept at most usecs latency. This setting is used for
  75. * power management and similar tradeoffs.
  76. *
  77. * This function sleeps and can only be called from process
  78. * context.
  79. * Calling this function with an existing identifier is valid
  80. * and will cause the existing latency setting to be changed.
  81. */
  82. void set_acceptable_latency(char *identifier, int usecs)
  83. {
  84. struct latency_info *info, *iter;
  85. unsigned long flags;
  86. int found_old = 0;
  87. info = kzalloc(sizeof(struct latency_info), GFP_KERNEL);
  88. if (!info)
  89. return;
  90. info->usecs = usecs;
  91. info->identifier = kstrdup(identifier, GFP_KERNEL);
  92. if (!info->identifier)
  93. goto free_info;
  94. spin_lock_irqsave(&latency_lock, flags);
  95. list_for_each_entry(iter, &latency_list, list) {
  96. if (strcmp(iter->identifier, identifier)==0) {
  97. found_old = 1;
  98. iter->usecs = usecs;
  99. break;
  100. }
  101. }
  102. if (!found_old)
  103. list_add(&info->list, &latency_list);
  104. if (usecs < atomic_read(&current_max_latency))
  105. atomic_set(&current_max_latency, usecs);
  106. spin_unlock_irqrestore(&latency_lock, flags);
  107. blocking_notifier_call_chain(&latency_notifier,
  108. atomic_read(&current_max_latency), NULL);
  109. /*
  110. * if we inserted the new one, we're done; otherwise there was
  111. * an existing one so we need to free the redundant data
  112. */
  113. if (!found_old)
  114. return;
  115. kfree(info->identifier);
  116. free_info:
  117. kfree(info);
  118. }
  119. EXPORT_SYMBOL_GPL(set_acceptable_latency);
  120. /**
  121. * modify_acceptable_latency - changes the maximum latency acceptable
  122. * @identifier: string that identifies this driver
  123. * @usecs: maximum acceptable latency for this driver
  124. *
  125. * This function informs the kernel that this device(driver)
  126. * can accept at most usecs latency. This setting is used for
  127. * power management and similar tradeoffs.
  128. *
  129. * This function does not sleep and can be called in any context.
  130. * Trying to use a non-existing identifier silently gets ignored.
  131. *
  132. * Due to the atomic nature of this function, the modified latency
  133. * value will only be used for future decisions; past decisions
  134. * can still lead to longer latencies in the near future.
  135. */
  136. void modify_acceptable_latency(char *identifier, int usecs)
  137. {
  138. struct latency_info *iter;
  139. unsigned long flags;
  140. spin_lock_irqsave(&latency_lock, flags);
  141. list_for_each_entry(iter, &latency_list, list) {
  142. if (strcmp(iter->identifier, identifier) == 0) {
  143. iter->usecs = usecs;
  144. break;
  145. }
  146. }
  147. if (usecs < atomic_read(&current_max_latency))
  148. atomic_set(&current_max_latency, usecs);
  149. spin_unlock_irqrestore(&latency_lock, flags);
  150. }
  151. EXPORT_SYMBOL_GPL(modify_acceptable_latency);
  152. /**
  153. * remove_acceptable_latency - removes the maximum latency acceptable
  154. * @identifier: string that identifies this driver
  155. *
  156. * This function removes a previously set maximum latency setting
  157. * for the driver and frees up any resources associated with the
  158. * bookkeeping needed for this.
  159. *
  160. * This function does not sleep and can be called in any context.
  161. * Trying to use a non-existing identifier silently gets ignored.
  162. */
  163. void remove_acceptable_latency(char *identifier)
  164. {
  165. unsigned long flags;
  166. int newmax = 0;
  167. struct latency_info *iter, *temp;
  168. spin_lock_irqsave(&latency_lock, flags);
  169. list_for_each_entry_safe(iter, temp, &latency_list, list) {
  170. if (strcmp(iter->identifier, identifier) == 0) {
  171. list_del(&iter->list);
  172. newmax = iter->usecs;
  173. kfree(iter->identifier);
  174. kfree(iter);
  175. break;
  176. }
  177. }
  178. /* If we just deleted the system wide value, we need to
  179. * recalculate with a full search
  180. */
  181. if (newmax == atomic_read(&current_max_latency)) {
  182. newmax = __find_max_latency();
  183. atomic_set(&current_max_latency, newmax);
  184. }
  185. spin_unlock_irqrestore(&latency_lock, flags);
  186. }
  187. EXPORT_SYMBOL_GPL(remove_acceptable_latency);
  188. /**
  189. * system_latency_constraint - queries the system wide latency maximum
  190. *
  191. * This function returns the system wide maximum latency in
  192. * microseconds.
  193. *
  194. * This function does not sleep and can be called in any context.
  195. */
  196. int system_latency_constraint(void)
  197. {
  198. return atomic_read(&current_max_latency);
  199. }
  200. EXPORT_SYMBOL_GPL(system_latency_constraint);
  201. /**
  202. * synchronize_acceptable_latency - recalculates all latency decisions
  203. *
  204. * This function will cause a callback to various kernel pieces that
  205. * will make those pieces rethink their latency decisions. This implies
  206. * that if there are overlong latencies in hardware state already, those
  207. * latencies get taken right now. When this call completes no overlong
  208. * latency decisions should be active anymore.
  209. *
  210. * Typical usecase of this is after a modify_acceptable_latency() call,
  211. * which in itself is non-blocking and non-synchronizing.
  212. *
  213. * This function blocks and should not be called with locks held.
  214. */
  215. void synchronize_acceptable_latency(void)
  216. {
  217. blocking_notifier_call_chain(&latency_notifier,
  218. atomic_read(&current_max_latency), NULL);
  219. }
  220. EXPORT_SYMBOL_GPL(synchronize_acceptable_latency);
  221. /*
  222. * Latency notifier: this notifier gets called when a non-atomic new
  223. * latency value gets set. The expectation nof the caller of the
  224. * non-atomic set is that when the call returns, future latencies
  225. * are within bounds, so the functions on the notifier list are
  226. * expected to take the overlong latencies immediately, inside the
  227. * callback, and not make a overlong latency decision anymore.
  228. *
  229. * The callback gets called when the new latency value is made
  230. * active so system_latency_constraint() returns the new latency.
  231. */
  232. int register_latency_notifier(struct notifier_block * nb)
  233. {
  234. return blocking_notifier_chain_register(&latency_notifier, nb);
  235. }
  236. EXPORT_SYMBOL_GPL(register_latency_notifier);
  237. int unregister_latency_notifier(struct notifier_block * nb)
  238. {
  239. return blocking_notifier_chain_unregister(&latency_notifier, nb);
  240. }
  241. EXPORT_SYMBOL_GPL(unregister_latency_notifier);
  242. static __init int latency_init(void)
  243. {
  244. atomic_set(&current_max_latency, INFINITE_LATENCY);
  245. /*
  246. * we don't want by default to have longer latencies than 2 ticks,
  247. * since that would cause lost ticks
  248. */
  249. set_acceptable_latency("kernel", 2*1000000/HZ);
  250. return 0;
  251. }
  252. module_init(latency_init);