perf_event_intel_cstate.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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,SKL
  51. * Scope: Core
  52. * MSR_CORE_C7_RESIDENCY: CORE C7 Residency Counter
  53. * perf code: 0x03
  54. * Available model: SNB,IVB,HSW,BDW,SKL
  55. * Scope: Core
  56. * MSR_PKG_C2_RESIDENCY: Package C2 Residency Counter.
  57. * perf code: 0x00
  58. * Available model: SNB,IVB,HSW,BDW,SKL
  59. * Scope: Package (physical package)
  60. * MSR_PKG_C3_RESIDENCY: Package C3 Residency Counter.
  61. * perf code: 0x01
  62. * Available model: NHM,WSM,SNB,IVB,HSW,BDW,SKL
  63. * Scope: Package (physical package)
  64. * MSR_PKG_C6_RESIDENCY: Package C6 Residency Counter.
  65. * perf code: 0x02
  66. * Available model: SLM,AMT,NHM,WSM,SNB,IVB,HSW,BDW,SKL
  67. * Scope: Package (physical package)
  68. * MSR_PKG_C7_RESIDENCY: Package C7 Residency Counter.
  69. * perf code: 0x03
  70. * Available model: NHM,WSM,SNB,IVB,HSW,BDW,SKL
  71. * Scope: Package (physical package)
  72. * MSR_PKG_C8_RESIDENCY: Package C8 Residency Counter.
  73. * perf code: 0x04
  74. * Available model: HSW ULT only
  75. * Scope: Package (physical package)
  76. * MSR_PKG_C9_RESIDENCY: Package C9 Residency Counter.
  77. * perf code: 0x05
  78. * Available model: HSW ULT only
  79. * Scope: Package (physical package)
  80. * MSR_PKG_C10_RESIDENCY: Package C10 Residency Counter.
  81. * perf code: 0x06
  82. * Available model: HSW ULT only
  83. * Scope: Package (physical package)
  84. *
  85. */
  86. #include <linux/module.h>
  87. #include <linux/slab.h>
  88. #include <linux/perf_event.h>
  89. #include <asm/cpu_device_id.h>
  90. #include "perf_event.h"
  91. #define DEFINE_CSTATE_FORMAT_ATTR(_var, _name, _format) \
  92. static ssize_t __cstate_##_var##_show(struct kobject *kobj, \
  93. struct kobj_attribute *attr, \
  94. char *page) \
  95. { \
  96. BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \
  97. return sprintf(page, _format "\n"); \
  98. } \
  99. static struct kobj_attribute format_attr_##_var = \
  100. __ATTR(_name, 0444, __cstate_##_var##_show, NULL)
  101. static ssize_t cstate_get_attr_cpumask(struct device *dev,
  102. struct device_attribute *attr,
  103. char *buf);
  104. struct perf_cstate_msr {
  105. u64 msr;
  106. struct perf_pmu_events_attr *attr;
  107. bool (*test)(int idx);
  108. };
  109. /* cstate_core PMU */
  110. static struct pmu cstate_core_pmu;
  111. static bool has_cstate_core;
  112. enum perf_cstate_core_id {
  113. /*
  114. * cstate_core events
  115. */
  116. PERF_CSTATE_CORE_C1_RES = 0,
  117. PERF_CSTATE_CORE_C3_RES,
  118. PERF_CSTATE_CORE_C6_RES,
  119. PERF_CSTATE_CORE_C7_RES,
  120. PERF_CSTATE_CORE_EVENT_MAX,
  121. };
  122. bool test_core(int idx)
  123. {
  124. if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL ||
  125. boot_cpu_data.x86 != 6)
  126. return false;
  127. switch (boot_cpu_data.x86_model) {
  128. case 30: /* 45nm Nehalem */
  129. case 26: /* 45nm Nehalem-EP */
  130. case 46: /* 45nm Nehalem-EX */
  131. case 37: /* 32nm Westmere */
  132. case 44: /* 32nm Westmere-EP */
  133. case 47: /* 32nm Westmere-EX */
  134. if (idx == PERF_CSTATE_CORE_C3_RES ||
  135. idx == PERF_CSTATE_CORE_C6_RES)
  136. return true;
  137. break;
  138. case 42: /* 32nm SandyBridge */
  139. case 45: /* 32nm SandyBridge-E/EN/EP */
  140. case 58: /* 22nm IvyBridge */
  141. case 62: /* 22nm IvyBridge-EP/EX */
  142. case 60: /* 22nm Haswell Core */
  143. case 63: /* 22nm Haswell Server */
  144. case 69: /* 22nm Haswell ULT */
  145. case 70: /* 22nm Haswell + GT3e (Intel Iris Pro graphics) */
  146. case 61: /* 14nm Broadwell Core-M */
  147. case 86: /* 14nm Broadwell Xeon D */
  148. case 71: /* 14nm Broadwell + GT3e (Intel Iris Pro graphics) */
  149. case 79: /* 14nm Broadwell Server */
  150. case 78: /* 14nm Skylake Mobile */
  151. case 94: /* 14nm Skylake Desktop */
  152. if (idx == PERF_CSTATE_CORE_C3_RES ||
  153. idx == PERF_CSTATE_CORE_C6_RES ||
  154. idx == PERF_CSTATE_CORE_C7_RES)
  155. return true;
  156. break;
  157. case 55: /* 22nm Atom "Silvermont" */
  158. case 77: /* 22nm Atom "Silvermont Avoton/Rangely" */
  159. case 76: /* 14nm Atom "Airmont" */
  160. if (idx == PERF_CSTATE_CORE_C1_RES ||
  161. idx == PERF_CSTATE_CORE_C6_RES)
  162. return true;
  163. break;
  164. }
  165. return false;
  166. }
  167. PMU_EVENT_ATTR_STRING(c1-residency, evattr_cstate_core_c1, "event=0x00");
  168. PMU_EVENT_ATTR_STRING(c3-residency, evattr_cstate_core_c3, "event=0x01");
  169. PMU_EVENT_ATTR_STRING(c6-residency, evattr_cstate_core_c6, "event=0x02");
  170. PMU_EVENT_ATTR_STRING(c7-residency, evattr_cstate_core_c7, "event=0x03");
  171. static struct perf_cstate_msr core_msr[] = {
  172. [PERF_CSTATE_CORE_C1_RES] = { MSR_CORE_C1_RES, &evattr_cstate_core_c1, test_core, },
  173. [PERF_CSTATE_CORE_C3_RES] = { MSR_CORE_C3_RESIDENCY, &evattr_cstate_core_c3, test_core, },
  174. [PERF_CSTATE_CORE_C6_RES] = { MSR_CORE_C6_RESIDENCY, &evattr_cstate_core_c6, test_core, },
  175. [PERF_CSTATE_CORE_C7_RES] = { MSR_CORE_C7_RESIDENCY, &evattr_cstate_core_c7, test_core, },
  176. };
  177. static struct attribute *core_events_attrs[PERF_CSTATE_CORE_EVENT_MAX + 1] = {
  178. NULL,
  179. };
  180. static struct attribute_group core_events_attr_group = {
  181. .name = "events",
  182. .attrs = core_events_attrs,
  183. };
  184. DEFINE_CSTATE_FORMAT_ATTR(core_event, event, "config:0-63");
  185. static struct attribute *core_format_attrs[] = {
  186. &format_attr_core_event.attr,
  187. NULL,
  188. };
  189. static struct attribute_group core_format_attr_group = {
  190. .name = "format",
  191. .attrs = core_format_attrs,
  192. };
  193. static cpumask_t cstate_core_cpu_mask;
  194. static DEVICE_ATTR(cpumask, S_IRUGO, cstate_get_attr_cpumask, NULL);
  195. static struct attribute *cstate_cpumask_attrs[] = {
  196. &dev_attr_cpumask.attr,
  197. NULL,
  198. };
  199. static struct attribute_group cpumask_attr_group = {
  200. .attrs = cstate_cpumask_attrs,
  201. };
  202. static const struct attribute_group *core_attr_groups[] = {
  203. &core_events_attr_group,
  204. &core_format_attr_group,
  205. &cpumask_attr_group,
  206. NULL,
  207. };
  208. /* cstate_core PMU end */
  209. /* cstate_pkg PMU */
  210. static struct pmu cstate_pkg_pmu;
  211. static bool has_cstate_pkg;
  212. enum perf_cstate_pkg_id {
  213. /*
  214. * cstate_pkg events
  215. */
  216. PERF_CSTATE_PKG_C2_RES = 0,
  217. PERF_CSTATE_PKG_C3_RES,
  218. PERF_CSTATE_PKG_C6_RES,
  219. PERF_CSTATE_PKG_C7_RES,
  220. PERF_CSTATE_PKG_C8_RES,
  221. PERF_CSTATE_PKG_C9_RES,
  222. PERF_CSTATE_PKG_C10_RES,
  223. PERF_CSTATE_PKG_EVENT_MAX,
  224. };
  225. bool test_pkg(int idx)
  226. {
  227. if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL ||
  228. boot_cpu_data.x86 != 6)
  229. return false;
  230. switch (boot_cpu_data.x86_model) {
  231. case 30: /* 45nm Nehalem */
  232. case 26: /* 45nm Nehalem-EP */
  233. case 46: /* 45nm Nehalem-EX */
  234. case 37: /* 32nm Westmere */
  235. case 44: /* 32nm Westmere-EP */
  236. case 47: /* 32nm Westmere-EX */
  237. if (idx == PERF_CSTATE_CORE_C3_RES ||
  238. idx == PERF_CSTATE_CORE_C6_RES ||
  239. idx == PERF_CSTATE_CORE_C7_RES)
  240. return true;
  241. break;
  242. case 42: /* 32nm SandyBridge */
  243. case 45: /* 32nm SandyBridge-E/EN/EP */
  244. case 58: /* 22nm IvyBridge */
  245. case 62: /* 22nm IvyBridge-EP/EX */
  246. case 60: /* 22nm Haswell Core */
  247. case 63: /* 22nm Haswell Server */
  248. case 70: /* 22nm Haswell + GT3e (Intel Iris Pro graphics) */
  249. case 61: /* 14nm Broadwell Core-M */
  250. case 86: /* 14nm Broadwell Xeon D */
  251. case 71: /* 14nm Broadwell + GT3e (Intel Iris Pro graphics) */
  252. case 79: /* 14nm Broadwell Server */
  253. case 78: /* 14nm Skylake Mobile */
  254. case 94: /* 14nm Skylake Desktop */
  255. if (idx == PERF_CSTATE_PKG_C2_RES ||
  256. idx == PERF_CSTATE_PKG_C3_RES ||
  257. idx == PERF_CSTATE_PKG_C6_RES ||
  258. idx == PERF_CSTATE_PKG_C7_RES)
  259. return true;
  260. break;
  261. case 55: /* 22nm Atom "Silvermont" */
  262. case 77: /* 22nm Atom "Silvermont Avoton/Rangely" */
  263. case 76: /* 14nm Atom "Airmont" */
  264. if (idx == PERF_CSTATE_CORE_C6_RES)
  265. return true;
  266. break;
  267. case 69: /* 22nm Haswell ULT */
  268. if (idx == PERF_CSTATE_PKG_C2_RES ||
  269. idx == PERF_CSTATE_PKG_C3_RES ||
  270. idx == PERF_CSTATE_PKG_C6_RES ||
  271. idx == PERF_CSTATE_PKG_C7_RES ||
  272. idx == PERF_CSTATE_PKG_C8_RES ||
  273. idx == PERF_CSTATE_PKG_C9_RES ||
  274. idx == PERF_CSTATE_PKG_C10_RES)
  275. return true;
  276. break;
  277. }
  278. return false;
  279. }
  280. PMU_EVENT_ATTR_STRING(c2-residency, evattr_cstate_pkg_c2, "event=0x00");
  281. PMU_EVENT_ATTR_STRING(c3-residency, evattr_cstate_pkg_c3, "event=0x01");
  282. PMU_EVENT_ATTR_STRING(c6-residency, evattr_cstate_pkg_c6, "event=0x02");
  283. PMU_EVENT_ATTR_STRING(c7-residency, evattr_cstate_pkg_c7, "event=0x03");
  284. PMU_EVENT_ATTR_STRING(c8-residency, evattr_cstate_pkg_c8, "event=0x04");
  285. PMU_EVENT_ATTR_STRING(c9-residency, evattr_cstate_pkg_c9, "event=0x05");
  286. PMU_EVENT_ATTR_STRING(c10-residency, evattr_cstate_pkg_c10, "event=0x06");
  287. static struct perf_cstate_msr pkg_msr[] = {
  288. [PERF_CSTATE_PKG_C2_RES] = { MSR_PKG_C2_RESIDENCY, &evattr_cstate_pkg_c2, test_pkg, },
  289. [PERF_CSTATE_PKG_C3_RES] = { MSR_PKG_C3_RESIDENCY, &evattr_cstate_pkg_c3, test_pkg, },
  290. [PERF_CSTATE_PKG_C6_RES] = { MSR_PKG_C6_RESIDENCY, &evattr_cstate_pkg_c6, test_pkg, },
  291. [PERF_CSTATE_PKG_C7_RES] = { MSR_PKG_C7_RESIDENCY, &evattr_cstate_pkg_c7, test_pkg, },
  292. [PERF_CSTATE_PKG_C8_RES] = { MSR_PKG_C8_RESIDENCY, &evattr_cstate_pkg_c8, test_pkg, },
  293. [PERF_CSTATE_PKG_C9_RES] = { MSR_PKG_C9_RESIDENCY, &evattr_cstate_pkg_c9, test_pkg, },
  294. [PERF_CSTATE_PKG_C10_RES] = { MSR_PKG_C10_RESIDENCY, &evattr_cstate_pkg_c10, test_pkg, },
  295. };
  296. static struct attribute *pkg_events_attrs[PERF_CSTATE_PKG_EVENT_MAX + 1] = {
  297. NULL,
  298. };
  299. static struct attribute_group pkg_events_attr_group = {
  300. .name = "events",
  301. .attrs = pkg_events_attrs,
  302. };
  303. DEFINE_CSTATE_FORMAT_ATTR(pkg_event, event, "config:0-63");
  304. static struct attribute *pkg_format_attrs[] = {
  305. &format_attr_pkg_event.attr,
  306. NULL,
  307. };
  308. static struct attribute_group pkg_format_attr_group = {
  309. .name = "format",
  310. .attrs = pkg_format_attrs,
  311. };
  312. static cpumask_t cstate_pkg_cpu_mask;
  313. static const struct attribute_group *pkg_attr_groups[] = {
  314. &pkg_events_attr_group,
  315. &pkg_format_attr_group,
  316. &cpumask_attr_group,
  317. NULL,
  318. };
  319. /* cstate_pkg PMU end*/
  320. static ssize_t cstate_get_attr_cpumask(struct device *dev,
  321. struct device_attribute *attr,
  322. char *buf)
  323. {
  324. struct pmu *pmu = dev_get_drvdata(dev);
  325. if (pmu == &cstate_core_pmu)
  326. return cpumap_print_to_pagebuf(true, buf, &cstate_core_cpu_mask);
  327. else if (pmu == &cstate_pkg_pmu)
  328. return cpumap_print_to_pagebuf(true, buf, &cstate_pkg_cpu_mask);
  329. else
  330. return 0;
  331. }
  332. static int cstate_pmu_event_init(struct perf_event *event)
  333. {
  334. u64 cfg = event->attr.config;
  335. int ret = 0;
  336. if (event->attr.type != event->pmu->type)
  337. return -ENOENT;
  338. /* unsupported modes and filters */
  339. if (event->attr.exclude_user ||
  340. event->attr.exclude_kernel ||
  341. event->attr.exclude_hv ||
  342. event->attr.exclude_idle ||
  343. event->attr.exclude_host ||
  344. event->attr.exclude_guest ||
  345. event->attr.sample_period) /* no sampling */
  346. return -EINVAL;
  347. if (event->pmu == &cstate_core_pmu) {
  348. if (cfg >= PERF_CSTATE_CORE_EVENT_MAX)
  349. return -EINVAL;
  350. if (!core_msr[cfg].attr)
  351. return -EINVAL;
  352. event->hw.event_base = core_msr[cfg].msr;
  353. } else if (event->pmu == &cstate_pkg_pmu) {
  354. if (cfg >= PERF_CSTATE_PKG_EVENT_MAX)
  355. return -EINVAL;
  356. if (!pkg_msr[cfg].attr)
  357. return -EINVAL;
  358. event->hw.event_base = pkg_msr[cfg].msr;
  359. } else
  360. return -ENOENT;
  361. /* must be done before validate_group */
  362. event->hw.config = cfg;
  363. event->hw.idx = -1;
  364. return ret;
  365. }
  366. static inline u64 cstate_pmu_read_counter(struct perf_event *event)
  367. {
  368. u64 val;
  369. rdmsrl(event->hw.event_base, val);
  370. return val;
  371. }
  372. static void cstate_pmu_event_update(struct perf_event *event)
  373. {
  374. struct hw_perf_event *hwc = &event->hw;
  375. u64 prev_raw_count, new_raw_count;
  376. again:
  377. prev_raw_count = local64_read(&hwc->prev_count);
  378. new_raw_count = cstate_pmu_read_counter(event);
  379. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  380. new_raw_count) != prev_raw_count)
  381. goto again;
  382. local64_add(new_raw_count - prev_raw_count, &event->count);
  383. }
  384. static void cstate_pmu_event_start(struct perf_event *event, int mode)
  385. {
  386. local64_set(&event->hw.prev_count, cstate_pmu_read_counter(event));
  387. }
  388. static void cstate_pmu_event_stop(struct perf_event *event, int mode)
  389. {
  390. cstate_pmu_event_update(event);
  391. }
  392. static void cstate_pmu_event_del(struct perf_event *event, int mode)
  393. {
  394. cstate_pmu_event_stop(event, PERF_EF_UPDATE);
  395. }
  396. static int cstate_pmu_event_add(struct perf_event *event, int mode)
  397. {
  398. if (mode & PERF_EF_START)
  399. cstate_pmu_event_start(event, mode);
  400. return 0;
  401. }
  402. static void cstate_cpu_exit(int cpu)
  403. {
  404. int i, id, target;
  405. /* cpu exit for cstate core */
  406. if (has_cstate_core) {
  407. id = topology_core_id(cpu);
  408. target = -1;
  409. for_each_online_cpu(i) {
  410. if (i == cpu)
  411. continue;
  412. if (id == topology_core_id(i)) {
  413. target = i;
  414. break;
  415. }
  416. }
  417. if (cpumask_test_and_clear_cpu(cpu, &cstate_core_cpu_mask) && target >= 0)
  418. cpumask_set_cpu(target, &cstate_core_cpu_mask);
  419. WARN_ON(cpumask_empty(&cstate_core_cpu_mask));
  420. if (target >= 0)
  421. perf_pmu_migrate_context(&cstate_core_pmu, cpu, target);
  422. }
  423. /* cpu exit for cstate pkg */
  424. if (has_cstate_pkg) {
  425. id = topology_physical_package_id(cpu);
  426. target = -1;
  427. for_each_online_cpu(i) {
  428. if (i == cpu)
  429. continue;
  430. if (id == topology_physical_package_id(i)) {
  431. target = i;
  432. break;
  433. }
  434. }
  435. if (cpumask_test_and_clear_cpu(cpu, &cstate_pkg_cpu_mask) && target >= 0)
  436. cpumask_set_cpu(target, &cstate_pkg_cpu_mask);
  437. WARN_ON(cpumask_empty(&cstate_pkg_cpu_mask));
  438. if (target >= 0)
  439. perf_pmu_migrate_context(&cstate_pkg_pmu, cpu, target);
  440. }
  441. }
  442. static void cstate_cpu_init(int cpu)
  443. {
  444. int i, id;
  445. /* cpu init for cstate core */
  446. if (has_cstate_core) {
  447. id = topology_core_id(cpu);
  448. for_each_cpu(i, &cstate_core_cpu_mask) {
  449. if (id == topology_core_id(i))
  450. break;
  451. }
  452. if (i >= nr_cpu_ids)
  453. cpumask_set_cpu(cpu, &cstate_core_cpu_mask);
  454. }
  455. /* cpu init for cstate pkg */
  456. if (has_cstate_pkg) {
  457. id = topology_physical_package_id(cpu);
  458. for_each_cpu(i, &cstate_pkg_cpu_mask) {
  459. if (id == topology_physical_package_id(i))
  460. break;
  461. }
  462. if (i >= nr_cpu_ids)
  463. cpumask_set_cpu(cpu, &cstate_pkg_cpu_mask);
  464. }
  465. }
  466. static int cstate_cpu_notifier(struct notifier_block *self,
  467. unsigned long action, void *hcpu)
  468. {
  469. unsigned int cpu = (long)hcpu;
  470. switch (action & ~CPU_TASKS_FROZEN) {
  471. case CPU_UP_PREPARE:
  472. break;
  473. case CPU_STARTING:
  474. cstate_cpu_init(cpu);
  475. break;
  476. case CPU_UP_CANCELED:
  477. case CPU_DYING:
  478. break;
  479. case CPU_ONLINE:
  480. case CPU_DEAD:
  481. break;
  482. case CPU_DOWN_PREPARE:
  483. cstate_cpu_exit(cpu);
  484. break;
  485. default:
  486. break;
  487. }
  488. return NOTIFY_OK;
  489. }
  490. /*
  491. * Probe the cstate events and insert the available one into sysfs attrs
  492. * Return false if there is no available events.
  493. */
  494. static bool cstate_probe_msr(struct perf_cstate_msr *msr,
  495. struct attribute **events_attrs,
  496. int max_event_nr)
  497. {
  498. int i, j = 0;
  499. u64 val;
  500. /* Probe the cstate events. */
  501. for (i = 0; i < max_event_nr; i++) {
  502. if (!msr[i].test(i) || rdmsrl_safe(msr[i].msr, &val))
  503. msr[i].attr = NULL;
  504. }
  505. /* List remaining events in the sysfs attrs. */
  506. for (i = 0; i < max_event_nr; i++) {
  507. if (msr[i].attr)
  508. events_attrs[j++] = &msr[i].attr->attr.attr;
  509. }
  510. events_attrs[j] = NULL;
  511. return (j > 0) ? true : false;
  512. }
  513. static int __init cstate_init(void)
  514. {
  515. /* SLM has different MSR for PKG C6 */
  516. switch (boot_cpu_data.x86_model) {
  517. case 55:
  518. case 76:
  519. case 77:
  520. pkg_msr[PERF_CSTATE_PKG_C6_RES].msr = MSR_PKG_C7_RESIDENCY;
  521. }
  522. if (cstate_probe_msr(core_msr, core_events_attrs, PERF_CSTATE_CORE_EVENT_MAX))
  523. has_cstate_core = true;
  524. if (cstate_probe_msr(pkg_msr, pkg_events_attrs, PERF_CSTATE_PKG_EVENT_MAX))
  525. has_cstate_pkg = true;
  526. return (has_cstate_core || has_cstate_pkg) ? 0 : -ENODEV;
  527. }
  528. static void __init cstate_cpumask_init(void)
  529. {
  530. int cpu;
  531. cpu_notifier_register_begin();
  532. for_each_online_cpu(cpu)
  533. cstate_cpu_init(cpu);
  534. __perf_cpu_notifier(cstate_cpu_notifier);
  535. cpu_notifier_register_done();
  536. }
  537. static struct pmu cstate_core_pmu = {
  538. .attr_groups = core_attr_groups,
  539. .name = "cstate_core",
  540. .task_ctx_nr = perf_invalid_context,
  541. .event_init = cstate_pmu_event_init,
  542. .add = cstate_pmu_event_add, /* must have */
  543. .del = cstate_pmu_event_del, /* must have */
  544. .start = cstate_pmu_event_start,
  545. .stop = cstate_pmu_event_stop,
  546. .read = cstate_pmu_event_update,
  547. .capabilities = PERF_PMU_CAP_NO_INTERRUPT,
  548. };
  549. static struct pmu cstate_pkg_pmu = {
  550. .attr_groups = pkg_attr_groups,
  551. .name = "cstate_pkg",
  552. .task_ctx_nr = perf_invalid_context,
  553. .event_init = cstate_pmu_event_init,
  554. .add = cstate_pmu_event_add, /* must have */
  555. .del = cstate_pmu_event_del, /* must have */
  556. .start = cstate_pmu_event_start,
  557. .stop = cstate_pmu_event_stop,
  558. .read = cstate_pmu_event_update,
  559. .capabilities = PERF_PMU_CAP_NO_INTERRUPT,
  560. };
  561. static void __init cstate_pmus_register(void)
  562. {
  563. int err;
  564. if (has_cstate_core) {
  565. err = perf_pmu_register(&cstate_core_pmu, cstate_core_pmu.name, -1);
  566. if (WARN_ON(err))
  567. pr_info("Failed to register PMU %s error %d\n",
  568. cstate_core_pmu.name, err);
  569. }
  570. if (has_cstate_pkg) {
  571. err = perf_pmu_register(&cstate_pkg_pmu, cstate_pkg_pmu.name, -1);
  572. if (WARN_ON(err))
  573. pr_info("Failed to register PMU %s error %d\n",
  574. cstate_pkg_pmu.name, err);
  575. }
  576. }
  577. static int __init cstate_pmu_init(void)
  578. {
  579. int err;
  580. if (cpu_has_hypervisor)
  581. return -ENODEV;
  582. err = cstate_init();
  583. if (err)
  584. return err;
  585. cstate_cpumask_init();
  586. cstate_pmus_register();
  587. return 0;
  588. }
  589. device_initcall(cstate_pmu_init);