Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * (C) 2002 - 2003  Dominik Brodowski <linux@brodo.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *  BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/cpufreq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/timex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <asm/msr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <asm/processor.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <asm/cpu_device_id.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) static struct cpufreq_driver	longrun_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * longrun_{low,high}_freq is needed for the conversion of cpufreq kHz
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * values into per cent values. In TMTA microcode, the following is valid:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * performance_pctg = (current_freq - low_freq)/(high_freq - low_freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) static unsigned int longrun_low_freq, longrun_high_freq;
^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) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * longrun_get_policy - get the current LongRun policy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * @policy: struct cpufreq_policy where current policy is written into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * Reads the current LongRun policy by access to MSR_TMTA_LONGRUN_FLAGS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * and MSR_TMTA_LONGRUN_CTRL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) static void longrun_get_policy(struct cpufreq_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	u32 msr_lo, msr_hi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	rdmsr(MSR_TMTA_LONGRUN_FLAGS, msr_lo, msr_hi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	pr_debug("longrun flags are %x - %x\n", msr_lo, msr_hi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	if (msr_lo & 0x01)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		policy->policy = CPUFREQ_POLICY_PERFORMANCE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		policy->policy = CPUFREQ_POLICY_POWERSAVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	rdmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	pr_debug("longrun ctrl is %x - %x\n", msr_lo, msr_hi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	msr_lo &= 0x0000007F;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	msr_hi &= 0x0000007F;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	if (longrun_high_freq <= longrun_low_freq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		/* Assume degenerate Longrun table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		policy->min = policy->max = longrun_high_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		policy->min = longrun_low_freq + msr_lo *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 			((longrun_high_freq - longrun_low_freq) / 100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		policy->max = longrun_low_freq + msr_hi *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 			((longrun_high_freq - longrun_low_freq) / 100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	policy->cpu = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) }
^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) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65)  * longrun_set_policy - sets a new CPUFreq policy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66)  * @policy: new policy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68)  * Sets a new CPUFreq policy on LongRun-capable processors. This function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69)  * has to be called with cpufreq_driver locked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) static int longrun_set_policy(struct cpufreq_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	u32 msr_lo, msr_hi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	u32 pctg_lo, pctg_hi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	if (!policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	if (longrun_high_freq <= longrun_low_freq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		/* Assume degenerate Longrun table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		pctg_lo = pctg_hi = 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		pctg_lo = (policy->min - longrun_low_freq) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 			((longrun_high_freq - longrun_low_freq) / 100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		pctg_hi = (policy->max - longrun_low_freq) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 			((longrun_high_freq - longrun_low_freq) / 100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	if (pctg_hi > 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		pctg_hi = 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	if (pctg_lo > pctg_hi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		pctg_lo = pctg_hi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	/* performance or economy mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	rdmsr(MSR_TMTA_LONGRUN_FLAGS, msr_lo, msr_hi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	msr_lo &= 0xFFFFFFFE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	switch (policy->policy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	case CPUFREQ_POLICY_PERFORMANCE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		msr_lo |= 0x00000001;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	case CPUFREQ_POLICY_POWERSAVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	wrmsr(MSR_TMTA_LONGRUN_FLAGS, msr_lo, msr_hi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	/* lower and upper boundary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	rdmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	msr_lo &= 0xFFFFFF80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	msr_hi &= 0xFFFFFF80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	msr_lo |= pctg_lo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	msr_hi |= pctg_hi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	wrmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^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)  * longrun_verify_poliy - verifies a new CPUFreq policy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)  * @policy: the policy to verify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)  * Validates a new CPUFreq policy. This function has to be called with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)  * cpufreq_driver locked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static int longrun_verify_policy(struct cpufreq_policy_data *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	if (!policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	policy->cpu = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	cpufreq_verify_within_cpu_limits(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static unsigned int longrun_get(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	u32 eax, ebx, ecx, edx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	if (cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	cpuid(0x80860007, &eax, &ebx, &ecx, &edx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	pr_debug("cpuid eax is %u\n", eax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	return eax * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)  * longrun_determine_freqs - determines the lowest and highest possible core frequency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)  * @low_freq: an int to put the lowest frequency into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)  * @high_freq: an int to put the highest frequency into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)  * Determines the lowest and highest possible core frequencies on this CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)  * This is necessary to calculate the performance percentage according to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)  * TMTA rules:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)  * performance_pctg = (target_freq - low_freq)/(high_freq - low_freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static int longrun_determine_freqs(unsigned int *low_freq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 						      unsigned int *high_freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	u32 msr_lo, msr_hi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	u32 save_lo, save_hi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	u32 eax, ebx, ecx, edx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	u32 try_hi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	struct cpuinfo_x86 *c = &cpu_data(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	if (!low_freq || !high_freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	if (cpu_has(c, X86_FEATURE_LRTI)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		/* if the LongRun Table Interface is present, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		 * detection is a bit easier:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		 * For minimum frequency, read out the maximum
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		 * level (msr_hi), write that into "currently
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		 * selected level", and read out the frequency.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		 * For maximum frequency, read out level zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		/* minimum */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		rdmsr(MSR_TMTA_LRTI_READOUT, msr_lo, msr_hi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		wrmsr(MSR_TMTA_LRTI_READOUT, msr_hi, msr_hi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		rdmsr(MSR_TMTA_LRTI_VOLT_MHZ, msr_lo, msr_hi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		*low_freq = msr_lo * 1000; /* to kHz */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		/* maximum */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		wrmsr(MSR_TMTA_LRTI_READOUT, 0, msr_hi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		rdmsr(MSR_TMTA_LRTI_VOLT_MHZ, msr_lo, msr_hi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		*high_freq = msr_lo * 1000; /* to kHz */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		pr_debug("longrun table interface told %u - %u kHz\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 				*low_freq, *high_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		if (*low_freq > *high_freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			*low_freq = *high_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	/* set the upper border to the value determined during TSC init */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	*high_freq = (cpu_khz / 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	*high_freq = *high_freq * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	pr_debug("high frequency is %u kHz\n", *high_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	/* get current borders */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	rdmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	save_lo = msr_lo & 0x0000007F;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	save_hi = msr_hi & 0x0000007F;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	/* if current perf_pctg is larger than 90%, we need to decrease the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	 * upper limit to make the calculation more accurate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	cpuid(0x80860007, &eax, &ebx, &ecx, &edx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	/* try decreasing in 10% steps, some processors react only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	 * on some barrier values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	for (try_hi = 80; try_hi > 0 && ecx > 90; try_hi -= 10) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		/* set to 0 to try_hi perf_pctg */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		msr_lo &= 0xFFFFFF80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		msr_hi &= 0xFFFFFF80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		msr_hi |= try_hi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		wrmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		/* read out current core MHz and current perf_pctg */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		cpuid(0x80860007, &eax, &ebx, &ecx, &edx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		/* restore values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		wrmsr(MSR_TMTA_LONGRUN_CTRL, save_lo, save_hi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	pr_debug("percentage is %u %%, freq is %u MHz\n", ecx, eax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	/* performance_pctg = (current_freq - low_freq)/(high_freq - low_freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	 * eqals
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	 * low_freq * (1 - perf_pctg) = (cur_freq - high_freq * perf_pctg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	 * high_freq * perf_pctg is stored tempoarily into "ebx".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	ebx = (((cpu_khz / 1000) * ecx) / 100); /* to MHz */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	if ((ecx > 95) || (ecx == 0) || (eax < ebx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	edx = ((eax - ebx) * 100) / (100 - ecx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	*low_freq = edx * 1000; /* back to kHz */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	pr_debug("low frequency is %u kHz\n", *low_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	if (*low_freq > *high_freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		*low_freq = *high_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) static int longrun_cpu_init(struct cpufreq_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	int result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	/* capability check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	if (policy->cpu != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	/* detect low and high frequency */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	result = longrun_determine_freqs(&longrun_low_freq, &longrun_high_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	/* cpuinfo and default policy values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	policy->cpuinfo.min_freq = longrun_low_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	policy->cpuinfo.max_freq = longrun_high_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	longrun_get_policy(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) static struct cpufreq_driver longrun_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	.flags		= CPUFREQ_CONST_LOOPS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	.verify		= longrun_verify_policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	.setpolicy	= longrun_set_policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	.get		= longrun_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	.init		= longrun_cpu_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	.name		= "longrun",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) static const struct x86_cpu_id longrun_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	X86_MATCH_VENDOR_FEATURE(TRANSMETA, X86_FEATURE_LONGRUN, NULL),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	{}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) MODULE_DEVICE_TABLE(x86cpu, longrun_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)  * longrun_init - initializes the Transmeta Crusoe LongRun CPUFreq driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)  * Initializes the LongRun support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) static int __init longrun_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	if (!x86_match_cpu(longrun_ids))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	return cpufreq_register_driver(&longrun_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)  * longrun_exit - unregisters LongRun support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) static void __exit longrun_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	cpufreq_unregister_driver(&longrun_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) MODULE_DESCRIPTION("LongRun driver for Transmeta Crusoe and "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		"Efficeon processors.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) module_init(longrun_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) module_exit(longrun_exit);