xref: /OK3568_Linux_fs/kernel/arch/x86/events/amd/power.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Performance events - AMD Processor Power Reporting Mechanism
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2016 Advanced Micro Devices, Inc.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Author: Huang Rui <ray.huang@amd.com>
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/module.h>
11*4882a593Smuzhiyun #include <linux/slab.h>
12*4882a593Smuzhiyun #include <linux/perf_event.h>
13*4882a593Smuzhiyun #include <asm/cpu_device_id.h>
14*4882a593Smuzhiyun #include "../perf_event.h"
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun /* Event code: LSB 8 bits, passed in attr->config any other bit is reserved. */
17*4882a593Smuzhiyun #define AMD_POWER_EVENT_MASK		0xFFULL
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun /*
20*4882a593Smuzhiyun  * Accumulated power status counters.
21*4882a593Smuzhiyun  */
22*4882a593Smuzhiyun #define AMD_POWER_EVENTSEL_PKG		1
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun /*
25*4882a593Smuzhiyun  * The ratio of compute unit power accumulator sample period to the
26*4882a593Smuzhiyun  * PTSC period.
27*4882a593Smuzhiyun  */
28*4882a593Smuzhiyun static unsigned int cpu_pwr_sample_ratio;
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun /* Maximum accumulated power of a compute unit. */
31*4882a593Smuzhiyun static u64 max_cu_acc_power;
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun static struct pmu pmu_class;
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /*
36*4882a593Smuzhiyun  * Accumulated power represents the sum of each compute unit's (CU) power
37*4882a593Smuzhiyun  * consumption. On any core of each CU we read the total accumulated power from
38*4882a593Smuzhiyun  * MSR_F15H_CU_PWR_ACCUMULATOR. cpu_mask represents CPU bit map of all cores
39*4882a593Smuzhiyun  * which are picked to measure the power for the CUs they belong to.
40*4882a593Smuzhiyun  */
41*4882a593Smuzhiyun static cpumask_t cpu_mask;
42*4882a593Smuzhiyun 
event_update(struct perf_event * event)43*4882a593Smuzhiyun static void event_update(struct perf_event *event)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun 	struct hw_perf_event *hwc = &event->hw;
46*4882a593Smuzhiyun 	u64 prev_pwr_acc, new_pwr_acc, prev_ptsc, new_ptsc;
47*4882a593Smuzhiyun 	u64 delta, tdelta;
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	prev_pwr_acc = hwc->pwr_acc;
50*4882a593Smuzhiyun 	prev_ptsc = hwc->ptsc;
51*4882a593Smuzhiyun 	rdmsrl(MSR_F15H_CU_PWR_ACCUMULATOR, new_pwr_acc);
52*4882a593Smuzhiyun 	rdmsrl(MSR_F15H_PTSC, new_ptsc);
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	/*
55*4882a593Smuzhiyun 	 * Calculate the CU power consumption over a time period, the unit of
56*4882a593Smuzhiyun 	 * final value (delta) is micro-Watts. Then add it to the event count.
57*4882a593Smuzhiyun 	 */
58*4882a593Smuzhiyun 	if (new_pwr_acc < prev_pwr_acc) {
59*4882a593Smuzhiyun 		delta = max_cu_acc_power + new_pwr_acc;
60*4882a593Smuzhiyun 		delta -= prev_pwr_acc;
61*4882a593Smuzhiyun 	} else
62*4882a593Smuzhiyun 		delta = new_pwr_acc - prev_pwr_acc;
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	delta *= cpu_pwr_sample_ratio * 1000;
65*4882a593Smuzhiyun 	tdelta = new_ptsc - prev_ptsc;
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	do_div(delta, tdelta);
68*4882a593Smuzhiyun 	local64_add(delta, &event->count);
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun 
__pmu_event_start(struct perf_event * event)71*4882a593Smuzhiyun static void __pmu_event_start(struct perf_event *event)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun 	if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
74*4882a593Smuzhiyun 		return;
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	event->hw.state = 0;
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	rdmsrl(MSR_F15H_PTSC, event->hw.ptsc);
79*4882a593Smuzhiyun 	rdmsrl(MSR_F15H_CU_PWR_ACCUMULATOR, event->hw.pwr_acc);
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun 
pmu_event_start(struct perf_event * event,int mode)82*4882a593Smuzhiyun static void pmu_event_start(struct perf_event *event, int mode)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun 	__pmu_event_start(event);
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun 
pmu_event_stop(struct perf_event * event,int mode)87*4882a593Smuzhiyun static void pmu_event_stop(struct perf_event *event, int mode)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun 	struct hw_perf_event *hwc = &event->hw;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	/* Mark event as deactivated and stopped. */
92*4882a593Smuzhiyun 	if (!(hwc->state & PERF_HES_STOPPED))
93*4882a593Smuzhiyun 		hwc->state |= PERF_HES_STOPPED;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	/* Check if software counter update is necessary. */
96*4882a593Smuzhiyun 	if ((mode & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
97*4882a593Smuzhiyun 		/*
98*4882a593Smuzhiyun 		 * Drain the remaining delta count out of an event
99*4882a593Smuzhiyun 		 * that we are disabling:
100*4882a593Smuzhiyun 		 */
101*4882a593Smuzhiyun 		event_update(event);
102*4882a593Smuzhiyun 		hwc->state |= PERF_HES_UPTODATE;
103*4882a593Smuzhiyun 	}
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun 
pmu_event_add(struct perf_event * event,int mode)106*4882a593Smuzhiyun static int pmu_event_add(struct perf_event *event, int mode)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun 	struct hw_perf_event *hwc = &event->hw;
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	if (mode & PERF_EF_START)
113*4882a593Smuzhiyun 		__pmu_event_start(event);
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	return 0;
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun 
pmu_event_del(struct perf_event * event,int flags)118*4882a593Smuzhiyun static void pmu_event_del(struct perf_event *event, int flags)
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun 	pmu_event_stop(event, PERF_EF_UPDATE);
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun 
pmu_event_init(struct perf_event * event)123*4882a593Smuzhiyun static int pmu_event_init(struct perf_event *event)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun 	u64 cfg = event->attr.config & AMD_POWER_EVENT_MASK;
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	/* Only look at AMD power events. */
128*4882a593Smuzhiyun 	if (event->attr.type != pmu_class.type)
129*4882a593Smuzhiyun 		return -ENOENT;
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	/* Unsupported modes and filters. */
132*4882a593Smuzhiyun 	if (event->attr.sample_period)
133*4882a593Smuzhiyun 		return -EINVAL;
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	if (cfg != AMD_POWER_EVENTSEL_PKG)
136*4882a593Smuzhiyun 		return -EINVAL;
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	return 0;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun 
pmu_event_read(struct perf_event * event)141*4882a593Smuzhiyun static void pmu_event_read(struct perf_event *event)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun 	event_update(event);
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun static ssize_t
get_attr_cpumask(struct device * dev,struct device_attribute * attr,char * buf)147*4882a593Smuzhiyun get_attr_cpumask(struct device *dev, struct device_attribute *attr, char *buf)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun 	return cpumap_print_to_pagebuf(true, buf, &cpu_mask);
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun static DEVICE_ATTR(cpumask, S_IRUGO, get_attr_cpumask, NULL);
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun static struct attribute *pmu_attrs[] = {
155*4882a593Smuzhiyun 	&dev_attr_cpumask.attr,
156*4882a593Smuzhiyun 	NULL,
157*4882a593Smuzhiyun };
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun static struct attribute_group pmu_attr_group = {
160*4882a593Smuzhiyun 	.attrs = pmu_attrs,
161*4882a593Smuzhiyun };
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun /*
164*4882a593Smuzhiyun  * Currently it only supports to report the power of each
165*4882a593Smuzhiyun  * processor/package.
166*4882a593Smuzhiyun  */
167*4882a593Smuzhiyun EVENT_ATTR_STR(power-pkg, power_pkg, "event=0x01");
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun EVENT_ATTR_STR(power-pkg.unit, power_pkg_unit, "mWatts");
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun /* Convert the count from micro-Watts to milli-Watts. */
172*4882a593Smuzhiyun EVENT_ATTR_STR(power-pkg.scale, power_pkg_scale, "1.000000e-3");
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun static struct attribute *events_attr[] = {
175*4882a593Smuzhiyun 	EVENT_PTR(power_pkg),
176*4882a593Smuzhiyun 	EVENT_PTR(power_pkg_unit),
177*4882a593Smuzhiyun 	EVENT_PTR(power_pkg_scale),
178*4882a593Smuzhiyun 	NULL,
179*4882a593Smuzhiyun };
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun static struct attribute_group pmu_events_group = {
182*4882a593Smuzhiyun 	.name	= "events",
183*4882a593Smuzhiyun 	.attrs	= events_attr,
184*4882a593Smuzhiyun };
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun PMU_FORMAT_ATTR(event, "config:0-7");
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun static struct attribute *formats_attr[] = {
189*4882a593Smuzhiyun 	&format_attr_event.attr,
190*4882a593Smuzhiyun 	NULL,
191*4882a593Smuzhiyun };
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun static struct attribute_group pmu_format_group = {
194*4882a593Smuzhiyun 	.name	= "format",
195*4882a593Smuzhiyun 	.attrs	= formats_attr,
196*4882a593Smuzhiyun };
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun static const struct attribute_group *attr_groups[] = {
199*4882a593Smuzhiyun 	&pmu_attr_group,
200*4882a593Smuzhiyun 	&pmu_format_group,
201*4882a593Smuzhiyun 	&pmu_events_group,
202*4882a593Smuzhiyun 	NULL,
203*4882a593Smuzhiyun };
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun static struct pmu pmu_class = {
206*4882a593Smuzhiyun 	.attr_groups	= attr_groups,
207*4882a593Smuzhiyun 	/* system-wide only */
208*4882a593Smuzhiyun 	.task_ctx_nr	= perf_invalid_context,
209*4882a593Smuzhiyun 	.event_init	= pmu_event_init,
210*4882a593Smuzhiyun 	.add		= pmu_event_add,
211*4882a593Smuzhiyun 	.del		= pmu_event_del,
212*4882a593Smuzhiyun 	.start		= pmu_event_start,
213*4882a593Smuzhiyun 	.stop		= pmu_event_stop,
214*4882a593Smuzhiyun 	.read		= pmu_event_read,
215*4882a593Smuzhiyun 	.capabilities	= PERF_PMU_CAP_NO_EXCLUDE,
216*4882a593Smuzhiyun 	.module		= THIS_MODULE,
217*4882a593Smuzhiyun };
218*4882a593Smuzhiyun 
power_cpu_exit(unsigned int cpu)219*4882a593Smuzhiyun static int power_cpu_exit(unsigned int cpu)
220*4882a593Smuzhiyun {
221*4882a593Smuzhiyun 	int target;
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	if (!cpumask_test_and_clear_cpu(cpu, &cpu_mask))
224*4882a593Smuzhiyun 		return 0;
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	/*
227*4882a593Smuzhiyun 	 * Find a new CPU on the same compute unit, if was set in cpumask
228*4882a593Smuzhiyun 	 * and still some CPUs on compute unit. Then migrate event and
229*4882a593Smuzhiyun 	 * context to new CPU.
230*4882a593Smuzhiyun 	 */
231*4882a593Smuzhiyun 	target = cpumask_any_but(topology_sibling_cpumask(cpu), cpu);
232*4882a593Smuzhiyun 	if (target < nr_cpumask_bits) {
233*4882a593Smuzhiyun 		cpumask_set_cpu(target, &cpu_mask);
234*4882a593Smuzhiyun 		perf_pmu_migrate_context(&pmu_class, cpu, target);
235*4882a593Smuzhiyun 	}
236*4882a593Smuzhiyun 	return 0;
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun 
power_cpu_init(unsigned int cpu)239*4882a593Smuzhiyun static int power_cpu_init(unsigned int cpu)
240*4882a593Smuzhiyun {
241*4882a593Smuzhiyun 	int target;
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	/*
244*4882a593Smuzhiyun 	 * 1) If any CPU is set at cpu_mask in the same compute unit, do
245*4882a593Smuzhiyun 	 * nothing.
246*4882a593Smuzhiyun 	 * 2) If no CPU is set at cpu_mask in the same compute unit,
247*4882a593Smuzhiyun 	 * set current ONLINE CPU.
248*4882a593Smuzhiyun 	 *
249*4882a593Smuzhiyun 	 * Note: if there is a CPU aside of the new one already in the
250*4882a593Smuzhiyun 	 * sibling mask, then it is also in cpu_mask.
251*4882a593Smuzhiyun 	 */
252*4882a593Smuzhiyun 	target = cpumask_any_but(topology_sibling_cpumask(cpu), cpu);
253*4882a593Smuzhiyun 	if (target >= nr_cpumask_bits)
254*4882a593Smuzhiyun 		cpumask_set_cpu(cpu, &cpu_mask);
255*4882a593Smuzhiyun 	return 0;
256*4882a593Smuzhiyun }
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun static const struct x86_cpu_id cpu_match[] = {
259*4882a593Smuzhiyun 	X86_MATCH_VENDOR_FAM(AMD, 0x15, NULL),
260*4882a593Smuzhiyun 	{},
261*4882a593Smuzhiyun };
262*4882a593Smuzhiyun 
amd_power_pmu_init(void)263*4882a593Smuzhiyun static int __init amd_power_pmu_init(void)
264*4882a593Smuzhiyun {
265*4882a593Smuzhiyun 	int ret;
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	if (!x86_match_cpu(cpu_match))
268*4882a593Smuzhiyun 		return -ENODEV;
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	if (!boot_cpu_has(X86_FEATURE_ACC_POWER))
271*4882a593Smuzhiyun 		return -ENODEV;
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	cpu_pwr_sample_ratio = cpuid_ecx(0x80000007);
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 	if (rdmsrl_safe(MSR_F15H_CU_MAX_PWR_ACCUMULATOR, &max_cu_acc_power)) {
276*4882a593Smuzhiyun 		pr_err("Failed to read max compute unit power accumulator MSR\n");
277*4882a593Smuzhiyun 		return -ENODEV;
278*4882a593Smuzhiyun 	}
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	cpuhp_setup_state(CPUHP_AP_PERF_X86_AMD_POWER_ONLINE,
282*4882a593Smuzhiyun 			  "perf/x86/amd/power:online",
283*4882a593Smuzhiyun 			  power_cpu_init, power_cpu_exit);
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	ret = perf_pmu_register(&pmu_class, "power", -1);
286*4882a593Smuzhiyun 	if (WARN_ON(ret)) {
287*4882a593Smuzhiyun 		pr_warn("AMD Power PMU registration failed\n");
288*4882a593Smuzhiyun 		return ret;
289*4882a593Smuzhiyun 	}
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun 	pr_info("AMD Power PMU detected\n");
292*4882a593Smuzhiyun 	return ret;
293*4882a593Smuzhiyun }
294*4882a593Smuzhiyun module_init(amd_power_pmu_init);
295*4882a593Smuzhiyun 
amd_power_pmu_exit(void)296*4882a593Smuzhiyun static void __exit amd_power_pmu_exit(void)
297*4882a593Smuzhiyun {
298*4882a593Smuzhiyun 	cpuhp_remove_state_nocalls(CPUHP_AP_PERF_X86_AMD_POWER_ONLINE);
299*4882a593Smuzhiyun 	perf_pmu_unregister(&pmu_class);
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun module_exit(amd_power_pmu_exit);
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun MODULE_AUTHOR("Huang Rui <ray.huang@amd.com>");
304*4882a593Smuzhiyun MODULE_DESCRIPTION("AMD Processor Power Reporting Mechanism");
305*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
306