sparc-us3-cpufreq.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* us3_cpufreq.c: UltraSPARC-III cpu frequency support
  2. *
  3. * Copyright (C) 2003 David S. Miller (davem@redhat.com)
  4. *
  5. * Many thanks to Dominik Brodowski for fixing up the cpufreq
  6. * infrastructure in order to make this driver easier to implement.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/sched.h>
  11. #include <linux/smp.h>
  12. #include <linux/cpufreq.h>
  13. #include <linux/threads.h>
  14. #include <linux/slab.h>
  15. #include <linux/init.h>
  16. #include <asm/head.h>
  17. #include <asm/timer.h>
  18. static struct cpufreq_driver *cpufreq_us3_driver;
  19. struct us3_freq_percpu_info {
  20. struct cpufreq_frequency_table table[4];
  21. };
  22. /* Indexed by cpu number. */
  23. static struct us3_freq_percpu_info *us3_freq_table;
  24. /* UltraSPARC-III has three dividers: 1, 2, and 32. These are controlled
  25. * in the Safari config register.
  26. */
  27. #define SAFARI_CFG_DIV_1 0x0000000000000000UL
  28. #define SAFARI_CFG_DIV_2 0x0000000040000000UL
  29. #define SAFARI_CFG_DIV_32 0x0000000080000000UL
  30. #define SAFARI_CFG_DIV_MASK 0x00000000C0000000UL
  31. static unsigned long read_safari_cfg(void)
  32. {
  33. unsigned long ret;
  34. __asm__ __volatile__("ldxa [%%g0] %1, %0"
  35. : "=&r" (ret)
  36. : "i" (ASI_SAFARI_CONFIG));
  37. return ret;
  38. }
  39. static void write_safari_cfg(unsigned long val)
  40. {
  41. __asm__ __volatile__("stxa %0, [%%g0] %1\n\t"
  42. "membar #Sync"
  43. : /* no outputs */
  44. : "r" (val), "i" (ASI_SAFARI_CONFIG)
  45. : "memory");
  46. }
  47. static unsigned long get_current_freq(unsigned int cpu, unsigned long safari_cfg)
  48. {
  49. unsigned long clock_tick = sparc64_get_clock_tick(cpu) / 1000;
  50. unsigned long ret;
  51. switch (safari_cfg & SAFARI_CFG_DIV_MASK) {
  52. case SAFARI_CFG_DIV_1:
  53. ret = clock_tick / 1;
  54. break;
  55. case SAFARI_CFG_DIV_2:
  56. ret = clock_tick / 2;
  57. break;
  58. case SAFARI_CFG_DIV_32:
  59. ret = clock_tick / 32;
  60. break;
  61. default:
  62. BUG();
  63. }
  64. return ret;
  65. }
  66. static unsigned int us3_freq_get(unsigned int cpu)
  67. {
  68. cpumask_t cpus_allowed;
  69. unsigned long reg;
  70. unsigned int ret;
  71. cpumask_copy(&cpus_allowed, tsk_cpus_allowed(current));
  72. set_cpus_allowed_ptr(current, cpumask_of(cpu));
  73. reg = read_safari_cfg();
  74. ret = get_current_freq(cpu, reg);
  75. set_cpus_allowed_ptr(current, &cpus_allowed);
  76. return ret;
  77. }
  78. static void us3_set_cpu_divider_index(struct cpufreq_policy *policy,
  79. unsigned int index)
  80. {
  81. unsigned int cpu = policy->cpu;
  82. unsigned long new_bits, new_freq, reg;
  83. cpumask_t cpus_allowed;
  84. struct cpufreq_freqs freqs;
  85. cpumask_copy(&cpus_allowed, tsk_cpus_allowed(current));
  86. set_cpus_allowed_ptr(current, cpumask_of(cpu));
  87. new_freq = sparc64_get_clock_tick(cpu) / 1000;
  88. switch (index) {
  89. case 0:
  90. new_bits = SAFARI_CFG_DIV_1;
  91. new_freq /= 1;
  92. break;
  93. case 1:
  94. new_bits = SAFARI_CFG_DIV_2;
  95. new_freq /= 2;
  96. break;
  97. case 2:
  98. new_bits = SAFARI_CFG_DIV_32;
  99. new_freq /= 32;
  100. break;
  101. default:
  102. BUG();
  103. }
  104. reg = read_safari_cfg();
  105. freqs.old = get_current_freq(cpu, reg);
  106. freqs.new = new_freq;
  107. cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
  108. reg &= ~SAFARI_CFG_DIV_MASK;
  109. reg |= new_bits;
  110. write_safari_cfg(reg);
  111. cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
  112. set_cpus_allowed_ptr(current, &cpus_allowed);
  113. }
  114. static int us3_freq_target(struct cpufreq_policy *policy,
  115. unsigned int target_freq,
  116. unsigned int relation)
  117. {
  118. unsigned int new_index = 0;
  119. if (cpufreq_frequency_table_target(policy,
  120. &us3_freq_table[policy->cpu].table[0],
  121. target_freq,
  122. relation,
  123. &new_index))
  124. return -EINVAL;
  125. us3_set_cpu_divider_index(policy, new_index);
  126. return 0;
  127. }
  128. static int us3_freq_verify(struct cpufreq_policy *policy)
  129. {
  130. return cpufreq_frequency_table_verify(policy,
  131. &us3_freq_table[policy->cpu].table[0]);
  132. }
  133. static int __init us3_freq_cpu_init(struct cpufreq_policy *policy)
  134. {
  135. unsigned int cpu = policy->cpu;
  136. unsigned long clock_tick = sparc64_get_clock_tick(cpu) / 1000;
  137. struct cpufreq_frequency_table *table =
  138. &us3_freq_table[cpu].table[0];
  139. table[0].driver_data = 0;
  140. table[0].frequency = clock_tick / 1;
  141. table[1].driver_data = 1;
  142. table[1].frequency = clock_tick / 2;
  143. table[2].driver_data = 2;
  144. table[2].frequency = clock_tick / 32;
  145. table[3].driver_data = 0;
  146. table[3].frequency = CPUFREQ_TABLE_END;
  147. policy->cpuinfo.transition_latency = 0;
  148. policy->cur = clock_tick;
  149. return cpufreq_frequency_table_cpuinfo(policy, table);
  150. }
  151. static int us3_freq_cpu_exit(struct cpufreq_policy *policy)
  152. {
  153. if (cpufreq_us3_driver)
  154. us3_set_cpu_divider_index(policy, 0);
  155. return 0;
  156. }
  157. static int __init us3_freq_init(void)
  158. {
  159. unsigned long manuf, impl, ver;
  160. int ret;
  161. if (tlb_type != cheetah && tlb_type != cheetah_plus)
  162. return -ENODEV;
  163. __asm__("rdpr %%ver, %0" : "=r" (ver));
  164. manuf = ((ver >> 48) & 0xffff);
  165. impl = ((ver >> 32) & 0xffff);
  166. if (manuf == CHEETAH_MANUF &&
  167. (impl == CHEETAH_IMPL ||
  168. impl == CHEETAH_PLUS_IMPL ||
  169. impl == JAGUAR_IMPL ||
  170. impl == PANTHER_IMPL)) {
  171. struct cpufreq_driver *driver;
  172. ret = -ENOMEM;
  173. driver = kzalloc(sizeof(*driver), GFP_KERNEL);
  174. if (!driver)
  175. goto err_out;
  176. us3_freq_table = kzalloc((NR_CPUS * sizeof(*us3_freq_table)),
  177. GFP_KERNEL);
  178. if (!us3_freq_table)
  179. goto err_out;
  180. driver->init = us3_freq_cpu_init;
  181. driver->verify = us3_freq_verify;
  182. driver->target = us3_freq_target;
  183. driver->get = us3_freq_get;
  184. driver->exit = us3_freq_cpu_exit;
  185. strcpy(driver->name, "UltraSPARC-III");
  186. cpufreq_us3_driver = driver;
  187. ret = cpufreq_register_driver(driver);
  188. if (ret)
  189. goto err_out;
  190. return 0;
  191. err_out:
  192. if (driver) {
  193. kfree(driver);
  194. cpufreq_us3_driver = NULL;
  195. }
  196. kfree(us3_freq_table);
  197. us3_freq_table = NULL;
  198. return ret;
  199. }
  200. return -ENODEV;
  201. }
  202. static void __exit us3_freq_exit(void)
  203. {
  204. if (cpufreq_us3_driver) {
  205. cpufreq_unregister_driver(cpufreq_us3_driver);
  206. kfree(cpufreq_us3_driver);
  207. cpufreq_us3_driver = NULL;
  208. kfree(us3_freq_table);
  209. us3_freq_table = NULL;
  210. }
  211. }
  212. MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
  213. MODULE_DESCRIPTION("cpufreq driver for UltraSPARC-III");
  214. MODULE_LICENSE("GPL");
  215. module_init(us3_freq_init);
  216. module_exit(us3_freq_exit);