]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - tools/power/cpupower/utils/helpers/amd.c
Input: wm97xx: add new AC97 bus support
[mirror_ubuntu-focal-kernel.git] / tools / power / cpupower / utils / helpers / amd.c
1 #if defined(__i386__) || defined(__x86_64__)
2 #include <unistd.h>
3 #include <errno.h>
4 #include <stdio.h>
5 #include <stdint.h>
6
7 #include <pci/pci.h>
8
9 #include "helpers/helpers.h"
10
11 #define MSR_AMD_PSTATE_STATUS 0xc0010063
12 #define MSR_AMD_PSTATE 0xc0010064
13 #define MSR_AMD_PSTATE_LIMIT 0xc0010061
14
15 union msr_pstate {
16 struct {
17 unsigned fid:6;
18 unsigned did:3;
19 unsigned vid:7;
20 unsigned res1:6;
21 unsigned nbdid:1;
22 unsigned res2:2;
23 unsigned nbvid:7;
24 unsigned iddval:8;
25 unsigned idddiv:2;
26 unsigned res3:21;
27 unsigned en:1;
28 } bits;
29 struct {
30 unsigned fid:8;
31 unsigned did:6;
32 unsigned vid:8;
33 unsigned iddval:8;
34 unsigned idddiv:2;
35 unsigned res1:30;
36 unsigned en:1;
37 } fam17h_bits;
38 unsigned long long val;
39 };
40
41 static int get_did(int family, union msr_pstate pstate)
42 {
43 int t;
44
45 if (family == 0x12)
46 t = pstate.val & 0xf;
47 else if (family == 0x17)
48 t = pstate.fam17h_bits.did;
49 else
50 t = pstate.bits.did;
51
52 return t;
53 }
54
55 static int get_cof(int family, union msr_pstate pstate)
56 {
57 int t;
58 int fid, did, cof;
59
60 did = get_did(family, pstate);
61 if (family == 0x17) {
62 fid = pstate.fam17h_bits.fid;
63 cof = 200 * fid / did;
64 } else {
65 t = 0x10;
66 fid = pstate.bits.fid;
67 if (family == 0x11)
68 t = 0x8;
69 cof = (100 * (fid + t)) >> did;
70 }
71 return cof;
72 }
73
74 /* Needs:
75 * cpu -> the cpu that gets evaluated
76 * cpu_family -> The cpu's family (0x10, 0x12,...)
77 * boots_states -> how much boost states the machines support
78 *
79 * Fills up:
80 * pstates -> a pointer to an array of size MAX_HW_PSTATES
81 * must be initialized with zeros.
82 * All available HW pstates (including boost states)
83 * no -> amount of pstates above array got filled up with
84 *
85 * returns zero on success, -1 on failure
86 */
87 int decode_pstates(unsigned int cpu, unsigned int cpu_family,
88 int boost_states, unsigned long *pstates, int *no)
89 {
90 int i, psmax, pscur;
91 union msr_pstate pstate;
92 unsigned long long val;
93
94 /* Only read out frequencies from HW when CPU might be boostable
95 to keep the code as short and clean as possible.
96 Otherwise frequencies are exported via ACPI tables.
97 */
98 if (cpu_family < 0x10 || cpu_family == 0x14)
99 return -1;
100
101 if (read_msr(cpu, MSR_AMD_PSTATE_LIMIT, &val))
102 return -1;
103
104 psmax = (val >> 4) & 0x7;
105
106 if (read_msr(cpu, MSR_AMD_PSTATE_STATUS, &val))
107 return -1;
108
109 pscur = val & 0x7;
110
111 pscur += boost_states;
112 psmax += boost_states;
113 for (i = 0; i <= psmax; i++) {
114 if (i >= MAX_HW_PSTATES) {
115 fprintf(stderr, "HW pstates [%d] exceeding max [%d]\n",
116 psmax, MAX_HW_PSTATES);
117 return -1;
118 }
119 if (read_msr(cpu, MSR_AMD_PSTATE + i, &pstate.val))
120 return -1;
121 pstates[i] = get_cof(cpu_family, pstate);
122 }
123 *no = i;
124 return 0;
125 }
126
127 int amd_pci_get_num_boost_states(int *active, int *states)
128 {
129 struct pci_access *pci_acc;
130 struct pci_dev *device;
131 uint8_t val = 0;
132
133 *active = *states = 0;
134
135 device = pci_slot_func_init(&pci_acc, 0x18, 4);
136
137 if (device == NULL)
138 return -ENODEV;
139
140 val = pci_read_byte(device, 0x15c);
141 if (val & 3)
142 *active = 1;
143 else
144 *active = 0;
145 *states = (val >> 2) & 7;
146
147 pci_cleanup(pci_acc);
148 return 0;
149 }
150 #endif /* defined(__i386__) || defined(__x86_64__) */