| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Performance event support - powerpc architecture code |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * 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 | 6 | */ |
|---|
| 11 | 7 | #include <linux/kernel.h> |
|---|
| 12 | 8 | #include <linux/sched.h> |
|---|
| 9 | +#include <linux/sched/clock.h> |
|---|
| 13 | 10 | #include <linux/perf_event.h> |
|---|
| 14 | 11 | #include <linux/percpu.h> |
|---|
| 15 | 12 | #include <linux/hardirq.h> |
|---|
| .. | .. |
|---|
| 20 | 17 | #include <asm/firmware.h> |
|---|
| 21 | 18 | #include <asm/ptrace.h> |
|---|
| 22 | 19 | #include <asm/code-patching.h> |
|---|
| 20 | + |
|---|
| 21 | +#ifdef CONFIG_PPC64 |
|---|
| 22 | +#include "internal.h" |
|---|
| 23 | +#endif |
|---|
| 23 | 24 | |
|---|
| 24 | 25 | #define BHRB_MAX_ENTRIES 32 |
|---|
| 25 | 26 | #define BHRB_TARGET 0x0000000000000002 |
|---|
| .. | .. |
|---|
| 36 | 37 | struct perf_event *event[MAX_HWEVENTS]; |
|---|
| 37 | 38 | u64 events[MAX_HWEVENTS]; |
|---|
| 38 | 39 | unsigned int flags[MAX_HWEVENTS]; |
|---|
| 39 | | - /* |
|---|
| 40 | | - * The order of the MMCR array is: |
|---|
| 41 | | - * - 64-bit, MMCR0, MMCR1, MMCRA, MMCR2 |
|---|
| 42 | | - * - 32-bit, MMCR0, MMCR1, MMCR2 |
|---|
| 43 | | - */ |
|---|
| 44 | | - unsigned long mmcr[4]; |
|---|
| 40 | + struct mmcr_regs mmcr; |
|---|
| 45 | 41 | struct perf_event *limited_counter[MAX_LIMITED_HWCOUNTERS]; |
|---|
| 46 | 42 | u8 limited_hwidx[MAX_LIMITED_HWCOUNTERS]; |
|---|
| 47 | 43 | u64 alternatives[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES]; |
|---|
| .. | .. |
|---|
| 76 | 72 | /* |
|---|
| 77 | 73 | * 32-bit doesn't have MMCRA but does have an MMCR2, |
|---|
| 78 | 74 | * and a few other names are different. |
|---|
| 75 | + * Also 32-bit doesn't have MMCR3, SIER2 and SIER3. |
|---|
| 76 | + * Define them as zero knowing that any code path accessing |
|---|
| 77 | + * these registers (via mtspr/mfspr) are done under ppmu flag |
|---|
| 78 | + * check for PPMU_ARCH_31 and we will not enter that code path |
|---|
| 79 | + * for 32-bit. |
|---|
| 79 | 80 | */ |
|---|
| 80 | 81 | #ifdef CONFIG_PPC32 |
|---|
| 81 | 82 | |
|---|
| .. | .. |
|---|
| 89 | 90 | #define MMCR0_PMCC_U6 0 |
|---|
| 90 | 91 | |
|---|
| 91 | 92 | #define SPRN_MMCRA SPRN_MMCR2 |
|---|
| 93 | +#define SPRN_MMCR3 0 |
|---|
| 94 | +#define SPRN_SIER2 0 |
|---|
| 95 | +#define SPRN_SIER3 0 |
|---|
| 92 | 96 | #define MMCRA_SAMPLE_ENABLE 0 |
|---|
| 97 | +#define MMCRA_BHRB_DISABLE 0 |
|---|
| 98 | +#define MMCR0_PMCCEXT 0 |
|---|
| 93 | 99 | |
|---|
| 94 | 100 | static inline unsigned long perf_ip_adjust(struct pt_regs *regs) |
|---|
| 95 | 101 | { |
|---|
| .. | .. |
|---|
| 104 | 110 | { |
|---|
| 105 | 111 | regs->result = 0; |
|---|
| 106 | 112 | } |
|---|
| 107 | | -static inline int perf_intr_is_nmi(struct pt_regs *regs) |
|---|
| 108 | | -{ |
|---|
| 109 | | - return 0; |
|---|
| 110 | | -} |
|---|
| 111 | 113 | |
|---|
| 112 | 114 | static inline int siar_valid(struct pt_regs *regs) |
|---|
| 113 | 115 | { |
|---|
| .. | .. |
|---|
| 120 | 122 | static void ebb_switch_out(unsigned long mmcr0) { } |
|---|
| 121 | 123 | static unsigned long ebb_switch_in(bool ebb, struct cpu_hw_events *cpuhw) |
|---|
| 122 | 124 | { |
|---|
| 123 | | - return cpuhw->mmcr[0]; |
|---|
| 125 | + return cpuhw->mmcr.mmcr0; |
|---|
| 124 | 126 | } |
|---|
| 125 | 127 | |
|---|
| 126 | 128 | static inline void power_pmu_bhrb_enable(struct perf_event *event) {} |
|---|
| .. | .. |
|---|
| 129 | 131 | static inline void power_pmu_bhrb_read(struct perf_event *event, struct cpu_hw_events *cpuhw) {} |
|---|
| 130 | 132 | static void pmao_restore_workaround(bool ebb) { } |
|---|
| 131 | 133 | #endif /* CONFIG_PPC32 */ |
|---|
| 134 | + |
|---|
| 135 | +bool is_sier_available(void) |
|---|
| 136 | +{ |
|---|
| 137 | + if (!ppmu) |
|---|
| 138 | + return false; |
|---|
| 139 | + |
|---|
| 140 | + if (ppmu->flags & PPMU_HAS_SIER) |
|---|
| 141 | + return true; |
|---|
| 142 | + |
|---|
| 143 | + return false; |
|---|
| 144 | +} |
|---|
| 132 | 145 | |
|---|
| 133 | 146 | static bool regs_use_siar(struct pt_regs *regs) |
|---|
| 134 | 147 | { |
|---|
| .. | .. |
|---|
| 195 | 208 | if (!(mmcra & MMCRA_SAMPLE_ENABLE) || sdar_valid) |
|---|
| 196 | 209 | *addrp = mfspr(SPRN_SDAR); |
|---|
| 197 | 210 | |
|---|
| 198 | | - if (is_kernel_addr(mfspr(SPRN_SDAR)) && perf_allow_kernel(&event->attr) != 0) |
|---|
| 211 | + if (is_kernel_addr(mfspr(SPRN_SDAR)) && event->attr.exclude_kernel) |
|---|
| 199 | 212 | *addrp = 0; |
|---|
| 200 | 213 | } |
|---|
| 201 | 214 | |
|---|
| .. | .. |
|---|
| 316 | 329 | } |
|---|
| 317 | 330 | |
|---|
| 318 | 331 | /* |
|---|
| 319 | | - * If interrupts were soft-disabled when a PMU interrupt occurs, treat |
|---|
| 320 | | - * it as an NMI. |
|---|
| 321 | | - */ |
|---|
| 322 | | -static inline int perf_intr_is_nmi(struct pt_regs *regs) |
|---|
| 323 | | -{ |
|---|
| 324 | | - return (regs->softe & IRQS_DISABLED); |
|---|
| 325 | | -} |
|---|
| 326 | | - |
|---|
| 327 | | -/* |
|---|
| 328 | 332 | * On processors like P7+ that have the SIAR-Valid bit, marked instructions |
|---|
| 329 | 333 | * must be sampled only if the SIAR-valid bit is set. |
|---|
| 330 | 334 | * |
|---|
| .. | .. |
|---|
| 406 | 410 | static __u64 power_pmu_bhrb_to(u64 addr) |
|---|
| 407 | 411 | { |
|---|
| 408 | 412 | unsigned int instr; |
|---|
| 409 | | - int ret; |
|---|
| 410 | 413 | __u64 target; |
|---|
| 411 | 414 | |
|---|
| 412 | 415 | if (is_kernel_addr(addr)) { |
|---|
| 413 | | - if (probe_kernel_read(&instr, (void *)addr, sizeof(instr))) |
|---|
| 416 | + if (copy_from_kernel_nofault(&instr, (void *)addr, |
|---|
| 417 | + sizeof(instr))) |
|---|
| 414 | 418 | return 0; |
|---|
| 415 | 419 | |
|---|
| 416 | | - return branch_target(&instr); |
|---|
| 420 | + return branch_target((struct ppc_inst *)&instr); |
|---|
| 417 | 421 | } |
|---|
| 418 | 422 | |
|---|
| 419 | 423 | /* Userspace: need copy instruction here then translate it */ |
|---|
| 420 | | - pagefault_disable(); |
|---|
| 421 | | - ret = __get_user_inatomic(instr, (unsigned int __user *)addr); |
|---|
| 422 | | - if (ret) { |
|---|
| 423 | | - pagefault_enable(); |
|---|
| 424 | + if (copy_from_user_nofault(&instr, (unsigned int __user *)addr, |
|---|
| 425 | + sizeof(instr))) |
|---|
| 424 | 426 | return 0; |
|---|
| 425 | | - } |
|---|
| 426 | | - pagefault_enable(); |
|---|
| 427 | 427 | |
|---|
| 428 | | - target = branch_target(&instr); |
|---|
| 428 | + target = branch_target((struct ppc_inst *)&instr); |
|---|
| 429 | 429 | if ((!target) || (instr & BRANCH_ABSOLUTE)) |
|---|
| 430 | 430 | return target; |
|---|
| 431 | 431 | |
|---|
| .. | .. |
|---|
| 461 | 461 | * addresses at this point. Check the privileges before |
|---|
| 462 | 462 | * exporting it to userspace (avoid exposure of regions |
|---|
| 463 | 463 | * where we could have speculative execution) |
|---|
| 464 | + * Incase of ISA v3.1, BHRB will capture only user-space |
|---|
| 465 | + * addresses, hence include a check before filtering code |
|---|
| 464 | 466 | */ |
|---|
| 465 | | - if (is_kernel_addr(addr) && perf_allow_kernel(&event->attr) != 0) |
|---|
| 467 | + if (!(ppmu->flags & PPMU_ARCH_31) && |
|---|
| 468 | + is_kernel_addr(addr) && event->attr.exclude_kernel) |
|---|
| 466 | 469 | continue; |
|---|
| 467 | 470 | |
|---|
| 468 | 471 | /* Branches are read most recent first (ie. mfbhrb 0 is |
|---|
| .. | .. |
|---|
| 515 | 518 | } |
|---|
| 516 | 519 | } |
|---|
| 517 | 520 | cpuhw->bhrb_stack.nr = u_index; |
|---|
| 521 | + cpuhw->bhrb_stack.hw_idx = -1ULL; |
|---|
| 518 | 522 | return; |
|---|
| 519 | 523 | } |
|---|
| 520 | 524 | |
|---|
| .. | .. |
|---|
| 580 | 584 | current->thread.sdar = mfspr(SPRN_SDAR); |
|---|
| 581 | 585 | current->thread.mmcr0 = mmcr0 & MMCR0_USER_MASK; |
|---|
| 582 | 586 | current->thread.mmcr2 = mfspr(SPRN_MMCR2) & MMCR2_USER_MASK; |
|---|
| 587 | + if (ppmu->flags & PPMU_ARCH_31) { |
|---|
| 588 | + current->thread.mmcr3 = mfspr(SPRN_MMCR3); |
|---|
| 589 | + current->thread.sier2 = mfspr(SPRN_SIER2); |
|---|
| 590 | + current->thread.sier3 = mfspr(SPRN_SIER3); |
|---|
| 591 | + } |
|---|
| 583 | 592 | } |
|---|
| 584 | 593 | |
|---|
| 585 | 594 | static unsigned long ebb_switch_in(bool ebb, struct cpu_hw_events *cpuhw) |
|---|
| 586 | 595 | { |
|---|
| 587 | | - unsigned long mmcr0 = cpuhw->mmcr[0]; |
|---|
| 596 | + unsigned long mmcr0 = cpuhw->mmcr.mmcr0; |
|---|
| 588 | 597 | |
|---|
| 589 | 598 | if (!ebb) |
|---|
| 590 | 599 | goto out; |
|---|
| .. | .. |
|---|
| 618 | 627 | * unfreeze counters, it should not set exclude_xxx in its events and |
|---|
| 619 | 628 | * instead manage the MMCR2 entirely by itself. |
|---|
| 620 | 629 | */ |
|---|
| 621 | | - mtspr(SPRN_MMCR2, cpuhw->mmcr[3] | current->thread.mmcr2); |
|---|
| 630 | + mtspr(SPRN_MMCR2, cpuhw->mmcr.mmcr2 | current->thread.mmcr2); |
|---|
| 631 | + |
|---|
| 632 | + if (ppmu->flags & PPMU_ARCH_31) { |
|---|
| 633 | + mtspr(SPRN_MMCR3, current->thread.mmcr3); |
|---|
| 634 | + mtspr(SPRN_SIER2, current->thread.sier2); |
|---|
| 635 | + mtspr(SPRN_SIER3, current->thread.sier3); |
|---|
| 636 | + } |
|---|
| 622 | 637 | out: |
|---|
| 623 | 638 | return mmcr0; |
|---|
| 624 | 639 | } |
|---|
| .. | .. |
|---|
| 790 | 805 | } |
|---|
| 791 | 806 | } |
|---|
| 792 | 807 | |
|---|
| 808 | +static int any_pmc_overflown(struct cpu_hw_events *cpuhw) |
|---|
| 809 | +{ |
|---|
| 810 | + int i, idx; |
|---|
| 811 | + |
|---|
| 812 | + for (i = 0; i < cpuhw->n_events; i++) { |
|---|
| 813 | + idx = cpuhw->event[i]->hw.idx; |
|---|
| 814 | + if ((idx) && ((int)read_pmc(idx) < 0)) |
|---|
| 815 | + return idx; |
|---|
| 816 | + } |
|---|
| 817 | + |
|---|
| 818 | + return 0; |
|---|
| 819 | +} |
|---|
| 820 | + |
|---|
| 793 | 821 | /* Called from sysrq_handle_showregs() */ |
|---|
| 794 | 822 | void perf_event_print_debug(void) |
|---|
| 795 | 823 | { |
|---|
| .. | .. |
|---|
| 839 | 867 | pr_info("EBBRR: %016lx BESCR: %016lx\n", |
|---|
| 840 | 868 | mfspr(SPRN_EBBRR), mfspr(SPRN_BESCR)); |
|---|
| 841 | 869 | } |
|---|
| 870 | + |
|---|
| 871 | + if (ppmu->flags & PPMU_ARCH_31) { |
|---|
| 872 | + pr_info("MMCR3: %016lx SIER2: %016lx SIER3: %016lx\n", |
|---|
| 873 | + mfspr(SPRN_MMCR3), mfspr(SPRN_SIER2), mfspr(SPRN_SIER3)); |
|---|
| 874 | + } |
|---|
| 842 | 875 | #endif |
|---|
| 843 | 876 | pr_info("SIAR: %016lx SDAR: %016lx SIER: %016lx\n", |
|---|
| 844 | 877 | mfspr(SPRN_SIAR), sdar, sier); |
|---|
| .. | .. |
|---|
| 862 | 895 | int i, j; |
|---|
| 863 | 896 | unsigned long addf = ppmu->add_fields; |
|---|
| 864 | 897 | unsigned long tadd = ppmu->test_adder; |
|---|
| 898 | + unsigned long grp_mask = ppmu->group_constraint_mask; |
|---|
| 899 | + unsigned long grp_val = ppmu->group_constraint_val; |
|---|
| 865 | 900 | |
|---|
| 866 | 901 | if (n_ev > ppmu->n_counter) |
|---|
| 867 | 902 | return -1; |
|---|
| .. | .. |
|---|
| 882 | 917 | for (i = 0; i < n_ev; ++i) { |
|---|
| 883 | 918 | nv = (value | cpuhw->avalues[i][0]) + |
|---|
| 884 | 919 | (value & cpuhw->avalues[i][0] & addf); |
|---|
| 885 | | - if ((((nv + tadd) ^ value) & mask) != 0 || |
|---|
| 886 | | - (((nv + tadd) ^ cpuhw->avalues[i][0]) & |
|---|
| 887 | | - cpuhw->amasks[i][0]) != 0) |
|---|
| 920 | + |
|---|
| 921 | + if (((((nv + tadd) ^ value) & mask) & (~grp_mask)) != 0) |
|---|
| 888 | 922 | break; |
|---|
| 923 | + |
|---|
| 924 | + if (((((nv + tadd) ^ cpuhw->avalues[i][0]) & cpuhw->amasks[i][0]) |
|---|
| 925 | + & (~grp_mask)) != 0) |
|---|
| 926 | + break; |
|---|
| 927 | + |
|---|
| 889 | 928 | value = nv; |
|---|
| 890 | 929 | mask |= cpuhw->amasks[i][0]; |
|---|
| 891 | 930 | } |
|---|
| 892 | | - if (i == n_ev) |
|---|
| 893 | | - return 0; /* all OK */ |
|---|
| 931 | + if (i == n_ev) { |
|---|
| 932 | + if ((value & mask & grp_mask) != (mask & grp_val)) |
|---|
| 933 | + return -1; |
|---|
| 934 | + else |
|---|
| 935 | + return 0; /* all OK */ |
|---|
| 936 | + } |
|---|
| 894 | 937 | |
|---|
| 895 | 938 | /* doesn't work, gather alternatives... */ |
|---|
| 896 | 939 | if (!ppmu->get_alternatives) |
|---|
| .. | .. |
|---|
| 1180 | 1223 | static void power_pmu_disable(struct pmu *pmu) |
|---|
| 1181 | 1224 | { |
|---|
| 1182 | 1225 | struct cpu_hw_events *cpuhw; |
|---|
| 1183 | | - unsigned long flags, mmcr0, val; |
|---|
| 1226 | + unsigned long flags, mmcr0, val, mmcra; |
|---|
| 1184 | 1227 | |
|---|
| 1185 | 1228 | if (!ppmu) |
|---|
| 1186 | 1229 | return; |
|---|
| .. | .. |
|---|
| 1198 | 1241 | |
|---|
| 1199 | 1242 | /* |
|---|
| 1200 | 1243 | * Set the 'freeze counters' bit, clear EBE/BHRBA/PMCC/PMAO/FC56 |
|---|
| 1244 | + * Also clear PMXE to disable PMI's getting triggered in some |
|---|
| 1245 | + * corner cases during PMU disable. |
|---|
| 1201 | 1246 | */ |
|---|
| 1202 | 1247 | val = mmcr0 = mfspr(SPRN_MMCR0); |
|---|
| 1203 | 1248 | val |= MMCR0_FC; |
|---|
| 1204 | 1249 | val &= ~(MMCR0_EBE | MMCR0_BHRBA | MMCR0_PMCC | MMCR0_PMAO | |
|---|
| 1205 | | - MMCR0_FC56); |
|---|
| 1250 | + MMCR0_PMXE | MMCR0_FC56); |
|---|
| 1251 | + /* Set mmcr0 PMCCEXT for p10 */ |
|---|
| 1252 | + if (ppmu->flags & PPMU_ARCH_31) |
|---|
| 1253 | + val |= MMCR0_PMCCEXT; |
|---|
| 1206 | 1254 | |
|---|
| 1207 | 1255 | /* |
|---|
| 1208 | 1256 | * The barrier is to make sure the mtspr has been |
|---|
| .. | .. |
|---|
| 1214 | 1262 | isync(); |
|---|
| 1215 | 1263 | |
|---|
| 1216 | 1264 | /* |
|---|
| 1265 | + * Some corner cases could clear the PMU counter overflow |
|---|
| 1266 | + * while a masked PMI is pending. One such case is when |
|---|
| 1267 | + * a PMI happens during interrupt replay and perf counter |
|---|
| 1268 | + * values are cleared by PMU callbacks before replay. |
|---|
| 1269 | + * |
|---|
| 1270 | + * Disable the interrupt by clearing the paca bit for PMI |
|---|
| 1271 | + * since we are disabling the PMU now. Otherwise provide a |
|---|
| 1272 | + * warning if there is PMI pending, but no counter is found |
|---|
| 1273 | + * overflown. |
|---|
| 1274 | + * |
|---|
| 1275 | + * Since power_pmu_disable runs under local_irq_save, it |
|---|
| 1276 | + * could happen that code hits a PMC overflow without PMI |
|---|
| 1277 | + * pending in paca. Hence only clear PMI pending if it was |
|---|
| 1278 | + * set. |
|---|
| 1279 | + * |
|---|
| 1280 | + * If a PMI is pending, then MSR[EE] must be disabled (because |
|---|
| 1281 | + * the masked PMI handler disabling EE). So it is safe to |
|---|
| 1282 | + * call clear_pmi_irq_pending(). |
|---|
| 1283 | + */ |
|---|
| 1284 | + if (pmi_irq_pending()) |
|---|
| 1285 | + clear_pmi_irq_pending(); |
|---|
| 1286 | + |
|---|
| 1287 | + val = mmcra = cpuhw->mmcr.mmcra; |
|---|
| 1288 | + |
|---|
| 1289 | + /* |
|---|
| 1217 | 1290 | * Disable instruction sampling if it was enabled |
|---|
| 1218 | 1291 | */ |
|---|
| 1219 | | - if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) { |
|---|
| 1220 | | - mtspr(SPRN_MMCRA, |
|---|
| 1221 | | - cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE); |
|---|
| 1292 | + if (cpuhw->mmcr.mmcra & MMCRA_SAMPLE_ENABLE) |
|---|
| 1293 | + val &= ~MMCRA_SAMPLE_ENABLE; |
|---|
| 1294 | + |
|---|
| 1295 | + /* Disable BHRB via mmcra (BHRBRD) for p10 */ |
|---|
| 1296 | + if (ppmu->flags & PPMU_ARCH_31) |
|---|
| 1297 | + val |= MMCRA_BHRB_DISABLE; |
|---|
| 1298 | + |
|---|
| 1299 | + /* |
|---|
| 1300 | + * Write SPRN_MMCRA if mmcra has either disabled |
|---|
| 1301 | + * instruction sampling or BHRB. |
|---|
| 1302 | + */ |
|---|
| 1303 | + if (val != mmcra) { |
|---|
| 1304 | + mtspr(SPRN_MMCRA, mmcra); |
|---|
| 1222 | 1305 | mb(); |
|---|
| 1223 | 1306 | isync(); |
|---|
| 1224 | 1307 | } |
|---|
| .. | .. |
|---|
| 1292 | 1375 | * (possibly updated for removal of events). |
|---|
| 1293 | 1376 | */ |
|---|
| 1294 | 1377 | if (!cpuhw->n_added) { |
|---|
| 1295 | | - mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE); |
|---|
| 1296 | | - mtspr(SPRN_MMCR1, cpuhw->mmcr[1]); |
|---|
| 1378 | + /* |
|---|
| 1379 | + * If there is any active event with an overflown PMC |
|---|
| 1380 | + * value, set back PACA_IRQ_PMI which would have been |
|---|
| 1381 | + * cleared in power_pmu_disable(). |
|---|
| 1382 | + */ |
|---|
| 1383 | + hard_irq_disable(); |
|---|
| 1384 | + if (any_pmc_overflown(cpuhw)) |
|---|
| 1385 | + set_pmi_irq_pending(); |
|---|
| 1386 | + |
|---|
| 1387 | + mtspr(SPRN_MMCRA, cpuhw->mmcr.mmcra & ~MMCRA_SAMPLE_ENABLE); |
|---|
| 1388 | + mtspr(SPRN_MMCR1, cpuhw->mmcr.mmcr1); |
|---|
| 1389 | + if (ppmu->flags & PPMU_ARCH_31) |
|---|
| 1390 | + mtspr(SPRN_MMCR3, cpuhw->mmcr.mmcr3); |
|---|
| 1297 | 1391 | goto out_enable; |
|---|
| 1298 | 1392 | } |
|---|
| 1299 | 1393 | |
|---|
| 1300 | 1394 | /* |
|---|
| 1301 | 1395 | * Clear all MMCR settings and recompute them for the new set of events. |
|---|
| 1302 | 1396 | */ |
|---|
| 1303 | | - memset(cpuhw->mmcr, 0, sizeof(cpuhw->mmcr)); |
|---|
| 1397 | + memset(&cpuhw->mmcr, 0, sizeof(cpuhw->mmcr)); |
|---|
| 1304 | 1398 | |
|---|
| 1305 | 1399 | if (ppmu->compute_mmcr(cpuhw->events, cpuhw->n_events, hwc_index, |
|---|
| 1306 | | - cpuhw->mmcr, cpuhw->event)) { |
|---|
| 1400 | + &cpuhw->mmcr, cpuhw->event)) { |
|---|
| 1307 | 1401 | /* shouldn't ever get here */ |
|---|
| 1308 | 1402 | printk(KERN_ERR "oops compute_mmcr failed\n"); |
|---|
| 1309 | 1403 | goto out; |
|---|
| .. | .. |
|---|
| 1317 | 1411 | */ |
|---|
| 1318 | 1412 | event = cpuhw->event[0]; |
|---|
| 1319 | 1413 | if (event->attr.exclude_user) |
|---|
| 1320 | | - cpuhw->mmcr[0] |= MMCR0_FCP; |
|---|
| 1414 | + cpuhw->mmcr.mmcr0 |= MMCR0_FCP; |
|---|
| 1321 | 1415 | if (event->attr.exclude_kernel) |
|---|
| 1322 | | - cpuhw->mmcr[0] |= freeze_events_kernel; |
|---|
| 1416 | + cpuhw->mmcr.mmcr0 |= freeze_events_kernel; |
|---|
| 1323 | 1417 | if (event->attr.exclude_hv) |
|---|
| 1324 | | - cpuhw->mmcr[0] |= MMCR0_FCHV; |
|---|
| 1418 | + cpuhw->mmcr.mmcr0 |= MMCR0_FCHV; |
|---|
| 1325 | 1419 | } |
|---|
| 1326 | 1420 | |
|---|
| 1327 | 1421 | /* |
|---|
| .. | .. |
|---|
| 1330 | 1424 | * Then unfreeze the events. |
|---|
| 1331 | 1425 | */ |
|---|
| 1332 | 1426 | ppc_set_pmu_inuse(1); |
|---|
| 1333 | | - mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE); |
|---|
| 1334 | | - mtspr(SPRN_MMCR1, cpuhw->mmcr[1]); |
|---|
| 1335 | | - mtspr(SPRN_MMCR0, (cpuhw->mmcr[0] & ~(MMCR0_PMC1CE | MMCR0_PMCjCE)) |
|---|
| 1427 | + mtspr(SPRN_MMCRA, cpuhw->mmcr.mmcra & ~MMCRA_SAMPLE_ENABLE); |
|---|
| 1428 | + mtspr(SPRN_MMCR1, cpuhw->mmcr.mmcr1); |
|---|
| 1429 | + mtspr(SPRN_MMCR0, (cpuhw->mmcr.mmcr0 & ~(MMCR0_PMC1CE | MMCR0_PMCjCE)) |
|---|
| 1336 | 1430 | | MMCR0_FC); |
|---|
| 1337 | 1431 | if (ppmu->flags & PPMU_ARCH_207S) |
|---|
| 1338 | | - mtspr(SPRN_MMCR2, cpuhw->mmcr[3]); |
|---|
| 1432 | + mtspr(SPRN_MMCR2, cpuhw->mmcr.mmcr2); |
|---|
| 1433 | + |
|---|
| 1434 | + if (ppmu->flags & PPMU_ARCH_31) |
|---|
| 1435 | + mtspr(SPRN_MMCR3, cpuhw->mmcr.mmcr3); |
|---|
| 1339 | 1436 | |
|---|
| 1340 | 1437 | /* |
|---|
| 1341 | 1438 | * Read off any pre-existing events that need to move |
|---|
| .. | .. |
|---|
| 1386 | 1483 | perf_event_update_userpage(event); |
|---|
| 1387 | 1484 | } |
|---|
| 1388 | 1485 | cpuhw->n_limited = n_lim; |
|---|
| 1389 | | - cpuhw->mmcr[0] |= MMCR0_PMXE | MMCR0_FCECE; |
|---|
| 1486 | + cpuhw->mmcr.mmcr0 |= MMCR0_PMXE | MMCR0_FCECE; |
|---|
| 1390 | 1487 | |
|---|
| 1391 | 1488 | out_enable: |
|---|
| 1392 | 1489 | pmao_restore_workaround(ebb); |
|---|
| .. | .. |
|---|
| 1402 | 1499 | /* |
|---|
| 1403 | 1500 | * Enable instruction sampling if necessary |
|---|
| 1404 | 1501 | */ |
|---|
| 1405 | | - if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) { |
|---|
| 1502 | + if (cpuhw->mmcr.mmcra & MMCRA_SAMPLE_ENABLE) { |
|---|
| 1406 | 1503 | mb(); |
|---|
| 1407 | | - mtspr(SPRN_MMCRA, cpuhw->mmcr[2]); |
|---|
| 1504 | + mtspr(SPRN_MMCRA, cpuhw->mmcr.mmcra); |
|---|
| 1408 | 1505 | } |
|---|
| 1409 | 1506 | |
|---|
| 1410 | 1507 | out: |
|---|
| .. | .. |
|---|
| 1501 | 1598 | ret = 0; |
|---|
| 1502 | 1599 | out: |
|---|
| 1503 | 1600 | if (has_branch_stack(event)) { |
|---|
| 1504 | | - power_pmu_bhrb_enable(event); |
|---|
| 1505 | | - cpuhw->bhrb_filter = ppmu->bhrb_filter_map( |
|---|
| 1506 | | - event->attr.branch_sample_type); |
|---|
| 1601 | + u64 bhrb_filter = -1; |
|---|
| 1602 | + |
|---|
| 1603 | + if (ppmu->bhrb_filter_map) |
|---|
| 1604 | + bhrb_filter = ppmu->bhrb_filter_map( |
|---|
| 1605 | + event->attr.branch_sample_type); |
|---|
| 1606 | + |
|---|
| 1607 | + if (bhrb_filter != -1) { |
|---|
| 1608 | + cpuhw->bhrb_filter = bhrb_filter; |
|---|
| 1609 | + power_pmu_bhrb_enable(event); |
|---|
| 1610 | + } |
|---|
| 1507 | 1611 | } |
|---|
| 1508 | 1612 | |
|---|
| 1509 | 1613 | perf_pmu_enable(event->pmu); |
|---|
| .. | .. |
|---|
| 1534 | 1638 | cpuhw->flags[i-1] = cpuhw->flags[i]; |
|---|
| 1535 | 1639 | } |
|---|
| 1536 | 1640 | --cpuhw->n_events; |
|---|
| 1537 | | - ppmu->disable_pmc(event->hw.idx - 1, cpuhw->mmcr); |
|---|
| 1641 | + ppmu->disable_pmc(event->hw.idx - 1, &cpuhw->mmcr); |
|---|
| 1538 | 1642 | if (event->hw.idx) { |
|---|
| 1539 | 1643 | write_pmc(event->hw.idx, 0); |
|---|
| 1540 | 1644 | event->hw.idx = 0; |
|---|
| .. | .. |
|---|
| 1555 | 1659 | } |
|---|
| 1556 | 1660 | if (cpuhw->n_events == 0) { |
|---|
| 1557 | 1661 | /* disable exceptions if no events are running */ |
|---|
| 1558 | | - cpuhw->mmcr[0] &= ~(MMCR0_PMXE | MMCR0_FCECE); |
|---|
| 1662 | + cpuhw->mmcr.mmcr0 &= ~(MMCR0_PMXE | MMCR0_FCECE); |
|---|
| 1559 | 1663 | } |
|---|
| 1560 | 1664 | |
|---|
| 1561 | 1665 | if (has_branch_stack(event)) |
|---|
| .. | .. |
|---|
| 1779 | 1883 | static int hw_perf_cache_event(u64 config, u64 *eventp) |
|---|
| 1780 | 1884 | { |
|---|
| 1781 | 1885 | unsigned long type, op, result; |
|---|
| 1782 | | - int ev; |
|---|
| 1886 | + u64 ev; |
|---|
| 1783 | 1887 | |
|---|
| 1784 | 1888 | if (!ppmu->cache_events) |
|---|
| 1785 | 1889 | return -EINVAL; |
|---|
| .. | .. |
|---|
| 1818 | 1922 | static int power_pmu_event_init(struct perf_event *event) |
|---|
| 1819 | 1923 | { |
|---|
| 1820 | 1924 | u64 ev; |
|---|
| 1821 | | - unsigned long flags; |
|---|
| 1925 | + unsigned long flags, irq_flags; |
|---|
| 1822 | 1926 | struct perf_event *ctrs[MAX_HWEVENTS]; |
|---|
| 1823 | 1927 | u64 events[MAX_HWEVENTS]; |
|---|
| 1824 | 1928 | unsigned int cflags[MAX_HWEVENTS]; |
|---|
| 1825 | 1929 | int n; |
|---|
| 1826 | 1930 | int err; |
|---|
| 1827 | 1931 | struct cpu_hw_events *cpuhw; |
|---|
| 1828 | | - u64 bhrb_filter; |
|---|
| 1829 | 1932 | |
|---|
| 1830 | 1933 | if (!ppmu) |
|---|
| 1831 | 1934 | return -ENOENT; |
|---|
| .. | .. |
|---|
| 1927 | 2030 | if (check_excludes(ctrs, cflags, n, 1)) |
|---|
| 1928 | 2031 | return -EINVAL; |
|---|
| 1929 | 2032 | |
|---|
| 1930 | | - cpuhw = &get_cpu_var(cpu_hw_events); |
|---|
| 2033 | + local_irq_save(irq_flags); |
|---|
| 2034 | + cpuhw = this_cpu_ptr(&cpu_hw_events); |
|---|
| 2035 | + |
|---|
| 1931 | 2036 | err = power_check_constraints(cpuhw, events, cflags, n + 1); |
|---|
| 1932 | 2037 | |
|---|
| 1933 | 2038 | if (has_branch_stack(event)) { |
|---|
| 1934 | | - bhrb_filter = ppmu->bhrb_filter_map( |
|---|
| 2039 | + u64 bhrb_filter = -1; |
|---|
| 2040 | + |
|---|
| 2041 | + if (ppmu->bhrb_filter_map) |
|---|
| 2042 | + bhrb_filter = ppmu->bhrb_filter_map( |
|---|
| 1935 | 2043 | event->attr.branch_sample_type); |
|---|
| 1936 | 2044 | |
|---|
| 1937 | 2045 | if (bhrb_filter == -1) { |
|---|
| 1938 | | - put_cpu_var(cpu_hw_events); |
|---|
| 2046 | + local_irq_restore(irq_flags); |
|---|
| 1939 | 2047 | return -EOPNOTSUPP; |
|---|
| 1940 | 2048 | } |
|---|
| 1941 | 2049 | cpuhw->bhrb_filter = bhrb_filter; |
|---|
| 1942 | 2050 | } |
|---|
| 1943 | 2051 | |
|---|
| 1944 | | - put_cpu_var(cpu_hw_events); |
|---|
| 2052 | + local_irq_restore(irq_flags); |
|---|
| 1945 | 2053 | if (err) |
|---|
| 1946 | 2054 | return -EINVAL; |
|---|
| 1947 | 2055 | |
|---|
| .. | .. |
|---|
| 2173 | 2281 | /* |
|---|
| 2174 | 2282 | * Performance monitor interrupt stuff |
|---|
| 2175 | 2283 | */ |
|---|
| 2176 | | -static void perf_event_interrupt(struct pt_regs *regs) |
|---|
| 2284 | +static void __perf_event_interrupt(struct pt_regs *regs) |
|---|
| 2177 | 2285 | { |
|---|
| 2178 | 2286 | int i, j; |
|---|
| 2179 | 2287 | struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events); |
|---|
| 2180 | 2288 | struct perf_event *event; |
|---|
| 2181 | 2289 | unsigned long val[8]; |
|---|
| 2182 | 2290 | int found, active; |
|---|
| 2183 | | - int nmi; |
|---|
| 2184 | 2291 | |
|---|
| 2185 | 2292 | if (cpuhw->n_limited) |
|---|
| 2186 | 2293 | freeze_limited_counters(cpuhw, mfspr(SPRN_PMC5), |
|---|
| 2187 | 2294 | mfspr(SPRN_PMC6)); |
|---|
| 2188 | 2295 | |
|---|
| 2189 | 2296 | perf_read_regs(regs); |
|---|
| 2190 | | - |
|---|
| 2191 | | - nmi = perf_intr_is_nmi(regs); |
|---|
| 2192 | | - if (nmi) |
|---|
| 2193 | | - nmi_enter(); |
|---|
| 2194 | | - else |
|---|
| 2195 | | - irq_enter(); |
|---|
| 2196 | 2297 | |
|---|
| 2197 | 2298 | /* Read all the PMCs since we'll need them a bunch of times */ |
|---|
| 2198 | 2299 | for (i = 0; i < ppmu->n_counter; ++i) |
|---|
| .. | .. |
|---|
| 2220 | 2321 | break; |
|---|
| 2221 | 2322 | } |
|---|
| 2222 | 2323 | } |
|---|
| 2324 | + |
|---|
| 2325 | + /* |
|---|
| 2326 | + * Clear PACA_IRQ_PMI in case it was set by |
|---|
| 2327 | + * set_pmi_irq_pending() when PMU was enabled |
|---|
| 2328 | + * after accounting for interrupts. |
|---|
| 2329 | + */ |
|---|
| 2330 | + clear_pmi_irq_pending(); |
|---|
| 2331 | + |
|---|
| 2223 | 2332 | if (!active) |
|---|
| 2224 | 2333 | /* reset non active counters that have overflowed */ |
|---|
| 2225 | 2334 | write_pmc(i + 1, 0); |
|---|
| .. | .. |
|---|
| 2239 | 2348 | } |
|---|
| 2240 | 2349 | } |
|---|
| 2241 | 2350 | } |
|---|
| 2242 | | - if (!found && !nmi && printk_ratelimit()) |
|---|
| 2243 | | - printk(KERN_WARNING "Can't find PMC that caused IRQ\n"); |
|---|
| 2351 | + |
|---|
| 2352 | + /* |
|---|
| 2353 | + * During system wide profling or while specific CPU is monitored for an |
|---|
| 2354 | + * event, some corner cases could cause PMC to overflow in idle path. This |
|---|
| 2355 | + * will trigger a PMI after waking up from idle. Since counter values are _not_ |
|---|
| 2356 | + * saved/restored in idle path, can lead to below "Can't find PMC" message. |
|---|
| 2357 | + */ |
|---|
| 2358 | + if (unlikely(!found) && !arch_irq_disabled_regs(regs)) |
|---|
| 2359 | + printk_ratelimited(KERN_WARNING "Can't find PMC that caused IRQ\n"); |
|---|
| 2244 | 2360 | |
|---|
| 2245 | 2361 | /* |
|---|
| 2246 | 2362 | * Reset MMCR0 to its normal value. This will set PMXE and |
|---|
| .. | .. |
|---|
| 2249 | 2365 | * XXX might want to use MSR.PM to keep the events frozen until |
|---|
| 2250 | 2366 | * we get back out of this interrupt. |
|---|
| 2251 | 2367 | */ |
|---|
| 2252 | | - write_mmcr0(cpuhw, cpuhw->mmcr[0]); |
|---|
| 2368 | + write_mmcr0(cpuhw, cpuhw->mmcr.mmcr0); |
|---|
| 2369 | +} |
|---|
| 2253 | 2370 | |
|---|
| 2254 | | - if (nmi) |
|---|
| 2255 | | - nmi_exit(); |
|---|
| 2256 | | - else |
|---|
| 2257 | | - irq_exit(); |
|---|
| 2371 | +static void perf_event_interrupt(struct pt_regs *regs) |
|---|
| 2372 | +{ |
|---|
| 2373 | + u64 start_clock = sched_clock(); |
|---|
| 2374 | + |
|---|
| 2375 | + __perf_event_interrupt(regs); |
|---|
| 2376 | + perf_sample_event_took(sched_clock() - start_clock); |
|---|
| 2258 | 2377 | } |
|---|
| 2259 | 2378 | |
|---|
| 2260 | 2379 | static int power_pmu_prepare_cpu(unsigned int cpu) |
|---|
| .. | .. |
|---|
| 2263 | 2382 | |
|---|
| 2264 | 2383 | if (ppmu) { |
|---|
| 2265 | 2384 | memset(cpuhw, 0, sizeof(*cpuhw)); |
|---|
| 2266 | | - cpuhw->mmcr[0] = MMCR0_FC; |
|---|
| 2385 | + cpuhw->mmcr.mmcr0 = MMCR0_FC; |
|---|
| 2267 | 2386 | } |
|---|
| 2268 | 2387 | return 0; |
|---|
| 2269 | 2388 | } |
|---|
| .. | .. |
|---|
| 2278 | 2397 | pmu->name); |
|---|
| 2279 | 2398 | |
|---|
| 2280 | 2399 | power_pmu.attr_groups = ppmu->attr_groups; |
|---|
| 2400 | + power_pmu.capabilities |= (ppmu->capabilities & PERF_PMU_CAP_EXTENDED_REGS); |
|---|
| 2281 | 2401 | |
|---|
| 2282 | 2402 | #ifdef MSR_HV |
|---|
| 2283 | 2403 | /* |
|---|
| .. | .. |
|---|
| 2292 | 2412 | power_pmu_prepare_cpu, NULL); |
|---|
| 2293 | 2413 | return 0; |
|---|
| 2294 | 2414 | } |
|---|
| 2415 | + |
|---|
| 2416 | +#ifdef CONFIG_PPC64 |
|---|
| 2417 | +static int __init init_ppc64_pmu(void) |
|---|
| 2418 | +{ |
|---|
| 2419 | + /* run through all the pmu drivers one at a time */ |
|---|
| 2420 | + if (!init_power5_pmu()) |
|---|
| 2421 | + return 0; |
|---|
| 2422 | + else if (!init_power5p_pmu()) |
|---|
| 2423 | + return 0; |
|---|
| 2424 | + else if (!init_power6_pmu()) |
|---|
| 2425 | + return 0; |
|---|
| 2426 | + else if (!init_power7_pmu()) |
|---|
| 2427 | + return 0; |
|---|
| 2428 | + else if (!init_power8_pmu()) |
|---|
| 2429 | + return 0; |
|---|
| 2430 | + else if (!init_power9_pmu()) |
|---|
| 2431 | + return 0; |
|---|
| 2432 | + else if (!init_power10_pmu()) |
|---|
| 2433 | + return 0; |
|---|
| 2434 | + else if (!init_ppc970_pmu()) |
|---|
| 2435 | + return 0; |
|---|
| 2436 | + else |
|---|
| 2437 | + return init_generic_compat_pmu(); |
|---|
| 2438 | +} |
|---|
| 2439 | +early_initcall(init_ppc64_pmu); |
|---|
| 2440 | +#endif |
|---|