1 // SPDX-License-Identifier: GPL-2.0
2 #if defined(__i386__) || defined(__x86_64__)
10 #include "helpers/helpers.h"
12 #define MSR_AMD_PSTATE_STATUS 0xc0010063
13 #define MSR_AMD_PSTATE 0xc0010064
14 #define MSR_AMD_PSTATE_LIMIT 0xc0010061
39 unsigned long long val
;
42 static int get_did(int family
, union msr_pstate pstate
)
48 else if (family
== 0x17 || family
== 0x18)
49 t
= pstate
.fam17h_bits
.did
;
56 static int get_cof(int family
, union msr_pstate pstate
)
61 did
= get_did(family
, pstate
);
62 if (family
== 0x17 || family
== 0x18) {
63 fid
= pstate
.fam17h_bits
.fid
;
64 cof
= 200 * fid
/ did
;
67 fid
= pstate
.bits
.fid
;
70 cof
= (100 * (fid
+ t
)) >> did
;
76 * cpu -> the cpu that gets evaluated
77 * cpu_family -> The cpu's family (0x10, 0x12,...)
78 * boots_states -> how much boost states the machines support
81 * pstates -> a pointer to an array of size MAX_HW_PSTATES
82 * must be initialized with zeros.
83 * All available HW pstates (including boost states)
84 * no -> amount of pstates above array got filled up with
86 * returns zero on success, -1 on failure
88 int decode_pstates(unsigned int cpu
, unsigned int cpu_family
,
89 int boost_states
, unsigned long *pstates
, int *no
)
92 union msr_pstate pstate
;
93 unsigned long long val
;
95 /* Only read out frequencies from HW when CPU might be boostable
96 to keep the code as short and clean as possible.
97 Otherwise frequencies are exported via ACPI tables.
99 if (cpu_family
< 0x10 || cpu_family
== 0x14)
102 if (read_msr(cpu
, MSR_AMD_PSTATE_LIMIT
, &val
))
105 psmax
= (val
>> 4) & 0x7;
107 if (read_msr(cpu
, MSR_AMD_PSTATE_STATUS
, &val
))
112 pscur
+= boost_states
;
113 psmax
+= boost_states
;
114 for (i
= 0; i
<= psmax
; i
++) {
115 if (i
>= MAX_HW_PSTATES
) {
116 fprintf(stderr
, "HW pstates [%d] exceeding max [%d]\n",
117 psmax
, MAX_HW_PSTATES
);
120 if (read_msr(cpu
, MSR_AMD_PSTATE
+ i
, &pstate
.val
))
122 if ((cpu_family
== 0x17) && (!pstate
.fam17h_bits
.en
))
124 else if (!pstate
.bits
.en
)
127 pstates
[i
] = get_cof(cpu_family
, pstate
);
133 int amd_pci_get_num_boost_states(int *active
, int *states
)
135 struct pci_access
*pci_acc
;
136 struct pci_dev
*device
;
139 *active
= *states
= 0;
141 device
= pci_slot_func_init(&pci_acc
, 0x18, 4);
146 val
= pci_read_byte(device
, 0x15c);
151 *states
= (val
>> 2) & 7;
153 pci_cleanup(pci_acc
);
156 #endif /* defined(__i386__) || defined(__x86_64__) */