Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Performance events - AMD IBS |
| 3 | * |
| 4 | * Copyright (C) 2011 Advanced Micro Devices, Inc., Robert Richter |
| 5 | * |
| 6 | * For licencing details see kernel-base/COPYING |
| 7 | */ |
| 8 | |
| 9 | #include <linux/perf_event.h> |
Paul Gortmaker | eb008eb | 2016-07-13 20:19:01 -0400 | [diff] [blame] | 10 | #include <linux/init.h> |
| 11 | #include <linux/export.h> |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 12 | #include <linux/pci.h> |
Robert Richter | d47e823 | 2012-04-02 20:19:11 +0200 | [diff] [blame] | 13 | #include <linux/ptrace.h> |
Robert Richter | bee09ed | 2014-01-15 15:57:29 +0100 | [diff] [blame] | 14 | #include <linux/syscore_ops.h> |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 15 | |
| 16 | #include <asm/apic.h> |
| 17 | |
Borislav Petkov | 27f6d22 | 2016-02-10 10:55:23 +0100 | [diff] [blame] | 18 | #include "../perf_event.h" |
Peter Zijlstra | d07bdfd | 2012-07-10 09:42:15 +0200 | [diff] [blame] | 19 | |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 20 | static u32 ibs_caps; |
| 21 | |
| 22 | #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_AMD) |
| 23 | |
Robert Richter | b7074f1 | 2011-12-15 17:56:37 +0100 | [diff] [blame] | 24 | #include <linux/kprobes.h> |
| 25 | #include <linux/hardirq.h> |
| 26 | |
| 27 | #include <asm/nmi.h> |
| 28 | |
Robert Richter | 5104194 | 2011-12-15 17:56:36 +0100 | [diff] [blame] | 29 | #define IBS_FETCH_CONFIG_MASK (IBS_FETCH_RAND_EN | IBS_FETCH_MAX_CNT) |
| 30 | #define IBS_OP_CONFIG_MASK IBS_OP_MAX_CNT |
| 31 | |
Peter Zijlstra | 85dc600 | 2016-03-21 11:47:52 +0100 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | * IBS states: |
| 35 | * |
| 36 | * ENABLED; tracks the pmu::add(), pmu::del() state, when set the counter is taken |
| 37 | * and any further add()s must fail. |
| 38 | * |
| 39 | * STARTED/STOPPING/STOPPED; deal with pmu::start(), pmu::stop() state but are |
| 40 | * complicated by the fact that the IBS hardware can send late NMIs (ie. after |
| 41 | * we've cleared the EN bit). |
| 42 | * |
| 43 | * In order to consume these late NMIs we have the STOPPED state, any NMI that |
| 44 | * happens after we've cleared the EN state will clear this bit and report the |
| 45 | * NMI handled (this is fundamentally racy in the face or multiple NMI sources, |
| 46 | * someone else can consume our BIT and our NMI will go unhandled). |
| 47 | * |
| 48 | * And since we cannot set/clear this separate bit together with the EN bit, |
| 49 | * there are races; if we cleared STARTED early, an NMI could land in |
| 50 | * between clearing STARTED and clearing the EN bit (in fact multiple NMIs |
| 51 | * could happen if the period is small enough), and consume our STOPPED bit |
| 52 | * and trigger streams of unhandled NMIs. |
| 53 | * |
| 54 | * If, however, we clear STARTED late, an NMI can hit between clearing the |
| 55 | * EN bit and clearing STARTED, still see STARTED set and process the event. |
| 56 | * If this event will have the VALID bit clear, we bail properly, but this |
| 57 | * is not a given. With VALID set we can end up calling pmu::stop() again |
| 58 | * (the throttle logic) and trigger the WARNs in there. |
| 59 | * |
| 60 | * So what we do is set STOPPING before clearing EN to avoid the pmu::stop() |
| 61 | * nesting, and clear STARTED late, so that we have a well defined state over |
| 62 | * the clearing of the EN bit. |
| 63 | * |
| 64 | * XXX: we could probably be using !atomic bitops for all this. |
| 65 | */ |
| 66 | |
Robert Richter | 4db2e8e | 2011-12-15 17:56:38 +0100 | [diff] [blame] | 67 | enum ibs_states { |
| 68 | IBS_ENABLED = 0, |
| 69 | IBS_STARTED = 1, |
| 70 | IBS_STOPPING = 2, |
Peter Zijlstra | 85dc600 | 2016-03-21 11:47:52 +0100 | [diff] [blame] | 71 | IBS_STOPPED = 3, |
Robert Richter | 4db2e8e | 2011-12-15 17:56:38 +0100 | [diff] [blame] | 72 | |
| 73 | IBS_MAX_STATES, |
| 74 | }; |
| 75 | |
| 76 | struct cpu_perf_ibs { |
| 77 | struct perf_event *event; |
| 78 | unsigned long state[BITS_TO_LONGS(IBS_MAX_STATES)]; |
| 79 | }; |
| 80 | |
Robert Richter | 5104194 | 2011-12-15 17:56:36 +0100 | [diff] [blame] | 81 | struct perf_ibs { |
Robert Richter | 2e132b1 | 2012-09-12 12:59:44 +0200 | [diff] [blame] | 82 | struct pmu pmu; |
| 83 | unsigned int msr; |
| 84 | u64 config_mask; |
| 85 | u64 cnt_mask; |
| 86 | u64 enable_mask; |
| 87 | u64 valid_mask; |
| 88 | u64 max_period; |
| 89 | unsigned long offset_mask[1]; |
| 90 | int offset_max; |
| 91 | struct cpu_perf_ibs __percpu *pcpu; |
| 92 | |
| 93 | struct attribute **format_attrs; |
| 94 | struct attribute_group format_group; |
| 95 | const struct attribute_group *attr_groups[2]; |
| 96 | |
| 97 | u64 (*get_count)(u64 config); |
Robert Richter | b7074f1 | 2011-12-15 17:56:37 +0100 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | struct perf_ibs_data { |
| 101 | u32 size; |
| 102 | union { |
| 103 | u32 data[0]; /* data buffer starts here */ |
| 104 | u32 caps; |
| 105 | }; |
| 106 | u64 regs[MSR_AMD64_IBS_REG_COUNT_MAX]; |
Robert Richter | 5104194 | 2011-12-15 17:56:36 +0100 | [diff] [blame] | 107 | }; |
| 108 | |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 109 | static int |
Robert Richter | 98112d2 | 2012-04-02 20:19:13 +0200 | [diff] [blame] | 110 | perf_event_set_period(struct hw_perf_event *hwc, u64 min, u64 max, u64 *hw_period) |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 111 | { |
| 112 | s64 left = local64_read(&hwc->period_left); |
| 113 | s64 period = hwc->sample_period; |
| 114 | int overflow = 0; |
| 115 | |
| 116 | /* |
| 117 | * If we are way outside a reasonable range then just skip forward: |
| 118 | */ |
| 119 | if (unlikely(left <= -period)) { |
| 120 | left = period; |
| 121 | local64_set(&hwc->period_left, left); |
| 122 | hwc->last_period = period; |
| 123 | overflow = 1; |
| 124 | } |
| 125 | |
Robert Richter | fc006cf | 2012-04-02 20:19:14 +0200 | [diff] [blame] | 126 | if (unlikely(left < (s64)min)) { |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 127 | left += period; |
| 128 | local64_set(&hwc->period_left, left); |
| 129 | hwc->last_period = period; |
| 130 | overflow = 1; |
| 131 | } |
| 132 | |
Robert Richter | 7caaf4d | 2012-04-02 20:19:15 +0200 | [diff] [blame] | 133 | /* |
| 134 | * If the hw period that triggers the sw overflow is too short |
| 135 | * we might hit the irq handler. This biases the results. |
| 136 | * Thus we shorten the next-to-last period and set the last |
| 137 | * period to the max period. |
| 138 | */ |
| 139 | if (left > max) { |
| 140 | left -= max; |
| 141 | if (left > max) |
| 142 | left = max; |
| 143 | else if (left < min) |
| 144 | left = min; |
| 145 | } |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 146 | |
Robert Richter | 98112d2 | 2012-04-02 20:19:13 +0200 | [diff] [blame] | 147 | *hw_period = (u64)left; |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 148 | |
| 149 | return overflow; |
| 150 | } |
| 151 | |
| 152 | static int |
| 153 | perf_event_try_update(struct perf_event *event, u64 new_raw_count, int width) |
| 154 | { |
| 155 | struct hw_perf_event *hwc = &event->hw; |
| 156 | int shift = 64 - width; |
| 157 | u64 prev_raw_count; |
| 158 | u64 delta; |
| 159 | |
| 160 | /* |
| 161 | * Careful: an NMI might modify the previous event value. |
| 162 | * |
| 163 | * Our tactic to handle this is to first atomically read and |
| 164 | * exchange a new raw count - then add that new-prev delta |
| 165 | * count to the generic event atomically: |
| 166 | */ |
| 167 | prev_raw_count = local64_read(&hwc->prev_count); |
| 168 | if (local64_cmpxchg(&hwc->prev_count, prev_raw_count, |
| 169 | new_raw_count) != prev_raw_count) |
| 170 | return 0; |
| 171 | |
| 172 | /* |
| 173 | * Now we have the new raw value and have updated the prev |
| 174 | * timestamp already. We can now calculate the elapsed delta |
| 175 | * (event-)time and add that to the generic event. |
| 176 | * |
| 177 | * Careful, not all hw sign-extends above the physical width |
| 178 | * of the count. |
| 179 | */ |
| 180 | delta = (new_raw_count << shift) - (prev_raw_count << shift); |
| 181 | delta >>= shift; |
| 182 | |
| 183 | local64_add(delta, &event->count); |
| 184 | local64_sub(delta, &hwc->period_left); |
| 185 | |
| 186 | return 1; |
| 187 | } |
| 188 | |
Robert Richter | 5104194 | 2011-12-15 17:56:36 +0100 | [diff] [blame] | 189 | static struct perf_ibs perf_ibs_fetch; |
| 190 | static struct perf_ibs perf_ibs_op; |
| 191 | |
| 192 | static struct perf_ibs *get_ibs_pmu(int type) |
| 193 | { |
| 194 | if (perf_ibs_fetch.pmu.type == type) |
| 195 | return &perf_ibs_fetch; |
| 196 | if (perf_ibs_op.pmu.type == type) |
| 197 | return &perf_ibs_op; |
| 198 | return NULL; |
| 199 | } |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 200 | |
Robert Richter | 450bbd4 | 2012-03-12 12:54:32 +0100 | [diff] [blame] | 201 | /* |
| 202 | * Use IBS for precise event sampling: |
| 203 | * |
| 204 | * perf record -a -e cpu-cycles:p ... # use ibs op counting cycle count |
| 205 | * perf record -a -e r076:p ... # same as -e cpu-cycles:p |
| 206 | * perf record -a -e r0C1:p ... # use ibs op counting micro-ops |
| 207 | * |
| 208 | * IbsOpCntCtl (bit 19) of IBS Execution Control Register (IbsOpCtl, |
| 209 | * MSRC001_1033) is used to select either cycle or micro-ops counting |
| 210 | * mode. |
| 211 | * |
| 212 | * The rip of IBS samples has skid 0. Thus, IBS supports precise |
| 213 | * levels 1 and 2 and the PERF_EFLAGS_EXACT is set. In rare cases the |
| 214 | * rip is invalid when IBS was not able to record the rip correctly. |
| 215 | * We clear PERF_EFLAGS_EXACT and take the rip from pt_regs then. |
| 216 | * |
| 217 | */ |
| 218 | static int perf_ibs_precise_event(struct perf_event *event, u64 *config) |
| 219 | { |
| 220 | switch (event->attr.precise_ip) { |
| 221 | case 0: |
| 222 | return -ENOENT; |
| 223 | case 1: |
| 224 | case 2: |
| 225 | break; |
| 226 | default: |
| 227 | return -EOPNOTSUPP; |
| 228 | } |
| 229 | |
| 230 | switch (event->attr.type) { |
| 231 | case PERF_TYPE_HARDWARE: |
| 232 | switch (event->attr.config) { |
| 233 | case PERF_COUNT_HW_CPU_CYCLES: |
| 234 | *config = 0; |
| 235 | return 0; |
| 236 | } |
| 237 | break; |
| 238 | case PERF_TYPE_RAW: |
| 239 | switch (event->attr.config) { |
| 240 | case 0x0076: |
| 241 | *config = 0; |
| 242 | return 0; |
| 243 | case 0x00C1: |
| 244 | *config = IBS_OP_CNT_CTL; |
| 245 | return 0; |
| 246 | } |
| 247 | break; |
| 248 | default: |
| 249 | return -ENOENT; |
| 250 | } |
| 251 | |
| 252 | return -EOPNOTSUPP; |
| 253 | } |
| 254 | |
Robert Richter | bad9ac2 | 2012-07-25 19:12:45 +0200 | [diff] [blame] | 255 | static const struct perf_event_attr ibs_notsupp = { |
| 256 | .exclude_user = 1, |
| 257 | .exclude_kernel = 1, |
| 258 | .exclude_hv = 1, |
| 259 | .exclude_idle = 1, |
| 260 | .exclude_host = 1, |
| 261 | .exclude_guest = 1, |
| 262 | }; |
| 263 | |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 264 | static int perf_ibs_init(struct perf_event *event) |
| 265 | { |
Robert Richter | 5104194 | 2011-12-15 17:56:36 +0100 | [diff] [blame] | 266 | struct hw_perf_event *hwc = &event->hw; |
| 267 | struct perf_ibs *perf_ibs; |
| 268 | u64 max_cnt, config; |
Robert Richter | 450bbd4 | 2012-03-12 12:54:32 +0100 | [diff] [blame] | 269 | int ret; |
Robert Richter | 5104194 | 2011-12-15 17:56:36 +0100 | [diff] [blame] | 270 | |
| 271 | perf_ibs = get_ibs_pmu(event->attr.type); |
Robert Richter | 450bbd4 | 2012-03-12 12:54:32 +0100 | [diff] [blame] | 272 | if (perf_ibs) { |
| 273 | config = event->attr.config; |
| 274 | } else { |
| 275 | perf_ibs = &perf_ibs_op; |
| 276 | ret = perf_ibs_precise_event(event, &config); |
| 277 | if (ret) |
| 278 | return ret; |
| 279 | } |
| 280 | |
| 281 | if (event->pmu != &perf_ibs->pmu) |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 282 | return -ENOENT; |
Robert Richter | 5104194 | 2011-12-15 17:56:36 +0100 | [diff] [blame] | 283 | |
Robert Richter | bad9ac2 | 2012-07-25 19:12:45 +0200 | [diff] [blame] | 284 | if (perf_flags(&event->attr) & perf_flags(&ibs_notsupp)) |
| 285 | return -EINVAL; |
| 286 | |
Robert Richter | 5104194 | 2011-12-15 17:56:36 +0100 | [diff] [blame] | 287 | if (config & ~perf_ibs->config_mask) |
| 288 | return -EINVAL; |
| 289 | |
| 290 | if (hwc->sample_period) { |
| 291 | if (config & perf_ibs->cnt_mask) |
| 292 | /* raw max_cnt may not be set */ |
| 293 | return -EINVAL; |
Robert Richter | 6accb9c | 2012-04-02 20:19:10 +0200 | [diff] [blame] | 294 | if (!event->attr.sample_freq && hwc->sample_period & 0x0f) |
| 295 | /* |
| 296 | * lower 4 bits can not be set in ibs max cnt, |
| 297 | * but allowing it in case we adjust the |
| 298 | * sample period to set a frequency. |
| 299 | */ |
Robert Richter | 5104194 | 2011-12-15 17:56:36 +0100 | [diff] [blame] | 300 | return -EINVAL; |
Robert Richter | 6accb9c | 2012-04-02 20:19:10 +0200 | [diff] [blame] | 301 | hwc->sample_period &= ~0x0FULL; |
| 302 | if (!hwc->sample_period) |
| 303 | hwc->sample_period = 0x10; |
Robert Richter | 5104194 | 2011-12-15 17:56:36 +0100 | [diff] [blame] | 304 | } else { |
| 305 | max_cnt = config & perf_ibs->cnt_mask; |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 306 | config &= ~perf_ibs->cnt_mask; |
Robert Richter | 5104194 | 2011-12-15 17:56:36 +0100 | [diff] [blame] | 307 | event->attr.sample_period = max_cnt << 4; |
| 308 | hwc->sample_period = event->attr.sample_period; |
| 309 | } |
| 310 | |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 311 | if (!hwc->sample_period) |
Robert Richter | 5104194 | 2011-12-15 17:56:36 +0100 | [diff] [blame] | 312 | return -EINVAL; |
| 313 | |
Robert Richter | 6accb9c | 2012-04-02 20:19:10 +0200 | [diff] [blame] | 314 | /* |
| 315 | * If we modify hwc->sample_period, we also need to update |
| 316 | * hwc->last_period and hwc->period_left. |
| 317 | */ |
| 318 | hwc->last_period = hwc->sample_period; |
| 319 | local64_set(&hwc->period_left, hwc->sample_period); |
| 320 | |
Robert Richter | 5104194 | 2011-12-15 17:56:36 +0100 | [diff] [blame] | 321 | hwc->config_base = perf_ibs->msr; |
| 322 | hwc->config = config; |
| 323 | |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 324 | return 0; |
| 325 | } |
| 326 | |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 327 | static int perf_ibs_set_period(struct perf_ibs *perf_ibs, |
| 328 | struct hw_perf_event *hwc, u64 *period) |
| 329 | { |
Robert Richter | 98112d2 | 2012-04-02 20:19:13 +0200 | [diff] [blame] | 330 | int overflow; |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 331 | |
| 332 | /* ignore lower 4 bits in min count: */ |
Robert Richter | 98112d2 | 2012-04-02 20:19:13 +0200 | [diff] [blame] | 333 | overflow = perf_event_set_period(hwc, 1<<4, perf_ibs->max_period, period); |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 334 | local64_set(&hwc->prev_count, 0); |
| 335 | |
Robert Richter | 98112d2 | 2012-04-02 20:19:13 +0200 | [diff] [blame] | 336 | return overflow; |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | static u64 get_ibs_fetch_count(u64 config) |
| 340 | { |
| 341 | return (config & IBS_FETCH_CNT) >> 12; |
| 342 | } |
| 343 | |
| 344 | static u64 get_ibs_op_count(u64 config) |
| 345 | { |
Robert Richter | 8b1e136 | 2012-04-02 20:19:18 +0200 | [diff] [blame] | 346 | u64 count = 0; |
| 347 | |
| 348 | if (config & IBS_OP_VAL) |
| 349 | count += (config & IBS_OP_MAX_CNT) << 4; /* cnt rolled over */ |
| 350 | |
| 351 | if (ibs_caps & IBS_CAPS_RDWROPCNT) |
| 352 | count += (config & IBS_OP_CUR_CNT) >> 32; |
| 353 | |
| 354 | return count; |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | static void |
| 358 | perf_ibs_event_update(struct perf_ibs *perf_ibs, struct perf_event *event, |
Robert Richter | c9574fe | 2012-04-02 20:19:16 +0200 | [diff] [blame] | 359 | u64 *config) |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 360 | { |
Robert Richter | c9574fe | 2012-04-02 20:19:16 +0200 | [diff] [blame] | 361 | u64 count = perf_ibs->get_count(*config); |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 362 | |
Robert Richter | 8b1e136 | 2012-04-02 20:19:18 +0200 | [diff] [blame] | 363 | /* |
| 364 | * Set width to 64 since we do not overflow on max width but |
| 365 | * instead on max count. In perf_ibs_set_period() we clear |
| 366 | * prev count manually on overflow. |
| 367 | */ |
| 368 | while (!perf_event_try_update(event, count, 64)) { |
Robert Richter | c9574fe | 2012-04-02 20:19:16 +0200 | [diff] [blame] | 369 | rdmsrl(event->hw.config_base, *config); |
| 370 | count = perf_ibs->get_count(*config); |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
Robert Richter | c9574fe | 2012-04-02 20:19:16 +0200 | [diff] [blame] | 374 | static inline void perf_ibs_enable_event(struct perf_ibs *perf_ibs, |
| 375 | struct hw_perf_event *hwc, u64 config) |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 376 | { |
Robert Richter | c9574fe | 2012-04-02 20:19:16 +0200 | [diff] [blame] | 377 | wrmsrl(hwc->config_base, hwc->config | config | perf_ibs->enable_mask); |
| 378 | } |
| 379 | |
| 380 | /* |
| 381 | * Erratum #420 Instruction-Based Sampling Engine May Generate |
| 382 | * Interrupt that Cannot Be Cleared: |
| 383 | * |
| 384 | * Must clear counter mask first, then clear the enable bit. See |
| 385 | * Revision Guide for AMD Family 10h Processors, Publication #41322. |
| 386 | */ |
| 387 | static inline void perf_ibs_disable_event(struct perf_ibs *perf_ibs, |
| 388 | struct hw_perf_event *hwc, u64 config) |
| 389 | { |
| 390 | config &= ~perf_ibs->cnt_mask; |
| 391 | wrmsrl(hwc->config_base, config); |
| 392 | config &= ~perf_ibs->enable_mask; |
| 393 | wrmsrl(hwc->config_base, config); |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | /* |
| 397 | * We cannot restore the ibs pmu state, so we always needs to update |
| 398 | * the event while stopping it and then reset the state when starting |
| 399 | * again. Thus, ignoring PERF_EF_RELOAD and PERF_EF_UPDATE flags in |
| 400 | * perf_ibs_start()/perf_ibs_stop() and instead always do it. |
| 401 | */ |
Robert Richter | 4db2e8e | 2011-12-15 17:56:38 +0100 | [diff] [blame] | 402 | static void perf_ibs_start(struct perf_event *event, int flags) |
| 403 | { |
| 404 | struct hw_perf_event *hwc = &event->hw; |
| 405 | struct perf_ibs *perf_ibs = container_of(event->pmu, struct perf_ibs, pmu); |
| 406 | struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu); |
Robert Richter | c9574fe | 2012-04-02 20:19:16 +0200 | [diff] [blame] | 407 | u64 period; |
Robert Richter | 4db2e8e | 2011-12-15 17:56:38 +0100 | [diff] [blame] | 408 | |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 409 | if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED))) |
Robert Richter | 4db2e8e | 2011-12-15 17:56:38 +0100 | [diff] [blame] | 410 | return; |
| 411 | |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 412 | WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE)); |
| 413 | hwc->state = 0; |
| 414 | |
Robert Richter | c9574fe | 2012-04-02 20:19:16 +0200 | [diff] [blame] | 415 | perf_ibs_set_period(perf_ibs, hwc, &period); |
Peter Zijlstra | 5a50f52 | 2016-03-16 23:55:21 +0100 | [diff] [blame] | 416 | /* |
Peter Zijlstra | 85dc600 | 2016-03-21 11:47:52 +0100 | [diff] [blame] | 417 | * Set STARTED before enabling the hardware, such that a subsequent NMI |
| 418 | * must observe it. |
Peter Zijlstra | 5a50f52 | 2016-03-16 23:55:21 +0100 | [diff] [blame] | 419 | */ |
Peter Zijlstra | 85dc600 | 2016-03-21 11:47:52 +0100 | [diff] [blame] | 420 | set_bit(IBS_STARTED, pcpu->state); |
Peter Zijlstra | 5a50f52 | 2016-03-16 23:55:21 +0100 | [diff] [blame] | 421 | clear_bit(IBS_STOPPING, pcpu->state); |
Robert Richter | c9574fe | 2012-04-02 20:19:16 +0200 | [diff] [blame] | 422 | perf_ibs_enable_event(perf_ibs, hwc, period >> 4); |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 423 | |
| 424 | perf_event_update_userpage(event); |
Robert Richter | 4db2e8e | 2011-12-15 17:56:38 +0100 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | static void perf_ibs_stop(struct perf_event *event, int flags) |
| 428 | { |
| 429 | struct hw_perf_event *hwc = &event->hw; |
| 430 | struct perf_ibs *perf_ibs = container_of(event->pmu, struct perf_ibs, pmu); |
| 431 | struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu); |
Robert Richter | c9574fe | 2012-04-02 20:19:16 +0200 | [diff] [blame] | 432 | u64 config; |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 433 | int stopping; |
Robert Richter | 4db2e8e | 2011-12-15 17:56:38 +0100 | [diff] [blame] | 434 | |
Peter Zijlstra | 85dc600 | 2016-03-21 11:47:52 +0100 | [diff] [blame] | 435 | if (test_and_set_bit(IBS_STOPPING, pcpu->state)) |
| 436 | return; |
| 437 | |
Peter Zijlstra | 5a50f52 | 2016-03-16 23:55:21 +0100 | [diff] [blame] | 438 | stopping = test_bit(IBS_STARTED, pcpu->state); |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 439 | |
| 440 | if (!stopping && (hwc->state & PERF_HES_UPTODATE)) |
Robert Richter | 4db2e8e | 2011-12-15 17:56:38 +0100 | [diff] [blame] | 441 | return; |
| 442 | |
Robert Richter | c9574fe | 2012-04-02 20:19:16 +0200 | [diff] [blame] | 443 | rdmsrl(hwc->config_base, config); |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 444 | |
| 445 | if (stopping) { |
Peter Zijlstra | 5a50f52 | 2016-03-16 23:55:21 +0100 | [diff] [blame] | 446 | /* |
Peter Zijlstra | 85dc600 | 2016-03-21 11:47:52 +0100 | [diff] [blame] | 447 | * Set STOPPED before disabling the hardware, such that it |
Peter Zijlstra | 5a50f52 | 2016-03-16 23:55:21 +0100 | [diff] [blame] | 448 | * must be visible to NMIs the moment we clear the EN bit, |
| 449 | * at which point we can generate an !VALID sample which |
| 450 | * we need to consume. |
| 451 | */ |
Peter Zijlstra | 85dc600 | 2016-03-21 11:47:52 +0100 | [diff] [blame] | 452 | set_bit(IBS_STOPPED, pcpu->state); |
Robert Richter | c9574fe | 2012-04-02 20:19:16 +0200 | [diff] [blame] | 453 | perf_ibs_disable_event(perf_ibs, hwc, config); |
Peter Zijlstra | 5a50f52 | 2016-03-16 23:55:21 +0100 | [diff] [blame] | 454 | /* |
| 455 | * Clear STARTED after disabling the hardware; if it were |
| 456 | * cleared before an NMI hitting after the clear but before |
| 457 | * clearing the EN bit might think it a spurious NMI and not |
| 458 | * handle it. |
| 459 | * |
| 460 | * Clearing it after, however, creates the problem of the NMI |
| 461 | * handler seeing STARTED but not having a valid sample. |
| 462 | */ |
| 463 | clear_bit(IBS_STARTED, pcpu->state); |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 464 | WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED); |
| 465 | hwc->state |= PERF_HES_STOPPED; |
| 466 | } |
| 467 | |
| 468 | if (hwc->state & PERF_HES_UPTODATE) |
| 469 | return; |
| 470 | |
Robert Richter | 8b1e136 | 2012-04-02 20:19:18 +0200 | [diff] [blame] | 471 | /* |
| 472 | * Clear valid bit to not count rollovers on update, rollovers |
| 473 | * are only updated in the irq handler. |
| 474 | */ |
| 475 | config &= ~perf_ibs->valid_mask; |
| 476 | |
Robert Richter | c9574fe | 2012-04-02 20:19:16 +0200 | [diff] [blame] | 477 | perf_ibs_event_update(perf_ibs, event, &config); |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 478 | hwc->state |= PERF_HES_UPTODATE; |
Robert Richter | 4db2e8e | 2011-12-15 17:56:38 +0100 | [diff] [blame] | 479 | } |
| 480 | |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 481 | static int perf_ibs_add(struct perf_event *event, int flags) |
| 482 | { |
Robert Richter | 4db2e8e | 2011-12-15 17:56:38 +0100 | [diff] [blame] | 483 | struct perf_ibs *perf_ibs = container_of(event->pmu, struct perf_ibs, pmu); |
| 484 | struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu); |
| 485 | |
| 486 | if (test_and_set_bit(IBS_ENABLED, pcpu->state)) |
| 487 | return -ENOSPC; |
| 488 | |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 489 | event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED; |
| 490 | |
Robert Richter | 4db2e8e | 2011-12-15 17:56:38 +0100 | [diff] [blame] | 491 | pcpu->event = event; |
| 492 | |
| 493 | if (flags & PERF_EF_START) |
| 494 | perf_ibs_start(event, PERF_EF_RELOAD); |
| 495 | |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | static void perf_ibs_del(struct perf_event *event, int flags) |
| 500 | { |
Robert Richter | 4db2e8e | 2011-12-15 17:56:38 +0100 | [diff] [blame] | 501 | struct perf_ibs *perf_ibs = container_of(event->pmu, struct perf_ibs, pmu); |
| 502 | struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu); |
| 503 | |
| 504 | if (!test_and_clear_bit(IBS_ENABLED, pcpu->state)) |
| 505 | return; |
| 506 | |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 507 | perf_ibs_stop(event, PERF_EF_UPDATE); |
Robert Richter | 4db2e8e | 2011-12-15 17:56:38 +0100 | [diff] [blame] | 508 | |
| 509 | pcpu->event = NULL; |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 510 | |
| 511 | perf_event_update_userpage(event); |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 512 | } |
| 513 | |
Robert Richter | 4db2e8e | 2011-12-15 17:56:38 +0100 | [diff] [blame] | 514 | static void perf_ibs_read(struct perf_event *event) { } |
| 515 | |
Robert Richter | 2e132b1 | 2012-09-12 12:59:44 +0200 | [diff] [blame] | 516 | PMU_FORMAT_ATTR(rand_en, "config:57"); |
| 517 | PMU_FORMAT_ATTR(cnt_ctl, "config:19"); |
| 518 | |
| 519 | static struct attribute *ibs_fetch_format_attrs[] = { |
| 520 | &format_attr_rand_en.attr, |
| 521 | NULL, |
| 522 | }; |
| 523 | |
| 524 | static struct attribute *ibs_op_format_attrs[] = { |
| 525 | NULL, /* &format_attr_cnt_ctl.attr if IBS_CAPS_OPCNT */ |
| 526 | NULL, |
| 527 | }; |
| 528 | |
Robert Richter | 5104194 | 2011-12-15 17:56:36 +0100 | [diff] [blame] | 529 | static struct perf_ibs perf_ibs_fetch = { |
| 530 | .pmu = { |
| 531 | .task_ctx_nr = perf_invalid_context, |
| 532 | |
| 533 | .event_init = perf_ibs_init, |
| 534 | .add = perf_ibs_add, |
| 535 | .del = perf_ibs_del, |
Robert Richter | 4db2e8e | 2011-12-15 17:56:38 +0100 | [diff] [blame] | 536 | .start = perf_ibs_start, |
| 537 | .stop = perf_ibs_stop, |
| 538 | .read = perf_ibs_read, |
Robert Richter | 5104194 | 2011-12-15 17:56:36 +0100 | [diff] [blame] | 539 | }, |
| 540 | .msr = MSR_AMD64_IBSFETCHCTL, |
| 541 | .config_mask = IBS_FETCH_CONFIG_MASK, |
| 542 | .cnt_mask = IBS_FETCH_MAX_CNT, |
| 543 | .enable_mask = IBS_FETCH_ENABLE, |
Robert Richter | b7074f1 | 2011-12-15 17:56:37 +0100 | [diff] [blame] | 544 | .valid_mask = IBS_FETCH_VAL, |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 545 | .max_period = IBS_FETCH_MAX_CNT << 4, |
Robert Richter | b7074f1 | 2011-12-15 17:56:37 +0100 | [diff] [blame] | 546 | .offset_mask = { MSR_AMD64_IBSFETCH_REG_MASK }, |
| 547 | .offset_max = MSR_AMD64_IBSFETCH_REG_COUNT, |
Robert Richter | 2e132b1 | 2012-09-12 12:59:44 +0200 | [diff] [blame] | 548 | .format_attrs = ibs_fetch_format_attrs, |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 549 | |
| 550 | .get_count = get_ibs_fetch_count, |
Robert Richter | 5104194 | 2011-12-15 17:56:36 +0100 | [diff] [blame] | 551 | }; |
| 552 | |
| 553 | static struct perf_ibs perf_ibs_op = { |
| 554 | .pmu = { |
| 555 | .task_ctx_nr = perf_invalid_context, |
| 556 | |
| 557 | .event_init = perf_ibs_init, |
| 558 | .add = perf_ibs_add, |
| 559 | .del = perf_ibs_del, |
Robert Richter | 4db2e8e | 2011-12-15 17:56:38 +0100 | [diff] [blame] | 560 | .start = perf_ibs_start, |
| 561 | .stop = perf_ibs_stop, |
| 562 | .read = perf_ibs_read, |
Robert Richter | 5104194 | 2011-12-15 17:56:36 +0100 | [diff] [blame] | 563 | }, |
| 564 | .msr = MSR_AMD64_IBSOPCTL, |
| 565 | .config_mask = IBS_OP_CONFIG_MASK, |
| 566 | .cnt_mask = IBS_OP_MAX_CNT, |
| 567 | .enable_mask = IBS_OP_ENABLE, |
Robert Richter | b7074f1 | 2011-12-15 17:56:37 +0100 | [diff] [blame] | 568 | .valid_mask = IBS_OP_VAL, |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 569 | .max_period = IBS_OP_MAX_CNT << 4, |
Robert Richter | b7074f1 | 2011-12-15 17:56:37 +0100 | [diff] [blame] | 570 | .offset_mask = { MSR_AMD64_IBSOP_REG_MASK }, |
| 571 | .offset_max = MSR_AMD64_IBSOP_REG_COUNT, |
Robert Richter | 2e132b1 | 2012-09-12 12:59:44 +0200 | [diff] [blame] | 572 | .format_attrs = ibs_op_format_attrs, |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 573 | |
| 574 | .get_count = get_ibs_op_count, |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 575 | }; |
| 576 | |
Robert Richter | b7074f1 | 2011-12-15 17:56:37 +0100 | [diff] [blame] | 577 | static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs) |
| 578 | { |
Robert Richter | 4db2e8e | 2011-12-15 17:56:38 +0100 | [diff] [blame] | 579 | struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu); |
| 580 | struct perf_event *event = pcpu->event; |
Robert Richter | b7074f1 | 2011-12-15 17:56:37 +0100 | [diff] [blame] | 581 | struct hw_perf_event *hwc = &event->hw; |
| 582 | struct perf_sample_data data; |
| 583 | struct perf_raw_record raw; |
| 584 | struct pt_regs regs; |
| 585 | struct perf_ibs_data ibs_data; |
Robert Richter | d47e823 | 2012-04-02 20:19:11 +0200 | [diff] [blame] | 586 | int offset, size, check_rip, offset_max, throttle = 0; |
Robert Richter | b7074f1 | 2011-12-15 17:56:37 +0100 | [diff] [blame] | 587 | unsigned int msr; |
Robert Richter | c9574fe | 2012-04-02 20:19:16 +0200 | [diff] [blame] | 588 | u64 *buf, *config, period; |
Robert Richter | b7074f1 | 2011-12-15 17:56:37 +0100 | [diff] [blame] | 589 | |
Robert Richter | 4db2e8e | 2011-12-15 17:56:38 +0100 | [diff] [blame] | 590 | if (!test_bit(IBS_STARTED, pcpu->state)) { |
Peter Zijlstra | 5a50f52 | 2016-03-16 23:55:21 +0100 | [diff] [blame] | 591 | fail: |
Robert Richter | fc5fb2b | 2012-04-02 20:19:17 +0200 | [diff] [blame] | 592 | /* |
| 593 | * Catch spurious interrupts after stopping IBS: After |
Jorrit Schippers | d82603c | 2012-12-27 17:33:02 +0100 | [diff] [blame] | 594 | * disabling IBS there could be still incoming NMIs |
Robert Richter | fc5fb2b | 2012-04-02 20:19:17 +0200 | [diff] [blame] | 595 | * with samples that even have the valid bit cleared. |
| 596 | * Mark all this NMIs as handled. |
| 597 | */ |
Peter Zijlstra | 85dc600 | 2016-03-21 11:47:52 +0100 | [diff] [blame] | 598 | if (test_and_clear_bit(IBS_STOPPED, pcpu->state)) |
Peter Zijlstra | 5a50f52 | 2016-03-16 23:55:21 +0100 | [diff] [blame] | 599 | return 1; |
| 600 | |
| 601 | return 0; |
Robert Richter | 4db2e8e | 2011-12-15 17:56:38 +0100 | [diff] [blame] | 602 | } |
| 603 | |
Robert Richter | b7074f1 | 2011-12-15 17:56:37 +0100 | [diff] [blame] | 604 | msr = hwc->config_base; |
| 605 | buf = ibs_data.regs; |
| 606 | rdmsrl(msr, *buf); |
| 607 | if (!(*buf++ & perf_ibs->valid_mask)) |
Peter Zijlstra | 5a50f52 | 2016-03-16 23:55:21 +0100 | [diff] [blame] | 608 | goto fail; |
Robert Richter | b7074f1 | 2011-12-15 17:56:37 +0100 | [diff] [blame] | 609 | |
Robert Richter | c9574fe | 2012-04-02 20:19:16 +0200 | [diff] [blame] | 610 | config = &ibs_data.regs[0]; |
Robert Richter | c75841a | 2012-04-02 20:19:07 +0200 | [diff] [blame] | 611 | perf_ibs_event_update(perf_ibs, event, config); |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 612 | perf_sample_data_init(&data, 0, hwc->last_period); |
Robert Richter | c9574fe | 2012-04-02 20:19:16 +0200 | [diff] [blame] | 613 | if (!perf_ibs_set_period(perf_ibs, hwc, &period)) |
Robert Richter | d47e823 | 2012-04-02 20:19:11 +0200 | [diff] [blame] | 614 | goto out; /* no sw counter overflow */ |
| 615 | |
| 616 | ibs_data.caps = ibs_caps; |
| 617 | size = 1; |
| 618 | offset = 1; |
| 619 | check_rip = (perf_ibs == &perf_ibs_op && (ibs_caps & IBS_CAPS_RIPINVALIDCHK)); |
| 620 | if (event->attr.sample_type & PERF_SAMPLE_RAW) |
| 621 | offset_max = perf_ibs->offset_max; |
| 622 | else if (check_rip) |
| 623 | offset_max = 2; |
| 624 | else |
| 625 | offset_max = 1; |
| 626 | do { |
| 627 | rdmsrl(msr + offset, *buf++); |
| 628 | size++; |
| 629 | offset = find_next_bit(perf_ibs->offset_mask, |
| 630 | perf_ibs->offset_max, |
| 631 | offset + 1); |
| 632 | } while (offset < offset_max); |
Aravind Gopalakrishnan | 904cb36 | 2014-11-10 14:24:26 -0600 | [diff] [blame] | 633 | if (event->attr.sample_type & PERF_SAMPLE_RAW) { |
| 634 | /* |
| 635 | * Read IbsBrTarget and IbsOpData4 separately |
| 636 | * depending on their availability. |
| 637 | * Can't add to offset_max as they are staggered |
| 638 | */ |
| 639 | if (ibs_caps & IBS_CAPS_BRNTRGT) { |
| 640 | rdmsrl(MSR_AMD64_IBSBRTARGET, *buf++); |
| 641 | size++; |
| 642 | } |
| 643 | if (ibs_caps & IBS_CAPS_OPDATA4) { |
| 644 | rdmsrl(MSR_AMD64_IBSOPDATA4, *buf++); |
| 645 | size++; |
| 646 | } |
| 647 | } |
Robert Richter | d47e823 | 2012-04-02 20:19:11 +0200 | [diff] [blame] | 648 | ibs_data.size = sizeof(u64) * size; |
| 649 | |
| 650 | regs = *iregs; |
Robert Richter | 450bbd4 | 2012-03-12 12:54:32 +0100 | [diff] [blame] | 651 | if (check_rip && (ibs_data.regs[2] & IBS_RIP_INVALID)) { |
| 652 | regs.flags &= ~PERF_EFLAGS_EXACT; |
| 653 | } else { |
Peter Zijlstra | d07bdfd | 2012-07-10 09:42:15 +0200 | [diff] [blame] | 654 | set_linear_ip(®s, ibs_data.regs[1]); |
Robert Richter | 450bbd4 | 2012-03-12 12:54:32 +0100 | [diff] [blame] | 655 | regs.flags |= PERF_EFLAGS_EXACT; |
| 656 | } |
Robert Richter | c75841a | 2012-04-02 20:19:07 +0200 | [diff] [blame] | 657 | |
Robert Richter | b7074f1 | 2011-12-15 17:56:37 +0100 | [diff] [blame] | 658 | if (event->attr.sample_type & PERF_SAMPLE_RAW) { |
Daniel Borkmann | 7e3f977 | 2016-07-14 18:08:03 +0200 | [diff] [blame] | 659 | raw = (struct perf_raw_record){ |
| 660 | .frag = { |
| 661 | .size = sizeof(u32) + ibs_data.size, |
| 662 | .data = ibs_data.data, |
| 663 | }, |
| 664 | }; |
Robert Richter | b7074f1 | 2011-12-15 17:56:37 +0100 | [diff] [blame] | 665 | data.raw = &raw; |
| 666 | } |
| 667 | |
Robert Richter | d47e823 | 2012-04-02 20:19:11 +0200 | [diff] [blame] | 668 | throttle = perf_event_overflow(event, &data, ®s); |
| 669 | out: |
Robert Richter | c9574fe | 2012-04-02 20:19:16 +0200 | [diff] [blame] | 670 | if (throttle) |
Peter Zijlstra | 0158b83 | 2016-03-11 15:23:46 +0100 | [diff] [blame] | 671 | perf_ibs_stop(event, 0); |
Robert Richter | c9574fe | 2012-04-02 20:19:16 +0200 | [diff] [blame] | 672 | else |
| 673 | perf_ibs_enable_event(perf_ibs, hwc, period >> 4); |
Robert Richter | db98c5f | 2011-12-15 17:56:39 +0100 | [diff] [blame] | 674 | |
| 675 | perf_event_update_userpage(event); |
Robert Richter | b7074f1 | 2011-12-15 17:56:37 +0100 | [diff] [blame] | 676 | |
| 677 | return 1; |
| 678 | } |
| 679 | |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 680 | static int |
Robert Richter | b7074f1 | 2011-12-15 17:56:37 +0100 | [diff] [blame] | 681 | perf_ibs_nmi_handler(unsigned int cmd, struct pt_regs *regs) |
| 682 | { |
Peter Zijlstra | c2872d3 | 2016-03-17 15:06:58 +0100 | [diff] [blame] | 683 | u64 stamp = sched_clock(); |
Robert Richter | b7074f1 | 2011-12-15 17:56:37 +0100 | [diff] [blame] | 684 | int handled = 0; |
| 685 | |
| 686 | handled += perf_ibs_handle_irq(&perf_ibs_fetch, regs); |
| 687 | handled += perf_ibs_handle_irq(&perf_ibs_op, regs); |
| 688 | |
| 689 | if (handled) |
| 690 | inc_irq_stat(apic_perf_irqs); |
| 691 | |
Peter Zijlstra | c2872d3 | 2016-03-17 15:06:58 +0100 | [diff] [blame] | 692 | perf_sample_event_took(sched_clock() - stamp); |
| 693 | |
Robert Richter | b7074f1 | 2011-12-15 17:56:37 +0100 | [diff] [blame] | 694 | return handled; |
| 695 | } |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 696 | NOKPROBE_SYMBOL(perf_ibs_nmi_handler); |
Robert Richter | b7074f1 | 2011-12-15 17:56:37 +0100 | [diff] [blame] | 697 | |
Robert Richter | 4db2e8e | 2011-12-15 17:56:38 +0100 | [diff] [blame] | 698 | static __init int perf_ibs_pmu_init(struct perf_ibs *perf_ibs, char *name) |
| 699 | { |
| 700 | struct cpu_perf_ibs __percpu *pcpu; |
| 701 | int ret; |
| 702 | |
| 703 | pcpu = alloc_percpu(struct cpu_perf_ibs); |
| 704 | if (!pcpu) |
| 705 | return -ENOMEM; |
| 706 | |
| 707 | perf_ibs->pcpu = pcpu; |
| 708 | |
Robert Richter | 2e132b1 | 2012-09-12 12:59:44 +0200 | [diff] [blame] | 709 | /* register attributes */ |
| 710 | if (perf_ibs->format_attrs[0]) { |
| 711 | memset(&perf_ibs->format_group, 0, sizeof(perf_ibs->format_group)); |
| 712 | perf_ibs->format_group.name = "format"; |
| 713 | perf_ibs->format_group.attrs = perf_ibs->format_attrs; |
| 714 | |
| 715 | memset(&perf_ibs->attr_groups, 0, sizeof(perf_ibs->attr_groups)); |
| 716 | perf_ibs->attr_groups[0] = &perf_ibs->format_group; |
| 717 | perf_ibs->pmu.attr_groups = perf_ibs->attr_groups; |
| 718 | } |
| 719 | |
Robert Richter | 4db2e8e | 2011-12-15 17:56:38 +0100 | [diff] [blame] | 720 | ret = perf_pmu_register(&perf_ibs->pmu, name, -1); |
| 721 | if (ret) { |
| 722 | perf_ibs->pcpu = NULL; |
| 723 | free_percpu(pcpu); |
| 724 | } |
| 725 | |
| 726 | return ret; |
| 727 | } |
| 728 | |
Thomas Gleixner | 9744f7b | 2016-07-13 17:16:14 +0000 | [diff] [blame] | 729 | static __init void perf_event_ibs_init(void) |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 730 | { |
Robert Richter | 2e132b1 | 2012-09-12 12:59:44 +0200 | [diff] [blame] | 731 | struct attribute **attr = ibs_op_format_attrs; |
| 732 | |
Robert Richter | 4db2e8e | 2011-12-15 17:56:38 +0100 | [diff] [blame] | 733 | perf_ibs_pmu_init(&perf_ibs_fetch, "ibs_fetch"); |
Robert Richter | 2e132b1 | 2012-09-12 12:59:44 +0200 | [diff] [blame] | 734 | |
| 735 | if (ibs_caps & IBS_CAPS_OPCNT) { |
Robert Richter | 7bf3523 | 2012-04-02 20:19:09 +0200 | [diff] [blame] | 736 | perf_ibs_op.config_mask |= IBS_OP_CNT_CTL; |
Robert Richter | 2e132b1 | 2012-09-12 12:59:44 +0200 | [diff] [blame] | 737 | *attr++ = &format_attr_cnt_ctl.attr; |
| 738 | } |
Robert Richter | 4db2e8e | 2011-12-15 17:56:38 +0100 | [diff] [blame] | 739 | perf_ibs_pmu_init(&perf_ibs_op, "ibs_op"); |
Robert Richter | 2e132b1 | 2012-09-12 12:59:44 +0200 | [diff] [blame] | 740 | |
Ingo Molnar | fab0699 | 2012-04-25 12:55:22 +0200 | [diff] [blame] | 741 | register_nmi_handler(NMI_LOCAL, perf_ibs_nmi_handler, 0, "perf_ibs"); |
Chen Yucong | 1b74dde | 2016-02-02 11:45:02 +0800 | [diff] [blame] | 742 | pr_info("perf: AMD IBS detected (0x%08x)\n", ibs_caps); |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | #else /* defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_AMD) */ |
| 746 | |
Thomas Gleixner | 9744f7b | 2016-07-13 17:16:14 +0000 | [diff] [blame] | 747 | static __init void perf_event_ibs_init(void) { } |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 748 | |
| 749 | #endif |
| 750 | |
| 751 | /* IBS - apic initialization, for perf and oprofile */ |
| 752 | |
| 753 | static __init u32 __get_ibs_caps(void) |
| 754 | { |
| 755 | u32 caps; |
| 756 | unsigned int max_level; |
| 757 | |
| 758 | if (!boot_cpu_has(X86_FEATURE_IBS)) |
| 759 | return 0; |
| 760 | |
| 761 | /* check IBS cpuid feature flags */ |
| 762 | max_level = cpuid_eax(0x80000000); |
| 763 | if (max_level < IBS_CPUID_FEATURES) |
| 764 | return IBS_CAPS_DEFAULT; |
| 765 | |
| 766 | caps = cpuid_eax(IBS_CPUID_FEATURES); |
| 767 | if (!(caps & IBS_CAPS_AVAIL)) |
| 768 | /* cpuid flags not valid */ |
| 769 | return IBS_CAPS_DEFAULT; |
| 770 | |
| 771 | return caps; |
| 772 | } |
| 773 | |
| 774 | u32 get_ibs_caps(void) |
| 775 | { |
| 776 | return ibs_caps; |
| 777 | } |
| 778 | |
| 779 | EXPORT_SYMBOL(get_ibs_caps); |
| 780 | |
| 781 | static inline int get_eilvt(int offset) |
| 782 | { |
| 783 | return !setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_NMI, 1); |
| 784 | } |
| 785 | |
| 786 | static inline int put_eilvt(int offset) |
| 787 | { |
| 788 | return !setup_APIC_eilvt(offset, 0, 0, 1); |
| 789 | } |
| 790 | |
| 791 | /* |
| 792 | * Check and reserve APIC extended interrupt LVT offset for IBS if available. |
| 793 | */ |
| 794 | static inline int ibs_eilvt_valid(void) |
| 795 | { |
| 796 | int offset; |
| 797 | u64 val; |
| 798 | int valid = 0; |
| 799 | |
| 800 | preempt_disable(); |
| 801 | |
| 802 | rdmsrl(MSR_AMD64_IBSCTL, val); |
| 803 | offset = val & IBSCTL_LVT_OFFSET_MASK; |
| 804 | |
| 805 | if (!(val & IBSCTL_LVT_OFFSET_VALID)) { |
| 806 | pr_err(FW_BUG "cpu %d, invalid IBS interrupt offset %d (MSR%08X=0x%016llx)\n", |
| 807 | smp_processor_id(), offset, MSR_AMD64_IBSCTL, val); |
| 808 | goto out; |
| 809 | } |
| 810 | |
| 811 | if (!get_eilvt(offset)) { |
| 812 | pr_err(FW_BUG "cpu %d, IBS interrupt offset %d not available (MSR%08X=0x%016llx)\n", |
| 813 | smp_processor_id(), offset, MSR_AMD64_IBSCTL, val); |
| 814 | goto out; |
| 815 | } |
| 816 | |
| 817 | valid = 1; |
| 818 | out: |
| 819 | preempt_enable(); |
| 820 | |
| 821 | return valid; |
| 822 | } |
| 823 | |
| 824 | static int setup_ibs_ctl(int ibs_eilvt_off) |
| 825 | { |
| 826 | struct pci_dev *cpu_cfg; |
| 827 | int nodes; |
| 828 | u32 value = 0; |
| 829 | |
| 830 | nodes = 0; |
| 831 | cpu_cfg = NULL; |
| 832 | do { |
| 833 | cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD, |
| 834 | PCI_DEVICE_ID_AMD_10H_NB_MISC, |
| 835 | cpu_cfg); |
| 836 | if (!cpu_cfg) |
| 837 | break; |
| 838 | ++nodes; |
| 839 | pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off |
| 840 | | IBSCTL_LVT_OFFSET_VALID); |
| 841 | pci_read_config_dword(cpu_cfg, IBSCTL, &value); |
| 842 | if (value != (ibs_eilvt_off | IBSCTL_LVT_OFFSET_VALID)) { |
| 843 | pci_dev_put(cpu_cfg); |
Chen Yucong | 1b74dde | 2016-02-02 11:45:02 +0800 | [diff] [blame] | 844 | pr_debug("Failed to setup IBS LVT offset, IBSCTL = 0x%08x\n", |
| 845 | value); |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 846 | return -EINVAL; |
| 847 | } |
| 848 | } while (1); |
| 849 | |
| 850 | if (!nodes) { |
Chen Yucong | 1b74dde | 2016-02-02 11:45:02 +0800 | [diff] [blame] | 851 | pr_debug("No CPU node configured for IBS\n"); |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 852 | return -ENODEV; |
| 853 | } |
| 854 | |
| 855 | return 0; |
| 856 | } |
| 857 | |
| 858 | /* |
| 859 | * This runs only on the current cpu. We try to find an LVT offset and |
| 860 | * setup the local APIC. For this we must disable preemption. On |
| 861 | * success we initialize all nodes with this offset. This updates then |
| 862 | * the offset in the IBS_CTL per-node msr. The per-core APIC setup of |
| 863 | * the IBS interrupt vector is handled by perf_ibs_cpu_notifier that |
| 864 | * is using the new offset. |
| 865 | */ |
Aravind Gopalakrishnan | c796b20 | 2015-01-23 12:19:35 -0600 | [diff] [blame] | 866 | static void force_ibs_eilvt_setup(void) |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 867 | { |
| 868 | int offset; |
| 869 | int ret; |
| 870 | |
| 871 | preempt_disable(); |
| 872 | /* find the next free available EILVT entry, skip offset 0 */ |
| 873 | for (offset = 1; offset < APIC_EILVT_NR_MAX; offset++) { |
| 874 | if (get_eilvt(offset)) |
| 875 | break; |
| 876 | } |
| 877 | preempt_enable(); |
| 878 | |
| 879 | if (offset == APIC_EILVT_NR_MAX) { |
Chen Yucong | 1b74dde | 2016-02-02 11:45:02 +0800 | [diff] [blame] | 880 | pr_debug("No EILVT entry available\n"); |
Aravind Gopalakrishnan | c796b20 | 2015-01-23 12:19:35 -0600 | [diff] [blame] | 881 | return; |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | ret = setup_ibs_ctl(offset); |
| 885 | if (ret) |
| 886 | goto out; |
| 887 | |
Aravind Gopalakrishnan | c796b20 | 2015-01-23 12:19:35 -0600 | [diff] [blame] | 888 | if (!ibs_eilvt_valid()) |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 889 | goto out; |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 890 | |
Robert Richter | 16e5294 | 2011-11-08 19:20:44 +0100 | [diff] [blame] | 891 | pr_info("IBS: LVT offset %d assigned\n", offset); |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 892 | |
Aravind Gopalakrishnan | c796b20 | 2015-01-23 12:19:35 -0600 | [diff] [blame] | 893 | return; |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 894 | out: |
| 895 | preempt_disable(); |
| 896 | put_eilvt(offset); |
| 897 | preempt_enable(); |
Aravind Gopalakrishnan | c796b20 | 2015-01-23 12:19:35 -0600 | [diff] [blame] | 898 | return; |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 899 | } |
| 900 | |
Robert Richter | bee09ed | 2014-01-15 15:57:29 +0100 | [diff] [blame] | 901 | static void ibs_eilvt_setup(void) |
| 902 | { |
| 903 | /* |
| 904 | * Force LVT offset assignment for family 10h: The offsets are |
| 905 | * not assigned by the BIOS for this family, so the OS is |
| 906 | * responsible for doing it. If the OS assignment fails, fall |
| 907 | * back to BIOS settings and try to setup this. |
| 908 | */ |
| 909 | if (boot_cpu_data.x86 == 0x10) |
| 910 | force_ibs_eilvt_setup(); |
| 911 | } |
| 912 | |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 913 | static inline int get_ibs_lvt_offset(void) |
| 914 | { |
| 915 | u64 val; |
| 916 | |
| 917 | rdmsrl(MSR_AMD64_IBSCTL, val); |
| 918 | if (!(val & IBSCTL_LVT_OFFSET_VALID)) |
| 919 | return -EINVAL; |
| 920 | |
| 921 | return val & IBSCTL_LVT_OFFSET_MASK; |
| 922 | } |
| 923 | |
Thomas Gleixner | 9744f7b | 2016-07-13 17:16:14 +0000 | [diff] [blame] | 924 | static void setup_APIC_ibs(void) |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 925 | { |
| 926 | int offset; |
| 927 | |
| 928 | offset = get_ibs_lvt_offset(); |
| 929 | if (offset < 0) |
| 930 | goto failed; |
| 931 | |
| 932 | if (!setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_NMI, 0)) |
| 933 | return; |
| 934 | failed: |
| 935 | pr_warn("perf: IBS APIC setup failed on cpu #%d\n", |
| 936 | smp_processor_id()); |
| 937 | } |
| 938 | |
Thomas Gleixner | 9744f7b | 2016-07-13 17:16:14 +0000 | [diff] [blame] | 939 | static void clear_APIC_ibs(void) |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 940 | { |
| 941 | int offset; |
| 942 | |
| 943 | offset = get_ibs_lvt_offset(); |
| 944 | if (offset >= 0) |
| 945 | setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_FIX, 1); |
| 946 | } |
| 947 | |
Thomas Gleixner | 9744f7b | 2016-07-13 17:16:14 +0000 | [diff] [blame] | 948 | static int x86_pmu_amd_ibs_starting_cpu(unsigned int cpu) |
| 949 | { |
| 950 | setup_APIC_ibs(); |
| 951 | return 0; |
| 952 | } |
| 953 | |
Robert Richter | bee09ed | 2014-01-15 15:57:29 +0100 | [diff] [blame] | 954 | #ifdef CONFIG_PM |
| 955 | |
| 956 | static int perf_ibs_suspend(void) |
| 957 | { |
Thomas Gleixner | 9744f7b | 2016-07-13 17:16:14 +0000 | [diff] [blame] | 958 | clear_APIC_ibs(); |
Robert Richter | bee09ed | 2014-01-15 15:57:29 +0100 | [diff] [blame] | 959 | return 0; |
| 960 | } |
| 961 | |
| 962 | static void perf_ibs_resume(void) |
| 963 | { |
| 964 | ibs_eilvt_setup(); |
Thomas Gleixner | 9744f7b | 2016-07-13 17:16:14 +0000 | [diff] [blame] | 965 | setup_APIC_ibs(); |
Robert Richter | bee09ed | 2014-01-15 15:57:29 +0100 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | static struct syscore_ops perf_ibs_syscore_ops = { |
| 969 | .resume = perf_ibs_resume, |
| 970 | .suspend = perf_ibs_suspend, |
| 971 | }; |
| 972 | |
| 973 | static void perf_ibs_pm_init(void) |
| 974 | { |
| 975 | register_syscore_ops(&perf_ibs_syscore_ops); |
| 976 | } |
| 977 | |
| 978 | #else |
| 979 | |
| 980 | static inline void perf_ibs_pm_init(void) { } |
| 981 | |
| 982 | #endif |
| 983 | |
Thomas Gleixner | 9744f7b | 2016-07-13 17:16:14 +0000 | [diff] [blame] | 984 | static int x86_pmu_amd_ibs_dying_cpu(unsigned int cpu) |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 985 | { |
Thomas Gleixner | 9744f7b | 2016-07-13 17:16:14 +0000 | [diff] [blame] | 986 | clear_APIC_ibs(); |
| 987 | return 0; |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | static __init int amd_ibs_init(void) |
| 991 | { |
| 992 | u32 caps; |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 993 | |
| 994 | caps = __get_ibs_caps(); |
| 995 | if (!caps) |
| 996 | return -ENODEV; /* ibs not supported by the cpu */ |
| 997 | |
Robert Richter | bee09ed | 2014-01-15 15:57:29 +0100 | [diff] [blame] | 998 | ibs_eilvt_setup(); |
Robert Richter | 16e5294 | 2011-11-08 19:20:44 +0100 | [diff] [blame] | 999 | |
| 1000 | if (!ibs_eilvt_valid()) |
Thomas Gleixner | 9744f7b | 2016-07-13 17:16:14 +0000 | [diff] [blame] | 1001 | return -EINVAL; |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 1002 | |
Robert Richter | bee09ed | 2014-01-15 15:57:29 +0100 | [diff] [blame] | 1003 | perf_ibs_pm_init(); |
Thomas Gleixner | 9744f7b | 2016-07-13 17:16:14 +0000 | [diff] [blame] | 1004 | |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 1005 | ibs_caps = caps; |
| 1006 | /* make ibs_caps visible to other cpus: */ |
| 1007 | smp_mb(); |
Thomas Gleixner | 9744f7b | 2016-07-13 17:16:14 +0000 | [diff] [blame] | 1008 | /* |
| 1009 | * x86_pmu_amd_ibs_starting_cpu will be called from core on |
| 1010 | * all online cpus. |
| 1011 | */ |
| 1012 | cpuhp_setup_state(CPUHP_AP_PERF_X86_AMD_IBS_STARTING, |
Sedat Dilek | 7e164ce | 2016-12-26 11:05:11 +0100 | [diff] [blame] | 1013 | "perf/x86/amd/ibs:starting", |
Thomas Gleixner | 9744f7b | 2016-07-13 17:16:14 +0000 | [diff] [blame] | 1014 | x86_pmu_amd_ibs_starting_cpu, |
| 1015 | x86_pmu_amd_ibs_dying_cpu); |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 1016 | |
Thomas Gleixner | 9744f7b | 2016-07-13 17:16:14 +0000 | [diff] [blame] | 1017 | perf_event_ibs_init(); |
| 1018 | |
| 1019 | return 0; |
Robert Richter | b716916 | 2011-09-21 11:30:18 +0200 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | /* Since we need the pci subsystem to init ibs we can't do this earlier: */ |
| 1023 | device_initcall(amd_ibs_init); |