/*1* (c) 2003-2006 Advanced Micro Devices, Inc.2* Your use of this code is subject to the terms and conditions of the3* GNU general public license version 2. See "COPYING" or4* http://www.gnu.org/licenses/gpl.html5*/67enum pstate {8HW_PSTATE_INVALID = 0xff,9HW_PSTATE_0 = 0,10HW_PSTATE_1 = 1,11HW_PSTATE_2 = 2,12HW_PSTATE_3 = 3,13HW_PSTATE_4 = 4,14HW_PSTATE_5 = 5,15HW_PSTATE_6 = 6,16HW_PSTATE_7 = 7,17};1819struct powernow_k8_data {20unsigned int cpu;2122u32 numps; /* number of p-states */23u32 batps; /* number of p-states supported on battery */24u32 max_hw_pstate; /* maximum legal hardware pstate */2526/* these values are constant when the PSB is used to determine27* vid/fid pairings, but are modified during the ->target() call28* when ACPI is used */29u32 rvo; /* ramp voltage offset */30u32 irt; /* isochronous relief time */31u32 vidmvs; /* usable value calculated from mvs */32u32 vstable; /* voltage stabilization time, units 20 us */33u32 plllock; /* pll lock time, units 1 us */34u32 exttype; /* extended interface = 1 */3536/* keep track of the current fid / vid or pstate */37u32 currvid;38u32 currfid;39enum pstate currpstate;4041/* the powernow_table includes all frequency and vid/fid pairings:42* fid are the lower 8 bits of the index, vid are the upper 8 bits.43* frequency is in kHz */44struct cpufreq_frequency_table *powernow_table;4546/* the acpi table needs to be kept. it's only available if ACPI was47* used to determine valid frequency/vid/fid states */48struct acpi_processor_performance acpi_data;4950/* we need to keep track of associated cores, but let cpufreq51* handle hotplug events - so just point at cpufreq pol->cpus52* structure */53struct cpumask *available_cores;54};5556/* processor's cpuid instruction support */57#define CPUID_PROCESSOR_SIGNATURE 1 /* function 1 */58#define CPUID_XFAM 0x0ff00000 /* extended family */59#define CPUID_XFAM_K8 060#define CPUID_XMOD 0x000f0000 /* extended model */61#define CPUID_XMOD_REV_MASK 0x000c000062#define CPUID_XFAM_10H 0x00100000 /* family 0x10 */63#define CPUID_USE_XFAM_XMOD 0x00000f0064#define CPUID_GET_MAX_CAPABILITIES 0x8000000065#define CPUID_FREQ_VOLT_CAPABILITIES 0x8000000766#define P_STATE_TRANSITION_CAPABLE 66768/* Model Specific Registers for p-state transitions. MSRs are 64-bit. For */69/* writes (wrmsr - opcode 0f 30), the register number is placed in ecx, and */70/* the value to write is placed in edx:eax. For reads (rdmsr - opcode 0f 32), */71/* the register number is placed in ecx, and the data is returned in edx:eax. */7273#define MSR_FIDVID_CTL 0xc001004174#define MSR_FIDVID_STATUS 0xc00100427576/* Field definitions within the FID VID Low Control MSR : */77#define MSR_C_LO_INIT_FID_VID 0x0001000078#define MSR_C_LO_NEW_VID 0x00003f0079#define MSR_C_LO_NEW_FID 0x0000003f80#define MSR_C_LO_VID_SHIFT 88182/* Field definitions within the FID VID High Control MSR : */83#define MSR_C_HI_STP_GNT_TO 0x000fffff8485/* Field definitions within the FID VID Low Status MSR : */86#define MSR_S_LO_CHANGE_PENDING 0x80000000 /* cleared when completed */87#define MSR_S_LO_MAX_RAMP_VID 0x3f00000088#define MSR_S_LO_MAX_FID 0x003f000089#define MSR_S_LO_START_FID 0x00003f0090#define MSR_S_LO_CURRENT_FID 0x0000003f9192/* Field definitions within the FID VID High Status MSR : */93#define MSR_S_HI_MIN_WORKING_VID 0x3f00000094#define MSR_S_HI_MAX_WORKING_VID 0x003f000095#define MSR_S_HI_START_VID 0x00003f0096#define MSR_S_HI_CURRENT_VID 0x0000003f97#define MSR_C_HI_STP_GNT_BENIGN 0x000000019899100/* Hardware Pstate _PSS and MSR definitions */101#define USE_HW_PSTATE 0x00000080102#define HW_PSTATE_MASK 0x00000007103#define HW_PSTATE_VALID_MASK 0x80000000104#define HW_PSTATE_MAX_MASK 0x000000f0105#define HW_PSTATE_MAX_SHIFT 4106#define MSR_PSTATE_DEF_BASE 0xc0010064 /* base of Pstate MSRs */107#define MSR_PSTATE_STATUS 0xc0010063 /* Pstate Status MSR */108#define MSR_PSTATE_CTRL 0xc0010062 /* Pstate control MSR */109#define MSR_PSTATE_CUR_LIMIT 0xc0010061 /* pstate current limit MSR */110111/* define the two driver architectures */112#define CPU_OPTERON 0113#define CPU_HW_PSTATE 1114115116/*117* There are restrictions frequencies have to follow:118* - only 1 entry in the low fid table ( <=1.4GHz )119* - lowest entry in the high fid table must be >= 2 * the entry in the120* low fid table121* - lowest entry in the high fid table must be a <= 200MHz + 2 * the entry122* in the low fid table123* - the parts can only step at <= 200 MHz intervals, odd fid values are124* supported in revision G and later revisions.125* - lowest frequency must be >= interprocessor hypertransport link speed126* (only applies to MP systems obviously)127*/128129/* fids (frequency identifiers) are arranged in 2 tables - lo and hi */130#define LO_FID_TABLE_TOP 7 /* fid values marking the boundary */131#define HI_FID_TABLE_BOTTOM 8 /* between the low and high tables */132133#define LO_VCOFREQ_TABLE_TOP 1400 /* corresponding vco frequency values */134#define HI_VCOFREQ_TABLE_BOTTOM 1600135136#define MIN_FREQ_RESOLUTION 200 /* fids jump by 2 matching freq jumps by 200 */137138#define MAX_FID 0x2a /* Spec only gives FID values as far as 5 GHz */139#define LEAST_VID 0x3e /* Lowest (numerically highest) useful vid value */140141#define MIN_FREQ 800 /* Min and max freqs, per spec */142#define MAX_FREQ 5000143144#define INVALID_FID_MASK 0xffffffc0 /* not a valid fid if these bits are set */145#define INVALID_VID_MASK 0xffffffc0 /* not a valid vid if these bits are set */146147#define VID_OFF 0x3f148149#define STOP_GRANT_5NS 1 /* min poss memory access latency for voltage change */150151#define PLL_LOCK_CONVERSION (1000/5) /* ms to ns, then divide by clock period */152153#define MAXIMUM_VID_STEPS 1 /* Current cpus only allow a single step of 25mV */154#define VST_UNITS_20US 20 /* Voltage Stabilization Time is in units of 20us */155156/*157* Most values of interest are encoded in a single field of the _PSS158* entries: the "control" value.159*/160161#define IRT_SHIFT 30162#define RVO_SHIFT 28163#define EXT_TYPE_SHIFT 27164#define PLL_L_SHIFT 20165#define MVS_SHIFT 18166#define VST_SHIFT 11167#define VID_SHIFT 6168#define IRT_MASK 3169#define RVO_MASK 3170#define EXT_TYPE_MASK 1171#define PLL_L_MASK 0x7f172#define MVS_MASK 3173#define VST_MASK 0x7f174#define VID_MASK 0x1f175#define FID_MASK 0x1f176#define EXT_VID_MASK 0x3f177#define EXT_FID_MASK 0x3f178179180/*181* Version 1.4 of the PSB table. This table is constructed by BIOS and is182* to tell the OS's power management driver which VIDs and FIDs are183* supported by this particular processor.184* If the data in the PSB / PST is wrong, then this driver will program the185* wrong values into hardware, which is very likely to lead to a crash.186*/187188#define PSB_ID_STRING "AMDK7PNOW!"189#define PSB_ID_STRING_LEN 10190191#define PSB_VERSION_1_4 0x14192193struct psb_s {194u8 signature[10];195u8 tableversion;196u8 flags1;197u16 vstable;198u8 flags2;199u8 num_tables;200u32 cpuid;201u8 plllocktime;202u8 maxfid;203u8 maxvid;204u8 numps;205};206207/* Pairs of fid/vid values are appended to the version 1.4 PSB table. */208struct pst_s {209u8 fid;210u8 vid;211};212213static int core_voltage_pre_transition(struct powernow_k8_data *data,214u32 reqvid, u32 regfid);215static int core_voltage_post_transition(struct powernow_k8_data *data, u32 reqvid);216static int core_frequency_transition(struct powernow_k8_data *data, u32 reqfid);217218static void powernow_k8_acpi_pst_values(struct powernow_k8_data *data, unsigned int index);219220static int fill_powernow_table_pstate(struct powernow_k8_data *data, struct cpufreq_frequency_table *powernow_table);221static int fill_powernow_table_fidvid(struct powernow_k8_data *data, struct cpufreq_frequency_table *powernow_table);222223224