cbe_cpufreq.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * cpufreq driver for the cell processor
  3. *
  4. * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
  5. *
  6. * Author: Christian Krafft <krafft@de.ibm.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2, or (at your option)
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/cpufreq.h>
  23. #include <linux/timer.h>
  24. #include <asm/hw_irq.h>
  25. #include <asm/io.h>
  26. #include <asm/machdep.h>
  27. #include <asm/processor.h>
  28. #include <asm/prom.h>
  29. #include <asm/time.h>
  30. #include <asm/pmi.h>
  31. #include <asm/of_platform.h>
  32. #include "cbe_regs.h"
  33. static DEFINE_MUTEX(cbe_switch_mutex);
  34. /* the CBE supports an 8 step frequency scaling */
  35. static struct cpufreq_frequency_table cbe_freqs[] = {
  36. {1, 0},
  37. {2, 0},
  38. {3, 0},
  39. {4, 0},
  40. {5, 0},
  41. {6, 0},
  42. {8, 0},
  43. {10, 0},
  44. {0, CPUFREQ_TABLE_END},
  45. };
  46. /* to write to MIC register */
  47. static u64 MIC_Slow_Fast_Timer_table[] = {
  48. [0 ... 7] = 0x007fc00000000000ull,
  49. };
  50. /* more values for the MIC */
  51. static u64 MIC_Slow_Next_Timer_table[] = {
  52. 0x0000240000000000ull,
  53. 0x0000268000000000ull,
  54. 0x000029C000000000ull,
  55. 0x00002D0000000000ull,
  56. 0x0000300000000000ull,
  57. 0x0000334000000000ull,
  58. 0x000039C000000000ull,
  59. 0x00003FC000000000ull,
  60. };
  61. static unsigned int pmi_frequency_limit = 0;
  62. /*
  63. * hardware specific functions
  64. */
  65. static struct of_device *pmi_dev;
  66. #ifdef CONFIG_PPC_PMI
  67. static int set_pmode_pmi(int cpu, unsigned int pmode)
  68. {
  69. int ret;
  70. pmi_message_t pmi_msg;
  71. #ifdef DEBUG
  72. u64 time;
  73. #endif
  74. pmi_msg.type = PMI_TYPE_FREQ_CHANGE;
  75. pmi_msg.data1 = cbe_cpu_to_node(cpu);
  76. pmi_msg.data2 = pmode;
  77. #ifdef DEBUG
  78. time = (u64) get_cycles();
  79. #endif
  80. pmi_send_message(pmi_dev, pmi_msg);
  81. ret = pmi_msg.data2;
  82. pr_debug("PMI returned slow mode %d\n", ret);
  83. #ifdef DEBUG
  84. time = (u64) get_cycles() - time; /* actual cycles (not cpu cycles!) */
  85. time = 1000000000 * time / CLOCK_TICK_RATE; /* time in ns (10^-9) */
  86. pr_debug("had to wait %lu ns for a transition\n", time);
  87. #endif
  88. return ret;
  89. }
  90. #endif
  91. static int get_pmode(int cpu)
  92. {
  93. int ret;
  94. struct cbe_pmd_regs __iomem *pmd_regs;
  95. pmd_regs = cbe_get_cpu_pmd_regs(cpu);
  96. ret = in_be64(&pmd_regs->pmsr) & 0x07;
  97. return ret;
  98. }
  99. static int set_pmode_reg(int cpu, unsigned int pmode)
  100. {
  101. struct cbe_pmd_regs __iomem *pmd_regs;
  102. struct cbe_mic_tm_regs __iomem *mic_tm_regs;
  103. u64 flags;
  104. u64 value;
  105. local_irq_save(flags);
  106. mic_tm_regs = cbe_get_cpu_mic_tm_regs(cpu);
  107. pmd_regs = cbe_get_cpu_pmd_regs(cpu);
  108. pr_debug("pm register is mapped at %p\n", &pmd_regs->pmcr);
  109. pr_debug("mic register is mapped at %p\n", &mic_tm_regs->slow_fast_timer_0);
  110. out_be64(&mic_tm_regs->slow_fast_timer_0, MIC_Slow_Fast_Timer_table[pmode]);
  111. out_be64(&mic_tm_regs->slow_fast_timer_1, MIC_Slow_Fast_Timer_table[pmode]);
  112. out_be64(&mic_tm_regs->slow_next_timer_0, MIC_Slow_Next_Timer_table[pmode]);
  113. out_be64(&mic_tm_regs->slow_next_timer_1, MIC_Slow_Next_Timer_table[pmode]);
  114. value = in_be64(&pmd_regs->pmcr);
  115. /* set bits to zero */
  116. value &= 0xFFFFFFFFFFFFFFF8ull;
  117. /* set bits to next pmode */
  118. value |= pmode;
  119. out_be64(&pmd_regs->pmcr, value);
  120. /* wait until new pmode appears in status register */
  121. value = in_be64(&pmd_regs->pmsr) & 0x07;
  122. while(value != pmode) {
  123. cpu_relax();
  124. value = in_be64(&pmd_regs->pmsr) & 0x07;
  125. }
  126. local_irq_restore(flags);
  127. return 0;
  128. }
  129. static int set_pmode(int cpu, unsigned int slow_mode) {
  130. #ifdef CONFIG_PPC_PMI
  131. if (pmi_dev)
  132. return set_pmode_pmi(cpu, slow_mode);
  133. else
  134. #endif
  135. return set_pmode_reg(cpu, slow_mode);
  136. }
  137. static void cbe_cpufreq_handle_pmi(struct of_device *dev, pmi_message_t pmi_msg)
  138. {
  139. u8 cpu;
  140. u8 cbe_pmode_new;
  141. BUG_ON(pmi_msg.type != PMI_TYPE_FREQ_CHANGE);
  142. cpu = cbe_node_to_cpu(pmi_msg.data1);
  143. cbe_pmode_new = pmi_msg.data2;
  144. pmi_frequency_limit = cbe_freqs[cbe_pmode_new].frequency;
  145. pr_debug("cbe_handle_pmi: max freq=%d\n", pmi_frequency_limit);
  146. }
  147. static int pmi_notifier(struct notifier_block *nb,
  148. unsigned long event, void *data)
  149. {
  150. struct cpufreq_policy *policy = data;
  151. if (event != CPUFREQ_INCOMPATIBLE)
  152. return 0;
  153. cpufreq_verify_within_limits(policy, 0, pmi_frequency_limit);
  154. return 0;
  155. }
  156. static struct notifier_block pmi_notifier_block = {
  157. .notifier_call = pmi_notifier,
  158. };
  159. static struct pmi_handler cbe_pmi_handler = {
  160. .type = PMI_TYPE_FREQ_CHANGE,
  161. .handle_pmi_message = cbe_cpufreq_handle_pmi,
  162. };
  163. /*
  164. * cpufreq functions
  165. */
  166. static int cbe_cpufreq_cpu_init(struct cpufreq_policy *policy)
  167. {
  168. const u32 *max_freqp;
  169. u32 max_freq;
  170. int i, cur_pmode;
  171. struct device_node *cpu;
  172. cpu = of_get_cpu_node(policy->cpu, NULL);
  173. if (!cpu)
  174. return -ENODEV;
  175. pr_debug("init cpufreq on CPU %d\n", policy->cpu);
  176. max_freqp = of_get_property(cpu, "clock-frequency", NULL);
  177. if (!max_freqp)
  178. return -EINVAL;
  179. /* we need the freq in kHz */
  180. max_freq = *max_freqp / 1000;
  181. pr_debug("max clock-frequency is at %u kHz\n", max_freq);
  182. pr_debug("initializing frequency table\n");
  183. /* initialize frequency table */
  184. for (i=0; cbe_freqs[i].frequency!=CPUFREQ_TABLE_END; i++) {
  185. cbe_freqs[i].frequency = max_freq / cbe_freqs[i].index;
  186. pr_debug("%d: %d\n", i, cbe_freqs[i].frequency);
  187. }
  188. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  189. /* if DEBUG is enabled set_pmode() measures the correct latency of a transition */
  190. policy->cpuinfo.transition_latency = 25000;
  191. cur_pmode = get_pmode(policy->cpu);
  192. pr_debug("current pmode is at %d\n",cur_pmode);
  193. policy->cur = cbe_freqs[cur_pmode].frequency;
  194. #ifdef CONFIG_SMP
  195. policy->cpus = cpu_sibling_map[policy->cpu];
  196. #endif
  197. cpufreq_frequency_table_get_attr(cbe_freqs, policy->cpu);
  198. if (pmi_dev) {
  199. /* frequency might get limited later, initialize limit with max_freq */
  200. pmi_frequency_limit = max_freq;
  201. cpufreq_register_notifier(&pmi_notifier_block, CPUFREQ_POLICY_NOTIFIER);
  202. }
  203. /* this ensures that policy->cpuinfo_min and policy->cpuinfo_max are set correctly */
  204. return cpufreq_frequency_table_cpuinfo(policy, cbe_freqs);
  205. }
  206. static int cbe_cpufreq_cpu_exit(struct cpufreq_policy *policy)
  207. {
  208. if (pmi_dev)
  209. cpufreq_unregister_notifier(&pmi_notifier_block, CPUFREQ_POLICY_NOTIFIER);
  210. cpufreq_frequency_table_put_attr(policy->cpu);
  211. return 0;
  212. }
  213. static int cbe_cpufreq_verify(struct cpufreq_policy *policy)
  214. {
  215. return cpufreq_frequency_table_verify(policy, cbe_freqs);
  216. }
  217. static int cbe_cpufreq_target(struct cpufreq_policy *policy, unsigned int target_freq,
  218. unsigned int relation)
  219. {
  220. int rc;
  221. struct cpufreq_freqs freqs;
  222. int cbe_pmode_new;
  223. cpufreq_frequency_table_target(policy,
  224. cbe_freqs,
  225. target_freq,
  226. relation,
  227. &cbe_pmode_new);
  228. freqs.old = policy->cur;
  229. freqs.new = cbe_freqs[cbe_pmode_new].frequency;
  230. freqs.cpu = policy->cpu;
  231. mutex_lock(&cbe_switch_mutex);
  232. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  233. pr_debug("setting frequency for cpu %d to %d kHz, 1/%d of max frequency\n",
  234. policy->cpu,
  235. cbe_freqs[cbe_pmode_new].frequency,
  236. cbe_freqs[cbe_pmode_new].index);
  237. rc = set_pmode(policy->cpu, cbe_pmode_new);
  238. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  239. mutex_unlock(&cbe_switch_mutex);
  240. return rc;
  241. }
  242. static struct cpufreq_driver cbe_cpufreq_driver = {
  243. .verify = cbe_cpufreq_verify,
  244. .target = cbe_cpufreq_target,
  245. .init = cbe_cpufreq_cpu_init,
  246. .exit = cbe_cpufreq_cpu_exit,
  247. .name = "cbe-cpufreq",
  248. .owner = THIS_MODULE,
  249. .flags = CPUFREQ_CONST_LOOPS,
  250. };
  251. /*
  252. * module init and destoy
  253. */
  254. static int __init cbe_cpufreq_init(void)
  255. {
  256. #ifdef CONFIG_PPC_PMI
  257. struct device_node *np;
  258. #endif
  259. if (!machine_is(cell))
  260. return -ENODEV;
  261. #ifdef CONFIG_PPC_PMI
  262. np = of_find_node_by_type(NULL, "ibm,pmi");
  263. pmi_dev = of_find_device_by_node(np);
  264. if (pmi_dev)
  265. pmi_register_handler(pmi_dev, &cbe_pmi_handler);
  266. #endif
  267. return cpufreq_register_driver(&cbe_cpufreq_driver);
  268. }
  269. static void __exit cbe_cpufreq_exit(void)
  270. {
  271. #ifdef CONFIG_PPC_PMI
  272. if (pmi_dev)
  273. pmi_unregister_handler(pmi_dev, &cbe_pmi_handler);
  274. #endif
  275. cpufreq_unregister_driver(&cbe_cpufreq_driver);
  276. }
  277. module_init(cbe_cpufreq_init);
  278. module_exit(cbe_cpufreq_exit);
  279. MODULE_LICENSE("GPL");
  280. MODULE_AUTHOR("Christian Krafft <krafft@de.ibm.com>");