rapl.c 24 KB

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