rapl.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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_DESCRIPTION("Support Intel/AMD RAPL energy consumption counters");
  65. MODULE_LICENSE("GPL");
  66. /*
  67. * RAPL energy status counters
  68. */
  69. enum perf_rapl_events {
  70. PERF_RAPL_PP0 = 0, /* all cores */
  71. PERF_RAPL_PKG, /* entire package */
  72. PERF_RAPL_RAM, /* DRAM */
  73. PERF_RAPL_PP1, /* gpu */
  74. PERF_RAPL_PSYS, /* psys */
  75. PERF_RAPL_MAX,
  76. NR_RAPL_DOMAINS = PERF_RAPL_MAX,
  77. };
  78. static const char *const rapl_domain_names[NR_RAPL_DOMAINS] __initconst = {
  79. "pp0-core",
  80. "package",
  81. "dram",
  82. "pp1-gpu",
  83. "psys",
  84. };
  85. /*
  86. * event code: LSB 8 bits, passed in attr->config
  87. * any other bit is reserved
  88. */
  89. #define RAPL_EVENT_MASK 0xFFULL
  90. #define RAPL_CNTR_WIDTH 32
  91. #define RAPL_EVENT_ATTR_STR(_name, v, str) \
  92. static struct perf_pmu_events_attr event_attr_##v = { \
  93. .attr = __ATTR(_name, 0444, perf_event_sysfs_show, NULL), \
  94. .id = 0, \
  95. .event_str = str, \
  96. };
  97. /*
  98. * RAPL Package energy counter scope:
  99. * 1. AMD/HYGON platforms have a per-PKG package energy counter
  100. * 2. For Intel platforms
  101. * 2.1. CLX-AP is multi-die and its RAPL MSRs are die-scope
  102. * 2.2. Other Intel platforms are single die systems so the scope can be
  103. * considered as either pkg-scope or die-scope, and we are considering
  104. * them as die-scope.
  105. */
  106. #define rapl_pmu_is_pkg_scope() \
  107. (boot_cpu_data.x86_vendor == X86_VENDOR_AMD || \
  108. boot_cpu_data.x86_vendor == X86_VENDOR_HYGON)
  109. struct rapl_pmu {
  110. raw_spinlock_t lock;
  111. int n_active;
  112. int cpu;
  113. struct list_head active_list;
  114. struct pmu *pmu;
  115. ktime_t timer_interval;
  116. struct hrtimer hrtimer;
  117. };
  118. struct rapl_pmus {
  119. struct pmu pmu;
  120. unsigned int nr_rapl_pmu;
  121. struct rapl_pmu *pmus[] __counted_by(nr_rapl_pmu);
  122. };
  123. enum rapl_unit_quirk {
  124. RAPL_UNIT_QUIRK_NONE,
  125. RAPL_UNIT_QUIRK_INTEL_HSW,
  126. RAPL_UNIT_QUIRK_INTEL_SPR,
  127. };
  128. struct rapl_model {
  129. struct perf_msr *rapl_msrs;
  130. unsigned long events;
  131. unsigned int msr_power_unit;
  132. enum rapl_unit_quirk unit_quirk;
  133. };
  134. /* 1/2^hw_unit Joule */
  135. static int rapl_hw_unit[NR_RAPL_DOMAINS] __read_mostly;
  136. static struct rapl_pmus *rapl_pmus;
  137. static cpumask_t rapl_cpu_mask;
  138. static unsigned int rapl_cntr_mask;
  139. static u64 rapl_timer_ms;
  140. static struct perf_msr *rapl_msrs;
  141. /*
  142. * Helper functions to get the correct topology macros according to the
  143. * RAPL PMU scope.
  144. */
  145. static inline unsigned int get_rapl_pmu_idx(int cpu)
  146. {
  147. return rapl_pmu_is_pkg_scope() ? topology_logical_package_id(cpu) :
  148. topology_logical_die_id(cpu);
  149. }
  150. static inline const struct cpumask *get_rapl_pmu_cpumask(int cpu)
  151. {
  152. return rapl_pmu_is_pkg_scope() ? topology_core_cpumask(cpu) :
  153. topology_die_cpumask(cpu);
  154. }
  155. static inline struct rapl_pmu *cpu_to_rapl_pmu(unsigned int cpu)
  156. {
  157. unsigned int rapl_pmu_idx = get_rapl_pmu_idx(cpu);
  158. /*
  159. * The unsigned check also catches the '-1' return value for non
  160. * existent mappings in the topology map.
  161. */
  162. return rapl_pmu_idx < rapl_pmus->nr_rapl_pmu ? rapl_pmus->pmus[rapl_pmu_idx] : NULL;
  163. }
  164. static inline u64 rapl_read_counter(struct perf_event *event)
  165. {
  166. u64 raw;
  167. rdmsrl(event->hw.event_base, raw);
  168. return raw;
  169. }
  170. static inline u64 rapl_scale(u64 v, int cfg)
  171. {
  172. if (cfg > NR_RAPL_DOMAINS) {
  173. pr_warn("Invalid domain %d, failed to scale data\n", cfg);
  174. return v;
  175. }
  176. /*
  177. * scale delta to smallest unit (1/2^32)
  178. * users must then scale back: count * 1/(1e9*2^32) to get Joules
  179. * or use ldexp(count, -32).
  180. * Watts = Joules/Time delta
  181. */
  182. return v << (32 - rapl_hw_unit[cfg - 1]);
  183. }
  184. static u64 rapl_event_update(struct perf_event *event)
  185. {
  186. struct hw_perf_event *hwc = &event->hw;
  187. u64 prev_raw_count, new_raw_count;
  188. s64 delta, sdelta;
  189. int shift = RAPL_CNTR_WIDTH;
  190. prev_raw_count = local64_read(&hwc->prev_count);
  191. do {
  192. rdmsrl(event->hw.event_base, new_raw_count);
  193. } while (!local64_try_cmpxchg(&hwc->prev_count,
  194. &prev_raw_count, new_raw_count));
  195. /*
  196. * Now we have the new raw value and have updated the prev
  197. * timestamp already. We can now calculate the elapsed delta
  198. * (event-)time and add that to the generic event.
  199. *
  200. * Careful, not all hw sign-extends above the physical width
  201. * of the count.
  202. */
  203. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  204. delta >>= shift;
  205. sdelta = rapl_scale(delta, event->hw.config);
  206. local64_add(sdelta, &event->count);
  207. return new_raw_count;
  208. }
  209. static void rapl_start_hrtimer(struct rapl_pmu *pmu)
  210. {
  211. hrtimer_start(&pmu->hrtimer, pmu->timer_interval,
  212. HRTIMER_MODE_REL_PINNED);
  213. }
  214. static enum hrtimer_restart rapl_hrtimer_handle(struct hrtimer *hrtimer)
  215. {
  216. struct rapl_pmu *pmu = container_of(hrtimer, struct rapl_pmu, hrtimer);
  217. struct perf_event *event;
  218. unsigned long flags;
  219. if (!pmu->n_active)
  220. return HRTIMER_NORESTART;
  221. raw_spin_lock_irqsave(&pmu->lock, flags);
  222. list_for_each_entry(event, &pmu->active_list, active_entry)
  223. rapl_event_update(event);
  224. raw_spin_unlock_irqrestore(&pmu->lock, flags);
  225. hrtimer_forward_now(hrtimer, pmu->timer_interval);
  226. return HRTIMER_RESTART;
  227. }
  228. static void rapl_hrtimer_init(struct rapl_pmu *pmu)
  229. {
  230. struct hrtimer *hr = &pmu->hrtimer;
  231. hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  232. hr->function = rapl_hrtimer_handle;
  233. }
  234. static void __rapl_pmu_event_start(struct rapl_pmu *pmu,
  235. struct perf_event *event)
  236. {
  237. if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
  238. return;
  239. event->hw.state = 0;
  240. list_add_tail(&event->active_entry, &pmu->active_list);
  241. local64_set(&event->hw.prev_count, rapl_read_counter(event));
  242. pmu->n_active++;
  243. if (pmu->n_active == 1)
  244. rapl_start_hrtimer(pmu);
  245. }
  246. static void rapl_pmu_event_start(struct perf_event *event, int mode)
  247. {
  248. struct rapl_pmu *pmu = event->pmu_private;
  249. unsigned long flags;
  250. raw_spin_lock_irqsave(&pmu->lock, flags);
  251. __rapl_pmu_event_start(pmu, event);
  252. raw_spin_unlock_irqrestore(&pmu->lock, flags);
  253. }
  254. static void rapl_pmu_event_stop(struct perf_event *event, int mode)
  255. {
  256. struct rapl_pmu *pmu = event->pmu_private;
  257. struct hw_perf_event *hwc = &event->hw;
  258. unsigned long flags;
  259. raw_spin_lock_irqsave(&pmu->lock, flags);
  260. /* mark event as deactivated and stopped */
  261. if (!(hwc->state & PERF_HES_STOPPED)) {
  262. WARN_ON_ONCE(pmu->n_active <= 0);
  263. pmu->n_active--;
  264. if (pmu->n_active == 0)
  265. hrtimer_cancel(&pmu->hrtimer);
  266. list_del(&event->active_entry);
  267. WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
  268. hwc->state |= PERF_HES_STOPPED;
  269. }
  270. /* check if update of sw counter is necessary */
  271. if ((mode & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
  272. /*
  273. * Drain the remaining delta count out of a event
  274. * that we are disabling:
  275. */
  276. rapl_event_update(event);
  277. hwc->state |= PERF_HES_UPTODATE;
  278. }
  279. raw_spin_unlock_irqrestore(&pmu->lock, flags);
  280. }
  281. static int rapl_pmu_event_add(struct perf_event *event, int mode)
  282. {
  283. struct rapl_pmu *pmu = event->pmu_private;
  284. struct hw_perf_event *hwc = &event->hw;
  285. unsigned long flags;
  286. raw_spin_lock_irqsave(&pmu->lock, flags);
  287. hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  288. if (mode & PERF_EF_START)
  289. __rapl_pmu_event_start(pmu, event);
  290. raw_spin_unlock_irqrestore(&pmu->lock, flags);
  291. return 0;
  292. }
  293. static void rapl_pmu_event_del(struct perf_event *event, int flags)
  294. {
  295. rapl_pmu_event_stop(event, PERF_EF_UPDATE);
  296. }
  297. static int rapl_pmu_event_init(struct perf_event *event)
  298. {
  299. u64 cfg = event->attr.config & RAPL_EVENT_MASK;
  300. int bit, ret = 0;
  301. struct rapl_pmu *pmu;
  302. /* only look at RAPL events */
  303. if (event->attr.type != rapl_pmus->pmu.type)
  304. return -ENOENT;
  305. /* check only supported bits are set */
  306. if (event->attr.config & ~RAPL_EVENT_MASK)
  307. return -EINVAL;
  308. if (event->cpu < 0)
  309. return -EINVAL;
  310. event->event_caps |= PERF_EV_CAP_READ_ACTIVE_PKG;
  311. if (!cfg || cfg >= NR_RAPL_DOMAINS + 1)
  312. return -EINVAL;
  313. cfg = array_index_nospec((long)cfg, NR_RAPL_DOMAINS + 1);
  314. bit = cfg - 1;
  315. /* check event supported */
  316. if (!(rapl_cntr_mask & (1 << bit)))
  317. return -EINVAL;
  318. /* unsupported modes and filters */
  319. if (event->attr.sample_period) /* no sampling */
  320. return -EINVAL;
  321. /* must be done before validate_group */
  322. pmu = cpu_to_rapl_pmu(event->cpu);
  323. if (!pmu)
  324. return -EINVAL;
  325. event->cpu = pmu->cpu;
  326. event->pmu_private = pmu;
  327. event->hw.event_base = rapl_msrs[bit].msr;
  328. event->hw.config = cfg;
  329. event->hw.idx = bit;
  330. return ret;
  331. }
  332. static void rapl_pmu_event_read(struct perf_event *event)
  333. {
  334. rapl_event_update(event);
  335. }
  336. static ssize_t rapl_get_attr_cpumask(struct device *dev,
  337. struct device_attribute *attr, char *buf)
  338. {
  339. return cpumap_print_to_pagebuf(true, buf, &rapl_cpu_mask);
  340. }
  341. static DEVICE_ATTR(cpumask, S_IRUGO, rapl_get_attr_cpumask, NULL);
  342. static struct attribute *rapl_pmu_attrs[] = {
  343. &dev_attr_cpumask.attr,
  344. NULL,
  345. };
  346. static struct attribute_group rapl_pmu_attr_group = {
  347. .attrs = rapl_pmu_attrs,
  348. };
  349. RAPL_EVENT_ATTR_STR(energy-cores, rapl_cores, "event=0x01");
  350. RAPL_EVENT_ATTR_STR(energy-pkg , rapl_pkg, "event=0x02");
  351. RAPL_EVENT_ATTR_STR(energy-ram , rapl_ram, "event=0x03");
  352. RAPL_EVENT_ATTR_STR(energy-gpu , rapl_gpu, "event=0x04");
  353. RAPL_EVENT_ATTR_STR(energy-psys, rapl_psys, "event=0x05");
  354. RAPL_EVENT_ATTR_STR(energy-cores.unit, rapl_cores_unit, "Joules");
  355. RAPL_EVENT_ATTR_STR(energy-pkg.unit , rapl_pkg_unit, "Joules");
  356. RAPL_EVENT_ATTR_STR(energy-ram.unit , rapl_ram_unit, "Joules");
  357. RAPL_EVENT_ATTR_STR(energy-gpu.unit , rapl_gpu_unit, "Joules");
  358. RAPL_EVENT_ATTR_STR(energy-psys.unit, rapl_psys_unit, "Joules");
  359. /*
  360. * we compute in 0.23 nJ increments regardless of MSR
  361. */
  362. RAPL_EVENT_ATTR_STR(energy-cores.scale, rapl_cores_scale, "2.3283064365386962890625e-10");
  363. RAPL_EVENT_ATTR_STR(energy-pkg.scale, rapl_pkg_scale, "2.3283064365386962890625e-10");
  364. RAPL_EVENT_ATTR_STR(energy-ram.scale, rapl_ram_scale, "2.3283064365386962890625e-10");
  365. RAPL_EVENT_ATTR_STR(energy-gpu.scale, rapl_gpu_scale, "2.3283064365386962890625e-10");
  366. RAPL_EVENT_ATTR_STR(energy-psys.scale, rapl_psys_scale, "2.3283064365386962890625e-10");
  367. /*
  368. * There are no default events, but we need to create
  369. * "events" group (with empty attrs) before updating
  370. * it with detected events.
  371. */
  372. static struct attribute *attrs_empty[] = {
  373. NULL,
  374. };
  375. static struct attribute_group rapl_pmu_events_group = {
  376. .name = "events",
  377. .attrs = attrs_empty,
  378. };
  379. PMU_FORMAT_ATTR(event, "config:0-7");
  380. static struct attribute *rapl_formats_attr[] = {
  381. &format_attr_event.attr,
  382. NULL,
  383. };
  384. static struct attribute_group rapl_pmu_format_group = {
  385. .name = "format",
  386. .attrs = rapl_formats_attr,
  387. };
  388. static const struct attribute_group *rapl_attr_groups[] = {
  389. &rapl_pmu_attr_group,
  390. &rapl_pmu_format_group,
  391. &rapl_pmu_events_group,
  392. NULL,
  393. };
  394. static struct attribute *rapl_events_cores[] = {
  395. EVENT_PTR(rapl_cores),
  396. EVENT_PTR(rapl_cores_unit),
  397. EVENT_PTR(rapl_cores_scale),
  398. NULL,
  399. };
  400. static struct attribute_group rapl_events_cores_group = {
  401. .name = "events",
  402. .attrs = rapl_events_cores,
  403. };
  404. static struct attribute *rapl_events_pkg[] = {
  405. EVENT_PTR(rapl_pkg),
  406. EVENT_PTR(rapl_pkg_unit),
  407. EVENT_PTR(rapl_pkg_scale),
  408. NULL,
  409. };
  410. static struct attribute_group rapl_events_pkg_group = {
  411. .name = "events",
  412. .attrs = rapl_events_pkg,
  413. };
  414. static struct attribute *rapl_events_ram[] = {
  415. EVENT_PTR(rapl_ram),
  416. EVENT_PTR(rapl_ram_unit),
  417. EVENT_PTR(rapl_ram_scale),
  418. NULL,
  419. };
  420. static struct attribute_group rapl_events_ram_group = {
  421. .name = "events",
  422. .attrs = rapl_events_ram,
  423. };
  424. static struct attribute *rapl_events_gpu[] = {
  425. EVENT_PTR(rapl_gpu),
  426. EVENT_PTR(rapl_gpu_unit),
  427. EVENT_PTR(rapl_gpu_scale),
  428. NULL,
  429. };
  430. static struct attribute_group rapl_events_gpu_group = {
  431. .name = "events",
  432. .attrs = rapl_events_gpu,
  433. };
  434. static struct attribute *rapl_events_psys[] = {
  435. EVENT_PTR(rapl_psys),
  436. EVENT_PTR(rapl_psys_unit),
  437. EVENT_PTR(rapl_psys_scale),
  438. NULL,
  439. };
  440. static struct attribute_group rapl_events_psys_group = {
  441. .name = "events",
  442. .attrs = rapl_events_psys,
  443. };
  444. static bool test_msr(int idx, void *data)
  445. {
  446. return test_bit(idx, (unsigned long *) data);
  447. }
  448. /* Only lower 32bits of the MSR represents the energy counter */
  449. #define RAPL_MSR_MASK 0xFFFFFFFF
  450. static struct perf_msr intel_rapl_msrs[] = {
  451. [PERF_RAPL_PP0] = { MSR_PP0_ENERGY_STATUS, &rapl_events_cores_group, test_msr, false, RAPL_MSR_MASK },
  452. [PERF_RAPL_PKG] = { MSR_PKG_ENERGY_STATUS, &rapl_events_pkg_group, test_msr, false, RAPL_MSR_MASK },
  453. [PERF_RAPL_RAM] = { MSR_DRAM_ENERGY_STATUS, &rapl_events_ram_group, test_msr, false, RAPL_MSR_MASK },
  454. [PERF_RAPL_PP1] = { MSR_PP1_ENERGY_STATUS, &rapl_events_gpu_group, test_msr, false, RAPL_MSR_MASK },
  455. [PERF_RAPL_PSYS] = { MSR_PLATFORM_ENERGY_STATUS, &rapl_events_psys_group, test_msr, false, RAPL_MSR_MASK },
  456. };
  457. static struct perf_msr intel_rapl_spr_msrs[] = {
  458. [PERF_RAPL_PP0] = { MSR_PP0_ENERGY_STATUS, &rapl_events_cores_group, test_msr, false, RAPL_MSR_MASK },
  459. [PERF_RAPL_PKG] = { MSR_PKG_ENERGY_STATUS, &rapl_events_pkg_group, test_msr, false, RAPL_MSR_MASK },
  460. [PERF_RAPL_RAM] = { MSR_DRAM_ENERGY_STATUS, &rapl_events_ram_group, test_msr, false, RAPL_MSR_MASK },
  461. [PERF_RAPL_PP1] = { MSR_PP1_ENERGY_STATUS, &rapl_events_gpu_group, test_msr, false, RAPL_MSR_MASK },
  462. [PERF_RAPL_PSYS] = { MSR_PLATFORM_ENERGY_STATUS, &rapl_events_psys_group, test_msr, true, RAPL_MSR_MASK },
  463. };
  464. /*
  465. * Force to PERF_RAPL_MAX size due to:
  466. * - perf_msr_probe(PERF_RAPL_MAX)
  467. * - want to use same event codes across both architectures
  468. */
  469. static struct perf_msr amd_rapl_msrs[] = {
  470. [PERF_RAPL_PP0] = { 0, &rapl_events_cores_group, NULL, false, 0 },
  471. [PERF_RAPL_PKG] = { MSR_AMD_PKG_ENERGY_STATUS, &rapl_events_pkg_group, test_msr, false, RAPL_MSR_MASK },
  472. [PERF_RAPL_RAM] = { 0, &rapl_events_ram_group, NULL, false, 0 },
  473. [PERF_RAPL_PP1] = { 0, &rapl_events_gpu_group, NULL, false, 0 },
  474. [PERF_RAPL_PSYS] = { 0, &rapl_events_psys_group, NULL, false, 0 },
  475. };
  476. static int rapl_cpu_offline(unsigned int cpu)
  477. {
  478. struct rapl_pmu *pmu = cpu_to_rapl_pmu(cpu);
  479. int target;
  480. /* Check if exiting cpu is used for collecting rapl events */
  481. if (!cpumask_test_and_clear_cpu(cpu, &rapl_cpu_mask))
  482. return 0;
  483. pmu->cpu = -1;
  484. /* Find a new cpu to collect rapl events */
  485. target = cpumask_any_but(get_rapl_pmu_cpumask(cpu), cpu);
  486. /* Migrate rapl events to the new target */
  487. if (target < nr_cpu_ids) {
  488. cpumask_set_cpu(target, &rapl_cpu_mask);
  489. pmu->cpu = target;
  490. perf_pmu_migrate_context(pmu->pmu, cpu, target);
  491. }
  492. return 0;
  493. }
  494. static int rapl_cpu_online(unsigned int cpu)
  495. {
  496. s32 rapl_pmu_idx = get_rapl_pmu_idx(cpu);
  497. if (rapl_pmu_idx < 0) {
  498. pr_err("topology_logical_(package/die)_id() returned a negative value");
  499. return -EINVAL;
  500. }
  501. struct rapl_pmu *pmu = cpu_to_rapl_pmu(cpu);
  502. int target;
  503. if (!pmu) {
  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. rapl_hrtimer_init(pmu);
  512. rapl_pmus->pmus[rapl_pmu_idx] = pmu;
  513. }
  514. /*
  515. * Check if there is an online cpu in the package which collects rapl
  516. * events already.
  517. */
  518. target = cpumask_any_and(&rapl_cpu_mask, get_rapl_pmu_cpumask(cpu));
  519. if (target < nr_cpu_ids)
  520. return 0;
  521. cpumask_set_cpu(cpu, &rapl_cpu_mask);
  522. pmu->cpu = cpu;
  523. return 0;
  524. }
  525. static int rapl_check_hw_unit(struct rapl_model *rm)
  526. {
  527. u64 msr_rapl_power_unit_bits;
  528. int i;
  529. /* protect rdmsrl() to handle virtualization */
  530. if (rdmsrl_safe(rm->msr_power_unit, &msr_rapl_power_unit_bits))
  531. return -1;
  532. for (i = 0; i < NR_RAPL_DOMAINS; i++)
  533. rapl_hw_unit[i] = (msr_rapl_power_unit_bits >> 8) & 0x1FULL;
  534. switch (rm->unit_quirk) {
  535. /*
  536. * DRAM domain on HSW server and KNL has fixed energy unit which can be
  537. * different than the unit from power unit MSR. See
  538. * "Intel Xeon Processor E5-1600 and E5-2600 v3 Product Families, V2
  539. * of 2. Datasheet, September 2014, Reference Number: 330784-001 "
  540. */
  541. case RAPL_UNIT_QUIRK_INTEL_HSW:
  542. rapl_hw_unit[PERF_RAPL_RAM] = 16;
  543. break;
  544. /* SPR uses a fixed energy unit for Psys domain. */
  545. case RAPL_UNIT_QUIRK_INTEL_SPR:
  546. rapl_hw_unit[PERF_RAPL_PSYS] = 0;
  547. break;
  548. default:
  549. break;
  550. }
  551. /*
  552. * Calculate the timer rate:
  553. * Use reference of 200W for scaling the timeout to avoid counter
  554. * overflows. 200W = 200 Joules/sec
  555. * Divide interval by 2 to avoid lockstep (2 * 100)
  556. * if hw unit is 32, then we use 2 ms 1/200/2
  557. */
  558. rapl_timer_ms = 2;
  559. if (rapl_hw_unit[0] < 32) {
  560. rapl_timer_ms = (1000 / (2 * 100));
  561. rapl_timer_ms *= (1ULL << (32 - rapl_hw_unit[0] - 1));
  562. }
  563. return 0;
  564. }
  565. static void __init rapl_advertise(void)
  566. {
  567. int i;
  568. pr_info("API unit is 2^-32 Joules, %d fixed counters, %llu ms ovfl timer\n",
  569. hweight32(rapl_cntr_mask), rapl_timer_ms);
  570. for (i = 0; i < NR_RAPL_DOMAINS; i++) {
  571. if (rapl_cntr_mask & (1 << i)) {
  572. pr_info("hw unit of domain %s 2^-%d Joules\n",
  573. rapl_domain_names[i], rapl_hw_unit[i]);
  574. }
  575. }
  576. }
  577. static void cleanup_rapl_pmus(void)
  578. {
  579. int i;
  580. for (i = 0; i < rapl_pmus->nr_rapl_pmu; i++)
  581. kfree(rapl_pmus->pmus[i]);
  582. kfree(rapl_pmus);
  583. }
  584. static const struct attribute_group *rapl_attr_update[] = {
  585. &rapl_events_cores_group,
  586. &rapl_events_pkg_group,
  587. &rapl_events_ram_group,
  588. &rapl_events_gpu_group,
  589. &rapl_events_psys_group,
  590. NULL,
  591. };
  592. static int __init init_rapl_pmus(void)
  593. {
  594. int nr_rapl_pmu = topology_max_packages();
  595. if (!rapl_pmu_is_pkg_scope())
  596. nr_rapl_pmu *= topology_max_dies_per_package();
  597. rapl_pmus = kzalloc(struct_size(rapl_pmus, pmus, nr_rapl_pmu), GFP_KERNEL);
  598. if (!rapl_pmus)
  599. return -ENOMEM;
  600. rapl_pmus->nr_rapl_pmu = nr_rapl_pmu;
  601. rapl_pmus->pmu.attr_groups = rapl_attr_groups;
  602. rapl_pmus->pmu.attr_update = rapl_attr_update;
  603. rapl_pmus->pmu.task_ctx_nr = perf_invalid_context;
  604. rapl_pmus->pmu.event_init = rapl_pmu_event_init;
  605. rapl_pmus->pmu.add = rapl_pmu_event_add;
  606. rapl_pmus->pmu.del = rapl_pmu_event_del;
  607. rapl_pmus->pmu.start = rapl_pmu_event_start;
  608. rapl_pmus->pmu.stop = rapl_pmu_event_stop;
  609. rapl_pmus->pmu.read = rapl_pmu_event_read;
  610. rapl_pmus->pmu.module = THIS_MODULE;
  611. rapl_pmus->pmu.capabilities = PERF_PMU_CAP_NO_EXCLUDE;
  612. return 0;
  613. }
  614. static struct rapl_model model_snb = {
  615. .events = BIT(PERF_RAPL_PP0) |
  616. BIT(PERF_RAPL_PKG) |
  617. BIT(PERF_RAPL_PP1),
  618. .msr_power_unit = MSR_RAPL_POWER_UNIT,
  619. .rapl_msrs = intel_rapl_msrs,
  620. };
  621. static struct rapl_model model_snbep = {
  622. .events = BIT(PERF_RAPL_PP0) |
  623. BIT(PERF_RAPL_PKG) |
  624. BIT(PERF_RAPL_RAM),
  625. .msr_power_unit = MSR_RAPL_POWER_UNIT,
  626. .rapl_msrs = intel_rapl_msrs,
  627. };
  628. static struct rapl_model model_hsw = {
  629. .events = BIT(PERF_RAPL_PP0) |
  630. BIT(PERF_RAPL_PKG) |
  631. BIT(PERF_RAPL_RAM) |
  632. BIT(PERF_RAPL_PP1),
  633. .msr_power_unit = MSR_RAPL_POWER_UNIT,
  634. .rapl_msrs = intel_rapl_msrs,
  635. };
  636. static struct rapl_model model_hsx = {
  637. .events = BIT(PERF_RAPL_PP0) |
  638. BIT(PERF_RAPL_PKG) |
  639. BIT(PERF_RAPL_RAM),
  640. .unit_quirk = RAPL_UNIT_QUIRK_INTEL_HSW,
  641. .msr_power_unit = MSR_RAPL_POWER_UNIT,
  642. .rapl_msrs = intel_rapl_msrs,
  643. };
  644. static struct rapl_model model_knl = {
  645. .events = BIT(PERF_RAPL_PKG) |
  646. BIT(PERF_RAPL_RAM),
  647. .unit_quirk = RAPL_UNIT_QUIRK_INTEL_HSW,
  648. .msr_power_unit = MSR_RAPL_POWER_UNIT,
  649. .rapl_msrs = intel_rapl_msrs,
  650. };
  651. static struct rapl_model model_skl = {
  652. .events = BIT(PERF_RAPL_PP0) |
  653. BIT(PERF_RAPL_PKG) |
  654. BIT(PERF_RAPL_RAM) |
  655. BIT(PERF_RAPL_PP1) |
  656. BIT(PERF_RAPL_PSYS),
  657. .msr_power_unit = MSR_RAPL_POWER_UNIT,
  658. .rapl_msrs = intel_rapl_msrs,
  659. };
  660. static struct rapl_model model_spr = {
  661. .events = BIT(PERF_RAPL_PP0) |
  662. BIT(PERF_RAPL_PKG) |
  663. BIT(PERF_RAPL_RAM) |
  664. BIT(PERF_RAPL_PSYS),
  665. .unit_quirk = RAPL_UNIT_QUIRK_INTEL_SPR,
  666. .msr_power_unit = MSR_RAPL_POWER_UNIT,
  667. .rapl_msrs = intel_rapl_spr_msrs,
  668. };
  669. static struct rapl_model model_amd_hygon = {
  670. .events = BIT(PERF_RAPL_PKG),
  671. .msr_power_unit = MSR_AMD_RAPL_POWER_UNIT,
  672. .rapl_msrs = amd_rapl_msrs,
  673. };
  674. static const struct x86_cpu_id rapl_model_match[] __initconst = {
  675. X86_MATCH_FEATURE(X86_FEATURE_RAPL, &model_amd_hygon),
  676. X86_MATCH_VFM(INTEL_SANDYBRIDGE, &model_snb),
  677. X86_MATCH_VFM(INTEL_SANDYBRIDGE_X, &model_snbep),
  678. X86_MATCH_VFM(INTEL_IVYBRIDGE, &model_snb),
  679. X86_MATCH_VFM(INTEL_IVYBRIDGE_X, &model_snbep),
  680. X86_MATCH_VFM(INTEL_HASWELL, &model_hsw),
  681. X86_MATCH_VFM(INTEL_HASWELL_X, &model_hsx),
  682. X86_MATCH_VFM(INTEL_HASWELL_L, &model_hsw),
  683. X86_MATCH_VFM(INTEL_HASWELL_G, &model_hsw),
  684. X86_MATCH_VFM(INTEL_BROADWELL, &model_hsw),
  685. X86_MATCH_VFM(INTEL_BROADWELL_G, &model_hsw),
  686. X86_MATCH_VFM(INTEL_BROADWELL_X, &model_hsx),
  687. X86_MATCH_VFM(INTEL_BROADWELL_D, &model_hsx),
  688. X86_MATCH_VFM(INTEL_XEON_PHI_KNL, &model_knl),
  689. X86_MATCH_VFM(INTEL_XEON_PHI_KNM, &model_knl),
  690. X86_MATCH_VFM(INTEL_SKYLAKE_L, &model_skl),
  691. X86_MATCH_VFM(INTEL_SKYLAKE, &model_skl),
  692. X86_MATCH_VFM(INTEL_SKYLAKE_X, &model_hsx),
  693. X86_MATCH_VFM(INTEL_KABYLAKE_L, &model_skl),
  694. X86_MATCH_VFM(INTEL_KABYLAKE, &model_skl),
  695. X86_MATCH_VFM(INTEL_CANNONLAKE_L, &model_skl),
  696. X86_MATCH_VFM(INTEL_ATOM_GOLDMONT, &model_hsw),
  697. X86_MATCH_VFM(INTEL_ATOM_GOLDMONT_D, &model_hsw),
  698. X86_MATCH_VFM(INTEL_ATOM_GOLDMONT_PLUS, &model_hsw),
  699. X86_MATCH_VFM(INTEL_ICELAKE_L, &model_skl),
  700. X86_MATCH_VFM(INTEL_ICELAKE, &model_skl),
  701. X86_MATCH_VFM(INTEL_ICELAKE_D, &model_hsx),
  702. X86_MATCH_VFM(INTEL_ICELAKE_X, &model_hsx),
  703. X86_MATCH_VFM(INTEL_COMETLAKE_L, &model_skl),
  704. X86_MATCH_VFM(INTEL_COMETLAKE, &model_skl),
  705. X86_MATCH_VFM(INTEL_TIGERLAKE_L, &model_skl),
  706. X86_MATCH_VFM(INTEL_TIGERLAKE, &model_skl),
  707. X86_MATCH_VFM(INTEL_ALDERLAKE, &model_skl),
  708. X86_MATCH_VFM(INTEL_ALDERLAKE_L, &model_skl),
  709. X86_MATCH_VFM(INTEL_ATOM_GRACEMONT, &model_skl),
  710. X86_MATCH_VFM(INTEL_SAPPHIRERAPIDS_X, &model_spr),
  711. X86_MATCH_VFM(INTEL_EMERALDRAPIDS_X, &model_spr),
  712. X86_MATCH_VFM(INTEL_RAPTORLAKE, &model_skl),
  713. X86_MATCH_VFM(INTEL_RAPTORLAKE_P, &model_skl),
  714. X86_MATCH_VFM(INTEL_RAPTORLAKE_S, &model_skl),
  715. X86_MATCH_VFM(INTEL_METEORLAKE, &model_skl),
  716. X86_MATCH_VFM(INTEL_METEORLAKE_L, &model_skl),
  717. X86_MATCH_VFM(INTEL_ARROWLAKE_H, &model_skl),
  718. X86_MATCH_VFM(INTEL_ARROWLAKE, &model_skl),
  719. X86_MATCH_VFM(INTEL_LUNARLAKE_M, &model_skl),
  720. {},
  721. };
  722. MODULE_DEVICE_TABLE(x86cpu, rapl_model_match);
  723. static int __init rapl_pmu_init(void)
  724. {
  725. const struct x86_cpu_id *id;
  726. struct rapl_model *rm;
  727. int ret;
  728. id = x86_match_cpu(rapl_model_match);
  729. if (!id)
  730. return -ENODEV;
  731. rm = (struct rapl_model *) id->driver_data;
  732. rapl_msrs = rm->rapl_msrs;
  733. rapl_cntr_mask = perf_msr_probe(rapl_msrs, PERF_RAPL_MAX,
  734. false, (void *) &rm->events);
  735. ret = rapl_check_hw_unit(rm);
  736. if (ret)
  737. return ret;
  738. ret = init_rapl_pmus();
  739. if (ret)
  740. return ret;
  741. /*
  742. * Install callbacks. Core will call them for each online cpu.
  743. */
  744. ret = cpuhp_setup_state(CPUHP_AP_PERF_X86_RAPL_ONLINE,
  745. "perf/x86/rapl:online",
  746. rapl_cpu_online, rapl_cpu_offline);
  747. if (ret)
  748. goto out;
  749. ret = perf_pmu_register(&rapl_pmus->pmu, "power", -1);
  750. if (ret)
  751. goto out1;
  752. rapl_advertise();
  753. return 0;
  754. out1:
  755. cpuhp_remove_state(CPUHP_AP_PERF_X86_RAPL_ONLINE);
  756. out:
  757. pr_warn("Initialization failed (%d), disabled\n", ret);
  758. cleanup_rapl_pmus();
  759. return ret;
  760. }
  761. module_init(rapl_pmu_init);
  762. static void __exit intel_rapl_exit(void)
  763. {
  764. cpuhp_remove_state_nocalls(CPUHP_AP_PERF_X86_RAPL_ONLINE);
  765. perf_pmu_unregister(&rapl_pmus->pmu);
  766. cleanup_rapl_pmus();
  767. }
  768. module_exit(intel_rapl_exit);