cpufreq_kernel.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Linux 2.6.32 and later Kernel module for VMware MVP Hypervisor Support
  3. *
  4. * Copyright (C) 2010-2013 VMware, Inc. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; see the file COPYING. If not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. #line 5
  20. /**
  21. * @file
  22. *
  23. * @brief MVP host kernel cpufreq related
  24. *
  25. * Track CPU frequency changes.
  26. */
  27. #include <linux/module.h>
  28. #include <linux/notifier.h>
  29. #include <linux/cpufreq.h>
  30. #include <linux/smp.h>
  31. #include "mvp.h"
  32. #include "cpufreq_kernel.h"
  33. #include "mvpkm_kernel.h"
  34. #include "mvp_timer.h"
  35. /**
  36. * @brief Return current CPU frequency
  37. * @param cpu CPU number
  38. * @return CPU frequency in kHz, might be 0 (cpufreq)
  39. *
  40. * When CPU_FREQ is not available, it uses hardcoded frequencies.
  41. */
  42. static uint32
  43. GetCpuFrequency(unsigned int cpu)
  44. {
  45. unsigned int counterKHZ;
  46. #ifdef CONFIG_CPU_FREQ
  47. counterKHZ = cpufreq_quick_get(cpu);
  48. #elif defined(MVP_HOST_BOARD_ve)
  49. /**
  50. * @knownjira{MVP-143}
  51. * We're only using this under the simulator, and it's almost non
  52. * perceptible to provide a fixed TSC frequency as the instructions /
  53. * second executed widely varies depending over time. While we resolve
  54. * this issue we can use the BogoMIPS reported at boot for now.
  55. */
  56. KNOWN_BUG(MVP-143);
  57. counterKHZ = 125e3;
  58. printk_once(KERN_INFO "mvpkm: CPU_FREQ not available, " \
  59. "forcing TSC to %d kHz\n", counterKHZ);
  60. #elif defined(MVP_HOST_BOARD_panda)
  61. counterKHZ = 1e6;
  62. #else
  63. /*
  64. * If the kernel can't tell us and we have no further host knowledge,
  65. * time to die.
  66. */
  67. #error "host TSC frequency unknown."
  68. #endif
  69. return counterKHZ;
  70. }
  71. /**
  72. * @brief Compute TSC to RATE64 ratio
  73. * @param cpuFreq TSC frequency in kHz
  74. * @param[out] ttr tscToRate64 pointer
  75. */
  76. static void
  77. TscToRate64(uint32 cpuFreq,
  78. struct TscToRate64Cb *ttr)
  79. {
  80. uint32 shift;
  81. uint64 mult;
  82. /*
  83. * A little bit of math!
  84. *
  85. * We need here to convert the TSC value to our RATE64 timebase.
  86. *
  87. * In other words:
  88. *
  89. * tsc * MVP_TIMER_RATE64
  90. * rate64 = ----------------------
  91. * cpuFreq
  92. *
  93. * But we are limited by CPU performance (does not divide easily), CPU
  94. * instruction set, and CPU register file width. To fit performance
  95. * requirement, the math becomes:
  96. *
  97. * rate64 = (cpuFreq * mult) >> shift
  98. *
  99. * To respect instruction set, both cpuFreq and mult must be 32-bit
  100. * numbers. Thus (cpuFreq * mult) will be a 64-bit number.
  101. *
  102. *
  103. * Log2 rate64 = Log2 cpuFreq + Log2 mult - shift
  104. *
  105. * shift = Log2 mult + Log2 cpuFreq - Log2 rate64
  106. *
  107. * && Log2 mult < 32
  108. *
  109. * => shift < 32 + Log2 cpuFreq - Log2 rate64
  110. *
  111. * rate64 << shift
  112. * => mult = ---------------
  113. * cpuFreq
  114. *
  115. * (rate64 << shift) must be a 64-bit number:
  116. *
  117. * Log2 rate64 + shift < 64
  118. *
  119. * => shift < 64 - Log2 rate64
  120. *
  121. * While cpuFreq is lower than 2^32 Hz, we have:
  122. *
  123. * shift < 32 + Log2 cpuFreq - Log2 rate64 < 64 - Log2 rate64
  124. *
  125. * As (31 - CLZ32 x) <= Log2 x < (32 - CLZ32 x):
  126. *
  127. * 31 - CLZ32 cpuFreq <= Log2 cpuFreq &&
  128. *
  129. * CLZ32 rate64 - 32 < - Log2 rate64
  130. *
  131. * 31 + CLZ32 rate64 - CLZ32 cpuFreq <
  132. *
  133. * 32 + Log2 cpuFreq - Log2 rate64
  134. *
  135. * As we want shift to be as great as possible:
  136. *
  137. * => shift = 31 + CLZ32 rate64 - CLZ32 cpuFreq
  138. *
  139. * rate64 << shift
  140. * && mult = ---------------
  141. * cpuFreq
  142. *
  143. *
  144. */
  145. /* cpuFreq comes in kHz */
  146. cpuFreq *= 1000;
  147. /* CLZ(MVP_TIMER_RATE64) is optimized by compiler in a constant */
  148. shift = 31 + CLZ(MVP_TIMER_RATE64) - CLZ(cpuFreq);
  149. mult = MVP_TIMER_RATE64;
  150. mult <<= shift;
  151. do_div(mult, cpuFreq);
  152. /* verify Log2 mult < 32 */
  153. ASSERT(mult < (1ULL<<32));
  154. /* update global variables */
  155. ttr->mult = mult;
  156. ttr->shift = shift;
  157. }
  158. /**
  159. * @brief Compute a new TSC to rate64 ratio if CPU frequency changed
  160. * @param[in,out] freq Pointer to previous CPU frequency in kHz
  161. * @param[in,out] ttr Pointer to ratio values, set on change
  162. * @return 1 if ratio has changed, else 0
  163. */
  164. int
  165. CpuFreqUpdate(unsigned int *freq,
  166. struct TscToRate64Cb *ttr)
  167. {
  168. unsigned int cur = GetCpuFrequency(smp_processor_id());
  169. int ret = (cur != *freq);
  170. if (ret) {
  171. if (cur) {
  172. TscToRate64(cur, ttr);
  173. } else {
  174. /*
  175. * Note that cpufreq_quick_get(cpu) can return 0 while
  176. * cpufreq is not yet ready on that core. This will
  177. * make monitor run with a degraded time for few ms.
  178. */
  179. ttr->mult = 1;
  180. ttr->shift = 64;
  181. }
  182. *freq = cur;
  183. }
  184. return ret;
  185. }
  186. /**
  187. * @brief Nop function
  188. * @param info Ignored
  189. */
  190. static void
  191. CpuFreqNop(void *info)
  192. {
  193. }
  194. /**
  195. * @brief Handle cpufreq transition notifications.
  196. * @param nb Notifier block
  197. * @param val Notified event
  198. * @param data Linux cpufreq_freqs info
  199. * @return NOTIFY_OK
  200. *
  201. * @note A frequency change can fail in which case PRECHANGE and POSTCHANGE
  202. * will not be paired and you get any number of PRECHANGE and maybe never a
  203. * POSTCHANGE (i.e. there is not enough battery voltage available to support a
  204. * high frequency).
  205. * @note This is called once per cpu core that is changing but not always on
  206. * the core that is changing.
  207. */
  208. static int
  209. CpuFreqNotifier(struct notifier_block *nb,
  210. unsigned long val,
  211. void *data)
  212. {
  213. struct cpufreq_freqs *freq = data;
  214. /*
  215. * Call CpuFreqNop() on the correct CPU core to force any currently
  216. * running vCPU's to worldswitch back to the host and update TSC to
  217. * rate64 ratio on next execution.
  218. */
  219. if (freq->old != freq->new &&
  220. val == CPUFREQ_POSTCHANGE &&
  221. cpumask_test_cpu(freq->cpu, &inMonitor))
  222. smp_call_function_single(freq->cpu, CpuFreqNop, NULL, false);
  223. return NOTIFY_OK;
  224. }
  225. /**
  226. * @brief Notifier block for cpufreq transitions
  227. */
  228. static struct notifier_block cpuFreqNotifierBlock = {
  229. .notifier_call = CpuFreqNotifier
  230. };
  231. /**
  232. * @brief Initialize TSC ratio and register cpufreq transitions.
  233. */
  234. void
  235. CpuFreq_Init(void)
  236. {
  237. int ret;
  238. /* register callback on frequency change */
  239. ret = cpufreq_register_notifier(&cpuFreqNotifierBlock,
  240. CPUFREQ_TRANSITION_NOTIFIER);
  241. FATAL_IF(ret < 0);
  242. }
  243. /**
  244. * @brief Exit cpufreq, unregister cpufreq transitions
  245. */
  246. void
  247. CpuFreq_Exit(void)
  248. {
  249. cpufreq_unregister_notifier(&cpuFreqNotifierBlock,
  250. CPUFREQ_TRANSITION_NOTIFIER);
  251. }