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> |
Michael Neuling | 6912318 | 2013-05-13 18:44:58 +0000 | [diff] [blame] | 16 | #include <linux/uaccess.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 17 | #include <asm/reg.h> |
| 18 | #include <asm/pmc.h> |
| 19 | #include <asm/machdep.h> |
| 20 | #include <asm/firmware.h> |
| 21 | #include <asm/ptrace.h> |
Michael Neuling | 6912318 | 2013-05-13 18:44:58 +0000 | [diff] [blame] | 22 | #include <asm/code-patching.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 23 | |
Anshuman Khandual | 3925f46 | 2013-04-22 19:42:44 +0000 | [diff] [blame] | 24 | #define BHRB_MAX_ENTRIES 32 |
| 25 | #define BHRB_TARGET 0x0000000000000002 |
| 26 | #define BHRB_PREDICTION 0x0000000000000001 |
| 27 | #define BHRB_EA 0xFFFFFFFFFFFFFFFC |
| 28 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 29 | struct cpu_hw_events { |
| 30 | int n_events; |
| 31 | int n_percpu; |
| 32 | int disabled; |
| 33 | int n_added; |
| 34 | int n_limited; |
| 35 | u8 pmcs_enabled; |
| 36 | struct perf_event *event[MAX_HWEVENTS]; |
| 37 | u64 events[MAX_HWEVENTS]; |
| 38 | unsigned int flags[MAX_HWEVENTS]; |
| 39 | unsigned long mmcr[3]; |
Paul Mackerras | a8f90e9 | 2009-09-22 09:48:08 +1000 | [diff] [blame] | 40 | struct perf_event *limited_counter[MAX_LIMITED_HWCOUNTERS]; |
| 41 | u8 limited_hwidx[MAX_LIMITED_HWCOUNTERS]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 42 | u64 alternatives[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES]; |
| 43 | unsigned long amasks[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES]; |
| 44 | unsigned long avalues[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES]; |
Lin Ming | 8e6d557 | 2010-05-08 20:28:41 +1000 | [diff] [blame] | 45 | |
| 46 | unsigned int group_flag; |
| 47 | int n_txn_start; |
Anshuman Khandual | 3925f46 | 2013-04-22 19:42:44 +0000 | [diff] [blame] | 48 | |
| 49 | /* BHRB bits */ |
| 50 | u64 bhrb_filter; /* BHRB HW branch filter */ |
| 51 | int bhrb_users; |
| 52 | void *bhrb_context; |
| 53 | struct perf_branch_stack bhrb_stack; |
| 54 | struct perf_branch_entry bhrb_entries[BHRB_MAX_ENTRIES]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 55 | }; |
Anshuman Khandual | 3925f46 | 2013-04-22 19:42:44 +0000 | [diff] [blame] | 56 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 57 | DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events); |
| 58 | |
| 59 | struct power_pmu *ppmu; |
| 60 | |
| 61 | /* |
Ingo Molnar | 57c0c15 | 2009-09-21 12:20:38 +0200 | [diff] [blame] | 62 | * Normally, to ignore kernel events we set the FCS (freeze counters |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 63 | * in supervisor mode) bit in MMCR0, but if the kernel runs with the |
| 64 | * hypervisor bit set in the MSR, or if we are running on a processor |
| 65 | * where the hypervisor bit is forced to 1 (as on Apple G5 processors), |
| 66 | * then we need to use the FCHV bit to ignore kernel events. |
| 67 | */ |
| 68 | static unsigned int freeze_events_kernel = MMCR0_FCS; |
| 69 | |
| 70 | /* |
| 71 | * 32-bit doesn't have MMCRA but does have an MMCR2, |
| 72 | * and a few other names are different. |
| 73 | */ |
| 74 | #ifdef CONFIG_PPC32 |
| 75 | |
| 76 | #define MMCR0_FCHV 0 |
| 77 | #define MMCR0_PMCjCE MMCR0_PMCnCE |
Michael Ellerman | 7a7a41f | 2013-06-28 18:15:12 +1000 | [diff] [blame] | 78 | #define MMCR0_FC56 0 |
Michael Ellerman | 378a6ee | 2013-06-28 18:15:11 +1000 | [diff] [blame] | 79 | #define MMCR0_PMAO 0 |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 80 | |
| 81 | #define SPRN_MMCRA SPRN_MMCR2 |
| 82 | #define MMCRA_SAMPLE_ENABLE 0 |
| 83 | |
| 84 | static inline unsigned long perf_ip_adjust(struct pt_regs *regs) |
| 85 | { |
| 86 | return 0; |
| 87 | } |
| 88 | static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp) { } |
| 89 | static inline u32 perf_get_misc_flags(struct pt_regs *regs) |
| 90 | { |
| 91 | return 0; |
| 92 | } |
Anton Blanchard | 75382aa | 2012-06-26 01:01:36 +0000 | [diff] [blame] | 93 | static inline void perf_read_regs(struct pt_regs *regs) |
| 94 | { |
| 95 | regs->result = 0; |
| 96 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 97 | static inline int perf_intr_is_nmi(struct pt_regs *regs) |
| 98 | { |
| 99 | return 0; |
| 100 | } |
| 101 | |
sukadev@linux.vnet.ibm.com | e687883 | 2012-09-18 20:56:11 +0000 | [diff] [blame] | 102 | static inline int siar_valid(struct pt_regs *regs) |
| 103 | { |
| 104 | return 1; |
| 105 | } |
| 106 | |
Michael Neuling | d52f2dc | 2013-05-13 18:44:56 +0000 | [diff] [blame] | 107 | static inline void power_pmu_bhrb_enable(struct perf_event *event) {} |
| 108 | static inline void power_pmu_bhrb_disable(struct perf_event *event) {} |
| 109 | void power_pmu_flush_branch_stack(void) {} |
| 110 | static inline void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw) {} |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 111 | #endif /* CONFIG_PPC32 */ |
| 112 | |
Michael Ellerman | 3390405 | 2013-04-25 19:28:25 +0000 | [diff] [blame] | 113 | static bool regs_use_siar(struct pt_regs *regs) |
| 114 | { |
Michael Ellerman | cbda6aa | 2013-05-15 20:19:30 +0000 | [diff] [blame] | 115 | return !!regs->result; |
Michael Ellerman | 3390405 | 2013-04-25 19:28:25 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 118 | /* |
| 119 | * Things that are specific to 64-bit implementations. |
| 120 | */ |
| 121 | #ifdef CONFIG_PPC64 |
| 122 | |
| 123 | static inline unsigned long perf_ip_adjust(struct pt_regs *regs) |
| 124 | { |
| 125 | unsigned long mmcra = regs->dsisr; |
| 126 | |
Michael Ellerman | 7a78683 | 2013-04-25 19:28:23 +0000 | [diff] [blame] | 127 | if ((ppmu->flags & PPMU_HAS_SSLOT) && (mmcra & MMCRA_SAMPLE_ENABLE)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 128 | unsigned long slot = (mmcra & MMCRA_SLOT) >> MMCRA_SLOT_SHIFT; |
| 129 | if (slot > 1) |
| 130 | return 4 * (slot - 1); |
| 131 | } |
Michael Ellerman | 7a78683 | 2013-04-25 19:28:23 +0000 | [diff] [blame] | 132 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * The user wants a data address recorded. |
| 138 | * If we're not doing instruction sampling, give them the SDAR |
| 139 | * (sampled data address). If we are doing instruction sampling, then |
| 140 | * only give them the SDAR if it corresponds to the instruction |
Michael Ellerman | 58a032c | 2013-05-15 20:19:31 +0000 | [diff] [blame] | 141 | * pointed to by SIAR; this is indicated by the [POWER6_]MMCRA_SDSYNC, the |
| 142 | * [POWER7P_]MMCRA_SDAR_VALID bit in MMCRA, or the SDAR_VALID bit in SIER. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 143 | */ |
| 144 | static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp) |
| 145 | { |
| 146 | unsigned long mmcra = regs->dsisr; |
Michael Ellerman | 58a032c | 2013-05-15 20:19:31 +0000 | [diff] [blame] | 147 | bool sdar_valid; |
sukadev@linux.vnet.ibm.com | e687883 | 2012-09-18 20:56:11 +0000 | [diff] [blame] | 148 | |
Michael Ellerman | 58a032c | 2013-05-15 20:19:31 +0000 | [diff] [blame] | 149 | if (ppmu->flags & PPMU_HAS_SIER) |
| 150 | sdar_valid = regs->dar & SIER_SDAR_VALID; |
| 151 | else { |
| 152 | unsigned long sdsync; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 153 | |
Michael Ellerman | 58a032c | 2013-05-15 20:19:31 +0000 | [diff] [blame] | 154 | if (ppmu->flags & PPMU_SIAR_VALID) |
| 155 | sdsync = POWER7P_MMCRA_SDAR_VALID; |
| 156 | else if (ppmu->flags & PPMU_ALT_SIPR) |
| 157 | sdsync = POWER6_MMCRA_SDSYNC; |
| 158 | else |
| 159 | sdsync = MMCRA_SDSYNC; |
| 160 | |
| 161 | sdar_valid = mmcra & sdsync; |
| 162 | } |
| 163 | |
| 164 | if (!(mmcra & MMCRA_SAMPLE_ENABLE) || sdar_valid) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 165 | *addrp = mfspr(SPRN_SDAR); |
| 166 | } |
| 167 | |
Michael Ellerman | 5682c46 | 2013-04-25 19:28:24 +0000 | [diff] [blame] | 168 | static bool regs_sihv(struct pt_regs *regs) |
Anton Blanchard | 68b30bb | 2012-06-26 01:00:13 +0000 | [diff] [blame] | 169 | { |
| 170 | unsigned long sihv = MMCRA_SIHV; |
| 171 | |
Michael Ellerman | 8f61aa3 | 2013-04-25 19:28:27 +0000 | [diff] [blame] | 172 | if (ppmu->flags & PPMU_HAS_SIER) |
| 173 | return !!(regs->dar & SIER_SIHV); |
| 174 | |
Anton Blanchard | 68b30bb | 2012-06-26 01:00:13 +0000 | [diff] [blame] | 175 | if (ppmu->flags & PPMU_ALT_SIPR) |
| 176 | sihv = POWER6_MMCRA_SIHV; |
| 177 | |
Michael Ellerman | 5682c46 | 2013-04-25 19:28:24 +0000 | [diff] [blame] | 178 | return !!(regs->dsisr & sihv); |
Anton Blanchard | 68b30bb | 2012-06-26 01:00:13 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Michael Ellerman | 5682c46 | 2013-04-25 19:28:24 +0000 | [diff] [blame] | 181 | static bool regs_sipr(struct pt_regs *regs) |
Anton Blanchard | 68b30bb | 2012-06-26 01:00:13 +0000 | [diff] [blame] | 182 | { |
| 183 | unsigned long sipr = MMCRA_SIPR; |
| 184 | |
Michael Ellerman | 8f61aa3 | 2013-04-25 19:28:27 +0000 | [diff] [blame] | 185 | if (ppmu->flags & PPMU_HAS_SIER) |
| 186 | return !!(regs->dar & SIER_SIPR); |
| 187 | |
Anton Blanchard | 68b30bb | 2012-06-26 01:00:13 +0000 | [diff] [blame] | 188 | if (ppmu->flags & PPMU_ALT_SIPR) |
| 189 | sipr = POWER6_MMCRA_SIPR; |
| 190 | |
Michael Ellerman | 5682c46 | 2013-04-25 19:28:24 +0000 | [diff] [blame] | 191 | return !!(regs->dsisr & sipr); |
Anton Blanchard | 68b30bb | 2012-06-26 01:00:13 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Benjamin Herrenschmidt | 1ce447b | 2012-03-26 20:47:34 +0000 | [diff] [blame] | 194 | static inline u32 perf_flags_from_msr(struct pt_regs *regs) |
| 195 | { |
| 196 | if (regs->msr & MSR_PR) |
| 197 | return PERF_RECORD_MISC_USER; |
| 198 | if ((regs->msr & MSR_HV) && freeze_events_kernel != MMCR0_FCHV) |
| 199 | return PERF_RECORD_MISC_HYPERVISOR; |
| 200 | return PERF_RECORD_MISC_KERNEL; |
| 201 | } |
| 202 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 203 | static inline u32 perf_get_misc_flags(struct pt_regs *regs) |
| 204 | { |
Michael Ellerman | 3390405 | 2013-04-25 19:28:25 +0000 | [diff] [blame] | 205 | bool use_siar = regs_use_siar(regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 206 | |
Anton Blanchard | 75382aa | 2012-06-26 01:01:36 +0000 | [diff] [blame] | 207 | if (!use_siar) |
Benjamin Herrenschmidt | 1ce447b | 2012-03-26 20:47:34 +0000 | [diff] [blame] | 208 | return perf_flags_from_msr(regs); |
| 209 | |
| 210 | /* |
| 211 | * If we don't have flags in MMCRA, rather than using |
| 212 | * the MSR, we intuit the flags from the address in |
| 213 | * SIAR which should give slightly more reliable |
| 214 | * results |
| 215 | */ |
Michael Ellerman | cbda6aa | 2013-05-15 20:19:30 +0000 | [diff] [blame] | 216 | if (ppmu->flags & PPMU_NO_SIPR) { |
Benjamin Herrenschmidt | 1ce447b | 2012-03-26 20:47:34 +0000 | [diff] [blame] | 217 | unsigned long siar = mfspr(SPRN_SIAR); |
| 218 | if (siar >= PAGE_OFFSET) |
| 219 | return PERF_RECORD_MISC_KERNEL; |
| 220 | return PERF_RECORD_MISC_USER; |
| 221 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 222 | |
Michael Neuling | 7abb840 | 2009-10-14 19:32:15 +0000 | [diff] [blame] | 223 | /* PR has priority over HV, so order below is important */ |
Michael Ellerman | 5682c46 | 2013-04-25 19:28:24 +0000 | [diff] [blame] | 224 | if (regs_sipr(regs)) |
Michael Neuling | 7abb840 | 2009-10-14 19:32:15 +0000 | [diff] [blame] | 225 | return PERF_RECORD_MISC_USER; |
Michael Ellerman | 5682c46 | 2013-04-25 19:28:24 +0000 | [diff] [blame] | 226 | |
| 227 | if (regs_sihv(regs) && (freeze_events_kernel != MMCR0_FCHV)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 228 | return PERF_RECORD_MISC_HYPERVISOR; |
Michael Ellerman | 5682c46 | 2013-04-25 19:28:24 +0000 | [diff] [blame] | 229 | |
Michael Neuling | 7abb840 | 2009-10-14 19:32:15 +0000 | [diff] [blame] | 230 | return PERF_RECORD_MISC_KERNEL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /* |
| 234 | * Overload regs->dsisr to store MMCRA so we only need to read it once |
| 235 | * on each interrupt. |
Michael Ellerman | 8f61aa3 | 2013-04-25 19:28:27 +0000 | [diff] [blame] | 236 | * Overload regs->dar to store SIER if we have it. |
Anton Blanchard | 75382aa | 2012-06-26 01:01:36 +0000 | [diff] [blame] | 237 | * Overload regs->result to specify whether we should use the MSR (result |
| 238 | * is zero) or the SIAR (result is non zero). |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 239 | */ |
| 240 | static inline void perf_read_regs(struct pt_regs *regs) |
| 241 | { |
Anton Blanchard | 75382aa | 2012-06-26 01:01:36 +0000 | [diff] [blame] | 242 | unsigned long mmcra = mfspr(SPRN_MMCRA); |
| 243 | int marked = mmcra & MMCRA_SAMPLE_ENABLE; |
| 244 | int use_siar; |
| 245 | |
Michael Ellerman | 5682c46 | 2013-04-25 19:28:24 +0000 | [diff] [blame] | 246 | regs->dsisr = mmcra; |
Michael Ellerman | 860aad7 | 2013-04-25 19:28:26 +0000 | [diff] [blame] | 247 | |
Michael Ellerman | cbda6aa | 2013-05-15 20:19:30 +0000 | [diff] [blame] | 248 | if (ppmu->flags & PPMU_HAS_SIER) |
| 249 | regs->dar = mfspr(SPRN_SIER); |
Michael Ellerman | 8f61aa3 | 2013-04-25 19:28:27 +0000 | [diff] [blame] | 250 | |
| 251 | /* |
Anton Blanchard | 5c093ef | 2012-06-26 01:02:15 +0000 | [diff] [blame] | 252 | * If this isn't a PMU exception (eg a software event) the SIAR is |
| 253 | * not valid. Use pt_regs. |
| 254 | * |
| 255 | * If it is a marked event use the SIAR. |
| 256 | * |
| 257 | * If the PMU doesn't update the SIAR for non marked events use |
| 258 | * pt_regs. |
| 259 | * |
| 260 | * If the PMU has HV/PR flags then check to see if they |
| 261 | * place the exception in userspace. If so, use pt_regs. In |
| 262 | * continuous sampling mode the SIAR and the PMU exception are |
| 263 | * not synchronised, so they may be many instructions apart. |
| 264 | * This can result in confusing backtraces. We still want |
| 265 | * hypervisor samples as well as samples in the kernel with |
| 266 | * interrupts off hence the userspace check. |
| 267 | */ |
Anton Blanchard | 75382aa | 2012-06-26 01:01:36 +0000 | [diff] [blame] | 268 | if (TRAP(regs) != 0xf00) |
| 269 | use_siar = 0; |
Anton Blanchard | 5c093ef | 2012-06-26 01:02:15 +0000 | [diff] [blame] | 270 | else if (marked) |
| 271 | use_siar = 1; |
| 272 | else if ((ppmu->flags & PPMU_NO_CONT_SAMPLING)) |
| 273 | use_siar = 0; |
Michael Ellerman | cbda6aa | 2013-05-15 20:19:30 +0000 | [diff] [blame] | 274 | else if (!(ppmu->flags & PPMU_NO_SIPR) && regs_sipr(regs)) |
Anton Blanchard | 75382aa | 2012-06-26 01:01:36 +0000 | [diff] [blame] | 275 | use_siar = 0; |
| 276 | else |
| 277 | use_siar = 1; |
| 278 | |
Michael Ellerman | cbda6aa | 2013-05-15 20:19:30 +0000 | [diff] [blame] | 279 | regs->result = use_siar; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | /* |
| 283 | * If interrupts were soft-disabled when a PMU interrupt occurs, treat |
| 284 | * it as an NMI. |
| 285 | */ |
| 286 | static inline int perf_intr_is_nmi(struct pt_regs *regs) |
| 287 | { |
| 288 | return !regs->softe; |
| 289 | } |
| 290 | |
sukadev@linux.vnet.ibm.com | e687883 | 2012-09-18 20:56:11 +0000 | [diff] [blame] | 291 | /* |
| 292 | * On processors like P7+ that have the SIAR-Valid bit, marked instructions |
| 293 | * must be sampled only if the SIAR-valid bit is set. |
| 294 | * |
| 295 | * For unmarked instructions and for processors that don't have the SIAR-Valid |
| 296 | * bit, assume that SIAR is valid. |
| 297 | */ |
| 298 | static inline int siar_valid(struct pt_regs *regs) |
| 299 | { |
| 300 | unsigned long mmcra = regs->dsisr; |
| 301 | int marked = mmcra & MMCRA_SAMPLE_ENABLE; |
| 302 | |
Michael Ellerman | 58a032c | 2013-05-15 20:19:31 +0000 | [diff] [blame] | 303 | if (marked) { |
| 304 | if (ppmu->flags & PPMU_HAS_SIER) |
| 305 | return regs->dar & SIER_SIAR_VALID; |
| 306 | |
| 307 | if (ppmu->flags & PPMU_SIAR_VALID) |
| 308 | return mmcra & POWER7P_MMCRA_SIAR_VALID; |
| 309 | } |
sukadev@linux.vnet.ibm.com | e687883 | 2012-09-18 20:56:11 +0000 | [diff] [blame] | 310 | |
| 311 | return 1; |
| 312 | } |
| 313 | |
Michael Neuling | d52f2dc | 2013-05-13 18:44:56 +0000 | [diff] [blame] | 314 | |
| 315 | /* Reset all possible BHRB entries */ |
| 316 | static void power_pmu_bhrb_reset(void) |
| 317 | { |
| 318 | asm volatile(PPC_CLRBHRB); |
| 319 | } |
| 320 | |
| 321 | static void power_pmu_bhrb_enable(struct perf_event *event) |
| 322 | { |
| 323 | struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events); |
| 324 | |
| 325 | if (!ppmu->bhrb_nr) |
| 326 | return; |
| 327 | |
| 328 | /* Clear BHRB if we changed task context to avoid data leaks */ |
| 329 | if (event->ctx->task && cpuhw->bhrb_context != event->ctx) { |
| 330 | power_pmu_bhrb_reset(); |
| 331 | cpuhw->bhrb_context = event->ctx; |
| 332 | } |
| 333 | cpuhw->bhrb_users++; |
| 334 | } |
| 335 | |
| 336 | static void power_pmu_bhrb_disable(struct perf_event *event) |
| 337 | { |
| 338 | struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events); |
| 339 | |
| 340 | if (!ppmu->bhrb_nr) |
| 341 | return; |
| 342 | |
| 343 | cpuhw->bhrb_users--; |
| 344 | WARN_ON_ONCE(cpuhw->bhrb_users < 0); |
| 345 | |
| 346 | if (!cpuhw->disabled && !cpuhw->bhrb_users) { |
| 347 | /* BHRB cannot be turned off when other |
| 348 | * events are active on the PMU. |
| 349 | */ |
| 350 | |
| 351 | /* avoid stale pointer */ |
| 352 | cpuhw->bhrb_context = NULL; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | /* Called from ctxsw to prevent one process's branch entries to |
| 357 | * mingle with the other process's entries during context switch. |
| 358 | */ |
| 359 | void power_pmu_flush_branch_stack(void) |
| 360 | { |
| 361 | if (ppmu->bhrb_nr) |
| 362 | power_pmu_bhrb_reset(); |
| 363 | } |
Michael Neuling | 6912318 | 2013-05-13 18:44:58 +0000 | [diff] [blame] | 364 | /* Calculate the to address for a branch */ |
| 365 | static __u64 power_pmu_bhrb_to(u64 addr) |
| 366 | { |
| 367 | unsigned int instr; |
| 368 | int ret; |
| 369 | __u64 target; |
| 370 | |
| 371 | if (is_kernel_addr(addr)) |
| 372 | return branch_target((unsigned int *)addr); |
| 373 | |
| 374 | /* Userspace: need copy instruction here then translate it */ |
| 375 | pagefault_disable(); |
| 376 | ret = __get_user_inatomic(instr, (unsigned int __user *)addr); |
| 377 | if (ret) { |
| 378 | pagefault_enable(); |
| 379 | return 0; |
| 380 | } |
| 381 | pagefault_enable(); |
| 382 | |
| 383 | target = branch_target(&instr); |
| 384 | if ((!target) || (instr & BRANCH_ABSOLUTE)) |
| 385 | return target; |
| 386 | |
| 387 | /* Translate relative branch target from kernel to user address */ |
| 388 | return target - (unsigned long)&instr + addr; |
| 389 | } |
Michael Neuling | d52f2dc | 2013-05-13 18:44:56 +0000 | [diff] [blame] | 390 | |
Michael Neuling | d52f2dc | 2013-05-13 18:44:56 +0000 | [diff] [blame] | 391 | /* Processing BHRB entries */ |
Michael Neuling | 506e70d | 2013-05-13 18:44:57 +0000 | [diff] [blame] | 392 | void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw) |
Michael Neuling | d52f2dc | 2013-05-13 18:44:56 +0000 | [diff] [blame] | 393 | { |
| 394 | u64 val; |
| 395 | u64 addr; |
Michael Neuling | 506e70d | 2013-05-13 18:44:57 +0000 | [diff] [blame] | 396 | int r_index, u_index, pred; |
Michael Neuling | d52f2dc | 2013-05-13 18:44:56 +0000 | [diff] [blame] | 397 | |
| 398 | r_index = 0; |
| 399 | u_index = 0; |
| 400 | while (r_index < ppmu->bhrb_nr) { |
| 401 | /* Assembly read function */ |
Michael Neuling | 506e70d | 2013-05-13 18:44:57 +0000 | [diff] [blame] | 402 | val = read_bhrb(r_index++); |
| 403 | if (!val) |
| 404 | /* Terminal marker: End of valid BHRB entries */ |
Michael Neuling | d52f2dc | 2013-05-13 18:44:56 +0000 | [diff] [blame] | 405 | break; |
Michael Neuling | 506e70d | 2013-05-13 18:44:57 +0000 | [diff] [blame] | 406 | else { |
Michael Neuling | d52f2dc | 2013-05-13 18:44:56 +0000 | [diff] [blame] | 407 | addr = val & BHRB_EA; |
| 408 | pred = val & BHRB_PREDICTION; |
Michael Neuling | d52f2dc | 2013-05-13 18:44:56 +0000 | [diff] [blame] | 409 | |
Michael Neuling | 506e70d | 2013-05-13 18:44:57 +0000 | [diff] [blame] | 410 | if (!addr) |
| 411 | /* invalid entry */ |
Michael Neuling | d52f2dc | 2013-05-13 18:44:56 +0000 | [diff] [blame] | 412 | continue; |
Michael Neuling | d52f2dc | 2013-05-13 18:44:56 +0000 | [diff] [blame] | 413 | |
Michael Neuling | 506e70d | 2013-05-13 18:44:57 +0000 | [diff] [blame] | 414 | /* Branches are read most recent first (ie. mfbhrb 0 is |
| 415 | * the most recent branch). |
| 416 | * There are two types of valid entries: |
| 417 | * 1) a target entry which is the to address of a |
| 418 | * computed goto like a blr,bctr,btar. The next |
| 419 | * entry read from the bhrb will be branch |
| 420 | * corresponding to this target (ie. the actual |
| 421 | * blr/bctr/btar instruction). |
| 422 | * 2) a from address which is an actual branch. If a |
| 423 | * target entry proceeds this, then this is the |
| 424 | * matching branch for that target. If this is not |
| 425 | * following a target entry, then this is a branch |
| 426 | * where the target is given as an immediate field |
| 427 | * in the instruction (ie. an i or b form branch). |
| 428 | * In this case we need to read the instruction from |
| 429 | * memory to determine the target/to address. |
| 430 | */ |
Michael Neuling | d52f2dc | 2013-05-13 18:44:56 +0000 | [diff] [blame] | 431 | |
Michael Neuling | d52f2dc | 2013-05-13 18:44:56 +0000 | [diff] [blame] | 432 | if (val & BHRB_TARGET) { |
Michael Neuling | 506e70d | 2013-05-13 18:44:57 +0000 | [diff] [blame] | 433 | /* Target branches use two entries |
| 434 | * (ie. computed gotos/XL form) |
| 435 | */ |
| 436 | cpuhw->bhrb_entries[u_index].to = addr; |
Michael Neuling | d52f2dc | 2013-05-13 18:44:56 +0000 | [diff] [blame] | 437 | cpuhw->bhrb_entries[u_index].mispred = pred; |
| 438 | cpuhw->bhrb_entries[u_index].predicted = ~pred; |
| 439 | |
Michael Neuling | 506e70d | 2013-05-13 18:44:57 +0000 | [diff] [blame] | 440 | /* Get from address in next entry */ |
| 441 | val = read_bhrb(r_index++); |
| 442 | addr = val & BHRB_EA; |
| 443 | if (val & BHRB_TARGET) { |
| 444 | /* Shouldn't have two targets in a |
| 445 | row.. Reset index and try again */ |
| 446 | r_index--; |
| 447 | addr = 0; |
| 448 | } |
| 449 | cpuhw->bhrb_entries[u_index].from = addr; |
| 450 | } else { |
| 451 | /* Branches to immediate field |
| 452 | (ie I or B form) */ |
| 453 | cpuhw->bhrb_entries[u_index].from = addr; |
Michael Neuling | 6912318 | 2013-05-13 18:44:58 +0000 | [diff] [blame] | 454 | cpuhw->bhrb_entries[u_index].to = |
| 455 | power_pmu_bhrb_to(addr); |
Michael Neuling | 506e70d | 2013-05-13 18:44:57 +0000 | [diff] [blame] | 456 | cpuhw->bhrb_entries[u_index].mispred = pred; |
| 457 | cpuhw->bhrb_entries[u_index].predicted = ~pred; |
Michael Neuling | d52f2dc | 2013-05-13 18:44:56 +0000 | [diff] [blame] | 458 | } |
Michael Neuling | 506e70d | 2013-05-13 18:44:57 +0000 | [diff] [blame] | 459 | u_index++; |
| 460 | |
Michael Neuling | d52f2dc | 2013-05-13 18:44:56 +0000 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | cpuhw->bhrb_stack.nr = u_index; |
| 464 | return; |
| 465 | } |
| 466 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 467 | #endif /* CONFIG_PPC64 */ |
| 468 | |
| 469 | static void perf_event_interrupt(struct pt_regs *regs); |
| 470 | |
| 471 | void perf_event_print_debug(void) |
| 472 | { |
| 473 | } |
| 474 | |
| 475 | /* |
Ingo Molnar | 57c0c15 | 2009-09-21 12:20:38 +0200 | [diff] [blame] | 476 | * Read one performance monitor counter (PMC). |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 477 | */ |
| 478 | static unsigned long read_pmc(int idx) |
| 479 | { |
| 480 | unsigned long val; |
| 481 | |
| 482 | switch (idx) { |
| 483 | case 1: |
| 484 | val = mfspr(SPRN_PMC1); |
| 485 | break; |
| 486 | case 2: |
| 487 | val = mfspr(SPRN_PMC2); |
| 488 | break; |
| 489 | case 3: |
| 490 | val = mfspr(SPRN_PMC3); |
| 491 | break; |
| 492 | case 4: |
| 493 | val = mfspr(SPRN_PMC4); |
| 494 | break; |
| 495 | case 5: |
| 496 | val = mfspr(SPRN_PMC5); |
| 497 | break; |
| 498 | case 6: |
| 499 | val = mfspr(SPRN_PMC6); |
| 500 | break; |
| 501 | #ifdef CONFIG_PPC64 |
| 502 | case 7: |
| 503 | val = mfspr(SPRN_PMC7); |
| 504 | break; |
| 505 | case 8: |
| 506 | val = mfspr(SPRN_PMC8); |
| 507 | break; |
| 508 | #endif /* CONFIG_PPC64 */ |
| 509 | default: |
| 510 | printk(KERN_ERR "oops trying to read PMC%d\n", idx); |
| 511 | val = 0; |
| 512 | } |
| 513 | return val; |
| 514 | } |
| 515 | |
| 516 | /* |
| 517 | * Write one PMC. |
| 518 | */ |
| 519 | static void write_pmc(int idx, unsigned long val) |
| 520 | { |
| 521 | switch (idx) { |
| 522 | case 1: |
| 523 | mtspr(SPRN_PMC1, val); |
| 524 | break; |
| 525 | case 2: |
| 526 | mtspr(SPRN_PMC2, val); |
| 527 | break; |
| 528 | case 3: |
| 529 | mtspr(SPRN_PMC3, val); |
| 530 | break; |
| 531 | case 4: |
| 532 | mtspr(SPRN_PMC4, val); |
| 533 | break; |
| 534 | case 5: |
| 535 | mtspr(SPRN_PMC5, val); |
| 536 | break; |
| 537 | case 6: |
| 538 | mtspr(SPRN_PMC6, val); |
| 539 | break; |
| 540 | #ifdef CONFIG_PPC64 |
| 541 | case 7: |
| 542 | mtspr(SPRN_PMC7, val); |
| 543 | break; |
| 544 | case 8: |
| 545 | mtspr(SPRN_PMC8, val); |
| 546 | break; |
| 547 | #endif /* CONFIG_PPC64 */ |
| 548 | default: |
| 549 | printk(KERN_ERR "oops trying to write PMC%d\n", idx); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | /* |
| 554 | * Check if a set of events can all go on the PMU at once. |
| 555 | * If they can't, this will look at alternative codes for the events |
| 556 | * and see if any combination of alternative codes is feasible. |
| 557 | * The feasible set is returned in event_id[]. |
| 558 | */ |
| 559 | static int power_check_constraints(struct cpu_hw_events *cpuhw, |
| 560 | u64 event_id[], unsigned int cflags[], |
| 561 | int n_ev) |
| 562 | { |
| 563 | unsigned long mask, value, nv; |
| 564 | unsigned long smasks[MAX_HWEVENTS], svalues[MAX_HWEVENTS]; |
| 565 | int n_alt[MAX_HWEVENTS], choice[MAX_HWEVENTS]; |
| 566 | int i, j; |
| 567 | unsigned long addf = ppmu->add_fields; |
| 568 | unsigned long tadd = ppmu->test_adder; |
| 569 | |
Paul Mackerras | a8f90e9 | 2009-09-22 09:48:08 +1000 | [diff] [blame] | 570 | if (n_ev > ppmu->n_counter) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 571 | return -1; |
| 572 | |
| 573 | /* First see if the events will go on as-is */ |
| 574 | for (i = 0; i < n_ev; ++i) { |
| 575 | if ((cflags[i] & PPMU_LIMITED_PMC_REQD) |
| 576 | && !ppmu->limited_pmc_event(event_id[i])) { |
| 577 | ppmu->get_alternatives(event_id[i], cflags[i], |
| 578 | cpuhw->alternatives[i]); |
| 579 | event_id[i] = cpuhw->alternatives[i][0]; |
| 580 | } |
| 581 | if (ppmu->get_constraint(event_id[i], &cpuhw->amasks[i][0], |
| 582 | &cpuhw->avalues[i][0])) |
| 583 | return -1; |
| 584 | } |
| 585 | value = mask = 0; |
| 586 | for (i = 0; i < n_ev; ++i) { |
| 587 | nv = (value | cpuhw->avalues[i][0]) + |
| 588 | (value & cpuhw->avalues[i][0] & addf); |
| 589 | if ((((nv + tadd) ^ value) & mask) != 0 || |
| 590 | (((nv + tadd) ^ cpuhw->avalues[i][0]) & |
| 591 | cpuhw->amasks[i][0]) != 0) |
| 592 | break; |
| 593 | value = nv; |
| 594 | mask |= cpuhw->amasks[i][0]; |
| 595 | } |
| 596 | if (i == n_ev) |
| 597 | return 0; /* all OK */ |
| 598 | |
| 599 | /* doesn't work, gather alternatives... */ |
| 600 | if (!ppmu->get_alternatives) |
| 601 | return -1; |
| 602 | for (i = 0; i < n_ev; ++i) { |
| 603 | choice[i] = 0; |
| 604 | n_alt[i] = ppmu->get_alternatives(event_id[i], cflags[i], |
| 605 | cpuhw->alternatives[i]); |
| 606 | for (j = 1; j < n_alt[i]; ++j) |
| 607 | ppmu->get_constraint(cpuhw->alternatives[i][j], |
| 608 | &cpuhw->amasks[i][j], |
| 609 | &cpuhw->avalues[i][j]); |
| 610 | } |
| 611 | |
| 612 | /* enumerate all possibilities and see if any will work */ |
| 613 | i = 0; |
| 614 | j = -1; |
| 615 | value = mask = nv = 0; |
| 616 | while (i < n_ev) { |
| 617 | if (j >= 0) { |
| 618 | /* we're backtracking, restore context */ |
| 619 | value = svalues[i]; |
| 620 | mask = smasks[i]; |
| 621 | j = choice[i]; |
| 622 | } |
| 623 | /* |
| 624 | * See if any alternative k for event_id i, |
| 625 | * where k > j, will satisfy the constraints. |
| 626 | */ |
| 627 | while (++j < n_alt[i]) { |
| 628 | nv = (value | cpuhw->avalues[i][j]) + |
| 629 | (value & cpuhw->avalues[i][j] & addf); |
| 630 | if ((((nv + tadd) ^ value) & mask) == 0 && |
| 631 | (((nv + tadd) ^ cpuhw->avalues[i][j]) |
| 632 | & cpuhw->amasks[i][j]) == 0) |
| 633 | break; |
| 634 | } |
| 635 | if (j >= n_alt[i]) { |
| 636 | /* |
| 637 | * No feasible alternative, backtrack |
| 638 | * to event_id i-1 and continue enumerating its |
| 639 | * alternatives from where we got up to. |
| 640 | */ |
| 641 | if (--i < 0) |
| 642 | return -1; |
| 643 | } else { |
| 644 | /* |
| 645 | * Found a feasible alternative for event_id i, |
| 646 | * remember where we got up to with this event_id, |
| 647 | * go on to the next event_id, and start with |
| 648 | * the first alternative for it. |
| 649 | */ |
| 650 | choice[i] = j; |
| 651 | svalues[i] = value; |
| 652 | smasks[i] = mask; |
| 653 | value = nv; |
| 654 | mask |= cpuhw->amasks[i][j]; |
| 655 | ++i; |
| 656 | j = -1; |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | /* OK, we have a feasible combination, tell the caller the solution */ |
| 661 | for (i = 0; i < n_ev; ++i) |
| 662 | event_id[i] = cpuhw->alternatives[i][choice[i]]; |
| 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | /* |
| 667 | * Check if newly-added events have consistent settings for |
| 668 | * exclude_{user,kernel,hv} with each other and any previously |
| 669 | * added events. |
| 670 | */ |
| 671 | static int check_excludes(struct perf_event **ctrs, unsigned int cflags[], |
| 672 | int n_prev, int n_new) |
| 673 | { |
| 674 | int eu = 0, ek = 0, eh = 0; |
| 675 | int i, n, first; |
| 676 | struct perf_event *event; |
| 677 | |
| 678 | n = n_prev + n_new; |
| 679 | if (n <= 1) |
| 680 | return 0; |
| 681 | |
| 682 | first = 1; |
| 683 | for (i = 0; i < n; ++i) { |
| 684 | if (cflags[i] & PPMU_LIMITED_PMC_OK) { |
| 685 | cflags[i] &= ~PPMU_LIMITED_PMC_REQD; |
| 686 | continue; |
| 687 | } |
| 688 | event = ctrs[i]; |
| 689 | if (first) { |
| 690 | eu = event->attr.exclude_user; |
| 691 | ek = event->attr.exclude_kernel; |
| 692 | eh = event->attr.exclude_hv; |
| 693 | first = 0; |
| 694 | } else if (event->attr.exclude_user != eu || |
| 695 | event->attr.exclude_kernel != ek || |
| 696 | event->attr.exclude_hv != eh) { |
| 697 | return -EAGAIN; |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | if (eu || ek || eh) |
| 702 | for (i = 0; i < n; ++i) |
| 703 | if (cflags[i] & PPMU_LIMITED_PMC_OK) |
| 704 | cflags[i] |= PPMU_LIMITED_PMC_REQD; |
| 705 | |
| 706 | return 0; |
| 707 | } |
| 708 | |
Eric B Munson | 86c74ab | 2011-04-15 08:12:30 +0000 | [diff] [blame] | 709 | static u64 check_and_compute_delta(u64 prev, u64 val) |
| 710 | { |
| 711 | u64 delta = (val - prev) & 0xfffffffful; |
| 712 | |
| 713 | /* |
| 714 | * POWER7 can roll back counter values, if the new value is smaller |
| 715 | * than the previous value it will cause the delta and the counter to |
| 716 | * have bogus values unless we rolled a counter over. If a coutner is |
| 717 | * rolled back, it will be smaller, but within 256, which is the maximum |
| 718 | * number of events to rollback at once. If we dectect a rollback |
| 719 | * return 0. This can lead to a small lack of precision in the |
| 720 | * counters. |
| 721 | */ |
| 722 | if (prev > val && (prev - val) < 256) |
| 723 | delta = 0; |
| 724 | |
| 725 | return delta; |
| 726 | } |
| 727 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 728 | static void power_pmu_read(struct perf_event *event) |
| 729 | { |
| 730 | s64 val, delta, prev; |
| 731 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 732 | if (event->hw.state & PERF_HES_STOPPED) |
| 733 | return; |
| 734 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 735 | if (!event->hw.idx) |
| 736 | return; |
| 737 | /* |
| 738 | * Performance monitor interrupts come even when interrupts |
| 739 | * are soft-disabled, as long as interrupts are hard-enabled. |
| 740 | * Therefore we treat them like NMIs. |
| 741 | */ |
| 742 | do { |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 743 | prev = local64_read(&event->hw.prev_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 744 | barrier(); |
| 745 | val = read_pmc(event->hw.idx); |
Eric B Munson | 86c74ab | 2011-04-15 08:12:30 +0000 | [diff] [blame] | 746 | delta = check_and_compute_delta(prev, val); |
| 747 | if (!delta) |
| 748 | return; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 749 | } while (local64_cmpxchg(&event->hw.prev_count, prev, val) != prev); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 750 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 751 | local64_add(delta, &event->count); |
| 752 | local64_sub(delta, &event->hw.period_left); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | /* |
| 756 | * On some machines, PMC5 and PMC6 can't be written, don't respect |
| 757 | * the freeze conditions, and don't generate interrupts. This tells |
| 758 | * us if `event' is using such a PMC. |
| 759 | */ |
| 760 | static int is_limited_pmc(int pmcnum) |
| 761 | { |
| 762 | return (ppmu->flags & PPMU_LIMITED_PMC5_6) |
| 763 | && (pmcnum == 5 || pmcnum == 6); |
| 764 | } |
| 765 | |
Paul Mackerras | a8f90e9 | 2009-09-22 09:48:08 +1000 | [diff] [blame] | 766 | static void freeze_limited_counters(struct cpu_hw_events *cpuhw, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 767 | unsigned long pmc5, unsigned long pmc6) |
| 768 | { |
| 769 | struct perf_event *event; |
| 770 | u64 val, prev, delta; |
| 771 | int i; |
| 772 | |
| 773 | for (i = 0; i < cpuhw->n_limited; ++i) { |
Paul Mackerras | a8f90e9 | 2009-09-22 09:48:08 +1000 | [diff] [blame] | 774 | event = cpuhw->limited_counter[i]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 775 | if (!event->hw.idx) |
| 776 | continue; |
| 777 | val = (event->hw.idx == 5) ? pmc5 : pmc6; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 778 | prev = local64_read(&event->hw.prev_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 779 | event->hw.idx = 0; |
Eric B Munson | 86c74ab | 2011-04-15 08:12:30 +0000 | [diff] [blame] | 780 | delta = check_and_compute_delta(prev, val); |
| 781 | if (delta) |
| 782 | local64_add(delta, &event->count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 783 | } |
| 784 | } |
| 785 | |
Paul Mackerras | a8f90e9 | 2009-09-22 09:48:08 +1000 | [diff] [blame] | 786 | static void thaw_limited_counters(struct cpu_hw_events *cpuhw, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 787 | unsigned long pmc5, unsigned long pmc6) |
| 788 | { |
| 789 | struct perf_event *event; |
Eric B Munson | 86c74ab | 2011-04-15 08:12:30 +0000 | [diff] [blame] | 790 | u64 val, prev; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 791 | int i; |
| 792 | |
| 793 | for (i = 0; i < cpuhw->n_limited; ++i) { |
Paul Mackerras | a8f90e9 | 2009-09-22 09:48:08 +1000 | [diff] [blame] | 794 | event = cpuhw->limited_counter[i]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 795 | event->hw.idx = cpuhw->limited_hwidx[i]; |
| 796 | val = (event->hw.idx == 5) ? pmc5 : pmc6; |
Eric B Munson | 86c74ab | 2011-04-15 08:12:30 +0000 | [diff] [blame] | 797 | prev = local64_read(&event->hw.prev_count); |
| 798 | if (check_and_compute_delta(prev, val)) |
| 799 | local64_set(&event->hw.prev_count, val); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 800 | perf_event_update_userpage(event); |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | /* |
| 805 | * Since limited events don't respect the freeze conditions, we |
| 806 | * have to read them immediately after freezing or unfreezing the |
| 807 | * other events. We try to keep the values from the limited |
| 808 | * events as consistent as possible by keeping the delay (in |
| 809 | * cycles and instructions) between freezing/unfreezing and reading |
| 810 | * the limited events as small and consistent as possible. |
| 811 | * Therefore, if any limited events are in use, we read them |
| 812 | * both, and always in the same order, to minimize variability, |
| 813 | * and do it inside the same asm that writes MMCR0. |
| 814 | */ |
| 815 | static void write_mmcr0(struct cpu_hw_events *cpuhw, unsigned long mmcr0) |
| 816 | { |
| 817 | unsigned long pmc5, pmc6; |
| 818 | |
| 819 | if (!cpuhw->n_limited) { |
| 820 | mtspr(SPRN_MMCR0, mmcr0); |
| 821 | return; |
| 822 | } |
| 823 | |
| 824 | /* |
| 825 | * Write MMCR0, then read PMC5 and PMC6 immediately. |
| 826 | * To ensure we don't get a performance monitor interrupt |
| 827 | * between writing MMCR0 and freezing/thawing the limited |
| 828 | * events, we first write MMCR0 with the event overflow |
| 829 | * interrupt enable bits turned off. |
| 830 | */ |
| 831 | asm volatile("mtspr %3,%2; mfspr %0,%4; mfspr %1,%5" |
| 832 | : "=&r" (pmc5), "=&r" (pmc6) |
| 833 | : "r" (mmcr0 & ~(MMCR0_PMC1CE | MMCR0_PMCjCE)), |
| 834 | "i" (SPRN_MMCR0), |
| 835 | "i" (SPRN_PMC5), "i" (SPRN_PMC6)); |
| 836 | |
| 837 | if (mmcr0 & MMCR0_FC) |
Paul Mackerras | a8f90e9 | 2009-09-22 09:48:08 +1000 | [diff] [blame] | 838 | freeze_limited_counters(cpuhw, pmc5, pmc6); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 839 | else |
Paul Mackerras | a8f90e9 | 2009-09-22 09:48:08 +1000 | [diff] [blame] | 840 | thaw_limited_counters(cpuhw, pmc5, pmc6); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 841 | |
| 842 | /* |
| 843 | * Write the full MMCR0 including the event overflow interrupt |
| 844 | * enable bits, if necessary. |
| 845 | */ |
| 846 | if (mmcr0 & (MMCR0_PMC1CE | MMCR0_PMCjCE)) |
| 847 | mtspr(SPRN_MMCR0, mmcr0); |
| 848 | } |
| 849 | |
| 850 | /* |
| 851 | * Disable all events to prevent PMU interrupts and to allow |
| 852 | * events to be added or removed. |
| 853 | */ |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 854 | static void power_pmu_disable(struct pmu *pmu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 855 | { |
| 856 | struct cpu_hw_events *cpuhw; |
Michael Ellerman | 378a6ee | 2013-06-28 18:15:11 +1000 | [diff] [blame] | 857 | unsigned long flags, val; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 858 | |
| 859 | if (!ppmu) |
| 860 | return; |
| 861 | local_irq_save(flags); |
| 862 | cpuhw = &__get_cpu_var(cpu_hw_events); |
| 863 | |
| 864 | if (!cpuhw->disabled) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 865 | /* |
| 866 | * Check if we ever enabled the PMU on this cpu. |
| 867 | */ |
| 868 | if (!cpuhw->pmcs_enabled) { |
| 869 | ppc_enable_pmcs(); |
| 870 | cpuhw->pmcs_enabled = 1; |
| 871 | } |
| 872 | |
| 873 | /* |
Michael Ellerman | 7a7a41f | 2013-06-28 18:15:12 +1000 | [diff] [blame] | 874 | * Set the 'freeze counters' bit, clear PMAO/FC56. |
Michael Ellerman | 378a6ee | 2013-06-28 18:15:11 +1000 | [diff] [blame] | 875 | */ |
| 876 | val = mfspr(SPRN_MMCR0); |
| 877 | val |= MMCR0_FC; |
Michael Ellerman | 7a7a41f | 2013-06-28 18:15:12 +1000 | [diff] [blame] | 878 | val &= ~(MMCR0_PMAO | MMCR0_FC56); |
Michael Ellerman | 378a6ee | 2013-06-28 18:15:11 +1000 | [diff] [blame] | 879 | |
| 880 | /* |
| 881 | * The barrier is to make sure the mtspr has been |
| 882 | * executed and the PMU has frozen the events etc. |
| 883 | * before we return. |
| 884 | */ |
| 885 | write_mmcr0(cpuhw, val); |
| 886 | mb(); |
| 887 | |
| 888 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 889 | * Disable instruction sampling if it was enabled |
| 890 | */ |
| 891 | if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) { |
| 892 | mtspr(SPRN_MMCRA, |
| 893 | cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE); |
| 894 | mb(); |
| 895 | } |
| 896 | |
Michael Ellerman | 378a6ee | 2013-06-28 18:15:11 +1000 | [diff] [blame] | 897 | cpuhw->disabled = 1; |
| 898 | cpuhw->n_added = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 899 | } |
| 900 | local_irq_restore(flags); |
| 901 | } |
| 902 | |
| 903 | /* |
| 904 | * Re-enable all events if disable == 0. |
| 905 | * If we were previously disabled and events were added, then |
| 906 | * put the new config on the PMU. |
| 907 | */ |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 908 | static void power_pmu_enable(struct pmu *pmu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 909 | { |
| 910 | struct perf_event *event; |
| 911 | struct cpu_hw_events *cpuhw; |
| 912 | unsigned long flags; |
| 913 | long i; |
| 914 | unsigned long val; |
| 915 | s64 left; |
| 916 | unsigned int hwc_index[MAX_HWEVENTS]; |
| 917 | int n_lim; |
| 918 | int idx; |
| 919 | |
| 920 | if (!ppmu) |
| 921 | return; |
Michael Ellerman | 0a48843 | 2013-06-28 18:15:13 +1000 | [diff] [blame] | 922 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 923 | local_irq_save(flags); |
Michael Ellerman | 0a48843 | 2013-06-28 18:15:13 +1000 | [diff] [blame] | 924 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 925 | cpuhw = &__get_cpu_var(cpu_hw_events); |
Michael Ellerman | 0a48843 | 2013-06-28 18:15:13 +1000 | [diff] [blame] | 926 | if (!cpuhw->disabled) |
| 927 | goto out; |
| 928 | |
Michael Ellerman | 4ea355b | 2013-06-28 18:15:14 +1000 | [diff] [blame^] | 929 | if (cpuhw->n_events == 0) { |
| 930 | ppc_set_pmu_inuse(0); |
| 931 | goto out; |
| 932 | } |
| 933 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 934 | cpuhw->disabled = 0; |
| 935 | |
| 936 | /* |
| 937 | * If we didn't change anything, or only removed events, |
| 938 | * no need to recalculate MMCR* settings and reset the PMCs. |
| 939 | * Just reenable the PMU with the current MMCR* settings |
| 940 | * (possibly updated for removal of events). |
| 941 | */ |
| 942 | if (!cpuhw->n_added) { |
| 943 | mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE); |
| 944 | mtspr(SPRN_MMCR1, cpuhw->mmcr[1]); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 945 | goto out_enable; |
| 946 | } |
| 947 | |
| 948 | /* |
| 949 | * Compute MMCR* values for the new set of events |
| 950 | */ |
| 951 | if (ppmu->compute_mmcr(cpuhw->events, cpuhw->n_events, hwc_index, |
| 952 | cpuhw->mmcr)) { |
| 953 | /* shouldn't ever get here */ |
| 954 | printk(KERN_ERR "oops compute_mmcr failed\n"); |
| 955 | goto out; |
| 956 | } |
| 957 | |
| 958 | /* |
| 959 | * Add in MMCR0 freeze bits corresponding to the |
| 960 | * attr.exclude_* bits for the first event. |
| 961 | * We have already checked that all events have the |
| 962 | * same values for these bits as the first event. |
| 963 | */ |
| 964 | event = cpuhw->event[0]; |
| 965 | if (event->attr.exclude_user) |
| 966 | cpuhw->mmcr[0] |= MMCR0_FCP; |
| 967 | if (event->attr.exclude_kernel) |
| 968 | cpuhw->mmcr[0] |= freeze_events_kernel; |
| 969 | if (event->attr.exclude_hv) |
| 970 | cpuhw->mmcr[0] |= MMCR0_FCHV; |
| 971 | |
| 972 | /* |
| 973 | * Write the new configuration to MMCR* with the freeze |
| 974 | * bit set and set the hardware events to their initial values. |
| 975 | * Then unfreeze the events. |
| 976 | */ |
| 977 | ppc_set_pmu_inuse(1); |
| 978 | mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE); |
| 979 | mtspr(SPRN_MMCR1, cpuhw->mmcr[1]); |
| 980 | mtspr(SPRN_MMCR0, (cpuhw->mmcr[0] & ~(MMCR0_PMC1CE | MMCR0_PMCjCE)) |
| 981 | | MMCR0_FC); |
| 982 | |
| 983 | /* |
| 984 | * Read off any pre-existing events that need to move |
| 985 | * to another PMC. |
| 986 | */ |
| 987 | for (i = 0; i < cpuhw->n_events; ++i) { |
| 988 | event = cpuhw->event[i]; |
| 989 | if (event->hw.idx && event->hw.idx != hwc_index[i] + 1) { |
| 990 | power_pmu_read(event); |
| 991 | write_pmc(event->hw.idx, 0); |
| 992 | event->hw.idx = 0; |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | /* |
| 997 | * Initialize the PMCs for all the new and moved events. |
| 998 | */ |
| 999 | cpuhw->n_limited = n_lim = 0; |
| 1000 | for (i = 0; i < cpuhw->n_events; ++i) { |
| 1001 | event = cpuhw->event[i]; |
| 1002 | if (event->hw.idx) |
| 1003 | continue; |
| 1004 | idx = hwc_index[i] + 1; |
| 1005 | if (is_limited_pmc(idx)) { |
Paul Mackerras | a8f90e9 | 2009-09-22 09:48:08 +1000 | [diff] [blame] | 1006 | cpuhw->limited_counter[n_lim] = event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1007 | cpuhw->limited_hwidx[n_lim] = idx; |
| 1008 | ++n_lim; |
| 1009 | continue; |
| 1010 | } |
| 1011 | val = 0; |
| 1012 | if (event->hw.sample_period) { |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 1013 | left = local64_read(&event->hw.period_left); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1014 | if (left < 0x80000000L) |
| 1015 | val = 0x80000000L - left; |
| 1016 | } |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 1017 | local64_set(&event->hw.prev_count, val); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1018 | event->hw.idx = idx; |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1019 | if (event->hw.state & PERF_HES_STOPPED) |
| 1020 | val = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1021 | write_pmc(idx, val); |
| 1022 | perf_event_update_userpage(event); |
| 1023 | } |
| 1024 | cpuhw->n_limited = n_lim; |
| 1025 | cpuhw->mmcr[0] |= MMCR0_PMXE | MMCR0_FCECE; |
| 1026 | |
| 1027 | out_enable: |
| 1028 | mb(); |
| 1029 | write_mmcr0(cpuhw, cpuhw->mmcr[0]); |
| 1030 | |
| 1031 | /* |
| 1032 | * Enable instruction sampling if necessary |
| 1033 | */ |
| 1034 | if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) { |
| 1035 | mb(); |
| 1036 | mtspr(SPRN_MMCRA, cpuhw->mmcr[2]); |
| 1037 | } |
| 1038 | |
| 1039 | out: |
Anshuman Khandual | 3925f46 | 2013-04-22 19:42:44 +0000 | [diff] [blame] | 1040 | if (cpuhw->bhrb_users) |
| 1041 | ppmu->config_bhrb(cpuhw->bhrb_filter); |
| 1042 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1043 | local_irq_restore(flags); |
| 1044 | } |
| 1045 | |
| 1046 | static int collect_events(struct perf_event *group, int max_count, |
| 1047 | struct perf_event *ctrs[], u64 *events, |
| 1048 | unsigned int *flags) |
| 1049 | { |
| 1050 | int n = 0; |
| 1051 | struct perf_event *event; |
| 1052 | |
| 1053 | if (!is_software_event(group)) { |
| 1054 | if (n >= max_count) |
| 1055 | return -1; |
| 1056 | ctrs[n] = group; |
| 1057 | flags[n] = group->hw.event_base; |
| 1058 | events[n++] = group->hw.config; |
| 1059 | } |
Paul Mackerras | a8f90e9 | 2009-09-22 09:48:08 +1000 | [diff] [blame] | 1060 | list_for_each_entry(event, &group->sibling_list, group_entry) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1061 | if (!is_software_event(event) && |
| 1062 | event->state != PERF_EVENT_STATE_OFF) { |
| 1063 | if (n >= max_count) |
| 1064 | return -1; |
| 1065 | ctrs[n] = event; |
| 1066 | flags[n] = event->hw.event_base; |
| 1067 | events[n++] = event->hw.config; |
| 1068 | } |
| 1069 | } |
| 1070 | return n; |
| 1071 | } |
| 1072 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1073 | /* |
| 1074 | * Add a event to the PMU. |
| 1075 | * If all events are not already frozen, then we disable and |
| 1076 | * re-enable the PMU in order to get hw_perf_enable to do the |
| 1077 | * actual work of reconfiguring the PMU. |
| 1078 | */ |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1079 | static int power_pmu_add(struct perf_event *event, int ef_flags) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1080 | { |
| 1081 | struct cpu_hw_events *cpuhw; |
| 1082 | unsigned long flags; |
| 1083 | int n0; |
| 1084 | int ret = -EAGAIN; |
| 1085 | |
| 1086 | local_irq_save(flags); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 1087 | perf_pmu_disable(event->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1088 | |
| 1089 | /* |
| 1090 | * Add the event to the list (if there is room) |
| 1091 | * and check whether the total set is still feasible. |
| 1092 | */ |
| 1093 | cpuhw = &__get_cpu_var(cpu_hw_events); |
| 1094 | n0 = cpuhw->n_events; |
Paul Mackerras | a8f90e9 | 2009-09-22 09:48:08 +1000 | [diff] [blame] | 1095 | if (n0 >= ppmu->n_counter) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1096 | goto out; |
| 1097 | cpuhw->event[n0] = event; |
| 1098 | cpuhw->events[n0] = event->hw.config; |
| 1099 | cpuhw->flags[n0] = event->hw.event_base; |
Lin Ming | 8e6d557 | 2010-05-08 20:28:41 +1000 | [diff] [blame] | 1100 | |
sukadev@linux.vnet.ibm.com | f53d168 | 2013-01-24 13:25:23 +0000 | [diff] [blame] | 1101 | /* |
| 1102 | * This event may have been disabled/stopped in record_and_restart() |
| 1103 | * because we exceeded the ->event_limit. If re-starting the event, |
| 1104 | * clear the ->hw.state (STOPPED and UPTODATE flags), so the user |
| 1105 | * notification is re-enabled. |
| 1106 | */ |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1107 | if (!(ef_flags & PERF_EF_START)) |
| 1108 | event->hw.state = PERF_HES_STOPPED | PERF_HES_UPTODATE; |
sukadev@linux.vnet.ibm.com | f53d168 | 2013-01-24 13:25:23 +0000 | [diff] [blame] | 1109 | else |
| 1110 | event->hw.state = 0; |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1111 | |
Lin Ming | 8e6d557 | 2010-05-08 20:28:41 +1000 | [diff] [blame] | 1112 | /* |
| 1113 | * If group events scheduling transaction was started, |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1114 | * skip the schedulability test here, it will be performed |
Lin Ming | 8e6d557 | 2010-05-08 20:28:41 +1000 | [diff] [blame] | 1115 | * at commit time(->commit_txn) as a whole |
| 1116 | */ |
Peter Zijlstra | 8d2cacb | 2010-05-25 17:49:05 +0200 | [diff] [blame] | 1117 | if (cpuhw->group_flag & PERF_EVENT_TXN) |
Lin Ming | 8e6d557 | 2010-05-08 20:28:41 +1000 | [diff] [blame] | 1118 | goto nocheck; |
| 1119 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1120 | if (check_excludes(cpuhw->event, cpuhw->flags, n0, 1)) |
| 1121 | goto out; |
| 1122 | if (power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n0 + 1)) |
| 1123 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1124 | event->hw.config = cpuhw->events[n0]; |
Lin Ming | 8e6d557 | 2010-05-08 20:28:41 +1000 | [diff] [blame] | 1125 | |
| 1126 | nocheck: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1127 | ++cpuhw->n_events; |
| 1128 | ++cpuhw->n_added; |
| 1129 | |
| 1130 | ret = 0; |
| 1131 | out: |
Anshuman Khandual | 3925f46 | 2013-04-22 19:42:44 +0000 | [diff] [blame] | 1132 | if (has_branch_stack(event)) |
| 1133 | power_pmu_bhrb_enable(event); |
| 1134 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 1135 | perf_pmu_enable(event->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1136 | local_irq_restore(flags); |
| 1137 | return ret; |
| 1138 | } |
| 1139 | |
| 1140 | /* |
| 1141 | * Remove a event from the PMU. |
| 1142 | */ |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1143 | static void power_pmu_del(struct perf_event *event, int ef_flags) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1144 | { |
| 1145 | struct cpu_hw_events *cpuhw; |
| 1146 | long i; |
| 1147 | unsigned long flags; |
| 1148 | |
| 1149 | local_irq_save(flags); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 1150 | perf_pmu_disable(event->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1151 | |
| 1152 | power_pmu_read(event); |
| 1153 | |
| 1154 | cpuhw = &__get_cpu_var(cpu_hw_events); |
| 1155 | for (i = 0; i < cpuhw->n_events; ++i) { |
| 1156 | if (event == cpuhw->event[i]) { |
Matt Evans | 219a92a | 2010-07-05 17:36:32 +0000 | [diff] [blame] | 1157 | while (++i < cpuhw->n_events) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1158 | cpuhw->event[i-1] = cpuhw->event[i]; |
Matt Evans | 219a92a | 2010-07-05 17:36:32 +0000 | [diff] [blame] | 1159 | cpuhw->events[i-1] = cpuhw->events[i]; |
| 1160 | cpuhw->flags[i-1] = cpuhw->flags[i]; |
| 1161 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1162 | --cpuhw->n_events; |
| 1163 | ppmu->disable_pmc(event->hw.idx - 1, cpuhw->mmcr); |
| 1164 | if (event->hw.idx) { |
| 1165 | write_pmc(event->hw.idx, 0); |
| 1166 | event->hw.idx = 0; |
| 1167 | } |
| 1168 | perf_event_update_userpage(event); |
| 1169 | break; |
| 1170 | } |
| 1171 | } |
| 1172 | for (i = 0; i < cpuhw->n_limited; ++i) |
Paul Mackerras | a8f90e9 | 2009-09-22 09:48:08 +1000 | [diff] [blame] | 1173 | if (event == cpuhw->limited_counter[i]) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1174 | break; |
| 1175 | if (i < cpuhw->n_limited) { |
| 1176 | while (++i < cpuhw->n_limited) { |
Paul Mackerras | a8f90e9 | 2009-09-22 09:48:08 +1000 | [diff] [blame] | 1177 | cpuhw->limited_counter[i-1] = cpuhw->limited_counter[i]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1178 | cpuhw->limited_hwidx[i-1] = cpuhw->limited_hwidx[i]; |
| 1179 | } |
| 1180 | --cpuhw->n_limited; |
| 1181 | } |
| 1182 | if (cpuhw->n_events == 0) { |
| 1183 | /* disable exceptions if no events are running */ |
| 1184 | cpuhw->mmcr[0] &= ~(MMCR0_PMXE | MMCR0_FCECE); |
| 1185 | } |
| 1186 | |
Anshuman Khandual | 3925f46 | 2013-04-22 19:42:44 +0000 | [diff] [blame] | 1187 | if (has_branch_stack(event)) |
| 1188 | power_pmu_bhrb_disable(event); |
| 1189 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 1190 | perf_pmu_enable(event->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1191 | local_irq_restore(flags); |
| 1192 | } |
| 1193 | |
| 1194 | /* |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1195 | * POWER-PMU does not support disabling individual counters, hence |
| 1196 | * program their cycle counter to their max value and ignore the interrupts. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1197 | */ |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1198 | |
| 1199 | static void power_pmu_start(struct perf_event *event, int ef_flags) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1200 | { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1201 | unsigned long flags; |
| 1202 | s64 left; |
Anton Blanchard | 9a45a94 | 2012-02-15 18:48:22 +0000 | [diff] [blame] | 1203 | unsigned long val; |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1204 | |
| 1205 | if (!event->hw.idx || !event->hw.sample_period) |
| 1206 | return; |
| 1207 | |
| 1208 | if (!(event->hw.state & PERF_HES_STOPPED)) |
| 1209 | return; |
| 1210 | |
| 1211 | if (ef_flags & PERF_EF_RELOAD) |
| 1212 | WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE)); |
| 1213 | |
| 1214 | local_irq_save(flags); |
| 1215 | perf_pmu_disable(event->pmu); |
| 1216 | |
| 1217 | event->hw.state = 0; |
| 1218 | left = local64_read(&event->hw.period_left); |
Anton Blanchard | 9a45a94 | 2012-02-15 18:48:22 +0000 | [diff] [blame] | 1219 | |
| 1220 | val = 0; |
| 1221 | if (left < 0x80000000L) |
| 1222 | val = 0x80000000L - left; |
| 1223 | |
| 1224 | write_pmc(event->hw.idx, val); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1225 | |
| 1226 | perf_event_update_userpage(event); |
| 1227 | perf_pmu_enable(event->pmu); |
| 1228 | local_irq_restore(flags); |
| 1229 | } |
| 1230 | |
| 1231 | static void power_pmu_stop(struct perf_event *event, int ef_flags) |
| 1232 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1233 | unsigned long flags; |
| 1234 | |
| 1235 | if (!event->hw.idx || !event->hw.sample_period) |
| 1236 | return; |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1237 | |
| 1238 | if (event->hw.state & PERF_HES_STOPPED) |
| 1239 | return; |
| 1240 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1241 | local_irq_save(flags); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 1242 | perf_pmu_disable(event->pmu); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1243 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1244 | power_pmu_read(event); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1245 | event->hw.state |= PERF_HES_STOPPED | PERF_HES_UPTODATE; |
| 1246 | write_pmc(event->hw.idx, 0); |
| 1247 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1248 | perf_event_update_userpage(event); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 1249 | perf_pmu_enable(event->pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1250 | local_irq_restore(flags); |
| 1251 | } |
| 1252 | |
Lin Ming | 8e6d557 | 2010-05-08 20:28:41 +1000 | [diff] [blame] | 1253 | /* |
| 1254 | * Start group events scheduling transaction |
| 1255 | * Set the flag to make pmu::enable() not perform the |
| 1256 | * schedulability test, it will be performed at commit time |
| 1257 | */ |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 1258 | void power_pmu_start_txn(struct pmu *pmu) |
Lin Ming | 8e6d557 | 2010-05-08 20:28:41 +1000 | [diff] [blame] | 1259 | { |
| 1260 | struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events); |
| 1261 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 1262 | perf_pmu_disable(pmu); |
Peter Zijlstra | 8d2cacb | 2010-05-25 17:49:05 +0200 | [diff] [blame] | 1263 | cpuhw->group_flag |= PERF_EVENT_TXN; |
Lin Ming | 8e6d557 | 2010-05-08 20:28:41 +1000 | [diff] [blame] | 1264 | cpuhw->n_txn_start = cpuhw->n_events; |
| 1265 | } |
| 1266 | |
| 1267 | /* |
| 1268 | * Stop group events scheduling transaction |
| 1269 | * Clear the flag and pmu::enable() will perform the |
| 1270 | * schedulability test. |
| 1271 | */ |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 1272 | void power_pmu_cancel_txn(struct pmu *pmu) |
Lin Ming | 8e6d557 | 2010-05-08 20:28:41 +1000 | [diff] [blame] | 1273 | { |
| 1274 | struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events); |
| 1275 | |
Peter Zijlstra | 8d2cacb | 2010-05-25 17:49:05 +0200 | [diff] [blame] | 1276 | cpuhw->group_flag &= ~PERF_EVENT_TXN; |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 1277 | perf_pmu_enable(pmu); |
Lin Ming | 8e6d557 | 2010-05-08 20:28:41 +1000 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | /* |
| 1281 | * Commit group events scheduling transaction |
| 1282 | * Perform the group schedulability test as a whole |
| 1283 | * Return 0 if success |
| 1284 | */ |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 1285 | int power_pmu_commit_txn(struct pmu *pmu) |
Lin Ming | 8e6d557 | 2010-05-08 20:28:41 +1000 | [diff] [blame] | 1286 | { |
| 1287 | struct cpu_hw_events *cpuhw; |
| 1288 | long i, n; |
| 1289 | |
| 1290 | if (!ppmu) |
| 1291 | return -EAGAIN; |
| 1292 | cpuhw = &__get_cpu_var(cpu_hw_events); |
| 1293 | n = cpuhw->n_events; |
| 1294 | if (check_excludes(cpuhw->event, cpuhw->flags, 0, n)) |
| 1295 | return -EAGAIN; |
| 1296 | i = power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n); |
| 1297 | if (i < 0) |
| 1298 | return -EAGAIN; |
| 1299 | |
| 1300 | for (i = cpuhw->n_txn_start; i < n; ++i) |
| 1301 | cpuhw->event[i]->hw.config = cpuhw->events[i]; |
| 1302 | |
Peter Zijlstra | 8d2cacb | 2010-05-25 17:49:05 +0200 | [diff] [blame] | 1303 | cpuhw->group_flag &= ~PERF_EVENT_TXN; |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 1304 | perf_pmu_enable(pmu); |
Lin Ming | 8e6d557 | 2010-05-08 20:28:41 +1000 | [diff] [blame] | 1305 | return 0; |
| 1306 | } |
| 1307 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1308 | /* |
| 1309 | * Return 1 if we might be able to put event on a limited PMC, |
| 1310 | * or 0 if not. |
| 1311 | * A event can only go on a limited PMC if it counts something |
| 1312 | * that a limited PMC can count, doesn't require interrupts, and |
| 1313 | * doesn't exclude any processor mode. |
| 1314 | */ |
| 1315 | static int can_go_on_limited_pmc(struct perf_event *event, u64 ev, |
| 1316 | unsigned int flags) |
| 1317 | { |
| 1318 | int n; |
| 1319 | u64 alt[MAX_EVENT_ALTERNATIVES]; |
| 1320 | |
| 1321 | if (event->attr.exclude_user |
| 1322 | || event->attr.exclude_kernel |
| 1323 | || event->attr.exclude_hv |
| 1324 | || event->attr.sample_period) |
| 1325 | return 0; |
| 1326 | |
| 1327 | if (ppmu->limited_pmc_event(ev)) |
| 1328 | return 1; |
| 1329 | |
| 1330 | /* |
| 1331 | * The requested event_id isn't on a limited PMC already; |
| 1332 | * see if any alternative code goes on a limited PMC. |
| 1333 | */ |
| 1334 | if (!ppmu->get_alternatives) |
| 1335 | return 0; |
| 1336 | |
| 1337 | flags |= PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD; |
| 1338 | n = ppmu->get_alternatives(ev, flags, alt); |
| 1339 | |
| 1340 | return n > 0; |
| 1341 | } |
| 1342 | |
| 1343 | /* |
| 1344 | * Find an alternative event_id that goes on a normal PMC, if possible, |
| 1345 | * and return the event_id code, or 0 if there is no such alternative. |
| 1346 | * (Note: event_id code 0 is "don't count" on all machines.) |
| 1347 | */ |
| 1348 | static u64 normal_pmc_alternative(u64 ev, unsigned long flags) |
| 1349 | { |
| 1350 | u64 alt[MAX_EVENT_ALTERNATIVES]; |
| 1351 | int n; |
| 1352 | |
| 1353 | flags &= ~(PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD); |
| 1354 | n = ppmu->get_alternatives(ev, flags, alt); |
| 1355 | if (!n) |
| 1356 | return 0; |
| 1357 | return alt[0]; |
| 1358 | } |
| 1359 | |
| 1360 | /* Number of perf_events counting hardware events */ |
| 1361 | static atomic_t num_events; |
| 1362 | /* Used to avoid races in calling reserve/release_pmc_hardware */ |
| 1363 | static DEFINE_MUTEX(pmc_reserve_mutex); |
| 1364 | |
| 1365 | /* |
| 1366 | * Release the PMU if this is the last perf_event. |
| 1367 | */ |
| 1368 | static void hw_perf_event_destroy(struct perf_event *event) |
| 1369 | { |
| 1370 | if (!atomic_add_unless(&num_events, -1, 1)) { |
| 1371 | mutex_lock(&pmc_reserve_mutex); |
| 1372 | if (atomic_dec_return(&num_events) == 0) |
| 1373 | release_pmc_hardware(); |
| 1374 | mutex_unlock(&pmc_reserve_mutex); |
| 1375 | } |
| 1376 | } |
| 1377 | |
| 1378 | /* |
| 1379 | * Translate a generic cache event_id config to a raw event_id code. |
| 1380 | */ |
| 1381 | static int hw_perf_cache_event(u64 config, u64 *eventp) |
| 1382 | { |
| 1383 | unsigned long type, op, result; |
| 1384 | int ev; |
| 1385 | |
| 1386 | if (!ppmu->cache_events) |
| 1387 | return -EINVAL; |
| 1388 | |
| 1389 | /* unpack config */ |
| 1390 | type = config & 0xff; |
| 1391 | op = (config >> 8) & 0xff; |
| 1392 | result = (config >> 16) & 0xff; |
| 1393 | |
| 1394 | if (type >= PERF_COUNT_HW_CACHE_MAX || |
| 1395 | op >= PERF_COUNT_HW_CACHE_OP_MAX || |
| 1396 | result >= PERF_COUNT_HW_CACHE_RESULT_MAX) |
| 1397 | return -EINVAL; |
| 1398 | |
| 1399 | ev = (*ppmu->cache_events)[type][op][result]; |
| 1400 | if (ev == 0) |
| 1401 | return -EOPNOTSUPP; |
| 1402 | if (ev == -1) |
| 1403 | return -EINVAL; |
| 1404 | *eventp = ev; |
| 1405 | return 0; |
| 1406 | } |
| 1407 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1408 | static int power_pmu_event_init(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1409 | { |
| 1410 | u64 ev; |
| 1411 | unsigned long flags; |
| 1412 | struct perf_event *ctrs[MAX_HWEVENTS]; |
| 1413 | u64 events[MAX_HWEVENTS]; |
| 1414 | unsigned int cflags[MAX_HWEVENTS]; |
| 1415 | int n; |
| 1416 | int err; |
| 1417 | struct cpu_hw_events *cpuhw; |
| 1418 | |
| 1419 | if (!ppmu) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1420 | return -ENOENT; |
| 1421 | |
Anshuman Khandual | 3925f46 | 2013-04-22 19:42:44 +0000 | [diff] [blame] | 1422 | if (has_branch_stack(event)) { |
| 1423 | /* PMU has BHRB enabled */ |
| 1424 | if (!(ppmu->flags & PPMU_BHRB)) |
| 1425 | return -EOPNOTSUPP; |
| 1426 | } |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 1427 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1428 | switch (event->attr.type) { |
| 1429 | case PERF_TYPE_HARDWARE: |
| 1430 | ev = event->attr.config; |
| 1431 | if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1432 | return -EOPNOTSUPP; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1433 | ev = ppmu->generic_events[ev]; |
| 1434 | break; |
| 1435 | case PERF_TYPE_HW_CACHE: |
| 1436 | err = hw_perf_cache_event(event->attr.config, &ev); |
| 1437 | if (err) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1438 | return err; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1439 | break; |
| 1440 | case PERF_TYPE_RAW: |
| 1441 | ev = event->attr.config; |
| 1442 | break; |
| 1443 | default: |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1444 | return -ENOENT; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1445 | } |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1446 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1447 | event->hw.config_base = ev; |
| 1448 | event->hw.idx = 0; |
| 1449 | |
| 1450 | /* |
| 1451 | * If we are not running on a hypervisor, force the |
| 1452 | * exclude_hv bit to 0 so that we don't care what |
| 1453 | * the user set it to. |
| 1454 | */ |
| 1455 | if (!firmware_has_feature(FW_FEATURE_LPAR)) |
| 1456 | event->attr.exclude_hv = 0; |
| 1457 | |
| 1458 | /* |
| 1459 | * If this is a per-task event, then we can use |
| 1460 | * PM_RUN_* events interchangeably with their non RUN_* |
| 1461 | * equivalents, e.g. PM_RUN_CYC instead of PM_CYC. |
| 1462 | * XXX we should check if the task is an idle task. |
| 1463 | */ |
| 1464 | flags = 0; |
Paul Mackerras | 57fa721 | 2010-10-19 16:55:35 +1100 | [diff] [blame] | 1465 | if (event->attach_state & PERF_ATTACH_TASK) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1466 | flags |= PPMU_ONLY_COUNT_RUN; |
| 1467 | |
| 1468 | /* |
| 1469 | * If this machine has limited events, check whether this |
| 1470 | * event_id could go on a limited event. |
| 1471 | */ |
| 1472 | if (ppmu->flags & PPMU_LIMITED_PMC5_6) { |
| 1473 | if (can_go_on_limited_pmc(event, ev, flags)) { |
| 1474 | flags |= PPMU_LIMITED_PMC_OK; |
| 1475 | } else if (ppmu->limited_pmc_event(ev)) { |
| 1476 | /* |
| 1477 | * The requested event_id is on a limited PMC, |
| 1478 | * but we can't use a limited PMC; see if any |
| 1479 | * alternative goes on a normal PMC. |
| 1480 | */ |
| 1481 | ev = normal_pmc_alternative(ev, flags); |
| 1482 | if (!ev) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1483 | return -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | /* |
| 1488 | * If this is in a group, check if it can go on with all the |
| 1489 | * other hardware events in the group. We assume the event |
| 1490 | * hasn't been linked into its leader's sibling list at this point. |
| 1491 | */ |
| 1492 | n = 0; |
| 1493 | if (event->group_leader != event) { |
Paul Mackerras | a8f90e9 | 2009-09-22 09:48:08 +1000 | [diff] [blame] | 1494 | n = collect_events(event->group_leader, ppmu->n_counter - 1, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1495 | ctrs, events, cflags); |
| 1496 | if (n < 0) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1497 | return -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1498 | } |
| 1499 | events[n] = ev; |
| 1500 | ctrs[n] = event; |
| 1501 | cflags[n] = flags; |
| 1502 | if (check_excludes(ctrs, cflags, n, 1)) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1503 | return -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1504 | |
| 1505 | cpuhw = &get_cpu_var(cpu_hw_events); |
| 1506 | err = power_check_constraints(cpuhw, events, cflags, n + 1); |
Anshuman Khandual | 3925f46 | 2013-04-22 19:42:44 +0000 | [diff] [blame] | 1507 | |
| 1508 | if (has_branch_stack(event)) { |
| 1509 | cpuhw->bhrb_filter = ppmu->bhrb_filter_map( |
| 1510 | event->attr.branch_sample_type); |
| 1511 | |
| 1512 | if(cpuhw->bhrb_filter == -1) |
| 1513 | return -EOPNOTSUPP; |
| 1514 | } |
| 1515 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1516 | put_cpu_var(cpu_hw_events); |
| 1517 | if (err) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1518 | return -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1519 | |
| 1520 | event->hw.config = events[n]; |
| 1521 | event->hw.event_base = cflags[n]; |
| 1522 | event->hw.last_period = event->hw.sample_period; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 1523 | local64_set(&event->hw.period_left, event->hw.last_period); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1524 | |
| 1525 | /* |
| 1526 | * See if we need to reserve the PMU. |
| 1527 | * If no events are currently in use, then we have to take a |
| 1528 | * mutex to ensure that we don't race with another task doing |
| 1529 | * reserve_pmc_hardware or release_pmc_hardware. |
| 1530 | */ |
| 1531 | err = 0; |
| 1532 | if (!atomic_inc_not_zero(&num_events)) { |
| 1533 | mutex_lock(&pmc_reserve_mutex); |
| 1534 | if (atomic_read(&num_events) == 0 && |
| 1535 | reserve_pmc_hardware(perf_event_interrupt)) |
| 1536 | err = -EBUSY; |
| 1537 | else |
| 1538 | atomic_inc(&num_events); |
| 1539 | mutex_unlock(&pmc_reserve_mutex); |
| 1540 | } |
| 1541 | event->destroy = hw_perf_event_destroy; |
| 1542 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1543 | return err; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1544 | } |
| 1545 | |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 1546 | static int power_pmu_event_idx(struct perf_event *event) |
| 1547 | { |
| 1548 | return event->hw.idx; |
| 1549 | } |
| 1550 | |
Sukadev Bhattiprolu | 1c53a27 | 2013-01-22 22:24:54 -0800 | [diff] [blame] | 1551 | ssize_t power_events_sysfs_show(struct device *dev, |
| 1552 | struct device_attribute *attr, char *page) |
| 1553 | { |
| 1554 | struct perf_pmu_events_attr *pmu_attr; |
| 1555 | |
| 1556 | pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr); |
| 1557 | |
| 1558 | return sprintf(page, "event=0x%02llx\n", pmu_attr->id); |
| 1559 | } |
| 1560 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1561 | struct pmu power_pmu = { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1562 | .pmu_enable = power_pmu_enable, |
| 1563 | .pmu_disable = power_pmu_disable, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1564 | .event_init = power_pmu_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1565 | .add = power_pmu_add, |
| 1566 | .del = power_pmu_del, |
| 1567 | .start = power_pmu_start, |
| 1568 | .stop = power_pmu_stop, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1569 | .read = power_pmu_read, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1570 | .start_txn = power_pmu_start_txn, |
| 1571 | .cancel_txn = power_pmu_cancel_txn, |
| 1572 | .commit_txn = power_pmu_commit_txn, |
Peter Zijlstra | 35edc2a | 2011-11-20 20:36:02 +0100 | [diff] [blame] | 1573 | .event_idx = power_pmu_event_idx, |
Anshuman Khandual | 3925f46 | 2013-04-22 19:42:44 +0000 | [diff] [blame] | 1574 | .flush_branch_stack = power_pmu_flush_branch_stack, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1575 | }; |
| 1576 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1577 | /* |
Ingo Molnar | 57c0c15 | 2009-09-21 12:20:38 +0200 | [diff] [blame] | 1578 | * A counter has overflowed; update its count and record |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1579 | * things if requested. Note that interrupts are hard-disabled |
| 1580 | * here so there is no possibility of being interrupted. |
| 1581 | */ |
| 1582 | static void record_and_restart(struct perf_event *event, unsigned long val, |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 1583 | struct pt_regs *regs) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1584 | { |
| 1585 | u64 period = event->hw.sample_period; |
| 1586 | s64 prev, delta, left; |
| 1587 | int record = 0; |
| 1588 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1589 | if (event->hw.state & PERF_HES_STOPPED) { |
| 1590 | write_pmc(event->hw.idx, 0); |
| 1591 | return; |
| 1592 | } |
| 1593 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1594 | /* we don't have to worry about interrupts here */ |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 1595 | prev = local64_read(&event->hw.prev_count); |
Eric B Munson | 86c74ab | 2011-04-15 08:12:30 +0000 | [diff] [blame] | 1596 | delta = check_and_compute_delta(prev, val); |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 1597 | local64_add(delta, &event->count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1598 | |
| 1599 | /* |
| 1600 | * See if the total period for this event has expired, |
| 1601 | * and update for the next period. |
| 1602 | */ |
| 1603 | val = 0; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 1604 | left = local64_read(&event->hw.period_left) - delta; |
Michael Neuling | e13e895 | 2012-11-05 15:08:38 +0000 | [diff] [blame] | 1605 | if (delta == 0) |
| 1606 | left++; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1607 | if (period) { |
| 1608 | if (left <= 0) { |
| 1609 | left += period; |
| 1610 | if (left <= 0) |
| 1611 | left = period; |
sukadev@linux.vnet.ibm.com | e687883 | 2012-09-18 20:56:11 +0000 | [diff] [blame] | 1612 | record = siar_valid(regs); |
Anton Blanchard | 4bca770 | 2011-01-17 16:17:42 +1100 | [diff] [blame] | 1613 | event->hw.last_period = event->hw.sample_period; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1614 | } |
| 1615 | if (left < 0x80000000LL) |
| 1616 | val = 0x80000000LL - left; |
| 1617 | } |
| 1618 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1619 | write_pmc(event->hw.idx, val); |
| 1620 | local64_set(&event->hw.prev_count, val); |
| 1621 | local64_set(&event->hw.period_left, left); |
| 1622 | perf_event_update_userpage(event); |
| 1623 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1624 | /* |
| 1625 | * Finally record data if requested. |
| 1626 | */ |
| 1627 | if (record) { |
Peter Zijlstra | dc1d628 | 2010-03-03 15:55:04 +0100 | [diff] [blame] | 1628 | struct perf_sample_data data; |
| 1629 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 1630 | perf_sample_data_init(&data, ~0ULL, event->hw.last_period); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1631 | |
| 1632 | if (event->attr.sample_type & PERF_SAMPLE_ADDR) |
| 1633 | perf_get_data_addr(regs, &data.addr); |
| 1634 | |
Anshuman Khandual | 3925f46 | 2013-04-22 19:42:44 +0000 | [diff] [blame] | 1635 | if (event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK) { |
| 1636 | struct cpu_hw_events *cpuhw; |
| 1637 | cpuhw = &__get_cpu_var(cpu_hw_events); |
| 1638 | power_pmu_bhrb_read(cpuhw); |
| 1639 | data.br_stack = &cpuhw->bhrb_stack; |
| 1640 | } |
| 1641 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 1642 | if (perf_event_overflow(event, &data, regs)) |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1643 | power_pmu_stop(event, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1644 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1645 | } |
| 1646 | |
| 1647 | /* |
| 1648 | * Called from generic code to get the misc flags (i.e. processor mode) |
| 1649 | * for an event_id. |
| 1650 | */ |
| 1651 | unsigned long perf_misc_flags(struct pt_regs *regs) |
| 1652 | { |
| 1653 | u32 flags = perf_get_misc_flags(regs); |
| 1654 | |
| 1655 | if (flags) |
| 1656 | return flags; |
| 1657 | return user_mode(regs) ? PERF_RECORD_MISC_USER : |
| 1658 | PERF_RECORD_MISC_KERNEL; |
| 1659 | } |
| 1660 | |
| 1661 | /* |
| 1662 | * Called from generic code to get the instruction pointer |
| 1663 | * for an event_id. |
| 1664 | */ |
| 1665 | unsigned long perf_instruction_pointer(struct pt_regs *regs) |
| 1666 | { |
Michael Ellerman | 3390405 | 2013-04-25 19:28:25 +0000 | [diff] [blame] | 1667 | bool use_siar = regs_use_siar(regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1668 | |
sukadev@linux.vnet.ibm.com | e687883 | 2012-09-18 20:56:11 +0000 | [diff] [blame] | 1669 | if (use_siar && siar_valid(regs)) |
Anton Blanchard | 75382aa | 2012-06-26 01:01:36 +0000 | [diff] [blame] | 1670 | return mfspr(SPRN_SIAR) + perf_ip_adjust(regs); |
sukadev@linux.vnet.ibm.com | e687883 | 2012-09-18 20:56:11 +0000 | [diff] [blame] | 1671 | else if (use_siar) |
| 1672 | return 0; // no valid instruction pointer |
Anton Blanchard | 75382aa | 2012-06-26 01:01:36 +0000 | [diff] [blame] | 1673 | else |
Benjamin Herrenschmidt | 1ce447b | 2012-03-26 20:47:34 +0000 | [diff] [blame] | 1674 | return regs->nip; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1675 | } |
| 1676 | |
Michael Neuling | bc09c21 | 2012-11-05 15:53:54 +0000 | [diff] [blame] | 1677 | static bool pmc_overflow_power7(unsigned long val) |
Anton Blanchard | 0837e32 | 2011-03-09 14:38:42 +1100 | [diff] [blame] | 1678 | { |
Anton Blanchard | 0837e32 | 2011-03-09 14:38:42 +1100 | [diff] [blame] | 1679 | /* |
| 1680 | * Events on POWER7 can roll back if a speculative event doesn't |
| 1681 | * eventually complete. Unfortunately in some rare cases they will |
| 1682 | * raise a performance monitor exception. We need to catch this to |
| 1683 | * ensure we reset the PMC. In all cases the PMC will be 256 or less |
| 1684 | * cycles from overflow. |
| 1685 | * |
| 1686 | * We only do this if the first pass fails to find any overflowing |
| 1687 | * PMCs because a user might set a period of less than 256 and we |
| 1688 | * don't want to mistakenly reset them. |
| 1689 | */ |
Michael Neuling | bc09c21 | 2012-11-05 15:53:54 +0000 | [diff] [blame] | 1690 | if ((0x80000000 - val) <= 256) |
| 1691 | return true; |
| 1692 | |
| 1693 | return false; |
| 1694 | } |
| 1695 | |
| 1696 | static bool pmc_overflow(unsigned long val) |
| 1697 | { |
| 1698 | if ((int)val < 0) |
Anton Blanchard | 0837e32 | 2011-03-09 14:38:42 +1100 | [diff] [blame] | 1699 | return true; |
| 1700 | |
| 1701 | return false; |
| 1702 | } |
| 1703 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1704 | /* |
| 1705 | * Performance monitor interrupt stuff |
| 1706 | */ |
| 1707 | static void perf_event_interrupt(struct pt_regs *regs) |
| 1708 | { |
Michael Neuling | bc09c21 | 2012-11-05 15:53:54 +0000 | [diff] [blame] | 1709 | int i, j; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1710 | struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events); |
| 1711 | struct perf_event *event; |
Michael Neuling | bc09c21 | 2012-11-05 15:53:54 +0000 | [diff] [blame] | 1712 | unsigned long val[8]; |
| 1713 | int found, active; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1714 | int nmi; |
| 1715 | |
| 1716 | if (cpuhw->n_limited) |
Paul Mackerras | a8f90e9 | 2009-09-22 09:48:08 +1000 | [diff] [blame] | 1717 | freeze_limited_counters(cpuhw, mfspr(SPRN_PMC5), |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1718 | mfspr(SPRN_PMC6)); |
| 1719 | |
| 1720 | perf_read_regs(regs); |
| 1721 | |
| 1722 | nmi = perf_intr_is_nmi(regs); |
| 1723 | if (nmi) |
| 1724 | nmi_enter(); |
| 1725 | else |
| 1726 | irq_enter(); |
| 1727 | |
Michael Neuling | bc09c21 | 2012-11-05 15:53:54 +0000 | [diff] [blame] | 1728 | /* Read all the PMCs since we'll need them a bunch of times */ |
| 1729 | for (i = 0; i < ppmu->n_counter; ++i) |
| 1730 | val[i] = read_pmc(i + 1); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1731 | |
Michael Neuling | bc09c21 | 2012-11-05 15:53:54 +0000 | [diff] [blame] | 1732 | /* Try to find what caused the IRQ */ |
| 1733 | found = 0; |
| 1734 | for (i = 0; i < ppmu->n_counter; ++i) { |
| 1735 | if (!pmc_overflow(val[i])) |
| 1736 | continue; |
| 1737 | if (is_limited_pmc(i + 1)) |
| 1738 | continue; /* these won't generate IRQs */ |
| 1739 | /* |
| 1740 | * We've found one that's overflowed. For active |
| 1741 | * counters we need to log this. For inactive |
| 1742 | * counters, we need to reset it anyway |
| 1743 | */ |
| 1744 | found = 1; |
| 1745 | active = 0; |
| 1746 | for (j = 0; j < cpuhw->n_events; ++j) { |
| 1747 | event = cpuhw->event[j]; |
| 1748 | if (event->hw.idx == (i + 1)) { |
| 1749 | active = 1; |
| 1750 | record_and_restart(event, val[i], regs); |
| 1751 | break; |
| 1752 | } |
| 1753 | } |
| 1754 | if (!active) |
| 1755 | /* reset non active counters that have overflowed */ |
| 1756 | write_pmc(i + 1, 0); |
| 1757 | } |
| 1758 | if (!found && pvr_version_is(PVR_POWER7)) { |
| 1759 | /* check active counters for special buggy p7 overflow */ |
| 1760 | for (i = 0; i < cpuhw->n_events; ++i) { |
| 1761 | event = cpuhw->event[i]; |
| 1762 | if (!event->hw.idx || is_limited_pmc(event->hw.idx)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1763 | continue; |
Michael Neuling | bc09c21 | 2012-11-05 15:53:54 +0000 | [diff] [blame] | 1764 | if (pmc_overflow_power7(val[event->hw.idx - 1])) { |
| 1765 | /* event has overflowed in a buggy way*/ |
| 1766 | found = 1; |
| 1767 | record_and_restart(event, |
| 1768 | val[event->hw.idx - 1], |
| 1769 | regs); |
| 1770 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1771 | } |
| 1772 | } |
Michael Ellerman | 6772faa | 2013-06-05 17:58:20 +0000 | [diff] [blame] | 1773 | if (!found && !nmi && printk_ratelimit()) |
Michael Neuling | bc09c21 | 2012-11-05 15:53:54 +0000 | [diff] [blame] | 1774 | printk(KERN_WARNING "Can't find PMC that caused IRQ\n"); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1775 | |
| 1776 | /* |
| 1777 | * Reset MMCR0 to its normal value. This will set PMXE and |
Ingo Molnar | 57c0c15 | 2009-09-21 12:20:38 +0200 | [diff] [blame] | 1778 | * clear FC (freeze counters) and PMAO (perf mon alert occurred) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1779 | * and thus allow interrupts to occur again. |
| 1780 | * XXX might want to use MSR.PM to keep the events frozen until |
| 1781 | * we get back out of this interrupt. |
| 1782 | */ |
| 1783 | write_mmcr0(cpuhw, cpuhw->mmcr[0]); |
| 1784 | |
| 1785 | if (nmi) |
| 1786 | nmi_exit(); |
| 1787 | else |
| 1788 | irq_exit(); |
| 1789 | } |
| 1790 | |
Peter Zijlstra | 3f6da39 | 2010-03-05 13:01:18 +0100 | [diff] [blame] | 1791 | static void power_pmu_setup(int cpu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1792 | { |
| 1793 | struct cpu_hw_events *cpuhw = &per_cpu(cpu_hw_events, cpu); |
| 1794 | |
| 1795 | if (!ppmu) |
| 1796 | return; |
| 1797 | memset(cpuhw, 0, sizeof(*cpuhw)); |
| 1798 | cpuhw->mmcr[0] = MMCR0_FC; |
| 1799 | } |
| 1800 | |
Paul Gortmaker | 061d19f | 2013-06-24 15:30:09 -0400 | [diff] [blame] | 1801 | static int |
Peter Zijlstra | 85cfabb | 2010-03-11 13:06:56 +0100 | [diff] [blame] | 1802 | power_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu) |
Peter Zijlstra | 3f6da39 | 2010-03-05 13:01:18 +0100 | [diff] [blame] | 1803 | { |
| 1804 | unsigned int cpu = (long)hcpu; |
| 1805 | |
| 1806 | switch (action & ~CPU_TASKS_FROZEN) { |
| 1807 | case CPU_UP_PREPARE: |
| 1808 | power_pmu_setup(cpu); |
| 1809 | break; |
| 1810 | |
| 1811 | default: |
| 1812 | break; |
| 1813 | } |
| 1814 | |
| 1815 | return NOTIFY_OK; |
| 1816 | } |
| 1817 | |
Paul Gortmaker | 061d19f | 2013-06-24 15:30:09 -0400 | [diff] [blame] | 1818 | int register_power_pmu(struct power_pmu *pmu) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1819 | { |
| 1820 | if (ppmu) |
| 1821 | return -EBUSY; /* something's already registered */ |
| 1822 | |
| 1823 | ppmu = pmu; |
| 1824 | pr_info("%s performance monitor hardware support registered\n", |
| 1825 | pmu->name); |
| 1826 | |
Sukadev Bhattiprolu | 1c53a27 | 2013-01-22 22:24:54 -0800 | [diff] [blame] | 1827 | power_pmu.attr_groups = ppmu->attr_groups; |
| 1828 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1829 | #ifdef MSR_HV |
| 1830 | /* |
| 1831 | * Use FCHV to ignore kernel events if MSR.HV is set. |
| 1832 | */ |
| 1833 | if (mfmsr() & MSR_HV) |
| 1834 | freeze_events_kernel = MMCR0_FCHV; |
| 1835 | #endif /* CONFIG_PPC64 */ |
| 1836 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 1837 | perf_pmu_register(&power_pmu, "cpu", PERF_TYPE_RAW); |
Peter Zijlstra | 3f6da39 | 2010-03-05 13:01:18 +0100 | [diff] [blame] | 1838 | perf_cpu_notifier(power_pmu_notifier); |
| 1839 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1840 | return 0; |
| 1841 | } |