Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Performance event support - powerpc architecture code |
| 3 | * |
| 4 | * Copyright 2008-2009 Paul Mackerras, IBM Corporation. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/sched.h> |
| 13 | #include <linux/perf_event.h> |
| 14 | #include <linux/percpu.h> |
| 15 | #include <linux/hardirq.h> |
| 16 | #include <asm/reg.h> |
| 17 | #include <asm/pmc.h> |
| 18 | #include <asm/machdep.h> |
| 19 | #include <asm/firmware.h> |
| 20 | #include <asm/ptrace.h> |
| 21 | |
| 22 | struct cpu_hw_events { |
| 23 | int n_events; |
| 24 | int n_percpu; |
| 25 | int disabled; |
| 26 | int n_added; |
| 27 | int n_limited; |
| 28 | u8 pmcs_enabled; |
| 29 | struct perf_event *event[MAX_HWEVENTS]; |
| 30 | u64 events[MAX_HWEVENTS]; |
| 31 | unsigned int flags[MAX_HWEVENTS]; |
| 32 | unsigned long mmcr[3]; |
Paul Mackerras | a8f90e9 | 2009-09-22 09:48:08 +1000 | [diff] [blame] | 33 | struct perf_event *limited_counter[MAX_LIMITED_HWCOUNTERS]; |
| 34 | u8 limited_hwidx[MAX_LIMITED_HWCOUNTERS]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 35 | u64 alternatives[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES]; |
| 36 | unsigned long amasks[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES]; |
| 37 | unsigned long avalues[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES]; |
Lin Ming | 8e6d557 | 2010-05-08 20:28:41 +1000 | [diff] [blame] | 38 | |
| 39 | unsigned int group_flag; |
| 40 | int n_txn_start; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 41 | }; |
| 42 | DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events); |
| 43 | |
| 44 | struct power_pmu *ppmu; |
| 45 | |
| 46 | /* |
Ingo Molnar | 57c0c15 | 2009-09-21 12:20:38 +0200 | [diff] [blame] | 47 | * Normally, to ignore kernel events we set the FCS (freeze counters |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 48 | * in supervisor mode) bit in MMCR0, but if the kernel runs with the |
| 49 | * hypervisor bit set in the MSR, or if we are running on a processor |
| 50 | * where the hypervisor bit is forced to 1 (as on Apple G5 processors), |
| 51 | * then we need to use the FCHV bit to ignore kernel events. |
| 52 | */ |
| 53 | static unsigned int freeze_events_kernel = MMCR0_FCS; |
| 54 | |
| 55 | /* |
| 56 | * 32-bit doesn't have MMCRA but does have an MMCR2, |
| 57 | * and a few other names are different. |
| 58 | */ |
| 59 | #ifdef CONFIG_PPC32 |
| 60 | |
| 61 | #define MMCR0_FCHV 0 |
| 62 | #define MMCR0_PMCjCE MMCR0_PMCnCE |
| 63 | |
| 64 | #define SPRN_MMCRA SPRN_MMCR2 |
| 65 | #define MMCRA_SAMPLE_ENABLE 0 |
| 66 | |
| 67 | static inline unsigned long perf_ip_adjust(struct pt_regs *regs) |
| 68 | { |
| 69 | return 0; |
| 70 | } |
| 71 | static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp) { } |
| 72 | static inline u32 perf_get_misc_flags(struct pt_regs *regs) |
| 73 | { |
| 74 | return 0; |
| 75 | } |
Anton Blanchard | 75382aa | 2012-06-26 01:01:36 +0000 | [diff] [blame^] | 76 | static inline void perf_read_regs(struct pt_regs *regs) |
| 77 | { |
| 78 | regs->result = 0; |
| 79 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 80 | static inline int perf_intr_is_nmi(struct pt_regs *regs) |
| 81 | { |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | #endif /* CONFIG_PPC32 */ |
| 86 | |
| 87 | /* |
| 88 | * Things that are specific to 64-bit implementations. |
| 89 | */ |
| 90 | #ifdef CONFIG_PPC64 |
| 91 | |
| 92 | static inline unsigned long perf_ip_adjust(struct pt_regs *regs) |
| 93 | { |
| 94 | unsigned long mmcra = regs->dsisr; |
| 95 | |
| 96 | if ((mmcra & MMCRA_SAMPLE_ENABLE) && !(ppmu->flags & PPMU_ALT_SIPR)) { |
| 97 | unsigned long slot = (mmcra & MMCRA_SLOT) >> MMCRA_SLOT_SHIFT; |
| 98 | if (slot > 1) |
| 99 | return 4 * (slot - 1); |
| 100 | } |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * The user wants a data address recorded. |
| 106 | * If we're not doing instruction sampling, give them the SDAR |
| 107 | * (sampled data address). If we are doing instruction sampling, then |
| 108 | * only give them the SDAR if it corresponds to the instruction |
| 109 | * pointed to by SIAR; this is indicated by the [POWER6_]MMCRA_SDSYNC |
| 110 | * bit in MMCRA. |
| 111 | */ |
| 112 | static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp) |
| 113 | { |
| 114 | unsigned long mmcra = regs->dsisr; |
| 115 | unsigned long sdsync = (ppmu->flags & PPMU_ALT_SIPR) ? |
| 116 | POWER6_MMCRA_SDSYNC : MMCRA_SDSYNC; |
| 117 | |
| 118 | if (!(mmcra & MMCRA_SAMPLE_ENABLE) || (mmcra & sdsync)) |
| 119 | *addrp = mfspr(SPRN_SDAR); |
| 120 | } |
| 121 | |
Anton Blanchard | 68b30bb | 2012-06-26 01:00:13 +0000 | [diff] [blame] | 122 | static bool mmcra_sihv(unsigned long mmcra) |
| 123 | { |
| 124 | unsigned long sihv = MMCRA_SIHV; |
| 125 | |
| 126 | if (ppmu->flags & PPMU_ALT_SIPR) |
| 127 | sihv = POWER6_MMCRA_SIHV; |
| 128 | |
| 129 | return !!(mmcra & sihv); |
| 130 | } |
| 131 | |
| 132 | static bool mmcra_sipr(unsigned long mmcra) |
| 133 | { |
| 134 | unsigned long sipr = MMCRA_SIPR; |
| 135 | |
| 136 | if (ppmu->flags & PPMU_ALT_SIPR) |
| 137 | sipr = POWER6_MMCRA_SIPR; |
| 138 | |
| 139 | return !!(mmcra & sipr); |
| 140 | } |
| 141 | |
Benjamin Herrenschmidt | 1ce447b | 2012-03-26 20:47:34 +0000 | [diff] [blame] | 142 | static inline u32 perf_flags_from_msr(struct pt_regs *regs) |
| 143 | { |
| 144 | if (regs->msr & MSR_PR) |
| 145 | return PERF_RECORD_MISC_USER; |
| 146 | if ((regs->msr & MSR_HV) && freeze_events_kernel != MMCR0_FCHV) |
| 147 | return PERF_RECORD_MISC_HYPERVISOR; |
| 148 | return PERF_RECORD_MISC_KERNEL; |
| 149 | } |
| 150 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 151 | static inline u32 perf_get_misc_flags(struct pt_regs *regs) |
| 152 | { |
| 153 | unsigned long mmcra = regs->dsisr; |
Anton Blanchard | 75382aa | 2012-06-26 01:01:36 +0000 | [diff] [blame^] | 154 | unsigned long use_siar = regs->result; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 155 | |
Anton Blanchard | 75382aa | 2012-06-26 01:01:36 +0000 | [diff] [blame^] | 156 | if (!use_siar) |
Benjamin Herrenschmidt | 1ce447b | 2012-03-26 20:47:34 +0000 | [diff] [blame] | 157 | return perf_flags_from_msr(regs); |
| 158 | |
| 159 | /* |
| 160 | * If we don't have flags in MMCRA, rather than using |
| 161 | * the MSR, we intuit the flags from the address in |
| 162 | * SIAR which should give slightly more reliable |
| 163 | * results |
| 164 | */ |
| 165 | if (ppmu->flags & PPMU_NO_SIPR) { |
| 166 | unsigned long siar = mfspr(SPRN_SIAR); |
| 167 | if (siar >= PAGE_OFFSET) |
| 168 | return PERF_RECORD_MISC_KERNEL; |
| 169 | return PERF_RECORD_MISC_USER; |
| 170 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 171 | |
Michael Neuling | 7abb840 | 2009-10-14 19:32:15 +0000 | [diff] [blame] | 172 | /* PR has priority over HV, so order below is important */ |
Anton Blanchard | 68b30bb | 2012-06-26 01:00:13 +0000 | [diff] [blame] | 173 | if (mmcra_sipr(mmcra)) |
Michael Neuling | 7abb840 | 2009-10-14 19:32:15 +0000 | [diff] [blame] | 174 | return PERF_RECORD_MISC_USER; |
Anton Blanchard | 68b30bb | 2012-06-26 01:00:13 +0000 | [diff] [blame] | 175 | if (mmcra_sihv(mmcra) && (freeze_events_kernel != MMCR0_FCHV)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 176 | return PERF_RECORD_MISC_HYPERVISOR; |
Michael Neuling | 7abb840 | 2009-10-14 19:32:15 +0000 | [diff] [blame] | 177 | return PERF_RECORD_MISC_KERNEL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /* |
| 181 | * Overload regs->dsisr to store MMCRA so we only need to read it once |
| 182 | * on each interrupt. |
Anton Blanchard | 75382aa | 2012-06-26 01:01:36 +0000 | [diff] [blame^] | 183 | * Overload regs->result to specify whether we should use the MSR (result |
| 184 | * is zero) or the SIAR (result is non zero). |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 185 | */ |
| 186 | static inline void perf_read_regs(struct pt_regs *regs) |
| 187 | { |
Anton Blanchard | 75382aa | 2012-06-26 01:01:36 +0000 | [diff] [blame^] | 188 | unsigned long mmcra = mfspr(SPRN_MMCRA); |
| 189 | int marked = mmcra & MMCRA_SAMPLE_ENABLE; |
| 190 | int use_siar; |
| 191 | |
| 192 | if (TRAP(regs) != 0xf00) |
| 193 | use_siar = 0; |
| 194 | else if ((ppmu->flags & PPMU_NO_CONT_SAMPLING) && !marked) |
| 195 | use_siar = 0; |
| 196 | else |
| 197 | use_siar = 1; |
| 198 | |
| 199 | regs->dsisr = mmcra; |
| 200 | regs->result = use_siar; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /* |
| 204 | * If interrupts were soft-disabled when a PMU interrupt occurs, treat |
| 205 | * it as an NMI. |
| 206 | */ |
| 207 | static inline int perf_intr_is_nmi(struct pt_regs *regs) |
| 208 | { |
| 209 | return !regs->softe; |
| 210 | } |
| 211 | |
| 212 | #endif /* CONFIG_PPC64 */ |
| 213 | |
| 214 | static void perf_event_interrupt(struct pt_regs *regs); |
| 215 | |
| 216 | void perf_event_print_debug(void) |
| 217 | { |
| 218 | } |
| 219 | |
| 220 | /* |
Ingo Molnar | 57c0c15 | 2009-09-21 12:20:38 +0200 | [diff] [blame] | 221 | * Read one performance monitor counter (PMC). |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 222 | */ |
| 223 | static unsigned long read_pmc(int idx) |
| 224 | { |
| 225 | unsigned long val; |
| 226 | |
| 227 | switch (idx) { |
| 228 | case 1: |
| 229 | val = mfspr(SPRN_PMC1); |
| 230 | break; |
| 231 | case 2: |
| 232 | val = mfspr(SPRN_PMC2); |
| 233 | break; |
| 234 | case 3: |
| 235 | val = mfspr(SPRN_PMC3); |
| 236 | break; |
| 237 | case 4: |
| 238 | val = mfspr(SPRN_PMC4); |
| 239 | break; |
| 240 | case 5: |
| 241 | val = mfspr(SPRN_PMC5); |
| 242 | break; |
| 243 | case 6: |
| 244 | val = mfspr(SPRN_PMC6); |
| 245 | break; |
| 246 | #ifdef CONFIG_PPC64 |
| 247 | case 7: |
| 248 | val = mfspr(SPRN_PMC7); |
| 249 | break; |
| 250 | case 8: |
| 251 | val = mfspr(SPRN_PMC8); |
| 252 | break; |
| 253 | #endif /* CONFIG_PPC64 */ |
| 254 | default: |
| 255 | printk(KERN_ERR "oops trying to read PMC%d\n", idx); |
| 256 | val = 0; |
| 257 | } |
| 258 | return val; |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * Write one PMC. |
| 263 | */ |
| 264 | static void write_pmc(int idx, unsigned long val) |
| 265 | { |
| 266 | switch (idx) { |
| 267 | case 1: |
| 268 | mtspr(SPRN_PMC1, val); |
| 269 | break; |
| 270 | case 2: |
| 271 | mtspr(SPRN_PMC2, val); |
| 272 | break; |
| 273 | case 3: |
| 274 | mtspr(SPRN_PMC3, val); |
| 275 | break; |
| 276 | case 4: |
| 277 | mtspr(SPRN_PMC4, val); |
| 278 | break; |
| 279 | case 5: |
| 280 | mtspr(SPRN_PMC5, val); |
| 281 | break; |
| 282 | case 6: |
| 283 | mtspr(SPRN_PMC6, val); |
| 284 | break; |
| 285 | #ifdef CONFIG_PPC64 |
| 286 | case 7: |
| 287 | mtspr(SPRN_PMC7, val); |
| 288 | break; |
| 289 | case 8: |
| 290 | mtspr(SPRN_PMC8, val); |
| 291 | break; |
| 292 | #endif /* CONFIG_PPC64 */ |
| 293 | default: |
| 294 | printk(KERN_ERR "oops trying to write PMC%d\n", idx); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * Check if a set of events can all go on the PMU at once. |
| 300 | * If they can't, this will look at alternative codes for the events |
| 301 | * and see if any combination of alternative codes is feasible. |
| 302 | * The feasible set is returned in event_id[]. |
| 303 | */ |
| 304 | static int power_check_constraints(struct cpu_hw_events *cpuhw, |
| 305 | u64 event_id[], unsigned int cflags[], |
| 306 | int n_ev) |
| 307 | { |
| 308 | unsigned long mask, value, nv; |
| 309 | unsigned long smasks[MAX_HWEVENTS], svalues[MAX_HWEVENTS]; |
| 310 | int n_alt[MAX_HWEVENTS], choice[MAX_HWEVENTS]; |
| 311 | int i, j; |
| 312 | unsigned long addf = ppmu->add_fields; |
| 313 | unsigned long tadd = ppmu->test_adder; |
| 314 | |
Paul Mackerras | a8f90e9 | 2009-09-22 09:48:08 +1000 | [diff] [blame] | 315 | if (n_ev > ppmu->n_counter) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 316 | return -1; |
| 317 | |
| 318 | /* First see if the events will go on as-is */ |
| 319 | for (i = 0; i < n_ev; ++i) { |
| 320 | if ((cflags[i] & PPMU_LIMITED_PMC_REQD) |
| 321 | && !ppmu->limited_pmc_event(event_id[i])) { |
| 322 | ppmu->get_alternatives(event_id[i], cflags[i], |
| 323 | cpuhw->alternatives[i]); |
| 324 | event_id[i] = cpuhw->alternatives[i][0]; |
| 325 | } |
| 326 | if (ppmu->get_constraint(event_id[i], &cpuhw->amasks[i][0], |
| 327 | &cpuhw->avalues[i][0])) |
| 328 | return -1; |
| 329 | } |
| 330 | value = mask = 0; |
| 331 | for (i = 0; i < n_ev; ++i) { |
| 332 | nv = (value | cpuhw->avalues[i][0]) + |
| 333 | (value & cpuhw->avalues[i][0] & addf); |
| 334 | if ((((nv + tadd) ^ value) & mask) != 0 || |
| 335 | (((nv + tadd) ^ cpuhw->avalues[i][0]) & |
| 336 | cpuhw->amasks[i][0]) != 0) |
| 337 | break; |
| 338 | value = nv; |
| 339 | mask |= cpuhw->amasks[i][0]; |
| 340 | } |
| 341 | if (i == n_ev) |
| 342 | return 0; /* all OK */ |
| 343 | |
| 344 | /* doesn't work, gather alternatives... */ |
| 345 | if (!ppmu->get_alternatives) |
| 346 | return -1; |
| 347 | for (i = 0; i < n_ev; ++i) { |
| 348 | choice[i] = 0; |
| 349 | n_alt[i] = ppmu->get_alternatives(event_id[i], cflags[i], |
| 350 | cpuhw->alternatives[i]); |
| 351 | for (j = 1; j < n_alt[i]; ++j) |
| 352 | ppmu->get_constraint(cpuhw->alternatives[i][j], |
| 353 | &cpuhw->amasks[i][j], |
| 354 | &cpuhw->avalues[i][j]); |
| 355 | } |
| 356 | |
| 357 | /* enumerate all possibilities and see if any will work */ |
| 358 | i = 0; |
| 359 | j = -1; |
| 360 | value = mask = nv = 0; |
| 361 | while (i < n_ev) { |
| 362 | if (j >= 0) { |
| 363 | /* we're backtracking, restore context */ |
| 364 | value = svalues[i]; |
| 365 | mask = smasks[i]; |
| 366 | j = choice[i]; |
| 367 | } |
| 368 | /* |
| 369 | * See if any alternative k for event_id i, |
| 370 | * where k > j, will satisfy the constraints. |
| 371 | */ |
| 372 | while (++j < n_alt[i]) { |
| 373 | nv = (value | cpuhw->avalues[i][j]) + |
| 374 | (value & cpuhw->avalues[i][j] & addf); |
| 375 | if ((((nv + tadd) ^ value) & mask) == 0 && |
| 376 | (((nv + tadd) ^ cpuhw->avalues[i][j]) |
| 377 | & cpuhw->amasks[i][j]) == 0) |
| 378 | break; |
| 379 | } |
| 380 | if (j >= n_alt[i]) { |
| 381 | /* |
| 382 | * No feasible alternative, backtrack |
| 383 | * to event_id i-1 and continue enumerating its |
| 384 | * alternatives from where we got up to. |
| 385 | */ |
| 386 | if (--i < 0) |
| 387 | return -1; |
| 388 | } else { |
| 389 | /* |
| 390 | * Found a feasible alternative for event_id i, |
| 391 | * remember where we got up to with this event_id, |
| 392 | * go on to the next event_id, and start with |
| 393 | * the first alternative for it. |
| 394 | */ |
| 395 | choice[i] = j; |
| 396 | svalues[i] = value; |
| 397 | smasks[i] = mask; |
| 398 | value = nv; |
| 399 | mask |= cpuhw->amasks[i][j]; |
| 400 | ++i; |
| 401 | j = -1; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | /* OK, we have a feasible combination, tell the caller the solution */ |
| 406 | for (i = 0; i < n_ev; ++i) |
| 407 | event_id[i] = cpuhw->alternatives[i][choice[i]]; |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | /* |
| 412 | * Check if newly-added events have consistent settings for |
| 413 | * exclude_{user,kernel,hv} with each other and any previously |
| 414 | * added events. |
| 415 | */ |
| 416 | static int check_excludes(struct perf_event **ctrs, unsigned int cflags[], |
| 417 | int n_prev, int n_new) |
| 418 | { |
| 419 | int eu = 0, ek = 0, eh = 0; |
| 420 | int i, n, first; |
| 421 | struct perf_event *event; |
| 422 | |
| 423 | n = n_prev + n_new; |
| 424 | if (n <= 1) |
| 425 | return 0; |
| 426 | |
| 427 | first = 1; |
| 428 | for (i = 0; i < n; ++i) { |
| 429 | if (cflags[i] & PPMU_LIMITED_PMC_OK) { |
| 430 | cflags[i] &= ~PPMU_LIMITED_PMC_REQD; |
| 431 | continue; |
| 432 | } |
| 433 | event = ctrs[i]; |
| 434 | if (first) { |
| 435 | eu = event->attr.exclude_user; |
| 436 | ek = event->attr.exclude_kernel; |
| 437 | eh = event->attr.exclude_hv; |
| 438 | first = 0; |
| 439 | } else if (event->attr.exclude_user != eu || |
| 440 | event->attr.exclude_kernel != ek || |
| 441 | event->attr.exclude_hv != eh) { |
| 442 | return -EAGAIN; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | if (eu || ek || eh) |
| 447 | for (i = 0; i < n; ++i) |
| 448 | if (cflags[i] & PPMU_LIMITED_PMC_OK) |
| 449 | cflags[i] |= PPMU_LIMITED_PMC_REQD; |
| 450 | |
| 451 | return 0; |
| 452 | } |
| 453 | |
Eric B Munson | 86c74ab | 2011-04-15 08:12:30 +0000 | [diff] [blame] | 454 | static u64 check_and_compute_delta(u64 prev, u64 val) |
| 455 | { |
| 456 | u64 delta = (val - prev) & 0xfffffffful; |
| 457 | |
| 458 | /* |
| 459 | * POWER7 can roll back counter values, if the new value is smaller |
| 460 | * than the previous value it will cause the delta and the counter to |
| 461 | * have bogus values unless we rolled a counter over. If a coutner is |
| 462 | * rolled back, it will be smaller, but within 256, which is the maximum |
| 463 | * number of events to rollback at once. If we dectect a rollback |
| 464 | * return 0. This can lead to a small lack of precision in the |
| 465 | * counters. |
| 466 | */ |
| 467 | if (prev > val && (prev - val) < 256) |
| 468 | delta = 0; |
| 469 | |
| 470 | return delta; |
| 471 | } |
| 472 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 473 | static void power_pmu_read(struct perf_event *event) |
| 474 | { |
| 475 | s64 val, delta, prev; |
| 476 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 477 | if (event->hw.state & PERF_HES_STOPPED) |
| 478 | return; |
| 479 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 480 | if (!event->hw.idx) |
| 481 | return; |
| 482 | /* |
| 483 | * Performance monitor interrupts come even when interrupts |
| 484 | * are soft-disabled, as long as interrupts are hard-enabled. |
| 485 | * Therefore we treat them like NMIs. |
| 486 | */ |
| 487 | do { |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 488 | prev = local64_read(&event->hw.prev_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 489 | barrier(); |
| 490 | val = read_pmc(event->hw.idx); |
Eric B Munson | 86c74ab | 2011-04-15 08:12:30 +0000 | [diff] [blame] | 491 | delta = check_and_compute_delta(prev, val); |
| 492 | if (!delta) |
| 493 | return; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 494 | } while (local64_cmpxchg(&event->hw.prev_count, prev, val) != prev); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 495 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 496 | local64_add(delta, &event->count); |
| 497 | local64_sub(delta, &event->hw.period_left); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | /* |
| 501 | * On some machines, PMC5 and PMC6 can't be written, don't respect |
| 502 | * the freeze conditions, and don't generate interrupts. This tells |
| 503 | * us if `event' is using such a PMC. |
| 504 | */ |
| 505 | static int is_limited_pmc(int pmcnum) |
| 506 | { |
| 507 | return (ppmu->flags & PPMU_LIMITED_PMC5_6) |
| 508 | && (pmcnum == 5 || pmcnum == 6); |
| 509 | } |
| 510 | |
Paul Mackerras | a8f90e9 | 2009-09-22 09:48:08 +1000 | [diff] [blame] | 511 | static void freeze_limited_counters(struct cpu_hw_events *cpuhw, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 512 | unsigned long pmc5, unsigned long pmc6) |
| 513 | { |
| 514 | struct perf_event *event; |
| 515 | u64 val, prev, delta; |
| 516 | int i; |
| 517 | |
| 518 | for (i = 0; i < cpuhw->n_limited; ++i) { |
Paul Mackerras | a8f90e9 | 2009-09-22 09:48:08 +1000 | [diff] [blame] | 519 | event = cpuhw->limited_counter[i]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 520 | if (!event->hw.idx) |
| 521 | continue; |
| 522 | val = (event->hw.idx == 5) ? pmc5 : pmc6; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 523 | prev = local64_read(&event->hw.prev_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 524 | event->hw.idx = 0; |
Eric B Munson | 86c74ab | 2011-04-15 08:12:30 +0000 | [diff] [blame] | 525 | delta = check_and_compute_delta(prev, val); |
| 526 | if (delta) |
| 527 | local64_add(delta, &event->count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
Paul Mackerras | a8f90e9 | 2009-09-22 09:48:08 +1000 | [diff] [blame] | 531 | static void thaw_limited_counters(struct cpu_hw_events *cpuhw, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 532 | unsigned long pmc5, unsigned long pmc6) |
| 533 | { |
| 534 | struct perf_event *event; |
Eric B Munson | 86c74ab | 2011-04-15 08:12:30 +0000 | [diff] [blame] | 535 | u64 val, prev; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 536 | int i; |
| 537 | |
| 538 | for (i = 0; i < cpuhw->n_limited; ++i) { |
Paul Mackerras | a8f90e9 | 2009-09-22 09:48:08 +1000 | [diff] [blame] | 539 | event = cpuhw->limited_counter[i]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 540 | event->hw.idx = cpuhw->limited_hwidx[i]; |
| 541 | val = (event->hw.idx == 5) ? pmc5 : pmc6; |
Eric B Munson | 86c74ab | 2011-04-15 08:12:30 +0000 | [diff] [blame] | 542 | prev = local64_read(&event->hw.prev_count); |
| 543 | if (check_and_compute_delta(prev, val)) |
| 544 | local64_set(&event->hw.prev_count, val); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 545 | perf_event_update_userpage(event); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | /* |
| 550 | * Since limited events don't respect the freeze conditions, we |
| 551 | * have to read them immediately after freezing or unfreezing the |
| 552 | * other events. We try to keep the values from the limited |
| 553 | * events as consistent as possible by keeping the delay (in |
| 554 | * cycles and instructions) between freezing/unfreezing and reading |
| 555 | * the limited events as small and consistent as possible. |
| 556 | * Therefore, if any limited events are in use, we read them |
| 557 | * both, and always in the same order, to minimize variability, |
| 558 | * and do it inside the same asm that writes MMCR0. |
| 559 | */ |
| 560 | static void write_mmcr0(struct cpu_hw_events *cpuhw, unsigned long mmcr0) |
| 561 | { |
| 562 | unsigned long pmc5, pmc6; |
| 563 | |
| 564 | if (!cpuhw->n_limited) { |
| 565 | mtspr(SPRN_MMCR0, mmcr0); |
| 566 | return; |
| 567 | } |
| 568 | |
| 569 | /* |
| 570 | * Write MMCR0, then read PMC5 and PMC6 immediately. |
| 571 | * To ensure we don't get a performance monitor interrupt |
| 572 | * between writing MMCR0 and freezing/thawing the limited |
| 573 | * events, we first write MMCR0 with the event overflow |
| 574 | * interrupt enable bits turned off. |
| 575 | */ |
| 576 | asm volatile("mtspr %3,%2; mfspr %0,%4; mfspr %1,%5" |
| 577 | : "=&r" (pmc5), "=&r" (pmc6) |
| 578 | : "r" (mmcr0 & ~(MMCR0_PMC1CE | MMCR0_PMCjCE)), |
| 579 | "i" (SPRN_MMCR0), |
| 580 | "i" (SPRN_PMC5), "i" (SPRN_PMC6)); |
| 581 | |
| 582 | if (mmcr0 & MMCR0_FC) |
Paul Mackerras | a8f90e9 | 2009-09-22 09:48:08 +1000 | [diff] [blame] | 583 | freeze_limited_counters(cpuhw, pmc5, pmc6); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 584 | else |
Paul Mackerras | a8f90e9 | 2009-09-22 09:48:08 +1000 | [diff] [blame] | 585 | thaw_limited_counters(cpuhw, pmc5, pmc6); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 586 | |
| 587 | /* |
| 588 | * Write the full MMCR0 including the event overflow interrupt |
| 589 | * enable bits, if necessary. |
| 590 | */ |
| 591 | if (mmcr0 & (MMCR0_PMC1CE | MMCR0_PMCjCE)) |
| 592 | mtspr(SPRN_MMCR0, mmcr0); |
| 593 | } |
| 594 | |
| 595 | /* |
| 596 | * Disable all events to prevent PMU interrupts and to allow |
| 597 | * events to be added or removed. |
| 598 | */ |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 599 | static void power_pmu_disable(struct pmu *pmu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 600 | { |
| 601 | struct cpu_hw_events *cpuhw; |
| 602 | unsigned long flags; |
| 603 | |
| 604 | if (!ppmu) |
| 605 | return; |
| 606 | local_irq_save(flags); |
| 607 | cpuhw = &__get_cpu_var(cpu_hw_events); |
| 608 | |
| 609 | if (!cpuhw->disabled) { |
| 610 | cpuhw->disabled = 1; |
| 611 | cpuhw->n_added = 0; |
| 612 | |
| 613 | /* |
| 614 | * Check if we ever enabled the PMU on this cpu. |
| 615 | */ |
| 616 | if (!cpuhw->pmcs_enabled) { |
| 617 | ppc_enable_pmcs(); |
| 618 | cpuhw->pmcs_enabled = 1; |
| 619 | } |
| 620 | |
| 621 | /* |
| 622 | * Disable instruction sampling if it was enabled |
| 623 | */ |
| 624 | if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) { |
| 625 | mtspr(SPRN_MMCRA, |
| 626 | cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE); |
| 627 | mb(); |
| 628 | } |
| 629 | |
| 630 | /* |
Ingo Molnar | 57c0c15 | 2009-09-21 12:20:38 +0200 | [diff] [blame] | 631 | * Set the 'freeze counters' bit. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 632 | * The barrier is to make sure the mtspr has been |
| 633 | * executed and the PMU has frozen the events |
| 634 | * before we return. |
| 635 | */ |
| 636 | write_mmcr0(cpuhw, mfspr(SPRN_MMCR0) | MMCR0_FC); |
| 637 | mb(); |
| 638 | } |
| 639 | local_irq_restore(flags); |
| 640 | } |
| 641 | |
| 642 | /* |
| 643 | * Re-enable all events if disable == 0. |
| 644 | * If we were previously disabled and events were added, then |
| 645 | * put the new config on the PMU. |
| 646 | */ |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 647 | static void power_pmu_enable(struct pmu *pmu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 648 | { |
| 649 | struct perf_event *event; |
| 650 | struct cpu_hw_events *cpuhw; |
| 651 | unsigned long flags; |
| 652 | long i; |
| 653 | unsigned long val; |
| 654 | s64 left; |
| 655 | unsigned int hwc_index[MAX_HWEVENTS]; |
| 656 | int n_lim; |
| 657 | int idx; |
| 658 | |
| 659 | if (!ppmu) |
| 660 | return; |
| 661 | local_irq_save(flags); |
| 662 | cpuhw = &__get_cpu_var(cpu_hw_events); |
| 663 | if (!cpuhw->disabled) { |
| 664 | local_irq_restore(flags); |
| 665 | return; |
| 666 | } |
| 667 | cpuhw->disabled = 0; |
| 668 | |
| 669 | /* |
| 670 | * If we didn't change anything, or only removed events, |
| 671 | * no need to recalculate MMCR* settings and reset the PMCs. |
| 672 | * Just reenable the PMU with the current MMCR* settings |
| 673 | * (possibly updated for removal of events). |
| 674 | */ |
| 675 | if (!cpuhw->n_added) { |
| 676 | mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE); |
| 677 | mtspr(SPRN_MMCR1, cpuhw->mmcr[1]); |
| 678 | if (cpuhw->n_events == 0) |
| 679 | ppc_set_pmu_inuse(0); |
| 680 | goto out_enable; |
| 681 | } |
| 682 | |
| 683 | /* |
| 684 | * Compute MMCR* values for the new set of events |
| 685 | */ |
| 686 | if (ppmu->compute_mmcr(cpuhw->events, cpuhw->n_events, hwc_index, |
| 687 | cpuhw->mmcr)) { |
| 688 | /* shouldn't ever get here */ |
| 689 | printk(KERN_ERR "oops compute_mmcr failed\n"); |
| 690 | goto out; |
| 691 | } |
| 692 | |
| 693 | /* |
| 694 | * Add in MMCR0 freeze bits corresponding to the |
| 695 | * attr.exclude_* bits for the first event. |
| 696 | * We have already checked that all events have the |
| 697 | * same values for these bits as the first event. |
| 698 | */ |
| 699 | event = cpuhw->event[0]; |
| 700 | if (event->attr.exclude_user) |
| 701 | cpuhw->mmcr[0] |= MMCR0_FCP; |
| 702 | if (event->attr.exclude_kernel) |
| 703 | cpuhw->mmcr[0] |= freeze_events_kernel; |
| 704 | if (event->attr.exclude_hv) |
| 705 | cpuhw->mmcr[0] |= MMCR0_FCHV; |
| 706 | |
| 707 | /* |
| 708 | * Write the new configuration to MMCR* with the freeze |
| 709 | * bit set and set the hardware events to their initial values. |
| 710 | * Then unfreeze the events. |
| 711 | */ |
| 712 | ppc_set_pmu_inuse(1); |
| 713 | mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE); |
| 714 | mtspr(SPRN_MMCR1, cpuhw->mmcr[1]); |
| 715 | mtspr(SPRN_MMCR0, (cpuhw->mmcr[0] & ~(MMCR0_PMC1CE | MMCR0_PMCjCE)) |
| 716 | | MMCR0_FC); |
| 717 | |
| 718 | /* |
| 719 | * Read off any pre-existing events that need to move |
| 720 | * to another PMC. |
| 721 | */ |
| 722 | for (i = 0; i < cpuhw->n_events; ++i) { |
| 723 | event = cpuhw->event[i]; |
| 724 | if (event->hw.idx && event->hw.idx != hwc_index[i] + 1) { |
| 725 | power_pmu_read(event); |
| 726 | write_pmc(event->hw.idx, 0); |
| 727 | event->hw.idx = 0; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | /* |
| 732 | * Initialize the PMCs for all the new and moved events. |
| 733 | */ |
| 734 | cpuhw->n_limited = n_lim = 0; |
| 735 | for (i = 0; i < cpuhw->n_events; ++i) { |
| 736 | event = cpuhw->event[i]; |
| 737 | if (event->hw.idx) |
| 738 | continue; |
| 739 | idx = hwc_index[i] + 1; |
| 740 | if (is_limited_pmc(idx)) { |
Paul Mackerras | a8f90e9 | 2009-09-22 09:48:08 +1000 | [diff] [blame] | 741 | cpuhw->limited_counter[n_lim] = event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 742 | cpuhw->limited_hwidx[n_lim] = idx; |
| 743 | ++n_lim; |
| 744 | continue; |
| 745 | } |
| 746 | val = 0; |
| 747 | if (event->hw.sample_period) { |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 748 | left = local64_read(&event->hw.period_left); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 749 | if (left < 0x80000000L) |
| 750 | val = 0x80000000L - left; |
| 751 | } |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 752 | local64_set(&event->hw.prev_count, val); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 753 | event->hw.idx = idx; |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 754 | if (event->hw.state & PERF_HES_STOPPED) |
| 755 | val = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 756 | write_pmc(idx, val); |
| 757 | perf_event_update_userpage(event); |
| 758 | } |
| 759 | cpuhw->n_limited = n_lim; |
| 760 | cpuhw->mmcr[0] |= MMCR0_PMXE | MMCR0_FCECE; |
| 761 | |
| 762 | out_enable: |
| 763 | mb(); |
| 764 | write_mmcr0(cpuhw, cpuhw->mmcr[0]); |
| 765 | |
| 766 | /* |
| 767 | * Enable instruction sampling if necessary |
| 768 | */ |
| 769 | if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) { |
| 770 | mb(); |
| 771 | mtspr(SPRN_MMCRA, cpuhw->mmcr[2]); |
| 772 | } |
| 773 | |
| 774 | out: |
| 775 | local_irq_restore(flags); |
| 776 | } |
| 777 | |
| 778 | static int collect_events(struct perf_event *group, int max_count, |
| 779 | struct perf_event *ctrs[], u64 *events, |
| 780 | unsigned int *flags) |
| 781 | { |
| 782 | int n = 0; |
| 783 | struct perf_event *event; |
| 784 | |
| 785 | if (!is_software_event(group)) { |
| 786 | if (n >= max_count) |
| 787 | return -1; |
| 788 | ctrs[n] = group; |
| 789 | flags[n] = group->hw.event_base; |
| 790 | events[n++] = group->hw.config; |
| 791 | } |
Paul Mackerras | a8f90e9 | 2009-09-22 09:48:08 +1000 | [diff] [blame] | 792 | list_for_each_entry(event, &group->sibling_list, group_entry) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 793 | if (!is_software_event(event) && |
| 794 | event->state != PERF_EVENT_STATE_OFF) { |
| 795 | if (n >= max_count) |
| 796 | return -1; |
| 797 | ctrs[n] = event; |
| 798 | flags[n] = event->hw.event_base; |
| 799 | events[n++] = event->hw.config; |
| 800 | } |
| 801 | } |
| 802 | return n; |
| 803 | } |
| 804 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 805 | /* |
| 806 | * Add a event to the PMU. |
| 807 | * If all events are not already frozen, then we disable and |
| 808 | * re-enable the PMU in order to get hw_perf_enable to do the |
| 809 | * actual work of reconfiguring the PMU. |
| 810 | */ |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 811 | static int power_pmu_add(struct perf_event *event, int ef_flags) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 812 | { |
| 813 | struct cpu_hw_events *cpuhw; |
| 814 | unsigned long flags; |
| 815 | int n0; |
| 816 | int ret = -EAGAIN; |
| 817 | |
| 818 | local_irq_save(flags); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 819 | perf_pmu_disable(event->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 820 | |
| 821 | /* |
| 822 | * Add the event to the list (if there is room) |
| 823 | * and check whether the total set is still feasible. |
| 824 | */ |
| 825 | cpuhw = &__get_cpu_var(cpu_hw_events); |
| 826 | n0 = cpuhw->n_events; |
Paul Mackerras | a8f90e9 | 2009-09-22 09:48:08 +1000 | [diff] [blame] | 827 | if (n0 >= ppmu->n_counter) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 828 | goto out; |
| 829 | cpuhw->event[n0] = event; |
| 830 | cpuhw->events[n0] = event->hw.config; |
| 831 | cpuhw->flags[n0] = event->hw.event_base; |
Lin Ming | 8e6d557 | 2010-05-08 20:28:41 +1000 | [diff] [blame] | 832 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 833 | if (!(ef_flags & PERF_EF_START)) |
| 834 | event->hw.state = PERF_HES_STOPPED | PERF_HES_UPTODATE; |
| 835 | |
Lin Ming | 8e6d557 | 2010-05-08 20:28:41 +1000 | [diff] [blame] | 836 | /* |
| 837 | * If group events scheduling transaction was started, |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 838 | * skip the schedulability test here, it will be performed |
Lin Ming | 8e6d557 | 2010-05-08 20:28:41 +1000 | [diff] [blame] | 839 | * at commit time(->commit_txn) as a whole |
| 840 | */ |
Peter Zijlstra | 8d2cacb | 2010-05-25 17:49:05 +0200 | [diff] [blame] | 841 | if (cpuhw->group_flag & PERF_EVENT_TXN) |
Lin Ming | 8e6d557 | 2010-05-08 20:28:41 +1000 | [diff] [blame] | 842 | goto nocheck; |
| 843 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 844 | if (check_excludes(cpuhw->event, cpuhw->flags, n0, 1)) |
| 845 | goto out; |
| 846 | if (power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n0 + 1)) |
| 847 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 848 | event->hw.config = cpuhw->events[n0]; |
Lin Ming | 8e6d557 | 2010-05-08 20:28:41 +1000 | [diff] [blame] | 849 | |
| 850 | nocheck: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 851 | ++cpuhw->n_events; |
| 852 | ++cpuhw->n_added; |
| 853 | |
| 854 | ret = 0; |
| 855 | out: |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 856 | perf_pmu_enable(event->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 857 | local_irq_restore(flags); |
| 858 | return ret; |
| 859 | } |
| 860 | |
| 861 | /* |
| 862 | * Remove a event from the PMU. |
| 863 | */ |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 864 | static void power_pmu_del(struct perf_event *event, int ef_flags) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 865 | { |
| 866 | struct cpu_hw_events *cpuhw; |
| 867 | long i; |
| 868 | unsigned long flags; |
| 869 | |
| 870 | local_irq_save(flags); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 871 | perf_pmu_disable(event->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 872 | |
| 873 | power_pmu_read(event); |
| 874 | |
| 875 | cpuhw = &__get_cpu_var(cpu_hw_events); |
| 876 | for (i = 0; i < cpuhw->n_events; ++i) { |
| 877 | if (event == cpuhw->event[i]) { |
Matt Evans | 219a92a | 2010-07-05 17:36:32 +0000 | [diff] [blame] | 878 | while (++i < cpuhw->n_events) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 879 | cpuhw->event[i-1] = cpuhw->event[i]; |
Matt Evans | 219a92a | 2010-07-05 17:36:32 +0000 | [diff] [blame] | 880 | cpuhw->events[i-1] = cpuhw->events[i]; |
| 881 | cpuhw->flags[i-1] = cpuhw->flags[i]; |
| 882 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 883 | --cpuhw->n_events; |
| 884 | ppmu->disable_pmc(event->hw.idx - 1, cpuhw->mmcr); |
| 885 | if (event->hw.idx) { |
| 886 | write_pmc(event->hw.idx, 0); |
| 887 | event->hw.idx = 0; |
| 888 | } |
| 889 | perf_event_update_userpage(event); |
| 890 | break; |
| 891 | } |
| 892 | } |
| 893 | for (i = 0; i < cpuhw->n_limited; ++i) |
Paul Mackerras | a8f90e9 | 2009-09-22 09:48:08 +1000 | [diff] [blame] | 894 | if (event == cpuhw->limited_counter[i]) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 895 | break; |
| 896 | if (i < cpuhw->n_limited) { |
| 897 | while (++i < cpuhw->n_limited) { |
Paul Mackerras | a8f90e9 | 2009-09-22 09:48:08 +1000 | [diff] [blame] | 898 | cpuhw->limited_counter[i-1] = cpuhw->limited_counter[i]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 899 | cpuhw->limited_hwidx[i-1] = cpuhw->limited_hwidx[i]; |
| 900 | } |
| 901 | --cpuhw->n_limited; |
| 902 | } |
| 903 | if (cpuhw->n_events == 0) { |
| 904 | /* disable exceptions if no events are running */ |
| 905 | cpuhw->mmcr[0] &= ~(MMCR0_PMXE | MMCR0_FCECE); |
| 906 | } |
| 907 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 908 | perf_pmu_enable(event->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 909 | local_irq_restore(flags); |
| 910 | } |
| 911 | |
| 912 | /* |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 913 | * POWER-PMU does not support disabling individual counters, hence |
| 914 | * program their cycle counter to their max value and ignore the interrupts. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 915 | */ |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 916 | |
| 917 | static void power_pmu_start(struct perf_event *event, int ef_flags) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 918 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 919 | unsigned long flags; |
| 920 | s64 left; |
Anton Blanchard | 9a45a94 | 2012-02-15 18:48:22 +0000 | [diff] [blame] | 921 | unsigned long val; |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 922 | |
| 923 | if (!event->hw.idx || !event->hw.sample_period) |
| 924 | return; |
| 925 | |
| 926 | if (!(event->hw.state & PERF_HES_STOPPED)) |
| 927 | return; |
| 928 | |
| 929 | if (ef_flags & PERF_EF_RELOAD) |
| 930 | WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE)); |
| 931 | |
| 932 | local_irq_save(flags); |
| 933 | perf_pmu_disable(event->pmu); |
| 934 | |
| 935 | event->hw.state = 0; |
| 936 | left = local64_read(&event->hw.period_left); |
Anton Blanchard | 9a45a94 | 2012-02-15 18:48:22 +0000 | [diff] [blame] | 937 | |
| 938 | val = 0; |
| 939 | if (left < 0x80000000L) |
| 940 | val = 0x80000000L - left; |
| 941 | |
| 942 | write_pmc(event->hw.idx, val); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 943 | |
| 944 | perf_event_update_userpage(event); |
| 945 | perf_pmu_enable(event->pmu); |
| 946 | local_irq_restore(flags); |
| 947 | } |
| 948 | |
| 949 | static void power_pmu_stop(struct perf_event *event, int ef_flags) |
| 950 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 951 | unsigned long flags; |
| 952 | |
| 953 | if (!event->hw.idx || !event->hw.sample_period) |
| 954 | return; |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 955 | |
| 956 | if (event->hw.state & PERF_HES_STOPPED) |
| 957 | return; |
| 958 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 959 | local_irq_save(flags); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 960 | perf_pmu_disable(event->pmu); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 961 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 962 | power_pmu_read(event); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 963 | event->hw.state |= PERF_HES_STOPPED | PERF_HES_UPTODATE; |
| 964 | write_pmc(event->hw.idx, 0); |
| 965 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 966 | perf_event_update_userpage(event); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 967 | perf_pmu_enable(event->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 968 | local_irq_restore(flags); |
| 969 | } |
| 970 | |
Lin Ming | 8e6d557 | 2010-05-08 20:28:41 +1000 | [diff] [blame] | 971 | /* |
| 972 | * Start group events scheduling transaction |
| 973 | * Set the flag to make pmu::enable() not perform the |
| 974 | * schedulability test, it will be performed at commit time |
| 975 | */ |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 976 | void power_pmu_start_txn(struct pmu *pmu) |
Lin Ming | 8e6d557 | 2010-05-08 20:28:41 +1000 | [diff] [blame] | 977 | { |
| 978 | struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events); |
| 979 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 980 | perf_pmu_disable(pmu); |
Peter Zijlstra | 8d2cacb | 2010-05-25 17:49:05 +0200 | [diff] [blame] | 981 | cpuhw->group_flag |= PERF_EVENT_TXN; |
Lin Ming | 8e6d557 | 2010-05-08 20:28:41 +1000 | [diff] [blame] | 982 | cpuhw->n_txn_start = cpuhw->n_events; |
| 983 | } |
| 984 | |
| 985 | /* |
| 986 | * Stop group events scheduling transaction |
| 987 | * Clear the flag and pmu::enable() will perform the |
| 988 | * schedulability test. |
| 989 | */ |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 990 | void power_pmu_cancel_txn(struct pmu *pmu) |
Lin Ming | 8e6d557 | 2010-05-08 20:28:41 +1000 | [diff] [blame] | 991 | { |
| 992 | struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events); |
| 993 | |
Peter Zijlstra | 8d2cacb | 2010-05-25 17:49:05 +0200 | [diff] [blame] | 994 | cpuhw->group_flag &= ~PERF_EVENT_TXN; |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 995 | perf_pmu_enable(pmu); |
Lin Ming | 8e6d557 | 2010-05-08 20:28:41 +1000 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | /* |
| 999 | * Commit group events scheduling transaction |
| 1000 | * Perform the group schedulability test as a whole |
| 1001 | * Return 0 if success |
| 1002 | */ |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 1003 | int power_pmu_commit_txn(struct pmu *pmu) |
Lin Ming | 8e6d557 | 2010-05-08 20:28:41 +1000 | [diff] [blame] | 1004 | { |
| 1005 | struct cpu_hw_events *cpuhw; |
| 1006 | long i, n; |
| 1007 | |
| 1008 | if (!ppmu) |
| 1009 | return -EAGAIN; |
| 1010 | cpuhw = &__get_cpu_var(cpu_hw_events); |
| 1011 | n = cpuhw->n_events; |
| 1012 | if (check_excludes(cpuhw->event, cpuhw->flags, 0, n)) |
| 1013 | return -EAGAIN; |
| 1014 | i = power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n); |
| 1015 | if (i < 0) |
| 1016 | return -EAGAIN; |
| 1017 | |
| 1018 | for (i = cpuhw->n_txn_start; i < n; ++i) |
| 1019 | cpuhw->event[i]->hw.config = cpuhw->events[i]; |
| 1020 | |
Peter Zijlstra | 8d2cacb | 2010-05-25 17:49:05 +0200 | [diff] [blame] | 1021 | cpuhw->group_flag &= ~PERF_EVENT_TXN; |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 1022 | perf_pmu_enable(pmu); |
Lin Ming | 8e6d557 | 2010-05-08 20:28:41 +1000 | [diff] [blame] | 1023 | return 0; |
| 1024 | } |
| 1025 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1026 | /* |
| 1027 | * Return 1 if we might be able to put event on a limited PMC, |
| 1028 | * or 0 if not. |
| 1029 | * A event can only go on a limited PMC if it counts something |
| 1030 | * that a limited PMC can count, doesn't require interrupts, and |
| 1031 | * doesn't exclude any processor mode. |
| 1032 | */ |
| 1033 | static int can_go_on_limited_pmc(struct perf_event *event, u64 ev, |
| 1034 | unsigned int flags) |
| 1035 | { |
| 1036 | int n; |
| 1037 | u64 alt[MAX_EVENT_ALTERNATIVES]; |
| 1038 | |
| 1039 | if (event->attr.exclude_user |
| 1040 | || event->attr.exclude_kernel |
| 1041 | || event->attr.exclude_hv |
| 1042 | || event->attr.sample_period) |
| 1043 | return 0; |
| 1044 | |
| 1045 | if (ppmu->limited_pmc_event(ev)) |
| 1046 | return 1; |
| 1047 | |
| 1048 | /* |
| 1049 | * The requested event_id isn't on a limited PMC already; |
| 1050 | * see if any alternative code goes on a limited PMC. |
| 1051 | */ |
| 1052 | if (!ppmu->get_alternatives) |
| 1053 | return 0; |
| 1054 | |
| 1055 | flags |= PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD; |
| 1056 | n = ppmu->get_alternatives(ev, flags, alt); |
| 1057 | |
| 1058 | return n > 0; |
| 1059 | } |
| 1060 | |
| 1061 | /* |
| 1062 | * Find an alternative event_id that goes on a normal PMC, if possible, |
| 1063 | * and return the event_id code, or 0 if there is no such alternative. |
| 1064 | * (Note: event_id code 0 is "don't count" on all machines.) |
| 1065 | */ |
| 1066 | static u64 normal_pmc_alternative(u64 ev, unsigned long flags) |
| 1067 | { |
| 1068 | u64 alt[MAX_EVENT_ALTERNATIVES]; |
| 1069 | int n; |
| 1070 | |
| 1071 | flags &= ~(PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD); |
| 1072 | n = ppmu->get_alternatives(ev, flags, alt); |
| 1073 | if (!n) |
| 1074 | return 0; |
| 1075 | return alt[0]; |
| 1076 | } |
| 1077 | |
| 1078 | /* Number of perf_events counting hardware events */ |
| 1079 | static atomic_t num_events; |
| 1080 | /* Used to avoid races in calling reserve/release_pmc_hardware */ |
| 1081 | static DEFINE_MUTEX(pmc_reserve_mutex); |
| 1082 | |
| 1083 | /* |
| 1084 | * Release the PMU if this is the last perf_event. |
| 1085 | */ |
| 1086 | static void hw_perf_event_destroy(struct perf_event *event) |
| 1087 | { |
| 1088 | if (!atomic_add_unless(&num_events, -1, 1)) { |
| 1089 | mutex_lock(&pmc_reserve_mutex); |
| 1090 | if (atomic_dec_return(&num_events) == 0) |
| 1091 | release_pmc_hardware(); |
| 1092 | mutex_unlock(&pmc_reserve_mutex); |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | /* |
| 1097 | * Translate a generic cache event_id config to a raw event_id code. |
| 1098 | */ |
| 1099 | static int hw_perf_cache_event(u64 config, u64 *eventp) |
| 1100 | { |
| 1101 | unsigned long type, op, result; |
| 1102 | int ev; |
| 1103 | |
| 1104 | if (!ppmu->cache_events) |
| 1105 | return -EINVAL; |
| 1106 | |
| 1107 | /* unpack config */ |
| 1108 | type = config & 0xff; |
| 1109 | op = (config >> 8) & 0xff; |
| 1110 | result = (config >> 16) & 0xff; |
| 1111 | |
| 1112 | if (type >= PERF_COUNT_HW_CACHE_MAX || |
| 1113 | op >= PERF_COUNT_HW_CACHE_OP_MAX || |
| 1114 | result >= PERF_COUNT_HW_CACHE_RESULT_MAX) |
| 1115 | return -EINVAL; |
| 1116 | |
| 1117 | ev = (*ppmu->cache_events)[type][op][result]; |
| 1118 | if (ev == 0) |
| 1119 | return -EOPNOTSUPP; |
| 1120 | if (ev == -1) |
| 1121 | return -EINVAL; |
| 1122 | *eventp = ev; |
| 1123 | return 0; |
| 1124 | } |
| 1125 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1126 | static int power_pmu_event_init(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1127 | { |
| 1128 | u64 ev; |
| 1129 | unsigned long flags; |
| 1130 | struct perf_event *ctrs[MAX_HWEVENTS]; |
| 1131 | u64 events[MAX_HWEVENTS]; |
| 1132 | unsigned int cflags[MAX_HWEVENTS]; |
| 1133 | int n; |
| 1134 | int err; |
| 1135 | struct cpu_hw_events *cpuhw; |
| 1136 | |
| 1137 | if (!ppmu) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1138 | return -ENOENT; |
| 1139 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 1140 | /* does not support taken branch sampling */ |
| 1141 | if (has_branch_stack(event)) |
| 1142 | return -EOPNOTSUPP; |
| 1143 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1144 | switch (event->attr.type) { |
| 1145 | case PERF_TYPE_HARDWARE: |
| 1146 | ev = event->attr.config; |
| 1147 | if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1148 | return -EOPNOTSUPP; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1149 | ev = ppmu->generic_events[ev]; |
| 1150 | break; |
| 1151 | case PERF_TYPE_HW_CACHE: |
| 1152 | err = hw_perf_cache_event(event->attr.config, &ev); |
| 1153 | if (err) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1154 | return err; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1155 | break; |
| 1156 | case PERF_TYPE_RAW: |
| 1157 | ev = event->attr.config; |
| 1158 | break; |
| 1159 | default: |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1160 | return -ENOENT; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1161 | } |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1162 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1163 | event->hw.config_base = ev; |
| 1164 | event->hw.idx = 0; |
| 1165 | |
| 1166 | /* |
| 1167 | * If we are not running on a hypervisor, force the |
| 1168 | * exclude_hv bit to 0 so that we don't care what |
| 1169 | * the user set it to. |
| 1170 | */ |
| 1171 | if (!firmware_has_feature(FW_FEATURE_LPAR)) |
| 1172 | event->attr.exclude_hv = 0; |
| 1173 | |
| 1174 | /* |
| 1175 | * If this is a per-task event, then we can use |
| 1176 | * PM_RUN_* events interchangeably with their non RUN_* |
| 1177 | * equivalents, e.g. PM_RUN_CYC instead of PM_CYC. |
| 1178 | * XXX we should check if the task is an idle task. |
| 1179 | */ |
| 1180 | flags = 0; |
Paul Mackerras | 57fa721 | 2010-10-19 16:55:35 +1100 | [diff] [blame] | 1181 | if (event->attach_state & PERF_ATTACH_TASK) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1182 | flags |= PPMU_ONLY_COUNT_RUN; |
| 1183 | |
| 1184 | /* |
| 1185 | * If this machine has limited events, check whether this |
| 1186 | * event_id could go on a limited event. |
| 1187 | */ |
| 1188 | if (ppmu->flags & PPMU_LIMITED_PMC5_6) { |
| 1189 | if (can_go_on_limited_pmc(event, ev, flags)) { |
| 1190 | flags |= PPMU_LIMITED_PMC_OK; |
| 1191 | } else if (ppmu->limited_pmc_event(ev)) { |
| 1192 | /* |
| 1193 | * The requested event_id is on a limited PMC, |
| 1194 | * but we can't use a limited PMC; see if any |
| 1195 | * alternative goes on a normal PMC. |
| 1196 | */ |
| 1197 | ev = normal_pmc_alternative(ev, flags); |
| 1198 | if (!ev) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1199 | return -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | /* |
| 1204 | * If this is in a group, check if it can go on with all the |
| 1205 | * other hardware events in the group. We assume the event |
| 1206 | * hasn't been linked into its leader's sibling list at this point. |
| 1207 | */ |
| 1208 | n = 0; |
| 1209 | if (event->group_leader != event) { |
Paul Mackerras | a8f90e9 | 2009-09-22 09:48:08 +1000 | [diff] [blame] | 1210 | n = collect_events(event->group_leader, ppmu->n_counter - 1, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1211 | ctrs, events, cflags); |
| 1212 | if (n < 0) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1213 | return -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1214 | } |
| 1215 | events[n] = ev; |
| 1216 | ctrs[n] = event; |
| 1217 | cflags[n] = flags; |
| 1218 | if (check_excludes(ctrs, cflags, n, 1)) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1219 | return -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1220 | |
| 1221 | cpuhw = &get_cpu_var(cpu_hw_events); |
| 1222 | err = power_check_constraints(cpuhw, events, cflags, n + 1); |
| 1223 | put_cpu_var(cpu_hw_events); |
| 1224 | if (err) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1225 | return -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1226 | |
| 1227 | event->hw.config = events[n]; |
| 1228 | event->hw.event_base = cflags[n]; |
| 1229 | event->hw.last_period = event->hw.sample_period; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 1230 | local64_set(&event->hw.period_left, event->hw.last_period); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1231 | |
| 1232 | /* |
| 1233 | * See if we need to reserve the PMU. |
| 1234 | * If no events are currently in use, then we have to take a |
| 1235 | * mutex to ensure that we don't race with another task doing |
| 1236 | * reserve_pmc_hardware or release_pmc_hardware. |
| 1237 | */ |
| 1238 | err = 0; |
| 1239 | if (!atomic_inc_not_zero(&num_events)) { |
| 1240 | mutex_lock(&pmc_reserve_mutex); |
| 1241 | if (atomic_read(&num_events) == 0 && |
| 1242 | reserve_pmc_hardware(perf_event_interrupt)) |
| 1243 | err = -EBUSY; |
| 1244 | else |
| 1245 | atomic_inc(&num_events); |
| 1246 | mutex_unlock(&pmc_reserve_mutex); |
| 1247 | } |
| 1248 | event->destroy = hw_perf_event_destroy; |
| 1249 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1250 | return err; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1251 | } |
| 1252 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 1253 | static int power_pmu_event_idx(struct perf_event *event) |
| 1254 | { |
| 1255 | return event->hw.idx; |
| 1256 | } |
| 1257 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1258 | struct pmu power_pmu = { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1259 | .pmu_enable = power_pmu_enable, |
| 1260 | .pmu_disable = power_pmu_disable, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1261 | .event_init = power_pmu_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1262 | .add = power_pmu_add, |
| 1263 | .del = power_pmu_del, |
| 1264 | .start = power_pmu_start, |
| 1265 | .stop = power_pmu_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1266 | .read = power_pmu_read, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1267 | .start_txn = power_pmu_start_txn, |
| 1268 | .cancel_txn = power_pmu_cancel_txn, |
| 1269 | .commit_txn = power_pmu_commit_txn, |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 1270 | .event_idx = power_pmu_event_idx, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1271 | }; |
| 1272 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1273 | /* |
Ingo Molnar | 57c0c15 | 2009-09-21 12:20:38 +0200 | [diff] [blame] | 1274 | * A counter has overflowed; update its count and record |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1275 | * things if requested. Note that interrupts are hard-disabled |
| 1276 | * here so there is no possibility of being interrupted. |
| 1277 | */ |
| 1278 | static void record_and_restart(struct perf_event *event, unsigned long val, |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 1279 | struct pt_regs *regs) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1280 | { |
| 1281 | u64 period = event->hw.sample_period; |
| 1282 | s64 prev, delta, left; |
| 1283 | int record = 0; |
| 1284 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1285 | if (event->hw.state & PERF_HES_STOPPED) { |
| 1286 | write_pmc(event->hw.idx, 0); |
| 1287 | return; |
| 1288 | } |
| 1289 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1290 | /* we don't have to worry about interrupts here */ |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 1291 | prev = local64_read(&event->hw.prev_count); |
Eric B Munson | 86c74ab | 2011-04-15 08:12:30 +0000 | [diff] [blame] | 1292 | delta = check_and_compute_delta(prev, val); |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 1293 | local64_add(delta, &event->count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1294 | |
| 1295 | /* |
| 1296 | * See if the total period for this event has expired, |
| 1297 | * and update for the next period. |
| 1298 | */ |
| 1299 | val = 0; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 1300 | left = local64_read(&event->hw.period_left) - delta; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1301 | if (period) { |
| 1302 | if (left <= 0) { |
| 1303 | left += period; |
| 1304 | if (left <= 0) |
| 1305 | left = period; |
| 1306 | record = 1; |
Anton Blanchard | 4bca770 | 2011-01-17 16:17:42 +1100 | [diff] [blame] | 1307 | event->hw.last_period = event->hw.sample_period; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1308 | } |
| 1309 | if (left < 0x80000000LL) |
| 1310 | val = 0x80000000LL - left; |
| 1311 | } |
| 1312 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1313 | write_pmc(event->hw.idx, val); |
| 1314 | local64_set(&event->hw.prev_count, val); |
| 1315 | local64_set(&event->hw.period_left, left); |
| 1316 | perf_event_update_userpage(event); |
| 1317 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1318 | /* |
| 1319 | * Finally record data if requested. |
| 1320 | */ |
| 1321 | if (record) { |
Peter Zijlstra | dc1d628 | 2010-03-03 15:55:04 +0100 | [diff] [blame] | 1322 | struct perf_sample_data data; |
| 1323 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 1324 | perf_sample_data_init(&data, ~0ULL, event->hw.last_period); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1325 | |
| 1326 | if (event->attr.sample_type & PERF_SAMPLE_ADDR) |
| 1327 | perf_get_data_addr(regs, &data.addr); |
| 1328 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 1329 | if (perf_event_overflow(event, &data, regs)) |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1330 | power_pmu_stop(event, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1331 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | /* |
| 1335 | * Called from generic code to get the misc flags (i.e. processor mode) |
| 1336 | * for an event_id. |
| 1337 | */ |
| 1338 | unsigned long perf_misc_flags(struct pt_regs *regs) |
| 1339 | { |
| 1340 | u32 flags = perf_get_misc_flags(regs); |
| 1341 | |
| 1342 | if (flags) |
| 1343 | return flags; |
| 1344 | return user_mode(regs) ? PERF_RECORD_MISC_USER : |
| 1345 | PERF_RECORD_MISC_KERNEL; |
| 1346 | } |
| 1347 | |
| 1348 | /* |
| 1349 | * Called from generic code to get the instruction pointer |
| 1350 | * for an event_id. |
| 1351 | */ |
| 1352 | unsigned long perf_instruction_pointer(struct pt_regs *regs) |
| 1353 | { |
Anton Blanchard | 75382aa | 2012-06-26 01:01:36 +0000 | [diff] [blame^] | 1354 | unsigned long use_siar = regs->result; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1355 | |
Anton Blanchard | 75382aa | 2012-06-26 01:01:36 +0000 | [diff] [blame^] | 1356 | if (use_siar) |
| 1357 | return mfspr(SPRN_SIAR) + perf_ip_adjust(regs); |
| 1358 | else |
Benjamin Herrenschmidt | 1ce447b | 2012-03-26 20:47:34 +0000 | [diff] [blame] | 1359 | return regs->nip; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1360 | } |
| 1361 | |
Anton Blanchard | 0837e32 | 2011-03-09 14:38:42 +1100 | [diff] [blame] | 1362 | static bool pmc_overflow(unsigned long val) |
| 1363 | { |
| 1364 | if ((int)val < 0) |
| 1365 | return true; |
| 1366 | |
| 1367 | /* |
| 1368 | * Events on POWER7 can roll back if a speculative event doesn't |
| 1369 | * eventually complete. Unfortunately in some rare cases they will |
| 1370 | * raise a performance monitor exception. We need to catch this to |
| 1371 | * ensure we reset the PMC. In all cases the PMC will be 256 or less |
| 1372 | * cycles from overflow. |
| 1373 | * |
| 1374 | * We only do this if the first pass fails to find any overflowing |
| 1375 | * PMCs because a user might set a period of less than 256 and we |
| 1376 | * don't want to mistakenly reset them. |
| 1377 | */ |
| 1378 | if (__is_processor(PV_POWER7) && ((0x80000000 - val) <= 256)) |
| 1379 | return true; |
| 1380 | |
| 1381 | return false; |
| 1382 | } |
| 1383 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1384 | /* |
| 1385 | * Performance monitor interrupt stuff |
| 1386 | */ |
| 1387 | static void perf_event_interrupt(struct pt_regs *regs) |
| 1388 | { |
| 1389 | int i; |
| 1390 | struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events); |
| 1391 | struct perf_event *event; |
| 1392 | unsigned long val; |
| 1393 | int found = 0; |
| 1394 | int nmi; |
| 1395 | |
| 1396 | if (cpuhw->n_limited) |
Paul Mackerras | a8f90e9 | 2009-09-22 09:48:08 +1000 | [diff] [blame] | 1397 | freeze_limited_counters(cpuhw, mfspr(SPRN_PMC5), |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1398 | mfspr(SPRN_PMC6)); |
| 1399 | |
| 1400 | perf_read_regs(regs); |
| 1401 | |
| 1402 | nmi = perf_intr_is_nmi(regs); |
| 1403 | if (nmi) |
| 1404 | nmi_enter(); |
| 1405 | else |
| 1406 | irq_enter(); |
| 1407 | |
| 1408 | for (i = 0; i < cpuhw->n_events; ++i) { |
| 1409 | event = cpuhw->event[i]; |
| 1410 | if (!event->hw.idx || is_limited_pmc(event->hw.idx)) |
| 1411 | continue; |
| 1412 | val = read_pmc(event->hw.idx); |
| 1413 | if ((int)val < 0) { |
| 1414 | /* event has overflowed */ |
| 1415 | found = 1; |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 1416 | record_and_restart(event, val, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | /* |
| 1421 | * In case we didn't find and reset the event that caused |
| 1422 | * the interrupt, scan all events and reset any that are |
| 1423 | * negative, to avoid getting continual interrupts. |
| 1424 | * Any that we processed in the previous loop will not be negative. |
| 1425 | */ |
| 1426 | if (!found) { |
Paul Mackerras | a8f90e9 | 2009-09-22 09:48:08 +1000 | [diff] [blame] | 1427 | for (i = 0; i < ppmu->n_counter; ++i) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1428 | if (is_limited_pmc(i + 1)) |
| 1429 | continue; |
| 1430 | val = read_pmc(i + 1); |
Anton Blanchard | 0837e32 | 2011-03-09 14:38:42 +1100 | [diff] [blame] | 1431 | if (pmc_overflow(val)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1432 | write_pmc(i + 1, 0); |
| 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | /* |
| 1437 | * Reset MMCR0 to its normal value. This will set PMXE and |
Ingo Molnar | 57c0c15 | 2009-09-21 12:20:38 +0200 | [diff] [blame] | 1438 | * clear FC (freeze counters) and PMAO (perf mon alert occurred) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1439 | * and thus allow interrupts to occur again. |
| 1440 | * XXX might want to use MSR.PM to keep the events frozen until |
| 1441 | * we get back out of this interrupt. |
| 1442 | */ |
| 1443 | write_mmcr0(cpuhw, cpuhw->mmcr[0]); |
| 1444 | |
| 1445 | if (nmi) |
| 1446 | nmi_exit(); |
| 1447 | else |
| 1448 | irq_exit(); |
| 1449 | } |
| 1450 | |
Peter Zijlstra | 3f6da39 | 2010-03-05 13:01:18 +0100 | [diff] [blame] | 1451 | static void power_pmu_setup(int cpu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1452 | { |
| 1453 | struct cpu_hw_events *cpuhw = &per_cpu(cpu_hw_events, cpu); |
| 1454 | |
| 1455 | if (!ppmu) |
| 1456 | return; |
| 1457 | memset(cpuhw, 0, sizeof(*cpuhw)); |
| 1458 | cpuhw->mmcr[0] = MMCR0_FC; |
| 1459 | } |
| 1460 | |
Peter Zijlstra | 3f6da39 | 2010-03-05 13:01:18 +0100 | [diff] [blame] | 1461 | static int __cpuinit |
Peter Zijlstra | 85cfabb | 2010-03-11 13:06:56 +0100 | [diff] [blame] | 1462 | power_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu) |
Peter Zijlstra | 3f6da39 | 2010-03-05 13:01:18 +0100 | [diff] [blame] | 1463 | { |
| 1464 | unsigned int cpu = (long)hcpu; |
| 1465 | |
| 1466 | switch (action & ~CPU_TASKS_FROZEN) { |
| 1467 | case CPU_UP_PREPARE: |
| 1468 | power_pmu_setup(cpu); |
| 1469 | break; |
| 1470 | |
| 1471 | default: |
| 1472 | break; |
| 1473 | } |
| 1474 | |
| 1475 | return NOTIFY_OK; |
| 1476 | } |
| 1477 | |
Dmitry Eremin-Solenikov | 77c2342 | 2011-06-29 04:54:00 +0000 | [diff] [blame] | 1478 | int __cpuinit register_power_pmu(struct power_pmu *pmu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1479 | { |
| 1480 | if (ppmu) |
| 1481 | return -EBUSY; /* something's already registered */ |
| 1482 | |
| 1483 | ppmu = pmu; |
| 1484 | pr_info("%s performance monitor hardware support registered\n", |
| 1485 | pmu->name); |
| 1486 | |
| 1487 | #ifdef MSR_HV |
| 1488 | /* |
| 1489 | * Use FCHV to ignore kernel events if MSR.HV is set. |
| 1490 | */ |
| 1491 | if (mfmsr() & MSR_HV) |
| 1492 | freeze_events_kernel = MMCR0_FCHV; |
| 1493 | #endif /* CONFIG_PPC64 */ |
| 1494 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 1495 | perf_pmu_register(&power_pmu, "cpu", PERF_TYPE_RAW); |
Peter Zijlstra | 3f6da39 | 2010-03-05 13:01:18 +0100 | [diff] [blame] | 1496 | perf_cpu_notifier(power_pmu_notifier); |
| 1497 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1498 | return 0; |
| 1499 | } |