summaryrefslogtreecommitdiffstats
path: root/tools/power/cpupower/utils/helpers/amd.c
blob: 0a56e22240fc8b8b026db7d72e0f5055cc420a1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// SPDX-License-Identifier: GPL-2.0
#if defined(__i386__) || defined(__x86_64__)
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdint.h>

#include <pci/pci.h>

#include "helpers/helpers.h"
#include "cpufreq.h"
#include "acpi_cppc.h"

/* ACPI P-States Helper Functions for AMD Processors ***************/
#define MSR_AMD_PSTATE_STATUS	0xc0010063
#define MSR_AMD_PSTATE		0xc0010064
#define MSR_AMD_PSTATE_LIMIT	0xc0010061

union core_pstate {
	/* pre fam 17h: */
	struct {
		unsigned fid:6;
		unsigned did:3;
		unsigned vid:7;
		unsigned res1:6;
		unsigned nbdid:1;
		unsigned res2:2;
		unsigned nbvid:7;
		unsigned iddval:8;
		unsigned idddiv:2;
		unsigned res3:21;
		unsigned en:1;
	} pstate;
	/* since fam 17h: */
	struct {
		unsigned fid:8;
		unsigned did:6;
		unsigned vid:8;
		unsigned iddval:8;
		unsigned idddiv:2;
		unsigned res1:31;
		unsigned en:1;
	} pstatedef;
	/* since fam 1Ah: */
	struct {
		unsigned fid:12;
		unsigned res1:2;
		unsigned vid:8;
		unsigned iddval:8;
		unsigned idddiv:2;
		unsigned res2:31;
		unsigned en:1;
	} pstatedef2;
	unsigned long long val;
};

static int get_did(union core_pstate pstate)
{
	int t;

	/* Fam 1Ah onward do not use did */
	if (cpupower_cpu_info.family >= 0x1A)
		return 0;

	if (cpupower_cpu_info.caps & CPUPOWER_CAP_AMD_PSTATEDEF)
		t = pstate.pstatedef.did;
	else if (cpupower_cpu_info.family == 0x12)
		t = pstate.val & 0xf;
	else
		t = pstate.pstate.did;

	return t;
}

static int get_cof(union core_pstate pstate)
{
	int t;
	int fid, did, cof = 0;

	did = get_did(pstate);
	if (cpupower_cpu_info.caps & CPUPOWER_CAP_AMD_PSTATEDEF) {
		if (cpupower_cpu_info.family >= 0x1A) {
			fid = pstate.pstatedef2.fid;
			if (fid > 0x0f)
				cof = (fid * 5);
		} else {
			fid = pstate.pstatedef.fid;
			cof = 200 * fid / did;
		}
	} else {
		t = 0x10;
		fid = pstate.pstate.fid;
		if (cpupower_cpu_info.family == 0x11)
			t = 0x8;
		cof = (100 * (fid + t)) >> did;
	}
	return cof;
}

/* Needs:
 * cpu          -> the cpu that gets evaluated
 * boost_states -> how much boost states the machines support
 *
 * Fills up:
 * pstates -> a pointer to an array of size MAX_HW_PSTATES
 *            must be initialized with zeros.
 *            All available  HW pstates (including boost states)
 * no      -> amount of pstates above array got filled up with
 *
 * returns zero on success, -1 on failure
 */
int decode_pstates(unsigned int cpu, int boost_states,
		   unsigned long *pstates, int *no)
{
	int i, psmax;
	union core_pstate pstate;
	unsigned long long val;

	/* Only read out frequencies from HW if HW Pstate is supported,
	 * otherwise frequencies are exported via ACPI tables.
	 */
	if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_AMD_HW_PSTATE))
		return -1;

	if (read_msr(cpu, MSR_AMD_PSTATE_LIMIT, &val))
		return -1;

	psmax = (val >> 4) & 0x7;
	psmax += boost_states;
	for (i = 0; i <= psmax; i++) {
		if (i >= MAX_HW_PSTATES) {
			fprintf(stderr, "HW pstates [%d] exceeding max [%d]\n",
				psmax, MAX_HW_PSTATES);
			return -1;
		}
		if (read_msr(cpu, MSR_AMD_PSTATE + i, &pstate.val))
			return -1;

		/* The enabled bit (bit 63) is common for all families */
		if (!pstate.pstatedef.en)
			continue;

		pstates[i] = get_cof(pstate);
	}
	*no = i;
	return 0;
}

int amd_pci_get_num_boost_states(int *active, int *states)
{
	struct pci_access *pci_acc;
	struct pci_dev *device;
	uint8_t val = 0;

	*active = *states = 0;

	device = pci_slot_func_init(&pci_acc, 0x18, 4);

	if (device == NULL)
		return -ENODEV;

	val = pci_read_byte(device, 0x15c);
	if (val & 3)
		*active = 1;
	else
		*active = 0;
	*states = (val >> 2) & 7;

	pci_cleanup(pci_acc);
	return 0;
}

/* ACPI P-States Helper Functions for AMD Processors ***************/

/* AMD P-State Helper Functions ************************************/
enum amd_pstate_value {
	AMD_PSTATE_HIGHEST_PERF,
	AMD_PSTATE_MAX_FREQ,
	AMD_PSTATE_LOWEST_NONLINEAR_FREQ,
	MAX_AMD_PSTATE_VALUE_READ_FILES,
};

static const char *amd_pstate_value_files[MAX_AMD_PSTATE_VALUE_READ_FILES] = {
	[AMD_PSTATE_HIGHEST_PERF] = "amd_pstate_highest_perf",
	[AMD_PSTATE_MAX_FREQ] = "amd_pstate_max_freq",
	[AMD_PSTATE_LOWEST_NONLINEAR_FREQ] = "amd_pstate_lowest_nonlinear_freq",
};

static unsigned long amd_pstate_get_data(unsigned int cpu,
					 enum amd_pstate_value value)
{
	return cpufreq_get_sysfs_value_from_table(cpu,
						  amd_pstate_value_files,
						  value,
						  MAX_AMD_PSTATE_VALUE_READ_FILES);
}

void amd_pstate_boost_init(unsigned int cpu, int *support, int *active)
{
	unsigned long highest_perf, nominal_perf, cpuinfo_min,
		      cpuinfo_max, amd_pstate_max;

	highest_perf = amd_pstate_get_data(cpu, AMD_PSTATE_HIGHEST_PERF);
	nominal_perf = acpi_cppc_get_data(cpu, NOMINAL_PERF);

	*support = highest_perf > nominal_perf ? 1 : 0;
	if (!(*support))
		return;

	cpufreq_get_hardware_limits(cpu, &cpuinfo_min, &cpuinfo_max);
	amd_pstate_max = amd_pstate_get_data(cpu, AMD_PSTATE_MAX_FREQ);

	*active = cpuinfo_max == amd_pstate_max ? 1 : 0;
}

void amd_pstate_show_perf_and_freq(unsigned int cpu, int no_rounding)
{
	printf(_("    AMD PSTATE Highest Performance: %lu. Maximum Frequency: "),
	       amd_pstate_get_data(cpu, AMD_PSTATE_HIGHEST_PERF));
	/*
	 * If boost isn't active, the cpuinfo_max doesn't indicate real max
	 * frequency. So we read it back from amd-pstate sysfs entry.
	 */
	print_speed(amd_pstate_get_data(cpu, AMD_PSTATE_MAX_FREQ), no_rounding);
	printf(".\n");

	printf(_("    AMD PSTATE Nominal Performance: %lu. Nominal Frequency: "),
	       acpi_cppc_get_data(cpu, NOMINAL_PERF));
	print_speed(acpi_cppc_get_data(cpu, NOMINAL_FREQ) * 1000,
		    no_rounding);
	printf(".\n");

	printf(_("    AMD PSTATE Lowest Non-linear Performance: %lu. Lowest Non-linear Frequency: "),
	       acpi_cppc_get_data(cpu, LOWEST_NONLINEAR_PERF));
	print_speed(amd_pstate_get_data(cpu, AMD_PSTATE_LOWEST_NONLINEAR_FREQ),
		    no_rounding);
	printf(".\n");

	printf(_("    AMD PSTATE Lowest Performance: %lu. Lowest Frequency: "),
	       acpi_cppc_get_data(cpu, LOWEST_PERF));
	print_speed(acpi_cppc_get_data(cpu, LOWEST_FREQ) * 1000, no_rounding);
	printf(".\n");
}

/* AMD P-State Helper Functions ************************************/
#endif /* defined(__i386__) || defined(__x86_64__) */