cbe_cpufreq.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. /*
  62. * hardware specific functions
  63. */
  64. static struct of_device *pmi_dev;
  65. static int set_pmode_pmi(int cpu, unsigned int pmode)
  66. {
  67. int ret;
  68. pmi_message_t pmi_msg;
  69. #ifdef DEBUG
  70. u64 time;
  71. #endif
  72. pmi_msg.type = PMI_TYPE_FREQ_CHANGE;
  73. pmi_msg.data1 = cbe_cpu_to_node(cpu);
  74. pmi_msg.data2 = pmode;
  75. #ifdef DEBUG
  76. time = (u64) get_cycles();
  77. #endif
  78. pmi_send_message(pmi_dev, pmi_msg);
  79. ret = pmi_msg.data2;
  80. pr_debug("PMI returned slow mode %d\n", ret);
  81. #ifdef DEBUG
  82. time = (u64) get_cycles() - time; /* actual cycles (not cpu cycles!) */
  83. time = 1000000000 * time / CLOCK_TICK_RATE; /* time in ns (10^-9) */
  84. pr_debug("had to wait %lu ns for a transition\n", time);
  85. #endif
  86. return ret;
  87. }
  88. static int get_pmode(int cpu)
  89. {
  90. int ret;
  91. struct cbe_pmd_regs __iomem *pmd_regs;
  92. pmd_regs = cbe_get_cpu_pmd_regs(cpu);
  93. ret = in_be64(&pmd_regs->pmsr) & 0x07;
  94. return ret;
  95. }
  96. static int set_pmode_reg(int cpu, unsigned int pmode)
  97. {
  98. struct cbe_pmd_regs __iomem *pmd_regs;
  99. struct cbe_mic_tm_regs __iomem *mic_tm_regs;
  100. u64 flags;
  101. u64 value;
  102. local_irq_save(flags);
  103. mic_tm_regs = cbe_get_cpu_mic_tm_regs(cpu);
  104. pmd_regs = cbe_get_cpu_pmd_regs(cpu);
  105. pr_debug("pm register is mapped at %p\n", &pmd_regs->pmcr);
  106. pr_debug("mic register is mapped at %p\n", &mic_tm_regs->slow_fast_timer_0);
  107. out_be64(&mic_tm_regs->slow_fast_timer_0, MIC_Slow_Fast_Timer_table[pmode]);
  108. out_be64(&mic_tm_regs->slow_fast_timer_1, MIC_Slow_Fast_Timer_table[pmode]);
  109. out_be64(&mic_tm_regs->slow_next_timer_0, MIC_Slow_Next_Timer_table[pmode]);
  110. out_be64(&mic_tm_regs->slow_next_timer_1, MIC_Slow_Next_Timer_table[pmode]);
  111. value = in_be64(&pmd_regs->pmcr);
  112. /* set bits to zero */
  113. value &= 0xFFFFFFFFFFFFFFF8ull;
  114. /* set bits to next pmode */
  115. value |= pmode;
  116. out_be64(&pmd_regs->pmcr, value);
  117. /* wait until new pmode appears in status register */
  118. value = in_be64(&pmd_regs->pmsr) & 0x07;
  119. while(value != pmode) {
  120. cpu_relax();
  121. value = in_be64(&pmd_regs->pmsr) & 0x07;
  122. }
  123. local_irq_restore(flags);
  124. return 0;
  125. }
  126. static int set_pmode(int cpu, unsigned int slow_mode) {
  127. if (pmi_dev)
  128. return set_pmode_pmi(cpu, slow_mode);
  129. else
  130. return set_pmode_reg(cpu, slow_mode);
  131. }
  132. static void cbe_cpufreq_handle_pmi(struct of_device *dev, pmi_message_t pmi_msg)
  133. {
  134. struct cpufreq_policy policy;
  135. u8 cpu;
  136. u8 cbe_pmode_new;
  137. BUG_ON(pmi_msg.type != PMI_TYPE_FREQ_CHANGE);
  138. cpu = cbe_node_to_cpu(pmi_msg.data1);
  139. cbe_pmode_new = pmi_msg.data2;
  140. cpufreq_get_policy(&policy, cpu);
  141. policy.max = min(policy.max, cbe_freqs[cbe_pmode_new].frequency);
  142. policy.min = min(policy.min, policy.max);
  143. pr_debug("cbe_handle_pmi: new policy.min=%d policy.max=%d\n", policy.min, policy.max);
  144. cpufreq_set_policy(&policy);
  145. }
  146. static struct pmi_handler cbe_pmi_handler = {
  147. .type = PMI_TYPE_FREQ_CHANGE,
  148. .handle_pmi_message = cbe_cpufreq_handle_pmi,
  149. };
  150. /*
  151. * cpufreq functions
  152. */
  153. static int cbe_cpufreq_cpu_init(struct cpufreq_policy *policy)
  154. {
  155. const u32 *max_freqp;
  156. u32 max_freq;
  157. int i, cur_pmode;
  158. struct device_node *cpu;
  159. cpu = of_get_cpu_node(policy->cpu, NULL);
  160. if (!cpu)
  161. return -ENODEV;
  162. pr_debug("init cpufreq on CPU %d\n", policy->cpu);
  163. max_freqp = of_get_property(cpu, "clock-frequency", NULL);
  164. if (!max_freqp)
  165. return -EINVAL;
  166. /* we need the freq in kHz */
  167. max_freq = *max_freqp / 1000;
  168. pr_debug("max clock-frequency is at %u kHz\n", max_freq);
  169. pr_debug("initializing frequency table\n");
  170. /* initialize frequency table */
  171. for (i=0; cbe_freqs[i].frequency!=CPUFREQ_TABLE_END; i++) {
  172. cbe_freqs[i].frequency = max_freq / cbe_freqs[i].index;
  173. pr_debug("%d: %d\n", i, cbe_freqs[i].frequency);
  174. }
  175. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  176. /* if DEBUG is enabled set_pmode() measures the correct latency of a transition */
  177. policy->cpuinfo.transition_latency = 25000;
  178. cur_pmode = get_pmode(policy->cpu);
  179. pr_debug("current pmode is at %d\n",cur_pmode);
  180. policy->cur = cbe_freqs[cur_pmode].frequency;
  181. #ifdef CONFIG_SMP
  182. policy->cpus = cpu_sibling_map[policy->cpu];
  183. #endif
  184. cpufreq_frequency_table_get_attr(cbe_freqs, policy->cpu);
  185. /* this ensures that policy->cpuinfo_min and policy->cpuinfo_max are set correctly */
  186. return cpufreq_frequency_table_cpuinfo(policy, cbe_freqs);
  187. }
  188. static int cbe_cpufreq_cpu_exit(struct cpufreq_policy *policy)
  189. {
  190. cpufreq_frequency_table_put_attr(policy->cpu);
  191. return 0;
  192. }
  193. static int cbe_cpufreq_verify(struct cpufreq_policy *policy)
  194. {
  195. return cpufreq_frequency_table_verify(policy, cbe_freqs);
  196. }
  197. static int cbe_cpufreq_target(struct cpufreq_policy *policy, unsigned int target_freq,
  198. unsigned int relation)
  199. {
  200. int rc;
  201. struct cpufreq_freqs freqs;
  202. int cbe_pmode_new;
  203. cpufreq_frequency_table_target(policy,
  204. cbe_freqs,
  205. target_freq,
  206. relation,
  207. &cbe_pmode_new);
  208. freqs.old = policy->cur;
  209. freqs.new = cbe_freqs[cbe_pmode_new].frequency;
  210. freqs.cpu = policy->cpu;
  211. mutex_lock(&cbe_switch_mutex);
  212. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  213. pr_debug("setting frequency for cpu %d to %d kHz, 1/%d of max frequency\n",
  214. policy->cpu,
  215. cbe_freqs[cbe_pmode_new].frequency,
  216. cbe_freqs[cbe_pmode_new].index);
  217. rc = set_pmode(policy->cpu, cbe_pmode_new);
  218. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  219. mutex_unlock(&cbe_switch_mutex);
  220. return rc;
  221. }
  222. static struct cpufreq_driver cbe_cpufreq_driver = {
  223. .verify = cbe_cpufreq_verify,
  224. .target = cbe_cpufreq_target,
  225. .init = cbe_cpufreq_cpu_init,
  226. .exit = cbe_cpufreq_cpu_exit,
  227. .name = "cbe-cpufreq",
  228. .owner = THIS_MODULE,
  229. .flags = CPUFREQ_CONST_LOOPS,
  230. };
  231. /*
  232. * module init and destoy
  233. */
  234. static int __init cbe_cpufreq_init(void)
  235. {
  236. struct device_node *np;
  237. if (!machine_is(cell))
  238. return -ENODEV;
  239. np = of_find_node_by_type(NULL, "ibm,pmi");
  240. pmi_dev = of_find_device_by_node(np);
  241. if (pmi_dev)
  242. pmi_register_handler(pmi_dev, &cbe_pmi_handler);
  243. return cpufreq_register_driver(&cbe_cpufreq_driver);
  244. }
  245. static void __exit cbe_cpufreq_exit(void)
  246. {
  247. if (pmi_dev)
  248. pmi_unregister_handler(pmi_dev, &cbe_pmi_handler);
  249. cpufreq_unregister_driver(&cbe_cpufreq_driver);
  250. }
  251. module_init(cbe_cpufreq_init);
  252. module_exit(cbe_cpufreq_exit);
  253. MODULE_LICENSE("GPL");
  254. MODULE_AUTHOR("Christian Krafft <krafft@de.ibm.com>");