cbe_cpufreq.c 8.3 KB

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