xref: /linux/arch/x86/events/intel/cstate.c (revision 1dba23b12f49d7cf3d4504171c62541122b55141)
16aec1ad7SBorislav Petkov /*
26aec1ad7SBorislav Petkov  * perf_event_intel_cstate.c: support cstate residency counters
36aec1ad7SBorislav Petkov  *
46aec1ad7SBorislav Petkov  * Copyright (C) 2015, Intel Corp.
56aec1ad7SBorislav Petkov  * Author: Kan Liang (kan.liang@intel.com)
66aec1ad7SBorislav Petkov  *
76aec1ad7SBorislav Petkov  * This library is free software; you can redistribute it and/or
86aec1ad7SBorislav Petkov  * modify it under the terms of the GNU Library General Public
96aec1ad7SBorislav Petkov  * License as published by the Free Software Foundation; either
106aec1ad7SBorislav Petkov  * version 2 of the License, or (at your option) any later version.
116aec1ad7SBorislav Petkov  *
126aec1ad7SBorislav Petkov  * This library is distributed in the hope that it will be useful,
136aec1ad7SBorislav Petkov  * but WITHOUT ANY WARRANTY; without even the implied warranty of
146aec1ad7SBorislav Petkov  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
156aec1ad7SBorislav Petkov  * Library General Public License for more details.
166aec1ad7SBorislav Petkov  *
176aec1ad7SBorislav Petkov  */
186aec1ad7SBorislav Petkov 
196aec1ad7SBorislav Petkov /*
206aec1ad7SBorislav Petkov  * This file export cstate related free running (read-only) counters
216aec1ad7SBorislav Petkov  * for perf. These counters may be use simultaneously by other tools,
226aec1ad7SBorislav Petkov  * such as turbostat. However, it still make sense to implement them
236aec1ad7SBorislav Petkov  * in perf. Because we can conveniently collect them together with
246aec1ad7SBorislav Petkov  * other events, and allow to use them from tools without special MSR
256aec1ad7SBorislav Petkov  * access code.
266aec1ad7SBorislav Petkov  *
276aec1ad7SBorislav Petkov  * The events only support system-wide mode counting. There is no
286aec1ad7SBorislav Petkov  * sampling support because it is not supported by the hardware.
296aec1ad7SBorislav Petkov  *
306aec1ad7SBorislav Petkov  * According to counters' scope and category, two PMUs are registered
316aec1ad7SBorislav Petkov  * with the perf_event core subsystem.
326aec1ad7SBorislav Petkov  *  - 'cstate_core': The counter is available for each physical core.
336aec1ad7SBorislav Petkov  *    The counters include CORE_C*_RESIDENCY.
346aec1ad7SBorislav Petkov  *  - 'cstate_pkg': The counter is available for each physical package.
356aec1ad7SBorislav Petkov  *    The counters include PKG_C*_RESIDENCY.
366aec1ad7SBorislav Petkov  *
376aec1ad7SBorislav Petkov  * All of these counters are specified in the Intel® 64 and IA-32
386aec1ad7SBorislav Petkov  * Architectures Software Developer.s Manual Vol3b.
396aec1ad7SBorislav Petkov  *
406aec1ad7SBorislav Petkov  * Model specific counters:
416aec1ad7SBorislav Petkov  *	MSR_CORE_C1_RES: CORE C1 Residency Counter
426aec1ad7SBorislav Petkov  *			 perf code: 0x00
436aec1ad7SBorislav Petkov  *			 Available model: SLM,AMT
446aec1ad7SBorislav Petkov  *			 Scope: Core (each processor core has a MSR)
456aec1ad7SBorislav Petkov  *	MSR_CORE_C3_RESIDENCY: CORE C3 Residency Counter
466aec1ad7SBorislav Petkov  *			       perf code: 0x01
476aec1ad7SBorislav Petkov  *			       Available model: NHM,WSM,SNB,IVB,HSW,BDW,SKL
486aec1ad7SBorislav Petkov  *			       Scope: Core
496aec1ad7SBorislav Petkov  *	MSR_CORE_C6_RESIDENCY: CORE C6 Residency Counter
506aec1ad7SBorislav Petkov  *			       perf code: 0x02
51889882bcSLukasz Odzioba  *			       Available model: SLM,AMT,NHM,WSM,SNB,IVB,HSW,BDW
52889882bcSLukasz Odzioba  *						SKL,KNL
536aec1ad7SBorislav Petkov  *			       Scope: Core
546aec1ad7SBorislav Petkov  *	MSR_CORE_C7_RESIDENCY: CORE C7 Residency Counter
556aec1ad7SBorislav Petkov  *			       perf code: 0x03
566aec1ad7SBorislav Petkov  *			       Available model: SNB,IVB,HSW,BDW,SKL
576aec1ad7SBorislav Petkov  *			       Scope: Core
586aec1ad7SBorislav Petkov  *	MSR_PKG_C2_RESIDENCY:  Package C2 Residency Counter.
596aec1ad7SBorislav Petkov  *			       perf code: 0x00
60889882bcSLukasz Odzioba  *			       Available model: SNB,IVB,HSW,BDW,SKL,KNL
616aec1ad7SBorislav Petkov  *			       Scope: Package (physical package)
626aec1ad7SBorislav Petkov  *	MSR_PKG_C3_RESIDENCY:  Package C3 Residency Counter.
636aec1ad7SBorislav Petkov  *			       perf code: 0x01
64889882bcSLukasz Odzioba  *			       Available model: NHM,WSM,SNB,IVB,HSW,BDW,SKL,KNL
656aec1ad7SBorislav Petkov  *			       Scope: Package (physical package)
666aec1ad7SBorislav Petkov  *	MSR_PKG_C6_RESIDENCY:  Package C6 Residency Counter.
676aec1ad7SBorislav Petkov  *			       perf code: 0x02
68889882bcSLukasz Odzioba  *			       Available model: SLM,AMT,NHM,WSM,SNB,IVB,HSW,BDW
69889882bcSLukasz Odzioba  *						SKL,KNL
706aec1ad7SBorislav Petkov  *			       Scope: Package (physical package)
716aec1ad7SBorislav Petkov  *	MSR_PKG_C7_RESIDENCY:  Package C7 Residency Counter.
726aec1ad7SBorislav Petkov  *			       perf code: 0x03
736aec1ad7SBorislav Petkov  *			       Available model: NHM,WSM,SNB,IVB,HSW,BDW,SKL
746aec1ad7SBorislav Petkov  *			       Scope: Package (physical package)
756aec1ad7SBorislav Petkov  *	MSR_PKG_C8_RESIDENCY:  Package C8 Residency Counter.
766aec1ad7SBorislav Petkov  *			       perf code: 0x04
776aec1ad7SBorislav Petkov  *			       Available model: HSW ULT only
786aec1ad7SBorislav Petkov  *			       Scope: Package (physical package)
796aec1ad7SBorislav Petkov  *	MSR_PKG_C9_RESIDENCY:  Package C9 Residency Counter.
806aec1ad7SBorislav Petkov  *			       perf code: 0x05
816aec1ad7SBorislav Petkov  *			       Available model: HSW ULT only
826aec1ad7SBorislav Petkov  *			       Scope: Package (physical package)
836aec1ad7SBorislav Petkov  *	MSR_PKG_C10_RESIDENCY: Package C10 Residency Counter.
846aec1ad7SBorislav Petkov  *			       perf code: 0x06
856aec1ad7SBorislav Petkov  *			       Available model: HSW ULT only
866aec1ad7SBorislav Petkov  *			       Scope: Package (physical package)
876aec1ad7SBorislav Petkov  *
886aec1ad7SBorislav Petkov  */
896aec1ad7SBorislav Petkov 
906aec1ad7SBorislav Petkov #include <linux/module.h>
916aec1ad7SBorislav Petkov #include <linux/slab.h>
926aec1ad7SBorislav Petkov #include <linux/perf_event.h>
936aec1ad7SBorislav Petkov #include <asm/cpu_device_id.h>
94bf4ad541SDave Hansen #include <asm/intel-family.h>
9527f6d22bSBorislav Petkov #include "../perf_event.h"
966aec1ad7SBorislav Petkov 
97c7afba32SThomas Gleixner MODULE_LICENSE("GPL");
98c7afba32SThomas Gleixner 
996aec1ad7SBorislav Petkov #define DEFINE_CSTATE_FORMAT_ATTR(_var, _name, _format)		\
1006aec1ad7SBorislav Petkov static ssize_t __cstate_##_var##_show(struct kobject *kobj,	\
1016aec1ad7SBorislav Petkov 				struct kobj_attribute *attr,	\
1026aec1ad7SBorislav Petkov 				char *page)			\
1036aec1ad7SBorislav Petkov {								\
1046aec1ad7SBorislav Petkov 	BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE);		\
1056aec1ad7SBorislav Petkov 	return sprintf(page, _format "\n");			\
1066aec1ad7SBorislav Petkov }								\
1076aec1ad7SBorislav Petkov static struct kobj_attribute format_attr_##_var =		\
1086aec1ad7SBorislav Petkov 	__ATTR(_name, 0444, __cstate_##_var##_show, NULL)
1096aec1ad7SBorislav Petkov 
1106aec1ad7SBorislav Petkov static ssize_t cstate_get_attr_cpumask(struct device *dev,
1116aec1ad7SBorislav Petkov 				       struct device_attribute *attr,
1126aec1ad7SBorislav Petkov 				       char *buf);
1136aec1ad7SBorislav Petkov 
114424646eeSThomas Gleixner /* Model -> events mapping */
115424646eeSThomas Gleixner struct cstate_model {
116424646eeSThomas Gleixner 	unsigned long		core_events;
117424646eeSThomas Gleixner 	unsigned long		pkg_events;
118424646eeSThomas Gleixner 	unsigned long		quirks;
119424646eeSThomas Gleixner };
120424646eeSThomas Gleixner 
121424646eeSThomas Gleixner /* Quirk flags */
122424646eeSThomas Gleixner #define SLM_PKG_C6_USE_C7_MSR	(1UL << 0)
123889882bcSLukasz Odzioba #define KNL_CORE_C6_MSR		(1UL << 1)
124424646eeSThomas Gleixner 
1256aec1ad7SBorislav Petkov struct perf_cstate_msr {
1266aec1ad7SBorislav Petkov 	u64	msr;
1276aec1ad7SBorislav Petkov 	struct	perf_pmu_events_attr *attr;
1286aec1ad7SBorislav Petkov };
1296aec1ad7SBorislav Petkov 
1306aec1ad7SBorislav Petkov 
1316aec1ad7SBorislav Petkov /* cstate_core PMU */
1326aec1ad7SBorislav Petkov static struct pmu cstate_core_pmu;
1336aec1ad7SBorislav Petkov static bool has_cstate_core;
1346aec1ad7SBorislav Petkov 
135424646eeSThomas Gleixner enum perf_cstate_core_events {
1366aec1ad7SBorislav Petkov 	PERF_CSTATE_CORE_C1_RES = 0,
1376aec1ad7SBorislav Petkov 	PERF_CSTATE_CORE_C3_RES,
1386aec1ad7SBorislav Petkov 	PERF_CSTATE_CORE_C6_RES,
1396aec1ad7SBorislav Petkov 	PERF_CSTATE_CORE_C7_RES,
1406aec1ad7SBorislav Petkov 
1416aec1ad7SBorislav Petkov 	PERF_CSTATE_CORE_EVENT_MAX,
1426aec1ad7SBorislav Petkov };
1436aec1ad7SBorislav Petkov 
1446aec1ad7SBorislav Petkov PMU_EVENT_ATTR_STRING(c1-residency, evattr_cstate_core_c1, "event=0x00");
1456aec1ad7SBorislav Petkov PMU_EVENT_ATTR_STRING(c3-residency, evattr_cstate_core_c3, "event=0x01");
1466aec1ad7SBorislav Petkov PMU_EVENT_ATTR_STRING(c6-residency, evattr_cstate_core_c6, "event=0x02");
1476aec1ad7SBorislav Petkov PMU_EVENT_ATTR_STRING(c7-residency, evattr_cstate_core_c7, "event=0x03");
1486aec1ad7SBorislav Petkov 
1496aec1ad7SBorislav Petkov static struct perf_cstate_msr core_msr[] = {
150424646eeSThomas Gleixner 	[PERF_CSTATE_CORE_C1_RES] = { MSR_CORE_C1_RES,		&evattr_cstate_core_c1 },
151424646eeSThomas Gleixner 	[PERF_CSTATE_CORE_C3_RES] = { MSR_CORE_C3_RESIDENCY,	&evattr_cstate_core_c3 },
152424646eeSThomas Gleixner 	[PERF_CSTATE_CORE_C6_RES] = { MSR_CORE_C6_RESIDENCY,	&evattr_cstate_core_c6 },
153424646eeSThomas Gleixner 	[PERF_CSTATE_CORE_C7_RES] = { MSR_CORE_C7_RESIDENCY,	&evattr_cstate_core_c7 },
1546aec1ad7SBorislav Petkov };
1556aec1ad7SBorislav Petkov 
1566aec1ad7SBorislav Petkov static struct attribute *core_events_attrs[PERF_CSTATE_CORE_EVENT_MAX + 1] = {
1576aec1ad7SBorislav Petkov 	NULL,
1586aec1ad7SBorislav Petkov };
1596aec1ad7SBorislav Petkov 
1606aec1ad7SBorislav Petkov static struct attribute_group core_events_attr_group = {
1616aec1ad7SBorislav Petkov 	.name = "events",
1626aec1ad7SBorislav Petkov 	.attrs = core_events_attrs,
1636aec1ad7SBorislav Petkov };
1646aec1ad7SBorislav Petkov 
1656aec1ad7SBorislav Petkov DEFINE_CSTATE_FORMAT_ATTR(core_event, event, "config:0-63");
1666aec1ad7SBorislav Petkov static struct attribute *core_format_attrs[] = {
1676aec1ad7SBorislav Petkov 	&format_attr_core_event.attr,
1686aec1ad7SBorislav Petkov 	NULL,
1696aec1ad7SBorislav Petkov };
1706aec1ad7SBorislav Petkov 
1716aec1ad7SBorislav Petkov static struct attribute_group core_format_attr_group = {
1726aec1ad7SBorislav Petkov 	.name = "format",
1736aec1ad7SBorislav Petkov 	.attrs = core_format_attrs,
1746aec1ad7SBorislav Petkov };
1756aec1ad7SBorislav Petkov 
1766aec1ad7SBorislav Petkov static cpumask_t cstate_core_cpu_mask;
1776aec1ad7SBorislav Petkov static DEVICE_ATTR(cpumask, S_IRUGO, cstate_get_attr_cpumask, NULL);
1786aec1ad7SBorislav Petkov 
1796aec1ad7SBorislav Petkov static struct attribute *cstate_cpumask_attrs[] = {
1806aec1ad7SBorislav Petkov 	&dev_attr_cpumask.attr,
1816aec1ad7SBorislav Petkov 	NULL,
1826aec1ad7SBorislav Petkov };
1836aec1ad7SBorislav Petkov 
1846aec1ad7SBorislav Petkov static struct attribute_group cpumask_attr_group = {
1856aec1ad7SBorislav Petkov 	.attrs = cstate_cpumask_attrs,
1866aec1ad7SBorislav Petkov };
1876aec1ad7SBorislav Petkov 
1886aec1ad7SBorislav Petkov static const struct attribute_group *core_attr_groups[] = {
1896aec1ad7SBorislav Petkov 	&core_events_attr_group,
1906aec1ad7SBorislav Petkov 	&core_format_attr_group,
1916aec1ad7SBorislav Petkov 	&cpumask_attr_group,
1926aec1ad7SBorislav Petkov 	NULL,
1936aec1ad7SBorislav Petkov };
1946aec1ad7SBorislav Petkov 
1956aec1ad7SBorislav Petkov /* cstate_pkg PMU */
1966aec1ad7SBorislav Petkov static struct pmu cstate_pkg_pmu;
1976aec1ad7SBorislav Petkov static bool has_cstate_pkg;
1986aec1ad7SBorislav Petkov 
199424646eeSThomas Gleixner enum perf_cstate_pkg_events {
2006aec1ad7SBorislav Petkov 	PERF_CSTATE_PKG_C2_RES = 0,
2016aec1ad7SBorislav Petkov 	PERF_CSTATE_PKG_C3_RES,
2026aec1ad7SBorislav Petkov 	PERF_CSTATE_PKG_C6_RES,
2036aec1ad7SBorislav Petkov 	PERF_CSTATE_PKG_C7_RES,
2046aec1ad7SBorislav Petkov 	PERF_CSTATE_PKG_C8_RES,
2056aec1ad7SBorislav Petkov 	PERF_CSTATE_PKG_C9_RES,
2066aec1ad7SBorislav Petkov 	PERF_CSTATE_PKG_C10_RES,
2076aec1ad7SBorislav Petkov 
2086aec1ad7SBorislav Petkov 	PERF_CSTATE_PKG_EVENT_MAX,
2096aec1ad7SBorislav Petkov };
2106aec1ad7SBorislav Petkov 
2116aec1ad7SBorislav Petkov PMU_EVENT_ATTR_STRING(c2-residency, evattr_cstate_pkg_c2, "event=0x00");
2126aec1ad7SBorislav Petkov PMU_EVENT_ATTR_STRING(c3-residency, evattr_cstate_pkg_c3, "event=0x01");
2136aec1ad7SBorislav Petkov PMU_EVENT_ATTR_STRING(c6-residency, evattr_cstate_pkg_c6, "event=0x02");
2146aec1ad7SBorislav Petkov PMU_EVENT_ATTR_STRING(c7-residency, evattr_cstate_pkg_c7, "event=0x03");
2156aec1ad7SBorislav Petkov PMU_EVENT_ATTR_STRING(c8-residency, evattr_cstate_pkg_c8, "event=0x04");
2166aec1ad7SBorislav Petkov PMU_EVENT_ATTR_STRING(c9-residency, evattr_cstate_pkg_c9, "event=0x05");
2176aec1ad7SBorislav Petkov PMU_EVENT_ATTR_STRING(c10-residency, evattr_cstate_pkg_c10, "event=0x06");
2186aec1ad7SBorislav Petkov 
2196aec1ad7SBorislav Petkov static struct perf_cstate_msr pkg_msr[] = {
220424646eeSThomas Gleixner 	[PERF_CSTATE_PKG_C2_RES] = { MSR_PKG_C2_RESIDENCY,	&evattr_cstate_pkg_c2 },
221424646eeSThomas Gleixner 	[PERF_CSTATE_PKG_C3_RES] = { MSR_PKG_C3_RESIDENCY,	&evattr_cstate_pkg_c3 },
222424646eeSThomas Gleixner 	[PERF_CSTATE_PKG_C6_RES] = { MSR_PKG_C6_RESIDENCY,	&evattr_cstate_pkg_c6 },
223424646eeSThomas Gleixner 	[PERF_CSTATE_PKG_C7_RES] = { MSR_PKG_C7_RESIDENCY,	&evattr_cstate_pkg_c7 },
224424646eeSThomas Gleixner 	[PERF_CSTATE_PKG_C8_RES] = { MSR_PKG_C8_RESIDENCY,	&evattr_cstate_pkg_c8 },
225424646eeSThomas Gleixner 	[PERF_CSTATE_PKG_C9_RES] = { MSR_PKG_C9_RESIDENCY,	&evattr_cstate_pkg_c9 },
226424646eeSThomas Gleixner 	[PERF_CSTATE_PKG_C10_RES] = { MSR_PKG_C10_RESIDENCY,	&evattr_cstate_pkg_c10 },
2276aec1ad7SBorislav Petkov };
2286aec1ad7SBorislav Petkov 
2296aec1ad7SBorislav Petkov static struct attribute *pkg_events_attrs[PERF_CSTATE_PKG_EVENT_MAX + 1] = {
2306aec1ad7SBorislav Petkov 	NULL,
2316aec1ad7SBorislav Petkov };
2326aec1ad7SBorislav Petkov 
2336aec1ad7SBorislav Petkov static struct attribute_group pkg_events_attr_group = {
2346aec1ad7SBorislav Petkov 	.name = "events",
2356aec1ad7SBorislav Petkov 	.attrs = pkg_events_attrs,
2366aec1ad7SBorislav Petkov };
2376aec1ad7SBorislav Petkov 
2386aec1ad7SBorislav Petkov DEFINE_CSTATE_FORMAT_ATTR(pkg_event, event, "config:0-63");
2396aec1ad7SBorislav Petkov static struct attribute *pkg_format_attrs[] = {
2406aec1ad7SBorislav Petkov 	&format_attr_pkg_event.attr,
2416aec1ad7SBorislav Petkov 	NULL,
2426aec1ad7SBorislav Petkov };
2436aec1ad7SBorislav Petkov static struct attribute_group pkg_format_attr_group = {
2446aec1ad7SBorislav Petkov 	.name = "format",
2456aec1ad7SBorislav Petkov 	.attrs = pkg_format_attrs,
2466aec1ad7SBorislav Petkov };
2476aec1ad7SBorislav Petkov 
2486aec1ad7SBorislav Petkov static cpumask_t cstate_pkg_cpu_mask;
2496aec1ad7SBorislav Petkov 
2506aec1ad7SBorislav Petkov static const struct attribute_group *pkg_attr_groups[] = {
2516aec1ad7SBorislav Petkov 	&pkg_events_attr_group,
2526aec1ad7SBorislav Petkov 	&pkg_format_attr_group,
2536aec1ad7SBorislav Petkov 	&cpumask_attr_group,
2546aec1ad7SBorislav Petkov 	NULL,
2556aec1ad7SBorislav Petkov };
2566aec1ad7SBorislav Petkov 
2576aec1ad7SBorislav Petkov static ssize_t cstate_get_attr_cpumask(struct device *dev,
2586aec1ad7SBorislav Petkov 				       struct device_attribute *attr,
2596aec1ad7SBorislav Petkov 				       char *buf)
2606aec1ad7SBorislav Petkov {
2616aec1ad7SBorislav Petkov 	struct pmu *pmu = dev_get_drvdata(dev);
2626aec1ad7SBorislav Petkov 
2636aec1ad7SBorislav Petkov 	if (pmu == &cstate_core_pmu)
2646aec1ad7SBorislav Petkov 		return cpumap_print_to_pagebuf(true, buf, &cstate_core_cpu_mask);
2656aec1ad7SBorislav Petkov 	else if (pmu == &cstate_pkg_pmu)
2666aec1ad7SBorislav Petkov 		return cpumap_print_to_pagebuf(true, buf, &cstate_pkg_cpu_mask);
2676aec1ad7SBorislav Petkov 	else
2686aec1ad7SBorislav Petkov 		return 0;
2696aec1ad7SBorislav Petkov }
2706aec1ad7SBorislav Petkov 
2716aec1ad7SBorislav Petkov static int cstate_pmu_event_init(struct perf_event *event)
2726aec1ad7SBorislav Petkov {
2736aec1ad7SBorislav Petkov 	u64 cfg = event->attr.config;
27449de0493SThomas Gleixner 	int cpu;
2756aec1ad7SBorislav Petkov 
2766aec1ad7SBorislav Petkov 	if (event->attr.type != event->pmu->type)
2776aec1ad7SBorislav Petkov 		return -ENOENT;
2786aec1ad7SBorislav Petkov 
2796aec1ad7SBorislav Petkov 	/* unsupported modes and filters */
2806aec1ad7SBorislav Petkov 	if (event->attr.exclude_user   ||
2816aec1ad7SBorislav Petkov 	    event->attr.exclude_kernel ||
2826aec1ad7SBorislav Petkov 	    event->attr.exclude_hv     ||
2836aec1ad7SBorislav Petkov 	    event->attr.exclude_idle   ||
2846aec1ad7SBorislav Petkov 	    event->attr.exclude_host   ||
2856aec1ad7SBorislav Petkov 	    event->attr.exclude_guest  ||
2866aec1ad7SBorislav Petkov 	    event->attr.sample_period) /* no sampling */
2876aec1ad7SBorislav Petkov 		return -EINVAL;
2886aec1ad7SBorislav Petkov 
28949de0493SThomas Gleixner 	if (event->cpu < 0)
29049de0493SThomas Gleixner 		return -EINVAL;
29149de0493SThomas Gleixner 
2926aec1ad7SBorislav Petkov 	if (event->pmu == &cstate_core_pmu) {
2936aec1ad7SBorislav Petkov 		if (cfg >= PERF_CSTATE_CORE_EVENT_MAX)
2946aec1ad7SBorislav Petkov 			return -EINVAL;
2956aec1ad7SBorislav Petkov 		if (!core_msr[cfg].attr)
2966aec1ad7SBorislav Petkov 			return -EINVAL;
2976aec1ad7SBorislav Petkov 		event->hw.event_base = core_msr[cfg].msr;
29849de0493SThomas Gleixner 		cpu = cpumask_any_and(&cstate_core_cpu_mask,
29949de0493SThomas Gleixner 				      topology_sibling_cpumask(event->cpu));
3006aec1ad7SBorislav Petkov 	} else if (event->pmu == &cstate_pkg_pmu) {
3016aec1ad7SBorislav Petkov 		if (cfg >= PERF_CSTATE_PKG_EVENT_MAX)
3026aec1ad7SBorislav Petkov 			return -EINVAL;
3036aec1ad7SBorislav Petkov 		if (!pkg_msr[cfg].attr)
3046aec1ad7SBorislav Petkov 			return -EINVAL;
3056aec1ad7SBorislav Petkov 		event->hw.event_base = pkg_msr[cfg].msr;
30649de0493SThomas Gleixner 		cpu = cpumask_any_and(&cstate_pkg_cpu_mask,
30749de0493SThomas Gleixner 				      topology_core_cpumask(event->cpu));
30849de0493SThomas Gleixner 	} else {
3096aec1ad7SBorislav Petkov 		return -ENOENT;
31049de0493SThomas Gleixner 	}
3116aec1ad7SBorislav Petkov 
31249de0493SThomas Gleixner 	if (cpu >= nr_cpu_ids)
31349de0493SThomas Gleixner 		return -ENODEV;
31449de0493SThomas Gleixner 
31549de0493SThomas Gleixner 	event->cpu = cpu;
3166aec1ad7SBorislav Petkov 	event->hw.config = cfg;
3176aec1ad7SBorislav Petkov 	event->hw.idx = -1;
31849de0493SThomas Gleixner 	return 0;
3196aec1ad7SBorislav Petkov }
3206aec1ad7SBorislav Petkov 
3216aec1ad7SBorislav Petkov static inline u64 cstate_pmu_read_counter(struct perf_event *event)
3226aec1ad7SBorislav Petkov {
3236aec1ad7SBorislav Petkov 	u64 val;
3246aec1ad7SBorislav Petkov 
3256aec1ad7SBorislav Petkov 	rdmsrl(event->hw.event_base, val);
3266aec1ad7SBorislav Petkov 	return val;
3276aec1ad7SBorislav Petkov }
3286aec1ad7SBorislav Petkov 
3296aec1ad7SBorislav Petkov static void cstate_pmu_event_update(struct perf_event *event)
3306aec1ad7SBorislav Petkov {
3316aec1ad7SBorislav Petkov 	struct hw_perf_event *hwc = &event->hw;
3326aec1ad7SBorislav Petkov 	u64 prev_raw_count, new_raw_count;
3336aec1ad7SBorislav Petkov 
3346aec1ad7SBorislav Petkov again:
3356aec1ad7SBorislav Petkov 	prev_raw_count = local64_read(&hwc->prev_count);
3366aec1ad7SBorislav Petkov 	new_raw_count = cstate_pmu_read_counter(event);
3376aec1ad7SBorislav Petkov 
3386aec1ad7SBorislav Petkov 	if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
3396aec1ad7SBorislav Petkov 			    new_raw_count) != prev_raw_count)
3406aec1ad7SBorislav Petkov 		goto again;
3416aec1ad7SBorislav Petkov 
3426aec1ad7SBorislav Petkov 	local64_add(new_raw_count - prev_raw_count, &event->count);
3436aec1ad7SBorislav Petkov }
3446aec1ad7SBorislav Petkov 
3456aec1ad7SBorislav Petkov static void cstate_pmu_event_start(struct perf_event *event, int mode)
3466aec1ad7SBorislav Petkov {
3476aec1ad7SBorislav Petkov 	local64_set(&event->hw.prev_count, cstate_pmu_read_counter(event));
3486aec1ad7SBorislav Petkov }
3496aec1ad7SBorislav Petkov 
3506aec1ad7SBorislav Petkov static void cstate_pmu_event_stop(struct perf_event *event, int mode)
3516aec1ad7SBorislav Petkov {
3526aec1ad7SBorislav Petkov 	cstate_pmu_event_update(event);
3536aec1ad7SBorislav Petkov }
3546aec1ad7SBorislav Petkov 
3556aec1ad7SBorislav Petkov static void cstate_pmu_event_del(struct perf_event *event, int mode)
3566aec1ad7SBorislav Petkov {
3576aec1ad7SBorislav Petkov 	cstate_pmu_event_stop(event, PERF_EF_UPDATE);
3586aec1ad7SBorislav Petkov }
3596aec1ad7SBorislav Petkov 
3606aec1ad7SBorislav Petkov static int cstate_pmu_event_add(struct perf_event *event, int mode)
3616aec1ad7SBorislav Petkov {
3626aec1ad7SBorislav Petkov 	if (mode & PERF_EF_START)
3636aec1ad7SBorislav Petkov 		cstate_pmu_event_start(event, mode);
3646aec1ad7SBorislav Petkov 
3656aec1ad7SBorislav Petkov 	return 0;
3666aec1ad7SBorislav Petkov }
3676aec1ad7SBorislav Petkov 
36849de0493SThomas Gleixner /*
36949de0493SThomas Gleixner  * Check if exiting cpu is the designated reader. If so migrate the
37049de0493SThomas Gleixner  * events when there is a valid target available
37149de0493SThomas Gleixner  */
37277c34ef1SSebastian Andrzej Siewior static int cstate_cpu_exit(unsigned int cpu)
3736aec1ad7SBorislav Petkov {
37449de0493SThomas Gleixner 	unsigned int target;
3756aec1ad7SBorislav Petkov 
37649de0493SThomas Gleixner 	if (has_cstate_core &&
37749de0493SThomas Gleixner 	    cpumask_test_and_clear_cpu(cpu, &cstate_core_cpu_mask)) {
3786aec1ad7SBorislav Petkov 
37949de0493SThomas Gleixner 		target = cpumask_any_but(topology_sibling_cpumask(cpu), cpu);
38049de0493SThomas Gleixner 		/* Migrate events if there is a valid target */
38149de0493SThomas Gleixner 		if (target < nr_cpu_ids) {
3826aec1ad7SBorislav Petkov 			cpumask_set_cpu(target, &cstate_core_cpu_mask);
3836aec1ad7SBorislav Petkov 			perf_pmu_migrate_context(&cstate_core_pmu, cpu, target);
3846aec1ad7SBorislav Petkov 		}
3856aec1ad7SBorislav Petkov 	}
38649de0493SThomas Gleixner 
38749de0493SThomas Gleixner 	if (has_cstate_pkg &&
38849de0493SThomas Gleixner 	    cpumask_test_and_clear_cpu(cpu, &cstate_pkg_cpu_mask)) {
38949de0493SThomas Gleixner 
39049de0493SThomas Gleixner 		target = cpumask_any_but(topology_core_cpumask(cpu), cpu);
39149de0493SThomas Gleixner 		/* Migrate events if there is a valid target */
39249de0493SThomas Gleixner 		if (target < nr_cpu_ids) {
3936aec1ad7SBorislav Petkov 			cpumask_set_cpu(target, &cstate_pkg_cpu_mask);
3946aec1ad7SBorislav Petkov 			perf_pmu_migrate_context(&cstate_pkg_pmu, cpu, target);
3956aec1ad7SBorislav Petkov 		}
3966aec1ad7SBorislav Petkov 	}
39777c34ef1SSebastian Andrzej Siewior 	return 0;
39849de0493SThomas Gleixner }
3996aec1ad7SBorislav Petkov 
40077c34ef1SSebastian Andrzej Siewior static int cstate_cpu_init(unsigned int cpu)
4016aec1ad7SBorislav Petkov {
40249de0493SThomas Gleixner 	unsigned int target;
4036aec1ad7SBorislav Petkov 
40449de0493SThomas Gleixner 	/*
40549de0493SThomas Gleixner 	 * If this is the first online thread of that core, set it in
40649de0493SThomas Gleixner 	 * the core cpu mask as the designated reader.
40749de0493SThomas Gleixner 	 */
40849de0493SThomas Gleixner 	target = cpumask_any_and(&cstate_core_cpu_mask,
40949de0493SThomas Gleixner 				 topology_sibling_cpumask(cpu));
41049de0493SThomas Gleixner 
41149de0493SThomas Gleixner 	if (has_cstate_core && target >= nr_cpu_ids)
4126aec1ad7SBorislav Petkov 		cpumask_set_cpu(cpu, &cstate_core_cpu_mask);
4136aec1ad7SBorislav Petkov 
41449de0493SThomas Gleixner 	/*
41549de0493SThomas Gleixner 	 * If this is the first online thread of that package, set it
41649de0493SThomas Gleixner 	 * in the package cpu mask as the designated reader.
41749de0493SThomas Gleixner 	 */
41849de0493SThomas Gleixner 	target = cpumask_any_and(&cstate_pkg_cpu_mask,
41949de0493SThomas Gleixner 				 topology_core_cpumask(cpu));
42049de0493SThomas Gleixner 	if (has_cstate_pkg && target >= nr_cpu_ids)
4216aec1ad7SBorislav Petkov 		cpumask_set_cpu(cpu, &cstate_pkg_cpu_mask);
4226aec1ad7SBorislav Petkov 
42377c34ef1SSebastian Andrzej Siewior 	return 0;
4246aec1ad7SBorislav Petkov }
425c7afba32SThomas Gleixner 
426424646eeSThomas Gleixner static struct pmu cstate_core_pmu = {
427424646eeSThomas Gleixner 	.attr_groups	= core_attr_groups,
428424646eeSThomas Gleixner 	.name		= "cstate_core",
429424646eeSThomas Gleixner 	.task_ctx_nr	= perf_invalid_context,
430424646eeSThomas Gleixner 	.event_init	= cstate_pmu_event_init,
431424646eeSThomas Gleixner 	.add		= cstate_pmu_event_add,
432424646eeSThomas Gleixner 	.del		= cstate_pmu_event_del,
433424646eeSThomas Gleixner 	.start		= cstate_pmu_event_start,
434424646eeSThomas Gleixner 	.stop		= cstate_pmu_event_stop,
435424646eeSThomas Gleixner 	.read		= cstate_pmu_event_update,
436424646eeSThomas Gleixner 	.capabilities	= PERF_PMU_CAP_NO_INTERRUPT,
437424646eeSThomas Gleixner };
438424646eeSThomas Gleixner 
439424646eeSThomas Gleixner static struct pmu cstate_pkg_pmu = {
440424646eeSThomas Gleixner 	.attr_groups	= pkg_attr_groups,
441424646eeSThomas Gleixner 	.name		= "cstate_pkg",
442424646eeSThomas Gleixner 	.task_ctx_nr	= perf_invalid_context,
443424646eeSThomas Gleixner 	.event_init	= cstate_pmu_event_init,
444424646eeSThomas Gleixner 	.add		= cstate_pmu_event_add,
445424646eeSThomas Gleixner 	.del		= cstate_pmu_event_del,
446424646eeSThomas Gleixner 	.start		= cstate_pmu_event_start,
447424646eeSThomas Gleixner 	.stop		= cstate_pmu_event_stop,
448424646eeSThomas Gleixner 	.read		= cstate_pmu_event_update,
449424646eeSThomas Gleixner 	.capabilities	= PERF_PMU_CAP_NO_INTERRUPT,
450424646eeSThomas Gleixner };
451424646eeSThomas Gleixner 
452424646eeSThomas Gleixner static const struct cstate_model nhm_cstates __initconst = {
453424646eeSThomas Gleixner 	.core_events		= BIT(PERF_CSTATE_CORE_C3_RES) |
454424646eeSThomas Gleixner 				  BIT(PERF_CSTATE_CORE_C6_RES),
455424646eeSThomas Gleixner 
456424646eeSThomas Gleixner 	.pkg_events		= BIT(PERF_CSTATE_PKG_C3_RES) |
457424646eeSThomas Gleixner 				  BIT(PERF_CSTATE_PKG_C6_RES) |
458424646eeSThomas Gleixner 				  BIT(PERF_CSTATE_PKG_C7_RES),
459424646eeSThomas Gleixner };
460424646eeSThomas Gleixner 
461424646eeSThomas Gleixner static const struct cstate_model snb_cstates __initconst = {
462424646eeSThomas Gleixner 	.core_events		= BIT(PERF_CSTATE_CORE_C3_RES) |
463424646eeSThomas Gleixner 				  BIT(PERF_CSTATE_CORE_C6_RES) |
464424646eeSThomas Gleixner 				  BIT(PERF_CSTATE_CORE_C7_RES),
465424646eeSThomas Gleixner 
466424646eeSThomas Gleixner 	.pkg_events		= BIT(PERF_CSTATE_PKG_C2_RES) |
467424646eeSThomas Gleixner 				  BIT(PERF_CSTATE_PKG_C3_RES) |
468424646eeSThomas Gleixner 				  BIT(PERF_CSTATE_PKG_C6_RES) |
469424646eeSThomas Gleixner 				  BIT(PERF_CSTATE_PKG_C7_RES),
470424646eeSThomas Gleixner };
471424646eeSThomas Gleixner 
472424646eeSThomas Gleixner static const struct cstate_model hswult_cstates __initconst = {
473424646eeSThomas Gleixner 	.core_events		= BIT(PERF_CSTATE_CORE_C3_RES) |
474424646eeSThomas Gleixner 				  BIT(PERF_CSTATE_CORE_C6_RES) |
475424646eeSThomas Gleixner 				  BIT(PERF_CSTATE_CORE_C7_RES),
476424646eeSThomas Gleixner 
477424646eeSThomas Gleixner 	.pkg_events		= BIT(PERF_CSTATE_PKG_C2_RES) |
478424646eeSThomas Gleixner 				  BIT(PERF_CSTATE_PKG_C3_RES) |
479424646eeSThomas Gleixner 				  BIT(PERF_CSTATE_PKG_C6_RES) |
480424646eeSThomas Gleixner 				  BIT(PERF_CSTATE_PKG_C7_RES) |
481424646eeSThomas Gleixner 				  BIT(PERF_CSTATE_PKG_C8_RES) |
482424646eeSThomas Gleixner 				  BIT(PERF_CSTATE_PKG_C9_RES) |
483424646eeSThomas Gleixner 				  BIT(PERF_CSTATE_PKG_C10_RES),
484424646eeSThomas Gleixner };
485424646eeSThomas Gleixner 
486424646eeSThomas Gleixner static const struct cstate_model slm_cstates __initconst = {
487424646eeSThomas Gleixner 	.core_events		= BIT(PERF_CSTATE_CORE_C1_RES) |
488424646eeSThomas Gleixner 				  BIT(PERF_CSTATE_CORE_C6_RES),
489424646eeSThomas Gleixner 
490424646eeSThomas Gleixner 	.pkg_events		= BIT(PERF_CSTATE_PKG_C6_RES),
491424646eeSThomas Gleixner 	.quirks			= SLM_PKG_C6_USE_C7_MSR,
492424646eeSThomas Gleixner };
493424646eeSThomas Gleixner 
494889882bcSLukasz Odzioba 
495889882bcSLukasz Odzioba static const struct cstate_model knl_cstates __initconst = {
496889882bcSLukasz Odzioba 	.core_events		= BIT(PERF_CSTATE_CORE_C6_RES),
497889882bcSLukasz Odzioba 
498889882bcSLukasz Odzioba 	.pkg_events		= BIT(PERF_CSTATE_PKG_C2_RES) |
499889882bcSLukasz Odzioba 				  BIT(PERF_CSTATE_PKG_C3_RES) |
500889882bcSLukasz Odzioba 				  BIT(PERF_CSTATE_PKG_C6_RES),
501889882bcSLukasz Odzioba 	.quirks			= KNL_CORE_C6_MSR,
502889882bcSLukasz Odzioba };
503889882bcSLukasz Odzioba 
504889882bcSLukasz Odzioba 
505889882bcSLukasz Odzioba 
506424646eeSThomas Gleixner #define X86_CSTATES_MODEL(model, states)				\
507424646eeSThomas Gleixner 	{ X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, (unsigned long) &(states) }
508424646eeSThomas Gleixner 
509424646eeSThomas Gleixner static const struct x86_cpu_id intel_cstates_match[] __initconst = {
510bf4ad541SDave Hansen 	X86_CSTATES_MODEL(INTEL_FAM6_NEHALEM,    nhm_cstates),
511bf4ad541SDave Hansen 	X86_CSTATES_MODEL(INTEL_FAM6_NEHALEM_EP, nhm_cstates),
512bf4ad541SDave Hansen 	X86_CSTATES_MODEL(INTEL_FAM6_NEHALEM_EX, nhm_cstates),
513424646eeSThomas Gleixner 
514bf4ad541SDave Hansen 	X86_CSTATES_MODEL(INTEL_FAM6_WESTMERE,    nhm_cstates),
515bf4ad541SDave Hansen 	X86_CSTATES_MODEL(INTEL_FAM6_WESTMERE_EP, nhm_cstates),
516bf4ad541SDave Hansen 	X86_CSTATES_MODEL(INTEL_FAM6_WESTMERE_EX, nhm_cstates),
517424646eeSThomas Gleixner 
518bf4ad541SDave Hansen 	X86_CSTATES_MODEL(INTEL_FAM6_SANDYBRIDGE,   snb_cstates),
519bf4ad541SDave Hansen 	X86_CSTATES_MODEL(INTEL_FAM6_SANDYBRIDGE_X, snb_cstates),
520424646eeSThomas Gleixner 
521bf4ad541SDave Hansen 	X86_CSTATES_MODEL(INTEL_FAM6_IVYBRIDGE,   snb_cstates),
522bf4ad541SDave Hansen 	X86_CSTATES_MODEL(INTEL_FAM6_IVYBRIDGE_X, snb_cstates),
523424646eeSThomas Gleixner 
524bf4ad541SDave Hansen 	X86_CSTATES_MODEL(INTEL_FAM6_HASWELL_CORE, snb_cstates),
525bf4ad541SDave Hansen 	X86_CSTATES_MODEL(INTEL_FAM6_HASWELL_X,	   snb_cstates),
526bf4ad541SDave Hansen 	X86_CSTATES_MODEL(INTEL_FAM6_HASWELL_GT3E, snb_cstates),
527424646eeSThomas Gleixner 
528bf4ad541SDave Hansen 	X86_CSTATES_MODEL(INTEL_FAM6_HASWELL_ULT, hswult_cstates),
529424646eeSThomas Gleixner 
530bf4ad541SDave Hansen 	X86_CSTATES_MODEL(INTEL_FAM6_ATOM_SILVERMONT1, slm_cstates),
531bf4ad541SDave Hansen 	X86_CSTATES_MODEL(INTEL_FAM6_ATOM_SILVERMONT2, slm_cstates),
532bf4ad541SDave Hansen 	X86_CSTATES_MODEL(INTEL_FAM6_ATOM_AIRMONT,     slm_cstates),
533424646eeSThomas Gleixner 
534bf4ad541SDave Hansen 	X86_CSTATES_MODEL(INTEL_FAM6_BROADWELL_CORE,   snb_cstates),
535bf4ad541SDave Hansen 	X86_CSTATES_MODEL(INTEL_FAM6_BROADWELL_XEON_D, snb_cstates),
536bf4ad541SDave Hansen 	X86_CSTATES_MODEL(INTEL_FAM6_BROADWELL_GT3E,   snb_cstates),
537bf4ad541SDave Hansen 	X86_CSTATES_MODEL(INTEL_FAM6_BROADWELL_X,      snb_cstates),
538424646eeSThomas Gleixner 
539bf4ad541SDave Hansen 	X86_CSTATES_MODEL(INTEL_FAM6_SKYLAKE_MOBILE,  snb_cstates),
540bf4ad541SDave Hansen 	X86_CSTATES_MODEL(INTEL_FAM6_SKYLAKE_DESKTOP, snb_cstates),
541889882bcSLukasz Odzioba 
542889882bcSLukasz Odzioba 	X86_CSTATES_MODEL(INTEL_FAM6_XEON_PHI_KNL, knl_cstates),
543*1dba23b1SPiotr Luc 	X86_CSTATES_MODEL(INTEL_FAM6_XEON_PHI_KNM, knl_cstates),
544424646eeSThomas Gleixner 	{ },
545424646eeSThomas Gleixner };
546424646eeSThomas Gleixner MODULE_DEVICE_TABLE(x86cpu, intel_cstates_match);
547424646eeSThomas Gleixner 
5486aec1ad7SBorislav Petkov /*
5496aec1ad7SBorislav Petkov  * Probe the cstate events and insert the available one into sysfs attrs
550424646eeSThomas Gleixner  * Return false if there are no available events.
5516aec1ad7SBorislav Petkov  */
552424646eeSThomas Gleixner static bool __init cstate_probe_msr(const unsigned long evmsk, int max,
553424646eeSThomas Gleixner                                    struct perf_cstate_msr *msr,
554424646eeSThomas Gleixner                                    struct attribute **attrs)
5556aec1ad7SBorislav Petkov {
556424646eeSThomas Gleixner 	bool found = false;
557424646eeSThomas Gleixner 	unsigned int bit;
5586aec1ad7SBorislav Petkov 	u64 val;
5596aec1ad7SBorislav Petkov 
560424646eeSThomas Gleixner 	for (bit = 0; bit < max; bit++) {
561424646eeSThomas Gleixner 		if (test_bit(bit, &evmsk) && !rdmsrl_safe(msr[bit].msr, &val)) {
562424646eeSThomas Gleixner 			*attrs++ = &msr[bit].attr->attr.attr;
563424646eeSThomas Gleixner 			found = true;
564424646eeSThomas Gleixner 		} else {
565424646eeSThomas Gleixner 			msr[bit].attr = NULL;
566424646eeSThomas Gleixner 		}
567424646eeSThomas Gleixner 	}
568424646eeSThomas Gleixner 	*attrs = NULL;
569424646eeSThomas Gleixner 
570424646eeSThomas Gleixner 	return found;
5716aec1ad7SBorislav Petkov }
5726aec1ad7SBorislav Petkov 
573424646eeSThomas Gleixner static int __init cstate_probe(const struct cstate_model *cm)
5746aec1ad7SBorislav Petkov {
5756aec1ad7SBorislav Petkov 	/* SLM has different MSR for PKG C6 */
576424646eeSThomas Gleixner 	if (cm->quirks & SLM_PKG_C6_USE_C7_MSR)
5776aec1ad7SBorislav Petkov 		pkg_msr[PERF_CSTATE_PKG_C6_RES].msr = MSR_PKG_C7_RESIDENCY;
5786aec1ad7SBorislav Petkov 
579889882bcSLukasz Odzioba 	/* KNL has different MSR for CORE C6 */
580889882bcSLukasz Odzioba 	if (cm->quirks & KNL_CORE_C6_MSR)
581889882bcSLukasz Odzioba 		pkg_msr[PERF_CSTATE_CORE_C6_RES].msr = MSR_KNL_CORE_C6_RESIDENCY;
582889882bcSLukasz Odzioba 
583889882bcSLukasz Odzioba 
584424646eeSThomas Gleixner 	has_cstate_core = cstate_probe_msr(cm->core_events,
585424646eeSThomas Gleixner 					   PERF_CSTATE_CORE_EVENT_MAX,
586424646eeSThomas Gleixner 					   core_msr, core_events_attrs);
5876aec1ad7SBorislav Petkov 
588424646eeSThomas Gleixner 	has_cstate_pkg = cstate_probe_msr(cm->pkg_events,
589424646eeSThomas Gleixner 					  PERF_CSTATE_PKG_EVENT_MAX,
590424646eeSThomas Gleixner 					  pkg_msr, pkg_events_attrs);
5916aec1ad7SBorislav Petkov 
5926aec1ad7SBorislav Petkov 	return (has_cstate_core || has_cstate_pkg) ? 0 : -ENODEV;
5936aec1ad7SBorislav Petkov }
5946aec1ad7SBorislav Petkov 
595c7afba32SThomas Gleixner static inline void cstate_cleanup(void)
5966aec1ad7SBorislav Petkov {
597d29859e7SThomas Gleixner 	if (has_cstate_core)
598d29859e7SThomas Gleixner 		perf_pmu_unregister(&cstate_core_pmu);
599d29859e7SThomas Gleixner 
600d29859e7SThomas Gleixner 	if (has_cstate_pkg)
601d29859e7SThomas Gleixner 		perf_pmu_unregister(&cstate_pkg_pmu);
602d29859e7SThomas Gleixner }
603d29859e7SThomas Gleixner 
604d29859e7SThomas Gleixner static int __init cstate_init(void)
605d29859e7SThomas Gleixner {
60677c34ef1SSebastian Andrzej Siewior 	int err;
6076aec1ad7SBorislav Petkov 
60877c34ef1SSebastian Andrzej Siewior 	cpuhp_setup_state(CPUHP_AP_PERF_X86_CSTATE_STARTING,
60977c34ef1SSebastian Andrzej Siewior 			  "AP_PERF_X86_CSTATE_STARTING", cstate_cpu_init,
61077c34ef1SSebastian Andrzej Siewior 			  NULL);
61177c34ef1SSebastian Andrzej Siewior 	cpuhp_setup_state(CPUHP_AP_PERF_X86_CSTATE_ONLINE,
61277c34ef1SSebastian Andrzej Siewior 			  "AP_PERF_X86_CSTATE_ONLINE", NULL, cstate_cpu_exit);
6136aec1ad7SBorislav Petkov 
6146aec1ad7SBorislav Petkov 	if (has_cstate_core) {
6156aec1ad7SBorislav Petkov 		err = perf_pmu_register(&cstate_core_pmu, cstate_core_pmu.name, -1);
616d29859e7SThomas Gleixner 		if (err) {
617d29859e7SThomas Gleixner 			has_cstate_core = false;
618d29859e7SThomas Gleixner 			pr_info("Failed to register cstate core pmu\n");
61977c34ef1SSebastian Andrzej Siewior 			return err;
620d29859e7SThomas Gleixner 		}
6216aec1ad7SBorislav Petkov 	}
6226aec1ad7SBorislav Petkov 
6236aec1ad7SBorislav Petkov 	if (has_cstate_pkg) {
6246aec1ad7SBorislav Petkov 		err = perf_pmu_register(&cstate_pkg_pmu, cstate_pkg_pmu.name, -1);
625d29859e7SThomas Gleixner 		if (err) {
626d29859e7SThomas Gleixner 			has_cstate_pkg = false;
627d29859e7SThomas Gleixner 			pr_info("Failed to register cstate pkg pmu\n");
628d29859e7SThomas Gleixner 			cstate_cleanup();
62977c34ef1SSebastian Andrzej Siewior 			return err;
6306aec1ad7SBorislav Petkov 		}
6316aec1ad7SBorislav Petkov 	}
63277c34ef1SSebastian Andrzej Siewior 
633d29859e7SThomas Gleixner 	return err;
634d29859e7SThomas Gleixner }
6356aec1ad7SBorislav Petkov 
6366aec1ad7SBorislav Petkov static int __init cstate_pmu_init(void)
6376aec1ad7SBorislav Petkov {
638424646eeSThomas Gleixner 	const struct x86_cpu_id *id;
6396aec1ad7SBorislav Petkov 	int err;
6406aec1ad7SBorislav Petkov 
641424646eeSThomas Gleixner 	if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
6426aec1ad7SBorislav Petkov 		return -ENODEV;
6436aec1ad7SBorislav Petkov 
644424646eeSThomas Gleixner 	id = x86_match_cpu(intel_cstates_match);
645424646eeSThomas Gleixner 	if (!id)
646424646eeSThomas Gleixner 		return -ENODEV;
647424646eeSThomas Gleixner 
648424646eeSThomas Gleixner 	err = cstate_probe((const struct cstate_model *) id->driver_data);
6496aec1ad7SBorislav Petkov 	if (err)
6506aec1ad7SBorislav Petkov 		return err;
6516aec1ad7SBorislav Petkov 
652d29859e7SThomas Gleixner 	return cstate_init();
6536aec1ad7SBorislav Petkov }
654c7afba32SThomas Gleixner module_init(cstate_pmu_init);
655c7afba32SThomas Gleixner 
656c7afba32SThomas Gleixner static void __exit cstate_pmu_exit(void)
657c7afba32SThomas Gleixner {
65877c34ef1SSebastian Andrzej Siewior 	cpuhp_remove_state_nocalls(CPUHP_AP_PERF_X86_CSTATE_ONLINE);
65977c34ef1SSebastian Andrzej Siewior 	cpuhp_remove_state_nocalls(CPUHP_AP_PERF_X86_CSTATE_STARTING);
660c7afba32SThomas Gleixner 	cstate_cleanup();
661c7afba32SThomas Gleixner }
662c7afba32SThomas Gleixner module_exit(cstate_pmu_exit);
663