latency.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 <linux/jiffies.h>
  39. #include <asm/atomic.h>
  40. struct latency_info {
  41. struct list_head list;
  42. int usecs;
  43. char *identifier;
  44. };
  45. /*
  46. * locking rule: all modifications to current_max_latency and
  47. * latency_list need to be done while holding the latency_lock.
  48. * latency_lock needs to be taken _irqsave.
  49. */
  50. static atomic_t current_max_latency;
  51. static DEFINE_SPINLOCK(latency_lock);
  52. static LIST_HEAD(latency_list);
  53. static BLOCKING_NOTIFIER_HEAD(latency_notifier);
  54. /*
  55. * This function returns the maximum latency allowed, which
  56. * happens to be the minimum of all maximum latencies on the
  57. * list.
  58. */
  59. static int __find_max_latency(void)
  60. {
  61. int min = INFINITE_LATENCY;
  62. struct latency_info *info;
  63. list_for_each_entry(info, &latency_list, list) {
  64. if (info->usecs < min)
  65. min = info->usecs;
  66. }
  67. return min;
  68. }
  69. /**
  70. * set_acceptable_latency - sets the maximum latency acceptable
  71. * @identifier: string that identifies this driver
  72. * @usecs: maximum acceptable latency for this driver
  73. *
  74. * This function informs the kernel that this device(driver)
  75. * can accept at most usecs latency. This setting is used for
  76. * power management and similar tradeoffs.
  77. *
  78. * This function sleeps and can only be called from process
  79. * context.
  80. * Calling this function with an existing identifier is valid
  81. * and will cause the existing latency setting to be changed.
  82. */
  83. void set_acceptable_latency(char *identifier, int usecs)
  84. {
  85. struct latency_info *info, *iter;
  86. unsigned long flags;
  87. int found_old = 0;
  88. info = kzalloc(sizeof(struct latency_info), GFP_KERNEL);
  89. if (!info)
  90. return;
  91. info->usecs = usecs;
  92. info->identifier = kstrdup(identifier, GFP_KERNEL);
  93. if (!info->identifier)
  94. goto free_info;
  95. spin_lock_irqsave(&latency_lock, flags);
  96. list_for_each_entry(iter, &latency_list, list) {
  97. if (strcmp(iter->identifier, identifier)==0) {
  98. found_old = 1;
  99. iter->usecs = usecs;
  100. break;
  101. }
  102. }
  103. if (!found_old)
  104. list_add(&info->list, &latency_list);
  105. if (usecs < atomic_read(&current_max_latency))
  106. atomic_set(&current_max_latency, usecs);
  107. spin_unlock_irqrestore(&latency_lock, flags);
  108. blocking_notifier_call_chain(&latency_notifier,
  109. atomic_read(&current_max_latency), NULL);
  110. /*
  111. * if we inserted the new one, we're done; otherwise there was
  112. * an existing one so we need to free the redundant data
  113. */
  114. if (!found_old)
  115. return;
  116. kfree(info->identifier);
  117. free_info:
  118. kfree(info);
  119. }
  120. EXPORT_SYMBOL_GPL(set_acceptable_latency);
  121. /**
  122. * modify_acceptable_latency - changes the maximum latency acceptable
  123. * @identifier: string that identifies this driver
  124. * @usecs: maximum acceptable latency for this driver
  125. *
  126. * This function informs the kernel that this device(driver)
  127. * can accept at most usecs latency. This setting is used for
  128. * power management and similar tradeoffs.
  129. *
  130. * This function does not sleep and can be called in any context.
  131. * Trying to use a non-existing identifier silently gets ignored.
  132. *
  133. * Due to the atomic nature of this function, the modified latency
  134. * value will only be used for future decisions; past decisions
  135. * can still lead to longer latencies in the near future.
  136. */
  137. void modify_acceptable_latency(char *identifier, int usecs)
  138. {
  139. struct latency_info *iter;
  140. unsigned long flags;
  141. spin_lock_irqsave(&latency_lock, flags);
  142. list_for_each_entry(iter, &latency_list, list) {
  143. if (strcmp(iter->identifier, identifier) == 0) {
  144. iter->usecs = usecs;
  145. break;
  146. }
  147. }
  148. if (usecs < atomic_read(&current_max_latency))
  149. atomic_set(&current_max_latency, usecs);
  150. spin_unlock_irqrestore(&latency_lock, flags);
  151. }
  152. EXPORT_SYMBOL_GPL(modify_acceptable_latency);
  153. /**
  154. * remove_acceptable_latency - removes the maximum latency acceptable
  155. * @identifier: string that identifies this driver
  156. *
  157. * This function removes a previously set maximum latency setting
  158. * for the driver and frees up any resources associated with the
  159. * bookkeeping needed for this.
  160. *
  161. * This function does not sleep and can be called in any context.
  162. * Trying to use a non-existing identifier silently gets ignored.
  163. */
  164. void remove_acceptable_latency(char *identifier)
  165. {
  166. unsigned long flags;
  167. int newmax = 0;
  168. struct latency_info *iter, *temp;
  169. spin_lock_irqsave(&latency_lock, flags);
  170. list_for_each_entry_safe(iter, temp, &latency_list, list) {
  171. if (strcmp(iter->identifier, identifier) == 0) {
  172. list_del(&iter->list);
  173. newmax = iter->usecs;
  174. kfree(iter->identifier);
  175. kfree(iter);
  176. break;
  177. }
  178. }
  179. /* If we just deleted the system wide value, we need to
  180. * recalculate with a full search
  181. */
  182. if (newmax == atomic_read(&current_max_latency)) {
  183. newmax = __find_max_latency();
  184. atomic_set(&current_max_latency, newmax);
  185. }
  186. spin_unlock_irqrestore(&latency_lock, flags);
  187. }
  188. EXPORT_SYMBOL_GPL(remove_acceptable_latency);
  189. /**
  190. * system_latency_constraint - queries the system wide latency maximum
  191. *
  192. * This function returns the system wide maximum latency in
  193. * microseconds.
  194. *
  195. * This function does not sleep and can be called in any context.
  196. */
  197. int system_latency_constraint(void)
  198. {
  199. return atomic_read(&current_max_latency);
  200. }
  201. EXPORT_SYMBOL_GPL(system_latency_constraint);
  202. /**
  203. * synchronize_acceptable_latency - recalculates all latency decisions
  204. *
  205. * This function will cause a callback to various kernel pieces that
  206. * will make those pieces rethink their latency decisions. This implies
  207. * that if there are overlong latencies in hardware state already, those
  208. * latencies get taken right now. When this call completes no overlong
  209. * latency decisions should be active anymore.
  210. *
  211. * Typical usecase of this is after a modify_acceptable_latency() call,
  212. * which in itself is non-blocking and non-synchronizing.
  213. *
  214. * This function blocks and should not be called with locks held.
  215. */
  216. void synchronize_acceptable_latency(void)
  217. {
  218. blocking_notifier_call_chain(&latency_notifier,
  219. atomic_read(&current_max_latency), NULL);
  220. }
  221. EXPORT_SYMBOL_GPL(synchronize_acceptable_latency);
  222. /*
  223. * Latency notifier: this notifier gets called when a non-atomic new
  224. * latency value gets set. The expectation nof the caller of the
  225. * non-atomic set is that when the call returns, future latencies
  226. * are within bounds, so the functions on the notifier list are
  227. * expected to take the overlong latencies immediately, inside the
  228. * callback, and not make a overlong latency decision anymore.
  229. *
  230. * The callback gets called when the new latency value is made
  231. * active so system_latency_constraint() returns the new latency.
  232. */
  233. int register_latency_notifier(struct notifier_block * nb)
  234. {
  235. return blocking_notifier_chain_register(&latency_notifier, nb);
  236. }
  237. EXPORT_SYMBOL_GPL(register_latency_notifier);
  238. int unregister_latency_notifier(struct notifier_block * nb)
  239. {
  240. return blocking_notifier_chain_unregister(&latency_notifier, nb);
  241. }
  242. EXPORT_SYMBOL_GPL(unregister_latency_notifier);
  243. static __init int latency_init(void)
  244. {
  245. atomic_set(&current_max_latency, INFINITE_LATENCY);
  246. /*
  247. * we don't want by default to have longer latencies than 2 ticks,
  248. * since that would cause lost ticks
  249. */
  250. set_acceptable_latency("kernel", 2*1000000/HZ);
  251. return 0;
  252. }
  253. module_init(latency_init);