xref: /linux/arch/x86/events/intel/cstate.c (revision 42b16d3ac371a2fac9b6f08fd75f23f34ba3955a)
16aec1ad7SBorislav Petkov /*
2940b2f2fSBorislav Petkov  * 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
432da202aaSKan Liang  *			 Available model: SLM,AMT,GLM,CNL,ICX,TNT,ADL,RPL
4426579860SZhang Rui  *					  MTL,SRF,GRR,ARL,LNL
456aec1ad7SBorislav Petkov  *			 Scope: Core (each processor core has a MSR)
466aec1ad7SBorislav Petkov  *	MSR_CORE_C3_RESIDENCY: CORE C3 Residency Counter
476aec1ad7SBorislav Petkov  *			       perf code: 0x01
481159e094SHarry Pan  *			       Available model: NHM,WSM,SNB,IVB,HSW,BDW,SKL,GLM,
49ecf71fbcSKan Liang  *						CNL,KBL,CML,TNT
506aec1ad7SBorislav Petkov  *			       Scope: Core
516aec1ad7SBorislav Petkov  *	MSR_CORE_C6_RESIDENCY: CORE C6 Residency Counter
526aec1ad7SBorislav Petkov  *			       perf code: 0x02
531159e094SHarry Pan  *			       Available model: SLM,AMT,NHM,WSM,SNB,IVB,HSW,BDW,
5487bf399fSZhang Rui  *						SKL,KNL,GLM,CNL,KBL,CML,ICL,ICX,
55bbb96869SKan Liang  *						TGL,TNT,RKL,ADL,RPL,SPR,MTL,SRF,
5626579860SZhang Rui  *						GRR,ARL,LNL
576aec1ad7SBorislav Petkov  *			       Scope: Core
586aec1ad7SBorislav Petkov  *	MSR_CORE_C7_RESIDENCY: CORE C7 Residency Counter
596aec1ad7SBorislav Petkov  *			       perf code: 0x03
60f1857a24SKan Liang  *			       Available model: SNB,IVB,HSW,BDW,SKL,CNL,KBL,CML,
6126579860SZhang Rui  *						ICL,TGL,RKL,ADL,RPL,MTL,ARL,LNL
626aec1ad7SBorislav Petkov  *			       Scope: Core
636aec1ad7SBorislav Petkov  *	MSR_PKG_C2_RESIDENCY:  Package C2 Residency Counter.
646aec1ad7SBorislav Petkov  *			       perf code: 0x00
651ffa6c04SKan Liang  *			       Available model: SNB,IVB,HSW,BDW,SKL,KNL,GLM,CNL,
662da202aaSKan Liang  *						KBL,CML,ICL,ICX,TGL,TNT,RKL,ADL,
67*b1d0e15cSZhenyu Wang  *						RPL,SPR,MTL,ARL,LNL,SRF
686aec1ad7SBorislav Petkov  *			       Scope: Package (physical package)
696aec1ad7SBorislav Petkov  *	MSR_PKG_C3_RESIDENCY:  Package C3 Residency Counter.
706aec1ad7SBorislav Petkov  *			       perf code: 0x01
711159e094SHarry Pan  *			       Available model: NHM,WSM,SNB,IVB,HSW,BDW,SKL,KNL,
72d0ca946bSKan Liang  *						GLM,CNL,KBL,CML,ICL,TGL,TNT,RKL,
7326579860SZhang Rui  *						ADL,RPL,MTL,ARL,LNL
746aec1ad7SBorislav Petkov  *			       Scope: Package (physical package)
756aec1ad7SBorislav Petkov  *	MSR_PKG_C6_RESIDENCY:  Package C6 Residency Counter.
766aec1ad7SBorislav Petkov  *			       perf code: 0x02
77ecf71fbcSKan Liang  *			       Available model: SLM,AMT,NHM,WSM,SNB,IVB,HSW,BDW,
7887bf399fSZhang Rui  *						SKL,KNL,GLM,CNL,KBL,CML,ICL,ICX,
79a3100075SZhang Rui  *						TGL,TNT,RKL,ADL,RPL,SPR,MTL,SRF,
8026579860SZhang Rui  *						ARL,LNL
816aec1ad7SBorislav Petkov  *			       Scope: Package (physical package)
826aec1ad7SBorislav Petkov  *	MSR_PKG_C7_RESIDENCY:  Package C7 Residency Counter.
836aec1ad7SBorislav Petkov  *			       perf code: 0x03
841ffa6c04SKan Liang  *			       Available model: NHM,WSM,SNB,IVB,HSW,BDW,SKL,CNL,
852c3aedd9SZhang Rui  *						KBL,CML,ICL,TGL,RKL
866aec1ad7SBorislav Petkov  *			       Scope: Package (physical package)
876aec1ad7SBorislav Petkov  *	MSR_PKG_C8_RESIDENCY:  Package C8 Residency Counter.
886aec1ad7SBorislav Petkov  *			       perf code: 0x04
89d0ca946bSKan Liang  *			       Available model: HSW ULT,KBL,CNL,CML,ICL,TGL,RKL,
90a3100075SZhang Rui  *						ADL,RPL,MTL,ARL
916aec1ad7SBorislav Petkov  *			       Scope: Package (physical package)
926aec1ad7SBorislav Petkov  *	MSR_PKG_C9_RESIDENCY:  Package C9 Residency Counter.
936aec1ad7SBorislav Petkov  *			       perf code: 0x05
942c3aedd9SZhang Rui  *			       Available model: HSW ULT,KBL,CNL,CML,ICL,TGL,RKL
956aec1ad7SBorislav Petkov  *			       Scope: Package (physical package)
966aec1ad7SBorislav Petkov  *	MSR_PKG_C10_RESIDENCY: Package C10 Residency Counter.
976aec1ad7SBorislav Petkov  *			       perf code: 0x06
98ecf71fbcSKan Liang  *			       Available model: HSW ULT,KBL,GLM,CNL,CML,ICL,TGL,
9926579860SZhang Rui  *						TNT,RKL,ADL,RPL,MTL,ARL,LNL
1006aec1ad7SBorislav Petkov  *			       Scope: Package (physical package)
1013877d55aSKan Liang  *	MSR_MODULE_C6_RES_MS:  Module C6 Residency Counter.
1023877d55aSKan Liang  *			       perf code: 0x00
103bbb96869SKan Liang  *			       Available model: SRF,GRR
1043877d55aSKan Liang  *			       Scope: A cluster of cores shared L2 cache
1056aec1ad7SBorislav Petkov  *
1066aec1ad7SBorislav Petkov  */
1076aec1ad7SBorislav Petkov 
1086aec1ad7SBorislav Petkov #include <linux/module.h>
1096aec1ad7SBorislav Petkov #include <linux/slab.h>
1106aec1ad7SBorislav Petkov #include <linux/perf_event.h>
111a5f81290SPeter Zijlstra #include <linux/nospec.h>
1126aec1ad7SBorislav Petkov #include <asm/cpu_device_id.h>
113bf4ad541SDave Hansen #include <asm/intel-family.h>
11427f6d22bSBorislav Petkov #include "../perf_event.h"
1158f2a28c5SJiri Olsa #include "../probe.h"
1166aec1ad7SBorislav Petkov 
117dc8e5dfbSJeff Johnson MODULE_DESCRIPTION("Support for Intel cstate performance events");
118c7afba32SThomas Gleixner MODULE_LICENSE("GPL");
119c7afba32SThomas Gleixner 
1206aec1ad7SBorislav Petkov #define DEFINE_CSTATE_FORMAT_ATTR(_var, _name, _format)		\
121ebd19fc3SSami Tolvanen static ssize_t __cstate_##_var##_show(struct device *dev,	\
122ebd19fc3SSami Tolvanen 				struct device_attribute *attr,	\
1236aec1ad7SBorislav Petkov 				char *page)			\
1246aec1ad7SBorislav Petkov {								\
1256aec1ad7SBorislav Petkov 	BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE);		\
1266aec1ad7SBorislav Petkov 	return sprintf(page, _format "\n");			\
1276aec1ad7SBorislav Petkov }								\
128ebd19fc3SSami Tolvanen static struct device_attribute format_attr_##_var =		\
1296aec1ad7SBorislav Petkov 	__ATTR(_name, 0444, __cstate_##_var##_show, NULL)
1306aec1ad7SBorislav Petkov 
1316aec1ad7SBorislav Petkov /* Model -> events mapping */
1326aec1ad7SBorislav Petkov struct cstate_model {
1336aec1ad7SBorislav Petkov 	unsigned long		core_events;
1346aec1ad7SBorislav Petkov 	unsigned long		pkg_events;
135424646eeSThomas Gleixner 	unsigned long		module_events;
136424646eeSThomas Gleixner 	unsigned long		quirks;
137424646eeSThomas Gleixner };
138424646eeSThomas Gleixner 
1393877d55aSKan Liang /* Quirk flags */
140424646eeSThomas Gleixner #define SLM_PKG_C6_USE_C7_MSR	(1UL << 0)
141424646eeSThomas Gleixner #define KNL_CORE_C6_MSR		(1UL << 1)
142424646eeSThomas Gleixner 
143424646eeSThomas Gleixner /* cstate_core PMU */
144424646eeSThomas Gleixner static struct pmu cstate_core_pmu;
145889882bcSLukasz Odzioba static bool has_cstate_core;
146424646eeSThomas Gleixner 
1476aec1ad7SBorislav Petkov enum perf_cstate_core_events {
1486aec1ad7SBorislav Petkov 	PERF_CSTATE_CORE_C1_RES = 0,
1496aec1ad7SBorislav Petkov 	PERF_CSTATE_CORE_C3_RES,
1506aec1ad7SBorislav Petkov 	PERF_CSTATE_CORE_C6_RES,
151424646eeSThomas Gleixner 	PERF_CSTATE_CORE_C7_RES,
1526aec1ad7SBorislav Petkov 
1536aec1ad7SBorislav Petkov 	PERF_CSTATE_CORE_EVENT_MAX,
1546aec1ad7SBorislav Petkov };
1556aec1ad7SBorislav Petkov 
1566aec1ad7SBorislav Petkov PMU_EVENT_ATTR_STRING(c1-residency, attr_cstate_core_c1, "event=0x00");
1576aec1ad7SBorislav Petkov PMU_EVENT_ATTR_STRING(c3-residency, attr_cstate_core_c3, "event=0x01");
1586aec1ad7SBorislav Petkov PMU_EVENT_ATTR_STRING(c6-residency, attr_cstate_core_c6, "event=0x02");
1596aec1ad7SBorislav Petkov PMU_EVENT_ATTR_STRING(c7-residency, attr_cstate_core_c7, "event=0x03");
1608f2a28c5SJiri Olsa 
1618f2a28c5SJiri Olsa static unsigned long core_msr_mask;
1628f2a28c5SJiri Olsa 
1638f2a28c5SJiri Olsa PMU_EVENT_GROUP(events, cstate_core_c1);
1646aec1ad7SBorislav Petkov PMU_EVENT_GROUP(events, cstate_core_c3);
1658f2a28c5SJiri Olsa PMU_EVENT_GROUP(events, cstate_core_c6);
1668f2a28c5SJiri Olsa PMU_EVENT_GROUP(events, cstate_core_c7);
1678f2a28c5SJiri Olsa 
test_msr(int idx,void * data)1688f2a28c5SJiri Olsa static bool test_msr(int idx, void *data)
1698f2a28c5SJiri Olsa {
1708f2a28c5SJiri Olsa 	return test_bit(idx, (unsigned long *) data);
1718f2a28c5SJiri Olsa }
1728f2a28c5SJiri Olsa 
1738f2a28c5SJiri Olsa static struct perf_msr core_msr[] = {
1748f2a28c5SJiri Olsa 	[PERF_CSTATE_CORE_C1_RES] = { MSR_CORE_C1_RES,		&group_cstate_core_c1,	test_msr },
1758f2a28c5SJiri Olsa 	[PERF_CSTATE_CORE_C3_RES] = { MSR_CORE_C3_RESIDENCY,	&group_cstate_core_c3,	test_msr },
1768f2a28c5SJiri Olsa 	[PERF_CSTATE_CORE_C6_RES] = { MSR_CORE_C6_RESIDENCY,	&group_cstate_core_c6,	test_msr },
1778f2a28c5SJiri Olsa 	[PERF_CSTATE_CORE_C7_RES] = { MSR_CORE_C7_RESIDENCY,	&group_cstate_core_c7,	test_msr },
1788f2a28c5SJiri Olsa };
1798f2a28c5SJiri Olsa 
1808f2a28c5SJiri Olsa static struct attribute *attrs_empty[] = {
1818f2a28c5SJiri Olsa 	NULL,
1826aec1ad7SBorislav Petkov };
1836aec1ad7SBorislav Petkov 
1848f2a28c5SJiri Olsa /*
1856aec1ad7SBorislav Petkov  * There are no default events, but we need to create
1866aec1ad7SBorislav Petkov  * "events" group (with empty attrs) before updating
1876aec1ad7SBorislav Petkov  * it with detected events.
1888f2a28c5SJiri Olsa  */
1898f2a28c5SJiri Olsa static struct attribute_group cstate_events_attr_group = {
1908f2a28c5SJiri Olsa 	.name = "events",
1918f2a28c5SJiri Olsa 	.attrs = attrs_empty,
1928f2a28c5SJiri Olsa };
193243218caSKan Liang 
1946aec1ad7SBorislav Petkov DEFINE_CSTATE_FORMAT_ATTR(cstate_event, event, "config:0-63");
1958f2a28c5SJiri Olsa static struct attribute *cstate_format_attrs[] = {
1966aec1ad7SBorislav Petkov 	&format_attr_cstate_event.attr,
1976aec1ad7SBorislav Petkov 	NULL,
198243218caSKan Liang };
199243218caSKan Liang 
200243218caSKan Liang static struct attribute_group cstate_format_attr_group = {
2016aec1ad7SBorislav Petkov 	.name = "format",
2026aec1ad7SBorislav Petkov 	.attrs = cstate_format_attrs,
2036aec1ad7SBorislav Petkov };
204243218caSKan Liang 
2056aec1ad7SBorislav Petkov static const struct attribute_group *cstate_attr_groups[] = {
206243218caSKan Liang 	&cstate_events_attr_group,
2076aec1ad7SBorislav Petkov 	&cstate_format_attr_group,
2086aec1ad7SBorislav Petkov 	NULL,
2096aec1ad7SBorislav Petkov };
2106aec1ad7SBorislav Petkov 
2116aec1ad7SBorislav Petkov /* cstate_pkg PMU */
2126aec1ad7SBorislav Petkov static struct pmu cstate_pkg_pmu;
2136aec1ad7SBorislav Petkov static bool has_cstate_pkg;
2146aec1ad7SBorislav Petkov 
2156aec1ad7SBorislav Petkov enum perf_cstate_pkg_events {
2166aec1ad7SBorislav Petkov 	PERF_CSTATE_PKG_C2_RES = 0,
2176aec1ad7SBorislav Petkov 	PERF_CSTATE_PKG_C3_RES,
2186aec1ad7SBorislav Petkov 	PERF_CSTATE_PKG_C6_RES,
2196aec1ad7SBorislav Petkov 	PERF_CSTATE_PKG_C7_RES,
2206aec1ad7SBorislav Petkov 	PERF_CSTATE_PKG_C8_RES,
221243218caSKan Liang 	PERF_CSTATE_PKG_C9_RES,
222243218caSKan Liang 	PERF_CSTATE_PKG_C10_RES,
223243218caSKan Liang 
2246aec1ad7SBorislav Petkov 	PERF_CSTATE_PKG_EVENT_MAX,
2256aec1ad7SBorislav Petkov };
2266aec1ad7SBorislav Petkov 
2276aec1ad7SBorislav Petkov PMU_EVENT_ATTR_STRING(c2-residency,  attr_cstate_pkg_c2,  "event=0x00");
2286aec1ad7SBorislav Petkov PMU_EVENT_ATTR_STRING(c3-residency,  attr_cstate_pkg_c3,  "event=0x01");
2296aec1ad7SBorislav Petkov PMU_EVENT_ATTR_STRING(c6-residency,  attr_cstate_pkg_c6,  "event=0x02");
2306aec1ad7SBorislav Petkov PMU_EVENT_ATTR_STRING(c7-residency,  attr_cstate_pkg_c7,  "event=0x03");
2316aec1ad7SBorislav Petkov PMU_EVENT_ATTR_STRING(c8-residency,  attr_cstate_pkg_c8,  "event=0x04");
232424646eeSThomas Gleixner PMU_EVENT_ATTR_STRING(c9-residency,  attr_cstate_pkg_c9,  "event=0x05");
2336aec1ad7SBorislav Petkov PMU_EVENT_ATTR_STRING(c10-residency, attr_cstate_pkg_c10, "event=0x06");
2346aec1ad7SBorislav Petkov 
2356aec1ad7SBorislav Petkov static unsigned long pkg_msr_mask;
2366aec1ad7SBorislav Petkov 
2376aec1ad7SBorislav Petkov PMU_EVENT_GROUP(events, cstate_pkg_c2);
2386aec1ad7SBorislav Petkov PMU_EVENT_GROUP(events, cstate_pkg_c3);
2396aec1ad7SBorislav Petkov PMU_EVENT_GROUP(events, cstate_pkg_c6);
2406aec1ad7SBorislav Petkov PMU_EVENT_GROUP(events, cstate_pkg_c7);
2416aec1ad7SBorislav Petkov PMU_EVENT_GROUP(events, cstate_pkg_c8);
2426aec1ad7SBorislav Petkov PMU_EVENT_GROUP(events, cstate_pkg_c9);
2436aec1ad7SBorislav Petkov PMU_EVENT_GROUP(events, cstate_pkg_c10);
2448f2a28c5SJiri Olsa 
2458f2a28c5SJiri Olsa static struct perf_msr pkg_msr[] = {
2468f2a28c5SJiri Olsa 	[PERF_CSTATE_PKG_C2_RES]  = { MSR_PKG_C2_RESIDENCY,	&group_cstate_pkg_c2,	test_msr },
2478f2a28c5SJiri Olsa 	[PERF_CSTATE_PKG_C3_RES]  = { MSR_PKG_C3_RESIDENCY,	&group_cstate_pkg_c3,	test_msr },
2488f2a28c5SJiri Olsa 	[PERF_CSTATE_PKG_C6_RES]  = { MSR_PKG_C6_RESIDENCY,	&group_cstate_pkg_c6,	test_msr },
2498f2a28c5SJiri Olsa 	[PERF_CSTATE_PKG_C7_RES]  = { MSR_PKG_C7_RESIDENCY,	&group_cstate_pkg_c7,	test_msr },
2508f2a28c5SJiri Olsa 	[PERF_CSTATE_PKG_C8_RES]  = { MSR_PKG_C8_RESIDENCY,	&group_cstate_pkg_c8,	test_msr },
2516aec1ad7SBorislav Petkov 	[PERF_CSTATE_PKG_C9_RES]  = { MSR_PKG_C9_RESIDENCY,	&group_cstate_pkg_c9,	test_msr },
2528f2a28c5SJiri Olsa 	[PERF_CSTATE_PKG_C10_RES] = { MSR_PKG_C10_RESIDENCY,	&group_cstate_pkg_c10,	test_msr },
2536aec1ad7SBorislav Petkov };
2548f2a28c5SJiri Olsa 
2558f2a28c5SJiri Olsa /* cstate_module PMU */
2568f2a28c5SJiri Olsa static struct pmu cstate_module_pmu;
2578f2a28c5SJiri Olsa static bool has_cstate_module;
2588f2a28c5SJiri Olsa 
2598f2a28c5SJiri Olsa enum perf_cstate_module_events {
2608f2a28c5SJiri Olsa 	PERF_CSTATE_MODULE_C6_RES = 0,
2618f2a28c5SJiri Olsa 
2628f2a28c5SJiri Olsa 	PERF_CSTATE_MODULE_EVENT_MAX,
2638f2a28c5SJiri Olsa };
2648f2a28c5SJiri Olsa 
2658f2a28c5SJiri Olsa PMU_EVENT_ATTR_STRING(c6-residency, attr_cstate_module_c6, "event=0x00");
2668f2a28c5SJiri Olsa 
2678f2a28c5SJiri Olsa static unsigned long module_msr_mask;
2688f2a28c5SJiri Olsa 
2698f2a28c5SJiri Olsa PMU_EVENT_GROUP(events, cstate_module_c6);
2706aec1ad7SBorislav Petkov 
2716aec1ad7SBorislav Petkov static struct perf_msr module_msr[] = {
2726aec1ad7SBorislav Petkov 	[PERF_CSTATE_MODULE_C6_RES]  = { MSR_MODULE_C6_RES_MS,	&group_cstate_module_c6,	test_msr },
2736aec1ad7SBorislav Petkov };
2743877d55aSKan Liang 
cstate_pmu_event_init(struct perf_event * event)2753877d55aSKan Liang static int cstate_pmu_event_init(struct perf_event *event)
2763877d55aSKan Liang {
2773877d55aSKan Liang 	u64 cfg = event->attr.config;
2783877d55aSKan Liang 
2793877d55aSKan Liang 	if (event->attr.type != event->pmu->type)
2803877d55aSKan Liang 		return -ENOENT;
2813877d55aSKan Liang 
2823877d55aSKan Liang 	/* unsupported modes and filters */
2833877d55aSKan Liang 	if (event->attr.sample_period) /* no sampling */
2843877d55aSKan Liang 		return -EINVAL;
2853877d55aSKan Liang 
2863877d55aSKan Liang 	if (event->cpu < 0)
2873877d55aSKan Liang 		return -EINVAL;
2883877d55aSKan Liang 
2893877d55aSKan Liang 	if (event->pmu == &cstate_core_pmu) {
2903877d55aSKan Liang 		if (cfg >= PERF_CSTATE_CORE_EVENT_MAX)
2913877d55aSKan Liang 			return -EINVAL;
2923877d55aSKan Liang 		cfg = array_index_nospec((unsigned long)cfg, PERF_CSTATE_CORE_EVENT_MAX);
2933877d55aSKan Liang 		if (!(core_msr_mask & (1 << cfg)))
2943877d55aSKan Liang 			return -EINVAL;
2953877d55aSKan Liang 		event->hw.event_base = core_msr[cfg].msr;
2966aec1ad7SBorislav Petkov 	} else if (event->pmu == &cstate_pkg_pmu) {
2976aec1ad7SBorislav Petkov 		if (cfg >= PERF_CSTATE_PKG_EVENT_MAX)
2986aec1ad7SBorislav Petkov 			return -EINVAL;
2996aec1ad7SBorislav Petkov 		cfg = array_index_nospec((unsigned long)cfg, PERF_CSTATE_PKG_EVENT_MAX);
3006aec1ad7SBorislav Petkov 		if (!(pkg_msr_mask & (1 << cfg)))
3016aec1ad7SBorislav Petkov 			return -EINVAL;
3026aec1ad7SBorislav Petkov 		event->hw.event_base = pkg_msr[cfg].msr;
3036aec1ad7SBorislav Petkov 	} else if (event->pmu == &cstate_module_pmu) {
3046aec1ad7SBorislav Petkov 		if (cfg >= PERF_CSTATE_MODULE_EVENT_MAX)
3056aec1ad7SBorislav Petkov 			return -EINVAL;
3063877d55aSKan Liang 		cfg = array_index_nospec((unsigned long)cfg, PERF_CSTATE_MODULE_EVENT_MAX);
3073877d55aSKan Liang 		if (!(module_msr_mask & (1 << cfg)))
3086aec1ad7SBorislav Petkov 			return -EINVAL;
3096aec1ad7SBorislav Petkov 		event->hw.event_base = module_msr[cfg].msr;
3106aec1ad7SBorislav Petkov 	} else {
3116aec1ad7SBorislav Petkov 		return -ENOENT;
3126aec1ad7SBorislav Petkov 	}
3136aec1ad7SBorislav Petkov 
3146aec1ad7SBorislav Petkov 	event->hw.config = cfg;
31549de0493SThomas Gleixner 	event->hw.idx = -1;
3166aec1ad7SBorislav Petkov 	return 0;
3176aec1ad7SBorislav Petkov }
3186aec1ad7SBorislav Petkov 
cstate_pmu_read_counter(struct perf_event * event)3196aec1ad7SBorislav Petkov static inline u64 cstate_pmu_read_counter(struct perf_event *event)
3206aec1ad7SBorislav Petkov {
3212ff40250SAndrew Murray 	u64 val;
3226aec1ad7SBorislav Petkov 
3236aec1ad7SBorislav Petkov 	rdmsrl(event->hw.event_base, val);
32449de0493SThomas Gleixner 	return val;
32549de0493SThomas Gleixner }
32649de0493SThomas Gleixner 
cstate_pmu_event_update(struct perf_event * event)3276aec1ad7SBorislav Petkov static void cstate_pmu_event_update(struct perf_event *event)
3286aec1ad7SBorislav Petkov {
3296aec1ad7SBorislav Petkov 	struct hw_perf_event *hwc = &event->hw;
3308f2a28c5SJiri Olsa 	u64 prev_raw_count, new_raw_count;
3318f2a28c5SJiri Olsa 
3326aec1ad7SBorislav Petkov 	prev_raw_count = local64_read(&hwc->prev_count);
3336aec1ad7SBorislav Petkov 	do {
33449de0493SThomas Gleixner 		new_raw_count = cstate_pmu_read_counter(event);
33549de0493SThomas Gleixner 	} while (!local64_try_cmpxchg(&hwc->prev_count,
3366aec1ad7SBorislav Petkov 				      &prev_raw_count, new_raw_count));
3376aec1ad7SBorislav Petkov 
3386aec1ad7SBorislav Petkov 	local64_add(new_raw_count - prev_raw_count, &event->count);
339a5f81290SPeter Zijlstra }
3408f2a28c5SJiri Olsa 
cstate_pmu_event_start(struct perf_event * event,int mode)3416aec1ad7SBorislav Petkov static void cstate_pmu_event_start(struct perf_event *event, int mode)
34205276d48STero Kristo {
34305276d48STero Kristo 	local64_set(&event->hw.prev_count, cstate_pmu_read_counter(event));
34405276d48STero Kristo }
3456aec1ad7SBorislav Petkov 
cstate_pmu_event_stop(struct perf_event * event,int mode)34649de0493SThomas Gleixner static void cstate_pmu_event_stop(struct perf_event *event, int mode)
347cb63ba0fSKan Liang {
3483877d55aSKan Liang 	cstate_pmu_event_update(event);
3493877d55aSKan Liang }
3503877d55aSKan Liang 
cstate_pmu_event_del(struct perf_event * event,int mode)3513877d55aSKan Liang static void cstate_pmu_event_del(struct perf_event *event, int mode)
3523877d55aSKan Liang {
3533877d55aSKan Liang 	cstate_pmu_event_stop(event, PERF_EF_UPDATE);
3543877d55aSKan Liang }
3553877d55aSKan Liang 
cstate_pmu_event_add(struct perf_event * event,int mode)3563877d55aSKan Liang static int cstate_pmu_event_add(struct perf_event *event, int mode)
35749de0493SThomas Gleixner {
3586aec1ad7SBorislav Petkov 	if (mode & PERF_EF_START)
35949de0493SThomas Gleixner 		cstate_pmu_event_start(event, mode);
3606aec1ad7SBorislav Petkov 
36149de0493SThomas Gleixner 	return 0;
36249de0493SThomas Gleixner }
36349de0493SThomas Gleixner 
36449de0493SThomas Gleixner static const struct attribute_group *core_attr_update[] = {
3656aec1ad7SBorislav Petkov 	&group_cstate_core_c1,
3666aec1ad7SBorislav Petkov 	&group_cstate_core_c3,
36749de0493SThomas Gleixner 	&group_cstate_core_c6,
3686aec1ad7SBorislav Petkov 	&group_cstate_core_c7,
3696aec1ad7SBorislav Petkov 	NULL,
3706aec1ad7SBorislav Petkov };
3716aec1ad7SBorislav Petkov 
3726aec1ad7SBorislav Petkov static const struct attribute_group *pkg_attr_update[] = {
3736aec1ad7SBorislav Petkov 	&group_cstate_pkg_c2,
3746aec1ad7SBorislav Petkov 	&group_cstate_pkg_c3,
3756aec1ad7SBorislav Petkov 	&group_cstate_pkg_c6,
3766aec1ad7SBorislav Petkov 	&group_cstate_pkg_c7,
3776aec1ad7SBorislav Petkov 	&group_cstate_pkg_c8,
3786aec1ad7SBorislav Petkov 	&group_cstate_pkg_c9,
3796aec1ad7SBorislav Petkov 	&group_cstate_pkg_c10,
3806aec1ad7SBorislav Petkov 	NULL,
3816aec1ad7SBorislav Petkov };
3826aec1ad7SBorislav Petkov 
3836aec1ad7SBorislav Petkov static const struct attribute_group *module_attr_update[] = {
3844c1c9deaSUros Bizjak 	&group_cstate_module_c6,
3856aec1ad7SBorislav Petkov 	NULL
3864c1c9deaSUros Bizjak };
3874c1c9deaSUros Bizjak 
3886aec1ad7SBorislav Petkov static struct pmu cstate_core_pmu = {
3896aec1ad7SBorislav Petkov 	.attr_groups	= cstate_attr_groups,
3906aec1ad7SBorislav Petkov 	.attr_update	= core_attr_update,
3916aec1ad7SBorislav Petkov 	.name		= "cstate_core",
3926aec1ad7SBorislav Petkov 	.task_ctx_nr	= perf_invalid_context,
3936aec1ad7SBorislav Petkov 	.event_init	= cstate_pmu_event_init,
3946aec1ad7SBorislav Petkov 	.add		= cstate_pmu_event_add,
3956aec1ad7SBorislav Petkov 	.del		= cstate_pmu_event_del,
3966aec1ad7SBorislav Petkov 	.start		= cstate_pmu_event_start,
3976aec1ad7SBorislav Petkov 	.stop		= cstate_pmu_event_stop,
3986aec1ad7SBorislav Petkov 	.read		= cstate_pmu_event_update,
3996aec1ad7SBorislav Petkov 	.capabilities	= PERF_PMU_CAP_NO_INTERRUPT | PERF_PMU_CAP_NO_EXCLUDE,
4006aec1ad7SBorislav Petkov 	.scope		= PERF_PMU_SCOPE_CORE,
4016aec1ad7SBorislav Petkov 	.module		= THIS_MODULE,
4026aec1ad7SBorislav Petkov };
4036aec1ad7SBorislav Petkov 
4046aec1ad7SBorislav Petkov static struct pmu cstate_pkg_pmu = {
4056aec1ad7SBorislav Petkov 	.attr_groups	= cstate_attr_groups,
4066aec1ad7SBorislav Petkov 	.attr_update	= pkg_attr_update,
4076aec1ad7SBorislav Petkov 	.name		= "cstate_pkg",
4086aec1ad7SBorislav Petkov 	.task_ctx_nr	= perf_invalid_context,
4096aec1ad7SBorislav Petkov 	.event_init	= cstate_pmu_event_init,
4106aec1ad7SBorislav Petkov 	.add		= cstate_pmu_event_add,
4116aec1ad7SBorislav Petkov 	.del		= cstate_pmu_event_del,
4126aec1ad7SBorislav Petkov 	.start		= cstate_pmu_event_start,
4136aec1ad7SBorislav Petkov 	.stop		= cstate_pmu_event_stop,
4146aec1ad7SBorislav Petkov 	.read		= cstate_pmu_event_update,
41549de0493SThomas Gleixner 	.capabilities	= PERF_PMU_CAP_NO_INTERRUPT | PERF_PMU_CAP_NO_EXCLUDE,
41649de0493SThomas Gleixner 	.scope		= PERF_PMU_SCOPE_PKG,
41749de0493SThomas Gleixner 	.module		= THIS_MODULE,
41849de0493SThomas Gleixner };
41977c34ef1SSebastian Andrzej Siewior 
4206aec1ad7SBorislav Petkov static struct pmu cstate_module_pmu = {
42149de0493SThomas Gleixner 	.attr_groups	= cstate_attr_groups,
4226aec1ad7SBorislav Petkov 	.attr_update	= module_attr_update,
42349de0493SThomas Gleixner 	.name		= "cstate_module",
42449de0493SThomas Gleixner 	.task_ctx_nr	= perf_invalid_context,
4256aec1ad7SBorislav Petkov 	.event_init	= cstate_pmu_event_init,
42649de0493SThomas Gleixner 	.add		= cstate_pmu_event_add,
42749de0493SThomas Gleixner 	.del		= cstate_pmu_event_del,
42849de0493SThomas Gleixner 	.start		= cstate_pmu_event_start,
4296aec1ad7SBorislav Petkov 	.stop		= cstate_pmu_event_stop,
4306aec1ad7SBorislav Petkov 	.read		= cstate_pmu_event_update,
4316aec1ad7SBorislav Petkov 	.capabilities	= PERF_PMU_CAP_NO_INTERRUPT | PERF_PMU_CAP_NO_EXCLUDE,
4326aec1ad7SBorislav Petkov 	.scope		= PERF_PMU_SCOPE_CLUSTER,
43349de0493SThomas Gleixner 	.module		= THIS_MODULE,
43449de0493SThomas Gleixner };
43549de0493SThomas Gleixner 
43649de0493SThomas Gleixner static const struct cstate_model nhm_cstates __initconst = {
437cb63ba0fSKan Liang 	.core_events		= BIT(PERF_CSTATE_CORE_C3_RES) |
43849de0493SThomas Gleixner 				  BIT(PERF_CSTATE_CORE_C6_RES),
43949de0493SThomas Gleixner 
4406aec1ad7SBorislav Petkov 	.pkg_events		= BIT(PERF_CSTATE_PKG_C3_RES) |
4416aec1ad7SBorislav Petkov 				  BIT(PERF_CSTATE_PKG_C6_RES) |
4426aec1ad7SBorislav Petkov 				  BIT(PERF_CSTATE_PKG_C7_RES),
4436aec1ad7SBorislav Petkov };
4443877d55aSKan Liang 
4453877d55aSKan Liang static const struct cstate_model snb_cstates __initconst = {
4463877d55aSKan Liang 	.core_events		= BIT(PERF_CSTATE_CORE_C3_RES) |
4473877d55aSKan Liang 				  BIT(PERF_CSTATE_CORE_C6_RES) |
4483877d55aSKan Liang 				  BIT(PERF_CSTATE_CORE_C7_RES),
4493877d55aSKan Liang 
4503877d55aSKan Liang 	.pkg_events		= BIT(PERF_CSTATE_PKG_C2_RES) |
4513877d55aSKan Liang 				  BIT(PERF_CSTATE_PKG_C3_RES) |
4523877d55aSKan Liang 				  BIT(PERF_CSTATE_PKG_C6_RES) |
4533877d55aSKan Liang 				  BIT(PERF_CSTATE_PKG_C7_RES),
4543877d55aSKan Liang };
45577c34ef1SSebastian Andrzej Siewior 
45649de0493SThomas Gleixner static const struct cstate_model hswult_cstates __initconst = {
4576aec1ad7SBorislav Petkov 	.core_events		= BIT(PERF_CSTATE_CORE_C3_RES) |
45877c34ef1SSebastian Andrzej Siewior 				  BIT(PERF_CSTATE_CORE_C6_RES) |
4596aec1ad7SBorislav Petkov 				  BIT(PERF_CSTATE_CORE_C7_RES),
46049de0493SThomas Gleixner 
4616aec1ad7SBorislav Petkov 	.pkg_events		= BIT(PERF_CSTATE_PKG_C2_RES) |
46249de0493SThomas Gleixner 				  BIT(PERF_CSTATE_PKG_C3_RES) |
46349de0493SThomas Gleixner 				  BIT(PERF_CSTATE_PKG_C6_RES) |
46449de0493SThomas Gleixner 				  BIT(PERF_CSTATE_PKG_C7_RES) |
46549de0493SThomas Gleixner 				  BIT(PERF_CSTATE_PKG_C8_RES) |
46649de0493SThomas Gleixner 				  BIT(PERF_CSTATE_PKG_C9_RES) |
46749de0493SThomas Gleixner 				  BIT(PERF_CSTATE_PKG_C10_RES),
46849de0493SThomas Gleixner };
46949de0493SThomas Gleixner 
4706aec1ad7SBorislav Petkov static const struct cstate_model cnl_cstates __initconst = {
4716aec1ad7SBorislav Petkov 	.core_events		= BIT(PERF_CSTATE_CORE_C1_RES) |
47249de0493SThomas Gleixner 				  BIT(PERF_CSTATE_CORE_C3_RES) |
47349de0493SThomas Gleixner 				  BIT(PERF_CSTATE_CORE_C6_RES) |
47449de0493SThomas Gleixner 				  BIT(PERF_CSTATE_CORE_C7_RES),
47549de0493SThomas Gleixner 
47649de0493SThomas Gleixner 	.pkg_events		= BIT(PERF_CSTATE_PKG_C2_RES) |
477cb63ba0fSKan Liang 				  BIT(PERF_CSTATE_PKG_C3_RES) |
47849de0493SThomas Gleixner 				  BIT(PERF_CSTATE_PKG_C6_RES) |
4796aec1ad7SBorislav Petkov 				  BIT(PERF_CSTATE_PKG_C7_RES) |
4806aec1ad7SBorislav Petkov 				  BIT(PERF_CSTATE_PKG_C8_RES) |
4813877d55aSKan Liang 				  BIT(PERF_CSTATE_PKG_C9_RES) |
4823877d55aSKan Liang 				  BIT(PERF_CSTATE_PKG_C10_RES),
4833877d55aSKan Liang };
4843877d55aSKan Liang 
4853877d55aSKan Liang static const struct cstate_model icl_cstates __initconst = {
4863877d55aSKan Liang 	.core_events		= BIT(PERF_CSTATE_CORE_C6_RES) |
4873877d55aSKan Liang 				  BIT(PERF_CSTATE_CORE_C7_RES),
4883877d55aSKan Liang 
4893877d55aSKan Liang 	.pkg_events		= BIT(PERF_CSTATE_PKG_C2_RES) |
49077c34ef1SSebastian Andrzej Siewior 				  BIT(PERF_CSTATE_PKG_C3_RES) |
4916aec1ad7SBorislav Petkov 				  BIT(PERF_CSTATE_PKG_C6_RES) |
492c7afba32SThomas Gleixner 				  BIT(PERF_CSTATE_PKG_C7_RES) |
493d9f3b450SValdis Klētnieks 				  BIT(PERF_CSTATE_PKG_C8_RES) |
4948f2a28c5SJiri Olsa 				  BIT(PERF_CSTATE_PKG_C9_RES) |
4958f2a28c5SJiri Olsa 				  BIT(PERF_CSTATE_PKG_C10_RES),
4968f2a28c5SJiri Olsa };
4978f2a28c5SJiri Olsa 
4988f2a28c5SJiri Olsa static const struct cstate_model icx_cstates __initconst = {
4998f2a28c5SJiri Olsa 	.core_events		= BIT(PERF_CSTATE_CORE_C1_RES) |
5008f2a28c5SJiri Olsa 				  BIT(PERF_CSTATE_CORE_C6_RES),
501d9f3b450SValdis Klētnieks 
5028f2a28c5SJiri Olsa 	.pkg_events		= BIT(PERF_CSTATE_PKG_C2_RES) |
5038f2a28c5SJiri Olsa 				  BIT(PERF_CSTATE_PKG_C6_RES),
5048f2a28c5SJiri Olsa };
5058f2a28c5SJiri Olsa 
5068f2a28c5SJiri Olsa static const struct cstate_model adl_cstates __initconst = {
5078f2a28c5SJiri Olsa 	.core_events		= BIT(PERF_CSTATE_CORE_C1_RES) |
5088f2a28c5SJiri Olsa 				  BIT(PERF_CSTATE_CORE_C6_RES) |
5098f2a28c5SJiri Olsa 				  BIT(PERF_CSTATE_CORE_C7_RES),
5108f2a28c5SJiri Olsa 
5118f2a28c5SJiri Olsa 	.pkg_events		= BIT(PERF_CSTATE_PKG_C2_RES) |
5123877d55aSKan Liang 				  BIT(PERF_CSTATE_PKG_C3_RES) |
5133877d55aSKan Liang 				  BIT(PERF_CSTATE_PKG_C6_RES) |
5143877d55aSKan Liang 				  BIT(PERF_CSTATE_PKG_C8_RES) |
5153877d55aSKan Liang 				  BIT(PERF_CSTATE_PKG_C10_RES),
5163877d55aSKan Liang };
517424646eeSThomas Gleixner 
518243218caSKan Liang static const struct cstate_model lnl_cstates __initconst = {
5198f2a28c5SJiri Olsa 	.core_events		= BIT(PERF_CSTATE_CORE_C1_RES) |
520424646eeSThomas Gleixner 				  BIT(PERF_CSTATE_CORE_C6_RES) |
521424646eeSThomas Gleixner 				  BIT(PERF_CSTATE_CORE_C7_RES),
522424646eeSThomas Gleixner 
523424646eeSThomas Gleixner 	.pkg_events		= BIT(PERF_CSTATE_PKG_C2_RES) |
524424646eeSThomas Gleixner 				  BIT(PERF_CSTATE_PKG_C3_RES) |
525424646eeSThomas Gleixner 				  BIT(PERF_CSTATE_PKG_C6_RES) |
526424646eeSThomas Gleixner 				  BIT(PERF_CSTATE_PKG_C10_RES),
527424646eeSThomas Gleixner };
5282ff40250SAndrew Murray 
52974545f63SDavid Carrillo-Cisneros static const struct cstate_model slm_cstates __initconst = {
530424646eeSThomas Gleixner 	.core_events		= BIT(PERF_CSTATE_CORE_C1_RES) |
531424646eeSThomas Gleixner 				  BIT(PERF_CSTATE_CORE_C6_RES),
532424646eeSThomas Gleixner 
533243218caSKan Liang 	.pkg_events		= BIT(PERF_CSTATE_PKG_C6_RES),
5348f2a28c5SJiri Olsa 	.quirks			= SLM_PKG_C6_USE_C7_MSR,
535424646eeSThomas Gleixner };
536424646eeSThomas Gleixner 
537424646eeSThomas Gleixner 
538424646eeSThomas Gleixner static const struct cstate_model knl_cstates __initconst = {
539424646eeSThomas Gleixner 	.core_events		= BIT(PERF_CSTATE_CORE_C6_RES),
540424646eeSThomas Gleixner 
541424646eeSThomas Gleixner 	.pkg_events		= BIT(PERF_CSTATE_PKG_C2_RES) |
542424646eeSThomas Gleixner 				  BIT(PERF_CSTATE_PKG_C3_RES) |
5432ff40250SAndrew Murray 				  BIT(PERF_CSTATE_PKG_C6_RES),
54474545f63SDavid Carrillo-Cisneros 	.quirks			= KNL_CORE_C6_MSR,
545424646eeSThomas Gleixner };
546424646eeSThomas Gleixner 
5473877d55aSKan Liang 
5483877d55aSKan Liang static const struct cstate_model glm_cstates __initconst = {
5493877d55aSKan Liang 	.core_events		= BIT(PERF_CSTATE_CORE_C1_RES) |
5503877d55aSKan Liang 				  BIT(PERF_CSTATE_CORE_C3_RES) |
5513877d55aSKan Liang 				  BIT(PERF_CSTATE_CORE_C6_RES),
5523877d55aSKan Liang 
5533877d55aSKan Liang 	.pkg_events		= BIT(PERF_CSTATE_PKG_C2_RES) |
5543877d55aSKan Liang 				  BIT(PERF_CSTATE_PKG_C3_RES) |
5553877d55aSKan Liang 				  BIT(PERF_CSTATE_PKG_C6_RES) |
5563877d55aSKan Liang 				  BIT(PERF_CSTATE_PKG_C10_RES),
5573877d55aSKan Liang };
5583877d55aSKan Liang 
5593877d55aSKan Liang static const struct cstate_model grr_cstates __initconst = {
5603877d55aSKan Liang 	.core_events		= BIT(PERF_CSTATE_CORE_C1_RES) |
5613877d55aSKan Liang 				  BIT(PERF_CSTATE_CORE_C6_RES),
562424646eeSThomas Gleixner 
563424646eeSThomas Gleixner 	.module_events		= BIT(PERF_CSTATE_MODULE_C6_RES),
564424646eeSThomas Gleixner };
565424646eeSThomas Gleixner 
566424646eeSThomas Gleixner static const struct cstate_model srf_cstates __initconst = {
567424646eeSThomas Gleixner 	.core_events		= BIT(PERF_CSTATE_CORE_C1_RES) |
568424646eeSThomas Gleixner 				  BIT(PERF_CSTATE_CORE_C6_RES),
569424646eeSThomas Gleixner 
570424646eeSThomas Gleixner 	.pkg_events		= BIT(PERF_CSTATE_PKG_C2_RES) |
571424646eeSThomas Gleixner 				  BIT(PERF_CSTATE_PKG_C6_RES),
572424646eeSThomas Gleixner 
573424646eeSThomas Gleixner 	.module_events		= BIT(PERF_CSTATE_MODULE_C6_RES),
574424646eeSThomas Gleixner };
575424646eeSThomas Gleixner 
576424646eeSThomas Gleixner 
577424646eeSThomas Gleixner static const struct x86_cpu_id intel_cstates_match[] __initconst = {
578424646eeSThomas Gleixner 	X86_MATCH_VFM(INTEL_NEHALEM,		&nhm_cstates),
579424646eeSThomas Gleixner 	X86_MATCH_VFM(INTEL_NEHALEM_EP,		&nhm_cstates),
580424646eeSThomas Gleixner 	X86_MATCH_VFM(INTEL_NEHALEM_EX,		&nhm_cstates),
581424646eeSThomas Gleixner 
582424646eeSThomas Gleixner 	X86_MATCH_VFM(INTEL_WESTMERE,		&nhm_cstates),
583424646eeSThomas Gleixner 	X86_MATCH_VFM(INTEL_WESTMERE_EP,	&nhm_cstates),
584424646eeSThomas Gleixner 	X86_MATCH_VFM(INTEL_WESTMERE_EX,	&nhm_cstates),
585424646eeSThomas Gleixner 
586424646eeSThomas Gleixner 	X86_MATCH_VFM(INTEL_SANDYBRIDGE,	&snb_cstates),
587424646eeSThomas Gleixner 	X86_MATCH_VFM(INTEL_SANDYBRIDGE_X,	&snb_cstates),
588424646eeSThomas Gleixner 
589424646eeSThomas Gleixner 	X86_MATCH_VFM(INTEL_IVYBRIDGE,		&snb_cstates),
590424646eeSThomas Gleixner 	X86_MATCH_VFM(INTEL_IVYBRIDGE_X,	&snb_cstates),
591424646eeSThomas Gleixner 
592424646eeSThomas Gleixner 	X86_MATCH_VFM(INTEL_HASWELL,		&snb_cstates),
593424646eeSThomas Gleixner 	X86_MATCH_VFM(INTEL_HASWELL_X,		&snb_cstates),
594424646eeSThomas Gleixner 	X86_MATCH_VFM(INTEL_HASWELL_G,		&snb_cstates),
595424646eeSThomas Gleixner 
5961159e094SHarry Pan 	X86_MATCH_VFM(INTEL_HASWELL_L,		&hswult_cstates),
5971159e094SHarry Pan 
5981159e094SHarry Pan 	X86_MATCH_VFM(INTEL_ATOM_SILVERMONT,	&slm_cstates),
5991159e094SHarry Pan 	X86_MATCH_VFM(INTEL_ATOM_SILVERMONT_D,	&slm_cstates),
6001159e094SHarry Pan 	X86_MATCH_VFM(INTEL_ATOM_AIRMONT,	&slm_cstates),
6011159e094SHarry Pan 
6021159e094SHarry Pan 	X86_MATCH_VFM(INTEL_BROADWELL,		&snb_cstates),
6031159e094SHarry Pan 	X86_MATCH_VFM(INTEL_BROADWELL_D,	&snb_cstates),
6041159e094SHarry Pan 	X86_MATCH_VFM(INTEL_BROADWELL_G,	&snb_cstates),
6051159e094SHarry Pan 	X86_MATCH_VFM(INTEL_BROADWELL_X,	&snb_cstates),
6061159e094SHarry Pan 
6071159e094SHarry Pan 	X86_MATCH_VFM(INTEL_SKYLAKE_L,		&snb_cstates),
6081159e094SHarry Pan 	X86_MATCH_VFM(INTEL_SKYLAKE,		&snb_cstates),
6091159e094SHarry Pan 	X86_MATCH_VFM(INTEL_SKYLAKE_X,		&snb_cstates),
6101159e094SHarry Pan 
611f1857a24SKan Liang 	X86_MATCH_VFM(INTEL_KABYLAKE_L,		&hswult_cstates),
612f1857a24SKan Liang 	X86_MATCH_VFM(INTEL_KABYLAKE,		&hswult_cstates),
613f1857a24SKan Liang 	X86_MATCH_VFM(INTEL_COMETLAKE_L,	&hswult_cstates),
614f1857a24SKan Liang 	X86_MATCH_VFM(INTEL_COMETLAKE,		&hswult_cstates),
615f1857a24SKan Liang 
616f1857a24SKan Liang 	X86_MATCH_VFM(INTEL_CANNONLAKE_L,	&cnl_cstates),
617f1857a24SKan Liang 
618f1857a24SKan Liang 	X86_MATCH_VFM(INTEL_XEON_PHI_KNL,	&knl_cstates),
619f1857a24SKan Liang 	X86_MATCH_VFM(INTEL_XEON_PHI_KNM,	&knl_cstates),
620f1857a24SKan Liang 
621f1857a24SKan Liang 	X86_MATCH_VFM(INTEL_ATOM_GOLDMONT,	&glm_cstates),
622f1857a24SKan Liang 	X86_MATCH_VFM(INTEL_ATOM_GOLDMONT_D,	&glm_cstates),
623f1857a24SKan Liang 	X86_MATCH_VFM(INTEL_ATOM_GOLDMONT_PLUS,	&glm_cstates),
62487bf399fSZhang Rui 	X86_MATCH_VFM(INTEL_ATOM_TREMONT_D,	&glm_cstates),
62587bf399fSZhang Rui 	X86_MATCH_VFM(INTEL_ATOM_TREMONT,	&glm_cstates),
62687bf399fSZhang Rui 	X86_MATCH_VFM(INTEL_ATOM_TREMONT_L,	&glm_cstates),
62787bf399fSZhang Rui 	X86_MATCH_VFM(INTEL_ATOM_GRACEMONT,	&adl_cstates),
62887bf399fSZhang Rui 	X86_MATCH_VFM(INTEL_ATOM_CRESTMONT_X,	&srf_cstates),
62987bf399fSZhang Rui 	X86_MATCH_VFM(INTEL_ATOM_CRESTMONT,	&grr_cstates),
63087bf399fSZhang Rui 
63187bf399fSZhang Rui 	X86_MATCH_VFM(INTEL_ICELAKE_L,		&icl_cstates),
632d0ca946bSKan Liang 	X86_MATCH_VFM(INTEL_ICELAKE,		&icl_cstates),
633d0ca946bSKan Liang 	X86_MATCH_VFM(INTEL_ICELAKE_X,		&icx_cstates),
634d0ca946bSKan Liang 	X86_MATCH_VFM(INTEL_ICELAKE_D,		&icx_cstates),
635d0ca946bSKan Liang 	X86_MATCH_VFM(INTEL_SAPPHIRERAPIDS_X,	&icx_cstates),
636d0ca946bSKan Liang 	X86_MATCH_VFM(INTEL_EMERALDRAPIDS_X,	&icx_cstates),
637d0ca946bSKan Liang 	X86_MATCH_VFM(INTEL_GRANITERAPIDS_X,	&icx_cstates),
638d0ca946bSKan Liang 	X86_MATCH_VFM(INTEL_GRANITERAPIDS_D,	&icx_cstates),
639d0ca946bSKan Liang 
640d0ca946bSKan Liang 	X86_MATCH_VFM(INTEL_TIGERLAKE_L,	&icl_cstates),
641d0ca946bSKan Liang 	X86_MATCH_VFM(INTEL_TIGERLAKE,		&icl_cstates),
642d0ca946bSKan Liang 	X86_MATCH_VFM(INTEL_ROCKETLAKE,		&icl_cstates),
643d0ca946bSKan Liang 	X86_MATCH_VFM(INTEL_ALDERLAKE,		&adl_cstates),
64426579860SZhang Rui 	X86_MATCH_VFM(INTEL_ALDERLAKE_L,	&adl_cstates),
64526579860SZhang Rui 	X86_MATCH_VFM(INTEL_RAPTORLAKE,		&adl_cstates),
64626579860SZhang Rui 	X86_MATCH_VFM(INTEL_RAPTORLAKE_P,	&adl_cstates),
64726579860SZhang Rui 	X86_MATCH_VFM(INTEL_RAPTORLAKE_S,	&adl_cstates),
64826579860SZhang Rui 	X86_MATCH_VFM(INTEL_METEORLAKE,		&adl_cstates),
64926579860SZhang Rui 	X86_MATCH_VFM(INTEL_METEORLAKE_L,	&adl_cstates),
65026579860SZhang Rui 	X86_MATCH_VFM(INTEL_ARROWLAKE,		&adl_cstates),
65126579860SZhang Rui 	X86_MATCH_VFM(INTEL_ARROWLAKE_H,	&adl_cstates),
652424646eeSThomas Gleixner 	X86_MATCH_VFM(INTEL_ARROWLAKE_U,	&adl_cstates),
653424646eeSThomas Gleixner 	X86_MATCH_VFM(INTEL_LUNARLAKE_M,	&lnl_cstates),
654424646eeSThomas Gleixner 	{ },
655424646eeSThomas Gleixner };
656424646eeSThomas Gleixner MODULE_DEVICE_TABLE(x86cpu, intel_cstates_match);
657424646eeSThomas Gleixner 
cstate_probe(const struct cstate_model * cm)658424646eeSThomas Gleixner static int __init cstate_probe(const struct cstate_model *cm)
659424646eeSThomas Gleixner {
660424646eeSThomas Gleixner 	/* SLM has different MSR for PKG C6 */
661424646eeSThomas Gleixner 	if (cm->quirks & SLM_PKG_C6_USE_C7_MSR)
662424646eeSThomas Gleixner 		pkg_msr[PERF_CSTATE_PKG_C6_RES].msr = MSR_PKG_C7_RESIDENCY;
663889882bcSLukasz Odzioba 
664889882bcSLukasz Odzioba 	/* KNL has different MSR for CORE C6 */
665889882bcSLukasz Odzioba 	if (cm->quirks & KNL_CORE_C6_MSR)
666889882bcSLukasz Odzioba 		pkg_msr[PERF_CSTATE_CORE_C6_RES].msr = MSR_KNL_CORE_C6_RESIDENCY;
667889882bcSLukasz Odzioba 
668889882bcSLukasz Odzioba 
669889882bcSLukasz Odzioba 	core_msr_mask = perf_msr_probe(core_msr, PERF_CSTATE_CORE_EVENT_MAX,
670889882bcSLukasz Odzioba 				       true, (void *) &cm->core_events);
671889882bcSLukasz Odzioba 
672889882bcSLukasz Odzioba 	pkg_msr_mask = perf_msr_probe(pkg_msr, PERF_CSTATE_PKG_EVENT_MAX,
673889882bcSLukasz Odzioba 				      true, (void *) &cm->pkg_events);
6745c10b048SHarry Pan 
6755c10b048SHarry Pan 	module_msr_mask = perf_msr_probe(module_msr, PERF_CSTATE_MODULE_EVENT_MAX,
6765c10b048SHarry Pan 				      true, (void *) &cm->module_events);
6775c10b048SHarry Pan 
6785c10b048SHarry Pan 	has_cstate_core = !!core_msr_mask;
6795c10b048SHarry Pan 	has_cstate_pkg  = !!pkg_msr_mask;
6805c10b048SHarry Pan 	has_cstate_module  = !!module_msr_mask;
6815c10b048SHarry Pan 
6825c10b048SHarry Pan 	return (has_cstate_core || has_cstate_pkg || has_cstate_module) ? 0 : -ENODEV;
6835c10b048SHarry Pan }
6845c10b048SHarry Pan 
cstate_cleanup(void)685bbb96869SKan Liang static inline void cstate_cleanup(void)
686bbb96869SKan Liang {
687bbb96869SKan Liang 	if (has_cstate_core)
688bbb96869SKan Liang 		perf_pmu_unregister(&cstate_core_pmu);
689bbb96869SKan Liang 
690bbb96869SKan Liang 	if (has_cstate_pkg)
691bbb96869SKan Liang 		perf_pmu_unregister(&cstate_pkg_pmu);
6923877d55aSKan Liang 
6933877d55aSKan Liang 	if (has_cstate_module)
6943877d55aSKan Liang 		perf_pmu_unregister(&cstate_module_pmu);
6953877d55aSKan Liang }
696*b1d0e15cSZhenyu Wang 
cstate_init(void)697*b1d0e15cSZhenyu Wang static int __init cstate_init(void)
6983877d55aSKan Liang {
6993877d55aSKan Liang 	int err;
7003877d55aSKan Liang 
7013877d55aSKan Liang 	if (has_cstate_core) {
702889882bcSLukasz Odzioba 		err = perf_pmu_register(&cstate_core_pmu, cstate_core_pmu.name, -1);
703424646eeSThomas Gleixner 		if (err) {
7045ee80094STony Luck 			has_cstate_core = false;
7055ee80094STony Luck 			pr_info("Failed to register cstate core pmu\n");
7065ee80094STony Luck 			cstate_cleanup();
707424646eeSThomas Gleixner 			return err;
7085ee80094STony Luck 		}
7095ee80094STony Luck 	}
7105ee80094STony Luck 
711424646eeSThomas Gleixner 	if (has_cstate_pkg) {
7125ee80094STony Luck 		if (topology_max_dies_per_package() > 1) {
7135ee80094STony Luck 			/* CLX-AP is multi-die and the cstate is die-scope */
714424646eeSThomas Gleixner 			cstate_pkg_pmu.scope = PERF_PMU_SCOPE_DIE;
7155ee80094STony Luck 			err = perf_pmu_register(&cstate_pkg_pmu,
7165ee80094STony Luck 						"cstate_die", -1);
717424646eeSThomas Gleixner 		} else {
7185ee80094STony Luck 			err = perf_pmu_register(&cstate_pkg_pmu,
7195ee80094STony Luck 						cstate_pkg_pmu.name, -1);
7205ee80094STony Luck 		}
721424646eeSThomas Gleixner 		if (err) {
7225ee80094STony Luck 			has_cstate_pkg = false;
723424646eeSThomas Gleixner 			pr_info("Failed to register cstate pkg pmu\n");
7245ee80094STony Luck 			cstate_cleanup();
7255ee80094STony Luck 			return err;
7265ee80094STony Luck 		}
727424646eeSThomas Gleixner 	}
7285ee80094STony Luck 
7295ee80094STony Luck 	if (has_cstate_module) {
7305ee80094STony Luck 		err = perf_pmu_register(&cstate_module_pmu, cstate_module_pmu.name, -1);
7315ee80094STony Luck 		if (err) {
732424646eeSThomas Gleixner 			has_cstate_module = false;
7335ee80094STony Luck 			pr_info("Failed to register cstate cluster pmu\n");
7345ee80094STony Luck 			cstate_cleanup();
7355ee80094STony Luck 			return err;
736889882bcSLukasz Odzioba 		}
7375ee80094STony Luck 	}
7385ee80094STony Luck 	return 0;
7395ee80094STony Luck }
7405ee80094STony Luck 
cstate_pmu_init(void)741f2029b1eSSrinivas Pandruvada static int __init cstate_pmu_init(void)
7425ee80094STony Luck {
7431159e094SHarry Pan 	const struct x86_cpu_id *id;
7445ee80094STony Luck 	int err;
7455ee80094STony Luck 
7465c10b048SHarry Pan 	if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
7475ee80094STony Luck 		return -ENODEV;
7485ee80094STony Luck 
7495ee80094STony Luck 	id = x86_match_cpu(intel_cstates_match);
7505ee80094STony Luck 	if (!id)
7515ee80094STony Luck 		return -ENODEV;
7525ee80094STony Luck 
7535ee80094STony Luck 	err = cstate_probe((const struct cstate_model *) id->driver_data);
7545ee80094STony Luck 	if (err)
7555ee80094STony Luck 		return err;
756f08c47d1SKan Liang 
7575ee80094STony Luck 	return cstate_init();
7585ee80094STony Luck }
7595ee80094STony Luck module_init(cstate_pmu_init);
7605ee80094STony Luck 
cstate_pmu_exit(void)7615ee80094STony Luck static void __exit cstate_pmu_exit(void)
7625ee80094STony Luck {
7635ee80094STony Luck 	cstate_cleanup();
7645ee80094STony Luck }
76587bf399fSZhang Rui module_exit(cstate_pmu_exit);
7665ee80094STony Luck