treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / tools / power / cpupower / utils / helpers / amd.c
blob7c4f83a8c97371a5e073d969238ad491ea09fc62
1 // SPDX-License-Identifier: GPL-2.0
2 #if defined(__i386__) || defined(__x86_64__)
3 #include <unistd.h>
4 #include <errno.h>
5 #include <stdio.h>
6 #include <stdint.h>
8 #include <pci/pci.h>
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
16 union msr_pstate {
17 struct {
18 unsigned fid:6;
19 unsigned did:3;
20 unsigned vid:7;
21 unsigned res1:6;
22 unsigned nbdid:1;
23 unsigned res2:2;
24 unsigned nbvid:7;
25 unsigned iddval:8;
26 unsigned idddiv:2;
27 unsigned res3:21;
28 unsigned en:1;
29 } bits;
30 struct {
31 unsigned fid:8;
32 unsigned did:6;
33 unsigned vid:8;
34 unsigned iddval:8;
35 unsigned idddiv:2;
36 unsigned res1:31;
37 unsigned en:1;
38 } fam17h_bits;
39 unsigned long long val;
42 static int get_did(int family, union msr_pstate pstate)
44 int t;
46 if (family == 0x12)
47 t = pstate.val & 0xf;
48 else if (family == 0x17 || family == 0x18)
49 t = pstate.fam17h_bits.did;
50 else
51 t = pstate.bits.did;
53 return t;
56 static int get_cof(int family, union msr_pstate pstate)
58 int t;
59 int fid, did, cof;
61 did = get_did(family, pstate);
62 if (family == 0x17 || family == 0x18) {
63 fid = pstate.fam17h_bits.fid;
64 cof = 200 * fid / did;
65 } else {
66 t = 0x10;
67 fid = pstate.bits.fid;
68 if (family == 0x11)
69 t = 0x8;
70 cof = (100 * (fid + t)) >> did;
72 return cof;
75 /* Needs:
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
80 * Fills up:
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)
91 int i, psmax, pscur;
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)
100 return -1;
102 if (read_msr(cpu, MSR_AMD_PSTATE_LIMIT, &val))
103 return -1;
105 psmax = (val >> 4) & 0x7;
107 if (read_msr(cpu, MSR_AMD_PSTATE_STATUS, &val))
108 return -1;
110 pscur = val & 0x7;
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);
118 return -1;
120 if (read_msr(cpu, MSR_AMD_PSTATE + i, &pstate.val))
121 return -1;
122 if ((cpu_family == 0x17) && (!pstate.fam17h_bits.en))
123 continue;
124 else if (!pstate.bits.en)
125 continue;
127 pstates[i] = get_cof(cpu_family, pstate);
129 *no = i;
130 return 0;
133 int amd_pci_get_num_boost_states(int *active, int *states)
135 struct pci_access *pci_acc;
136 struct pci_dev *device;
137 uint8_t val = 0;
139 *active = *states = 0;
141 device = pci_slot_func_init(&pci_acc, 0x18, 4);
143 if (device == NULL)
144 return -ENODEV;
146 val = pci_read_byte(device, 0x15c);
147 if (val & 3)
148 *active = 1;
149 else
150 *active = 0;
151 *states = (val >> 2) & 7;
153 pci_cleanup(pci_acc);
154 return 0;
156 #endif /* defined(__i386__) || defined(__x86_64__) */