cstate.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. /*
  2. * Support cstate residency counters
  3. *
  4. * Copyright (C) 2015, Intel Corp.
  5. * Author: Kan Liang ([email protected])
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. */
  18. /*
  19. * This file export cstate related free running (read-only) counters
  20. * for perf. These counters may be use simultaneously by other tools,
  21. * such as turbostat. However, it still make sense to implement them
  22. * in perf. Because we can conveniently collect them together with
  23. * other events, and allow to use them from tools without special MSR
  24. * access code.
  25. *
  26. * The events only support system-wide mode counting. There is no
  27. * sampling support because it is not supported by the hardware.
  28. *
  29. * According to counters' scope and category, two PMUs are registered
  30. * with the perf_event core subsystem.
  31. * - 'cstate_core': The counter is available for each physical core.
  32. * The counters include CORE_C*_RESIDENCY.
  33. * - 'cstate_pkg': The counter is available for each physical package.
  34. * The counters include PKG_C*_RESIDENCY.
  35. *
  36. * All of these counters are specified in the Intel® 64 and IA-32
  37. * Architectures Software Developer.s Manual Vol3b.
  38. *
  39. * Model specific counters:
  40. * MSR_CORE_C1_RES: CORE C1 Residency Counter
  41. * perf code: 0x00
  42. * Available model: SLM,AMT,GLM,CNL,ICX,TNT,ADL,RPL
  43. * MTL
  44. * Scope: Core (each processor core has a MSR)
  45. * MSR_CORE_C3_RESIDENCY: CORE C3 Residency Counter
  46. * perf code: 0x01
  47. * Available model: NHM,WSM,SNB,IVB,HSW,BDW,SKL,GLM,
  48. * CNL,KBL,CML,TNT
  49. * Scope: Core
  50. * MSR_CORE_C6_RESIDENCY: CORE C6 Residency Counter
  51. * perf code: 0x02
  52. * Available model: SLM,AMT,NHM,WSM,SNB,IVB,HSW,BDW,
  53. * SKL,KNL,GLM,CNL,KBL,CML,ICL,ICX,
  54. * TGL,TNT,RKL,ADL,RPL,SPR,MTL
  55. * Scope: Core
  56. * MSR_CORE_C7_RESIDENCY: CORE C7 Residency Counter
  57. * perf code: 0x03
  58. * Available model: SNB,IVB,HSW,BDW,SKL,CNL,KBL,CML,
  59. * ICL,TGL,RKL,ADL,RPL,MTL
  60. * Scope: Core
  61. * MSR_PKG_C2_RESIDENCY: Package C2 Residency Counter.
  62. * perf code: 0x00
  63. * Available model: SNB,IVB,HSW,BDW,SKL,KNL,GLM,CNL,
  64. * KBL,CML,ICL,ICX,TGL,TNT,RKL,ADL,
  65. * RPL,SPR,MTL
  66. * Scope: Package (physical package)
  67. * MSR_PKG_C3_RESIDENCY: Package C3 Residency Counter.
  68. * perf code: 0x01
  69. * Available model: NHM,WSM,SNB,IVB,HSW,BDW,SKL,KNL,
  70. * GLM,CNL,KBL,CML,ICL,TGL,TNT,RKL,
  71. * ADL,RPL,MTL
  72. * Scope: Package (physical package)
  73. * MSR_PKG_C6_RESIDENCY: Package C6 Residency Counter.
  74. * perf code: 0x02
  75. * Available model: SLM,AMT,NHM,WSM,SNB,IVB,HSW,BDW,
  76. * SKL,KNL,GLM,CNL,KBL,CML,ICL,ICX,
  77. * TGL,TNT,RKL,ADL,RPL,SPR,MTL
  78. * Scope: Package (physical package)
  79. * MSR_PKG_C7_RESIDENCY: Package C7 Residency Counter.
  80. * perf code: 0x03
  81. * Available model: NHM,WSM,SNB,IVB,HSW,BDW,SKL,CNL,
  82. * KBL,CML,ICL,TGL,RKL,ADL,RPL,MTL
  83. * Scope: Package (physical package)
  84. * MSR_PKG_C8_RESIDENCY: Package C8 Residency Counter.
  85. * perf code: 0x04
  86. * Available model: HSW ULT,KBL,CNL,CML,ICL,TGL,RKL,
  87. * ADL,RPL,MTL
  88. * Scope: Package (physical package)
  89. * MSR_PKG_C9_RESIDENCY: Package C9 Residency Counter.
  90. * perf code: 0x05
  91. * Available model: HSW ULT,KBL,CNL,CML,ICL,TGL,RKL,
  92. * ADL,RPL,MTL
  93. * Scope: Package (physical package)
  94. * MSR_PKG_C10_RESIDENCY: Package C10 Residency Counter.
  95. * perf code: 0x06
  96. * Available model: HSW ULT,KBL,GLM,CNL,CML,ICL,TGL,
  97. * TNT,RKL,ADL,RPL,MTL
  98. * Scope: Package (physical package)
  99. *
  100. */
  101. #include <linux/module.h>
  102. #include <linux/slab.h>
  103. #include <linux/perf_event.h>
  104. #include <linux/nospec.h>
  105. #include <asm/cpu_device_id.h>
  106. #include <asm/intel-family.h>
  107. #include "../perf_event.h"
  108. #include "../probe.h"
  109. MODULE_LICENSE("GPL");
  110. #define DEFINE_CSTATE_FORMAT_ATTR(_var, _name, _format) \
  111. static ssize_t __cstate_##_var##_show(struct device *dev, \
  112. struct device_attribute *attr, \
  113. char *page) \
  114. { \
  115. BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \
  116. return sprintf(page, _format "\n"); \
  117. } \
  118. static struct device_attribute format_attr_##_var = \
  119. __ATTR(_name, 0444, __cstate_##_var##_show, NULL)
  120. static ssize_t cstate_get_attr_cpumask(struct device *dev,
  121. struct device_attribute *attr,
  122. char *buf);
  123. /* Model -> events mapping */
  124. struct cstate_model {
  125. unsigned long core_events;
  126. unsigned long pkg_events;
  127. unsigned long quirks;
  128. };
  129. /* Quirk flags */
  130. #define SLM_PKG_C6_USE_C7_MSR (1UL << 0)
  131. #define KNL_CORE_C6_MSR (1UL << 1)
  132. struct perf_cstate_msr {
  133. u64 msr;
  134. struct perf_pmu_events_attr *attr;
  135. };
  136. /* cstate_core PMU */
  137. static struct pmu cstate_core_pmu;
  138. static bool has_cstate_core;
  139. enum perf_cstate_core_events {
  140. PERF_CSTATE_CORE_C1_RES = 0,
  141. PERF_CSTATE_CORE_C3_RES,
  142. PERF_CSTATE_CORE_C6_RES,
  143. PERF_CSTATE_CORE_C7_RES,
  144. PERF_CSTATE_CORE_EVENT_MAX,
  145. };
  146. PMU_EVENT_ATTR_STRING(c1-residency, attr_cstate_core_c1, "event=0x00");
  147. PMU_EVENT_ATTR_STRING(c3-residency, attr_cstate_core_c3, "event=0x01");
  148. PMU_EVENT_ATTR_STRING(c6-residency, attr_cstate_core_c6, "event=0x02");
  149. PMU_EVENT_ATTR_STRING(c7-residency, attr_cstate_core_c7, "event=0x03");
  150. static unsigned long core_msr_mask;
  151. PMU_EVENT_GROUP(events, cstate_core_c1);
  152. PMU_EVENT_GROUP(events, cstate_core_c3);
  153. PMU_EVENT_GROUP(events, cstate_core_c6);
  154. PMU_EVENT_GROUP(events, cstate_core_c7);
  155. static bool test_msr(int idx, void *data)
  156. {
  157. return test_bit(idx, (unsigned long *) data);
  158. }
  159. static struct perf_msr core_msr[] = {
  160. [PERF_CSTATE_CORE_C1_RES] = { MSR_CORE_C1_RES, &group_cstate_core_c1, test_msr },
  161. [PERF_CSTATE_CORE_C3_RES] = { MSR_CORE_C3_RESIDENCY, &group_cstate_core_c3, test_msr },
  162. [PERF_CSTATE_CORE_C6_RES] = { MSR_CORE_C6_RESIDENCY, &group_cstate_core_c6, test_msr },
  163. [PERF_CSTATE_CORE_C7_RES] = { MSR_CORE_C7_RESIDENCY, &group_cstate_core_c7, test_msr },
  164. };
  165. static struct attribute *attrs_empty[] = {
  166. NULL,
  167. };
  168. /*
  169. * There are no default events, but we need to create
  170. * "events" group (with empty attrs) before updating
  171. * it with detected events.
  172. */
  173. static struct attribute_group core_events_attr_group = {
  174. .name = "events",
  175. .attrs = attrs_empty,
  176. };
  177. DEFINE_CSTATE_FORMAT_ATTR(core_event, event, "config:0-63");
  178. static struct attribute *core_format_attrs[] = {
  179. &format_attr_core_event.attr,
  180. NULL,
  181. };
  182. static struct attribute_group core_format_attr_group = {
  183. .name = "format",
  184. .attrs = core_format_attrs,
  185. };
  186. static cpumask_t cstate_core_cpu_mask;
  187. static DEVICE_ATTR(cpumask, S_IRUGO, cstate_get_attr_cpumask, NULL);
  188. static struct attribute *cstate_cpumask_attrs[] = {
  189. &dev_attr_cpumask.attr,
  190. NULL,
  191. };
  192. static struct attribute_group cpumask_attr_group = {
  193. .attrs = cstate_cpumask_attrs,
  194. };
  195. static const struct attribute_group *core_attr_groups[] = {
  196. &core_events_attr_group,
  197. &core_format_attr_group,
  198. &cpumask_attr_group,
  199. NULL,
  200. };
  201. /* cstate_pkg PMU */
  202. static struct pmu cstate_pkg_pmu;
  203. static bool has_cstate_pkg;
  204. enum perf_cstate_pkg_events {
  205. PERF_CSTATE_PKG_C2_RES = 0,
  206. PERF_CSTATE_PKG_C3_RES,
  207. PERF_CSTATE_PKG_C6_RES,
  208. PERF_CSTATE_PKG_C7_RES,
  209. PERF_CSTATE_PKG_C8_RES,
  210. PERF_CSTATE_PKG_C9_RES,
  211. PERF_CSTATE_PKG_C10_RES,
  212. PERF_CSTATE_PKG_EVENT_MAX,
  213. };
  214. PMU_EVENT_ATTR_STRING(c2-residency, attr_cstate_pkg_c2, "event=0x00");
  215. PMU_EVENT_ATTR_STRING(c3-residency, attr_cstate_pkg_c3, "event=0x01");
  216. PMU_EVENT_ATTR_STRING(c6-residency, attr_cstate_pkg_c6, "event=0x02");
  217. PMU_EVENT_ATTR_STRING(c7-residency, attr_cstate_pkg_c7, "event=0x03");
  218. PMU_EVENT_ATTR_STRING(c8-residency, attr_cstate_pkg_c8, "event=0x04");
  219. PMU_EVENT_ATTR_STRING(c9-residency, attr_cstate_pkg_c9, "event=0x05");
  220. PMU_EVENT_ATTR_STRING(c10-residency, attr_cstate_pkg_c10, "event=0x06");
  221. static unsigned long pkg_msr_mask;
  222. PMU_EVENT_GROUP(events, cstate_pkg_c2);
  223. PMU_EVENT_GROUP(events, cstate_pkg_c3);
  224. PMU_EVENT_GROUP(events, cstate_pkg_c6);
  225. PMU_EVENT_GROUP(events, cstate_pkg_c7);
  226. PMU_EVENT_GROUP(events, cstate_pkg_c8);
  227. PMU_EVENT_GROUP(events, cstate_pkg_c9);
  228. PMU_EVENT_GROUP(events, cstate_pkg_c10);
  229. static struct perf_msr pkg_msr[] = {
  230. [PERF_CSTATE_PKG_C2_RES] = { MSR_PKG_C2_RESIDENCY, &group_cstate_pkg_c2, test_msr },
  231. [PERF_CSTATE_PKG_C3_RES] = { MSR_PKG_C3_RESIDENCY, &group_cstate_pkg_c3, test_msr },
  232. [PERF_CSTATE_PKG_C6_RES] = { MSR_PKG_C6_RESIDENCY, &group_cstate_pkg_c6, test_msr },
  233. [PERF_CSTATE_PKG_C7_RES] = { MSR_PKG_C7_RESIDENCY, &group_cstate_pkg_c7, test_msr },
  234. [PERF_CSTATE_PKG_C8_RES] = { MSR_PKG_C8_RESIDENCY, &group_cstate_pkg_c8, test_msr },
  235. [PERF_CSTATE_PKG_C9_RES] = { MSR_PKG_C9_RESIDENCY, &group_cstate_pkg_c9, test_msr },
  236. [PERF_CSTATE_PKG_C10_RES] = { MSR_PKG_C10_RESIDENCY, &group_cstate_pkg_c10, test_msr },
  237. };
  238. static struct attribute_group pkg_events_attr_group = {
  239. .name = "events",
  240. .attrs = attrs_empty,
  241. };
  242. DEFINE_CSTATE_FORMAT_ATTR(pkg_event, event, "config:0-63");
  243. static struct attribute *pkg_format_attrs[] = {
  244. &format_attr_pkg_event.attr,
  245. NULL,
  246. };
  247. static struct attribute_group pkg_format_attr_group = {
  248. .name = "format",
  249. .attrs = pkg_format_attrs,
  250. };
  251. static cpumask_t cstate_pkg_cpu_mask;
  252. static const struct attribute_group *pkg_attr_groups[] = {
  253. &pkg_events_attr_group,
  254. &pkg_format_attr_group,
  255. &cpumask_attr_group,
  256. NULL,
  257. };
  258. static ssize_t cstate_get_attr_cpumask(struct device *dev,
  259. struct device_attribute *attr,
  260. char *buf)
  261. {
  262. struct pmu *pmu = dev_get_drvdata(dev);
  263. if (pmu == &cstate_core_pmu)
  264. return cpumap_print_to_pagebuf(true, buf, &cstate_core_cpu_mask);
  265. else if (pmu == &cstate_pkg_pmu)
  266. return cpumap_print_to_pagebuf(true, buf, &cstate_pkg_cpu_mask);
  267. else
  268. return 0;
  269. }
  270. static int cstate_pmu_event_init(struct perf_event *event)
  271. {
  272. u64 cfg = event->attr.config;
  273. int cpu;
  274. if (event->attr.type != event->pmu->type)
  275. return -ENOENT;
  276. /* unsupported modes and filters */
  277. if (event->attr.sample_period) /* no sampling */
  278. return -EINVAL;
  279. if (event->cpu < 0)
  280. return -EINVAL;
  281. if (event->pmu == &cstate_core_pmu) {
  282. if (cfg >= PERF_CSTATE_CORE_EVENT_MAX)
  283. return -EINVAL;
  284. cfg = array_index_nospec((unsigned long)cfg, PERF_CSTATE_CORE_EVENT_MAX);
  285. if (!(core_msr_mask & (1 << cfg)))
  286. return -EINVAL;
  287. event->hw.event_base = core_msr[cfg].msr;
  288. cpu = cpumask_any_and(&cstate_core_cpu_mask,
  289. topology_sibling_cpumask(event->cpu));
  290. } else if (event->pmu == &cstate_pkg_pmu) {
  291. if (cfg >= PERF_CSTATE_PKG_EVENT_MAX)
  292. return -EINVAL;
  293. cfg = array_index_nospec((unsigned long)cfg, PERF_CSTATE_PKG_EVENT_MAX);
  294. if (!(pkg_msr_mask & (1 << cfg)))
  295. return -EINVAL;
  296. event->hw.event_base = pkg_msr[cfg].msr;
  297. cpu = cpumask_any_and(&cstate_pkg_cpu_mask,
  298. topology_die_cpumask(event->cpu));
  299. } else {
  300. return -ENOENT;
  301. }
  302. if (cpu >= nr_cpu_ids)
  303. return -ENODEV;
  304. event->cpu = cpu;
  305. event->hw.config = cfg;
  306. event->hw.idx = -1;
  307. return 0;
  308. }
  309. static inline u64 cstate_pmu_read_counter(struct perf_event *event)
  310. {
  311. u64 val;
  312. rdmsrl(event->hw.event_base, val);
  313. return val;
  314. }
  315. static void cstate_pmu_event_update(struct perf_event *event)
  316. {
  317. struct hw_perf_event *hwc = &event->hw;
  318. u64 prev_raw_count, new_raw_count;
  319. again:
  320. prev_raw_count = local64_read(&hwc->prev_count);
  321. new_raw_count = cstate_pmu_read_counter(event);
  322. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  323. new_raw_count) != prev_raw_count)
  324. goto again;
  325. local64_add(new_raw_count - prev_raw_count, &event->count);
  326. }
  327. static void cstate_pmu_event_start(struct perf_event *event, int mode)
  328. {
  329. local64_set(&event->hw.prev_count, cstate_pmu_read_counter(event));
  330. }
  331. static void cstate_pmu_event_stop(struct perf_event *event, int mode)
  332. {
  333. cstate_pmu_event_update(event);
  334. }
  335. static void cstate_pmu_event_del(struct perf_event *event, int mode)
  336. {
  337. cstate_pmu_event_stop(event, PERF_EF_UPDATE);
  338. }
  339. static int cstate_pmu_event_add(struct perf_event *event, int mode)
  340. {
  341. if (mode & PERF_EF_START)
  342. cstate_pmu_event_start(event, mode);
  343. return 0;
  344. }
  345. /*
  346. * Check if exiting cpu is the designated reader. If so migrate the
  347. * events when there is a valid target available
  348. */
  349. static int cstate_cpu_exit(unsigned int cpu)
  350. {
  351. unsigned int target;
  352. if (has_cstate_core &&
  353. cpumask_test_and_clear_cpu(cpu, &cstate_core_cpu_mask)) {
  354. target = cpumask_any_but(topology_sibling_cpumask(cpu), cpu);
  355. /* Migrate events if there is a valid target */
  356. if (target < nr_cpu_ids) {
  357. cpumask_set_cpu(target, &cstate_core_cpu_mask);
  358. perf_pmu_migrate_context(&cstate_core_pmu, cpu, target);
  359. }
  360. }
  361. if (has_cstate_pkg &&
  362. cpumask_test_and_clear_cpu(cpu, &cstate_pkg_cpu_mask)) {
  363. target = cpumask_any_but(topology_die_cpumask(cpu), cpu);
  364. /* Migrate events if there is a valid target */
  365. if (target < nr_cpu_ids) {
  366. cpumask_set_cpu(target, &cstate_pkg_cpu_mask);
  367. perf_pmu_migrate_context(&cstate_pkg_pmu, cpu, target);
  368. }
  369. }
  370. return 0;
  371. }
  372. static int cstate_cpu_init(unsigned int cpu)
  373. {
  374. unsigned int target;
  375. /*
  376. * If this is the first online thread of that core, set it in
  377. * the core cpu mask as the designated reader.
  378. */
  379. target = cpumask_any_and(&cstate_core_cpu_mask,
  380. topology_sibling_cpumask(cpu));
  381. if (has_cstate_core && target >= nr_cpu_ids)
  382. cpumask_set_cpu(cpu, &cstate_core_cpu_mask);
  383. /*
  384. * If this is the first online thread of that package, set it
  385. * in the package cpu mask as the designated reader.
  386. */
  387. target = cpumask_any_and(&cstate_pkg_cpu_mask,
  388. topology_die_cpumask(cpu));
  389. if (has_cstate_pkg && target >= nr_cpu_ids)
  390. cpumask_set_cpu(cpu, &cstate_pkg_cpu_mask);
  391. return 0;
  392. }
  393. static const struct attribute_group *core_attr_update[] = {
  394. &group_cstate_core_c1,
  395. &group_cstate_core_c3,
  396. &group_cstate_core_c6,
  397. &group_cstate_core_c7,
  398. NULL,
  399. };
  400. static const struct attribute_group *pkg_attr_update[] = {
  401. &group_cstate_pkg_c2,
  402. &group_cstate_pkg_c3,
  403. &group_cstate_pkg_c6,
  404. &group_cstate_pkg_c7,
  405. &group_cstate_pkg_c8,
  406. &group_cstate_pkg_c9,
  407. &group_cstate_pkg_c10,
  408. NULL,
  409. };
  410. static struct pmu cstate_core_pmu = {
  411. .attr_groups = core_attr_groups,
  412. .attr_update = core_attr_update,
  413. .name = "cstate_core",
  414. .task_ctx_nr = perf_invalid_context,
  415. .event_init = cstate_pmu_event_init,
  416. .add = cstate_pmu_event_add,
  417. .del = cstate_pmu_event_del,
  418. .start = cstate_pmu_event_start,
  419. .stop = cstate_pmu_event_stop,
  420. .read = cstate_pmu_event_update,
  421. .capabilities = PERF_PMU_CAP_NO_INTERRUPT | PERF_PMU_CAP_NO_EXCLUDE,
  422. .module = THIS_MODULE,
  423. };
  424. static struct pmu cstate_pkg_pmu = {
  425. .attr_groups = pkg_attr_groups,
  426. .attr_update = pkg_attr_update,
  427. .name = "cstate_pkg",
  428. .task_ctx_nr = perf_invalid_context,
  429. .event_init = cstate_pmu_event_init,
  430. .add = cstate_pmu_event_add,
  431. .del = cstate_pmu_event_del,
  432. .start = cstate_pmu_event_start,
  433. .stop = cstate_pmu_event_stop,
  434. .read = cstate_pmu_event_update,
  435. .capabilities = PERF_PMU_CAP_NO_INTERRUPT | PERF_PMU_CAP_NO_EXCLUDE,
  436. .module = THIS_MODULE,
  437. };
  438. static const struct cstate_model nhm_cstates __initconst = {
  439. .core_events = BIT(PERF_CSTATE_CORE_C3_RES) |
  440. BIT(PERF_CSTATE_CORE_C6_RES),
  441. .pkg_events = BIT(PERF_CSTATE_PKG_C3_RES) |
  442. BIT(PERF_CSTATE_PKG_C6_RES) |
  443. BIT(PERF_CSTATE_PKG_C7_RES),
  444. };
  445. static const struct cstate_model snb_cstates __initconst = {
  446. .core_events = BIT(PERF_CSTATE_CORE_C3_RES) |
  447. BIT(PERF_CSTATE_CORE_C6_RES) |
  448. BIT(PERF_CSTATE_CORE_C7_RES),
  449. .pkg_events = BIT(PERF_CSTATE_PKG_C2_RES) |
  450. BIT(PERF_CSTATE_PKG_C3_RES) |
  451. BIT(PERF_CSTATE_PKG_C6_RES) |
  452. BIT(PERF_CSTATE_PKG_C7_RES),
  453. };
  454. static const struct cstate_model hswult_cstates __initconst = {
  455. .core_events = BIT(PERF_CSTATE_CORE_C3_RES) |
  456. BIT(PERF_CSTATE_CORE_C6_RES) |
  457. BIT(PERF_CSTATE_CORE_C7_RES),
  458. .pkg_events = BIT(PERF_CSTATE_PKG_C2_RES) |
  459. BIT(PERF_CSTATE_PKG_C3_RES) |
  460. BIT(PERF_CSTATE_PKG_C6_RES) |
  461. BIT(PERF_CSTATE_PKG_C7_RES) |
  462. BIT(PERF_CSTATE_PKG_C8_RES) |
  463. BIT(PERF_CSTATE_PKG_C9_RES) |
  464. BIT(PERF_CSTATE_PKG_C10_RES),
  465. };
  466. static const struct cstate_model cnl_cstates __initconst = {
  467. .core_events = BIT(PERF_CSTATE_CORE_C1_RES) |
  468. BIT(PERF_CSTATE_CORE_C3_RES) |
  469. BIT(PERF_CSTATE_CORE_C6_RES) |
  470. BIT(PERF_CSTATE_CORE_C7_RES),
  471. .pkg_events = BIT(PERF_CSTATE_PKG_C2_RES) |
  472. BIT(PERF_CSTATE_PKG_C3_RES) |
  473. BIT(PERF_CSTATE_PKG_C6_RES) |
  474. BIT(PERF_CSTATE_PKG_C7_RES) |
  475. BIT(PERF_CSTATE_PKG_C8_RES) |
  476. BIT(PERF_CSTATE_PKG_C9_RES) |
  477. BIT(PERF_CSTATE_PKG_C10_RES),
  478. };
  479. static const struct cstate_model icl_cstates __initconst = {
  480. .core_events = BIT(PERF_CSTATE_CORE_C6_RES) |
  481. BIT(PERF_CSTATE_CORE_C7_RES),
  482. .pkg_events = BIT(PERF_CSTATE_PKG_C2_RES) |
  483. BIT(PERF_CSTATE_PKG_C3_RES) |
  484. BIT(PERF_CSTATE_PKG_C6_RES) |
  485. BIT(PERF_CSTATE_PKG_C7_RES) |
  486. BIT(PERF_CSTATE_PKG_C8_RES) |
  487. BIT(PERF_CSTATE_PKG_C9_RES) |
  488. BIT(PERF_CSTATE_PKG_C10_RES),
  489. };
  490. static const struct cstate_model icx_cstates __initconst = {
  491. .core_events = BIT(PERF_CSTATE_CORE_C1_RES) |
  492. BIT(PERF_CSTATE_CORE_C6_RES),
  493. .pkg_events = BIT(PERF_CSTATE_PKG_C2_RES) |
  494. BIT(PERF_CSTATE_PKG_C6_RES),
  495. };
  496. static const struct cstate_model adl_cstates __initconst = {
  497. .core_events = BIT(PERF_CSTATE_CORE_C1_RES) |
  498. BIT(PERF_CSTATE_CORE_C6_RES) |
  499. BIT(PERF_CSTATE_CORE_C7_RES),
  500. .pkg_events = BIT(PERF_CSTATE_PKG_C2_RES) |
  501. BIT(PERF_CSTATE_PKG_C3_RES) |
  502. BIT(PERF_CSTATE_PKG_C6_RES) |
  503. BIT(PERF_CSTATE_PKG_C7_RES) |
  504. BIT(PERF_CSTATE_PKG_C8_RES) |
  505. BIT(PERF_CSTATE_PKG_C9_RES) |
  506. BIT(PERF_CSTATE_PKG_C10_RES),
  507. };
  508. static const struct cstate_model slm_cstates __initconst = {
  509. .core_events = BIT(PERF_CSTATE_CORE_C1_RES) |
  510. BIT(PERF_CSTATE_CORE_C6_RES),
  511. .pkg_events = BIT(PERF_CSTATE_PKG_C6_RES),
  512. .quirks = SLM_PKG_C6_USE_C7_MSR,
  513. };
  514. static const struct cstate_model knl_cstates __initconst = {
  515. .core_events = BIT(PERF_CSTATE_CORE_C6_RES),
  516. .pkg_events = BIT(PERF_CSTATE_PKG_C2_RES) |
  517. BIT(PERF_CSTATE_PKG_C3_RES) |
  518. BIT(PERF_CSTATE_PKG_C6_RES),
  519. .quirks = KNL_CORE_C6_MSR,
  520. };
  521. static const struct cstate_model glm_cstates __initconst = {
  522. .core_events = BIT(PERF_CSTATE_CORE_C1_RES) |
  523. BIT(PERF_CSTATE_CORE_C3_RES) |
  524. BIT(PERF_CSTATE_CORE_C6_RES),
  525. .pkg_events = BIT(PERF_CSTATE_PKG_C2_RES) |
  526. BIT(PERF_CSTATE_PKG_C3_RES) |
  527. BIT(PERF_CSTATE_PKG_C6_RES) |
  528. BIT(PERF_CSTATE_PKG_C10_RES),
  529. };
  530. static const struct x86_cpu_id intel_cstates_match[] __initconst = {
  531. X86_MATCH_INTEL_FAM6_MODEL(NEHALEM, &nhm_cstates),
  532. X86_MATCH_INTEL_FAM6_MODEL(NEHALEM_EP, &nhm_cstates),
  533. X86_MATCH_INTEL_FAM6_MODEL(NEHALEM_EX, &nhm_cstates),
  534. X86_MATCH_INTEL_FAM6_MODEL(WESTMERE, &nhm_cstates),
  535. X86_MATCH_INTEL_FAM6_MODEL(WESTMERE_EP, &nhm_cstates),
  536. X86_MATCH_INTEL_FAM6_MODEL(WESTMERE_EX, &nhm_cstates),
  537. X86_MATCH_INTEL_FAM6_MODEL(SANDYBRIDGE, &snb_cstates),
  538. X86_MATCH_INTEL_FAM6_MODEL(SANDYBRIDGE_X, &snb_cstates),
  539. X86_MATCH_INTEL_FAM6_MODEL(IVYBRIDGE, &snb_cstates),
  540. X86_MATCH_INTEL_FAM6_MODEL(IVYBRIDGE_X, &snb_cstates),
  541. X86_MATCH_INTEL_FAM6_MODEL(HASWELL, &snb_cstates),
  542. X86_MATCH_INTEL_FAM6_MODEL(HASWELL_X, &snb_cstates),
  543. X86_MATCH_INTEL_FAM6_MODEL(HASWELL_G, &snb_cstates),
  544. X86_MATCH_INTEL_FAM6_MODEL(HASWELL_L, &hswult_cstates),
  545. X86_MATCH_INTEL_FAM6_MODEL(ATOM_SILVERMONT, &slm_cstates),
  546. X86_MATCH_INTEL_FAM6_MODEL(ATOM_SILVERMONT_D, &slm_cstates),
  547. X86_MATCH_INTEL_FAM6_MODEL(ATOM_AIRMONT, &slm_cstates),
  548. X86_MATCH_INTEL_FAM6_MODEL(BROADWELL, &snb_cstates),
  549. X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_D, &snb_cstates),
  550. X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_G, &snb_cstates),
  551. X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_X, &snb_cstates),
  552. X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE_L, &snb_cstates),
  553. X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE, &snb_cstates),
  554. X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE_X, &snb_cstates),
  555. X86_MATCH_INTEL_FAM6_MODEL(KABYLAKE_L, &hswult_cstates),
  556. X86_MATCH_INTEL_FAM6_MODEL(KABYLAKE, &hswult_cstates),
  557. X86_MATCH_INTEL_FAM6_MODEL(COMETLAKE_L, &hswult_cstates),
  558. X86_MATCH_INTEL_FAM6_MODEL(COMETLAKE, &hswult_cstates),
  559. X86_MATCH_INTEL_FAM6_MODEL(CANNONLAKE_L, &cnl_cstates),
  560. X86_MATCH_INTEL_FAM6_MODEL(XEON_PHI_KNL, &knl_cstates),
  561. X86_MATCH_INTEL_FAM6_MODEL(XEON_PHI_KNM, &knl_cstates),
  562. X86_MATCH_INTEL_FAM6_MODEL(ATOM_GOLDMONT, &glm_cstates),
  563. X86_MATCH_INTEL_FAM6_MODEL(ATOM_GOLDMONT_D, &glm_cstates),
  564. X86_MATCH_INTEL_FAM6_MODEL(ATOM_GOLDMONT_PLUS, &glm_cstates),
  565. X86_MATCH_INTEL_FAM6_MODEL(ATOM_TREMONT_D, &glm_cstates),
  566. X86_MATCH_INTEL_FAM6_MODEL(ATOM_TREMONT, &glm_cstates),
  567. X86_MATCH_INTEL_FAM6_MODEL(ATOM_TREMONT_L, &glm_cstates),
  568. X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_L, &icl_cstates),
  569. X86_MATCH_INTEL_FAM6_MODEL(ICELAKE, &icl_cstates),
  570. X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_X, &icx_cstates),
  571. X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_D, &icx_cstates),
  572. X86_MATCH_INTEL_FAM6_MODEL(SAPPHIRERAPIDS_X, &icx_cstates),
  573. X86_MATCH_INTEL_FAM6_MODEL(EMERALDRAPIDS_X, &icx_cstates),
  574. X86_MATCH_INTEL_FAM6_MODEL(TIGERLAKE_L, &icl_cstates),
  575. X86_MATCH_INTEL_FAM6_MODEL(TIGERLAKE, &icl_cstates),
  576. X86_MATCH_INTEL_FAM6_MODEL(ROCKETLAKE, &icl_cstates),
  577. X86_MATCH_INTEL_FAM6_MODEL(ALDERLAKE, &adl_cstates),
  578. X86_MATCH_INTEL_FAM6_MODEL(ALDERLAKE_L, &adl_cstates),
  579. X86_MATCH_INTEL_FAM6_MODEL(ALDERLAKE_N, &adl_cstates),
  580. X86_MATCH_INTEL_FAM6_MODEL(RAPTORLAKE, &adl_cstates),
  581. X86_MATCH_INTEL_FAM6_MODEL(RAPTORLAKE_P, &adl_cstates),
  582. X86_MATCH_INTEL_FAM6_MODEL(RAPTORLAKE_S, &adl_cstates),
  583. X86_MATCH_INTEL_FAM6_MODEL(METEORLAKE, &adl_cstates),
  584. X86_MATCH_INTEL_FAM6_MODEL(METEORLAKE_L, &adl_cstates),
  585. { },
  586. };
  587. MODULE_DEVICE_TABLE(x86cpu, intel_cstates_match);
  588. static int __init cstate_probe(const struct cstate_model *cm)
  589. {
  590. /* SLM has different MSR for PKG C6 */
  591. if (cm->quirks & SLM_PKG_C6_USE_C7_MSR)
  592. pkg_msr[PERF_CSTATE_PKG_C6_RES].msr = MSR_PKG_C7_RESIDENCY;
  593. /* KNL has different MSR for CORE C6 */
  594. if (cm->quirks & KNL_CORE_C6_MSR)
  595. pkg_msr[PERF_CSTATE_CORE_C6_RES].msr = MSR_KNL_CORE_C6_RESIDENCY;
  596. core_msr_mask = perf_msr_probe(core_msr, PERF_CSTATE_CORE_EVENT_MAX,
  597. true, (void *) &cm->core_events);
  598. pkg_msr_mask = perf_msr_probe(pkg_msr, PERF_CSTATE_PKG_EVENT_MAX,
  599. true, (void *) &cm->pkg_events);
  600. has_cstate_core = !!core_msr_mask;
  601. has_cstate_pkg = !!pkg_msr_mask;
  602. return (has_cstate_core || has_cstate_pkg) ? 0 : -ENODEV;
  603. }
  604. static inline void cstate_cleanup(void)
  605. {
  606. cpuhp_remove_state_nocalls(CPUHP_AP_PERF_X86_CSTATE_ONLINE);
  607. cpuhp_remove_state_nocalls(CPUHP_AP_PERF_X86_CSTATE_STARTING);
  608. if (has_cstate_core)
  609. perf_pmu_unregister(&cstate_core_pmu);
  610. if (has_cstate_pkg)
  611. perf_pmu_unregister(&cstate_pkg_pmu);
  612. }
  613. static int __init cstate_init(void)
  614. {
  615. int err;
  616. cpuhp_setup_state(CPUHP_AP_PERF_X86_CSTATE_STARTING,
  617. "perf/x86/cstate:starting", cstate_cpu_init, NULL);
  618. cpuhp_setup_state(CPUHP_AP_PERF_X86_CSTATE_ONLINE,
  619. "perf/x86/cstate:online", NULL, cstate_cpu_exit);
  620. if (has_cstate_core) {
  621. err = perf_pmu_register(&cstate_core_pmu, cstate_core_pmu.name, -1);
  622. if (err) {
  623. has_cstate_core = false;
  624. pr_info("Failed to register cstate core pmu\n");
  625. cstate_cleanup();
  626. return err;
  627. }
  628. }
  629. if (has_cstate_pkg) {
  630. if (topology_max_die_per_package() > 1) {
  631. err = perf_pmu_register(&cstate_pkg_pmu,
  632. "cstate_die", -1);
  633. } else {
  634. err = perf_pmu_register(&cstate_pkg_pmu,
  635. cstate_pkg_pmu.name, -1);
  636. }
  637. if (err) {
  638. has_cstate_pkg = false;
  639. pr_info("Failed to register cstate pkg pmu\n");
  640. cstate_cleanup();
  641. return err;
  642. }
  643. }
  644. return 0;
  645. }
  646. static int __init cstate_pmu_init(void)
  647. {
  648. const struct x86_cpu_id *id;
  649. int err;
  650. if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
  651. return -ENODEV;
  652. id = x86_match_cpu(intel_cstates_match);
  653. if (!id)
  654. return -ENODEV;
  655. err = cstate_probe((const struct cstate_model *) id->driver_data);
  656. if (err)
  657. return err;
  658. return cstate_init();
  659. }
  660. module_init(cstate_pmu_init);
  661. static void __exit cstate_pmu_exit(void)
  662. {
  663. cstate_cleanup();
  664. }
  665. module_exit(cstate_pmu_exit);