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