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