cstate.c 19 KB

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