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