rapl.c 22 KB

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