cstate.c 19 KB

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