rapl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. /*
  2. * perf_event_intel_rapl.c: support Intel RAPL energy consumption counters
  3. * Copyright (C) 2013 Google, Inc., Stephane Eranian
  4. *
  5. * Intel RAPL interface is specified in the IA-32 Manual Vol3b
  6. * section 14.7.1 (September 2013)
  7. *
  8. * RAPL provides more controls than just reporting energy consumption
  9. * however here we only expose the 3 energy consumption free running
  10. * counters (pp0, pkg, dram).
  11. *
  12. * Each of those counters increments in a power unit defined by the
  13. * RAPL_POWER_UNIT MSR. On SandyBridge, this unit is 1/(2^16) Joules
  14. * but it can vary.
  15. *
  16. * Counter to rapl events mappings:
  17. *
  18. * pp0 counter: consumption of all physical cores (power plane 0)
  19. * event: rapl_energy_cores
  20. * perf code: 0x1
  21. *
  22. * pkg counter: consumption of the whole processor package
  23. * event: rapl_energy_pkg
  24. * perf code: 0x2
  25. *
  26. * dram counter: consumption of the dram domain (servers only)
  27. * event: rapl_energy_dram
  28. * perf code: 0x3
  29. *
  30. * gpu counter: consumption of the builtin-gpu domain (client only)
  31. * event: rapl_energy_gpu
  32. * perf code: 0x4
  33. *
  34. * psys counter: consumption of the builtin-psys domain (client only)
  35. * event: rapl_energy_psys
  36. * perf code: 0x5
  37. *
  38. * We manage those counters as free running (read-only). They may be
  39. * use simultaneously by other tools, such as turbostat.
  40. *
  41. * The events only support system-wide mode counting. There is no
  42. * sampling support because it does not make sense and is not
  43. * supported by the RAPL hardware.
  44. *
  45. * Because we want to avoid floating-point operations in the kernel,
  46. * the events are all reported in fixed point arithmetic (32.32).
  47. * Tools must adjust the counts to convert them to Watts using
  48. * the duration of the measurement. Tools may use a function such as
  49. * ldexp(raw_count, -32);
  50. */
  51. #define pr_fmt(fmt) "RAPL PMU: " fmt
  52. #include <linux/module.h>
  53. #include <linux/slab.h>
  54. #include <linux/perf_event.h>
  55. #include <asm/cpu_device_id.h>
  56. #include <asm/intel-family.h>
  57. #include "../perf_event.h"
  58. MODULE_LICENSE("GPL");
  59. /*
  60. * RAPL energy status counters
  61. */
  62. #define RAPL_IDX_PP0_NRG_STAT 0 /* all cores */
  63. #define INTEL_RAPL_PP0 0x1 /* pseudo-encoding */
  64. #define RAPL_IDX_PKG_NRG_STAT 1 /* entire package */
  65. #define INTEL_RAPL_PKG 0x2 /* pseudo-encoding */
  66. #define RAPL_IDX_RAM_NRG_STAT 2 /* DRAM */
  67. #define INTEL_RAPL_RAM 0x3 /* pseudo-encoding */
  68. #define RAPL_IDX_PP1_NRG_STAT 3 /* gpu */
  69. #define INTEL_RAPL_PP1 0x4 /* pseudo-encoding */
  70. #define RAPL_IDX_PSYS_NRG_STAT 4 /* psys */
  71. #define INTEL_RAPL_PSYS 0x5 /* pseudo-encoding */
  72. #define NR_RAPL_DOMAINS 0x5
  73. static const char *const rapl_domain_names[NR_RAPL_DOMAINS] __initconst = {
  74. "pp0-core",
  75. "package",
  76. "dram",
  77. "pp1-gpu",
  78. "psys",
  79. };
  80. /* Clients have PP0, PKG */
  81. #define RAPL_IDX_CLN (1<<RAPL_IDX_PP0_NRG_STAT|\
  82. 1<<RAPL_IDX_PKG_NRG_STAT|\
  83. 1<<RAPL_IDX_PP1_NRG_STAT)
  84. /* Servers have PP0, PKG, RAM */
  85. #define RAPL_IDX_SRV (1<<RAPL_IDX_PP0_NRG_STAT|\
  86. 1<<RAPL_IDX_PKG_NRG_STAT|\
  87. 1<<RAPL_IDX_RAM_NRG_STAT)
  88. /* Servers have PP0, PKG, RAM, PP1 */
  89. #define RAPL_IDX_HSW (1<<RAPL_IDX_PP0_NRG_STAT|\
  90. 1<<RAPL_IDX_PKG_NRG_STAT|\
  91. 1<<RAPL_IDX_RAM_NRG_STAT|\
  92. 1<<RAPL_IDX_PP1_NRG_STAT)
  93. /* SKL clients have PP0, PKG, RAM, PP1, PSYS */
  94. #define RAPL_IDX_SKL_CLN (1<<RAPL_IDX_PP0_NRG_STAT|\
  95. 1<<RAPL_IDX_PKG_NRG_STAT|\
  96. 1<<RAPL_IDX_RAM_NRG_STAT|\
  97. 1<<RAPL_IDX_PP1_NRG_STAT|\
  98. 1<<RAPL_IDX_PSYS_NRG_STAT)
  99. /* Knights Landing has PKG, RAM */
  100. #define RAPL_IDX_KNL (1<<RAPL_IDX_PKG_NRG_STAT|\
  101. 1<<RAPL_IDX_RAM_NRG_STAT)
  102. /*
  103. * event code: LSB 8 bits, passed in attr->config
  104. * any other bit is reserved
  105. */
  106. #define RAPL_EVENT_MASK 0xFFULL
  107. #define DEFINE_RAPL_FORMAT_ATTR(_var, _name, _format) \
  108. static ssize_t __rapl_##_var##_show(struct kobject *kobj, \
  109. struct kobj_attribute *attr, \
  110. char *page) \
  111. { \
  112. BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \
  113. return sprintf(page, _format "\n"); \
  114. } \
  115. static struct kobj_attribute format_attr_##_var = \
  116. __ATTR(_name, 0444, __rapl_##_var##_show, NULL)
  117. #define RAPL_CNTR_WIDTH 32
  118. #define RAPL_EVENT_ATTR_STR(_name, v, str) \
  119. static struct perf_pmu_events_attr event_attr_##v = { \
  120. .attr = __ATTR(_name, 0444, perf_event_sysfs_show, NULL), \
  121. .id = 0, \
  122. .event_str = str, \
  123. };
  124. struct rapl_pmu {
  125. raw_spinlock_t lock;
  126. int n_active;
  127. int cpu;
  128. struct list_head active_list;
  129. struct pmu *pmu;
  130. ktime_t timer_interval;
  131. struct hrtimer hrtimer;
  132. };
  133. struct rapl_pmus {
  134. struct pmu pmu;
  135. unsigned int maxpkg;
  136. struct rapl_pmu *pmus[];
  137. };
  138. /* 1/2^hw_unit Joule */
  139. static int rapl_hw_unit[NR_RAPL_DOMAINS] __read_mostly;
  140. static struct rapl_pmus *rapl_pmus;
  141. static cpumask_t rapl_cpu_mask;
  142. static unsigned int rapl_cntr_mask;
  143. static u64 rapl_timer_ms;
  144. static inline struct rapl_pmu *cpu_to_rapl_pmu(unsigned int cpu)
  145. {
  146. return rapl_pmus->pmus[topology_logical_package_id(cpu)];
  147. }
  148. static inline u64 rapl_read_counter(struct perf_event *event)
  149. {
  150. u64 raw;
  151. rdmsrl(event->hw.event_base, raw);
  152. return raw;
  153. }
  154. static inline u64 rapl_scale(u64 v, int cfg)
  155. {
  156. if (cfg > NR_RAPL_DOMAINS) {
  157. pr_warn("Invalid domain %d, failed to scale data\n", cfg);
  158. return v;
  159. }
  160. /*
  161. * scale delta to smallest unit (1/2^32)
  162. * users must then scale back: count * 1/(1e9*2^32) to get Joules
  163. * or use ldexp(count, -32).
  164. * Watts = Joules/Time delta
  165. */
  166. return v << (32 - rapl_hw_unit[cfg - 1]);
  167. }
  168. static u64 rapl_event_update(struct perf_event *event)
  169. {
  170. struct hw_perf_event *hwc = &event->hw;
  171. u64 prev_raw_count, new_raw_count;
  172. s64 delta, sdelta;
  173. int shift = RAPL_CNTR_WIDTH;
  174. again:
  175. prev_raw_count = local64_read(&hwc->prev_count);
  176. rdmsrl(event->hw.event_base, new_raw_count);
  177. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  178. new_raw_count) != prev_raw_count) {
  179. cpu_relax();
  180. goto again;
  181. }
  182. /*
  183. * Now we have the new raw value and have updated the prev
  184. * timestamp already. We can now calculate the elapsed delta
  185. * (event-)time and add that to the generic event.
  186. *
  187. * Careful, not all hw sign-extends above the physical width
  188. * of the count.
  189. */
  190. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  191. delta >>= shift;
  192. sdelta = rapl_scale(delta, event->hw.config);
  193. local64_add(sdelta, &event->count);
  194. return new_raw_count;
  195. }
  196. static void rapl_start_hrtimer(struct rapl_pmu *pmu)
  197. {
  198. hrtimer_start(&pmu->hrtimer, pmu->timer_interval,
  199. HRTIMER_MODE_REL_PINNED);
  200. }
  201. static enum hrtimer_restart rapl_hrtimer_handle(struct hrtimer *hrtimer)
  202. {
  203. struct rapl_pmu *pmu = container_of(hrtimer, struct rapl_pmu, hrtimer);
  204. struct perf_event *event;
  205. unsigned long flags;
  206. if (!pmu->n_active)
  207. return HRTIMER_NORESTART;
  208. raw_spin_lock_irqsave(&pmu->lock, flags);
  209. list_for_each_entry(event, &pmu->active_list, active_entry)
  210. rapl_event_update(event);
  211. raw_spin_unlock_irqrestore(&pmu->lock, flags);
  212. hrtimer_forward_now(hrtimer, pmu->timer_interval);
  213. return HRTIMER_RESTART;
  214. }
  215. static void rapl_hrtimer_init(struct rapl_pmu *pmu)
  216. {
  217. struct hrtimer *hr = &pmu->hrtimer;
  218. hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  219. hr->function = rapl_hrtimer_handle;
  220. }
  221. static void __rapl_pmu_event_start(struct rapl_pmu *pmu,
  222. struct perf_event *event)
  223. {
  224. if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
  225. return;
  226. event->hw.state = 0;
  227. list_add_tail(&event->active_entry, &pmu->active_list);
  228. local64_set(&event->hw.prev_count, rapl_read_counter(event));
  229. pmu->n_active++;
  230. if (pmu->n_active == 1)
  231. rapl_start_hrtimer(pmu);
  232. }
  233. static void rapl_pmu_event_start(struct perf_event *event, int mode)
  234. {
  235. struct rapl_pmu *pmu = event->pmu_private;
  236. unsigned long flags;
  237. raw_spin_lock_irqsave(&pmu->lock, flags);
  238. __rapl_pmu_event_start(pmu, event);
  239. raw_spin_unlock_irqrestore(&pmu->lock, flags);
  240. }
  241. static void rapl_pmu_event_stop(struct perf_event *event, int mode)
  242. {
  243. struct rapl_pmu *pmu = event->pmu_private;
  244. struct hw_perf_event *hwc = &event->hw;
  245. unsigned long flags;
  246. raw_spin_lock_irqsave(&pmu->lock, flags);
  247. /* mark event as deactivated and stopped */
  248. if (!(hwc->state & PERF_HES_STOPPED)) {
  249. WARN_ON_ONCE(pmu->n_active <= 0);
  250. pmu->n_active--;
  251. if (pmu->n_active == 0)
  252. hrtimer_cancel(&pmu->hrtimer);
  253. list_del(&event->active_entry);
  254. WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
  255. hwc->state |= PERF_HES_STOPPED;
  256. }
  257. /* check if update of sw counter is necessary */
  258. if ((mode & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
  259. /*
  260. * Drain the remaining delta count out of a event
  261. * that we are disabling:
  262. */
  263. rapl_event_update(event);
  264. hwc->state |= PERF_HES_UPTODATE;
  265. }
  266. raw_spin_unlock_irqrestore(&pmu->lock, flags);
  267. }
  268. static int rapl_pmu_event_add(struct perf_event *event, int mode)
  269. {
  270. struct rapl_pmu *pmu = event->pmu_private;
  271. struct hw_perf_event *hwc = &event->hw;
  272. unsigned long flags;
  273. raw_spin_lock_irqsave(&pmu->lock, flags);
  274. hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  275. if (mode & PERF_EF_START)
  276. __rapl_pmu_event_start(pmu, event);
  277. raw_spin_unlock_irqrestore(&pmu->lock, flags);
  278. return 0;
  279. }
  280. static void rapl_pmu_event_del(struct perf_event *event, int flags)
  281. {
  282. rapl_pmu_event_stop(event, PERF_EF_UPDATE);
  283. }
  284. static int rapl_pmu_event_init(struct perf_event *event)
  285. {
  286. u64 cfg = event->attr.config & RAPL_EVENT_MASK;
  287. int bit, msr, ret = 0;
  288. struct rapl_pmu *pmu;
  289. /* only look at RAPL events */
  290. if (event->attr.type != rapl_pmus->pmu.type)
  291. return -ENOENT;
  292. /* check only supported bits are set */
  293. if (event->attr.config & ~RAPL_EVENT_MASK)
  294. return -EINVAL;
  295. if (event->cpu < 0)
  296. return -EINVAL;
  297. event->event_caps |= PERF_EV_CAP_READ_ACTIVE_PKG;
  298. /*
  299. * check event is known (determines counter)
  300. */
  301. switch (cfg) {
  302. case INTEL_RAPL_PP0:
  303. bit = RAPL_IDX_PP0_NRG_STAT;
  304. msr = MSR_PP0_ENERGY_STATUS;
  305. break;
  306. case INTEL_RAPL_PKG:
  307. bit = RAPL_IDX_PKG_NRG_STAT;
  308. msr = MSR_PKG_ENERGY_STATUS;
  309. break;
  310. case INTEL_RAPL_RAM:
  311. bit = RAPL_IDX_RAM_NRG_STAT;
  312. msr = MSR_DRAM_ENERGY_STATUS;
  313. break;
  314. case INTEL_RAPL_PP1:
  315. bit = RAPL_IDX_PP1_NRG_STAT;
  316. msr = MSR_PP1_ENERGY_STATUS;
  317. break;
  318. case INTEL_RAPL_PSYS:
  319. bit = RAPL_IDX_PSYS_NRG_STAT;
  320. msr = MSR_PLATFORM_ENERGY_STATUS;
  321. break;
  322. default:
  323. return -EINVAL;
  324. }
  325. /* check event supported */
  326. if (!(rapl_cntr_mask & (1 << bit)))
  327. return -EINVAL;
  328. /* unsupported modes and filters */
  329. if (event->attr.exclude_user ||
  330. event->attr.exclude_kernel ||
  331. event->attr.exclude_hv ||
  332. event->attr.exclude_idle ||
  333. event->attr.exclude_host ||
  334. event->attr.exclude_guest ||
  335. event->attr.sample_period) /* no sampling */
  336. return -EINVAL;
  337. /* must be done before validate_group */
  338. pmu = cpu_to_rapl_pmu(event->cpu);
  339. event->cpu = pmu->cpu;
  340. event->pmu_private = pmu;
  341. event->hw.event_base = msr;
  342. event->hw.config = cfg;
  343. event->hw.idx = bit;
  344. return ret;
  345. }
  346. static void rapl_pmu_event_read(struct perf_event *event)
  347. {
  348. rapl_event_update(event);
  349. }
  350. static ssize_t rapl_get_attr_cpumask(struct device *dev,
  351. struct device_attribute *attr, char *buf)
  352. {
  353. return cpumap_print_to_pagebuf(true, buf, &rapl_cpu_mask);
  354. }
  355. static DEVICE_ATTR(cpumask, S_IRUGO, rapl_get_attr_cpumask, NULL);
  356. static struct attribute *rapl_pmu_attrs[] = {
  357. &dev_attr_cpumask.attr,
  358. NULL,
  359. };
  360. static struct attribute_group rapl_pmu_attr_group = {
  361. .attrs = rapl_pmu_attrs,
  362. };
  363. RAPL_EVENT_ATTR_STR(energy-cores, rapl_cores, "event=0x01");
  364. RAPL_EVENT_ATTR_STR(energy-pkg , rapl_pkg, "event=0x02");
  365. RAPL_EVENT_ATTR_STR(energy-ram , rapl_ram, "event=0x03");
  366. RAPL_EVENT_ATTR_STR(energy-gpu , rapl_gpu, "event=0x04");
  367. RAPL_EVENT_ATTR_STR(energy-psys, rapl_psys, "event=0x05");
  368. RAPL_EVENT_ATTR_STR(energy-cores.unit, rapl_cores_unit, "Joules");
  369. RAPL_EVENT_ATTR_STR(energy-pkg.unit , rapl_pkg_unit, "Joules");
  370. RAPL_EVENT_ATTR_STR(energy-ram.unit , rapl_ram_unit, "Joules");
  371. RAPL_EVENT_ATTR_STR(energy-gpu.unit , rapl_gpu_unit, "Joules");
  372. RAPL_EVENT_ATTR_STR(energy-psys.unit, rapl_psys_unit, "Joules");
  373. /*
  374. * we compute in 0.23 nJ increments regardless of MSR
  375. */
  376. RAPL_EVENT_ATTR_STR(energy-cores.scale, rapl_cores_scale, "2.3283064365386962890625e-10");
  377. RAPL_EVENT_ATTR_STR(energy-pkg.scale, rapl_pkg_scale, "2.3283064365386962890625e-10");
  378. RAPL_EVENT_ATTR_STR(energy-ram.scale, rapl_ram_scale, "2.3283064365386962890625e-10");
  379. RAPL_EVENT_ATTR_STR(energy-gpu.scale, rapl_gpu_scale, "2.3283064365386962890625e-10");
  380. RAPL_EVENT_ATTR_STR(energy-psys.scale, rapl_psys_scale, "2.3283064365386962890625e-10");
  381. static struct attribute *rapl_events_srv_attr[] = {
  382. EVENT_PTR(rapl_cores),
  383. EVENT_PTR(rapl_pkg),
  384. EVENT_PTR(rapl_ram),
  385. EVENT_PTR(rapl_cores_unit),
  386. EVENT_PTR(rapl_pkg_unit),
  387. EVENT_PTR(rapl_ram_unit),
  388. EVENT_PTR(rapl_cores_scale),
  389. EVENT_PTR(rapl_pkg_scale),
  390. EVENT_PTR(rapl_ram_scale),
  391. NULL,
  392. };
  393. static struct attribute *rapl_events_cln_attr[] = {
  394. EVENT_PTR(rapl_cores),
  395. EVENT_PTR(rapl_pkg),
  396. EVENT_PTR(rapl_gpu),
  397. EVENT_PTR(rapl_cores_unit),
  398. EVENT_PTR(rapl_pkg_unit),
  399. EVENT_PTR(rapl_gpu_unit),
  400. EVENT_PTR(rapl_cores_scale),
  401. EVENT_PTR(rapl_pkg_scale),
  402. EVENT_PTR(rapl_gpu_scale),
  403. NULL,
  404. };
  405. static struct attribute *rapl_events_hsw_attr[] = {
  406. EVENT_PTR(rapl_cores),
  407. EVENT_PTR(rapl_pkg),
  408. EVENT_PTR(rapl_gpu),
  409. EVENT_PTR(rapl_ram),
  410. EVENT_PTR(rapl_cores_unit),
  411. EVENT_PTR(rapl_pkg_unit),
  412. EVENT_PTR(rapl_gpu_unit),
  413. EVENT_PTR(rapl_ram_unit),
  414. EVENT_PTR(rapl_cores_scale),
  415. EVENT_PTR(rapl_pkg_scale),
  416. EVENT_PTR(rapl_gpu_scale),
  417. EVENT_PTR(rapl_ram_scale),
  418. NULL,
  419. };
  420. static struct attribute *rapl_events_skl_attr[] = {
  421. EVENT_PTR(rapl_cores),
  422. EVENT_PTR(rapl_pkg),
  423. EVENT_PTR(rapl_gpu),
  424. EVENT_PTR(rapl_ram),
  425. EVENT_PTR(rapl_psys),
  426. EVENT_PTR(rapl_cores_unit),
  427. EVENT_PTR(rapl_pkg_unit),
  428. EVENT_PTR(rapl_gpu_unit),
  429. EVENT_PTR(rapl_ram_unit),
  430. EVENT_PTR(rapl_psys_unit),
  431. EVENT_PTR(rapl_cores_scale),
  432. EVENT_PTR(rapl_pkg_scale),
  433. EVENT_PTR(rapl_gpu_scale),
  434. EVENT_PTR(rapl_ram_scale),
  435. EVENT_PTR(rapl_psys_scale),
  436. NULL,
  437. };
  438. static struct attribute *rapl_events_knl_attr[] = {
  439. EVENT_PTR(rapl_pkg),
  440. EVENT_PTR(rapl_ram),
  441. EVENT_PTR(rapl_pkg_unit),
  442. EVENT_PTR(rapl_ram_unit),
  443. EVENT_PTR(rapl_pkg_scale),
  444. EVENT_PTR(rapl_ram_scale),
  445. NULL,
  446. };
  447. static struct attribute_group rapl_pmu_events_group = {
  448. .name = "events",
  449. .attrs = NULL, /* patched at runtime */
  450. };
  451. DEFINE_RAPL_FORMAT_ATTR(event, event, "config:0-7");
  452. static struct attribute *rapl_formats_attr[] = {
  453. &format_attr_event.attr,
  454. NULL,
  455. };
  456. static struct attribute_group rapl_pmu_format_group = {
  457. .name = "format",
  458. .attrs = rapl_formats_attr,
  459. };
  460. const struct attribute_group *rapl_attr_groups[] = {
  461. &rapl_pmu_attr_group,
  462. &rapl_pmu_format_group,
  463. &rapl_pmu_events_group,
  464. NULL,
  465. };
  466. static int rapl_cpu_offline(unsigned int cpu)
  467. {
  468. struct rapl_pmu *pmu = cpu_to_rapl_pmu(cpu);
  469. int target;
  470. /* Check if exiting cpu is used for collecting rapl events */
  471. if (!cpumask_test_and_clear_cpu(cpu, &rapl_cpu_mask))
  472. return 0;
  473. pmu->cpu = -1;
  474. /* Find a new cpu to collect rapl events */
  475. target = cpumask_any_but(topology_core_cpumask(cpu), cpu);
  476. /* Migrate rapl events to the new target */
  477. if (target < nr_cpu_ids) {
  478. cpumask_set_cpu(target, &rapl_cpu_mask);
  479. pmu->cpu = target;
  480. perf_pmu_migrate_context(pmu->pmu, cpu, target);
  481. }
  482. return 0;
  483. }
  484. static int rapl_cpu_online(unsigned int cpu)
  485. {
  486. struct rapl_pmu *pmu = cpu_to_rapl_pmu(cpu);
  487. int target;
  488. /*
  489. * Check if there is an online cpu in the package which collects rapl
  490. * events already.
  491. */
  492. target = cpumask_any_and(&rapl_cpu_mask, topology_core_cpumask(cpu));
  493. if (target < nr_cpu_ids)
  494. return 0;
  495. cpumask_set_cpu(cpu, &rapl_cpu_mask);
  496. pmu->cpu = cpu;
  497. return 0;
  498. }
  499. static int rapl_cpu_prepare(unsigned int cpu)
  500. {
  501. struct rapl_pmu *pmu = cpu_to_rapl_pmu(cpu);
  502. if (pmu)
  503. return 0;
  504. pmu = kzalloc_node(sizeof(*pmu), GFP_KERNEL, cpu_to_node(cpu));
  505. if (!pmu)
  506. return -ENOMEM;
  507. raw_spin_lock_init(&pmu->lock);
  508. INIT_LIST_HEAD(&pmu->active_list);
  509. pmu->pmu = &rapl_pmus->pmu;
  510. pmu->timer_interval = ms_to_ktime(rapl_timer_ms);
  511. pmu->cpu = -1;
  512. rapl_hrtimer_init(pmu);
  513. rapl_pmus->pmus[topology_logical_package_id(cpu)] = pmu;
  514. return 0;
  515. }
  516. static int rapl_check_hw_unit(bool apply_quirk)
  517. {
  518. u64 msr_rapl_power_unit_bits;
  519. int i;
  520. /* protect rdmsrl() to handle virtualization */
  521. if (rdmsrl_safe(MSR_RAPL_POWER_UNIT, &msr_rapl_power_unit_bits))
  522. return -1;
  523. for (i = 0; i < NR_RAPL_DOMAINS; i++)
  524. rapl_hw_unit[i] = (msr_rapl_power_unit_bits >> 8) & 0x1FULL;
  525. /*
  526. * DRAM domain on HSW server and KNL has fixed energy unit which can be
  527. * different than the unit from power unit MSR. See
  528. * "Intel Xeon Processor E5-1600 and E5-2600 v3 Product Families, V2
  529. * of 2. Datasheet, September 2014, Reference Number: 330784-001 "
  530. */
  531. if (apply_quirk)
  532. rapl_hw_unit[RAPL_IDX_RAM_NRG_STAT] = 16;
  533. /*
  534. * Calculate the timer rate:
  535. * Use reference of 200W for scaling the timeout to avoid counter
  536. * overflows. 200W = 200 Joules/sec
  537. * Divide interval by 2 to avoid lockstep (2 * 100)
  538. * if hw unit is 32, then we use 2 ms 1/200/2
  539. */
  540. rapl_timer_ms = 2;
  541. if (rapl_hw_unit[0] < 32) {
  542. rapl_timer_ms = (1000 / (2 * 100));
  543. rapl_timer_ms *= (1ULL << (32 - rapl_hw_unit[0] - 1));
  544. }
  545. return 0;
  546. }
  547. static void __init rapl_advertise(void)
  548. {
  549. int i;
  550. pr_info("API unit is 2^-32 Joules, %d fixed counters, %llu ms ovfl timer\n",
  551. hweight32(rapl_cntr_mask), rapl_timer_ms);
  552. for (i = 0; i < NR_RAPL_DOMAINS; i++) {
  553. if (rapl_cntr_mask & (1 << i)) {
  554. pr_info("hw unit of domain %s 2^-%d Joules\n",
  555. rapl_domain_names[i], rapl_hw_unit[i]);
  556. }
  557. }
  558. }
  559. static void cleanup_rapl_pmus(void)
  560. {
  561. int i;
  562. for (i = 0; i < rapl_pmus->maxpkg; i++)
  563. kfree(rapl_pmus->pmus[i]);
  564. kfree(rapl_pmus);
  565. }
  566. static int __init init_rapl_pmus(void)
  567. {
  568. int maxpkg = topology_max_packages();
  569. size_t size;
  570. size = sizeof(*rapl_pmus) + maxpkg * sizeof(struct rapl_pmu *);
  571. rapl_pmus = kzalloc(size, GFP_KERNEL);
  572. if (!rapl_pmus)
  573. return -ENOMEM;
  574. rapl_pmus->maxpkg = maxpkg;
  575. rapl_pmus->pmu.attr_groups = rapl_attr_groups;
  576. rapl_pmus->pmu.task_ctx_nr = perf_invalid_context;
  577. rapl_pmus->pmu.event_init = rapl_pmu_event_init;
  578. rapl_pmus->pmu.add = rapl_pmu_event_add;
  579. rapl_pmus->pmu.del = rapl_pmu_event_del;
  580. rapl_pmus->pmu.start = rapl_pmu_event_start;
  581. rapl_pmus->pmu.stop = rapl_pmu_event_stop;
  582. rapl_pmus->pmu.read = rapl_pmu_event_read;
  583. return 0;
  584. }
  585. #define X86_RAPL_MODEL_MATCH(model, init) \
  586. { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, (unsigned long)&init }
  587. struct intel_rapl_init_fun {
  588. bool apply_quirk;
  589. int cntr_mask;
  590. struct attribute **attrs;
  591. };
  592. static const struct intel_rapl_init_fun snb_rapl_init __initconst = {
  593. .apply_quirk = false,
  594. .cntr_mask = RAPL_IDX_CLN,
  595. .attrs = rapl_events_cln_attr,
  596. };
  597. static const struct intel_rapl_init_fun hsx_rapl_init __initconst = {
  598. .apply_quirk = true,
  599. .cntr_mask = RAPL_IDX_SRV,
  600. .attrs = rapl_events_srv_attr,
  601. };
  602. static const struct intel_rapl_init_fun hsw_rapl_init __initconst = {
  603. .apply_quirk = false,
  604. .cntr_mask = RAPL_IDX_HSW,
  605. .attrs = rapl_events_hsw_attr,
  606. };
  607. static const struct intel_rapl_init_fun snbep_rapl_init __initconst = {
  608. .apply_quirk = false,
  609. .cntr_mask = RAPL_IDX_SRV,
  610. .attrs = rapl_events_srv_attr,
  611. };
  612. static const struct intel_rapl_init_fun knl_rapl_init __initconst = {
  613. .apply_quirk = true,
  614. .cntr_mask = RAPL_IDX_KNL,
  615. .attrs = rapl_events_knl_attr,
  616. };
  617. static const struct intel_rapl_init_fun skl_rapl_init __initconst = {
  618. .apply_quirk = false,
  619. .cntr_mask = RAPL_IDX_SKL_CLN,
  620. .attrs = rapl_events_skl_attr,
  621. };
  622. static const struct x86_cpu_id rapl_cpu_match[] __initconst = {
  623. X86_RAPL_MODEL_MATCH(INTEL_FAM6_SANDYBRIDGE, snb_rapl_init),
  624. X86_RAPL_MODEL_MATCH(INTEL_FAM6_SANDYBRIDGE_X, snbep_rapl_init),
  625. X86_RAPL_MODEL_MATCH(INTEL_FAM6_IVYBRIDGE, snb_rapl_init),
  626. X86_RAPL_MODEL_MATCH(INTEL_FAM6_IVYBRIDGE_X, snbep_rapl_init),
  627. X86_RAPL_MODEL_MATCH(INTEL_FAM6_HASWELL_CORE, hsw_rapl_init),
  628. X86_RAPL_MODEL_MATCH(INTEL_FAM6_HASWELL_X, hsw_rapl_init),
  629. X86_RAPL_MODEL_MATCH(INTEL_FAM6_HASWELL_ULT, hsw_rapl_init),
  630. X86_RAPL_MODEL_MATCH(INTEL_FAM6_HASWELL_GT3E, hsw_rapl_init),
  631. X86_RAPL_MODEL_MATCH(INTEL_FAM6_BROADWELL_CORE, hsw_rapl_init),
  632. X86_RAPL_MODEL_MATCH(INTEL_FAM6_BROADWELL_GT3E, hsw_rapl_init),
  633. X86_RAPL_MODEL_MATCH(INTEL_FAM6_BROADWELL_X, hsw_rapl_init),
  634. X86_RAPL_MODEL_MATCH(INTEL_FAM6_BROADWELL_XEON_D, hsw_rapl_init),
  635. X86_RAPL_MODEL_MATCH(INTEL_FAM6_XEON_PHI_KNL, knl_rapl_init),
  636. X86_RAPL_MODEL_MATCH(INTEL_FAM6_XEON_PHI_KNM, knl_rapl_init),
  637. X86_RAPL_MODEL_MATCH(INTEL_FAM6_SKYLAKE_MOBILE, skl_rapl_init),
  638. X86_RAPL_MODEL_MATCH(INTEL_FAM6_SKYLAKE_DESKTOP, skl_rapl_init),
  639. X86_RAPL_MODEL_MATCH(INTEL_FAM6_SKYLAKE_X, hsx_rapl_init),
  640. X86_RAPL_MODEL_MATCH(INTEL_FAM6_ATOM_GOLDMONT, hsw_rapl_init),
  641. {},
  642. };
  643. MODULE_DEVICE_TABLE(x86cpu, rapl_cpu_match);
  644. static int __init rapl_pmu_init(void)
  645. {
  646. const struct x86_cpu_id *id;
  647. struct intel_rapl_init_fun *rapl_init;
  648. bool apply_quirk;
  649. int ret;
  650. id = x86_match_cpu(rapl_cpu_match);
  651. if (!id)
  652. return -ENODEV;
  653. rapl_init = (struct intel_rapl_init_fun *)id->driver_data;
  654. apply_quirk = rapl_init->apply_quirk;
  655. rapl_cntr_mask = rapl_init->cntr_mask;
  656. rapl_pmu_events_group.attrs = rapl_init->attrs;
  657. ret = rapl_check_hw_unit(apply_quirk);
  658. if (ret)
  659. return ret;
  660. ret = init_rapl_pmus();
  661. if (ret)
  662. return ret;
  663. /*
  664. * Install callbacks. Core will call them for each online cpu.
  665. */
  666. ret = cpuhp_setup_state(CPUHP_PERF_X86_RAPL_PREP, "perf/x86/rapl:prepare",
  667. rapl_cpu_prepare, NULL);
  668. if (ret)
  669. goto out;
  670. ret = cpuhp_setup_state(CPUHP_AP_PERF_X86_RAPL_ONLINE,
  671. "perf/x86/rapl:online",
  672. rapl_cpu_online, rapl_cpu_offline);
  673. if (ret)
  674. goto out1;
  675. ret = perf_pmu_register(&rapl_pmus->pmu, "power", -1);
  676. if (ret)
  677. goto out2;
  678. rapl_advertise();
  679. return 0;
  680. out2:
  681. cpuhp_remove_state(CPUHP_AP_PERF_X86_RAPL_ONLINE);
  682. out1:
  683. cpuhp_remove_state(CPUHP_PERF_X86_RAPL_PREP);
  684. out:
  685. pr_warn("Initialization failed (%d), disabled\n", ret);
  686. cleanup_rapl_pmus();
  687. return ret;
  688. }
  689. module_init(rapl_pmu_init);
  690. static void __exit intel_rapl_exit(void)
  691. {
  692. cpuhp_remove_state_nocalls(CPUHP_AP_PERF_X86_RAPL_ONLINE);
  693. cpuhp_remove_state_nocalls(CPUHP_PERF_X86_RAPL_PREP);
  694. perf_pmu_unregister(&rapl_pmus->pmu);
  695. cleanup_rapl_pmus();
  696. }
  697. module_exit(intel_rapl_exit);