cstate.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /*
  2. * Support cstate residency counters
  3. *
  4. * Copyright (C) 2015, Intel Corp.
  5. * Author: Kan Liang (kan.liang@intel.com)
  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
  43. * Scope: Core (each processor core has a MSR)
  44. * MSR_CORE_C3_RESIDENCY: CORE C3 Residency Counter
  45. * perf code: 0x01
  46. * Available model: NHM,WSM,SNB,IVB,HSW,BDW,SKL,GLM,
  47. CNL
  48. * Scope: Core
  49. * MSR_CORE_C6_RESIDENCY: CORE C6 Residency Counter
  50. * perf code: 0x02
  51. * Available model: SLM,AMT,NHM,WSM,SNB,IVB,HSW,BDW,
  52. * SKL,KNL,GLM,CNL
  53. * Scope: Core
  54. * MSR_CORE_C7_RESIDENCY: CORE C7 Residency Counter
  55. * perf code: 0x03
  56. * Available model: SNB,IVB,HSW,BDW,SKL,CNL
  57. * Scope: Core
  58. * MSR_PKG_C2_RESIDENCY: Package C2 Residency Counter.
  59. * perf code: 0x00
  60. * Available model: SNB,IVB,HSW,BDW,SKL,KNL,GLM,CNL
  61. * Scope: Package (physical package)
  62. * MSR_PKG_C3_RESIDENCY: Package C3 Residency Counter.
  63. * perf code: 0x01
  64. * Available model: NHM,WSM,SNB,IVB,HSW,BDW,SKL,KNL,
  65. * GLM,CNL
  66. * Scope: Package (physical package)
  67. * MSR_PKG_C6_RESIDENCY: Package C6 Residency Counter.
  68. * perf code: 0x02
  69. * Available model: SLM,AMT,NHM,WSM,SNB,IVB,HSW,BDW
  70. * SKL,KNL,GLM,CNL
  71. * Scope: Package (physical package)
  72. * MSR_PKG_C7_RESIDENCY: Package C7 Residency Counter.
  73. * perf code: 0x03
  74. * Available model: NHM,WSM,SNB,IVB,HSW,BDW,SKL,CNL
  75. * Scope: Package (physical package)
  76. * MSR_PKG_C8_RESIDENCY: Package C8 Residency Counter.
  77. * perf code: 0x04
  78. * Available model: HSW ULT,KBL,CNL
  79. * Scope: Package (physical package)
  80. * MSR_PKG_C9_RESIDENCY: Package C9 Residency Counter.
  81. * perf code: 0x05
  82. * Available model: HSW ULT,KBL,CNL
  83. * Scope: Package (physical package)
  84. * MSR_PKG_C10_RESIDENCY: Package C10 Residency Counter.
  85. * perf code: 0x06
  86. * Available model: HSW ULT,KBL,GLM,CNL
  87. * Scope: Package (physical package)
  88. *
  89. */
  90. #include <linux/module.h>
  91. #include <linux/slab.h>
  92. #include <linux/perf_event.h>
  93. #include <linux/nospec.h>
  94. #include <asm/cpu_device_id.h>
  95. #include <asm/intel-family.h>
  96. #include "../perf_event.h"
  97. MODULE_LICENSE("GPL");
  98. #define DEFINE_CSTATE_FORMAT_ATTR(_var, _name, _format) \
  99. static ssize_t __cstate_##_var##_show(struct device *dev, \
  100. struct device_attribute *attr, \
  101. char *page) \
  102. { \
  103. BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \
  104. return sprintf(page, _format "\n"); \
  105. } \
  106. static struct device_attribute format_attr_##_var = \
  107. __ATTR(_name, 0444, __cstate_##_var##_show, NULL)
  108. static ssize_t cstate_get_attr_cpumask(struct device *dev,
  109. struct device_attribute *attr,
  110. char *buf);
  111. /* Model -> events mapping */
  112. struct cstate_model {
  113. unsigned long core_events;
  114. unsigned long pkg_events;
  115. unsigned long quirks;
  116. };
  117. /* Quirk flags */
  118. #define SLM_PKG_C6_USE_C7_MSR (1UL << 0)
  119. #define KNL_CORE_C6_MSR (1UL << 1)
  120. struct perf_cstate_msr {
  121. u64 msr;
  122. struct perf_pmu_events_attr *attr;
  123. };
  124. /* cstate_core PMU */
  125. static struct pmu cstate_core_pmu;
  126. static bool has_cstate_core;
  127. enum perf_cstate_core_events {
  128. PERF_CSTATE_CORE_C1_RES = 0,
  129. PERF_CSTATE_CORE_C3_RES,
  130. PERF_CSTATE_CORE_C6_RES,
  131. PERF_CSTATE_CORE_C7_RES,
  132. PERF_CSTATE_CORE_EVENT_MAX,
  133. };
  134. PMU_EVENT_ATTR_STRING(c1-residency, evattr_cstate_core_c1, "event=0x00");
  135. PMU_EVENT_ATTR_STRING(c3-residency, evattr_cstate_core_c3, "event=0x01");
  136. PMU_EVENT_ATTR_STRING(c6-residency, evattr_cstate_core_c6, "event=0x02");
  137. PMU_EVENT_ATTR_STRING(c7-residency, evattr_cstate_core_c7, "event=0x03");
  138. static struct perf_cstate_msr core_msr[] = {
  139. [PERF_CSTATE_CORE_C1_RES] = { MSR_CORE_C1_RES, &evattr_cstate_core_c1 },
  140. [PERF_CSTATE_CORE_C3_RES] = { MSR_CORE_C3_RESIDENCY, &evattr_cstate_core_c3 },
  141. [PERF_CSTATE_CORE_C6_RES] = { MSR_CORE_C6_RESIDENCY, &evattr_cstate_core_c6 },
  142. [PERF_CSTATE_CORE_C7_RES] = { MSR_CORE_C7_RESIDENCY, &evattr_cstate_core_c7 },
  143. };
  144. static struct attribute *core_events_attrs[PERF_CSTATE_CORE_EVENT_MAX + 1] = {
  145. NULL,
  146. };
  147. static struct attribute_group core_events_attr_group = {
  148. .name = "events",
  149. .attrs = core_events_attrs,
  150. };
  151. DEFINE_CSTATE_FORMAT_ATTR(core_event, event, "config:0-63");
  152. static struct attribute *core_format_attrs[] = {
  153. &format_attr_core_event.attr,
  154. NULL,
  155. };
  156. static struct attribute_group core_format_attr_group = {
  157. .name = "format",
  158. .attrs = core_format_attrs,
  159. };
  160. static cpumask_t cstate_core_cpu_mask;
  161. static DEVICE_ATTR(cpumask, S_IRUGO, cstate_get_attr_cpumask, NULL);
  162. static struct attribute *cstate_cpumask_attrs[] = {
  163. &dev_attr_cpumask.attr,
  164. NULL,
  165. };
  166. static struct attribute_group cpumask_attr_group = {
  167. .attrs = cstate_cpumask_attrs,
  168. };
  169. static const struct attribute_group *core_attr_groups[] = {
  170. &core_events_attr_group,
  171. &core_format_attr_group,
  172. &cpumask_attr_group,
  173. NULL,
  174. };
  175. /* cstate_pkg PMU */
  176. static struct pmu cstate_pkg_pmu;
  177. static bool has_cstate_pkg;
  178. enum perf_cstate_pkg_events {
  179. PERF_CSTATE_PKG_C2_RES = 0,
  180. PERF_CSTATE_PKG_C3_RES,
  181. PERF_CSTATE_PKG_C6_RES,
  182. PERF_CSTATE_PKG_C7_RES,
  183. PERF_CSTATE_PKG_C8_RES,
  184. PERF_CSTATE_PKG_C9_RES,
  185. PERF_CSTATE_PKG_C10_RES,
  186. PERF_CSTATE_PKG_EVENT_MAX,
  187. };
  188. PMU_EVENT_ATTR_STRING(c2-residency, evattr_cstate_pkg_c2, "event=0x00");
  189. PMU_EVENT_ATTR_STRING(c3-residency, evattr_cstate_pkg_c3, "event=0x01");
  190. PMU_EVENT_ATTR_STRING(c6-residency, evattr_cstate_pkg_c6, "event=0x02");
  191. PMU_EVENT_ATTR_STRING(c7-residency, evattr_cstate_pkg_c7, "event=0x03");
  192. PMU_EVENT_ATTR_STRING(c8-residency, evattr_cstate_pkg_c8, "event=0x04");
  193. PMU_EVENT_ATTR_STRING(c9-residency, evattr_cstate_pkg_c9, "event=0x05");
  194. PMU_EVENT_ATTR_STRING(c10-residency, evattr_cstate_pkg_c10, "event=0x06");
  195. static struct perf_cstate_msr pkg_msr[] = {
  196. [PERF_CSTATE_PKG_C2_RES] = { MSR_PKG_C2_RESIDENCY, &evattr_cstate_pkg_c2 },
  197. [PERF_CSTATE_PKG_C3_RES] = { MSR_PKG_C3_RESIDENCY, &evattr_cstate_pkg_c3 },
  198. [PERF_CSTATE_PKG_C6_RES] = { MSR_PKG_C6_RESIDENCY, &evattr_cstate_pkg_c6 },
  199. [PERF_CSTATE_PKG_C7_RES] = { MSR_PKG_C7_RESIDENCY, &evattr_cstate_pkg_c7 },
  200. [PERF_CSTATE_PKG_C8_RES] = { MSR_PKG_C8_RESIDENCY, &evattr_cstate_pkg_c8 },
  201. [PERF_CSTATE_PKG_C9_RES] = { MSR_PKG_C9_RESIDENCY, &evattr_cstate_pkg_c9 },
  202. [PERF_CSTATE_PKG_C10_RES] = { MSR_PKG_C10_RESIDENCY, &evattr_cstate_pkg_c10 },
  203. };
  204. static struct attribute *pkg_events_attrs[PERF_CSTATE_PKG_EVENT_MAX + 1] = {
  205. NULL,
  206. };
  207. static struct attribute_group pkg_events_attr_group = {
  208. .name = "events",
  209. .attrs = pkg_events_attrs,
  210. };
  211. DEFINE_CSTATE_FORMAT_ATTR(pkg_event, event, "config:0-63");
  212. static struct attribute *pkg_format_attrs[] = {
  213. &format_attr_pkg_event.attr,
  214. NULL,
  215. };
  216. static struct attribute_group pkg_format_attr_group = {
  217. .name = "format",
  218. .attrs = pkg_format_attrs,
  219. };
  220. static cpumask_t cstate_pkg_cpu_mask;
  221. static const struct attribute_group *pkg_attr_groups[] = {
  222. &pkg_events_attr_group,
  223. &pkg_format_attr_group,
  224. &cpumask_attr_group,
  225. NULL,
  226. };
  227. static ssize_t cstate_get_attr_cpumask(struct device *dev,
  228. struct device_attribute *attr,
  229. char *buf)
  230. {
  231. struct pmu *pmu = dev_get_drvdata(dev);
  232. if (pmu == &cstate_core_pmu)
  233. return cpumap_print_to_pagebuf(true, buf, &cstate_core_cpu_mask);
  234. else if (pmu == &cstate_pkg_pmu)
  235. return cpumap_print_to_pagebuf(true, buf, &cstate_pkg_cpu_mask);
  236. else
  237. return 0;
  238. }
  239. static int cstate_pmu_event_init(struct perf_event *event)
  240. {
  241. u64 cfg = event->attr.config;
  242. int cpu;
  243. if (event->attr.type != event->pmu->type)
  244. return -ENOENT;
  245. /* unsupported modes and filters */
  246. if (event->attr.exclude_user ||
  247. event->attr.exclude_kernel ||
  248. event->attr.exclude_hv ||
  249. event->attr.exclude_idle ||
  250. event->attr.exclude_host ||
  251. event->attr.exclude_guest ||
  252. event->attr.sample_period) /* no sampling */
  253. return -EINVAL;
  254. if (event->cpu < 0)
  255. return -EINVAL;
  256. if (event->pmu == &cstate_core_pmu) {
  257. if (cfg >= PERF_CSTATE_CORE_EVENT_MAX)
  258. return -EINVAL;
  259. if (!core_msr[cfg].attr)
  260. return -EINVAL;
  261. event->hw.event_base = core_msr[cfg].msr;
  262. cpu = cpumask_any_and(&cstate_core_cpu_mask,
  263. topology_sibling_cpumask(event->cpu));
  264. } else if (event->pmu == &cstate_pkg_pmu) {
  265. if (cfg >= PERF_CSTATE_PKG_EVENT_MAX)
  266. return -EINVAL;
  267. cfg = array_index_nospec((unsigned long)cfg, PERF_CSTATE_PKG_EVENT_MAX);
  268. if (!pkg_msr[cfg].attr)
  269. return -EINVAL;
  270. event->hw.event_base = pkg_msr[cfg].msr;
  271. cpu = cpumask_any_and(&cstate_pkg_cpu_mask,
  272. topology_core_cpumask(event->cpu));
  273. } else {
  274. return -ENOENT;
  275. }
  276. if (cpu >= nr_cpu_ids)
  277. return -ENODEV;
  278. event->cpu = cpu;
  279. event->hw.config = cfg;
  280. event->hw.idx = -1;
  281. return 0;
  282. }
  283. static inline u64 cstate_pmu_read_counter(struct perf_event *event)
  284. {
  285. u64 val;
  286. rdmsrl(event->hw.event_base, val);
  287. return val;
  288. }
  289. static void cstate_pmu_event_update(struct perf_event *event)
  290. {
  291. struct hw_perf_event *hwc = &event->hw;
  292. u64 prev_raw_count, new_raw_count;
  293. again:
  294. prev_raw_count = local64_read(&hwc->prev_count);
  295. new_raw_count = cstate_pmu_read_counter(event);
  296. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  297. new_raw_count) != prev_raw_count)
  298. goto again;
  299. local64_add(new_raw_count - prev_raw_count, &event->count);
  300. }
  301. static void cstate_pmu_event_start(struct perf_event *event, int mode)
  302. {
  303. local64_set(&event->hw.prev_count, cstate_pmu_read_counter(event));
  304. }
  305. static void cstate_pmu_event_stop(struct perf_event *event, int mode)
  306. {
  307. cstate_pmu_event_update(event);
  308. }
  309. static void cstate_pmu_event_del(struct perf_event *event, int mode)
  310. {
  311. cstate_pmu_event_stop(event, PERF_EF_UPDATE);
  312. }
  313. static int cstate_pmu_event_add(struct perf_event *event, int mode)
  314. {
  315. if (mode & PERF_EF_START)
  316. cstate_pmu_event_start(event, mode);
  317. return 0;
  318. }
  319. /*
  320. * Check if exiting cpu is the designated reader. If so migrate the
  321. * events when there is a valid target available
  322. */
  323. static int cstate_cpu_exit(unsigned int cpu)
  324. {
  325. unsigned int target;
  326. if (has_cstate_core &&
  327. cpumask_test_and_clear_cpu(cpu, &cstate_core_cpu_mask)) {
  328. target = cpumask_any_but(topology_sibling_cpumask(cpu), cpu);
  329. /* Migrate events if there is a valid target */
  330. if (target < nr_cpu_ids) {
  331. cpumask_set_cpu(target, &cstate_core_cpu_mask);
  332. perf_pmu_migrate_context(&cstate_core_pmu, cpu, target);
  333. }
  334. }
  335. if (has_cstate_pkg &&
  336. cpumask_test_and_clear_cpu(cpu, &cstate_pkg_cpu_mask)) {
  337. target = cpumask_any_but(topology_core_cpumask(cpu), cpu);
  338. /* Migrate events if there is a valid target */
  339. if (target < nr_cpu_ids) {
  340. cpumask_set_cpu(target, &cstate_pkg_cpu_mask);
  341. perf_pmu_migrate_context(&cstate_pkg_pmu, cpu, target);
  342. }
  343. }
  344. return 0;
  345. }
  346. static int cstate_cpu_init(unsigned int cpu)
  347. {
  348. unsigned int target;
  349. /*
  350. * If this is the first online thread of that core, set it in
  351. * the core cpu mask as the designated reader.
  352. */
  353. target = cpumask_any_and(&cstate_core_cpu_mask,
  354. topology_sibling_cpumask(cpu));
  355. if (has_cstate_core && target >= nr_cpu_ids)
  356. cpumask_set_cpu(cpu, &cstate_core_cpu_mask);
  357. /*
  358. * If this is the first online thread of that package, set it
  359. * in the package cpu mask as the designated reader.
  360. */
  361. target = cpumask_any_and(&cstate_pkg_cpu_mask,
  362. topology_core_cpumask(cpu));
  363. if (has_cstate_pkg && target >= nr_cpu_ids)
  364. cpumask_set_cpu(cpu, &cstate_pkg_cpu_mask);
  365. return 0;
  366. }
  367. static struct pmu cstate_core_pmu = {
  368. .attr_groups = core_attr_groups,
  369. .name = "cstate_core",
  370. .task_ctx_nr = perf_invalid_context,
  371. .event_init = cstate_pmu_event_init,
  372. .add = cstate_pmu_event_add,
  373. .del = cstate_pmu_event_del,
  374. .start = cstate_pmu_event_start,
  375. .stop = cstate_pmu_event_stop,
  376. .read = cstate_pmu_event_update,
  377. .capabilities = PERF_PMU_CAP_NO_INTERRUPT,
  378. .module = THIS_MODULE,
  379. };
  380. static struct pmu cstate_pkg_pmu = {
  381. .attr_groups = pkg_attr_groups,
  382. .name = "cstate_pkg",
  383. .task_ctx_nr = perf_invalid_context,
  384. .event_init = cstate_pmu_event_init,
  385. .add = cstate_pmu_event_add,
  386. .del = cstate_pmu_event_del,
  387. .start = cstate_pmu_event_start,
  388. .stop = cstate_pmu_event_stop,
  389. .read = cstate_pmu_event_update,
  390. .capabilities = PERF_PMU_CAP_NO_INTERRUPT,
  391. .module = THIS_MODULE,
  392. };
  393. static const struct cstate_model nhm_cstates __initconst = {
  394. .core_events = BIT(PERF_CSTATE_CORE_C3_RES) |
  395. BIT(PERF_CSTATE_CORE_C6_RES),
  396. .pkg_events = BIT(PERF_CSTATE_PKG_C3_RES) |
  397. BIT(PERF_CSTATE_PKG_C6_RES) |
  398. BIT(PERF_CSTATE_PKG_C7_RES),
  399. };
  400. static const struct cstate_model snb_cstates __initconst = {
  401. .core_events = BIT(PERF_CSTATE_CORE_C3_RES) |
  402. BIT(PERF_CSTATE_CORE_C6_RES) |
  403. BIT(PERF_CSTATE_CORE_C7_RES),
  404. .pkg_events = BIT(PERF_CSTATE_PKG_C2_RES) |
  405. BIT(PERF_CSTATE_PKG_C3_RES) |
  406. BIT(PERF_CSTATE_PKG_C6_RES) |
  407. BIT(PERF_CSTATE_PKG_C7_RES),
  408. };
  409. static const struct cstate_model hswult_cstates __initconst = {
  410. .core_events = BIT(PERF_CSTATE_CORE_C3_RES) |
  411. BIT(PERF_CSTATE_CORE_C6_RES) |
  412. BIT(PERF_CSTATE_CORE_C7_RES),
  413. .pkg_events = BIT(PERF_CSTATE_PKG_C2_RES) |
  414. BIT(PERF_CSTATE_PKG_C3_RES) |
  415. BIT(PERF_CSTATE_PKG_C6_RES) |
  416. BIT(PERF_CSTATE_PKG_C7_RES) |
  417. BIT(PERF_CSTATE_PKG_C8_RES) |
  418. BIT(PERF_CSTATE_PKG_C9_RES) |
  419. BIT(PERF_CSTATE_PKG_C10_RES),
  420. };
  421. static const struct cstate_model cnl_cstates __initconst = {
  422. .core_events = BIT(PERF_CSTATE_CORE_C1_RES) |
  423. BIT(PERF_CSTATE_CORE_C3_RES) |
  424. BIT(PERF_CSTATE_CORE_C6_RES) |
  425. BIT(PERF_CSTATE_CORE_C7_RES),
  426. .pkg_events = BIT(PERF_CSTATE_PKG_C2_RES) |
  427. BIT(PERF_CSTATE_PKG_C3_RES) |
  428. BIT(PERF_CSTATE_PKG_C6_RES) |
  429. BIT(PERF_CSTATE_PKG_C7_RES) |
  430. BIT(PERF_CSTATE_PKG_C8_RES) |
  431. BIT(PERF_CSTATE_PKG_C9_RES) |
  432. BIT(PERF_CSTATE_PKG_C10_RES),
  433. };
  434. static const struct cstate_model slm_cstates __initconst = {
  435. .core_events = BIT(PERF_CSTATE_CORE_C1_RES) |
  436. BIT(PERF_CSTATE_CORE_C6_RES),
  437. .pkg_events = BIT(PERF_CSTATE_PKG_C6_RES),
  438. .quirks = SLM_PKG_C6_USE_C7_MSR,
  439. };
  440. static const struct cstate_model knl_cstates __initconst = {
  441. .core_events = BIT(PERF_CSTATE_CORE_C6_RES),
  442. .pkg_events = BIT(PERF_CSTATE_PKG_C2_RES) |
  443. BIT(PERF_CSTATE_PKG_C3_RES) |
  444. BIT(PERF_CSTATE_PKG_C6_RES),
  445. .quirks = KNL_CORE_C6_MSR,
  446. };
  447. static const struct cstate_model glm_cstates __initconst = {
  448. .core_events = BIT(PERF_CSTATE_CORE_C1_RES) |
  449. BIT(PERF_CSTATE_CORE_C3_RES) |
  450. BIT(PERF_CSTATE_CORE_C6_RES),
  451. .pkg_events = BIT(PERF_CSTATE_PKG_C2_RES) |
  452. BIT(PERF_CSTATE_PKG_C3_RES) |
  453. BIT(PERF_CSTATE_PKG_C6_RES) |
  454. BIT(PERF_CSTATE_PKG_C10_RES),
  455. };
  456. #define X86_CSTATES_MODEL(model, states) \
  457. { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, (unsigned long) &(states) }
  458. static const struct x86_cpu_id intel_cstates_match[] __initconst = {
  459. X86_CSTATES_MODEL(INTEL_FAM6_NEHALEM, nhm_cstates),
  460. X86_CSTATES_MODEL(INTEL_FAM6_NEHALEM_EP, nhm_cstates),
  461. X86_CSTATES_MODEL(INTEL_FAM6_NEHALEM_EX, nhm_cstates),
  462. X86_CSTATES_MODEL(INTEL_FAM6_WESTMERE, nhm_cstates),
  463. X86_CSTATES_MODEL(INTEL_FAM6_WESTMERE_EP, nhm_cstates),
  464. X86_CSTATES_MODEL(INTEL_FAM6_WESTMERE_EX, nhm_cstates),
  465. X86_CSTATES_MODEL(INTEL_FAM6_SANDYBRIDGE, snb_cstates),
  466. X86_CSTATES_MODEL(INTEL_FAM6_SANDYBRIDGE_X, snb_cstates),
  467. X86_CSTATES_MODEL(INTEL_FAM6_IVYBRIDGE, snb_cstates),
  468. X86_CSTATES_MODEL(INTEL_FAM6_IVYBRIDGE_X, snb_cstates),
  469. X86_CSTATES_MODEL(INTEL_FAM6_HASWELL_CORE, snb_cstates),
  470. X86_CSTATES_MODEL(INTEL_FAM6_HASWELL_X, snb_cstates),
  471. X86_CSTATES_MODEL(INTEL_FAM6_HASWELL_GT3E, snb_cstates),
  472. X86_CSTATES_MODEL(INTEL_FAM6_HASWELL_ULT, hswult_cstates),
  473. X86_CSTATES_MODEL(INTEL_FAM6_ATOM_SILVERMONT, slm_cstates),
  474. X86_CSTATES_MODEL(INTEL_FAM6_ATOM_SILVERMONT_X, slm_cstates),
  475. X86_CSTATES_MODEL(INTEL_FAM6_ATOM_AIRMONT, slm_cstates),
  476. X86_CSTATES_MODEL(INTEL_FAM6_BROADWELL_CORE, snb_cstates),
  477. X86_CSTATES_MODEL(INTEL_FAM6_BROADWELL_XEON_D, snb_cstates),
  478. X86_CSTATES_MODEL(INTEL_FAM6_BROADWELL_GT3E, snb_cstates),
  479. X86_CSTATES_MODEL(INTEL_FAM6_BROADWELL_X, snb_cstates),
  480. X86_CSTATES_MODEL(INTEL_FAM6_SKYLAKE_MOBILE, snb_cstates),
  481. X86_CSTATES_MODEL(INTEL_FAM6_SKYLAKE_DESKTOP, snb_cstates),
  482. X86_CSTATES_MODEL(INTEL_FAM6_SKYLAKE_X, snb_cstates),
  483. X86_CSTATES_MODEL(INTEL_FAM6_KABYLAKE_MOBILE, hswult_cstates),
  484. X86_CSTATES_MODEL(INTEL_FAM6_KABYLAKE_DESKTOP, hswult_cstates),
  485. X86_CSTATES_MODEL(INTEL_FAM6_CANNONLAKE_MOBILE, cnl_cstates),
  486. X86_CSTATES_MODEL(INTEL_FAM6_XEON_PHI_KNL, knl_cstates),
  487. X86_CSTATES_MODEL(INTEL_FAM6_XEON_PHI_KNM, knl_cstates),
  488. X86_CSTATES_MODEL(INTEL_FAM6_ATOM_GOLDMONT, glm_cstates),
  489. X86_CSTATES_MODEL(INTEL_FAM6_ATOM_GOLDMONT_X, glm_cstates),
  490. X86_CSTATES_MODEL(INTEL_FAM6_ATOM_GOLDMONT_PLUS, glm_cstates),
  491. X86_CSTATES_MODEL(INTEL_FAM6_ICELAKE_MOBILE, snb_cstates),
  492. { },
  493. };
  494. MODULE_DEVICE_TABLE(x86cpu, intel_cstates_match);
  495. /*
  496. * Probe the cstate events and insert the available one into sysfs attrs
  497. * Return false if there are no available events.
  498. */
  499. static bool __init cstate_probe_msr(const unsigned long evmsk, int max,
  500. struct perf_cstate_msr *msr,
  501. struct attribute **attrs)
  502. {
  503. bool found = false;
  504. unsigned int bit;
  505. u64 val;
  506. for (bit = 0; bit < max; bit++) {
  507. if (test_bit(bit, &evmsk) && !rdmsrl_safe(msr[bit].msr, &val)) {
  508. *attrs++ = &msr[bit].attr->attr.attr;
  509. found = true;
  510. } else {
  511. msr[bit].attr = NULL;
  512. }
  513. }
  514. *attrs = NULL;
  515. return found;
  516. }
  517. static int __init cstate_probe(const struct cstate_model *cm)
  518. {
  519. /* SLM has different MSR for PKG C6 */
  520. if (cm->quirks & SLM_PKG_C6_USE_C7_MSR)
  521. pkg_msr[PERF_CSTATE_PKG_C6_RES].msr = MSR_PKG_C7_RESIDENCY;
  522. /* KNL has different MSR for CORE C6 */
  523. if (cm->quirks & KNL_CORE_C6_MSR)
  524. pkg_msr[PERF_CSTATE_CORE_C6_RES].msr = MSR_KNL_CORE_C6_RESIDENCY;
  525. has_cstate_core = cstate_probe_msr(cm->core_events,
  526. PERF_CSTATE_CORE_EVENT_MAX,
  527. core_msr, core_events_attrs);
  528. has_cstate_pkg = cstate_probe_msr(cm->pkg_events,
  529. PERF_CSTATE_PKG_EVENT_MAX,
  530. pkg_msr, pkg_events_attrs);
  531. return (has_cstate_core || has_cstate_pkg) ? 0 : -ENODEV;
  532. }
  533. static inline void cstate_cleanup(void)
  534. {
  535. cpuhp_remove_state_nocalls(CPUHP_AP_PERF_X86_CSTATE_ONLINE);
  536. cpuhp_remove_state_nocalls(CPUHP_AP_PERF_X86_CSTATE_STARTING);
  537. if (has_cstate_core)
  538. perf_pmu_unregister(&cstate_core_pmu);
  539. if (has_cstate_pkg)
  540. perf_pmu_unregister(&cstate_pkg_pmu);
  541. }
  542. static int __init cstate_init(void)
  543. {
  544. int err;
  545. cpuhp_setup_state(CPUHP_AP_PERF_X86_CSTATE_STARTING,
  546. "perf/x86/cstate:starting", cstate_cpu_init, NULL);
  547. cpuhp_setup_state(CPUHP_AP_PERF_X86_CSTATE_ONLINE,
  548. "perf/x86/cstate:online", NULL, cstate_cpu_exit);
  549. if (has_cstate_core) {
  550. err = perf_pmu_register(&cstate_core_pmu, cstate_core_pmu.name, -1);
  551. if (err) {
  552. has_cstate_core = false;
  553. pr_info("Failed to register cstate core pmu\n");
  554. cstate_cleanup();
  555. return err;
  556. }
  557. }
  558. if (has_cstate_pkg) {
  559. err = perf_pmu_register(&cstate_pkg_pmu, cstate_pkg_pmu.name, -1);
  560. if (err) {
  561. has_cstate_pkg = false;
  562. pr_info("Failed to register cstate pkg pmu\n");
  563. cstate_cleanup();
  564. return err;
  565. }
  566. }
  567. return 0;
  568. }
  569. static int __init cstate_pmu_init(void)
  570. {
  571. const struct x86_cpu_id *id;
  572. int err;
  573. if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
  574. return -ENODEV;
  575. id = x86_match_cpu(intel_cstates_match);
  576. if (!id)
  577. return -ENODEV;
  578. err = cstate_probe((const struct cstate_model *) id->driver_data);
  579. if (err)
  580. return err;
  581. return cstate_init();
  582. }
  583. module_init(cstate_pmu_init);
  584. static void __exit cstate_pmu_exit(void)
  585. {
  586. cstate_cleanup();
  587. }
  588. module_exit(cstate_pmu_exit);