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