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