Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 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 | * |
Srinivas Pandruvada | dcee75b | 2016-04-17 15:03:00 -0700 | [diff] [blame] | 30 | * gpu counter: consumption of the builtin-gpu domain (client only) |
Stephane Eranian | f228c5b | 2014-01-08 11:15:53 +0100 | [diff] [blame] | 31 | * event: rapl_energy_gpu |
| 32 | * perf code: 0x4 |
| 33 | * |
Srinivas Pandruvada | dcee75b | 2016-04-17 15:03:00 -0700 | [diff] [blame] | 34 | * psys counter: consumption of the builtin-psys domain (client only) |
| 35 | * event: rapl_energy_psys |
| 36 | * perf code: 0x5 |
| 37 | * |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 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 | */ |
Thomas Gleixner | 512089d | 2016-02-22 22:19:23 +0000 | [diff] [blame] | 51 | |
| 52 | #define pr_fmt(fmt) "RAPL PMU: " fmt |
| 53 | |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 54 | #include <linux/module.h> |
| 55 | #include <linux/slab.h> |
| 56 | #include <linux/perf_event.h> |
| 57 | #include <asm/cpu_device_id.h> |
Borislav Petkov | 27f6d22 | 2016-02-10 10:55:23 +0100 | [diff] [blame] | 58 | #include "../perf_event.h" |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 59 | |
Kan Liang | 4b6e257 | 2016-03-19 00:20:50 -0700 | [diff] [blame] | 60 | MODULE_LICENSE("GPL"); |
| 61 | |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 62 | /* |
| 63 | * RAPL energy status counters |
| 64 | */ |
| 65 | #define RAPL_IDX_PP0_NRG_STAT 0 /* all cores */ |
| 66 | #define INTEL_RAPL_PP0 0x1 /* pseudo-encoding */ |
| 67 | #define RAPL_IDX_PKG_NRG_STAT 1 /* entire package */ |
| 68 | #define INTEL_RAPL_PKG 0x2 /* pseudo-encoding */ |
| 69 | #define RAPL_IDX_RAM_NRG_STAT 2 /* DRAM */ |
| 70 | #define INTEL_RAPL_RAM 0x3 /* pseudo-encoding */ |
Vince Weaver | e69af46 | 2014-04-02 00:49:55 -0400 | [diff] [blame] | 71 | #define RAPL_IDX_PP1_NRG_STAT 3 /* gpu */ |
Stephane Eranian | f228c5b | 2014-01-08 11:15:53 +0100 | [diff] [blame] | 72 | #define INTEL_RAPL_PP1 0x4 /* pseudo-encoding */ |
Srinivas Pandruvada | dcee75b | 2016-04-17 15:03:00 -0700 | [diff] [blame] | 73 | #define RAPL_IDX_PSYS_NRG_STAT 4 /* psys */ |
| 74 | #define INTEL_RAPL_PSYS 0x5 /* pseudo-encoding */ |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 75 | |
Srinivas Pandruvada | dcee75b | 2016-04-17 15:03:00 -0700 | [diff] [blame] | 76 | #define NR_RAPL_DOMAINS 0x5 |
Andi Kleen | da008ee | 2015-11-30 09:48:42 -0800 | [diff] [blame] | 77 | static const char *const rapl_domain_names[NR_RAPL_DOMAINS] __initconst = { |
Jacob Pan | 6455239 | 2015-03-26 14:28:45 -0700 | [diff] [blame] | 78 | "pp0-core", |
| 79 | "package", |
| 80 | "dram", |
| 81 | "pp1-gpu", |
Srinivas Pandruvada | dcee75b | 2016-04-17 15:03:00 -0700 | [diff] [blame] | 82 | "psys", |
Jacob Pan | 6455239 | 2015-03-26 14:28:45 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 85 | /* Clients have PP0, PKG */ |
| 86 | #define RAPL_IDX_CLN (1<<RAPL_IDX_PP0_NRG_STAT|\ |
Stephane Eranian | f228c5b | 2014-01-08 11:15:53 +0100 | [diff] [blame] | 87 | 1<<RAPL_IDX_PKG_NRG_STAT|\ |
| 88 | 1<<RAPL_IDX_PP1_NRG_STAT) |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 89 | |
| 90 | /* Servers have PP0, PKG, RAM */ |
| 91 | #define RAPL_IDX_SRV (1<<RAPL_IDX_PP0_NRG_STAT|\ |
| 92 | 1<<RAPL_IDX_PKG_NRG_STAT|\ |
| 93 | 1<<RAPL_IDX_RAM_NRG_STAT) |
| 94 | |
Vince Weaver | e69af46 | 2014-04-02 00:49:55 -0400 | [diff] [blame] | 95 | /* Servers have PP0, PKG, RAM, PP1 */ |
| 96 | #define RAPL_IDX_HSW (1<<RAPL_IDX_PP0_NRG_STAT|\ |
| 97 | 1<<RAPL_IDX_PKG_NRG_STAT|\ |
| 98 | 1<<RAPL_IDX_RAM_NRG_STAT|\ |
| 99 | 1<<RAPL_IDX_PP1_NRG_STAT) |
| 100 | |
Srinivas Pandruvada | dcee75b | 2016-04-17 15:03:00 -0700 | [diff] [blame] | 101 | /* SKL clients have PP0, PKG, RAM, PP1, PSYS */ |
| 102 | #define RAPL_IDX_SKL_CLN (1<<RAPL_IDX_PP0_NRG_STAT|\ |
| 103 | 1<<RAPL_IDX_PKG_NRG_STAT|\ |
| 104 | 1<<RAPL_IDX_RAM_NRG_STAT|\ |
| 105 | 1<<RAPL_IDX_PP1_NRG_STAT|\ |
| 106 | 1<<RAPL_IDX_PSYS_NRG_STAT) |
| 107 | |
Dasaratharaman Chandramouli | 3a2a779 | 2015-05-26 11:47:39 -0700 | [diff] [blame] | 108 | /* Knights Landing has PKG, RAM */ |
| 109 | #define RAPL_IDX_KNL (1<<RAPL_IDX_PKG_NRG_STAT|\ |
| 110 | 1<<RAPL_IDX_RAM_NRG_STAT) |
| 111 | |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 112 | /* |
| 113 | * event code: LSB 8 bits, passed in attr->config |
| 114 | * any other bit is reserved |
| 115 | */ |
| 116 | #define RAPL_EVENT_MASK 0xFFULL |
| 117 | |
| 118 | #define DEFINE_RAPL_FORMAT_ATTR(_var, _name, _format) \ |
| 119 | static ssize_t __rapl_##_var##_show(struct kobject *kobj, \ |
| 120 | struct kobj_attribute *attr, \ |
| 121 | char *page) \ |
| 122 | { \ |
| 123 | BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \ |
| 124 | return sprintf(page, _format "\n"); \ |
| 125 | } \ |
| 126 | static struct kobj_attribute format_attr_##_var = \ |
| 127 | __ATTR(_name, 0444, __rapl_##_var##_show, NULL) |
| 128 | |
Thomas Gleixner | 7162b8f | 2016-02-22 22:19:24 +0000 | [diff] [blame] | 129 | #define RAPL_CNTR_WIDTH 32 |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 130 | |
Huang Rui | d3bcd64 | 2015-12-04 18:07:41 +0800 | [diff] [blame] | 131 | #define RAPL_EVENT_ATTR_STR(_name, v, str) \ |
| 132 | static struct perf_pmu_events_attr event_attr_##v = { \ |
| 133 | .attr = __ATTR(_name, 0444, perf_event_sysfs_show, NULL), \ |
| 134 | .id = 0, \ |
| 135 | .event_str = str, \ |
Stephane Eranian | 433678b | 2015-01-13 23:59:53 +0100 | [diff] [blame] | 136 | }; |
| 137 | |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 138 | struct rapl_pmu { |
Thomas Gleixner | a208749 | 2016-02-22 22:19:25 +0000 | [diff] [blame] | 139 | raw_spinlock_t lock; |
Thomas Gleixner | 7162b8f | 2016-02-22 22:19:24 +0000 | [diff] [blame] | 140 | int n_active; |
Thomas Gleixner | 8a6d2f8 | 2016-02-22 22:19:25 +0000 | [diff] [blame] | 141 | int cpu; |
Thomas Gleixner | 7162b8f | 2016-02-22 22:19:24 +0000 | [diff] [blame] | 142 | struct list_head active_list; |
| 143 | struct pmu *pmu; |
| 144 | ktime_t timer_interval; |
| 145 | struct hrtimer hrtimer; |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 146 | }; |
| 147 | |
Thomas Gleixner | 9de8d68 | 2016-02-22 22:19:26 +0000 | [diff] [blame] | 148 | struct rapl_pmus { |
| 149 | struct pmu pmu; |
| 150 | unsigned int maxpkg; |
| 151 | struct rapl_pmu *pmus[]; |
| 152 | }; |
| 153 | |
Thomas Gleixner | 7162b8f | 2016-02-22 22:19:24 +0000 | [diff] [blame] | 154 | /* 1/2^hw_unit Joule */ |
| 155 | static int rapl_hw_unit[NR_RAPL_DOMAINS] __read_mostly; |
Thomas Gleixner | 9de8d68 | 2016-02-22 22:19:26 +0000 | [diff] [blame] | 156 | static struct rapl_pmus *rapl_pmus; |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 157 | static cpumask_t rapl_cpu_mask; |
Thomas Gleixner | 9de8d68 | 2016-02-22 22:19:26 +0000 | [diff] [blame] | 158 | static unsigned int rapl_cntr_mask; |
Thomas Gleixner | 75c7003 | 2016-02-22 22:19:22 +0000 | [diff] [blame] | 159 | static u64 rapl_timer_ms; |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 160 | |
Thomas Gleixner | 9de8d68 | 2016-02-22 22:19:26 +0000 | [diff] [blame] | 161 | static inline struct rapl_pmu *cpu_to_rapl_pmu(unsigned int cpu) |
| 162 | { |
| 163 | return rapl_pmus->pmus[topology_logical_package_id(cpu)]; |
| 164 | } |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 165 | |
| 166 | static inline u64 rapl_read_counter(struct perf_event *event) |
| 167 | { |
| 168 | u64 raw; |
| 169 | rdmsrl(event->hw.event_base, raw); |
| 170 | return raw; |
| 171 | } |
| 172 | |
Jacob Pan | 6455239 | 2015-03-26 14:28:45 -0700 | [diff] [blame] | 173 | static inline u64 rapl_scale(u64 v, int cfg) |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 174 | { |
Jacob Pan | 6455239 | 2015-03-26 14:28:45 -0700 | [diff] [blame] | 175 | if (cfg > NR_RAPL_DOMAINS) { |
Thomas Gleixner | 512089d | 2016-02-22 22:19:23 +0000 | [diff] [blame] | 176 | pr_warn("Invalid domain %d, failed to scale data\n", cfg); |
Jacob Pan | 6455239 | 2015-03-26 14:28:45 -0700 | [diff] [blame] | 177 | return v; |
| 178 | } |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 179 | /* |
| 180 | * scale delta to smallest unit (1/2^32) |
| 181 | * users must then scale back: count * 1/(1e9*2^32) to get Joules |
| 182 | * or use ldexp(count, -32). |
| 183 | * Watts = Joules/Time delta |
| 184 | */ |
Jacob Pan | 6455239 | 2015-03-26 14:28:45 -0700 | [diff] [blame] | 185 | return v << (32 - rapl_hw_unit[cfg - 1]); |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | static u64 rapl_event_update(struct perf_event *event) |
| 189 | { |
| 190 | struct hw_perf_event *hwc = &event->hw; |
| 191 | u64 prev_raw_count, new_raw_count; |
| 192 | s64 delta, sdelta; |
| 193 | int shift = RAPL_CNTR_WIDTH; |
| 194 | |
| 195 | again: |
| 196 | prev_raw_count = local64_read(&hwc->prev_count); |
| 197 | rdmsrl(event->hw.event_base, new_raw_count); |
| 198 | |
| 199 | if (local64_cmpxchg(&hwc->prev_count, prev_raw_count, |
| 200 | new_raw_count) != prev_raw_count) { |
| 201 | cpu_relax(); |
| 202 | goto again; |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * Now we have the new raw value and have updated the prev |
| 207 | * timestamp already. We can now calculate the elapsed delta |
| 208 | * (event-)time and add that to the generic event. |
| 209 | * |
| 210 | * Careful, not all hw sign-extends above the physical width |
| 211 | * of the count. |
| 212 | */ |
| 213 | delta = (new_raw_count << shift) - (prev_raw_count << shift); |
| 214 | delta >>= shift; |
| 215 | |
Jacob Pan | 6455239 | 2015-03-26 14:28:45 -0700 | [diff] [blame] | 216 | sdelta = rapl_scale(delta, event->hw.config); |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 217 | |
| 218 | local64_add(sdelta, &event->count); |
| 219 | |
| 220 | return new_raw_count; |
| 221 | } |
| 222 | |
Stephane Eranian | 65661f9 | 2013-11-12 17:58:51 +0100 | [diff] [blame] | 223 | static void rapl_start_hrtimer(struct rapl_pmu *pmu) |
| 224 | { |
Thomas Gleixner | 514c230 | 2015-04-14 21:09:00 +0000 | [diff] [blame] | 225 | hrtimer_start(&pmu->hrtimer, pmu->timer_interval, |
| 226 | HRTIMER_MODE_REL_PINNED); |
Stephane Eranian | 65661f9 | 2013-11-12 17:58:51 +0100 | [diff] [blame] | 227 | } |
| 228 | |
Stephane Eranian | 65661f9 | 2013-11-12 17:58:51 +0100 | [diff] [blame] | 229 | static enum hrtimer_restart rapl_hrtimer_handle(struct hrtimer *hrtimer) |
| 230 | { |
Thomas Gleixner | 8a6d2f8 | 2016-02-22 22:19:25 +0000 | [diff] [blame] | 231 | struct rapl_pmu *pmu = container_of(hrtimer, struct rapl_pmu, hrtimer); |
Stephane Eranian | 65661f9 | 2013-11-12 17:58:51 +0100 | [diff] [blame] | 232 | struct perf_event *event; |
| 233 | unsigned long flags; |
| 234 | |
| 235 | if (!pmu->n_active) |
| 236 | return HRTIMER_NORESTART; |
| 237 | |
Thomas Gleixner | a208749 | 2016-02-22 22:19:25 +0000 | [diff] [blame] | 238 | raw_spin_lock_irqsave(&pmu->lock, flags); |
Stephane Eranian | 65661f9 | 2013-11-12 17:58:51 +0100 | [diff] [blame] | 239 | |
Thomas Gleixner | 7162b8f | 2016-02-22 22:19:24 +0000 | [diff] [blame] | 240 | list_for_each_entry(event, &pmu->active_list, active_entry) |
Stephane Eranian | 65661f9 | 2013-11-12 17:58:51 +0100 | [diff] [blame] | 241 | rapl_event_update(event); |
Stephane Eranian | 65661f9 | 2013-11-12 17:58:51 +0100 | [diff] [blame] | 242 | |
Thomas Gleixner | a208749 | 2016-02-22 22:19:25 +0000 | [diff] [blame] | 243 | raw_spin_unlock_irqrestore(&pmu->lock, flags); |
Stephane Eranian | 65661f9 | 2013-11-12 17:58:51 +0100 | [diff] [blame] | 244 | |
| 245 | hrtimer_forward_now(hrtimer, pmu->timer_interval); |
| 246 | |
| 247 | return HRTIMER_RESTART; |
| 248 | } |
| 249 | |
| 250 | static void rapl_hrtimer_init(struct rapl_pmu *pmu) |
| 251 | { |
| 252 | struct hrtimer *hr = &pmu->hrtimer; |
| 253 | |
| 254 | hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 255 | hr->function = rapl_hrtimer_handle; |
| 256 | } |
| 257 | |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 258 | static void __rapl_pmu_event_start(struct rapl_pmu *pmu, |
| 259 | struct perf_event *event) |
| 260 | { |
| 261 | if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED))) |
| 262 | return; |
| 263 | |
| 264 | event->hw.state = 0; |
| 265 | |
| 266 | list_add_tail(&event->active_entry, &pmu->active_list); |
| 267 | |
| 268 | local64_set(&event->hw.prev_count, rapl_read_counter(event)); |
| 269 | |
| 270 | pmu->n_active++; |
Stephane Eranian | 65661f9 | 2013-11-12 17:58:51 +0100 | [diff] [blame] | 271 | if (pmu->n_active == 1) |
| 272 | rapl_start_hrtimer(pmu); |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static void rapl_pmu_event_start(struct perf_event *event, int mode) |
| 276 | { |
Thomas Gleixner | 8a6d2f8 | 2016-02-22 22:19:25 +0000 | [diff] [blame] | 277 | struct rapl_pmu *pmu = event->pmu_private; |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 278 | unsigned long flags; |
| 279 | |
Thomas Gleixner | a208749 | 2016-02-22 22:19:25 +0000 | [diff] [blame] | 280 | raw_spin_lock_irqsave(&pmu->lock, flags); |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 281 | __rapl_pmu_event_start(pmu, event); |
Thomas Gleixner | a208749 | 2016-02-22 22:19:25 +0000 | [diff] [blame] | 282 | raw_spin_unlock_irqrestore(&pmu->lock, flags); |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | static void rapl_pmu_event_stop(struct perf_event *event, int mode) |
| 286 | { |
Thomas Gleixner | 8a6d2f8 | 2016-02-22 22:19:25 +0000 | [diff] [blame] | 287 | struct rapl_pmu *pmu = event->pmu_private; |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 288 | struct hw_perf_event *hwc = &event->hw; |
| 289 | unsigned long flags; |
| 290 | |
Thomas Gleixner | a208749 | 2016-02-22 22:19:25 +0000 | [diff] [blame] | 291 | raw_spin_lock_irqsave(&pmu->lock, flags); |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 292 | |
| 293 | /* mark event as deactivated and stopped */ |
| 294 | if (!(hwc->state & PERF_HES_STOPPED)) { |
| 295 | WARN_ON_ONCE(pmu->n_active <= 0); |
| 296 | pmu->n_active--; |
Stephane Eranian | 65661f9 | 2013-11-12 17:58:51 +0100 | [diff] [blame] | 297 | if (pmu->n_active == 0) |
Thomas Gleixner | 7162b8f | 2016-02-22 22:19:24 +0000 | [diff] [blame] | 298 | hrtimer_cancel(&pmu->hrtimer); |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 299 | |
| 300 | list_del(&event->active_entry); |
| 301 | |
| 302 | WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED); |
| 303 | hwc->state |= PERF_HES_STOPPED; |
| 304 | } |
| 305 | |
| 306 | /* check if update of sw counter is necessary */ |
| 307 | if ((mode & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) { |
| 308 | /* |
| 309 | * Drain the remaining delta count out of a event |
| 310 | * that we are disabling: |
| 311 | */ |
| 312 | rapl_event_update(event); |
| 313 | hwc->state |= PERF_HES_UPTODATE; |
| 314 | } |
| 315 | |
Thomas Gleixner | a208749 | 2016-02-22 22:19:25 +0000 | [diff] [blame] | 316 | raw_spin_unlock_irqrestore(&pmu->lock, flags); |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | static int rapl_pmu_event_add(struct perf_event *event, int mode) |
| 320 | { |
Thomas Gleixner | 8a6d2f8 | 2016-02-22 22:19:25 +0000 | [diff] [blame] | 321 | struct rapl_pmu *pmu = event->pmu_private; |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 322 | struct hw_perf_event *hwc = &event->hw; |
| 323 | unsigned long flags; |
| 324 | |
Thomas Gleixner | a208749 | 2016-02-22 22:19:25 +0000 | [diff] [blame] | 325 | raw_spin_lock_irqsave(&pmu->lock, flags); |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 326 | |
| 327 | hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED; |
| 328 | |
| 329 | if (mode & PERF_EF_START) |
| 330 | __rapl_pmu_event_start(pmu, event); |
| 331 | |
Thomas Gleixner | a208749 | 2016-02-22 22:19:25 +0000 | [diff] [blame] | 332 | raw_spin_unlock_irqrestore(&pmu->lock, flags); |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 333 | |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | static void rapl_pmu_event_del(struct perf_event *event, int flags) |
| 338 | { |
| 339 | rapl_pmu_event_stop(event, PERF_EF_UPDATE); |
| 340 | } |
| 341 | |
| 342 | static int rapl_pmu_event_init(struct perf_event *event) |
| 343 | { |
| 344 | u64 cfg = event->attr.config & RAPL_EVENT_MASK; |
| 345 | int bit, msr, ret = 0; |
Thomas Gleixner | 9de8d68 | 2016-02-22 22:19:26 +0000 | [diff] [blame] | 346 | struct rapl_pmu *pmu; |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 347 | |
| 348 | /* only look at RAPL events */ |
Thomas Gleixner | 9de8d68 | 2016-02-22 22:19:26 +0000 | [diff] [blame] | 349 | if (event->attr.type != rapl_pmus->pmu.type) |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 350 | return -ENOENT; |
| 351 | |
| 352 | /* check only supported bits are set */ |
| 353 | if (event->attr.config & ~RAPL_EVENT_MASK) |
| 354 | return -EINVAL; |
| 355 | |
Thomas Gleixner | 8a6d2f8 | 2016-02-22 22:19:25 +0000 | [diff] [blame] | 356 | if (event->cpu < 0) |
| 357 | return -EINVAL; |
| 358 | |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 359 | /* |
| 360 | * check event is known (determines counter) |
| 361 | */ |
| 362 | switch (cfg) { |
| 363 | case INTEL_RAPL_PP0: |
| 364 | bit = RAPL_IDX_PP0_NRG_STAT; |
| 365 | msr = MSR_PP0_ENERGY_STATUS; |
| 366 | break; |
| 367 | case INTEL_RAPL_PKG: |
| 368 | bit = RAPL_IDX_PKG_NRG_STAT; |
| 369 | msr = MSR_PKG_ENERGY_STATUS; |
| 370 | break; |
| 371 | case INTEL_RAPL_RAM: |
| 372 | bit = RAPL_IDX_RAM_NRG_STAT; |
| 373 | msr = MSR_DRAM_ENERGY_STATUS; |
| 374 | break; |
Stephane Eranian | f228c5b | 2014-01-08 11:15:53 +0100 | [diff] [blame] | 375 | case INTEL_RAPL_PP1: |
| 376 | bit = RAPL_IDX_PP1_NRG_STAT; |
| 377 | msr = MSR_PP1_ENERGY_STATUS; |
| 378 | break; |
Srinivas Pandruvada | dcee75b | 2016-04-17 15:03:00 -0700 | [diff] [blame] | 379 | case INTEL_RAPL_PSYS: |
| 380 | bit = RAPL_IDX_PSYS_NRG_STAT; |
| 381 | msr = MSR_PLATFORM_ENERGY_STATUS; |
| 382 | break; |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 383 | default: |
| 384 | return -EINVAL; |
| 385 | } |
| 386 | /* check event supported */ |
| 387 | if (!(rapl_cntr_mask & (1 << bit))) |
| 388 | return -EINVAL; |
| 389 | |
| 390 | /* unsupported modes and filters */ |
| 391 | if (event->attr.exclude_user || |
| 392 | event->attr.exclude_kernel || |
| 393 | event->attr.exclude_hv || |
| 394 | event->attr.exclude_idle || |
| 395 | event->attr.exclude_host || |
| 396 | event->attr.exclude_guest || |
| 397 | event->attr.sample_period) /* no sampling */ |
| 398 | return -EINVAL; |
| 399 | |
| 400 | /* must be done before validate_group */ |
Thomas Gleixner | 9de8d68 | 2016-02-22 22:19:26 +0000 | [diff] [blame] | 401 | pmu = cpu_to_rapl_pmu(event->cpu); |
Thomas Gleixner | 8a6d2f8 | 2016-02-22 22:19:25 +0000 | [diff] [blame] | 402 | event->cpu = pmu->cpu; |
| 403 | event->pmu_private = pmu; |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 404 | event->hw.event_base = msr; |
| 405 | event->hw.config = cfg; |
| 406 | event->hw.idx = bit; |
| 407 | |
| 408 | return ret; |
| 409 | } |
| 410 | |
| 411 | static void rapl_pmu_event_read(struct perf_event *event) |
| 412 | { |
| 413 | rapl_event_update(event); |
| 414 | } |
| 415 | |
| 416 | static ssize_t rapl_get_attr_cpumask(struct device *dev, |
| 417 | struct device_attribute *attr, char *buf) |
| 418 | { |
Sudeep Holla | 5aaba36 | 2014-09-30 14:48:22 +0100 | [diff] [blame] | 419 | return cpumap_print_to_pagebuf(true, buf, &rapl_cpu_mask); |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | static DEVICE_ATTR(cpumask, S_IRUGO, rapl_get_attr_cpumask, NULL); |
| 423 | |
| 424 | static struct attribute *rapl_pmu_attrs[] = { |
| 425 | &dev_attr_cpumask.attr, |
| 426 | NULL, |
| 427 | }; |
| 428 | |
| 429 | static struct attribute_group rapl_pmu_attr_group = { |
| 430 | .attrs = rapl_pmu_attrs, |
| 431 | }; |
| 432 | |
Stephane Eranian | 433678b | 2015-01-13 23:59:53 +0100 | [diff] [blame] | 433 | RAPL_EVENT_ATTR_STR(energy-cores, rapl_cores, "event=0x01"); |
| 434 | RAPL_EVENT_ATTR_STR(energy-pkg , rapl_pkg, "event=0x02"); |
| 435 | RAPL_EVENT_ATTR_STR(energy-ram , rapl_ram, "event=0x03"); |
| 436 | RAPL_EVENT_ATTR_STR(energy-gpu , rapl_gpu, "event=0x04"); |
Srinivas Pandruvada | dcee75b | 2016-04-17 15:03:00 -0700 | [diff] [blame] | 437 | RAPL_EVENT_ATTR_STR(energy-psys, rapl_psys, "event=0x05"); |
Stephane Eranian | 433678b | 2015-01-13 23:59:53 +0100 | [diff] [blame] | 438 | |
| 439 | RAPL_EVENT_ATTR_STR(energy-cores.unit, rapl_cores_unit, "Joules"); |
| 440 | RAPL_EVENT_ATTR_STR(energy-pkg.unit , rapl_pkg_unit, "Joules"); |
| 441 | RAPL_EVENT_ATTR_STR(energy-ram.unit , rapl_ram_unit, "Joules"); |
| 442 | RAPL_EVENT_ATTR_STR(energy-gpu.unit , rapl_gpu_unit, "Joules"); |
Srinivas Pandruvada | dcee75b | 2016-04-17 15:03:00 -0700 | [diff] [blame] | 443 | RAPL_EVENT_ATTR_STR(energy-psys.unit, rapl_psys_unit, "Joules"); |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 444 | |
| 445 | /* |
| 446 | * we compute in 0.23 nJ increments regardless of MSR |
| 447 | */ |
Stephane Eranian | 433678b | 2015-01-13 23:59:53 +0100 | [diff] [blame] | 448 | RAPL_EVENT_ATTR_STR(energy-cores.scale, rapl_cores_scale, "2.3283064365386962890625e-10"); |
| 449 | RAPL_EVENT_ATTR_STR(energy-pkg.scale, rapl_pkg_scale, "2.3283064365386962890625e-10"); |
| 450 | RAPL_EVENT_ATTR_STR(energy-ram.scale, rapl_ram_scale, "2.3283064365386962890625e-10"); |
| 451 | RAPL_EVENT_ATTR_STR(energy-gpu.scale, rapl_gpu_scale, "2.3283064365386962890625e-10"); |
Srinivas Pandruvada | dcee75b | 2016-04-17 15:03:00 -0700 | [diff] [blame] | 452 | RAPL_EVENT_ATTR_STR(energy-psys.scale, rapl_psys_scale, "2.3283064365386962890625e-10"); |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 453 | |
| 454 | static struct attribute *rapl_events_srv_attr[] = { |
| 455 | EVENT_PTR(rapl_cores), |
| 456 | EVENT_PTR(rapl_pkg), |
| 457 | EVENT_PTR(rapl_ram), |
| 458 | |
| 459 | EVENT_PTR(rapl_cores_unit), |
| 460 | EVENT_PTR(rapl_pkg_unit), |
| 461 | EVENT_PTR(rapl_ram_unit), |
| 462 | |
| 463 | EVENT_PTR(rapl_cores_scale), |
| 464 | EVENT_PTR(rapl_pkg_scale), |
| 465 | EVENT_PTR(rapl_ram_scale), |
| 466 | NULL, |
| 467 | }; |
| 468 | |
| 469 | static struct attribute *rapl_events_cln_attr[] = { |
| 470 | EVENT_PTR(rapl_cores), |
| 471 | EVENT_PTR(rapl_pkg), |
Stephane Eranian | f228c5b | 2014-01-08 11:15:53 +0100 | [diff] [blame] | 472 | EVENT_PTR(rapl_gpu), |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 473 | |
| 474 | EVENT_PTR(rapl_cores_unit), |
| 475 | EVENT_PTR(rapl_pkg_unit), |
Stephane Eranian | f228c5b | 2014-01-08 11:15:53 +0100 | [diff] [blame] | 476 | EVENT_PTR(rapl_gpu_unit), |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 477 | |
| 478 | EVENT_PTR(rapl_cores_scale), |
| 479 | EVENT_PTR(rapl_pkg_scale), |
Stephane Eranian | f228c5b | 2014-01-08 11:15:53 +0100 | [diff] [blame] | 480 | EVENT_PTR(rapl_gpu_scale), |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 481 | NULL, |
| 482 | }; |
| 483 | |
Vince Weaver | e69af46 | 2014-04-02 00:49:55 -0400 | [diff] [blame] | 484 | static struct attribute *rapl_events_hsw_attr[] = { |
| 485 | EVENT_PTR(rapl_cores), |
| 486 | EVENT_PTR(rapl_pkg), |
| 487 | EVENT_PTR(rapl_gpu), |
| 488 | EVENT_PTR(rapl_ram), |
| 489 | |
| 490 | EVENT_PTR(rapl_cores_unit), |
| 491 | EVENT_PTR(rapl_pkg_unit), |
| 492 | EVENT_PTR(rapl_gpu_unit), |
| 493 | EVENT_PTR(rapl_ram_unit), |
| 494 | |
| 495 | EVENT_PTR(rapl_cores_scale), |
| 496 | EVENT_PTR(rapl_pkg_scale), |
| 497 | EVENT_PTR(rapl_gpu_scale), |
| 498 | EVENT_PTR(rapl_ram_scale), |
| 499 | NULL, |
| 500 | }; |
| 501 | |
Srinivas Pandruvada | dcee75b | 2016-04-17 15:03:00 -0700 | [diff] [blame] | 502 | static struct attribute *rapl_events_skl_attr[] = { |
| 503 | EVENT_PTR(rapl_cores), |
| 504 | EVENT_PTR(rapl_pkg), |
| 505 | EVENT_PTR(rapl_gpu), |
| 506 | EVENT_PTR(rapl_ram), |
| 507 | EVENT_PTR(rapl_psys), |
| 508 | |
| 509 | EVENT_PTR(rapl_cores_unit), |
| 510 | EVENT_PTR(rapl_pkg_unit), |
| 511 | EVENT_PTR(rapl_gpu_unit), |
| 512 | EVENT_PTR(rapl_ram_unit), |
| 513 | EVENT_PTR(rapl_psys_unit), |
| 514 | |
| 515 | EVENT_PTR(rapl_cores_scale), |
| 516 | EVENT_PTR(rapl_pkg_scale), |
| 517 | EVENT_PTR(rapl_gpu_scale), |
| 518 | EVENT_PTR(rapl_ram_scale), |
| 519 | EVENT_PTR(rapl_psys_scale), |
| 520 | NULL, |
| 521 | }; |
| 522 | |
Dasaratharaman Chandramouli | 3a2a779 | 2015-05-26 11:47:39 -0700 | [diff] [blame] | 523 | static struct attribute *rapl_events_knl_attr[] = { |
| 524 | EVENT_PTR(rapl_pkg), |
| 525 | EVENT_PTR(rapl_ram), |
| 526 | |
| 527 | EVENT_PTR(rapl_pkg_unit), |
| 528 | EVENT_PTR(rapl_ram_unit), |
| 529 | |
| 530 | EVENT_PTR(rapl_pkg_scale), |
| 531 | EVENT_PTR(rapl_ram_scale), |
| 532 | NULL, |
| 533 | }; |
| 534 | |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 535 | static struct attribute_group rapl_pmu_events_group = { |
| 536 | .name = "events", |
| 537 | .attrs = NULL, /* patched at runtime */ |
| 538 | }; |
| 539 | |
| 540 | DEFINE_RAPL_FORMAT_ATTR(event, event, "config:0-7"); |
| 541 | static struct attribute *rapl_formats_attr[] = { |
| 542 | &format_attr_event.attr, |
| 543 | NULL, |
| 544 | }; |
| 545 | |
| 546 | static struct attribute_group rapl_pmu_format_group = { |
| 547 | .name = "format", |
| 548 | .attrs = rapl_formats_attr, |
| 549 | }; |
| 550 | |
| 551 | const struct attribute_group *rapl_attr_groups[] = { |
| 552 | &rapl_pmu_attr_group, |
| 553 | &rapl_pmu_format_group, |
| 554 | &rapl_pmu_events_group, |
| 555 | NULL, |
| 556 | }; |
| 557 | |
Richard Cochran | 8b5b773 | 2016-07-13 17:16:15 +0000 | [diff] [blame^] | 558 | static int rapl_cpu_offline(unsigned int cpu) |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 559 | { |
Thomas Gleixner | 9de8d68 | 2016-02-22 22:19:26 +0000 | [diff] [blame] | 560 | struct rapl_pmu *pmu = cpu_to_rapl_pmu(cpu); |
| 561 | int target; |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 562 | |
Thomas Gleixner | 9de8d68 | 2016-02-22 22:19:26 +0000 | [diff] [blame] | 563 | /* Check if exiting cpu is used for collecting rapl events */ |
| 564 | if (!cpumask_test_and_clear_cpu(cpu, &rapl_cpu_mask)) |
Richard Cochran | 8b5b773 | 2016-07-13 17:16:15 +0000 | [diff] [blame^] | 565 | return 0; |
Thomas Gleixner | 9de8d68 | 2016-02-22 22:19:26 +0000 | [diff] [blame] | 566 | |
| 567 | pmu->cpu = -1; |
| 568 | /* Find a new cpu to collect rapl events */ |
| 569 | target = cpumask_any_but(topology_core_cpumask(cpu), cpu); |
| 570 | |
| 571 | /* Migrate rapl events to the new target */ |
| 572 | if (target < nr_cpu_ids) { |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 573 | cpumask_set_cpu(target, &rapl_cpu_mask); |
Thomas Gleixner | 9de8d68 | 2016-02-22 22:19:26 +0000 | [diff] [blame] | 574 | pmu->cpu = target; |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 575 | perf_pmu_migrate_context(pmu->pmu, cpu, target); |
Thomas Gleixner | 9de8d68 | 2016-02-22 22:19:26 +0000 | [diff] [blame] | 576 | } |
Richard Cochran | 8b5b773 | 2016-07-13 17:16:15 +0000 | [diff] [blame^] | 577 | return 0; |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 578 | } |
| 579 | |
Richard Cochran | 8b5b773 | 2016-07-13 17:16:15 +0000 | [diff] [blame^] | 580 | static int rapl_cpu_online(unsigned int cpu) |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 581 | { |
Thomas Gleixner | 9de8d68 | 2016-02-22 22:19:26 +0000 | [diff] [blame] | 582 | struct rapl_pmu *pmu = cpu_to_rapl_pmu(cpu); |
| 583 | int target; |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 584 | |
Thomas Gleixner | 9de8d68 | 2016-02-22 22:19:26 +0000 | [diff] [blame] | 585 | /* |
| 586 | * Check if there is an online cpu in the package which collects rapl |
| 587 | * events already. |
| 588 | */ |
| 589 | target = cpumask_any_and(&rapl_cpu_mask, topology_core_cpumask(cpu)); |
| 590 | if (target < nr_cpu_ids) |
Richard Cochran | 8b5b773 | 2016-07-13 17:16:15 +0000 | [diff] [blame^] | 591 | return 0; |
Thomas Gleixner | 9de8d68 | 2016-02-22 22:19:26 +0000 | [diff] [blame] | 592 | |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 593 | cpumask_set_cpu(cpu, &rapl_cpu_mask); |
Thomas Gleixner | 9de8d68 | 2016-02-22 22:19:26 +0000 | [diff] [blame] | 594 | pmu->cpu = cpu; |
Richard Cochran | 8b5b773 | 2016-07-13 17:16:15 +0000 | [diff] [blame^] | 595 | return 0; |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 596 | } |
| 597 | |
Richard Cochran | 8b5b773 | 2016-07-13 17:16:15 +0000 | [diff] [blame^] | 598 | static int rapl_cpu_prepare(unsigned int cpu) |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 599 | { |
Thomas Gleixner | 9de8d68 | 2016-02-22 22:19:26 +0000 | [diff] [blame] | 600 | struct rapl_pmu *pmu = cpu_to_rapl_pmu(cpu); |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 601 | |
| 602 | if (pmu) |
| 603 | return 0; |
| 604 | |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 605 | pmu = kzalloc_node(sizeof(*pmu), GFP_KERNEL, cpu_to_node(cpu)); |
| 606 | if (!pmu) |
Thomas Gleixner | 9de8d68 | 2016-02-22 22:19:26 +0000 | [diff] [blame] | 607 | return -ENOMEM; |
| 608 | |
Thomas Gleixner | a208749 | 2016-02-22 22:19:25 +0000 | [diff] [blame] | 609 | raw_spin_lock_init(&pmu->lock); |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 610 | INIT_LIST_HEAD(&pmu->active_list); |
Thomas Gleixner | 9de8d68 | 2016-02-22 22:19:26 +0000 | [diff] [blame] | 611 | pmu->pmu = &rapl_pmus->pmu; |
Thomas Gleixner | 75c7003 | 2016-02-22 22:19:22 +0000 | [diff] [blame] | 612 | pmu->timer_interval = ms_to_ktime(rapl_timer_ms); |
Thomas Gleixner | 9de8d68 | 2016-02-22 22:19:26 +0000 | [diff] [blame] | 613 | pmu->cpu = -1; |
Stephane Eranian | 65661f9 | 2013-11-12 17:58:51 +0100 | [diff] [blame] | 614 | rapl_hrtimer_init(pmu); |
Thomas Gleixner | 9de8d68 | 2016-02-22 22:19:26 +0000 | [diff] [blame] | 615 | rapl_pmus->pmus[topology_logical_package_id(cpu)] = pmu; |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 616 | return 0; |
| 617 | } |
| 618 | |
Borislav Petkov | 7a86980 | 2016-03-08 17:40:41 +0100 | [diff] [blame] | 619 | static int rapl_check_hw_unit(bool apply_quirk) |
Jacob Pan | 6455239 | 2015-03-26 14:28:45 -0700 | [diff] [blame] | 620 | { |
| 621 | u64 msr_rapl_power_unit_bits; |
| 622 | int i; |
| 623 | |
| 624 | /* protect rdmsrl() to handle virtualization */ |
| 625 | if (rdmsrl_safe(MSR_RAPL_POWER_UNIT, &msr_rapl_power_unit_bits)) |
| 626 | return -1; |
| 627 | for (i = 0; i < NR_RAPL_DOMAINS; i++) |
| 628 | rapl_hw_unit[i] = (msr_rapl_power_unit_bits >> 8) & 0x1FULL; |
| 629 | |
Borislav Petkov | 7a86980 | 2016-03-08 17:40:41 +0100 | [diff] [blame] | 630 | /* |
| 631 | * DRAM domain on HSW server and KNL has fixed energy unit which can be |
| 632 | * different than the unit from power unit MSR. See |
| 633 | * "Intel Xeon Processor E5-1600 and E5-2600 v3 Product Families, V2 |
| 634 | * of 2. Datasheet, September 2014, Reference Number: 330784-001 " |
| 635 | */ |
| 636 | if (apply_quirk) |
| 637 | rapl_hw_unit[RAPL_IDX_RAM_NRG_STAT] = 16; |
Thomas Gleixner | 75c7003 | 2016-02-22 22:19:22 +0000 | [diff] [blame] | 638 | |
| 639 | /* |
| 640 | * Calculate the timer rate: |
| 641 | * Use reference of 200W for scaling the timeout to avoid counter |
| 642 | * overflows. 200W = 200 Joules/sec |
| 643 | * Divide interval by 2 to avoid lockstep (2 * 100) |
| 644 | * if hw unit is 32, then we use 2 ms 1/200/2 |
| 645 | */ |
| 646 | rapl_timer_ms = 2; |
| 647 | if (rapl_hw_unit[0] < 32) { |
| 648 | rapl_timer_ms = (1000 / (2 * 100)); |
| 649 | rapl_timer_ms *= (1ULL << (32 - rapl_hw_unit[0] - 1)); |
| 650 | } |
Jacob Pan | 6455239 | 2015-03-26 14:28:45 -0700 | [diff] [blame] | 651 | return 0; |
| 652 | } |
| 653 | |
Thomas Gleixner | 512089d | 2016-02-22 22:19:23 +0000 | [diff] [blame] | 654 | static void __init rapl_advertise(void) |
| 655 | { |
| 656 | int i; |
| 657 | |
| 658 | pr_info("API unit is 2^-32 Joules, %d fixed counters, %llu ms ovfl timer\n", |
| 659 | hweight32(rapl_cntr_mask), rapl_timer_ms); |
| 660 | |
| 661 | for (i = 0; i < NR_RAPL_DOMAINS; i++) { |
| 662 | if (rapl_cntr_mask & (1 << i)) { |
| 663 | pr_info("hw unit of domain %s 2^-%d Joules\n", |
| 664 | rapl_domain_names[i], rapl_hw_unit[i]); |
| 665 | } |
| 666 | } |
| 667 | } |
| 668 | |
Kan Liang | 4b6e257 | 2016-03-19 00:20:50 -0700 | [diff] [blame] | 669 | static void cleanup_rapl_pmus(void) |
Thomas Gleixner | 55f2890 | 2016-02-22 22:19:21 +0000 | [diff] [blame] | 670 | { |
Thomas Gleixner | 9de8d68 | 2016-02-22 22:19:26 +0000 | [diff] [blame] | 671 | int i; |
Thomas Gleixner | 55f2890 | 2016-02-22 22:19:21 +0000 | [diff] [blame] | 672 | |
Thomas Gleixner | 9de8d68 | 2016-02-22 22:19:26 +0000 | [diff] [blame] | 673 | for (i = 0; i < rapl_pmus->maxpkg; i++) |
Vincent Stehlé | 275ae41 | 2016-05-24 16:53:49 +0200 | [diff] [blame] | 674 | kfree(rapl_pmus->pmus[i]); |
Thomas Gleixner | 9de8d68 | 2016-02-22 22:19:26 +0000 | [diff] [blame] | 675 | kfree(rapl_pmus); |
| 676 | } |
| 677 | |
| 678 | static int __init init_rapl_pmus(void) |
| 679 | { |
| 680 | int maxpkg = topology_max_packages(); |
| 681 | size_t size; |
| 682 | |
| 683 | size = sizeof(*rapl_pmus) + maxpkg * sizeof(struct rapl_pmu *); |
| 684 | rapl_pmus = kzalloc(size, GFP_KERNEL); |
| 685 | if (!rapl_pmus) |
| 686 | return -ENOMEM; |
| 687 | |
| 688 | rapl_pmus->maxpkg = maxpkg; |
| 689 | rapl_pmus->pmu.attr_groups = rapl_attr_groups; |
| 690 | rapl_pmus->pmu.task_ctx_nr = perf_invalid_context; |
| 691 | rapl_pmus->pmu.event_init = rapl_pmu_event_init; |
| 692 | rapl_pmus->pmu.add = rapl_pmu_event_add; |
| 693 | rapl_pmus->pmu.del = rapl_pmu_event_del; |
| 694 | rapl_pmus->pmu.start = rapl_pmu_event_start; |
| 695 | rapl_pmus->pmu.stop = rapl_pmu_event_stop; |
| 696 | rapl_pmus->pmu.read = rapl_pmu_event_read; |
| 697 | return 0; |
Thomas Gleixner | 55f2890 | 2016-02-22 22:19:21 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Kan Liang | 4b6e257 | 2016-03-19 00:20:50 -0700 | [diff] [blame] | 700 | #define X86_RAPL_MODEL_MATCH(model, init) \ |
| 701 | { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, (unsigned long)&init } |
| 702 | |
| 703 | struct intel_rapl_init_fun { |
| 704 | bool apply_quirk; |
| 705 | int cntr_mask; |
| 706 | struct attribute **attrs; |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 707 | }; |
| 708 | |
Kan Liang | 4b6e257 | 2016-03-19 00:20:50 -0700 | [diff] [blame] | 709 | static const struct intel_rapl_init_fun snb_rapl_init __initconst = { |
| 710 | .apply_quirk = false, |
| 711 | .cntr_mask = RAPL_IDX_CLN, |
| 712 | .attrs = rapl_events_cln_attr, |
| 713 | }; |
| 714 | |
| 715 | static const struct intel_rapl_init_fun hsx_rapl_init __initconst = { |
| 716 | .apply_quirk = true, |
| 717 | .cntr_mask = RAPL_IDX_SRV, |
| 718 | .attrs = rapl_events_srv_attr, |
| 719 | }; |
| 720 | |
| 721 | static const struct intel_rapl_init_fun hsw_rapl_init __initconst = { |
| 722 | .apply_quirk = false, |
| 723 | .cntr_mask = RAPL_IDX_HSW, |
| 724 | .attrs = rapl_events_hsw_attr, |
| 725 | }; |
| 726 | |
| 727 | static const struct intel_rapl_init_fun snbep_rapl_init __initconst = { |
| 728 | .apply_quirk = false, |
| 729 | .cntr_mask = RAPL_IDX_SRV, |
| 730 | .attrs = rapl_events_srv_attr, |
| 731 | }; |
| 732 | |
| 733 | static const struct intel_rapl_init_fun knl_rapl_init __initconst = { |
| 734 | .apply_quirk = true, |
| 735 | .cntr_mask = RAPL_IDX_KNL, |
| 736 | .attrs = rapl_events_knl_attr, |
| 737 | }; |
| 738 | |
Srinivas Pandruvada | dcee75b | 2016-04-17 15:03:00 -0700 | [diff] [blame] | 739 | static const struct intel_rapl_init_fun skl_rapl_init __initconst = { |
| 740 | .apply_quirk = false, |
| 741 | .cntr_mask = RAPL_IDX_SKL_CLN, |
| 742 | .attrs = rapl_events_skl_attr, |
| 743 | }; |
| 744 | |
Kan Liang | 4b6e257 | 2016-03-19 00:20:50 -0700 | [diff] [blame] | 745 | static const struct x86_cpu_id rapl_cpu_match[] __initconst = { |
| 746 | X86_RAPL_MODEL_MATCH(42, snb_rapl_init), /* Sandy Bridge */ |
Peter Zijlstra | c416e5a | 2016-04-21 15:14:17 +0200 | [diff] [blame] | 747 | X86_RAPL_MODEL_MATCH(45, snbep_rapl_init), /* Sandy Bridge-EP */ |
| 748 | |
Kan Liang | 4b6e257 | 2016-03-19 00:20:50 -0700 | [diff] [blame] | 749 | X86_RAPL_MODEL_MATCH(58, snb_rapl_init), /* Ivy Bridge */ |
Peter Zijlstra | c416e5a | 2016-04-21 15:14:17 +0200 | [diff] [blame] | 750 | X86_RAPL_MODEL_MATCH(62, snbep_rapl_init), /* IvyTown */ |
| 751 | |
Kan Liang | 4b6e257 | 2016-03-19 00:20:50 -0700 | [diff] [blame] | 752 | X86_RAPL_MODEL_MATCH(60, hsw_rapl_init), /* Haswell */ |
Peter Zijlstra | c416e5a | 2016-04-21 15:14:17 +0200 | [diff] [blame] | 753 | X86_RAPL_MODEL_MATCH(63, hsx_rapl_init), /* Haswell-Server */ |
Kan Liang | 4b6e257 | 2016-03-19 00:20:50 -0700 | [diff] [blame] | 754 | X86_RAPL_MODEL_MATCH(69, hsw_rapl_init), /* Haswell-Celeron */ |
Ingo Molnar | 65cbbd0 | 2016-04-23 14:12:10 +0200 | [diff] [blame] | 755 | X86_RAPL_MODEL_MATCH(70, hsw_rapl_init), /* Haswell GT3e */ |
Peter Zijlstra | c416e5a | 2016-04-21 15:14:17 +0200 | [diff] [blame] | 756 | |
Kan Liang | 4b6e257 | 2016-03-19 00:20:50 -0700 | [diff] [blame] | 757 | X86_RAPL_MODEL_MATCH(61, hsw_rapl_init), /* Broadwell */ |
| 758 | X86_RAPL_MODEL_MATCH(71, hsw_rapl_init), /* Broadwell-H */ |
Peter Zijlstra | c416e5a | 2016-04-21 15:14:17 +0200 | [diff] [blame] | 759 | X86_RAPL_MODEL_MATCH(79, hsx_rapl_init), /* Broadwell-Server */ |
Peter Zijlstra | 31b8431 | 2016-04-21 15:15:47 +0200 | [diff] [blame] | 760 | X86_RAPL_MODEL_MATCH(86, hsx_rapl_init), /* Broadwell Xeon D */ |
Peter Zijlstra | c416e5a | 2016-04-21 15:14:17 +0200 | [diff] [blame] | 761 | |
Kan Liang | 4b6e257 | 2016-03-19 00:20:50 -0700 | [diff] [blame] | 762 | X86_RAPL_MODEL_MATCH(87, knl_rapl_init), /* Knights Landing */ |
Peter Zijlstra | c416e5a | 2016-04-21 15:14:17 +0200 | [diff] [blame] | 763 | |
Srinivas Pandruvada | dcee75b | 2016-04-17 15:03:00 -0700 | [diff] [blame] | 764 | X86_RAPL_MODEL_MATCH(78, skl_rapl_init), /* Skylake */ |
| 765 | X86_RAPL_MODEL_MATCH(94, skl_rapl_init), /* Skylake H/S */ |
Kan Liang | 4b6e257 | 2016-03-19 00:20:50 -0700 | [diff] [blame] | 766 | {}, |
| 767 | }; |
| 768 | |
| 769 | MODULE_DEVICE_TABLE(x86cpu, rapl_cpu_match); |
| 770 | |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 771 | static int __init rapl_pmu_init(void) |
| 772 | { |
Kan Liang | 4b6e257 | 2016-03-19 00:20:50 -0700 | [diff] [blame] | 773 | const struct x86_cpu_id *id; |
| 774 | struct intel_rapl_init_fun *rapl_init; |
| 775 | bool apply_quirk; |
Thomas Gleixner | 7162b8f | 2016-02-22 22:19:24 +0000 | [diff] [blame] | 776 | int ret; |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 777 | |
Kan Liang | 4b6e257 | 2016-03-19 00:20:50 -0700 | [diff] [blame] | 778 | id = x86_match_cpu(rapl_cpu_match); |
| 779 | if (!id) |
Thomas Gleixner | 55f2890 | 2016-02-22 22:19:21 +0000 | [diff] [blame] | 780 | return -ENODEV; |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 781 | |
Kan Liang | 4b6e257 | 2016-03-19 00:20:50 -0700 | [diff] [blame] | 782 | rapl_init = (struct intel_rapl_init_fun *)id->driver_data; |
| 783 | apply_quirk = rapl_init->apply_quirk; |
| 784 | rapl_cntr_mask = rapl_init->cntr_mask; |
| 785 | rapl_pmu_events_group.attrs = rapl_init->attrs; |
Thomas Gleixner | 55f2890 | 2016-02-22 22:19:21 +0000 | [diff] [blame] | 786 | |
Borislav Petkov | 7a86980 | 2016-03-08 17:40:41 +0100 | [diff] [blame] | 787 | ret = rapl_check_hw_unit(apply_quirk); |
Jacob Pan | 6455239 | 2015-03-26 14:28:45 -0700 | [diff] [blame] | 788 | if (ret) |
| 789 | return ret; |
Srivatsa S. Bhat | fd537e5 | 2014-03-11 02:08:09 +0530 | [diff] [blame] | 790 | |
Thomas Gleixner | 9de8d68 | 2016-02-22 22:19:26 +0000 | [diff] [blame] | 791 | ret = init_rapl_pmus(); |
| 792 | if (ret) |
| 793 | return ret; |
| 794 | |
Richard Cochran | 8b5b773 | 2016-07-13 17:16:15 +0000 | [diff] [blame^] | 795 | /* |
| 796 | * Install callbacks. Core will call them for each online cpu. |
| 797 | */ |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 798 | |
Richard Cochran | 8b5b773 | 2016-07-13 17:16:15 +0000 | [diff] [blame^] | 799 | ret = cpuhp_setup_state(CPUHP_PERF_X86_RAPL_PREP, "PERF_X86_RAPL_PREP", |
| 800 | rapl_cpu_prepare, NULL); |
Thomas Gleixner | 7162b8f | 2016-02-22 22:19:24 +0000 | [diff] [blame] | 801 | if (ret) |
| 802 | goto out; |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 803 | |
Richard Cochran | 8b5b773 | 2016-07-13 17:16:15 +0000 | [diff] [blame^] | 804 | ret = cpuhp_setup_state(CPUHP_AP_PERF_X86_RAPL_ONLINE, |
| 805 | "AP_PERF_X86_RAPL_ONLINE", |
| 806 | rapl_cpu_online, rapl_cpu_offline); |
| 807 | if (ret) |
| 808 | goto out1; |
| 809 | |
Thomas Gleixner | 9de8d68 | 2016-02-22 22:19:26 +0000 | [diff] [blame] | 810 | ret = perf_pmu_register(&rapl_pmus->pmu, "power", -1); |
Thomas Gleixner | 512089d | 2016-02-22 22:19:23 +0000 | [diff] [blame] | 811 | if (ret) |
Richard Cochran | 8b5b773 | 2016-07-13 17:16:15 +0000 | [diff] [blame^] | 812 | goto out2; |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 813 | |
Thomas Gleixner | 512089d | 2016-02-22 22:19:23 +0000 | [diff] [blame] | 814 | rapl_advertise(); |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 815 | return 0; |
Thomas Gleixner | 55f2890 | 2016-02-22 22:19:21 +0000 | [diff] [blame] | 816 | |
Richard Cochran | 8b5b773 | 2016-07-13 17:16:15 +0000 | [diff] [blame^] | 817 | out2: |
| 818 | cpuhp_remove_state(CPUHP_AP_PERF_X86_RAPL_ONLINE); |
| 819 | out1: |
| 820 | cpuhp_remove_state(CPUHP_PERF_X86_RAPL_PREP); |
Thomas Gleixner | 55f2890 | 2016-02-22 22:19:21 +0000 | [diff] [blame] | 821 | out: |
Thomas Gleixner | 512089d | 2016-02-22 22:19:23 +0000 | [diff] [blame] | 822 | pr_warn("Initialization failed (%d), disabled\n", ret); |
Thomas Gleixner | 55f2890 | 2016-02-22 22:19:21 +0000 | [diff] [blame] | 823 | cleanup_rapl_pmus(); |
Thomas Gleixner | 55f2890 | 2016-02-22 22:19:21 +0000 | [diff] [blame] | 824 | return ret; |
Stephane Eranian | 4788e5b | 2013-11-12 17:58:50 +0100 | [diff] [blame] | 825 | } |
Kan Liang | 4b6e257 | 2016-03-19 00:20:50 -0700 | [diff] [blame] | 826 | module_init(rapl_pmu_init); |
| 827 | |
| 828 | static void __exit intel_rapl_exit(void) |
| 829 | { |
Richard Cochran | 8b5b773 | 2016-07-13 17:16:15 +0000 | [diff] [blame^] | 830 | cpuhp_remove_state_nocalls(CPUHP_AP_PERF_X86_RAPL_ONLINE); |
| 831 | cpuhp_remove_state_nocalls(CPUHP_PERF_X86_RAPL_PREP); |
Kan Liang | 4b6e257 | 2016-03-19 00:20:50 -0700 | [diff] [blame] | 832 | perf_pmu_unregister(&rapl_pmus->pmu); |
| 833 | cleanup_rapl_pmus(); |
Kan Liang | 4b6e257 | 2016-03-19 00:20:50 -0700 | [diff] [blame] | 834 | } |
| 835 | module_exit(intel_rapl_exit); |