Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/sched.h> |
| 12 | #include <linux/smp.h> |
| 13 | #include <linux/cpufreq.h> |
| 14 | #include <linux/threads.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/init.h> |
| 17 | |
| 18 | #include <asm/head.h> |
| 19 | #include <asm/timer.h> |
| 20 | |
| 21 | static struct cpufreq_driver *cpufreq_us3_driver; |
| 22 | |
| 23 | struct us3_freq_percpu_info { |
| 24 | struct cpufreq_frequency_table table[4]; |
| 25 | }; |
| 26 | |
| 27 | /* Indexed by cpu number. */ |
| 28 | static struct us3_freq_percpu_info *us3_freq_table; |
| 29 | |
| 30 | /* UltraSPARC-III has three dividers: 1, 2, and 32. These are controlled |
| 31 | * in the Safari config register. |
| 32 | */ |
| 33 | #define SAFARI_CFG_DIV_1 0x0000000000000000UL |
| 34 | #define SAFARI_CFG_DIV_2 0x0000000040000000UL |
| 35 | #define SAFARI_CFG_DIV_32 0x0000000080000000UL |
| 36 | #define SAFARI_CFG_DIV_MASK 0x00000000C0000000UL |
| 37 | |
| 38 | static unsigned long read_safari_cfg(void) |
| 39 | { |
| 40 | unsigned long ret; |
| 41 | |
| 42 | __asm__ __volatile__("ldxa [%%g0] %1, %0" |
| 43 | : "=&r" (ret) |
| 44 | : "i" (ASI_SAFARI_CONFIG)); |
| 45 | return ret; |
| 46 | } |
| 47 | |
| 48 | static void write_safari_cfg(unsigned long val) |
| 49 | { |
| 50 | __asm__ __volatile__("stxa %0, [%%g0] %1\n\t" |
| 51 | "membar #Sync" |
| 52 | : /* no outputs */ |
| 53 | : "r" (val), "i" (ASI_SAFARI_CONFIG) |
| 54 | : "memory"); |
| 55 | } |
| 56 | |
| 57 | static unsigned long get_current_freq(unsigned int cpu, unsigned long safari_cfg) |
| 58 | { |
David S. Miller | 2cab224 | 2005-08-18 14:35:38 -0700 | [diff] [blame] | 59 | unsigned long clock_tick = sparc64_get_clock_tick(cpu) / 1000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | unsigned long ret; |
| 61 | |
| 62 | switch (safari_cfg & SAFARI_CFG_DIV_MASK) { |
| 63 | case SAFARI_CFG_DIV_1: |
| 64 | ret = clock_tick / 1; |
| 65 | break; |
| 66 | case SAFARI_CFG_DIV_2: |
| 67 | ret = clock_tick / 2; |
| 68 | break; |
| 69 | case SAFARI_CFG_DIV_32: |
| 70 | ret = clock_tick / 32; |
| 71 | break; |
| 72 | default: |
| 73 | BUG(); |
Joe Perches | 6cb79b3 | 2011-06-03 14:45:23 +0000 | [diff] [blame] | 74 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
| 76 | return ret; |
| 77 | } |
| 78 | |
David S. Miller | 2cab224 | 2005-08-18 14:35:38 -0700 | [diff] [blame] | 79 | static unsigned int us3_freq_get(unsigned int cpu) |
| 80 | { |
| 81 | cpumask_t cpus_allowed; |
| 82 | unsigned long reg; |
| 83 | unsigned int ret; |
| 84 | |
KOSAKI Motohiro | fb1fece | 2011-05-16 13:38:07 -0700 | [diff] [blame] | 85 | cpumask_copy(&cpus_allowed, tsk_cpus_allowed(current)); |
Julia Lawall | d2566af | 2010-03-27 21:11:56 -0700 | [diff] [blame] | 86 | set_cpus_allowed_ptr(current, cpumask_of(cpu)); |
David S. Miller | 2cab224 | 2005-08-18 14:35:38 -0700 | [diff] [blame] | 87 | |
| 88 | reg = read_safari_cfg(); |
| 89 | ret = get_current_freq(cpu, reg); |
| 90 | |
Julia Lawall | d2566af | 2010-03-27 21:11:56 -0700 | [diff] [blame] | 91 | set_cpus_allowed_ptr(current, &cpus_allowed); |
David S. Miller | 2cab224 | 2005-08-18 14:35:38 -0700 | [diff] [blame] | 92 | |
| 93 | return ret; |
| 94 | } |
| 95 | |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 96 | static int us3_freq_target(struct cpufreq_policy *policy, unsigned int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | { |
Viresh Kumar | b43a7ff | 2013-03-24 11:56:43 +0530 | [diff] [blame] | 98 | unsigned int cpu = policy->cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | unsigned long new_bits, new_freq, reg; |
| 100 | cpumask_t cpus_allowed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
KOSAKI Motohiro | fb1fece | 2011-05-16 13:38:07 -0700 | [diff] [blame] | 102 | cpumask_copy(&cpus_allowed, tsk_cpus_allowed(current)); |
Julia Lawall | d2566af | 2010-03-27 21:11:56 -0700 | [diff] [blame] | 103 | set_cpus_allowed_ptr(current, cpumask_of(cpu)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
David S. Miller | 2cab224 | 2005-08-18 14:35:38 -0700 | [diff] [blame] | 105 | new_freq = sparc64_get_clock_tick(cpu) / 1000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | switch (index) { |
| 107 | case 0: |
| 108 | new_bits = SAFARI_CFG_DIV_1; |
| 109 | new_freq /= 1; |
| 110 | break; |
| 111 | case 1: |
| 112 | new_bits = SAFARI_CFG_DIV_2; |
| 113 | new_freq /= 2; |
| 114 | break; |
| 115 | case 2: |
| 116 | new_bits = SAFARI_CFG_DIV_32; |
| 117 | new_freq /= 32; |
| 118 | break; |
| 119 | |
| 120 | default: |
| 121 | BUG(); |
Joe Perches | 6cb79b3 | 2011-06-03 14:45:23 +0000 | [diff] [blame] | 122 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
| 124 | reg = read_safari_cfg(); |
| 125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | reg &= ~SAFARI_CFG_DIV_MASK; |
| 127 | reg |= new_bits; |
| 128 | write_safari_cfg(reg); |
| 129 | |
Julia Lawall | d2566af | 2010-03-27 21:11:56 -0700 | [diff] [blame] | 130 | set_cpus_allowed_ptr(current, &cpus_allowed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | static int __init us3_freq_cpu_init(struct cpufreq_policy *policy) |
| 136 | { |
| 137 | unsigned int cpu = policy->cpu; |
David S. Miller | 2cab224 | 2005-08-18 14:35:38 -0700 | [diff] [blame] | 138 | unsigned long clock_tick = sparc64_get_clock_tick(cpu) / 1000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | struct cpufreq_frequency_table *table = |
| 140 | &us3_freq_table[cpu].table[0]; |
| 141 | |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 142 | table[0].driver_data = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | table[0].frequency = clock_tick / 1; |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 144 | table[1].driver_data = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | table[1].frequency = clock_tick / 2; |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 146 | table[2].driver_data = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | table[2].frequency = clock_tick / 32; |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 148 | table[3].driver_data = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | table[3].frequency = CPUFREQ_TABLE_END; |
| 150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | policy->cpuinfo.transition_latency = 0; |
| 152 | policy->cur = clock_tick; |
| 153 | |
Viresh Kumar | f1a707c | 2013-09-16 18:56:36 +0530 | [diff] [blame] | 154 | return cpufreq_table_validate_and_show(policy, table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | static int us3_freq_cpu_exit(struct cpufreq_policy *policy) |
| 158 | { |
Viresh Kumar | 18f130e | 2013-09-16 18:56:06 +0530 | [diff] [blame] | 159 | if (cpufreq_us3_driver) { |
| 160 | cpufreq_frequency_table_put_attr(policy->cpu); |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 161 | us3_freq_target(policy, 0); |
Viresh Kumar | 18f130e | 2013-09-16 18:56:06 +0530 | [diff] [blame] | 162 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | static int __init us3_freq_init(void) |
| 168 | { |
| 169 | unsigned long manuf, impl, ver; |
| 170 | int ret; |
| 171 | |
David S. Miller | d82ace7 | 2006-02-09 02:52:44 -0800 | [diff] [blame] | 172 | if (tlb_type != cheetah && tlb_type != cheetah_plus) |
| 173 | return -ENODEV; |
| 174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | __asm__("rdpr %%ver, %0" : "=r" (ver)); |
| 176 | manuf = ((ver >> 48) & 0xffff); |
| 177 | impl = ((ver >> 32) & 0xffff); |
| 178 | |
| 179 | if (manuf == CHEETAH_MANUF && |
David S. Miller | d2212bc | 2005-09-27 22:50:06 -0700 | [diff] [blame] | 180 | (impl == CHEETAH_IMPL || |
| 181 | impl == CHEETAH_PLUS_IMPL || |
| 182 | impl == JAGUAR_IMPL || |
| 183 | impl == PANTHER_IMPL)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | struct cpufreq_driver *driver; |
| 185 | |
| 186 | ret = -ENOMEM; |
Viresh Kumar | d5b73cd | 2013-08-06 22:53:06 +0530 | [diff] [blame] | 187 | driver = kzalloc(sizeof(*driver), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | if (!driver) |
| 189 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
Viresh Kumar | d5b73cd | 2013-08-06 22:53:06 +0530 | [diff] [blame] | 191 | us3_freq_table = kzalloc((NR_CPUS * sizeof(*us3_freq_table)), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | GFP_KERNEL); |
| 193 | if (!us3_freq_table) |
| 194 | goto err_out; |
| 195 | |
David S. Miller | 2cab224 | 2005-08-18 14:35:38 -0700 | [diff] [blame] | 196 | driver->init = us3_freq_cpu_init; |
Viresh Kumar | 7a1874a | 2013-10-03 20:28:26 +0530 | [diff] [blame] | 197 | driver->verify = cpufreq_generic_frequency_table_verify; |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 198 | driver->target_index = us3_freq_target; |
David S. Miller | 2cab224 | 2005-08-18 14:35:38 -0700 | [diff] [blame] | 199 | driver->get = us3_freq_get; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | driver->exit = us3_freq_cpu_exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | strcpy(driver->name, "UltraSPARC-III"); |
| 202 | |
| 203 | cpufreq_us3_driver = driver; |
| 204 | ret = cpufreq_register_driver(driver); |
| 205 | if (ret) |
| 206 | goto err_out; |
| 207 | |
| 208 | return 0; |
| 209 | |
| 210 | err_out: |
| 211 | if (driver) { |
| 212 | kfree(driver); |
| 213 | cpufreq_us3_driver = NULL; |
| 214 | } |
Jesper Juhl | b2325fe | 2005-11-07 01:01:35 -0800 | [diff] [blame] | 215 | kfree(us3_freq_table); |
| 216 | us3_freq_table = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | return -ENODEV; |
| 221 | } |
| 222 | |
| 223 | static void __exit us3_freq_exit(void) |
| 224 | { |
| 225 | if (cpufreq_us3_driver) { |
| 226 | cpufreq_unregister_driver(cpufreq_us3_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | kfree(cpufreq_us3_driver); |
| 228 | cpufreq_us3_driver = NULL; |
| 229 | kfree(us3_freq_table); |
| 230 | us3_freq_table = NULL; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | MODULE_AUTHOR("David S. Miller <davem@redhat.com>"); |
| 235 | MODULE_DESCRIPTION("cpufreq driver for UltraSPARC-III"); |
| 236 | MODULE_LICENSE("GPL"); |
| 237 | |
| 238 | module_init(us3_freq_init); |
| 239 | module_exit(us3_freq_exit); |