rapl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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. unsigned int pkgid = topology_logical_package_id(cpu);
  147. /*
  148. * The unsigned check also catches the '-1' return value for non
  149. * existent mappings in the topology map.
  150. */
  151. return pkgid < rapl_pmus->maxpkg ? rapl_pmus->pmus[pkgid] : NULL;
  152. }
  153. static inline u64 rapl_read_counter(struct perf_event *event)
  154. {
  155. u64 raw;
  156. rdmsrl(event->hw.event_base, raw);
  157. return raw;
  158. }
  159. static inline u64 rapl_scale(u64 v, int cfg)
  160. {
  161. if (cfg > NR_RAPL_DOMAINS) {
  162. pr_warn("Invalid domain %d, failed to scale data\n", cfg);
  163. return v;
  164. }
  165. /*
  166. * scale delta to smallest unit (1/2^32)
  167. * users must then scale back: count * 1/(1e9*2^32) to get Joules
  168. * or use ldexp(count, -32).
  169. * Watts = Joules/Time delta
  170. */
  171. return v << (32 - rapl_hw_unit[cfg - 1]);
  172. }
  173. static u64 rapl_event_update(struct perf_event *event)
  174. {
  175. struct hw_perf_event *hwc = &event->hw;
  176. u64 prev_raw_count, new_raw_count;
  177. s64 delta, sdelta;
  178. int shift = RAPL_CNTR_WIDTH;
  179. again:
  180. prev_raw_count = local64_read(&hwc->prev_count);
  181. rdmsrl(event->hw.event_base, new_raw_count);
  182. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  183. new_raw_count) != prev_raw_count) {
  184. cpu_relax();
  185. goto again;
  186. }
  187. /*
  188. * Now we have the new raw value and have updated the prev
  189. * timestamp already. We can now calculate the elapsed delta
  190. * (event-)time and add that to the generic event.
  191. *
  192. * Careful, not all hw sign-extends above the physical width
  193. * of the count.
  194. */
  195. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  196. delta >>= shift;
  197. sdelta = rapl_scale(delta, event->hw.config);
  198. local64_add(sdelta, &event->count);
  199. return new_raw_count;
  200. }
  201. static void rapl_start_hrtimer(struct rapl_pmu *pmu)
  202. {
  203. hrtimer_start(&pmu->hrtimer, pmu->timer_interval,
  204. HRTIMER_MODE_REL_PINNED);
  205. }
  206. static enum hrtimer_restart rapl_hrtimer_handle(struct hrtimer *hrtimer)
  207. {
  208. struct rapl_pmu *pmu = container_of(hrtimer, struct rapl_pmu, hrtimer);
  209. struct perf_event *event;
  210. unsigned long flags;
  211. if (!pmu->n_active)
  212. return HRTIMER_NORESTART;
  213. raw_spin_lock_irqsave(&pmu->lock, flags);
  214. list_for_each_entry(event, &pmu->active_list, active_entry)
  215. rapl_event_update(event);
  216. raw_spin_unlock_irqrestore(&pmu->lock, flags);
  217. hrtimer_forward_now(hrtimer, pmu->timer_interval);
  218. return HRTIMER_RESTART;
  219. }
  220. static void rapl_hrtimer_init(struct rapl_pmu *pmu)
  221. {
  222. struct hrtimer *hr = &pmu->hrtimer;
  223. hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  224. hr->function = rapl_hrtimer_handle;
  225. }
  226. static void __rapl_pmu_event_start(struct rapl_pmu *pmu,
  227. struct perf_event *event)
  228. {
  229. if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
  230. return;
  231. event->hw.state = 0;
  232. list_add_tail(&event->active_entry, &pmu->active_list);
  233. local64_set(&event->hw.prev_count, rapl_read_counter(event));
  234. pmu->n_active++;
  235. if (pmu->n_active == 1)
  236. rapl_start_hrtimer(pmu);
  237. }
  238. static void rapl_pmu_event_start(struct perf_event *event, int mode)
  239. {
  240. struct rapl_pmu *pmu = event->pmu_private;
  241. unsigned long flags;
  242. raw_spin_lock_irqsave(&pmu->lock, flags);
  243. __rapl_pmu_event_start(pmu, event);
  244. raw_spin_unlock_irqrestore(&pmu->lock, flags);
  245. }
  246. static void rapl_pmu_event_stop(struct perf_event *event, int mode)
  247. {
  248. struct rapl_pmu *pmu = event->pmu_private;
  249. struct hw_perf_event *hwc = &event->hw;
  250. unsigned long flags;
  251. raw_spin_lock_irqsave(&pmu->lock, flags);
  252. /* mark event as deactivated and stopped */
  253. if (!(hwc->state & PERF_HES_STOPPED)) {
  254. WARN_ON_ONCE(pmu->n_active <= 0);
  255. pmu->n_active--;
  256. if (pmu->n_active == 0)
  257. hrtimer_cancel(&pmu->hrtimer);
  258. list_del(&event->active_entry);
  259. WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
  260. hwc->state |= PERF_HES_STOPPED;
  261. }
  262. /* check if update of sw counter is necessary */
  263. if ((mode & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
  264. /*
  265. * Drain the remaining delta count out of a event
  266. * that we are disabling:
  267. */
  268. rapl_event_update(event);
  269. hwc->state |= PERF_HES_UPTODATE;
  270. }
  271. raw_spin_unlock_irqrestore(&pmu->lock, flags);
  272. }
  273. static int rapl_pmu_event_add(struct perf_event *event, int mode)
  274. {
  275. struct rapl_pmu *pmu = event->pmu_private;
  276. struct hw_perf_event *hwc = &event->hw;
  277. unsigned long flags;
  278. raw_spin_lock_irqsave(&pmu->lock, flags);
  279. hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  280. if (mode & PERF_EF_START)
  281. __rapl_pmu_event_start(pmu, event);
  282. raw_spin_unlock_irqrestore(&pmu->lock, flags);
  283. return 0;
  284. }
  285. static void rapl_pmu_event_del(struct perf_event *event, int flags)
  286. {
  287. rapl_pmu_event_stop(event, PERF_EF_UPDATE);
  288. }
  289. static int rapl_pmu_event_init(struct perf_event *event)
  290. {
  291. u64 cfg = event->attr.config & RAPL_EVENT_MASK;
  292. int bit, msr, ret = 0;
  293. struct rapl_pmu *pmu;
  294. /* only look at RAPL events */
  295. if (event->attr.type != rapl_pmus->pmu.type)
  296. return -ENOENT;
  297. /* check only supported bits are set */
  298. if (event->attr.config & ~RAPL_EVENT_MASK)
  299. return -EINVAL;
  300. if (event->cpu < 0)
  301. return -EINVAL;
  302. event->event_caps |= PERF_EV_CAP_READ_ACTIVE_PKG;
  303. /*
  304. * check event is known (determines counter)
  305. */
  306. switch (cfg) {
  307. case INTEL_RAPL_PP0:
  308. bit = RAPL_IDX_PP0_NRG_STAT;
  309. msr = MSR_PP0_ENERGY_STATUS;
  310. break;
  311. case INTEL_RAPL_PKG:
  312. bit = RAPL_IDX_PKG_NRG_STAT;
  313. msr = MSR_PKG_ENERGY_STATUS;
  314. break;
  315. case INTEL_RAPL_RAM:
  316. bit = RAPL_IDX_RAM_NRG_STAT;
  317. msr = MSR_DRAM_ENERGY_STATUS;
  318. break;
  319. case INTEL_RAPL_PP1:
  320. bit = RAPL_IDX_PP1_NRG_STAT;
  321. msr = MSR_PP1_ENERGY_STATUS;
  322. break;
  323. case INTEL_RAPL_PSYS:
  324. bit = RAPL_IDX_PSYS_NRG_STAT;
  325. msr = MSR_PLATFORM_ENERGY_STATUS;
  326. break;
  327. default:
  328. return -EINVAL;
  329. }
  330. /* check event supported */
  331. if (!(rapl_cntr_mask & (1 << bit)))
  332. return -EINVAL;
  333. /* unsupported modes and filters */
  334. if (event->attr.exclude_user ||
  335. event->attr.exclude_kernel ||
  336. event->attr.exclude_hv ||
  337. event->attr.exclude_idle ||
  338. event->attr.exclude_host ||
  339. event->attr.exclude_guest ||
  340. event->attr.sample_period) /* no sampling */
  341. return -EINVAL;
  342. /* must be done before validate_group */
  343. pmu = cpu_to_rapl_pmu(event->cpu);
  344. if (!pmu)
  345. return -EINVAL;
  346. event->cpu = pmu->cpu;
  347. event->pmu_private = pmu;
  348. event->hw.event_base = msr;
  349. event->hw.config = cfg;
  350. event->hw.idx = bit;
  351. return ret;
  352. }
  353. static void rapl_pmu_event_read(struct perf_event *event)
  354. {
  355. rapl_event_update(event);
  356. }
  357. static ssize_t rapl_get_attr_cpumask(struct device *dev,
  358. struct device_attribute *attr, char *buf)
  359. {
  360. return cpumap_print_to_pagebuf(true, buf, &rapl_cpu_mask);
  361. }
  362. static DEVICE_ATTR(cpumask, S_IRUGO, rapl_get_attr_cpumask, NULL);
  363. static struct attribute *rapl_pmu_attrs[] = {
  364. &dev_attr_cpumask.attr,
  365. NULL,
  366. };
  367. static struct attribute_group rapl_pmu_attr_group = {
  368. .attrs = rapl_pmu_attrs,
  369. };
  370. RAPL_EVENT_ATTR_STR(energy-cores, rapl_cores, "event=0x01");
  371. RAPL_EVENT_ATTR_STR(energy-pkg , rapl_pkg, "event=0x02");
  372. RAPL_EVENT_ATTR_STR(energy-ram , rapl_ram, "event=0x03");
  373. RAPL_EVENT_ATTR_STR(energy-gpu , rapl_gpu, "event=0x04");
  374. RAPL_EVENT_ATTR_STR(energy-psys, rapl_psys, "event=0x05");
  375. RAPL_EVENT_ATTR_STR(energy-cores.unit, rapl_cores_unit, "Joules");
  376. RAPL_EVENT_ATTR_STR(energy-pkg.unit , rapl_pkg_unit, "Joules");
  377. RAPL_EVENT_ATTR_STR(energy-ram.unit , rapl_ram_unit, "Joules");
  378. RAPL_EVENT_ATTR_STR(energy-gpu.unit , rapl_gpu_unit, "Joules");
  379. RAPL_EVENT_ATTR_STR(energy-psys.unit, rapl_psys_unit, "Joules");
  380. /*
  381. * we compute in 0.23 nJ increments regardless of MSR
  382. */
  383. RAPL_EVENT_ATTR_STR(energy-cores.scale, rapl_cores_scale, "2.3283064365386962890625e-10");
  384. RAPL_EVENT_ATTR_STR(energy-pkg.scale, rapl_pkg_scale, "2.3283064365386962890625e-10");
  385. RAPL_EVENT_ATTR_STR(energy-ram.scale, rapl_ram_scale, "2.3283064365386962890625e-10");
  386. RAPL_EVENT_ATTR_STR(energy-gpu.scale, rapl_gpu_scale, "2.3283064365386962890625e-10");
  387. RAPL_EVENT_ATTR_STR(energy-psys.scale, rapl_psys_scale, "2.3283064365386962890625e-10");
  388. static struct attribute *rapl_events_srv_attr[] = {
  389. EVENT_PTR(rapl_cores),
  390. EVENT_PTR(rapl_pkg),
  391. EVENT_PTR(rapl_ram),
  392. EVENT_PTR(rapl_cores_unit),
  393. EVENT_PTR(rapl_pkg_unit),
  394. EVENT_PTR(rapl_ram_unit),
  395. EVENT_PTR(rapl_cores_scale),
  396. EVENT_PTR(rapl_pkg_scale),
  397. EVENT_PTR(rapl_ram_scale),
  398. NULL,
  399. };
  400. static struct attribute *rapl_events_cln_attr[] = {
  401. EVENT_PTR(rapl_cores),
  402. EVENT_PTR(rapl_pkg),
  403. EVENT_PTR(rapl_gpu),
  404. EVENT_PTR(rapl_cores_unit),
  405. EVENT_PTR(rapl_pkg_unit),
  406. EVENT_PTR(rapl_gpu_unit),
  407. EVENT_PTR(rapl_cores_scale),
  408. EVENT_PTR(rapl_pkg_scale),
  409. EVENT_PTR(rapl_gpu_scale),
  410. NULL,
  411. };
  412. static struct attribute *rapl_events_hsw_attr[] = {
  413. EVENT_PTR(rapl_cores),
  414. EVENT_PTR(rapl_pkg),
  415. EVENT_PTR(rapl_gpu),
  416. EVENT_PTR(rapl_ram),
  417. EVENT_PTR(rapl_cores_unit),
  418. EVENT_PTR(rapl_pkg_unit),
  419. EVENT_PTR(rapl_gpu_unit),
  420. EVENT_PTR(rapl_ram_unit),
  421. EVENT_PTR(rapl_cores_scale),
  422. EVENT_PTR(rapl_pkg_scale),
  423. EVENT_PTR(rapl_gpu_scale),
  424. EVENT_PTR(rapl_ram_scale),
  425. NULL,
  426. };
  427. static struct attribute *rapl_events_skl_attr[] = {
  428. EVENT_PTR(rapl_cores),
  429. EVENT_PTR(rapl_pkg),
  430. EVENT_PTR(rapl_gpu),
  431. EVENT_PTR(rapl_ram),
  432. EVENT_PTR(rapl_psys),
  433. EVENT_PTR(rapl_cores_unit),
  434. EVENT_PTR(rapl_pkg_unit),
  435. EVENT_PTR(rapl_gpu_unit),
  436. EVENT_PTR(rapl_ram_unit),
  437. EVENT_PTR(rapl_psys_unit),
  438. EVENT_PTR(rapl_cores_scale),
  439. EVENT_PTR(rapl_pkg_scale),
  440. EVENT_PTR(rapl_gpu_scale),
  441. EVENT_PTR(rapl_ram_scale),
  442. EVENT_PTR(rapl_psys_scale),
  443. NULL,
  444. };
  445. static struct attribute *rapl_events_knl_attr[] = {
  446. EVENT_PTR(rapl_pkg),
  447. EVENT_PTR(rapl_ram),
  448. EVENT_PTR(rapl_pkg_unit),
  449. EVENT_PTR(rapl_ram_unit),
  450. EVENT_PTR(rapl_pkg_scale),
  451. EVENT_PTR(rapl_ram_scale),
  452. NULL,
  453. };
  454. static struct attribute_group rapl_pmu_events_group = {
  455. .name = "events",
  456. .attrs = NULL, /* patched at runtime */
  457. };
  458. DEFINE_RAPL_FORMAT_ATTR(event, event, "config:0-7");
  459. static struct attribute *rapl_formats_attr[] = {
  460. &format_attr_event.attr,
  461. NULL,
  462. };
  463. static struct attribute_group rapl_pmu_format_group = {
  464. .name = "format",
  465. .attrs = rapl_formats_attr,
  466. };
  467. const struct attribute_group *rapl_attr_groups[] = {
  468. &rapl_pmu_attr_group,
  469. &rapl_pmu_format_group,
  470. &rapl_pmu_events_group,
  471. NULL,
  472. };
  473. static int rapl_cpu_offline(unsigned int cpu)
  474. {
  475. struct rapl_pmu *pmu = cpu_to_rapl_pmu(cpu);
  476. int target;
  477. /* Check if exiting cpu is used for collecting rapl events */
  478. if (!cpumask_test_and_clear_cpu(cpu, &rapl_cpu_mask))
  479. return 0;
  480. pmu->cpu = -1;
  481. /* Find a new cpu to collect rapl events */
  482. target = cpumask_any_but(topology_core_cpumask(cpu), cpu);
  483. /* Migrate rapl events to the new target */
  484. if (target < nr_cpu_ids) {
  485. cpumask_set_cpu(target, &rapl_cpu_mask);
  486. pmu->cpu = target;
  487. perf_pmu_migrate_context(pmu->pmu, cpu, target);
  488. }
  489. return 0;
  490. }
  491. static int rapl_cpu_online(unsigned int cpu)
  492. {
  493. struct rapl_pmu *pmu = cpu_to_rapl_pmu(cpu);
  494. int target;
  495. if (!pmu) {
  496. pmu = kzalloc_node(sizeof(*pmu), GFP_KERNEL, cpu_to_node(cpu));
  497. if (!pmu)
  498. return -ENOMEM;
  499. raw_spin_lock_init(&pmu->lock);
  500. INIT_LIST_HEAD(&pmu->active_list);
  501. pmu->pmu = &rapl_pmus->pmu;
  502. pmu->timer_interval = ms_to_ktime(rapl_timer_ms);
  503. rapl_hrtimer_init(pmu);
  504. rapl_pmus->pmus[topology_logical_package_id(cpu)] = pmu;
  505. }
  506. /*
  507. * Check if there is an online cpu in the package which collects rapl
  508. * events already.
  509. */
  510. target = cpumask_any_and(&rapl_cpu_mask, topology_core_cpumask(cpu));
  511. if (target < nr_cpu_ids)
  512. return 0;
  513. cpumask_set_cpu(cpu, &rapl_cpu_mask);
  514. pmu->cpu = cpu;
  515. return 0;
  516. }
  517. static int rapl_check_hw_unit(bool apply_quirk)
  518. {
  519. u64 msr_rapl_power_unit_bits;
  520. int i;
  521. /* protect rdmsrl() to handle virtualization */
  522. if (rdmsrl_safe(MSR_RAPL_POWER_UNIT, &msr_rapl_power_unit_bits))
  523. return -1;
  524. for (i = 0; i < NR_RAPL_DOMAINS; i++)
  525. rapl_hw_unit[i] = (msr_rapl_power_unit_bits >> 8) & 0x1FULL;
  526. /*
  527. * DRAM domain on HSW server and KNL has fixed energy unit which can be
  528. * different than the unit from power unit MSR. See
  529. * "Intel Xeon Processor E5-1600 and E5-2600 v3 Product Families, V2
  530. * of 2. Datasheet, September 2014, Reference Number: 330784-001 "
  531. */
  532. if (apply_quirk)
  533. rapl_hw_unit[RAPL_IDX_RAM_NRG_STAT] = 16;
  534. /*
  535. * Calculate the timer rate:
  536. * Use reference of 200W for scaling the timeout to avoid counter
  537. * overflows. 200W = 200 Joules/sec
  538. * Divide interval by 2 to avoid lockstep (2 * 100)
  539. * if hw unit is 32, then we use 2 ms 1/200/2
  540. */
  541. rapl_timer_ms = 2;
  542. if (rapl_hw_unit[0] < 32) {
  543. rapl_timer_ms = (1000 / (2 * 100));
  544. rapl_timer_ms *= (1ULL << (32 - rapl_hw_unit[0] - 1));
  545. }
  546. return 0;
  547. }
  548. static void __init rapl_advertise(void)
  549. {
  550. int i;
  551. pr_info("API unit is 2^-32 Joules, %d fixed counters, %llu ms ovfl timer\n",
  552. hweight32(rapl_cntr_mask), rapl_timer_ms);
  553. for (i = 0; i < NR_RAPL_DOMAINS; i++) {
  554. if (rapl_cntr_mask & (1 << i)) {
  555. pr_info("hw unit of domain %s 2^-%d Joules\n",
  556. rapl_domain_names[i], rapl_hw_unit[i]);
  557. }
  558. }
  559. }
  560. static void cleanup_rapl_pmus(void)
  561. {
  562. int i;
  563. for (i = 0; i < rapl_pmus->maxpkg; i++)
  564. kfree(rapl_pmus->pmus[i]);
  565. kfree(rapl_pmus);
  566. }
  567. static int __init init_rapl_pmus(void)
  568. {
  569. int maxpkg = topology_max_packages();
  570. size_t size;
  571. size = sizeof(*rapl_pmus) + maxpkg * sizeof(struct rapl_pmu *);
  572. rapl_pmus = kzalloc(size, GFP_KERNEL);
  573. if (!rapl_pmus)
  574. return -ENOMEM;
  575. rapl_pmus->maxpkg = maxpkg;
  576. rapl_pmus->pmu.attr_groups = rapl_attr_groups;
  577. rapl_pmus->pmu.task_ctx_nr = perf_invalid_context;
  578. rapl_pmus->pmu.event_init = rapl_pmu_event_init;
  579. rapl_pmus->pmu.add = rapl_pmu_event_add;
  580. rapl_pmus->pmu.del = rapl_pmu_event_del;
  581. rapl_pmus->pmu.start = rapl_pmu_event_start;
  582. rapl_pmus->pmu.stop = rapl_pmu_event_stop;
  583. rapl_pmus->pmu.read = rapl_pmu_event_read;
  584. rapl_pmus->pmu.module = THIS_MODULE;
  585. return 0;
  586. }
  587. #define X86_RAPL_MODEL_MATCH(model, init) \
  588. { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, (unsigned long)&init }
  589. struct intel_rapl_init_fun {
  590. bool apply_quirk;
  591. int cntr_mask;
  592. struct attribute **attrs;
  593. };
  594. static const struct intel_rapl_init_fun snb_rapl_init __initconst = {
  595. .apply_quirk = false,
  596. .cntr_mask = RAPL_IDX_CLN,
  597. .attrs = rapl_events_cln_attr,
  598. };
  599. static const struct intel_rapl_init_fun hsx_rapl_init __initconst = {
  600. .apply_quirk = true,
  601. .cntr_mask = RAPL_IDX_SRV,
  602. .attrs = rapl_events_srv_attr,
  603. };
  604. static const struct intel_rapl_init_fun hsw_rapl_init __initconst = {
  605. .apply_quirk = false,
  606. .cntr_mask = RAPL_IDX_HSW,
  607. .attrs = rapl_events_hsw_attr,
  608. };
  609. static const struct intel_rapl_init_fun snbep_rapl_init __initconst = {
  610. .apply_quirk = false,
  611. .cntr_mask = RAPL_IDX_SRV,
  612. .attrs = rapl_events_srv_attr,
  613. };
  614. static const struct intel_rapl_init_fun knl_rapl_init __initconst = {
  615. .apply_quirk = true,
  616. .cntr_mask = RAPL_IDX_KNL,
  617. .attrs = rapl_events_knl_attr,
  618. };
  619. static const struct intel_rapl_init_fun skl_rapl_init __initconst = {
  620. .apply_quirk = false,
  621. .cntr_mask = RAPL_IDX_SKL_CLN,
  622. .attrs = rapl_events_skl_attr,
  623. };
  624. static const struct x86_cpu_id rapl_cpu_match[] __initconst = {
  625. X86_RAPL_MODEL_MATCH(INTEL_FAM6_SANDYBRIDGE, snb_rapl_init),
  626. X86_RAPL_MODEL_MATCH(INTEL_FAM6_SANDYBRIDGE_X, snbep_rapl_init),
  627. X86_RAPL_MODEL_MATCH(INTEL_FAM6_IVYBRIDGE, snb_rapl_init),
  628. X86_RAPL_MODEL_MATCH(INTEL_FAM6_IVYBRIDGE_X, snbep_rapl_init),
  629. X86_RAPL_MODEL_MATCH(INTEL_FAM6_HASWELL_CORE, hsw_rapl_init),
  630. X86_RAPL_MODEL_MATCH(INTEL_FAM6_HASWELL_X, hsw_rapl_init),
  631. X86_RAPL_MODEL_MATCH(INTEL_FAM6_HASWELL_ULT, hsw_rapl_init),
  632. X86_RAPL_MODEL_MATCH(INTEL_FAM6_HASWELL_GT3E, hsw_rapl_init),
  633. X86_RAPL_MODEL_MATCH(INTEL_FAM6_BROADWELL_CORE, hsw_rapl_init),
  634. X86_RAPL_MODEL_MATCH(INTEL_FAM6_BROADWELL_GT3E, hsw_rapl_init),
  635. X86_RAPL_MODEL_MATCH(INTEL_FAM6_BROADWELL_X, hsx_rapl_init),
  636. X86_RAPL_MODEL_MATCH(INTEL_FAM6_BROADWELL_XEON_D, hsw_rapl_init),
  637. X86_RAPL_MODEL_MATCH(INTEL_FAM6_XEON_PHI_KNL, knl_rapl_init),
  638. X86_RAPL_MODEL_MATCH(INTEL_FAM6_XEON_PHI_KNM, knl_rapl_init),
  639. X86_RAPL_MODEL_MATCH(INTEL_FAM6_SKYLAKE_MOBILE, skl_rapl_init),
  640. X86_RAPL_MODEL_MATCH(INTEL_FAM6_SKYLAKE_DESKTOP, skl_rapl_init),
  641. X86_RAPL_MODEL_MATCH(INTEL_FAM6_SKYLAKE_X, hsx_rapl_init),
  642. X86_RAPL_MODEL_MATCH(INTEL_FAM6_ATOM_GOLDMONT, hsw_rapl_init),
  643. {},
  644. };
  645. MODULE_DEVICE_TABLE(x86cpu, rapl_cpu_match);
  646. static int __init rapl_pmu_init(void)
  647. {
  648. const struct x86_cpu_id *id;
  649. struct intel_rapl_init_fun *rapl_init;
  650. bool apply_quirk;
  651. int ret;
  652. id = x86_match_cpu(rapl_cpu_match);
  653. if (!id)
  654. return -ENODEV;
  655. rapl_init = (struct intel_rapl_init_fun *)id->driver_data;
  656. apply_quirk = rapl_init->apply_quirk;
  657. rapl_cntr_mask = rapl_init->cntr_mask;
  658. rapl_pmu_events_group.attrs = rapl_init->attrs;
  659. ret = rapl_check_hw_unit(apply_quirk);
  660. if (ret)
  661. return ret;
  662. ret = init_rapl_pmus();
  663. if (ret)
  664. return ret;
  665. /*
  666. * Install callbacks. Core will call them for each online cpu.
  667. */
  668. ret = cpuhp_setup_state(CPUHP_AP_PERF_X86_RAPL_ONLINE,
  669. "AP_PERF_X86_RAPL_ONLINE",
  670. rapl_cpu_online, rapl_cpu_offline);
  671. if (ret)
  672. goto out;
  673. ret = perf_pmu_register(&rapl_pmus->pmu, "power", -1);
  674. if (ret)
  675. goto out1;
  676. rapl_advertise();
  677. return 0;
  678. out1:
  679. cpuhp_remove_state(CPUHP_AP_PERF_X86_RAPL_ONLINE);
  680. out:
  681. pr_warn("Initialization failed (%d), disabled\n", ret);
  682. cleanup_rapl_pmus();
  683. return ret;
  684. }
  685. module_init(rapl_pmu_init);
  686. static void __exit intel_rapl_exit(void)
  687. {
  688. cpuhp_remove_state_nocalls(CPUHP_AP_PERF_X86_RAPL_ONLINE);
  689. perf_pmu_unregister(&rapl_pmus->pmu);
  690. cleanup_rapl_pmus();
  691. }
  692. module_exit(intel_rapl_exit);