sparc-us3-cpufreq.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 int us3_freq_target(struct cpufreq_policy *policy, unsigned int index)
  79. {
  80. unsigned int cpu = policy->cpu;
  81. unsigned long new_bits, new_freq, reg;
  82. cpumask_t cpus_allowed;
  83. cpumask_copy(&cpus_allowed, tsk_cpus_allowed(current));
  84. set_cpus_allowed_ptr(current, cpumask_of(cpu));
  85. new_freq = sparc64_get_clock_tick(cpu) / 1000;
  86. switch (index) {
  87. case 0:
  88. new_bits = SAFARI_CFG_DIV_1;
  89. new_freq /= 1;
  90. break;
  91. case 1:
  92. new_bits = SAFARI_CFG_DIV_2;
  93. new_freq /= 2;
  94. break;
  95. case 2:
  96. new_bits = SAFARI_CFG_DIV_32;
  97. new_freq /= 32;
  98. break;
  99. default:
  100. BUG();
  101. }
  102. reg = read_safari_cfg();
  103. reg &= ~SAFARI_CFG_DIV_MASK;
  104. reg |= new_bits;
  105. write_safari_cfg(reg);
  106. set_cpus_allowed_ptr(current, &cpus_allowed);
  107. return 0;
  108. }
  109. static int __init us3_freq_cpu_init(struct cpufreq_policy *policy)
  110. {
  111. unsigned int cpu = policy->cpu;
  112. unsigned long clock_tick = sparc64_get_clock_tick(cpu) / 1000;
  113. struct cpufreq_frequency_table *table =
  114. &us3_freq_table[cpu].table[0];
  115. table[0].driver_data = 0;
  116. table[0].frequency = clock_tick / 1;
  117. table[1].driver_data = 1;
  118. table[1].frequency = clock_tick / 2;
  119. table[2].driver_data = 2;
  120. table[2].frequency = clock_tick / 32;
  121. table[3].driver_data = 0;
  122. table[3].frequency = CPUFREQ_TABLE_END;
  123. policy->cpuinfo.transition_latency = 0;
  124. policy->cur = clock_tick;
  125. return cpufreq_table_validate_and_show(policy, table);
  126. }
  127. static int us3_freq_cpu_exit(struct cpufreq_policy *policy)
  128. {
  129. if (cpufreq_us3_driver)
  130. us3_freq_target(policy, 0);
  131. return 0;
  132. }
  133. static int __init us3_freq_init(void)
  134. {
  135. unsigned long manuf, impl, ver;
  136. int ret;
  137. if (tlb_type != cheetah && tlb_type != cheetah_plus)
  138. return -ENODEV;
  139. __asm__("rdpr %%ver, %0" : "=r" (ver));
  140. manuf = ((ver >> 48) & 0xffff);
  141. impl = ((ver >> 32) & 0xffff);
  142. if (manuf == CHEETAH_MANUF &&
  143. (impl == CHEETAH_IMPL ||
  144. impl == CHEETAH_PLUS_IMPL ||
  145. impl == JAGUAR_IMPL ||
  146. impl == PANTHER_IMPL)) {
  147. struct cpufreq_driver *driver;
  148. ret = -ENOMEM;
  149. driver = kzalloc(sizeof(*driver), GFP_KERNEL);
  150. if (!driver)
  151. goto err_out;
  152. us3_freq_table = kzalloc((NR_CPUS * sizeof(*us3_freq_table)),
  153. GFP_KERNEL);
  154. if (!us3_freq_table)
  155. goto err_out;
  156. driver->init = us3_freq_cpu_init;
  157. driver->verify = cpufreq_generic_frequency_table_verify;
  158. driver->target_index = us3_freq_target;
  159. driver->get = us3_freq_get;
  160. driver->exit = us3_freq_cpu_exit;
  161. strcpy(driver->name, "UltraSPARC-III");
  162. cpufreq_us3_driver = driver;
  163. ret = cpufreq_register_driver(driver);
  164. if (ret)
  165. goto err_out;
  166. return 0;
  167. err_out:
  168. if (driver) {
  169. kfree(driver);
  170. cpufreq_us3_driver = NULL;
  171. }
  172. kfree(us3_freq_table);
  173. us3_freq_table = NULL;
  174. return ret;
  175. }
  176. return -ENODEV;
  177. }
  178. static void __exit us3_freq_exit(void)
  179. {
  180. if (cpufreq_us3_driver) {
  181. cpufreq_unregister_driver(cpufreq_us3_driver);
  182. kfree(cpufreq_us3_driver);
  183. cpufreq_us3_driver = NULL;
  184. kfree(us3_freq_table);
  185. us3_freq_table = NULL;
  186. }
  187. }
  188. MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
  189. MODULE_DESCRIPTION("cpufreq driver for UltraSPARC-III");
  190. MODULE_LICENSE("GPL");
  191. module_init(us3_freq_init);
  192. module_exit(us3_freq_exit);